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 3512530AAD8 for ; Sun, 26 Apr 2026 21:48:43 +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=1777240125; cv=none; b=dU90BnIP2ON638MGfbk1vGkf/HZXtbKrIN76MYd/4d6SyvMIYhLn/DkrLAWxzr4/r8JXISi85vPbgGZ77jidQxnla+NUrut2Nd/oWCkKF3BCz5HBSVUQdeeEb1xXd6GRBxXF0lP6SPuiDc5qhUG2vUhBDNLfPpkZup7F9az0tqM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777240125; c=relaxed/simple; bh=UOL/ZsOQuxZxCdyyXjZBk1f9WnmSmNTDh0iqg53ebK0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OwIbjgyfyjx0lJZ8UmtKUkTvT13QHW3KHyat9qBMEbvFge0L/OAfpZ/dNf+j+IxQoOWGxWTtPifKPNqc0itwMkpNME1sPJYpENbxSY0yKE9+jp/GcyP/71UXKFInZNK1tu/IiGKsNfwdMC/ms0X8NoSTC8Aamz8UFBvbh+DMRZA= 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=J+sbDDem; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=j5STN2aI; 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="J+sbDDem"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="j5STN2aI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1777240123; 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=yr2uOCi7TxHPhFnCx38/iKRYLu7TO1I9ea6WeFSAtGo=; b=J+sbDDemBD3MheA7xfu2vBzvL87YBNPnvZXSV+xw9M+/TF4UqjFTJ0flkUW+VYG7/8ckCx Lgga8DiptalSWJcOIIdXEKbsPmyV9Dg3NxAI+RDcvfqEpaY5b2sqBXjLVHk4KmDCWE41dn x0qRRMJnl0EGVPjYaCwFLCs5ylBeG5Y= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-408-HNVpJhstMQWMUARHw8q_nA-1; Sun, 26 Apr 2026 17:48:42 -0400 X-MC-Unique: HNVpJhstMQWMUARHw8q_nA-1 X-Mimecast-MFC-AGG-ID: HNVpJhstMQWMUARHw8q_nA_1777240121 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-488d2cd2674so74605995e9.0 for ; Sun, 26 Apr 2026 14:48:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1777240120; x=1777844920; 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=yr2uOCi7TxHPhFnCx38/iKRYLu7TO1I9ea6WeFSAtGo=; b=j5STN2aI/nd7ryzC/ocbfZDzSxv+WIxa+yYaeQa+yf/Ax2+mgBwfq5/QNP1eqz9/C8 ZWM9zD+aLh9YojwybLzKI6AUsWUrvEs1WO3hPAvpVfn9JLTUWu56bbTBa8jRcsVGGBZs 01UAmDo7VXSAB6EIBgF9aAfk5ChnW5dbpKX2ZHf1d4RP7laXxYbg70PTP/KI97oWpH6X m8SnecP13HKxOm5HZi4JaIqkTYteXV/d/CaUVqpUrqwO6Npij38q7k4VD6ZJ98Qzumyq YIb/WGClWfcqwqZZ84MTwaiHURhxC1RuqUEsiTnWpjsDfQvTy1baqxNpM9fy6uA2hDyR ncRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777240120; x=1777844920; 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=yr2uOCi7TxHPhFnCx38/iKRYLu7TO1I9ea6WeFSAtGo=; b=JTXgI/8zFI1DeSa1vf18kH167uq+KUkSmqp/alb/Zj272/pFcJKVy3KI69kvxwqQjh dJ171v5wJJalUZexspay0B5DNJnaQ4lwDhwAeAlX+V5M/0IU3ydj8tQE2bbHyy0nPpk4 L3fty9sS0yZP2QOHVFj5kEZDu2dlWAdix4PrCjDqZ/pJaeA0BgDegcsNG/h+32Pcj8DL 0ZYTDsWRxU3ClAuN0vQT4M0Pxd3f4y3Zl/Zx4UxjPVFZ5SJpx4fBmsJcskU6Iq9CSgds 0o/X+A2rCihfD3KjVJy21V7NSAb5CG/Apj2jcPweCMPmLcZO5VyFiONkLsxC65Frq9vZ nzTw== X-Gm-Message-State: AOJu0Yz/zWr4mncR4vMJxlvll1WGMch3dnHc970dY1AbP2jC2J7tCamc WIGRxvksDhnHDVUek0fdBqCza56MKNyl3DQh+IjQwHg9dJ83+8sQ5p+7nC4GiseduzIrdGvdlGV 1kF9us7A1+AuIvKJ3uq2Yd24wz8AevqKa3N0dmFeT2sDv01B/H8dfy20A0cOlPtNARFybPcUArv W0WdF6bXM1Yoh3aPqP23qCEmnBfLg/F1EOHiP9QJ30VCI= X-Gm-Gg: AeBDietb9iUdrs6Q+LID7WqwM/sAbRR9CwSuJ1Mex8hsWyg74oLKe7sye8nVPEK4gAD YhM0HchcK0yDBpP9XWUZF8X94GBWdvaFK6fWD0mzkw9mw/kDNiSaD+XjBLcoD4ZXwcXCNG9CsM2 ChH7b1EJjahHHKWpoPJOYm52ansqYs9Dpx7PT/JbVXso0uhgWZ5YbUoNdBYlPzvWIJR+SWNxwP/ xogw28NEAnq7lBPr9muJ3bDKF4BZTymLw/iwF5U2t6HXbz29jUVhbjyD9wYOqOCWgPUp3VmV4YK 9QRy/YkIrf9mZGXjnfwC6KNoCGTSVPKm6TGZ/ETDn3kZ6gK2md0Siho8wpBXDxDY+FN3MuAle5k 3n8LVHCqIkFPgoyb+X4eWs4kpaTAhuwEi/4wAXDc9yrsn8l2oPWXoXRll X-Received: by 2002:a05:600c:4746:b0:488:9439:881a with SMTP id 5b1f17b1804b1-488fb738412mr555456555e9.2.1777240120470; Sun, 26 Apr 2026 14:48:40 -0700 (PDT) X-Received: by 2002:a05:600c:4746:b0:488:9439:881a with SMTP id 5b1f17b1804b1-488fb738412mr555455875e9.2.1777240119660; Sun, 26 Apr 2026 14:48:39 -0700 (PDT) Received: from redhat.com (IGLD-80-230-47-179.inter.net.il. [80.230.47.179]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-488fb7bf7besm270287995e9.34.2026.04.26.14.48.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Apr 2026 14:48:39 -0700 (PDT) Date: Sun, 26 Apr 2026 17:48:36 -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 , Gregory Price , linux-mm@kvack.org, virtualization@lists.linux.dev, Lorenzo Stoakes , "Liam R. Howlett" , Mike Rapoport , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu Subject: [PATCH RFC v4 21/22] mm: add put_page_zeroed and folio_put_zeroed Message-ID: <111219af35c4bb357e30cf1a77265fb63eecb21c.1777223007.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 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 page goes through __folio_put_zeroed() which calls free_frozen_pages_zeroed() so the zeroed hint is preserved. 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. Signed-off-by: Michael S. Tsirkin Assisted-by: Claude:claude-opus-4-6 --- include/linux/mm.h | 12 ++++++++++++ mm/swap.c | 18 ++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 541d36e5e420..1c6bf82a967a 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1640,6 +1640,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); @@ -1817,6 +1818,17 @@ static inline void folio_put(struct folio *folio) __folio_put(folio); } +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 bb19ccbece46..5d05a463b46a 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -94,7 +94,7 @@ static void page_cache_release(struct folio *folio) unlock_page_lruvec_irqrestore(lruvec, flags); } -void __folio_put(struct folio *folio) +static void ___folio_put(struct folio *folio, bool zeroed) { if (unlikely(folio_is_zone_device(folio))) { free_zone_device_folio(folio); @@ -109,10 +109,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