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 B847A3D093F for ; Mon, 11 May 2026 09:03:18 +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=1778490200; cv=none; b=j3UUnnqkctb9U9VH94iaDngXB0wQ4iXEla7oCexQ5y6n+g4glzYelCjT2nT/zlLd7ibqI7LJ5cFcow9XRvhcK0pbIIZWmt4/w3/lD6xp6233R7Q9KpVzqE/e/FV6FzApqp7Q2RIXGMGc9ryULK/Evz6oxCzy2X+RPKX9/GZDKkE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778490200; c=relaxed/simple; bh=uae5V1T54mMZVBXPfWqdPxGd95+bgq8V6VITT9f8SnY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=X86lupHYpjWd5rKYhGPzrpM70bAies1VJIM84ztbVhT3MA08QY500h1vHA8QIeUhoR/fPrp8CcT9e5BtmVuJYU9CmYM38BH3cj22gt43k6Ib1wD/SFbzWV2AbVZ1eoU6HFSCbxiwNdUXBcdXewuN3dS96a0oSJfVb6KWX6vxfEY= 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=XJYOY5dg; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=PAHmF6Go; 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="XJYOY5dg"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="PAHmF6Go" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1778490198; 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=q0Jd/7EvCnA8N9ANiNsWM9FOCa/+r1oqSAVkRa3NTmw=; b=XJYOY5dgS3VuSUmkVHyLr1hWvTIYMlltXarvz0c+xBmHG03emWLYEDkIcqHTqpngpNYsKN WD4TOzun9VrGnt6ycPe3DbBqOh83M06Y+qKjeufux0MMwhUrLLBlZ3lzl0pXwwFtMWGfoH hp7evnhPGRdx8YyWmiP2hD9QhQ0vMwc= 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-29-gis7nvr5Pb2TQCLQMl4r5g-1; Mon, 11 May 2026 05:03:16 -0400 X-MC-Unique: gis7nvr5Pb2TQCLQMl4r5g-1 X-Mimecast-MFC-AGG-ID: gis7nvr5Pb2TQCLQMl4r5g_1778490196 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-48d127eb013so22819255e9.1 for ; Mon, 11 May 2026 02:03:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1778490195; x=1779094995; 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=q0Jd/7EvCnA8N9ANiNsWM9FOCa/+r1oqSAVkRa3NTmw=; b=PAHmF6GouH7mjwbgv/R/KBpVhZWAtw5MP95qpCq0bjXnfLxCJst0MZrLA17ljSevbY 9yQluwn08UKb9ObD9/WDuGp3Xk2YakY8AqENDt9r+f/S6ZrZ5xRsvMmoYlFCI3+uQt2M dGBCFebrbGf98EJSgpAVSix0+1VBKupa2IJlFrjjCBHJ7RVFuNmx0BJEH94srB9cwst7 wXAYVDCo/aABnRrYXyUh44xjC/AGCdn1kghGcH5g1PB2mXTt2c6mvBekBBwV8F0AAS6t AqQml4wFHmGXy89sPvgjZFl1r1zm32PR9p8DOkYWjWw4A6LQKcJX4KQxFDJN5Sr7zLQs XEvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778490195; x=1779094995; 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=q0Jd/7EvCnA8N9ANiNsWM9FOCa/+r1oqSAVkRa3NTmw=; b=GSU/G4jgZWQtukVMXGtz1zYwOY4B9h8VXuCuku6siukP/W+X3sBwGZB2OzeVjncGbK y6jpF8yFDzYbERJg8e8bkWSTvq4KmaIu5A1sIZ3T1bfQUq6r5A+rFNEp1jK8vJZibG9l tjjn82IB3pUt2WXfB/7VZw/5vz1/btKWpPzulFw/VCbcQjVreAr2BxglgS5ZxMZGoSoH Xp2DBF3zEp1BFYiMxpjJJpuw0tp8GdSgnNzS7FYS+XCmOXhL38icbtOmbmuePbu1Qxqv 9W3rrjRDgJ2kfrVNRyO20EzNA616/La8aQUBsM6UpvHn4nK+uTr5PmUqhmfbnZcw+H+I qFiw== X-Gm-Message-State: AOJu0YxIqETP/6zpFH02NUoqPFTcNX7k/FEH7n7mskgLizgAU4aRdB7A 3skq1VaK34tKxjYaJA34VP0qe7KvishLfgth1uB8fHcmOl1tu7KSQn6UXO2J9BdNBeZutU4arhO fE3vGQbjFTLaBrr79QzksBDJ6DfWHWfqCssE/nuEFfO3SdzPL4Z3Q0BvSbX5n0pUcq2DaVaUpig pIQ54jDq4KfBHjj+Pjdlr3ltyqgLl1nkdGXB/pRlCsW1M= X-Gm-Gg: Acq92OGkLI2LhHgkGeNGTEX/p+VavxPrdVHCUJmjASQ2SCkJf7lW8WKYghnj/MjIqOW uiH8ZLBEZHKe0WZfgXHCCBqXQgpz8LGqQlSLLRIr5hA8321RbQ1sHGHxjkjfM81cYRu3Q08V7hk pNuxDOVFkrf4XnQ0uxzhA6yDCe2F+ht9s58ZbyvEhrTDPZvDnNam6TWlDBmfEuwAM6fIvog5Awv gYeaReuP7w3vImQVfpv6HJPPLgRUoJZuzpQcTMUST6gRoRU86NJH/gbzzUE7+bJEONsalb00S5/ uAqsgh1kc43K3zLTiBKm8EjNAjTFim8eO3AWqAWAt39DibQgHcmvLUJ28xWpy5c7/tl+qwTVH5Q rhDhUl18lYGYNLE6RoBJTPmSa+HkoyRD3jLUtKKZA X-Received: by 2002:a05:600c:4355:b0:48e:85b4:ac7b with SMTP id 5b1f17b1804b1-48e85b4ad2dmr11170465e9.18.1778490194131; Mon, 11 May 2026 02:03:14 -0700 (PDT) X-Received: by 2002:a05:600c:4355:b0:48e:85b4:ac7b with SMTP id 5b1f17b1804b1-48e85b4ad2dmr11169355e9.18.1778490193392; Mon, 11 May 2026 02:03:13 -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-48e6d8946a5sm98618955e9.0.2026.05.11.02.03.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 May 2026 02:03:12 -0700 (PDT) Date: Mon, 11 May 2026 05:03:08 -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 resend v6 15/30] mm: memfd: skip zeroing for zeroed hugetlb pool pages 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 gather_surplus_pages() pre-allocates hugetlb pages into the pool during mmap. Pass __GFP_ZERO so these pages are zeroed by the buddy allocator, and HPG_zeroed is set by alloc_surplus_hugetlb_folio. Add bool *zeroed output to alloc_hugetlb_folio_reserve() so callers can check whether the pool page is known-zero. memfd's memfd_alloc_folio() uses this to skip the explicit folio_zero_user() when the page is already zero. This avoids redundant zeroing for memfd hugetlb pages that were pre-allocated into the pool and never mapped to userspace. Signed-off-by: Michael S. Tsirkin Assisted-by: Claude:claude-opus-4-6 --- include/linux/hugetlb.h | 6 ++++-- mm/hugetlb.c | 11 +++++++++-- mm/memfd.c | 14 ++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 950e1702fbd8..c4e66a371fce 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -714,7 +714,8 @@ struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid, nodemask_t *nmask, gfp_t gfp_mask, bool allow_alloc_fallback); struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, - nodemask_t *nmask, gfp_t gfp_mask); + nodemask_t *nmask, gfp_t gfp_mask, + bool *zeroed); int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping, pgoff_t idx); @@ -1142,7 +1143,8 @@ static inline struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, static inline struct folio * alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, - nodemask_t *nmask, gfp_t gfp_mask) + nodemask_t *nmask, gfp_t gfp_mask, + bool *zeroed) { return NULL; } diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 8710366d14b7..03ad5c1e0655 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2205,7 +2205,7 @@ struct folio *alloc_buddy_hugetlb_folio_with_mpol(struct hstate *h, } struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, - nodemask_t *nmask, gfp_t gfp_mask) + nodemask_t *nmask, gfp_t gfp_mask, bool *zeroed) { struct folio *folio; @@ -2221,6 +2221,12 @@ struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, h->resv_huge_pages--; spin_unlock_irq(&hugetlb_lock); + + if (zeroed && folio) { + *zeroed = folio_test_hugetlb_zeroed(folio); + folio_clear_hugetlb_zeroed(folio); + } + return folio; } @@ -2305,7 +2311,8 @@ static int gather_surplus_pages(struct hstate *h, long delta) * It is okay to use NUMA_NO_NODE because we use numa_mem_id() * down the road to pick the current node if that is the case. */ - folio = alloc_surplus_hugetlb_folio(h, htlb_alloc_mask(h), + folio = alloc_surplus_hugetlb_folio(h, + htlb_alloc_mask(h) | __GFP_ZERO, NUMA_NO_NODE, &alloc_nodemask, USER_ADDR_NONE); if (!folio) { diff --git a/mm/memfd.c b/mm/memfd.c index fb425f4e315f..5518f7d2d91f 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -69,6 +69,7 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) #ifdef CONFIG_HUGETLB_PAGE struct folio *folio; gfp_t gfp_mask; + bool zeroed; if (is_file_hugepages(memfd)) { /* @@ -93,17 +94,18 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) folio = alloc_hugetlb_folio_reserve(h, numa_node_id(), NULL, - gfp_mask); + gfp_mask, + &zeroed); if (folio) { u32 hash; /* - * Zero the folio to prevent information leaks to userspace. - * Use folio_zero_user() which is optimized for huge/gigantic - * pages. Pass 0 as addr_hint since this is not a faulting path - * and we don't have a user virtual address yet. + * Zero the folio to prevent information leaks to + * userspace. Skip if the pool page is known-zero + * (HPG_zeroed set during pool pre-allocation). */ - folio_zero_user(folio, 0); + if (!zeroed) + folio_zero_user(folio, 0); /* * Mark the folio uptodate before adding to page cache, -- MST