[RFC PATCH 1/4] mm/shmem: add SGP_GET to get unlocked folio
From: Chi Zhiling
Date: Fri May 15 2026 - 06:08:00 EST
From: Chi Zhiling <chizhiling@xxxxxxxxxx>
Add a new sgp_type SGP_GET which is similar to SGP_READ but returns
the folio unlocked with an increased refcount. This eliminates the
lock/unlock overhead for read-only operations.
SGP_GET skips folio lock and mapping check, suitable only for
short-lived access. Caller must not rely on folio->mapping validity
as it can become invalid due to concurrent truncate. Safety relies
on refcount and uptodate flag (truncate doesn't clear content).
Signed-off-by: Chi Zhiling <chizhiling@xxxxxxxxxx>
---
include/linux/shmem_fs.h | 3 ++-
mm/shmem.c | 15 ++++++++++++---
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
index 93a0ba872ebe..24698faea5a4 100644
--- a/include/linux/shmem_fs.h
+++ b/include/linux/shmem_fs.h
@@ -164,7 +164,8 @@ extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
/* Flag allocation requirements to shmem_get_folio */
enum sgp_type {
- SGP_READ, /* don't exceed i_size, don't allocate page */
+ SGP_GET, /* don't exceed i_size, don't allocate page, don't lock */
+ SGP_READ, /* don't exceed i_size, don't allocate page, lock folio */
SGP_NOALLOC, /* similar, but fail on hole or use fallocated page */
SGP_CACHE, /* don't exceed i_size, may allocate page */
SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
diff --git a/mm/shmem.c b/mm/shmem.c
index 3b5dc21b323c..ef19968cc51c 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2504,6 +2504,13 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
}
if (folio) {
+ if (sgp == SGP_GET) {
+ if (!folio_test_uptodate(folio)) {
+ folio_put(folio);
+ folio = NULL;
+ }
+ goto out;
+ }
folio_lock(folio);
/* Has the folio been truncated or swapped out? */
@@ -2524,11 +2531,11 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
}
/*
- * SGP_READ: succeed on hole, with NULL folio, letting caller zero.
+ * SGP_READ/SGP_GET: succeed on hole, with NULL folio, letting caller zero.
* SGP_NOALLOC: fail on hole, with NULL folio, letting caller fail.
*/
*foliop = NULL;
- if (sgp == SGP_READ)
+ if (sgp == SGP_READ || sgp == SGP_GET)
return 0;
if (sgp == SGP_NOALLOC)
return -ENOENT;
@@ -2649,13 +2656,15 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
* @sgp: SGP_* flags to control behavior
*
* Looks up the page cache entry at @inode & @index. If a folio is
- * present, it is returned locked with an increased refcount.
+ * present, it is returned locked with an increased refcount, except
+ * for SGP_GET which returns the folio unlocked with an increased refcount.
*
* If the caller modifies data in the folio, it must call folio_mark_dirty()
* before unlocking the folio to ensure that the folio is not reclaimed.
* There is no need to reserve space before calling folio_mark_dirty().
*
* When no folio is found, the behavior depends on @sgp:
+ * - for SGP_GET, *@foliop is %NULL and 0 is returned
* - for SGP_READ, *@foliop is %NULL and 0 is returned
* - for SGP_NOALLOC, *@foliop is %NULL and -ENOENT is returned
* - for all other flags a new folio is allocated, inserted into the
--
2.43.0