* [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks
@ 2025-07-01 14:48 alexjlzheng
2025-07-01 18:47 ` Darrick J. Wong
2025-07-03 13:52 ` Christoph Hellwig
0 siblings, 2 replies; 11+ messages in thread
From: alexjlzheng @ 2025-07-01 14:48 UTC (permalink / raw)
To: brauner, djwong; +Cc: linux-xfs, linux-fsdevel, linux-kernel, Jinliang Zheng
From: Jinliang Zheng <alexjlzheng@tencent.com>
In the buffer write path, iomap_set_range_uptodate() is called every
time iomap_end_write() is called. But if folio_test_uptodate() holds, we
know that all blocks in this folio are already in the uptodate state, so
there is no need to go deep into the critical section of state_lock to
execute bitmap_set().
Although state_lock may not have significant lock contention due to
folio lock, this patch at least reduces the number of instructions.
Signed-off-by: Jinliang Zheng <alexjlzheng@tencent.com>
---
fs/iomap/buffered-io.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 3729391a18f3..fb4519158f3a 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -71,6 +71,9 @@ static void iomap_set_range_uptodate(struct folio *folio, size_t off,
unsigned long flags;
bool uptodate = true;
+ if (folio_test_uptodate(folio))
+ return;
+
if (ifs) {
spin_lock_irqsave(&ifs->state_lock, flags);
uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
--
2.49.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks
2025-07-01 14:48 [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks alexjlzheng
@ 2025-07-01 18:47 ` Darrick J. Wong
2025-07-02 12:09 ` Jinliang Zheng
2025-07-03 13:52 ` Christoph Hellwig
1 sibling, 1 reply; 11+ messages in thread
From: Darrick J. Wong @ 2025-07-01 18:47 UTC (permalink / raw)
To: alexjlzheng
Cc: brauner, linux-xfs, linux-fsdevel, linux-kernel, Jinliang Zheng
On Tue, Jul 01, 2025 at 10:48:47PM +0800, alexjlzheng@gmail.com wrote:
> From: Jinliang Zheng <alexjlzheng@tencent.com>
>
> In the buffer write path, iomap_set_range_uptodate() is called every
> time iomap_end_write() is called. But if folio_test_uptodate() holds, we
> know that all blocks in this folio are already in the uptodate state, so
> there is no need to go deep into the critical section of state_lock to
> execute bitmap_set().
>
> Although state_lock may not have significant lock contention due to
> folio lock, this patch at least reduces the number of instructions.
>
> Signed-off-by: Jinliang Zheng <alexjlzheng@tencent.com>
> ---
> fs/iomap/buffered-io.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
> index 3729391a18f3..fb4519158f3a 100644
> --- a/fs/iomap/buffered-io.c
> +++ b/fs/iomap/buffered-io.c
> @@ -71,6 +71,9 @@ static void iomap_set_range_uptodate(struct folio *folio, size_t off,
> unsigned long flags;
> bool uptodate = true;
>
> + if (folio_test_uptodate(folio))
> + return;
Looks fine, but how exhaustively have you tested this with heavy IO
workloads? I /think/ it's the case that folios always creep towards
ifs_is_fully_uptodate() == true state and once they've gotten there
never go back. But folio state bugs are tricky to detect once they've
crept in.
--D
> +
> if (ifs) {
> spin_lock_irqsave(&ifs->state_lock, flags);
> uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
> --
> 2.49.0
>
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks
2025-07-01 18:47 ` Darrick J. Wong
@ 2025-07-02 12:09 ` Jinliang Zheng
2025-07-02 18:52 ` Darrick J. Wong
2025-07-03 13:50 ` Christoph Hellwig
0 siblings, 2 replies; 11+ messages in thread
From: Jinliang Zheng @ 2025-07-02 12:09 UTC (permalink / raw)
To: djwong
Cc: alexjlzheng, alexjlzheng, brauner, linux-fsdevel, linux-kernel,
linux-xfs
On Tue, 1 Jul 2025 11:47:37 -0700, djwong@kernel.org wrote:
> On Tue, Jul 03, 2025 at 10:48:47PM +0800, alexjlzheng@gmail.com wrote:
> > From: Jinliang Zheng <alexjlzheng@tencent.com>
> >
> > In the buffer write path, iomap_set_range_uptodate() is called every
> > time iomap_end_write() is called. But if folio_test_uptodate() holds, we
> > know that all blocks in this folio are already in the uptodate state, so
> > there is no need to go deep into the critical section of state_lock to
> > execute bitmap_set().
> >
> > Although state_lock may not have significant lock contention due to
> > folio lock, this patch at least reduces the number of instructions.
> >
> > Signed-off-by: Jinliang Zheng <alexjlzheng@tencent.com>
> > ---
> > fs/iomap/buffered-io.c | 3 +++
> > 1 file changed, 3 insertions(+)
> >
> > diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
> > index 3729391a18f3..fb4519158f3a 100644
> > --- a/fs/iomap/buffered-io.c
> > +++ b/fs/iomap/buffered-io.c
> > @@ -71,6 +71,9 @@ static void iomap_set_range_uptodate(struct folio *folio, size_t off,
> > unsigned long flags;
> > bool uptodate = true;
> >
> > + if (folio_test_uptodate(folio))
> > + return;
>
> Looks fine, but how exhaustively have you tested this with heavy IO
> workloads? I /think/ it's the case that folios always creep towards
> ifs_is_fully_uptodate() == true state and once they've gotten there
> never go back. But folio state bugs are tricky to detect once they've
> crept in.
I tested fio, ltp and xfstests combined for about 30 hours. The command
used for fio test is:
fio --name=4k-rw \
--filename=/data2/testfile \
--size=1G \
--bs=4096 \
--ioengine=libaio \
--iodepth=32 \
--rw=randrw \
--direct=0 \
--buffered=1 \
--numjobs=16 \
--runtime=60 \
--time_based \
--group_reporting
ltp and xfstests showed no noticeable errors caused by this patch.
thanks,
Jinliang Zheng. :)
>
> --D
>
> > +
> > if (ifs) {
> > spin_lock_irqsave(&ifs->state_lock, flags);
> > uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
> > --
> > 2.49.0
> >
> >
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks
2025-07-02 12:09 ` Jinliang Zheng
@ 2025-07-02 18:52 ` Darrick J. Wong
2025-07-03 13:50 ` Christoph Hellwig
1 sibling, 0 replies; 11+ messages in thread
From: Darrick J. Wong @ 2025-07-02 18:52 UTC (permalink / raw)
To: Jinliang Zheng
Cc: alexjlzheng, brauner, linux-fsdevel, linux-kernel, linux-xfs
On Wed, Jul 02, 2025 at 08:09:12PM +0800, Jinliang Zheng wrote:
> On Tue, 1 Jul 2025 11:47:37 -0700, djwong@kernel.org wrote:
> > On Tue, Jul 03, 2025 at 10:48:47PM +0800, alexjlzheng@gmail.com wrote:
> > > From: Jinliang Zheng <alexjlzheng@tencent.com>
> > >
> > > In the buffer write path, iomap_set_range_uptodate() is called every
> > > time iomap_end_write() is called. But if folio_test_uptodate() holds, we
> > > know that all blocks in this folio are already in the uptodate state, so
> > > there is no need to go deep into the critical section of state_lock to
> > > execute bitmap_set().
> > >
> > > Although state_lock may not have significant lock contention due to
> > > folio lock, this patch at least reduces the number of instructions.
> > >
> > > Signed-off-by: Jinliang Zheng <alexjlzheng@tencent.com>
> > > ---
> > > fs/iomap/buffered-io.c | 3 +++
> > > 1 file changed, 3 insertions(+)
> > >
> > > diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
> > > index 3729391a18f3..fb4519158f3a 100644
> > > --- a/fs/iomap/buffered-io.c
> > > +++ b/fs/iomap/buffered-io.c
> > > @@ -71,6 +71,9 @@ static void iomap_set_range_uptodate(struct folio *folio, size_t off,
> > > unsigned long flags;
> > > bool uptodate = true;
> > >
> > > + if (folio_test_uptodate(folio))
> > > + return;
> >
> > Looks fine, but how exhaustively have you tested this with heavy IO
> > workloads? I /think/ it's the case that folios always creep towards
> > ifs_is_fully_uptodate() == true state and once they've gotten there
> > never go back. But folio state bugs are tricky to detect once they've
> > crept in.
>
> I tested fio, ltp and xfstests combined for about 30 hours. The command
> used for fio test is:
>
> fio --name=4k-rw \
> --filename=/data2/testfile \
> --size=1G \
> --bs=4096 \
> --ioengine=libaio \
> --iodepth=32 \
> --rw=randrw \
> --direct=0 \
> --buffered=1 \
> --numjobs=16 \
> --runtime=60 \
> --time_based \
> --group_reporting
>
> ltp and xfstests showed no noticeable errors caused by this patch.
<nod> I think this fine then...
Reviewed-by: "Darrick J. Wong" <djwong@kernel.org>
--D
>
> thanks,
> Jinliang Zheng. :)
>
> >
> > --D
> >
> > > +
> > > if (ifs) {
> > > spin_lock_irqsave(&ifs->state_lock, flags);
> > > uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
> > > --
> > > 2.49.0
> > >
> > >
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks
2025-07-02 12:09 ` Jinliang Zheng
2025-07-02 18:52 ` Darrick J. Wong
@ 2025-07-03 13:50 ` Christoph Hellwig
2025-07-03 14:33 ` Jinliang Zheng
1 sibling, 1 reply; 11+ messages in thread
From: Christoph Hellwig @ 2025-07-03 13:50 UTC (permalink / raw)
To: Jinliang Zheng
Cc: djwong, alexjlzheng, brauner, linux-fsdevel, linux-kernel,
linux-xfs
On Wed, Jul 02, 2025 at 08:09:12PM +0800, Jinliang Zheng wrote:
> ltp and xfstests showed no noticeable errors caused by this patch.
With what block and page size? I guess it was block size < PAGE_SIZE
as otherwise you wouldn't want to optimize this past, but just asking
in case.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks
2025-07-01 14:48 [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks alexjlzheng
2025-07-01 18:47 ` Darrick J. Wong
@ 2025-07-03 13:52 ` Christoph Hellwig
2025-07-03 17:34 ` Matthew Wilcox
2025-07-09 3:30 ` Jinliang Zheng
1 sibling, 2 replies; 11+ messages in thread
From: Christoph Hellwig @ 2025-07-03 13:52 UTC (permalink / raw)
To: alexjlzheng
Cc: brauner, djwong, linux-xfs, linux-fsdevel, linux-kernel,
Jinliang Zheng, linux-mm
On Tue, Jul 01, 2025 at 10:48:47PM +0800, alexjlzheng@gmail.com wrote:
> From: Jinliang Zheng <alexjlzheng@tencent.com>
>
> In the buffer write path, iomap_set_range_uptodate() is called every
> time iomap_end_write() is called. But if folio_test_uptodate() holds, we
> know that all blocks in this folio are already in the uptodate state, so
> there is no need to go deep into the critical section of state_lock to
> execute bitmap_set().
>
> Although state_lock may not have significant lock contention due to
> folio lock, this patch at least reduces the number of instructions.
That means the uptodate bitmap is stale in that case. That would
only matter if we could clear the folio uptodate bit and still
expect the page content to survive. Which sounds dubious and I could
not find anything relevant grepping the tree, but I'm adding the
linux-mm list just in case.
>
> Signed-off-by: Jinliang Zheng <alexjlzheng@tencent.com>
> ---
> fs/iomap/buffered-io.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
> index 3729391a18f3..fb4519158f3a 100644
> --- a/fs/iomap/buffered-io.c
> +++ b/fs/iomap/buffered-io.c
> @@ -71,6 +71,9 @@ static void iomap_set_range_uptodate(struct folio *folio, size_t off,
> unsigned long flags;
> bool uptodate = true;
>
> + if (folio_test_uptodate(folio))
> + return;
> +
> if (ifs) {
> spin_lock_irqsave(&ifs->state_lock, flags);
> uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
> --
> 2.49.0
>
>
---end quoted text---
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks
2025-07-03 13:50 ` Christoph Hellwig
@ 2025-07-03 14:33 ` Jinliang Zheng
0 siblings, 0 replies; 11+ messages in thread
From: Jinliang Zheng @ 2025-07-03 14:33 UTC (permalink / raw)
To: hch
Cc: alexjlzheng, alexjlzheng, brauner, djwong, linux-fsdevel,
linux-kernel, linux-xfs
On Thu, 3 Jul 2025 06:50:24 -0700, Christoph Hellwig wrote:
> On Wed, Jul 03, 2025 at 08:09:12PM +0800, Jinliang Zheng wrote:
> > ltp and xfstests showed no noticeable errors caused by this patch.
>
> With what block and page size? I guess it was block size < PAGE_SIZE
> as otherwise you wouldn't want to optimize this past, but just asking
> in case.
Hahaha, I really want to try -b size=512, but I don't want to turn off
crc, so I can only choose -b size=1024.
By the way, the test was done on xfs.
thanks,
Jinliang Zheng. :)
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks
2025-07-03 13:52 ` Christoph Hellwig
@ 2025-07-03 17:34 ` Matthew Wilcox
2025-07-07 4:08 ` Jinliang Zheng
2025-07-09 3:30 ` Jinliang Zheng
1 sibling, 1 reply; 11+ messages in thread
From: Matthew Wilcox @ 2025-07-03 17:34 UTC (permalink / raw)
To: Christoph Hellwig
Cc: alexjlzheng, brauner, djwong, linux-xfs, linux-fsdevel,
linux-kernel, Jinliang Zheng, linux-mm
On Thu, Jul 03, 2025 at 06:52:44AM -0700, Christoph Hellwig wrote:
> On Tue, Jul 01, 2025 at 10:48:47PM +0800, alexjlzheng@gmail.com wrote:
> > From: Jinliang Zheng <alexjlzheng@tencent.com>
> >
> > In the buffer write path, iomap_set_range_uptodate() is called every
> > time iomap_end_write() is called. But if folio_test_uptodate() holds, we
> > know that all blocks in this folio are already in the uptodate state, so
> > there is no need to go deep into the critical section of state_lock to
> > execute bitmap_set().
> >
> > Although state_lock may not have significant lock contention due to
> > folio lock, this patch at least reduces the number of instructions.
>
> That means the uptodate bitmap is stale in that case. That would
> only matter if we could clear the folio uptodate bit and still
> expect the page content to survive. Which sounds dubious and I could
> not find anything relevant grepping the tree, but I'm adding the
> linux-mm list just in case.
Once a folio is uptodate, there is no route back to !uptodate without
going through the removal of the folio from the page cache. The read()
path relies on this for example; once it has a refcount on the folio,
and has checked the uptodate bit, it will copy the contents to userspace.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks
2025-07-03 17:34 ` Matthew Wilcox
@ 2025-07-07 4:08 ` Jinliang Zheng
0 siblings, 0 replies; 11+ messages in thread
From: Jinliang Zheng @ 2025-07-07 4:08 UTC (permalink / raw)
To: willy
Cc: alexjlzheng, alexjlzheng, brauner, djwong, hch, linux-fsdevel,
linux-kernel, linux-mm, linux-xfs
On Thu, 3 Jul 2025 18:34:20 +0100, Matthew Wilcox wrote:
> On Thu, Jul 03, 2025 at 06:52:44AM -0700, Christoph Hellwig wrote:
> > On Tue, Jul 01, 2025 at 10:48:47PM +0800, alexjlzheng@gmail.com wrote:
> > > From: Jinliang Zheng <alexjlzheng@tencent.com>
> > >
> > > In the buffer write path, iomap_set_range_uptodate() is called every
> > > time iomap_end_write() is called. But if folio_test_uptodate() holds, we
> > > know that all blocks in this folio are already in the uptodate state, so
> > > there is no need to go deep into the critical section of state_lock to
> > > execute bitmap_set().
> > >
> > > Although state_lock may not have significant lock contention due to
> > > folio lock, this patch at least reduces the number of instructions.
> >
> > That means the uptodate bitmap is stale in that case. That would
> > only matter if we could clear the folio uptodate bit and still
> > expect the page content to survive. Which sounds dubious and I could
> > not find anything relevant grepping the tree, but I'm adding the
> > linux-mm list just in case.
>
> Once a folio is uptodate, there is no route back to !uptodate without
> going through the removal of the folio from the page cache. The read()
> path relies on this for example; once it has a refcount on the folio,
> and has checked the uptodate bit, it will copy the contents to userspace.
I agree, and this aligns with my perspective. Thank you for confirming this.
Jinliang Zheng. :)
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks
2025-07-03 13:52 ` Christoph Hellwig
2025-07-03 17:34 ` Matthew Wilcox
@ 2025-07-09 3:30 ` Jinliang Zheng
2025-07-11 7:44 ` Christoph Hellwig
1 sibling, 1 reply; 11+ messages in thread
From: Jinliang Zheng @ 2025-07-09 3:30 UTC (permalink / raw)
To: hch
Cc: alexjlzheng, alexjlzheng, brauner, djwong, linux-fsdevel,
linux-kernel, linux-mm, linux-xfs
On Thu, 3 Jul 2025 06:52:44 -0700, Christoph Hellwig wrote:
> On Tue, Jul 01, 2025 at 10:48:47PM +0800, alexjlzheng@gmail.com wrote:
> > From: Jinliang Zheng <alexjlzheng@tencent.com>
> >
> > In the buffer write path, iomap_set_range_uptodate() is called every
> > time iomap_end_write() is called. But if folio_test_uptodate() holds, we
> > know that all blocks in this folio are already in the uptodate state, so
> > there is no need to go deep into the critical section of state_lock to
> > execute bitmap_set().
> >
> > Although state_lock may not have significant lock contention due to
> > folio lock, this patch at least reduces the number of instructions.
>
> That means the uptodate bitmap is stale in that case. That would
Hi, after days of silence, I re-read this email thread to make sure I
didn't miss something important.
I realized that maybe we are not aligned and I didn't understand your
sentence above. Would you mind explaining your meaning in more detail?
In addition, what I want to say is that once folio_test_uptodate() is
true, all bits in ifs->state are in the uptodate state. So there is no
need to acquire the lock and set it again. This repeated setting happens
in __iomap_write_end().
thanks,
Jinliang Zheng. :)
> only matter if we could clear the folio uptodate bit and still
> expect the page content to survive. Which sounds dubious and I could
> not find anything relevant grepping the tree, but I'm adding the
> linux-mm list just in case.
>
> >
> > Signed-off-by: Jinliang Zheng <alexjlzheng@tencent.com>
> > ---
> > fs/iomap/buffered-io.c | 3 +++
> > 1 file changed, 3 insertions(+)
> >
> > diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
> > index 3729391a18f3..fb4519158f3a 100644
> > --- a/fs/iomap/buffered-io.c
> > +++ b/fs/iomap/buffered-io.c
> > @@ -71,6 +71,9 @@ static void iomap_set_range_uptodate(struct folio *folio, size_t off,
> > unsigned long flags;
> > bool uptodate = true;
> >
> > + if (folio_test_uptodate(folio))
> > + return;
> > +
> > if (ifs) {
> > spin_lock_irqsave(&ifs->state_lock, flags);
> > uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
> > --
> > 2.49.0
> >
> >
> ---end quoted text---
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks
2025-07-09 3:30 ` Jinliang Zheng
@ 2025-07-11 7:44 ` Christoph Hellwig
0 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2025-07-11 7:44 UTC (permalink / raw)
To: Jinliang Zheng
Cc: hch, alexjlzheng, brauner, djwong, linux-fsdevel, linux-kernel,
linux-mm, linux-xfs
On Wed, Jul 09, 2025 at 11:30:42AM +0800, Jinliang Zheng wrote:
> In addition, what I want to say is that once folio_test_uptodate() is
> true, all bits in ifs->state are in the uptodate state. So there is no
> need to acquire the lock and set it again. This repeated setting happens
> in __iomap_write_end().
Yes, that seems fine. Can you update the commit message with some of
the insights from this discussion, and with that the patch should be
fine.
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2025-07-11 7:44 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-01 14:48 [PATCH] iomap: avoid unnecessary ifs_set_range_uptodate() with locks alexjlzheng
2025-07-01 18:47 ` Darrick J. Wong
2025-07-02 12:09 ` Jinliang Zheng
2025-07-02 18:52 ` Darrick J. Wong
2025-07-03 13:50 ` Christoph Hellwig
2025-07-03 14:33 ` Jinliang Zheng
2025-07-03 13:52 ` Christoph Hellwig
2025-07-03 17:34 ` Matthew Wilcox
2025-07-07 4:08 ` Jinliang Zheng
2025-07-09 3:30 ` Jinliang Zheng
2025-07-11 7:44 ` Christoph Hellwig
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).