[RFC PATCH 2/4] mm/shmem: use SGP_GET in read operations

From: Chi Zhiling

Date: Fri May 15 2026 - 06:03:37 EST


From: Chi Zhiling <chizhiling@xxxxxxxxxx>

Replace SGP_READ with SGP_GET in shmem_file_read_iter(),
shmem_file_splice_read(), and shmem_get_link(). These functions
immediately unlock the folio after getting it, making the lock
acquisition redundant.

Even though folio_lock can protect folio data consistency or prevent
truncate while holding the lock, these can still happen after unlock.
Since these functions continue reading data after unlocking, the lock
does not provide effective protection. The folio reference count is
what actually prevents reclamation during access, making the lock
unnecessary.

Signed-off-by: Chi Zhiling <chizhiling@xxxxxxxxxx>
---
mm/shmem.c | 12 +++---------
1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/mm/shmem.c b/mm/shmem.c
index ef19968cc51c..767610f78d0d 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -3370,15 +3370,13 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
break;

index = iocb->ki_pos >> PAGE_SHIFT;
- error = shmem_get_folio(inode, index, 0, &folio, SGP_READ);
+ error = shmem_get_folio(inode, index, 0, &folio, SGP_GET);
if (error) {
if (error == -EINVAL)
error = 0;
break;
}
if (folio) {
- folio_unlock(folio);
-
page = folio_file_page(folio, index);
if (PageHWPoison(page)) {
folio_put(folio);
@@ -3561,15 +3559,13 @@ static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
break;

index = *ppos >> PAGE_SHIFT;
- error = shmem_get_folio(inode, index, 0, &folio, SGP_READ);
+ error = shmem_get_folio(inode, index, 0, &folio, SGP_GET);
if (error) {
if (error == -EINVAL)
error = 0;
break;
}
if (folio) {
- folio_unlock(folio);
-
page = folio_file_page(folio, index);
if (PageHWPoison(page)) {
error = -EIO;
@@ -4170,17 +4166,15 @@ static const char *shmem_get_link(struct dentry *dentry, struct inode *inode,
return ERR_PTR(-ECHILD);
}
} else {
- error = shmem_get_folio(inode, 0, 0, &folio, SGP_READ);
+ error = shmem_get_folio(inode, 0, 0, &folio, SGP_GET);
if (error)
return ERR_PTR(error);
if (!folio)
return ERR_PTR(-ECHILD);
if (PageHWPoison(folio_page(folio, 0))) {
- folio_unlock(folio);
folio_put(folio);
return ERR_PTR(-ECHILD);
}
- folio_unlock(folio);
}
set_delayed_call(done, shmem_put_link, folio);
return folio_address(folio);
--
2.43.0