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 D3D2F3BF66C for ; Wed, 20 May 2026 22:21:37 +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=1779315699; cv=none; b=Ga0kgK2F/b0EjEusvDlAC1IVNyHz/oNdt8qRfIGA3jIuE6KkalaWVyDfiUvDDWI7oi1CUuJ78pmXlnjYpFZ0R9yEpPE2SmKogKO8MTF/XQYGj+fJBX3nYGkmYn2/8HN2VFW5McpUlOPjyPTzg4LQywfi1PV9yuxiiK5tnBcvSQc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779315699; c=relaxed/simple; bh=Y8nJwk5bnDwHUaiRuu38UiZrbZgOyq6/yOrVYrwIdY8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=n28bWaFDAgoljsaXBwmMbeHsaJIJK/cwh8V7Vs3JMKsQo1Bw1OltoUpatEskIe/JtRZeMr44NF/vHLhFrVBJWpuxETWSkphaIvOcGgEXg0A0eGBXkIBoREK8GnnxltIRKakbuK++eYXBfLinB5qxcPxg+evbrl/szMikYX5qViY= 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=LsRKgfP6; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=AdSQbS2e; 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="LsRKgfP6"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="AdSQbS2e" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779315697; 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=Xr0Wq/tgum3c1wWvimcTvl9O1enHA5UssViDpRUdduM=; b=LsRKgfP65J1Gd4QwwTjG7pngG2208o8urC1jFdiLReF48/RZviSbNWVe/xVAQUokCPmUCP yjeQ9d6taqTtCXT4F7EHfvkIBAINo43k5qSLkfMZsZ94+9/xsya6DmKA2PcDhCt6zGNhTv VJYUJ7o41iXjMjhCek81oyi2btzyobY= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-104-1crSytsMPjSg7n1u4ib8tQ-1; Wed, 20 May 2026 18:21:35 -0400 X-MC-Unique: 1crSytsMPjSg7n1u4ib8tQ-1 X-Mimecast-MFC-AGG-ID: 1crSytsMPjSg7n1u4ib8tQ_1779315694 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-48feb8b9acfso31986405e9.3 for ; Wed, 20 May 2026 15:21:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1779315694; x=1779920494; 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=Xr0Wq/tgum3c1wWvimcTvl9O1enHA5UssViDpRUdduM=; b=AdSQbS2ebErSG0lgal0pInuneXoDDsmvOsakL90P89aHvb3rqRybRwwBFCoGzmvns5 G7eIZbJByYE6BiYUVC4At+eXoaFOKxfFwXga1xzKEUn98jT58RbOqdSG3kmHArO69zG2 OH0aJ3rg5Cx6qVUwEaynR49dElvkLuURfPd/fXa+Xz/pbmKfqQM6SF5at0ercghFtLt5 QObn4L0C+uU97hVh+UG3c2Emz6Y8uhjoO4nqWClIUjTIm4WWNIbrjNgZN/2NCAY16/XR 2MwgekSepPClnNXu5LacBA/1zQKUdHD1L14kMTkkq8sR/FZj0JVNl6Y7jZ5eokRuHylD pbUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779315694; x=1779920494; 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=Xr0Wq/tgum3c1wWvimcTvl9O1enHA5UssViDpRUdduM=; b=PJTZ19Y1ToMyApmICNFxVxi3M6ywdlwzsvQ+3DmRqQyft0FJ/XTb4FeNX88mcCzvgY ARFFPqPiCBrmzCr9e7K+l+fUzhTjT34vfZBSmVqRixlIhbIqGcW1VBiYGVhOz7438pnW wCRROAIrElnclZ7mttLdjziLjip0g3cMBXBJ3ymsHgd69AgIM80jXpcsUTQ5cLZdfrq+ Y2WK+KrSIjBjo5TJm3aWyKjIxZ/mIcz9ISv3kVYio9ztq6LTC5c9ilhEdpZXP5Fk+W0Y 5RFdfLxO63deIXRmCZ338FbvjHHPt2AU1VourbcPwnim6l3JScltVAHIoN/nn11eKQeZ SyYA== X-Gm-Message-State: AOJu0YzENZGdsUwD93eRzp9j1lLKMywJ04qgDYEUANtABM7sLjvfbhKF acDA4OXB/D+WjF8JO73am2voS2NSTjscrlviQhjFw9N5RmO7NHiWNWMYSBsSsBZZ+FalL71ps17 WpxR1MAhtc8WtmbYnYrp5ggKmspyzpSFaQcYTVLzfGDmsAntUyu7fwkhhLOkz4MvRHqLO7Tv9U/ +xUe2ifJc3D5lEvhZwjnuDEpquQ97yEgxkMN0spjkMjfs= X-Gm-Gg: Acq92OFHjVJYg1+mtV73xJQ1eTzgY/QkGz9DVXtEuMfkrE9zUzm8R4qfFkfI30jB0GL lwXyyTZOuwMcixvM7ZsP9NQ7YgijH5aZr9iB7li5S0eorgxlfECir5SSnynM5eTK0U1Eb79MCHZ /fs7+yeCQfaEcFm+QUTg6WhdCxBCHBu0yb1NIZ83kfof7PKe/zWVjkXPFbCi9Ia/3f0LCxrKsQn KjnEn3WmKyKOa6SoWS8aKTf+4Dgq/atjPtWoTfiJygwoIPzxGBzhYp0gvVaUqOC1iftn+s7Cw/4 QzmGCO/kUAFdFxevHHDMPyqHVRtyxz2tIaLIHonzo17weLmWysXiJlEwfx9Y+hwqyxZHZ2MM5gk G7zYzzqHH2CVkMl298jq2+mV1yOZ797Vv6BdyGtee3Yk= X-Received: by 2002:a05:600c:1f89:b0:489:201c:dc46 with SMTP id 5b1f17b1804b1-49036068fb7mr2635625e9.12.1779315694180; Wed, 20 May 2026 15:21:34 -0700 (PDT) X-Received: by 2002:a05:600c:1f89:b0:489:201c:dc46 with SMTP id 5b1f17b1804b1-49036068fb7mr2634915e9.12.1779315693632; Wed, 20 May 2026 15:21:33 -0700 (PDT) Received: from redhat.com (IGLD-80-230-25-45.inter.net.il. [80.230.25.45]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49035c2159esm1982445e9.4.2026.05.20.15.21.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 15:21:33 -0700 (PDT) Date: Wed, 20 May 2026 18:21:28 -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 v8 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.27.0.106.g8ac3dc51b1 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 3108ac9061ce..ad0655387e9d 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1864,9 +1864,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