From: Alice Ryhl <aliceryhl@google.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Carlos Llamas <cmllamas@google.com>,
Lorenzo Stoakes <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"T.J. Mercier" <tjmercier@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
linux-mm@kvack.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org, "Alice Ryhl" <aliceryhl@google.com>
Subject: [PATCH 1/2] rust: page: add Page::alloc_page_memcg()
Date: Fri, 16 Oct 2026 13:30:16 +0000 [thread overview]
Message-ID: <20261016-binder-memcg-v1-1-d659b8ae088b@google.com> (raw)
In-Reply-To: <20261016-binder-memcg-v1-0-d659b8ae088b@google.com>
Pages allocated on behalf of a userspace process and mapped into its
address space should be accounted to the process's memory cgroup. Thus,
add `Page::alloc_page_memcg()` to charge newly allocated pages using
`mem_cgroup_charge()`, and switch `Page::drop()` from `__free_pages()`
to `put_page()` so that charged folios are uncharged when freed.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
rust/helpers/page.c | 17 +++++++++++++++++
rust/kernel/page.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 66 insertions(+), 1 deletion(-)
diff --git a/rust/helpers/page.c b/rust/helpers/page.c
index f8463fbed2a2..145758a8a954 100644
--- a/rust/helpers/page.c
+++ b/rust/helpers/page.c
@@ -2,6 +2,7 @@
#include <linux/gfp.h>
#include <linux/highmem.h>
+#include <linux/memcontrol.h>
#include <linux/mm.h>
__rust_helper struct page *rust_helper_alloc_pages(gfp_t gfp_mask,
@@ -10,6 +11,22 @@ __rust_helper struct page *rust_helper_alloc_pages(gfp_t gfp_mask,
return alloc_pages(gfp_mask, order);
}
+__rust_helper void rust_helper_put_page(struct page *page)
+{
+ put_page(page);
+}
+
+__rust_helper struct folio *rust_helper_page_folio(struct page *page)
+{
+ return page_folio(page);
+}
+
+__rust_helper int rust_helper_mem_cgroup_charge(struct folio *folio,
+ struct mm_struct *mm, gfp_t gfp)
+{
+ return mem_cgroup_charge(folio, mm, gfp);
+}
+
__rust_helper void *rust_helper_kmap_local_page(struct page *page)
{
return kmap_local_page(page);
diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index 1c0796ea229f..7a36b7dd6c9c 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -10,8 +10,10 @@
bindings,
error::{
code::*,
+ Error,
Result, //
},
+ mm::Mm,
uaccess::UserSliceReader, //
};
use core::{
@@ -195,6 +197,28 @@ pub fn alloc_page(flags: Flags) -> Result<Self, AllocError> {
Ok(Self { page })
}
+ /// Allocates a new page and charges it to the memory cgroup of `mm`.
+ #[inline]
+ pub fn alloc_page_memcg(flags: Flags, mm: &Mm) -> Result<Self, MemcgAllocError> {
+ let page = Self::alloc_page(flags)?;
+ // Note: Charging the folio with `mem_cgroup_charge()` only works because `Page::drop` uses
+ // `put_page()` (which uncharges the folio before freeing it) rather than `__free_pages()`.
+ //
+ // SAFETY: `page` was just allocated, so we hold the only reference to it and it is not
+ // yet charged to any memcg. `mm.as_raw()` is a valid pointer to an `mm_struct`.
+ let ret = unsafe {
+ bindings::mem_cgroup_charge(
+ bindings::page_folio(page.as_ptr()),
+ mm.as_raw(),
+ flags.as_raw(),
+ )
+ };
+ if ret != 0 {
+ return Err(MemcgAllocError::Memcg);
+ }
+ Ok(page)
+ }
+
/// Returns a raw pointer to the page.
pub fn as_ptr(&self) -> *mut bindings::page {
self.page.as_ptr()
@@ -376,7 +400,31 @@ pub unsafe fn copy_from_user_slice_raw(
impl Drop for Page {
#[inline]
fn drop(&mut self) {
+ // We need to use `put_page()` instead of `__free_pages()` to properly handle memcg
+ // accounting.
+ //
// SAFETY: By the type invariants, we have ownership of the page and can free it.
- unsafe { bindings::__free_pages(self.page.as_ptr(), 0) };
+ unsafe { bindings::put_page(self.page.as_ptr()) };
+ }
+}
+
+/// Error returned by [`Page::alloc_page_memcg`].
+#[derive(Copy, Clone, PartialEq, Eq, Debug)]
+pub enum MemcgAllocError {
+ /// Allocating the page failed.
+ Alloc,
+ /// Charging the page to the memory cgroup failed.
+ Memcg,
+}
+
+impl From<AllocError> for MemcgAllocError {
+ fn from(_: AllocError) -> Self {
+ Self::Alloc
+ }
+}
+
+impl From<MemcgAllocError> for Error {
+ fn from(_: MemcgAllocError) -> Error {
+ ENOMEM
}
}
--
2.56.0.212.g129ede20d9-goog
next prev parent reply other threads:[~2026-10-16 13:30 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-16 13:30 [PATCH 0/2] rust_binder: charge allocated pages to memcg Alice Ryhl
2026-10-16 13:30 ` Alice Ryhl [this message]
2026-10-16 18:41 ` [PATCH 1/2] rust: page: add Page::alloc_page_memcg() T.J. Mercier
2026-10-16 13:30 ` [PATCH 2/2] rust_binder: charge allocated pages to memcg Alice Ryhl
2026-10-16 18:41 ` T.J. Mercier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261016-binder-memcg-v1-1-d659b8ae088b@google.com \
--to=aliceryhl@google.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=cmllamas@google.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tjmercier@google.com \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®