linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] erofs: fix missing xas_retry() in fscache mode
@ 2022-11-11  9:08 Jingbo Xu
  2022-11-14  4:42 ` Gao Xiang
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Jingbo Xu @ 2022-11-11  9:08 UTC (permalink / raw)
  To: xiang, chao, yinxin.x, linux-erofs; +Cc: linux-kernel, dhowells

The xarray iteration only holds RCU and thus may encounter
XA_RETRY_ENTRY if there's process modifying the xarray concurrently.
This will cause oops when referring to the invalid entry.

Fix this by adding the missing xas_retry(), which will make the
iteration wind back to the root node if XA_RETRY_ENTRY is encountered.

Fixes: d435d53228dd ("erofs: change to use asynchronous io for fscache readpage/readahead")
Signed-off-by: Jingbo Xu <jefflexu@linux.alibaba.com>
---
 fs/erofs/fscache.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
index fe05bc51f9f2..458c1c70ef30 100644
--- a/fs/erofs/fscache.c
+++ b/fs/erofs/fscache.c
@@ -75,11 +75,15 @@ static void erofs_fscache_rreq_unlock_folios(struct netfs_io_request *rreq)
 
 	rcu_read_lock();
 	xas_for_each(&xas, folio, last_page) {
-		unsigned int pgpos =
-			(folio_index(folio) - start_page) * PAGE_SIZE;
-		unsigned int pgend = pgpos + folio_size(folio);
+		unsigned int pgpos, pgend;
 		bool pg_failed = false;
 
+		if (xas_retry(&xas, folio))
+			continue;
+
+		pgpos = (folio_index(folio) - start_page) * PAGE_SIZE;
+		pgend = pgpos + folio_size(folio);
+
 		for (;;) {
 			if (!subreq) {
 				pg_failed = true;
-- 
2.19.1.6.gb485710b


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] erofs: fix missing xas_retry() in fscache mode
  2022-11-11  9:08 [PATCH] erofs: fix missing xas_retry() in fscache mode Jingbo Xu
@ 2022-11-14  4:42 ` Gao Xiang
  2022-11-14  6:27 ` [Phishing Risk] [External] " Jia Zhu
  2022-11-14 11:44 ` David Howells
  2 siblings, 0 replies; 5+ messages in thread
From: Gao Xiang @ 2022-11-14  4:42 UTC (permalink / raw)
  To: Jingbo Xu; +Cc: xiang, chao, yinxin.x, linux-erofs, linux-kernel, dhowells

On Fri, Nov 11, 2022 at 05:08:13PM +0800, Jingbo Xu wrote:
> The xarray iteration only holds RCU and thus may encounter
> XA_RETRY_ENTRY if there's process modifying the xarray concurrently.
> This will cause oops when referring to the invalid entry.
> 
> Fix this by adding the missing xas_retry(), which will make the
> iteration wind back to the root node if XA_RETRY_ENTRY is encountered.
> 
> Fixes: d435d53228dd ("erofs: change to use asynchronous io for fscache readpage/readahead")
> Signed-off-by: Jingbo Xu <jefflexu@linux.alibaba.com>

Reviewed-by: Gao Xiang <hsiangkao@linux.alibaba.com>

Thanks,
Gao Xiang

> ---
>  fs/erofs/fscache.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
> index fe05bc51f9f2..458c1c70ef30 100644
> --- a/fs/erofs/fscache.c
> +++ b/fs/erofs/fscache.c
> @@ -75,11 +75,15 @@ static void erofs_fscache_rreq_unlock_folios(struct netfs_io_request *rreq)
>  
>  	rcu_read_lock();
>  	xas_for_each(&xas, folio, last_page) {
> -		unsigned int pgpos =
> -			(folio_index(folio) - start_page) * PAGE_SIZE;
> -		unsigned int pgend = pgpos + folio_size(folio);
> +		unsigned int pgpos, pgend;
>  		bool pg_failed = false;
>  
> +		if (xas_retry(&xas, folio))
> +			continue;
> +
> +		pgpos = (folio_index(folio) - start_page) * PAGE_SIZE;
> +		pgend = pgpos + folio_size(folio);
> +
>  		for (;;) {
>  			if (!subreq) {
>  				pg_failed = true;
> -- 
> 2.19.1.6.gb485710b

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Phishing Risk] [External] [PATCH] erofs: fix missing xas_retry() in fscache mode
  2022-11-11  9:08 [PATCH] erofs: fix missing xas_retry() in fscache mode Jingbo Xu
  2022-11-14  4:42 ` Gao Xiang
@ 2022-11-14  6:27 ` Jia Zhu
  2022-11-14 11:44 ` David Howells
  2 siblings, 0 replies; 5+ messages in thread
From: Jia Zhu @ 2022-11-14  6:27 UTC (permalink / raw)
  To: Jingbo Xu, xiang, chao, yinxin.x, linux-erofs; +Cc: dhowells, linux-kernel

在 2022/11/11 17:08, Jingbo Xu 写道:
> The xarray iteration only holds RCU and thus may encounter
> XA_RETRY_ENTRY if there's process modifying the xarray concurrently.
> This will cause oops when referring to the invalid entry.
> 
> Fix this by adding the missing xas_retry(), which will make the
> iteration wind back to the root node if XA_RETRY_ENTRY is encountered.
> 
> Fixes: d435d53228dd ("erofs: change to use asynchronous io for fscache readpage/readahead")
> Signed-off-by: Jingbo Xu <jefflexu@linux.alibaba.com>
Reviewed-by: Jia Zhu <zhujia.zj@bytedance.com>
> ---
>   fs/erofs/fscache.c | 10 +++++++---
>   1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
> index fe05bc51f9f2..458c1c70ef30 100644
> --- a/fs/erofs/fscache.c
> +++ b/fs/erofs/fscache.c
> @@ -75,11 +75,15 @@ static void erofs_fscache_rreq_unlock_folios(struct netfs_io_request *rreq)
>   
>   	rcu_read_lock();
>   	xas_for_each(&xas, folio, last_page) {
> -		unsigned int pgpos =
> -			(folio_index(folio) - start_page) * PAGE_SIZE;
> -		unsigned int pgend = pgpos + folio_size(folio);
> +		unsigned int pgpos, pgend;
>   		bool pg_failed = false;
>   
> +		if (xas_retry(&xas, folio))
> +			continue;
> +
> +		pgpos = (folio_index(folio) - start_page) * PAGE_SIZE;
> +		pgend = pgpos + folio_size(folio);
> +
>   		for (;;) {
>   			if (!subreq) {
>   				pg_failed = true;

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] erofs: fix missing xas_retry() in fscache mode
  2022-11-11  9:08 [PATCH] erofs: fix missing xas_retry() in fscache mode Jingbo Xu
  2022-11-14  4:42 ` Gao Xiang
  2022-11-14  6:27 ` [Phishing Risk] [External] " Jia Zhu
@ 2022-11-14 11:44 ` David Howells
  2022-11-14 12:11   ` Jingbo Xu
  2 siblings, 1 reply; 5+ messages in thread
From: David Howells @ 2022-11-14 11:44 UTC (permalink / raw)
  To: Jingbo Xu; +Cc: dhowells, xiang, chao, yinxin.x, linux-erofs, linux-kernel

Jingbo Xu <jefflexu@linux.alibaba.com> wrote:

> The xarray iteration only holds RCU

I would say "the RCU read lock".

Also, I think you've copied the code to which my dodgy-maths fix applies:

	https://lore.kernel.org/linux-fsdevel/166757988611.950645.7626959069846893164.stgit@warthog.procyon.org.uk/

David


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] erofs: fix missing xas_retry() in fscache mode
  2022-11-14 11:44 ` David Howells
@ 2022-11-14 12:11   ` Jingbo Xu
  0 siblings, 0 replies; 5+ messages in thread
From: Jingbo Xu @ 2022-11-14 12:11 UTC (permalink / raw)
  To: David Howells; +Cc: xiang, chao, yinxin.x, linux-erofs, linux-kernel

Hi David,

Thanks for the comment.

On 11/14/22 7:44 PM, David Howells wrote:
> Jingbo Xu <jefflexu@linux.alibaba.com> wrote:
> 
>> The xarray iteration only holds RCU
> 
> I would say "the RCU read lock".

Yeah, this looks clearer. I will update the commit message in v2 later.

> 
> Also, I think you've copied the code to which my dodgy-maths fix applies:
> 
> 	https://lore.kernel.org/linux-fsdevel/166757988611.950645.7626959069846893164.stgit@warthog.procyon.org.uk/
> 

Thanks for the kindly reminder. Yeah this code was ever copied from
libnetfs. In the scenario of erofs, currently req->start is always
aligned with folio size and erofs doesn't support large folio yet. Thus
req->start won't be inside the folio so far, and I think the current
code works well in the scenario of erofs, though the issue indeed exist
mathematically.

Actually I'm working on the support for large folio now, and the
completion routine of erofs in fscache mode will be refactored quite a
lot. I think this issue will be fixed along with the refactoring.

Thanks again for the suggestion :)

-- 
Thanks,
Jingbo

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-11-14 12:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-11  9:08 [PATCH] erofs: fix missing xas_retry() in fscache mode Jingbo Xu
2022-11-14  4:42 ` Gao Xiang
2022-11-14  6:27 ` [Phishing Risk] [External] " Jia Zhu
2022-11-14 11:44 ` David Howells
2022-11-14 12:11   ` Jingbo Xu

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).