From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8B4753B3C12 for ; Mon, 8 Jun 2026 08:36:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780907822; cv=none; b=FzTklu71PaSyC41Frf7RnwZ5oJFGj8feRrlbx1ABCS4hBMK9hCU+MvGwfLuVXkzBtDJ1OeEk/pkaIYmuBOpKIvN1v3Pg2AwvuQxOlP2iKcB7AA2aSTIOfmbmvT3RCw2t+3WLrCN9jkR+5woa7KhphBagu1+bWeMNZOhwrCsqSc0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780907822; c=relaxed/simple; bh=y+61YvUymd++YcmeYOOWPNpu5ycFdBgEhoiyVksZMFs=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=HrNW7Zg25nMZw/QXRn8p8Xhy6PcptUuPYhJDQA7zdQX1/PHMGHU1JH3aWtuu6pXIlkO9iFWOFFu/rGesE6NeDOQ5noeXHs7K9DpEAtjN+pe2w1fEBbNuUV93WeVEL7RbhNdpmWE3TVYWuU8oefaLElj0zEO4HtNSKUMrTvIzpC4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=BneHsa1B; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=T+sWV4YW; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="BneHsa1B"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="T+sWV4YW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780907815; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=AR0svr1Wd/OcNae1i4toNU59Y7b0yC0WCO1bghbpkVI=; b=BneHsa1BpvEcXMOMe/1Pt681Ii5Lv7M41cUyVd8Iqkzn789l/WCNGXmYHQmnXfuQB5S9/u 4rUFUuh6zZMk4ojqqZTVuOKvYscOl33kolTYIKuKq80SaELj+t+ugxiI7yb9FZ2AtlS8Y2 6MVGq9UCcyUUVHxYDFkaxzR2grrSXY8= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-88-Zs3lGsGRPzqjWgewi3ArCQ-1; Mon, 08 Jun 2026 04:36:53 -0400 X-MC-Unique: Zs3lGsGRPzqjWgewi3ArCQ-1 X-Mimecast-MFC-AGG-ID: Zs3lGsGRPzqjWgewi3ArCQ_1780907812 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-490bde3d239so31373385e9.1 for ; Mon, 08 Jun 2026 01:36:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780907812; x=1781512612; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=AR0svr1Wd/OcNae1i4toNU59Y7b0yC0WCO1bghbpkVI=; b=T+sWV4YWb8mvzDZzbqOeNKoOnAvRVGld8RoxVec7glzwwSX52AJFpzeE+f8lmKbahz m6lQcz14aeJjysJjmsq8LM3kC8HRBthIA7LtKFU9dqtfKD2IQWucdErDNVXgfHDsRBiq 7KnYajrGnYXwDFybc7PU4SnOxROHXX4zTfCTQ8V3dKUGFsxcRQbEiDBncWZIJj9nGiju yJQIvau4PuCw08GFTZPHLw4hd/wu7fJEotIZyKd1NeajdnfnYkTMrE08vasEefSS25XN jij2yS+QgMf1U4XLRD1wpYHPnR0blQyReB1zYuBqlWwZ7eSPHtlIO4VC3gvjkQMEx4yy c/5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780907812; x=1781512612; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AR0svr1Wd/OcNae1i4toNU59Y7b0yC0WCO1bghbpkVI=; b=LN94RTCjO3v5OzwQ/zZGHdt8cSiDLsf2zCRBbpGoGDM4kKyNSjFqwU1qqhdp96HwT+ IaDso2qL4bNuPgNo3NOhn8AfSpfRKAi+Z8TS/wCF0N7dr/382lJqoi1saIuUUbVPewHi WNvSbsmOBCT33WA6G6EoaIVXUSE20vRV8g6rkX10hmKgIAhH6TpMBvYNrxHmLYmlEfcG ppXc9vw9fSP6KNaZ8ABRQTp2gAa9xGm5S5fZBcmw6/i7YGaDUizv2qtEg4iLeHRHgJxA t6n4U0EnnU0dBOwzX+zw0LqeewgE+WkWRAYVd5z1rNyVuwQjwniVpXBd11rWFrkic04O xyMA== X-Gm-Message-State: AOJu0Yy6l4Gr22A1JMxs+MNcdR4KiA/VBq+fIuVK1vw7H+svDd3uni7n FxgzvZbl7LOjZfPVqFwOV1jQhFNkerW1OMfVKUol8LPeQq76QDKCHcYdC4E/KWIrH4fFqwJo8Cz 0lbuQoFL4VmV97LA6pXKhi+l4l2RBi56D2KxuO5Ez0FkqgzEHeKaR0sBt6SrhamshyLy4ytoVgZ +7VgTfGOx6VWxnd14XXQPLzQZtoRV6EPWzP+YajC3h5DU= X-Gm-Gg: Acq92OGj+g1GCRy+Y4qysTveHKTpxwq0q6IxGWirBaVB2/lzinsT1nSlYHzMPsNw9Tt HWJ7lb2itvYs2b/xpky3nx6AS1Zi3WnCGlOKe7AxMK2KxLidEqd18/23BJP0NlYJ8oVcPV/xHpe YBStwL/yptRza3L7BBSFJc4kWkFEL7XZGqEDPVpze6iLq5+yMEpEUAMrX1Xm+jjkJfXOunOSJnw hzQUutZPeypAHdsLYgki8O2rBUoFpvAYOuDiV2tyuqS9bNiEvMLOPBBnfkAAbaqhDoOVTUvutYV oV125z24t3e7gJsGwD9beClzu00HstQnU6PMn+zZ4jYh7zMxh+f9XjX1AnAKjPGrydCk2IhQivU koKv9bOqXwQponPEbu+uBIZo= X-Received: by 2002:a05:600c:4ec7:b0:490:b8ee:d6a5 with SMTP id 5b1f17b1804b1-490c2589927mr241065885e9.6.1780907811971; Mon, 08 Jun 2026 01:36:51 -0700 (PDT) X-Received: by 2002:a05:600c:4ec7:b0:490:b8ee:d6a5 with SMTP id 5b1f17b1804b1-490c2589927mr241064915e9.6.1780907811451; Mon, 08 Jun 2026 01:36:51 -0700 (PDT) Received: from redhat.com ([31.152.37.159]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490bc39e024sm500006545e9.4.2026.06.08.01.36.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jun 2026 01:36:51 -0700 (PDT) Date: Mon, 8 Jun 2026 04:36:38 -0400 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: "David Hildenbrand (Arm)" , Jason Wang , Xuan Zhuo , Eugenio =?utf-8?B?UMOpcmV6?= , Muchun Song , Oscar Salvador , Andrew Morton , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Hugh Dickins , Matthew Brost , Joshua Hahn , Rakie Kim , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo , Axel Rasmussen , Yuanchu Xie , Wei Xu , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , virtualization@lists.linux.dev, linux-mm@kvack.org, Andrea Arcangeli Subject: [PATCH v10 12/37] mm: use folio_zero_user for user pages in post_alloc_hook Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email 2.51.2.2891.g4157995a80.dirty X-Mutt-Fcc: =sent When post_alloc_hook() needs to zero a page for an explicit __GFP_ZERO allocation for a user page (user_addr is set), use folio_zero_user() instead of kernel_init_pages(). This zeros near the faulting address last, keeping those cachelines hot for the impending user access. folio_zero_user() is only used for explicit __GFP_ZERO, not for init_on_alloc. On architectures with virtually-indexed caches (e.g., ARM), clear_user_highpage() performs per-line cache operations; using it for init_on_alloc would add overhead that kernel_init_pages() avoids (the page fault path flushes the cache at PTE installation time regardless). No functional change yet: current callers do not pass __GFP_ZERO for user pages (they zero at the callsite instead). Subsequent patches will convert them. Signed-off-by: Michael S. Tsirkin Assisted-by: Claude:claude-opus-4-6 --- mm/page_alloc.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 4676fd49819e..d4fbf1861a8a 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1861,9 +1861,38 @@ inline void post_alloc_hook(struct page *page, unsigned int order, for (i = 0; i != 1 << order; ++i) page_kasan_tag_reset(page + i); } - /* If memory is still not initialized, initialize it now. */ - if (init) - kernel_init_pages(page, 1 << order); + /* + * On architectures with cache aliasing, pages zeroed via the + * kernel direct map (e.g. init_on_free) must be re-zeroed + * through a user-congruent mapping. Host-zeroed pages + * (zeroed flag) don't need this: physical RAM is clean. + */ + if (!init && (gfp_flags & __GFP_ZERO) && + user_addr != USER_ADDR_NONE && + user_alloc_needs_zeroing()) + init = true; + /* + * If memory is still not initialized, initialize it now. + * When __GFP_ZERO was explicitly requested and user_addr is set, + * use folio_zero_user() which zeros near the faulting address + * last, keeping those cachelines hot. For init_on_alloc, use + * kernel_init_pages() to avoid unnecessary cache flush overhead + * on architectures with virtually-indexed caches. + */ + if (init) { + if ((gfp_flags & __GFP_ZERO) && user_addr != USER_ADDR_NONE) { + /* + * folio_zero_user relies on folio_nr_pages which + * requires __GFP_COMP for order > 0. All user folio + * allocations set __GFP_COMP via __folio_alloc. + * user_addr != USER_ADDR_NONE implies sleepable + * context (user page fault). + */ + VM_WARN_ON_ONCE(order && !(gfp_flags & __GFP_COMP)); + folio_zero_user(page_folio(page), user_addr); + } else + kernel_init_pages(page, 1 << order); + } set_page_owner(page, order, gfp_flags); page_table_check_alloc(page, order); -- MST