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 E6BA93FE640 for ; Fri, 29 May 2026 15:23:13 +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=1780068195; cv=none; b=mOhi7BI4i8Y1QVBdmbfQPQG5WScIJ8XLN8ndiJZUgLZqgIpRO22dIsXU6LWF6gXroNviV8ROnO3c4PuLiXeCaiqm5fy90Bk4RGxRwn37CdAxxvlu2sF98ex+LyMXnl+8MXtX16p63pkJ1j4TkTNkXt0MrU9WhHVTMpiC3cLWpzk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780068195; 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=L7puezxNHt3HgmscG5Snifs+io71m67WlLV5D9eVXyh3cM5fgaw5KUSuwNUjtrq6ggKpZEvfxKizFuZtZ+hHe7ntobP4PuCZTCeLbfMMbNqCBmjKRjsXrhHDFpC7HseO1+IFA/xPMk1yv+pP1yxNIBgIzueDRzA1BWq70NLtBVI= 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=APirCoaT; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=lvbi7PUV; 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="APirCoaT"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="lvbi7PUV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780068193; 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=APirCoaTRGpugczVmqZjXm8eOmvDF4MOK0vXhs0CisI6HivuXVCo2F1q5DzF7KhAWTmjyO vMGknFOTkbE8mK+2266t8N6F4OhWA2UShpBLpSOh9pObd2hU/rrser+qtva+RLUxh/QAar ObddwoeUqRRP82SKidiGijPOOOA1xaU= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-594-iLHg0es-M0uOTEUoYTUOBA-1; Fri, 29 May 2026 11:23:11 -0400 X-MC-Unique: iLHg0es-M0uOTEUoYTUOBA-1 X-Mimecast-MFC-AGG-ID: iLHg0es-M0uOTEUoYTUOBA_1780068189 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-44d9ace59efso8073174f8f.1 for ; Fri, 29 May 2026 08:23:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780068189; x=1780672989; 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=lvbi7PUV4shPHLtGCiVIvyLieeUaa4cR3/c03ruMENZOwg+4n7PlJAEyGgwcyWBNSS QYvpnuR7cfHiks+JAGtgI1vm7dzlS6b1BAEu29MlrMw6CvEdSPGQcOkCMsIWsrvIcxt6 0BqviX90e3L0spLfecpIpfcwo0R29QQ8KB3LnTzAEXR8G1YzQDTSOdAPKE1oSTdS9MOe rEREeuAY3WF6+6+qswOP3mnV7341ydMKsfvIID87ROWZIPil4BjSm6CMVZeJ2xAEMDxd LIKGd0C8f9/VKDdEFuY/rCZ/xpC0Edz/9rZ1FHyARJxv9uUyFHJapOcIw7MiPo8G0/x1 exng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780068189; x=1780672989; 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=j68dAX+V5qBdIMANvTFddE6EGG1IYcgx1KwYMM3Qe2Ri9D6xhlpo/4zB2xKPOEKCQS XeFaxgjy87orpBRHaFbBkrX+ASYAimXUsmx5nj8uQ40kOULpVD3BNXDtYvx4GHZyWdJi k6V0mhJsyO9YuewPIPaDdjMFDe4sG5C7RgpogCH0pS6cPZ9H/4CtgInqYxB6qHp8ZmDw taidBqpIGW+Yll2RID0mzhi3p6SwP3LQCLmU7ij5ps/RSw/BIMNi95YeyWiwvpixqRJX efkzXYft7/OL9og2aknNxpuykP7qIea9db/aSJmvCg+ylkuBq8EMIhDSonBUQCrpVlgK C7Pw== X-Gm-Message-State: AOJu0YzsvoOGMGLU5z72x7vUsIJ1jMCL6A/M7fZZR0HnNwFS+XE0L6oI VtwzF+ATP1AD/rcAUVCh1CY4X+1yyVri0eIkl2dU3RYsWc4Owh4S4Otvql1XHX3gL04pQeDg8F5 MIeHHVbaxlnS1xPfV18RljfKChrDXIlhbSTGB43Rw4YHsMeTboNy6KNZuGC+Ot0FBnNKbBhnPr4 M7FEUa2us9DowBfX75E3/4TGLKhxKulHUKh4qWecqpXEA= X-Gm-Gg: Acq92OH4VKdHsgNU4i2YE/R3EbTllcjsv1pZUvQqSjDUNEwfvD6dQxL9b4NoD+cyY5c MkAkVEg8iHb1J/stj3Inwm1qUHCJxIfEdL33XaOx4HdkrqMB1s04LHdwP3cnm9Vs1AsNgj48nQU +Rc2t5ZSdWD8/zEvRVhpldTuBlHaXLZeiU1nF5g9Pgv5Fe5VoaSqJAa7HCo1lABcGcVRXZ7FA/V LqZqH7KkUDZvT60n3FbZbh2rmhJ9oehEl1/NuPyt6iKfGzq3lj0Ad5X2c0eRIIwWNylGZuFTQxk TVGJnYdO9Zi6GHhDoVF9gbvVMwFmFwn0FJPbNPnMtLmubyvFupzd5VeI/36r602vuy4zwPtMMws zWVUdnKm4i+kTTX07OQyIseYKwiC++IUdGWZ0VQ9UQurowhY2mqmPVQ== X-Received: by 2002:a05:6000:41f9:b0:45e:93d6:359b with SMTP id ffacd0b85a97d-45ef6b2254amr513242f8f.9.1780068189059; Fri, 29 May 2026 08:23:09 -0700 (PDT) X-Received: by 2002:a05:6000:41f9:b0:45e:93d6:359b with SMTP id ffacd0b85a97d-45ef6b2254amr513116f8f.9.1780068188420; Fri, 29 May 2026 08:23:08 -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 ffacd0b85a97d-45ef35874b7sm4319354f8f.35.2026.05.29.08.23.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 08:23:07 -0700 (PDT) Date: Fri, 29 May 2026 11:23:03 -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 v9 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