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 1BC093B895E for ; Mon, 8 Jun 2026 08:39:05 +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=1780907949; cv=none; b=lvs1TZ8dfQd5cX+wcBTaOIwucQbyA56GRqYwJkTAMHcN92NJ6lGylaHcGpDzcWVXl/bF3yLfIcJV4fUwmjLH+tzePpcMrbaCoPlCOrk2AUrtwvYCD1Eq/7l0ylFuABOygugFRJg1EjZNolSqZnEQIroOYkvWQ49leaxkonqGniE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780907949; c=relaxed/simple; bh=JqpRnHMOZCHNy3L9y89dcwb7zbUTjUHqd7YR7olPip0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=f3qWQu3UJnFqZrbu+0RZUEsPL5OFp59AXBK1jMcPGofvPuWB4LyM+DYShHIpj8VuZ2kcC0k9n71rY/gI62YhIAqoW/u+FkhxXd7faKoRf03EXfi19w+1/AycPdwTNDyo7FuOWVjcjjx9R8jtqI08Rzjct77/9caevQ28yzaHJFc= 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=bVEXtFev; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=syHeloBy; 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="bVEXtFev"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="syHeloBy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780907945; 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=DMc8fDrj0D8JcjJ5dRd88tqBSMHmwk7bs26tvZRKcB8=; b=bVEXtFevJKB79zHKYaUfAZVYq5FkvbXosQ4kNoJ1Ky/zMh7zqDCF52oXC6yXHFmMDrMOaC 43DXRF6VC8NFbY9rN1xqQ1fYGCskYe99Mk538u2ya44JWvSAR+ZBy8O+ZY/ID5CQyh6bmV 2UzaY4LUu5aLz/ZGZMkMaqoYFl1AeEQ= 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-108-2OA2lsmDMXiwczzIwWIElw-1; Mon, 08 Jun 2026 04:39:03 -0400 X-MC-Unique: 2OA2lsmDMXiwczzIwWIElw-1 X-Mimecast-MFC-AGG-ID: 2OA2lsmDMXiwczzIwWIElw_1780907943 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-490b37e1f47so36123095e9.0 for ; Mon, 08 Jun 2026 01:39:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780907942; x=1781512742; 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=DMc8fDrj0D8JcjJ5dRd88tqBSMHmwk7bs26tvZRKcB8=; b=syHeloByWO/3ja62bJctJUW9RlVA7+bb5nMX6AQRuPuV8E+hCSCVrTFTuBxgYwLSxk 5VXnkF55EEO/K1MvhTaiuq7brROonoiQYXJqbDC2M5LOZoNkuKnJqLPR2hSffOlAxNeE twdraarMg8owdkPENlsDsJQRCIs/e3FL+RszlOdp+bCJAv8qdJQevz27quaX5UdSy9ul saO9X6RTiUQ5b1e9wOGziHwXlAhN1Vh4wj6bKyDPyjitCkOPe6Rtcxjm/GjoddQnEAVr xRWFm6dX/s42PSKtbUK91tp7aClLTa5CMz+DnGjvEnEtPo0FwvBjZ6x1qy7alBEqHN50 b8MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780907942; x=1781512742; 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=DMc8fDrj0D8JcjJ5dRd88tqBSMHmwk7bs26tvZRKcB8=; b=nu9pMZaAwyMB51nsE4pIMKOFyeww4cEa8Bdi31PvAyo41rT/Azg9zUtVJ0stLT7Ne4 AN1TeWxldSLPgeTEk9pYgi4CDAymw1kclLc4yZR2/XXlQgNF3AEzXbK3Ik8olMuOh7YX XFZYENxQ1sp17M+sJQFCVJYbh0YNSvaEs0yaFgbTGuL6Gw+7ZVfe66M6ysDBuiTL/ilh rGvBZDOAz5YDPv21+Ujjutxo92PfqXBcqQsBs7BSpICRBqIAfZSf9+/tOr+BypgBZ2to NpMmvypbd2Sg8yGLANEYkfsjNzhiYb0eVNiHK/kL8maGIue89vnLbRaGYhgNyke56uyi DPTw== X-Gm-Message-State: AOJu0YyjKdCltAx7nNbHXV/XfDbwwyi739nnoojHV3X8MELjCX6Yju0L VOKjKUlJ+A5UqYjVyoJHtbqQ10kMnwRbD2nNczBLon0zSXxefN4s8OmnUahw465I9WhT/gDEhQo 2W3FDREjMod1iPsgK2tBXfS+QkZHzkY7yB7Ej0P+BUaQFEBIS88Lxyg3jvAphteFz6mkSjcSLHn 7WF2l0WBE2d3PdFGArUDpOqhcarvme+lNsfb+TO0GvFso= X-Gm-Gg: Acq92OFzM/ZH25JRL+OAliTJ2xv6gy+TlXzKsrTeF1aa8w0g1jaxZQ2l1FdeVHrslhz cETGXBvEm2qN+7no3RfmaMHxX2Xpigd3Bsaq5MmWygEyarAea7L+pRpJLK+GMcq1KINUcBSSBCz 27AdXoC9Ol4Di5qG0g5BZvMSTJGyhAhnEHAJeogQXuBXy6QDgnfrS4OKCg2/6yVS8eCHsB+wZid M0sr0O0Xf/WUHen7cxF+GD3D/R5DLKLcT+n9GOivbXH3sdEWdPJzer/7WBzYNpQCCbQu3OENa3a 19UbomE1+a/xk94mZCKrJpFmbM/PHQkqbdeP28HtcJ3PkPyn1G2gQmVWaLtOHYCKoD4TZgrVV3x 44qwd+nl30ZxBRZUNowWhPl8= X-Received: by 2002:a05:600c:5252:b0:490:b2a6:8c1d with SMTP id 5b1f17b1804b1-490c25adb2fmr254958815e9.10.1780907942479; Mon, 08 Jun 2026 01:39:02 -0700 (PDT) X-Received: by 2002:a05:600c:5252:b0:490:b2a6:8c1d with SMTP id 5b1f17b1804b1-490c25adb2fmr254957625e9.10.1780907941897; Mon, 08 Jun 2026 01:39:01 -0700 (PDT) Received: from redhat.com ([31.152.37.159]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4601f2ed944sm52040479f8f.13.2026.06.08.01.38.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jun 2026 01:39:01 -0700 (PDT) Date: Mon, 8 Jun 2026 04:38:54 -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 v10 24/37] mm: add put_page_zeroed and folio_put_zeroed 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.51.2.2891.g4157995a80.dirty X-Mutt-Fcc: =sent Add put_page_zeroed() / folio_put_zeroed() for callers that hold a reference to a page known to be zeroed. If this drops the last reference, the zeroed hint is propagated to the buddy allocator. If someone else still holds a reference, the hint is simply lost - this is best-effort. This is useful for balloon drivers during deflation: the host has already zeroed the pages, and the balloon is typically the sole owner. But if the page happens to be shared, silently dropping the hint is safe and avoids the need for callers to check the refcount. Note: put_page_zeroed uses folio_put_testzero() which only detects sole ownership at the instant of the atomic decrement. A concurrent reference holder (e.g. migration) means the hint is silently lost. This is by design: the zeroed hint is a performance optimization, not a correctness requirement. Losing it just means the next allocation re-zeroes the page. Signed-off-by: Michael S. Tsirkin Assisted-by: Claude:claude-opus-4-6 --- include/linux/mm.h | 13 +++++++++++++ mm/swap.c | 20 ++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 06bbe9eba636..79b3a8cb9a3b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1913,6 +1913,7 @@ static inline struct folio *virt_to_folio(const void *x) } void __folio_put(struct folio *folio); +void __folio_put_zeroed(struct folio *folio); void split_page(struct page *page, unsigned int order); void folio_copy(struct folio *dst, struct folio *src); @@ -2090,6 +2091,18 @@ static inline void folio_put(struct folio *folio) __folio_put(folio); } +/* Caller must be sole owner to guarantee page is still zero */ +static inline void folio_put_zeroed(struct folio *folio) +{ + if (folio_put_testzero(folio)) + __folio_put_zeroed(folio); +} + +static inline void put_page_zeroed(struct page *page) +{ + folio_put_zeroed(page_folio(page)); +} + /** * folio_put_refs - Reduce the reference count on a folio. * @folio: The folio. diff --git a/mm/swap.c b/mm/swap.c index 5cc44f0de987..ecec780172ad 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -94,13 +94,15 @@ static void page_cache_release(struct folio *folio) lruvec_unlock_irqrestore(lruvec, flags); } -void __folio_put(struct folio *folio) +static void ___folio_put(struct folio *folio, bool zeroed) { + /* zeroed hint ignored for now, no current user */ if (unlikely(folio_is_zone_device(folio))) { free_zone_device_folio(folio); return; } + /* zeroed hint ignored for now, no current user */ if (folio_test_hugetlb(folio)) { free_huge_folio(folio); return; @@ -109,10 +111,24 @@ void __folio_put(struct folio *folio) page_cache_release(folio); folio_unqueue_deferred_split(folio); mem_cgroup_uncharge(folio); - free_frozen_pages(&folio->page, folio_order(folio)); + if (zeroed) + free_frozen_pages_zeroed(&folio->page, folio_order(folio)); + else + free_frozen_pages(&folio->page, folio_order(folio)); +} + +void __folio_put(struct folio *folio) +{ + ___folio_put(folio, false); } EXPORT_SYMBOL(__folio_put); +void __folio_put_zeroed(struct folio *folio) +{ + ___folio_put(folio, true); +} +EXPORT_SYMBOL(__folio_put_zeroed); + typedef void (*move_fn_t)(struct lruvec *lruvec, struct folio *folio); static void lru_add(struct lruvec *lruvec, struct folio *folio) -- MST