From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F71B318ED5 for ; Wed, 25 Feb 2026 07:20:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772004053; cv=none; b=Ij+yk1cuxVih1h0EWaJztjMY0AffUaJ8CxOlFymlxanxuoBh41+JHp/dvjhddfzYv/66q6sePNX9Z+AtKXqaqC/thqB6BSGVtAaBTyi/gNO3FgZ9bRKkdLRhZiNYzwSXoMu6emEG/sCiRCII48tANopetYA1vgngS8H2MPX1Mj8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772004053; c=relaxed/simple; bh=+MHXSU/V11E98W6bAwMdpmIpdBoJLEJvOCREqpfDnG0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=otRoIGnaJydTwqD0iwKoiky2DuoWuVVoqEwGKKjM0Efad4VwGmEUnG/ouPIftaHnGGBlCWyLuoLCK05QBmf06fr/I/5uIbxOXitrkL7+i1jDEgNtDZpJ3oSvkptdXCv/mJuj9IUjk0YNQKadjTMpyIVee9buHOvEWwY6ZpbiyBw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vOaO09bs; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vOaO09bs" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-358e5e33ddcso1398719a91.1 for ; Tue, 24 Feb 2026 23:20:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772004050; x=1772608850; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/OzVXGrSvNTobLr90pUtUa2BKuGgT0v7RjuQ020m/80=; b=vOaO09bso+xnxcobuyNRaQd7HOFqUhH6anqZc4F3sIPJ20xtYFg32HjHLIVuBGkftY AfsT8/nZtZXB1l0Cg3e9f2pTJslLDlUsEkXP77ypCwnH6SWocOCmukxTLor07m1+eyud cbQW4ikwgGRO2PWRc+8QnD6B+vvoqGxS6K9vmfumQcWKefuQh3ggX8pGbIQzY96oiaw2 +QzCjPSpsS7j9Cbi0ftIS5Ylp9alOrI975nDT/1kB3f0FBGE46yhRS+1sX1tFP1JTbvV pAwn4zb9diLL1cg7f6VHZpeuvPhXS7ywJTEzYpznfj47smk8x91NeXJ4LNZ/Aqwd/Mdm GPDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772004050; x=1772608850; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/OzVXGrSvNTobLr90pUtUa2BKuGgT0v7RjuQ020m/80=; b=bCzdlhY/ExfYhcCyiaGuJglWQPyoDZXg7exzufIe7P3eV399GrGtopTtS2M65R6PDZ nL1RGmDcte88SC/9eL5w7MoH0TDgEoq5G++ywxDJuu4GCZSYoNi7bJMIPGJQ4MUBuu+A uUIbB7+FOlEJ8nc1XCXus45EFeEjSh1boxV8KCSWyK/b8hhWKJZ2/ZxY2Z+GpAUz+4ui P2ApF0PFt1WfACVXdhvUvRw/FfnoZ9AKEjxrPQALq/VMXTLUeoSQKXNZYE2WGZHgue6S PCeQjZfK2xiA/5aJMyiviLEtXrjBXHSVSqvLP99mXHkbl0kP1NP2Njg4zQMxoBB5/mwa Irvg== X-Forwarded-Encrypted: i=1; AJvYcCVKOCARLhQGnGHjp+3ifMUV6kXAlP3IHNkEuzS/jRi+rrgznIxME3cSW1h7oXDzKvzVjo0F7XKukbZAZms=@vger.kernel.org X-Gm-Message-State: AOJu0YyhNI8SCHzytu4fVmCsyWnbyAGtxBg18XlLKyy0743hvSY49NCa Dt24qtOhDCwGFmVulhJ/rajmMxVdo04XBoTeJuqZ87Nlfcv1hIb/7LRqzvQYLgVL8jKLKL0ZCpq XTgaPQ3PVS4vqQglayY9gKXTHfQ== X-Received: from pgar14.prod.google.com ([2002:a05:6a02:2e8e:b0:c6e:4387:496a]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3297:b0:35f:27d:2ded with SMTP id adf61e73a8af0-39545ea8a07mr12406592637.25.1772004049471; Tue, 24 Feb 2026 23:20:49 -0800 (PST) Date: Wed, 25 Feb 2026 07:20:38 +0000 In-Reply-To: <20260225-gmem-st-blocks-v2-0-87d7098119a9@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260225-gmem-st-blocks-v2-0-87d7098119a9@google.com> X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Developer-Signature: v=1; a=ed25519-sha256; t=1772004043; l=3726; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=+MHXSU/V11E98W6bAwMdpmIpdBoJLEJvOCREqpfDnG0=; b=KKEMJylkZyQJS+IxTxG9HS0TUG+xGT7E9m0Nde3MM1Szh24vjYNbYhe+Zg9jWgMOtoQmg2xvM IBdn2AEimOhD80lhpH2xrQdgnNMxZuX0jFXVYJErsuwoepz+kV933x6 X-Mailer: b4 0.14.3 Message-ID: <20260225-gmem-st-blocks-v2-3-87d7098119a9@google.com> Subject: [PATCH RFC v2 3/6] fs: Add .unaccount_folio callback From: Ackerley Tng To: Paolo Bonzini , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , "Matthew Wilcox (Oracle)" , Shuah Khan , Jonathan Corbet , Alexander Viro , Christian Brauner , Jan Kara , seanjc@google.com, rientjes@google.com, rick.p.edgecombe@intel.com, yan.y.zhao@intel.com, fvdl@google.com, jthoughton@google.com, vannapurve@google.com, shivankg@amd.com, michael.roth@amd.com, pratyush@kernel.org, pasha.tatashin@soleen.com, kalyazin@amazon.com, tabba@google.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, Ackerley Tng Content-Type: text/plain; charset="utf-8" Add .unaccount_folio callback to allow filesystems to do accounting-related updates to the inode or struct address_space mapping, when the folio is about to be removed from the filemap/page_cache. .free_folio cannot be used since .free_folio cannot assume that struct address_space mapping still exists. >From the name, .invalidate_folio and .release_folio seem suitable, but those are meant only to handle freeing of a folio's private data. .release_folio is also not called in the truncation path. An alternative would be to add a more general callback and call that from filemap_remove_folio() and delete_from_page_cache_batch(). .unaccount_folio was chosen as it is more specific to the how guest_memfd will be using this callback in later patches. Also, .unaccount_folio only needs a single call site. This further refactoring was considered: if (mapping->a_ops->unaccount_folio && mapping->a_ops->unaccount_folio(folio)) ... do generic page_cache unaccounting ... but that was abandoned since a hugetlb folio may not have an associated mapping. Signed-off-by: Ackerley Tng --- Documentation/filesystems/vfs.rst | 8 ++++++++ include/linux/fs.h | 1 + mm/filemap.c | 3 +++ 3 files changed, 12 insertions(+) diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst index 670ba66b60e49..5ed5c43d5768b 100644 --- a/Documentation/filesystems/vfs.rst +++ b/Documentation/filesystems/vfs.rst @@ -809,6 +809,7 @@ cache in your filesystem. The following members are defined: sector_t (*bmap)(struct address_space *, sector_t); void (*invalidate_folio) (struct folio *, size_t start, size_t len); bool (*release_folio)(struct folio *, gfp_t); + void (*unaccount_folio)(struct folio *folio); void (*free_folio)(struct folio *); ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter); int (*migrate_folio)(struct mapping *, struct folio *dst, @@ -967,6 +968,13 @@ cache in your filesystem. The following members are defined: its release_folio will need to ensure this. Possibly it can clear the uptodate flag if it cannot free private data yet. +``unaccount_folio`` + unaccount_folio is called under inode lock and struct + address_space's xa_lock, just before the folio is removed from + the page cache in order to allow updating any kind of + accounting on the inode or address_space mapping while the + address_space mapping still exists. + ``free_folio`` free_folio is called once the folio is no longer visible in the page cache in order to allow the cleanup of any private data. diff --git a/include/linux/fs.h b/include/linux/fs.h index a01621fa636a6..c71f327032142 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -422,6 +422,7 @@ struct address_space_operations { sector_t (*bmap)(struct address_space *, sector_t); void (*invalidate_folio) (struct folio *, size_t offset, size_t len); bool (*release_folio)(struct folio *, gfp_t); + void (*unaccount_folio)(struct folio *folio); void (*free_folio)(struct folio *folio); ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter); /* diff --git a/mm/filemap.c b/mm/filemap.c index ebd75684cb0a7..ff957929e6087 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -176,6 +176,9 @@ static void filemap_unaccount_folio(struct address_space *mapping, } } + if (unlikely(mapping->a_ops->unaccount_folio)) + mapping->a_ops->unaccount_folio(folio); + /* hugetlb folios do not participate in page cache accounting. */ if (folio_test_hugetlb(folio)) return; -- 2.53.0.414.gf7e9f6c205-goog