From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 205573EF679 for ; Thu, 14 May 2026 13:49:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778766580; cv=none; b=O5vsKAXcyrZiQ+lXp1sglCFgzCI4PMPtGvwqxLj+SRFYan3j9NC3qNs848G7q14KVbIHP7CpR7BfaGMB3HlLYWB+nFSaMS8YIAs+d1PlBqGTA8Ro4gMmzw6Lr7JXatctWO3b4sMvh8uhGZ3L008kQnKYNPv6L3sVcViQnxBDmvM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778766580; c=relaxed/simple; bh=OZUU6wejiT7mjDvuWG9JChopoDXLhXCYiwgilmov9b4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BGs7kXRHSjcGfycRA/xBUdd2UA+QLaxE6YcsiXGfWSuKiFXoVhcyF8DB9c/dIF0sLH6WHWqBKIxYMB6sT/0wzszNcr1DVJS4cZZFQwptV+pyOaiDcAUJRvvE3TQ1ij0xBWZw5TvHHXcJcloYzZnPsac00RNR9rChvB8Xh/bKQ4A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gourry.net; spf=pass smtp.mailfrom=gourry.net; dkim=pass (2048-bit key) header.d=gourry.net header.i=@gourry.net header.b=CrgkU43e; arc=none smtp.client-ip=209.85.222.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gourry.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gourry.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gourry.net header.i=@gourry.net header.b="CrgkU43e" Received: by mail-qk1-f181.google.com with SMTP id af79cd13be357-90b2fcf90a0so560420085a.1 for ; Thu, 14 May 2026 06:49:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1778766576; x=1779371376; 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=oPX+//kFk8/jIPUJw7pgOciEeGW6jgVvj62i43St87w=; b=CrgkU43eh4N/KaPnhaR8Xw9UHIpw8KGRXGjL51e/zKFqBwd622Nzf+2ISwVjBboTod 2B4DcO5wqlat9YefuYQU3i7fk52oV7s/pb+MCU4L4Xde2dJNH6ch8Xsnrf4f2sQta7UQ pirFb0veBZYKFK+vZYvovCJ8oqV1TMs0Ih9e1JfLCJ+pTYGpLHu7uXw5ki9QSVUwoPi3 eZFNx9Nc0xT4epl+44uHBQNHA9VbqUoaisGrf4FKA35WDVhFhgm4WFujounGqytYAwl4 4azoJniP2dk97GfSXRB6i0lxN+S0TZFwmLPeL4Eqid5QHyqcH3S8bN2xOUKBTN7tE+dH pkJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778766576; x=1779371376; 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=oPX+//kFk8/jIPUJw7pgOciEeGW6jgVvj62i43St87w=; b=FUBCJNCF73ZNSjv/bk+D9mXLh1g444Sv4DFeWdwXkN5zz1XNqY8rc7KICGF5KnSGyd oUgfgt/ngMgTpMSGPGwzk28TN2i3dkZKoOgJVWrtanKWyxhdh40gAHV0qFkMN4qFPUD3 Hp9agr0LJsZtclJniCK/UDZjKF7PtRlqCUEMKSc8sX57gXmTzs1HdZaqMqie58itDvE2 +K+4Ds/sersP007JkQ+3ecjuQUL382XxAKNEHJAHDiCsO4v6G8y6ygynLSn/udzYQPGN rsxPepF70UugbXHkvQiyv9bjFB1kkxPP3BwIQVW6ojNysUf+H86ndEOqWen8AFb0dPYz nLCQ== X-Gm-Message-State: AOJu0YyQJJb934VRNGkBRNswhfEZaNBvoCXrP62I3zRHFWqiFDGB68xY DaZFkM6VdJdMSgGmOGbGcdNECs8cCBKJrhXZeFDQwY4DeSmFJitKzW1xstVBr3SrYYTG6+gn1Ev b8JvN X-Gm-Gg: Acq92OEcVSy4amZ7Y2kF+RwD+mhUyXeTZSYOPc3JcPippR52YqLin1cSfB7jb+J9uzr mK38Zm+VeAf+Jyia9JYwyvRYZg8FRbOvDsftWs1u35hTEdGO6HbOjkM7LcM0lfJ1iQCr+oCdu4k j6/2ZHU7xPyEm/PBpdNqYpkflUGFK7YsRZbKlN++Hgx/YMwzwgv4M6zF5UPbU8LkSmM4dUQX2zb JbADhwn9gffNEQnPATRS2YQrA8UC3r4zRe45s+yZTwgJLSWayHrHxhumWwFgx2cUzCSCMe2+BEI w+c4LuYfrHhnv9rVwrU8igJti7dAe8EhZMlR+4JOto4PjkZAj5946tochcwPbv8y+GzHbt+J4yZ VIWPSNoI+9j66VgvsoYVJ2e5mRU8+kgwwfpDx8zq3IM5OE5Vr9c0DDcDWjezK1kit0eFAy5dBox u0LL4oDiPCizniHpI+v3aWHVOTu/MCwDX8KRmH2j9rCeGwrv5C5HepOInqi3gS/bdVzCTq+c0Q0 eIIQTWdRGsW X-Received: by 2002:a05:620a:45a5:b0:8cd:c152:ee6b with SMTP id af79cd13be357-910af5599d3mr518683585a.8.1778766576120; Thu, 14 May 2026 06:49:36 -0700 (PDT) Received: from gourry-fedora-PF4VCD3F (pool-100-36-248-188.washdc.fios.verizon.net. [100.36.248.188]) by smtp.gmail.com with ESMTPSA id af79cd13be357-910bd62e233sm248608685a.45.2026.05.14.06.49.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 May 2026 06:49:35 -0700 (PDT) Date: Thu, 14 May 2026 09:49:33 -0400 From: Gregory Price To: "Michael S. Tsirkin" Cc: linux-kernel@vger.kernel.org, "David Hildenbrand (Arm)" , Jason Wang , Xuan Zhuo , Eugenio =?iso-8859-1?Q?P=E9rez?= , 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 , 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: Re: [PATCH v7 09/31] mm: use folio_zero_user for user pages in post_alloc_hook Message-ID: References: <45d1ea85b574399459a64fdba28fcf04abfa3e7e.1778616612.git.mst@redhat.com> 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: <45d1ea85b574399459a64fdba28fcf04abfa3e7e.1778616612.git.mst@redhat.com> On Tue, May 12, 2026 at 05:05:54PM -0400, Michael S. Tsirkin wrote: > 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 | 17 ++++++++++++++--- > 1 file changed, 14 insertions(+), 3 deletions(-) > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index db387dd6b813..76f39dd026ff 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -1861,9 +1861,20 @@ 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); > + /* > + * 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(page_folio(page), user_addr); > + else > + kernel_init_pages(page, 1 << order); > + } Open question but not necessarily in-scope: Should __GFP_ZERO just be implied if (user_addr != USER_ADDR_NONE)? Putting aside how that's done without introducing another gfp flag (maybe something explicit like `alloc_pages_nozero(...)` ), it seems like a very short jump to just adding __GFP_ZERO to any user-alloc by default. I'd be curious to know how many callers across the system omit __GFP_ZERO when allocating a user-page, and whether there might be scenarios where we subtly miss it (seems unlikely and narrow, but very possibly something a driver could do unintentionally). ~Gregory