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 88BB13A16BB for ; Mon, 20 Apr 2026 12:50:50 +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=1776689452; cv=none; b=JwK/9qJLHmTgd+tDOzhCUBh5QaZParJcCYeg4vtoL/BRqdL7A7PfsM+8BbH6hnFGl+FPHks3UkYCKXEzJbILCn/M0py1g5xkHafEsCSmIQ+M2hmsvIK1w0tI0Fc9Xy5QE2X20d8L4aWUR9AounbOVMQbznUsBUgVf7Hrax+7E+M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776689452; c=relaxed/simple; bh=Iqo+uRjUW8vQWkIcCauoHPwk80gsXmFn7kTaAYxVOHo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=eeNMWYDCSKW7ocfyDQLZyelb+I09+ANzl5qZ9DH+TQS7lVSMRJBqr1aDTGLByXACk2IsjPGpiDt6jMaVeFP6DAjA10+cHga06Rz2pKD4fPORuJ/8FGc8/6cRBti47nAed4hNSVA+ECMhfS4kl+zGYuPQrIJbQij/C+hNWalprvg= 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=dX8tmDHn; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=uCxIIkK1; 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="dX8tmDHn"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="uCxIIkK1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1776689449; 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=RqSLZ/IXDnoMvKzd+RuDqnZVTelRyZJIHxTKtL1QEDg=; b=dX8tmDHnrOp6lSC4rYJdvzI6fp6NBbYQXIpqSI6KydVGMNZlQWp3FWMac6tquG1XP0oSPd A8wye8hr1mXSAUb5Tg6TOC/cSl4LmcvO2rv6ghiX+QFDomeuN9fJ/JlCImMCii7WmyVKTN Gtkg31ufzuOWHzJfzBk2clT8pJqs9ZM= 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-677-6NIh73vQPNetj2zimlNVEA-1; Mon, 20 Apr 2026 08:50:48 -0400 X-MC-Unique: 6NIh73vQPNetj2zimlNVEA-1 X-Mimecast-MFC-AGG-ID: 6NIh73vQPNetj2zimlNVEA_1776689448 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-488c768a9a9so24869145e9.1 for ; Mon, 20 Apr 2026 05:50:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1776689447; x=1777294247; 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=RqSLZ/IXDnoMvKzd+RuDqnZVTelRyZJIHxTKtL1QEDg=; b=uCxIIkK1Xu8CUsz4hvBHihpV/UxSaKEwRctJRfwFdxFvpT8FMRe7Wyv10f+S4Uh5kZ uF5CoQYo/qNwXKTpTdpG2eFsbBzYDfirfIce9Co6Me3XssM20Tjfet0W69vxfNRcL26N mebEAEpp0AAEKrqKjwT9HbZVgW8A1IBHdcsT9/3RzfPlbwY7H+5MIMVN6sp5kA0r4KWx QgEbJKYVDk+jOrZZWP73eY33YVXyMancGaM0dBh9rAihvotgDxzkLnz+6zO38wkJtaYF A7RJngkxvIcY08NGEq2ZnNJTO2QQDzSkfRK5MWkGAjGVfBttuiyzQLF/hQgJpygD10xB BhJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776689447; x=1777294247; 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=RqSLZ/IXDnoMvKzd+RuDqnZVTelRyZJIHxTKtL1QEDg=; b=CPLvG4D1Su+YXN4rJME10RCkGz1HOwkRfniQIM/xuTIcGsyoGZkhTjNTjPxalUxO1m hZe2yEcPPd1vG62TIhfzAW465ZD7n0oQxtJ+kLpZXK6MHqcVbL6hWqH994L4ij21fagQ Laay5ZnSRiaYI9H0LPd5U30GRmTWMo6wEouDcowFLzFpKR/oZf4vFQhR8ngoYRdpBfwA mQi6w/aRSM4espQgmMYcwrXfitGl6FJWvlq1bSQjVKkGdwmVWI1NDYUfB6ED5y7NoM77 7oJWNWrl0untVpOgXyYdrpGYMsuiHaDSPzxyaeMfvKIar3jAOyIqocvAb0LvYouoeRjW S4Fg== X-Gm-Message-State: AOJu0YygRILXu2uTIsg4VymoOZ/Pu/mZ+eGL8ImYzPnO+BhI0anEhDEz Q69l26AdQfQcvH0+v3Lll1KuDXbYmHFzqCf2LU1gi2hckIvXsLdVxL0ByGMDDXLRiRFdNIi9zRH Hih/8MGjaup93G5yI2DKDAX9sfXDg01Oz1SLCRqdFd9YazWxaJeehbIM+qXwGwT8uECxVwB0ITH +noxOhnFqh0Yw5i+uBvorF0lcBXYAwEf5981QYNjvL1ss= X-Gm-Gg: AeBDietjrjEvBEkQJgXRl45O+UKeSu+t2F+14Glyt9T781nuwLUI01Wlu6rCsYVgPaC FoQOCc8fiWYEJ1OECtYULx5D3eA8hDYJwKxQJ+5YMBZxjvkPU3KcO7QOjCDjr1YRdJV5/ibCgTD NaqOMMpHRxruD3x0odGu3w9B51YmPGfMRsu1S9xGEQ9zaq0b6uB1fL4c5JrjCVS7rIL54t+Xf1u JBS4OOxq4rf8HTSY0RAy7DpF6dPd81YR7L5gUP+H8OjH4RkNzL/90LiPbrbtXCaiLZnN8/YT9S6 VaUvrWXmH/RpulWkOt4ouRWuit2XZ3bpVnGd9H0OUPPynAcCFn+GVyakdd+bhd67UWEBsxZYacH VkYYsr9i5GZblT7eivDlAfqVu4SGdq4Nrg2TvC636/TgV0wJrC7ApeQ== X-Received: by 2002:a05:600c:888b:b0:488:c40b:c8bf with SMTP id 5b1f17b1804b1-488fb73d234mr157904255e9.2.1776689447115; Mon, 20 Apr 2026 05:50:47 -0700 (PDT) X-Received: by 2002:a05:600c:888b:b0:488:c40b:c8bf with SMTP id 5b1f17b1804b1-488fb73d234mr157903665e9.2.1776689446452; Mon, 20 Apr 2026 05:50:46 -0700 (PDT) Received: from redhat.com (IGLD-80-230-25-21.inter.net.il. [80.230.25.21]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48a4b329542sm43629625e9.3.2026.04.20.05.50.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Apr 2026 05:50:46 -0700 (PDT) Date: Mon, 20 Apr 2026 08:50:43 -0400 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: Andrew Morton , David Hildenbrand , Vlastimil Babka , Brendan Jackman , Michal Hocko , Suren Baghdasaryan , Jason Wang , Andrea Arcangeli , linux-mm@kvack.org, virtualization@lists.linux.dev, Muchun Song , Oscar Salvador Subject: [PATCH RFC v2 09/18] mm: hugetlb: use PG_zeroed for pool pages, skip redundant zeroing 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 Set PG_zeroed on surplus hugetlb pages when buddy-allocated with PGHINT_ZEROED (indicating the page was pre-zeroed by the host). Clear PG_zeroed in free_huge_folio() when a user-mapped page returns to the pool. Check PG_zeroed at fault and fallocate callers to skip redundant folio_zero_user(). Signed-off-by: Michael S. Tsirkin Assisted-by: Claude:claude-opus-4-6 Assisted-by: cursor-agent:GPT-5.4-xhigh --- fs/hugetlbfs/inode.c | 5 ++++- mm/hugetlb.c | 31 +++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 3f70c47981de..3f9bdb5a7c85 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -828,7 +828,10 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, error = PTR_ERR(folio); goto out; } - folio_zero_user(folio, addr); + if (PageZeroed(&folio->page)) + __ClearPageZeroed(&folio->page); + else + folio_zero_user(folio, addr); __folio_mark_uptodate(folio); error = hugetlb_add_to_page_cache(folio, mapping, index); if (unlikely(error)) { diff --git a/mm/hugetlb.c b/mm/hugetlb.c index faa94a114fd4..704ec0817c5e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1746,6 +1746,8 @@ void free_huge_folio(struct folio *folio) bool restore_reserve; unsigned long flags; + __ClearPageZeroed(&folio->page); + VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); VM_BUG_ON_FOLIO(folio_mapcount(folio), folio); @@ -1919,12 +1921,13 @@ static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h, * pages is zero, and the accounting must be done in the caller. */ static struct folio *alloc_fresh_hugetlb_folio(struct hstate *h, - gfp_t gfp_mask, int nid, nodemask_t *nmask) + gfp_t gfp_mask, int nid, nodemask_t *nmask, + pghint_t *hints) { struct folio *folio; folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, - NULL, NULL); + NULL, hints); if (folio) hugetlb_vmemmap_optimize_folio(h, folio); return folio; @@ -2137,6 +2140,7 @@ static struct folio *alloc_surplus_hugetlb_folio(struct hstate *h, gfp_t gfp_mask, int nid, nodemask_t *nmask) { struct folio *folio = NULL; + pghint_t hints; if (hstate_is_gigantic_no_runtime(h)) return NULL; @@ -2146,10 +2150,13 @@ static struct folio *alloc_surplus_hugetlb_folio(struct hstate *h, goto out_unlock; spin_unlock_irq(&hugetlb_lock); - folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask); + folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, &hints); if (!folio) return NULL; + if (hints & PGHINT_ZEROED) + __SetPageZeroed(&folio->page); + spin_lock_irq(&hugetlb_lock); /* * nr_huge_pages needs to be adjusted within the same lock cycle @@ -2189,7 +2196,7 @@ static struct folio *alloc_migrate_hugetlb_folio(struct hstate *h, gfp_t gfp_mas if (hstate_is_gigantic(h)) return NULL; - folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask); + folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL); if (!folio) return NULL; @@ -2242,9 +2249,6 @@ struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, { struct folio *folio; - if (hints) - *hints = (pghint_t)0; - spin_lock_irq(&hugetlb_lock); if (!h->resv_huge_pages) { spin_unlock_irq(&hugetlb_lock); @@ -2253,8 +2257,12 @@ struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, preferred_nid, nmask); - if (folio) + if (folio) { h->resv_huge_pages--; + if (hints) + *hints = PageZeroed(&folio->page) ? PGHINT_ZEROED : 0; + __ClearPageZeroed(&folio->page); + } spin_unlock_irq(&hugetlb_lock); return folio; @@ -2748,7 +2756,7 @@ static int alloc_and_dissolve_hugetlb_folio(struct folio *old_folio, spin_unlock_irq(&hugetlb_lock); gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; new_folio = alloc_fresh_hugetlb_folio(h, gfp_mask, - nid, NULL); + nid, NULL, NULL); if (!new_folio) return -ENOMEM; goto retry; @@ -5820,7 +5828,10 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, ret = 0; goto out; } - folio_zero_user(folio, vmf->real_address); + if (PageZeroed(&folio->page)) + __ClearPageZeroed(&folio->page); + else + folio_zero_user(folio, vmf->real_address); __folio_mark_uptodate(folio); new_folio = true; -- MST