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.129.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 DF47439A056 for ; Thu, 7 May 2026 22:22:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778192570; cv=none; b=R23Bce6n7wKKKsud5Xu4obPBpkmMxNAhYJAzQEt4GspddRnTw4DMoroHM53izZetlvwNy13Cw1Olp7pe0ZPf7XK14/orTWke4qmJyr+jgl3sccM8i2ks4E+F29cBuQ+8s2KG537r8qDO3aLln15SRzA/1NyptfCNSr8i9aBefOY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778192570; c=relaxed/simple; bh=PR7dIULo6oVGd71HWH/+M3MZeJj687W04Wl35HO9EdY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=F83xuQxbU5baQUe1cVr86vckpfWMe5iD/+qXLF4UFX7Ha+Ax4neQ206h1CQ3qCs8rGl1ZRA+VIlaaEjMzUIaWaWSSSyF+9PstvkjFw7KjoAQkObrVgtIRE2io+CX3QrFGuGjPPbR5bHLyMKKzFacmBSIhcA8nYDZj00k7DNyAcY= 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=cWXHrN+G; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=uOuIhPlO; arc=none smtp.client-ip=170.10.129.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="cWXHrN+G"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="uOuIhPlO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1778192567; 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=6XZVwPk/FHchRW7F1q982DWChdDEL7ZRxXSzf1eFvwA=; b=cWXHrN+Gry8ZKxK5fJctv8Zl3s+MvWh0zt73Wpi3ReeMJrBpVgdmo0Xco9zLd7Hiv+glzn RjZRIsxe89QEtRgO39Y/r8hy9tfCpPH0HKPaBTgKvAF4Rjmu+9GMjWttUcAS/8kNrI9qld iVwPaJraVCxCVbnAu/o45xW3dOm9OIg= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-59-ltb2Hh7rNOeIhSv_fRRE4g-1; Thu, 07 May 2026 18:22:46 -0400 X-MC-Unique: ltb2Hh7rNOeIhSv_fRRE4g-1 X-Mimecast-MFC-AGG-ID: ltb2Hh7rNOeIhSv_fRRE4g_1778192566 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-43d789cebcfso1679561f8f.1 for ; Thu, 07 May 2026 15:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1778192565; x=1778797365; 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=6XZVwPk/FHchRW7F1q982DWChdDEL7ZRxXSzf1eFvwA=; b=uOuIhPlOgctZJeqLu3T77YRzSZ2b6OfoanmkZusWVlFkK0PZ04HMmZ53N2iwcvOKKt D196L/PSnF1g7xPiJFHwUxiuzJwgSRuwNAhCxufXQ1svueM9Wf5ogAVihDUAXlVZUpqu gAVGRPw1DFw0PP88YWr+Y3f/egt+aMerOorhHZ5FBctCUY3wFMlsGU7ktnKMIt/+yKuB mmS4Jw3bGkUV74mMoyomyq2C/MroJnGPadtsmbCdH0BXKBxepQLpoPLEDv+2GIzcXeYG ZSVQU9IFXU8WJgfjrccK3TIovTHx39y+SudMZ4c51RQ6lTlUvt06m53DOizbzmnlw5+U A2wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778192565; x=1778797365; 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=6XZVwPk/FHchRW7F1q982DWChdDEL7ZRxXSzf1eFvwA=; b=tH1H1A7ubjAxAWkmfYuWUq8gpjIhPvHj4tL5bv5Y2sxaaLRlUoOMaShTRufPjpkuSU SWTyAugeKzH/Q/AhhYSIZ/lOpxM93RKWYugD/XzLTBe0j4qTX+PdP2pOTnuvRJ/4hMhR oI95SGxtywa1TJe/U+5cOG1QN7v5SMnn3Vc4JKr1TdZVEWO9fv0WrwJnaanXkaBpIzJM pMOiOOgUB9rDNgYtuKatr4IO9IPxc8laFx//BPdF9/kCS4s+mSJoZjx2hyZuKkQFr6DZ d0BbVUClm6JSwx2fcQrfxC6c/dXBLCD5C9ULGGzEZFarcGc3S4UcaOkpJ+FFV29omvF1 bgqA== X-Gm-Message-State: AOJu0Yzb76Jhy7nmBiK5g69842YEvuNYqywCIVUsEOH4pBD6QkcsTpoP 20GmoIEUFkRHpkL6B34G2SNsx8YzczLbmL0LujqbdJRjEK7OCTAaR2lGcUz0j9LmiG/3TA3k+aD UnWA1+IBL96tdCWQgsGoNMJ1ol+ljtJbvvszvU8WBgKHRci5BP4yMYnfo7M8rocxjndrhTmdZIr ymatedUbsV68oVAXvxe1o9ELV15Xxuhoxy5WmGRDg21ug= X-Gm-Gg: AeBDietwbSsHXQYnFpWt7L1AAC96qEC0RrL7z8zh1r5dKXeoqdxybyJHY7Laupr0Tcy ns77C875C8JeVBDiK3zHk85C1zWMDpOJZfcgppU0U0TNkLGDl9WZ/z4us/r5b9wR1OkdTRxJlke 601kkrDvRoZJODhGkwz0UX95s6gfvT0WegGXd/rIG/vrVyYtqskuyUetOyd94DBBdz2bQA6kL+S +nABpEUVQME0PgcV/zlKLTGlJFUEuoVUx74Gy3Vwp5frNupNrJfJoBV3uvx2GS7PSdO31NL8WeW Ud04UBavkESCLZCeMEZouJ1qJK48qY92Tb69vyMQ11niuUd7yWGYspcGdUEsGPN4YDYdL7GFbsC +LLQpEEVJjLgxBOu/oKOY2Sqcpk0iI1I93AfH+sJC X-Received: by 2002:a05:600c:3b8f:b0:48a:79d8:a8d6 with SMTP id 5b1f17b1804b1-48e642deefamr16106415e9.7.1778192565123; Thu, 07 May 2026 15:22:45 -0700 (PDT) X-Received: by 2002:a05:600c:3b8f:b0:48a:79d8:a8d6 with SMTP id 5b1f17b1804b1-48e642deefamr16105995e9.7.1778192564484; Thu, 07 May 2026 15:22:44 -0700 (PDT) Received: from redhat.com (IGLD-80-230-48-7.inter.net.il. [80.230.48.7]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48e65c0da87sm3543195e9.21.2026.05.07.15.22.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 May 2026 15:22:43 -0700 (PDT) Date: Thu, 7 May 2026 18:22:40 -0400 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: Andrew Morton , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Zi Yan , linux-mm@kvack.org Subject: [PATCH v5 05/28] mm: use folio_zero_user for user pages in post_alloc_hook Message-ID: <4b89470a07497a55ce2a8d31e4b746e734df1f4e.1778192416.git.mst@redhat.com> 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 | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 92640ddb0b7b..2bfa9ab60976 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1882,9 +1882,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); + } set_page_owner(page, order, gfp_flags); page_table_check_alloc(page, order); -- MST