* [RFC PATCH -next 0/3] fs: Introduce the scope-based resource management for folio_lock/unlock
@ 2024-08-26 7:10 Li Zetao
2024-08-26 7:10 ` [RFC PATCH -next 1/3] mm: Support " Li Zetao
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Li Zetao @ 2024-08-26 7:10 UTC (permalink / raw)
To: viro, brauner, jack, willy, akpm
Cc: lizetao1, linux-fsdevel, linux-kernel, linux-mm
Hi, all
Currently, multiple kernel subsystems are switching to folio, and the
page API is gradually being phased out. When using folio, due to the
need to consider competition, folio needs to be locked, but
folio_lock/unlock is used directly. Developers need to be careful about
releasing the lock at the appropriate location. By using compiler
features, the kernel has implemented scope-based resource access.
Applying these features to folio can mitigate the risk of forgetting to
release folio lock, and at the same time, can remove some of the
controversial goto unlock code.
Some interfaces are currently not suitable for using scope-based
resource management mechanisms, such as iomap_page_mkwrite. This
interface only needs to release the lock when an error occurs, and needs
to hold the folio lock when it succeeds. Maybe we can intervene in the
compiler's automatic cleanup behavior in a certain scenario like
implementing no_free_ptr.
This patchset has been tested by fsstress for a long time, and no
problems were found.
Thanks,
Li Zetao.
Li Zetao (3):
mm: Support scope-based resource management for folio_lock/unlock
buffer: Using scope-based resource instead of folio_lock/unlock
splice: Using scope-based resource instead of folio_lock/unlock
fs/buffer.c | 6 ++----
fs/splice.c | 21 +++++----------------
include/linux/pagemap.h | 4 ++++
3 files changed, 11 insertions(+), 20 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [RFC PATCH -next 1/3] mm: Support scope-based resource management for folio_lock/unlock
2024-08-26 7:10 [RFC PATCH -next 0/3] fs: Introduce the scope-based resource management for folio_lock/unlock Li Zetao
@ 2024-08-26 7:10 ` Li Zetao
2024-08-26 7:10 ` [RFC PATCH -next 2/3] buffer: Using scope-based resource instead of folio_lock/unlock Li Zetao
2024-08-26 7:10 ` [RFC PATCH -next 3/3] splice: " Li Zetao
2 siblings, 0 replies; 4+ messages in thread
From: Li Zetao @ 2024-08-26 7:10 UTC (permalink / raw)
To: viro, brauner, jack, willy, akpm
Cc: lizetao1, linux-fsdevel, linux-kernel, linux-mm
By introducing the DEFINE_LOCK_GUARD_1 definition, it is possible to
lock and unlock of folio's lock based on scope. At the same time, the
use of folio_trylock is also supported.
Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
include/linux/pagemap.h | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index d9c7edb6422b..ed1831c115cc 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -1537,4 +1537,8 @@ unsigned int i_blocks_per_folio(struct inode *inode, struct folio *folio)
{
return folio_size(folio) >> inode->i_blkbits;
}
+
+DEFINE_LOCK_GUARD_1(folio, struct folio, folio_lock(_T->lock), folio_unlock(_T->lock))
+DEFINE_LOCK_GUARD_1_COND(folio, _try, folio_trylock(_T->lock))
+
#endif /* _LINUX_PAGEMAP_H */
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [RFC PATCH -next 2/3] buffer: Using scope-based resource instead of folio_lock/unlock
2024-08-26 7:10 [RFC PATCH -next 0/3] fs: Introduce the scope-based resource management for folio_lock/unlock Li Zetao
2024-08-26 7:10 ` [RFC PATCH -next 1/3] mm: Support " Li Zetao
@ 2024-08-26 7:10 ` Li Zetao
2024-08-26 7:10 ` [RFC PATCH -next 3/3] splice: " Li Zetao
2 siblings, 0 replies; 4+ messages in thread
From: Li Zetao @ 2024-08-26 7:10 UTC (permalink / raw)
To: viro, brauner, jack, willy, akpm
Cc: lizetao1, linux-fsdevel, linux-kernel, linux-mm
Use guard() to manage locking and unlocking a folio, thus avoiding the
use of goto unlock code. Remove the unlock_page label, and return
directly when an error occurs, allowing the compiler to release the
folio's lock.
Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
fs/buffer.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/fs/buffer.c b/fs/buffer.c
index 45eb06cb1a4e..77ab93531a33 100644
--- a/fs/buffer.c
+++ b/fs/buffer.c
@@ -1740,11 +1740,11 @@ void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
* to pin buffers here since we can afford to sleep and
* it scales better than a global spinlock lock.
*/
- folio_lock(folio);
+ guard(folio)(folio);
/* Recheck when the folio is locked which pins bhs */
head = folio_buffers(folio);
if (!head)
- goto unlock_page;
+ continue;
bh = head;
do {
if (!buffer_mapped(bh) || (bh->b_blocknr < block))
@@ -1757,8 +1757,6 @@ void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
next:
bh = bh->b_this_page;
} while (bh != head);
-unlock_page:
- folio_unlock(folio);
}
folio_batch_release(&fbatch);
cond_resched();
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [RFC PATCH -next 3/3] splice: Using scope-based resource instead of folio_lock/unlock
2024-08-26 7:10 [RFC PATCH -next 0/3] fs: Introduce the scope-based resource management for folio_lock/unlock Li Zetao
2024-08-26 7:10 ` [RFC PATCH -next 1/3] mm: Support " Li Zetao
2024-08-26 7:10 ` [RFC PATCH -next 2/3] buffer: Using scope-based resource instead of folio_lock/unlock Li Zetao
@ 2024-08-26 7:10 ` Li Zetao
2 siblings, 0 replies; 4+ messages in thread
From: Li Zetao @ 2024-08-26 7:10 UTC (permalink / raw)
To: viro, brauner, jack, willy, akpm
Cc: lizetao1, linux-fsdevel, linux-kernel, linux-mm
Use guard() to manage locking and unlocking a folio, thus avoiding the
use of goto unlock code. Remove the out_unlock and error label, and
return directly when an error occurs, allowing the compiler to release
the folio's lock.
Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
fs/splice.c | 21 +++++----------------
1 file changed, 5 insertions(+), 16 deletions(-)
diff --git a/fs/splice.c b/fs/splice.c
index 06232d7e505f..bf976f2edfc1 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -120,36 +120,25 @@ static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
struct pipe_buffer *buf)
{
struct folio *folio = page_folio(buf->page);
- int err;
if (!folio_test_uptodate(folio)) {
- folio_lock(folio);
+ guard(folio)(folio);
/*
* Folio got truncated/unhashed. This will cause a 0-byte
* splice, if this is the first page.
*/
- if (!folio->mapping) {
- err = -ENODATA;
- goto error;
- }
+ if (!folio->mapping)
+ return -ENODATA;
/*
* Uh oh, read-error from disk.
*/
- if (!folio_test_uptodate(folio)) {
- err = -EIO;
- goto error;
- }
-
- /* Folio is ok after all, we are done */
- folio_unlock(folio);
+ if (!folio_test_uptodate(folio))
+ return -EIO;
}
return 0;
-error:
- folio_unlock(folio);
- return err;
}
const struct pipe_buf_operations page_cache_pipe_buf_ops = {
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-08-26 7:02 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-26 7:10 [RFC PATCH -next 0/3] fs: Introduce the scope-based resource management for folio_lock/unlock Li Zetao
2024-08-26 7:10 ` [RFC PATCH -next 1/3] mm: Support " Li Zetao
2024-08-26 7:10 ` [RFC PATCH -next 2/3] buffer: Using scope-based resource instead of folio_lock/unlock Li Zetao
2024-08-26 7:10 ` [RFC PATCH -next 3/3] splice: " Li Zetao
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox