erofs: fix missing folio_unlock causing lock imbalance
PATCH 1/1 erofs: fix missing folio_unlock causing lock imbalance
folio_trylock() in erofs_try_to_free_all_cached_folios() may successfully acquire the folio lock, but the subsequent check for erofs_folio_is_managed() can skip unlocking when the folio is not managed by EROFS.
This leads to a lock imbalance and leaves the folio permanently locked, which may cause reclaim stalls or interfere with memory management.
Fix this by ensuring folio_unlock() is called before continuing.
--- fs/erofs/zdata.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/erofs/zdata.c +3 -1
@@ -605,8 +605,10 @@ static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,if (!folio_trylock (folio))return -EBUSY ; -if (!erofs_folio_is_managed (sbi, folio)) +if (!erofs_folio_is_managed (sbi, folio)) { +folio_unlock (folio);continue ; + } pcl->compressed_bvecs[i].page =NULL ;folio_detach_private (folio);folio_unlock (folio);
Signature · 1 line
2.43.0
Hi Zhan,
Quoted 4 lines · Zhan Xusheng
folio_trylock()inerofs_try_to_free_all_cached_folios()may successfully acquire the folio lock, but the subsequent check forerofs_folio_is_managed()can skip unlocking when the folio is not managed by EROFS.
Do you find some real timing?
I don't think it can really happen, because:
Quoted 6 lines
This leads to a lock imbalance and leaves the folio permanently locked, which may cause reclaim stalls or interfere with memory management.
Fix this by ensuring
folio_unlock()is called before continuing.
If a folio links to a pcluster, folio->private will be non-NULL, and pcl->compressed_bvecs[i] points to that folios.
And z_erofs_cache_release_folio() will be called with folio lock, and pcl->compressed_bvecs[i] will be set NULL here.
So I don't think erofs_try_to_free_all_cached_folios() can find !erofs_folio_is_managed(sbi, folio) in the real world.
Quoted 11 lines
Signed-off-by Zhan Xusheng <[email protected]>--- fs/erofs/zdata.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/erofs/zdata.c +0
-0
fs/erofs/zdata.c static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi, · line 613
Quoted 10 lines
fs/erofs/zdata.c +3
-1@@ -605,8 +605,10 @@ static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,if (!folio_trylock (folio))return -EBUSY ; -if (!erofs_folio_is_managed (sbi, folio)) +if (!erofs_folio_is_managed (sbi, folio)) { +folio_unlock (folio);continue ; + } pcl->compressed_bvecs[i].page =NULL ;folio_detach_private (folio);
But I admit that we should rewrite in function as:
if (!erofs_folio_is_managed(sbi, folio)) {
DBG_BUGON(1);
} else {
pcl->compressed_bvecs[i].page = NULL;
folio_detach_private(folio);
}
folio_unlock(folio);Thanks,
Gao Xiang
Quoted 1 line
fs/erofs/zdata.c +0
-0folio_unlock (folio);
Quoted 51 lines · Gao Xiang
Hi Zhan,
Quoted 4 lines · Zhan Xusheng
folio_trylock()inerofs_try_to_free_all_cached_folios()may successfully acquire the folio lock, but the subsequent check forerofs_folio_is_managed()can skip unlocking when the folio is not managed by EROFS.Do you find some real timing?
I don't think it can really happen, because:
Quoted 6 lines
This leads to a lock imbalance and leaves the folio permanently locked, which may cause reclaim stalls or interfere with memory management.
Fix this by ensuring
folio_unlock()is called before continuing.If a folio links to a pcluster,
folio->privatewill be non-NULL, and pcl->compressed_bvecs[i] points to that folios.And
z_erofs_cache_release_folio()will be called with folio lock, and pcl->compressed_bvecs[i] will be set NULL here.So I don't think
erofs_try_to_free_all_cached_folios()can find !erofs_folio_is_managed(sbi, folio) in the real world.Quoted 11 lines
Signed-off-by Zhan Xusheng <[email protected]>---
fs/erofs/zdata.c| 4 +++-1 file changed, 3 insertions(+), 1 deletion(-)fs/erofs/zdata.c +0
-0fs/erofs/zdata.c static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi, · line 612
Quoted 9 lines
fs/erofs/zdata.c +3
-1@@ -605,8 +605,10 @@ static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi, if (!folio_trylock(folio)) return -EBUSY; - if (!erofs_folio_is_managed(sbi, folio)) + if (!erofs_folio_is_managed(sbi, folio)) { + folio_unlock(folio); continue; + } pcl->compressed_bvecs[i].page = NULL; folio_detach_private(folio);But I admit that we should rewrite in function as:
Quoted 6 lines
if (!erofs_folio_is_managed(sbi, folio)) {
DBG_BUGON(1);
} else { pcl->compressed_bvecs[i].page = NULL;folio_detach_private(folio); }
Or maybe just:
DBG_BUGON(!erofs_folio_is_managed(sbi, folio)); pcl->compressed_bvecs[i].page = NULL; folio_detach_private(folio); folio_unlock(folio);
Since if a pcluster goes here (!pcl->lockref.count), pcl->compressed_bvecs[i] should leave all valid cached folios (Or some should be recycled by .release_folio instead.)
Unless there is the other bug somewhere, but in any case, I don't think your phenomenon is related to EROFS.
Thanks,
Gao Xiang
Quoted 8 lines
folio_unlock(folio);Thanks,
Gao XiangQuoted 1 line
fs/erofs/zdata.c +0
-0folio_unlock(folio);
Hi Gao,
Thanks for the detailed explanation.
I see your point that folios attached to a pcluster should always be managed by EROFS, and the !erofs_folio_is_managed() case should not happen in practice.
Instead of handling it silently, it makes more sense to treat this as an invariant and catch unexpected cases explicitly.
I'll update the patch accordingly by adding DBG_BUGON() and send a v2.
Thanks,
Zhan Xusheng
PATCH v2 1/1 erofs: fix missing folio_unlock causing lock imbalance
folio_trylock() in erofs_try_to_free_all_cached_folios() may successfully acquire the folio lock, but the subsequent check for erofs_folio_is_managed() can skip unlocking when the folio is not managed by EROFS.
As Gao Xiang pointed out, this condition should not happen in practice because compressed_bvecs[] only holds valid cached folios at this point — any non-managed folio would have already been detached by z_erofs_cache_release_folio() under folio lock.
Fix this by adding DBG_BUGON() to catch unexpected folios and ensure folio_unlock() is always called.
--- fs/erofs/zdata.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/erofs/zdata.c +1 -2
@@ -605,8 +605,7 @@ static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,if (!folio_trylock (folio))return -EBUSY ; -if (!erofs_folio_is_managed (sbi, folio)) -continue ; +DBG_BUGON (!erofs_folio_is_managed (sbi, folio)); pcl->compressed_bvecs[i].page =NULL ;folio_detach_private (folio);folio_unlock (folio);
Signature · 1 line
2.43.0
Quoted 10 lines · Zhan Xusheng
folio_trylock()inerofs_try_to_free_all_cached_folios()may successfully acquire the folio lock, but the subsequent check forerofs_folio_is_managed()can skip unlocking when the folio is not managed by EROFS.As Gao Xiang pointed out, this condition should not happen in practice because compressed_bvecs[] only holds valid cached folios at this point — any non-managed folio would have already been detached by
z_erofs_cache_release_folio()under folio lock.
Quoted 5 lines
Fix this by adding
DBG_BUGON()to catch unexpected folios and ensurefolio_unlock()is always called.Suggested-by Gao Xiang <[email protected]>Signed-off-by Zhan Xusheng <[email protected]>
emmm, the patch subject needs to be changed too, like:
[PATCH v3] erofs: ensure all folios are managed in erofs_try_to_free_all_cached_folios()
Thanks,
Gao Xiang
Quoted 18 lines
--- fs/erofs/zdata.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)fs/erofs/zdata.c +1
-2@@ -605,8 +605,7 @@ static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,if (!folio_trylock (folio))return -EBUSY ; -if (!erofs_folio_is_managed (sbi, folio)) -continue ; +DBG_BUGON (!erofs_folio_is_managed (sbi, folio)); pcl->compressed_bvecs[i].page =NULL ;folio_detach_private (folio);folio_unlock (folio);
PATCH v3 1/1 erofs: ensure all folios are managed in erofs_try_to_free_all_cached_folios()
folio_trylock() in erofs_try_to_free_all_cached_folios() may successfully acquire the folio lock, but the subsequent check for erofs_folio_is_managed() can skip unlocking when the folio is not managed by EROFS.
As Gao Xiang pointed out, this condition should not happen in practice because compressed_bvecs[] only holds valid cached folios at this point — any non-managed folio would have already been detached by z_erofs_cache_release_folio() under folio lock.
Fix this by adding DBG_BUGON() to catch unexpected folios and ensure folio_unlock() is always called.
--- fs/erofs/zdata.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/erofs/zdata.c +1 -2
@@ -605,8 +605,7 @@ static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,if (!folio_trylock (folio))return -EBUSY ; -if (!erofs_folio_is_managed (sbi, folio)) -continue ; +DBG_BUGON (!erofs_folio_is_managed (sbi, folio)); pcl->compressed_bvecs[i].page =NULL ;folio_detach_private (folio);folio_unlock (folio);
Signature · 1 line
2.43.0
Re: [PATCH] [PATCH v3] erofs: ensure all folios are managed in erofs_try_to_free_all_cached_folios()
Quoted 10 lines · Zhan Xusheng
folio_trylock()inerofs_try_to_free_all_cached_folios()may successfully acquire the folio lock, but the subsequent check forerofs_folio_is_managed()can skip unlocking when the folio is not managed by EROFS.As Gao Xiang pointed out, this condition should not happen in practice because compressed_bvecs[] only holds valid cached folios at this point — any non-managed folio would have already been detached by
z_erofs_cache_release_folio()under folio lock.
Quoted 5 lines
Fix this by adding
DBG_BUGON()to catch unexpected folios and ensurefolio_unlock()is always called.Suggested-by Gao Xiang <[email protected]>Signed-off-by Zhan Xusheng <[email protected]>
Thanks,
Gao Xiang
Re: [PATCH] [PATCH v3] erofs: ensure all folios are managed in erofs_try_to_free_all_cached_folios()
In reply to Zhan XushengQuoted 10 lines · Zhan Xusheng
folio_trylock()inerofs_try_to_free_all_cached_folios()may successfully acquire the folio lock, but the subsequent check forerofs_folio_is_managed()can skip unlocking when the folio is not managed by EROFS.As Gao Xiang pointed out, this condition should not happen in practice because compressed_bvecs[] only holds valid cached folios at this point — any non-managed folio would have already been detached by
z_erofs_cache_release_folio()under folio lock.
Quoted 5 lines
Fix this by adding
DBG_BUGON()to catch unexpected folios and ensurefolio_unlock()is always called.Suggested-by Gao Xiang <[email protected]>Signed-off-by Zhan Xusheng <[email protected]>
Thanks,