[RFC PATCH 2/3] mm/filemap: Avoid modifying iocb->ki_flags for AIO in filemap_get_pages()

Chi Zhiling posted 3 patches 2 months, 2 weeks ago
There is a newer version of this series
[RFC PATCH 2/3] mm/filemap: Avoid modifying iocb->ki_flags for AIO in filemap_get_pages()
Posted by Chi Zhiling 2 months, 2 weeks ago
From: Chi Zhiling <chizhiling@kylinos.cn>

Setting IOCB_NOWAIT in filemap_get_pages() for AIO is only used to
indicate not to block in the filemap_update_page(), with no other purpose.
Moreover, in filemap_read(), IOCB_NOWAIT will be set again for AIO.

Therefore, adding a parameter to the filemap_update_page function to
explicitly indicate not to block serves the same purpose as indicating
through iocb->ki_flags, thus avoiding modifications to iocb->ki_flags.

This patch does not change the original logic and is preparation for the
next patch.

Signed-off-by: Chi Zhiling <chizhiling@kylinos.cn>
---
 mm/filemap.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index af12d1cecc7d..350080f579ef 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2454,11 +2454,15 @@ static bool filemap_range_uptodate(struct address_space *mapping,
 
 static int filemap_update_page(struct kiocb *iocb,
 		struct address_space *mapping, size_t count,
-		struct folio *folio, bool need_uptodate)
+		struct folio *folio, bool need_uptodate,
+		bool no_wait)
 {
 	int error;
+	int flags = iocb->ki_flags;
+	if (no_wait)
+		flags |= IOCB_NOWAIT;
 
-	if (iocb->ki_flags & IOCB_NOWAIT) {
+	if (flags & IOCB_NOWAIT) {
 		if (!filemap_invalidate_trylock_shared(mapping))
 			return -EAGAIN;
 	} else {
@@ -2467,9 +2471,9 @@ static int filemap_update_page(struct kiocb *iocb,
 
 	if (!folio_trylock(folio)) {
 		error = -EAGAIN;
-		if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO))
+		if (flags & (IOCB_NOWAIT | IOCB_NOIO))
 			goto unlock_mapping;
-		if (!(iocb->ki_flags & IOCB_WAITQ)) {
+		if (!(flags & IOCB_WAITQ)) {
 			filemap_invalidate_unlock_shared(mapping);
 			/*
 			 * This is where we usually end up waiting for a
@@ -2493,7 +2497,7 @@ static int filemap_update_page(struct kiocb *iocb,
 		goto unlock;
 
 	error = -EAGAIN;
-	if (iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ))
+	if (flags & (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ))
 		goto unlock;
 
 	error = filemap_read_folio(iocb->ki_filp, mapping->a_ops->read_folio,
@@ -2621,11 +2625,13 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count,
 			goto err;
 	}
 	if (!folio_test_uptodate(folio)) {
+		bool no_wait = false;
+
 		if ((iocb->ki_flags & IOCB_WAITQ) &&
 		    folio_batch_count(fbatch) > 1)
-			iocb->ki_flags |= IOCB_NOWAIT;
+			no_wait = true;
 		err = filemap_update_page(iocb, mapping, count, folio,
-					  need_uptodate);
+					  need_uptodate, no_wait);
 		if (err)
 			goto err;
 	}
-- 
2.43.0
Re: [RFC PATCH 2/3] mm/filemap: Avoid modifying iocb->ki_flags for AIO in filemap_get_pages()
Posted by Matthew Wilcox 2 months, 2 weeks ago
On Wed, Jul 23, 2025 at 06:18:24PM +0800, Chi Zhiling wrote:
> From: Chi Zhiling <chizhiling@kylinos.cn>
> 
> Setting IOCB_NOWAIT in filemap_get_pages() for AIO is only used to
> indicate not to block in the filemap_update_page(), with no other purpose.
> Moreover, in filemap_read(), IOCB_NOWAIT will be set again for AIO.
> 
> Therefore, adding a parameter to the filemap_update_page function to
> explicitly indicate not to block serves the same purpose as indicating
> through iocb->ki_flags, thus avoiding modifications to iocb->ki_flags.
> 
> This patch does not change the original logic and is preparation for the
> next patch.

Passing multiple booleans to a function is an antipattern.
Particularly in this case, since we could just pass iocb->ki_flags
to the function.

But I think there's a less complicated way to do what you want.
Just don't call filemap_update_page() if there are uptodate folios
in the batch:

+++ b/mm/filemap.c
@@ -2616,9 +2616,10 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count,
                        goto err;
        }
        if (!folio_test_uptodate(folio)) {
-               if ((iocb->ki_flags & IOCB_WAITQ) &&
-                   folio_batch_count(fbatch) > 1)
-                       iocb->ki_flags |= IOCB_NOWAIT;
+               if (folio_batch_count(fbatch) > 1) {
+                       err = -EAGAIN;
+                       goto err;
+               }
                err = filemap_update_page(iocb, mapping, count, folio,
                                          need_uptodate);
                if (err)
Re: [RFC PATCH 2/3] mm/filemap: Avoid modifying iocb->ki_flags for AIO in filemap_get_pages()
Posted by Chi Zhiling 2 months, 2 weeks ago
On 2025/7/23 22:33, Matthew Wilcox wrote:
> On Wed, Jul 23, 2025 at 06:18:24PM +0800, Chi Zhiling wrote:
>> From: Chi Zhiling <chizhiling@kylinos.cn>
>>
>> Setting IOCB_NOWAIT in filemap_get_pages() for AIO is only used to
>> indicate not to block in the filemap_update_page(), with no other purpose.
>> Moreover, in filemap_read(), IOCB_NOWAIT will be set again for AIO.
>>
>> Therefore, adding a parameter to the filemap_update_page function to
>> explicitly indicate not to block serves the same purpose as indicating
>> through iocb->ki_flags, thus avoiding modifications to iocb->ki_flags.
>>
>> This patch does not change the original logic and is preparation for the
>> next patch.
> 
> Passing multiple booleans to a function is an antipattern.
> Particularly in this case, since we could just pass iocb->ki_flags
> to the function.
> 
> But I think there's a less complicated way to do what you want.
> Just don't call filemap_update_page() if there are uptodate folios
> in the batch:
> 
> +++ b/mm/filemap.c
> @@ -2616,9 +2616,10 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count,
>                          goto err;
>          }
>          if (!folio_test_uptodate(folio)) {
> -               if ((iocb->ki_flags & IOCB_WAITQ) &&
> -                   folio_batch_count(fbatch) > 1)
> -                       iocb->ki_flags |= IOCB_NOWAIT;
> +               if (folio_batch_count(fbatch) > 1) {
> +                       err = -EAGAIN;
> +                       goto err;
> +               }


Yes, this is a completely better way.


Would you mind if I add
"Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>"
in the next version?

>                  err = filemap_update_page(iocb, mapping, count, folio,
>                                            need_uptodate);
>                  if (err)

Thanks,