From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>,
netdev@vger.kernel.org, Vlastimil Babka <vbabka@suse.cz>,
linux-mm@kvack.org, Hannes Reinecke <hare@suse.de>
Subject: [PATCH] mm: Decline to manipulate the refcount on a slab page
Date: Mon, 10 Mar 2025 14:35:24 +0000 [thread overview]
Message-ID: <20250310143544.1216127-1-willy@infradead.org> (raw)
Slab pages now have a refcount of 0, so nobody should be trying to
manipulate the refcount on them. Doing so has little effect; the object
could be freed and reallocated to a different purpose, although the slab
itself would not be until the refcount was put making it behave rather
like TYPESAFE_BY_RCU.
Unfortunately, __iov_iter_get_pages_alloc() does take a refcount.
Fix that to not change the refcount, and make put_page() silently not
change the refcount. get_page() warns so that we can fix any other
callers that need to be changed.
Long-term, networking needs to stop taking a refcount on the pages that
it uses and rely on the caller to hold whatever references are necessary
to make the memory stable. In the medium term, more page types are going
to hav a zero refcount, so we'll want to move get_page() and put_page()
out of line.
Reported-by: Hannes Reinecke <hare@suse.de>
Fixes: 9aec2fb0fd5e (slab: allocate frozen pages)
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
include/linux/mm.h | 7 ++++++-
lib/iov_iter.c | 8 ++++++--
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 61de65c4e430..4e118cbe0556 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1539,7 +1539,10 @@ static inline void folio_get(struct folio *folio)
static inline void get_page(struct page *page)
{
- folio_get(page_folio(page));
+ struct folio *folio = page_folio(page);
+ if (WARN_ON_ONCE(folio_test_slab(folio)))
+ return;
+ folio_get(folio);
}
static inline __must_check bool try_get_page(struct page *page)
@@ -1633,6 +1636,8 @@ static inline void put_page(struct page *page)
{
struct folio *folio = page_folio(page);
+ if (folio_test_slab(folio))
+ return;
folio_put(folio);
}
diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index 65f550cb5081..8c7fdb7d8c8f 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -1190,8 +1190,12 @@ static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
if (!n)
return -ENOMEM;
p = *pages;
- for (int k = 0; k < n; k++)
- get_page(p[k] = page + k);
+ for (int k = 0; k < n; k++) {
+ struct folio *folio = page_folio(page);
+ p[k] = page + k;
+ if (!folio_test_slab(folio))
+ folio_get(folio);
+ }
maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start);
i->count -= maxsize;
i->iov_offset += maxsize;
--
2.47.2
next reply other threads:[~2025-03-10 14:36 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-10 14:35 Matthew Wilcox (Oracle) [this message]
2025-03-10 14:37 ` Vlastimil Babka
2025-03-10 14:50 ` Matthew Wilcox
2025-03-11 10:15 ` Jakub Kicinski
2025-03-11 15:46 ` Hannes Reinecke
2025-03-11 16:59 ` Matthew Wilcox
2025-03-12 5:48 ` Christoph Hellwig
2025-03-13 7:22 ` Hannes Reinecke
2025-03-13 7:36 ` Christoph Hellwig
2025-03-13 8:34 ` Hannes Reinecke
2025-03-13 8:44 ` Christoph Hellwig
2025-03-13 8:52 ` Hannes Reinecke
2025-03-13 9:15 ` Christoph Hellwig
2025-03-12 5:47 ` Christoph Hellwig
[not found] <20250310142750.1209192-1-willy@infradead.org>
[not found] ` <77fa8d7e-4752-4979-affe-aa45c8d7795a@suse.de>
[not found] ` <Z88vUFweLyk5s8UD@casper.infradead.org>
2025-03-11 7:05 ` Hannes Reinecke
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=20250310143544.1216127-1-willy@infradead.org \
--to=willy@infradead.org \
--cc=akpm@linux-foundation.org \
--cc=hare@suse.de \
--cc=linux-mm@kvack.org \
--cc=netdev@vger.kernel.org \
--cc=vbabka@suse.cz \
/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