staging: erofs: decompression stability enhancement
v2 cover staging: erofs: decompression stability enhancement
Hi,
This patchset mainly focuses on erofs decompression stability, most of them are found in the internal beta test. Some patches which are still in testing or reviewing aren't included in this patchset and will be sent after carefully testing.
Thanks,
Gao Xiang
Changelog from v1
===
o fix a redundant BUG_ON suggested by Greg since it never happens at all;
o add more description suggested by Greg in commit message of [01/10] staging: erofs: fix `trace_erofs_readpage' position
o add some description suggested by Greg of a memory barrier occurred in [06/10] staging: erofs: add a full barrier in erofs_workgroup_unfreeze
Shortlog
===
Gao Xiang (10):
staging: erofs: fix `trace_erofs_readpage' position
staging: erofs: fix the definition of DBG_BUGON
staging: erofs: fix race when the managed cache is enabled
staging: erofs: atomic_cond_read_relaxed on ref-locked workgroup
staging: erofs: fix `erofs_workgroup_{try_to_freeze, unfreeze}'
staging: erofs: add a full barrier in erofs_workgroup_unfreeze
staging: erofs: separate into init_once / always
staging: erofs: locked before registering for all new workgroups
staging: erofs: decompress asynchronously if PG_readahead page at
first
staging: erofs: rename strange variable names in z_erofs_vle_frontend
drivers/staging/erofs/internal.h | 73 +++++++++++++-------- drivers/staging/erofs/unzip_vle.c | 79 +++++++++++++++------- drivers/staging/erofs/utils.c | 135 +++++++++++++++++++++++++++----------- 3 files changed, 199 insertions(+), 88 deletions(-)
Signature · 1 line
2.11.0
PATCH v2 1/10 staging: erofs: fix `trace_erofs_readpage' position
From: Gao Xiang <[email protected]>
trace_erofs_readpage' was designed for .readpage() interface hook in order to trace the detailed synchronized read rather than the internal implementation z_erofs_do_read_page' which both .readpage() and .readpages() use.
It seems the tracepoint was placed to a wrong place by mistake and it should be moved to the right place.
--- drivers/staging/erofs/unzip_vle.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/erofs/unzip_vle.c +2 -2
@@ -598,8 +598,6 @@ static int z_erofs_do_read_page(struct z_erofs_vle_frontend *fe,unsigned int cur, end, spiltted, index;int err =0 ; -trace_erofs_readpage (page, false); -/* register locked file pages as online pages in pack */ z_erofs_onlinepage_init (page);
@@ -1288,6 +1286,8 @@ static int z_erofs_vle_normalaccess_readpage(struct file *file,int err;LIST_HEAD (pagepool); +trace_erofs_readpage (page, false); +#if (EROFS_FS_ZIP_CACHE_LVL >=2 ) f.cachedzone_la = (erofs_off_t )page->index <<PAGE_SHIFT ;#endif
Signature · 1 line
2.11.0
PATCH v2 2/10 staging: erofs: fix the definition of DBG_BUGON
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
It's better not to positively BUG_ON the kernel, however developers need a way to locate issues as soon as possible.
DBG_BUGON is introduced and it could only crash when EROFS_FS_DEBUG (EROFS developping feature) is on. It is helpful for developers to find and solve bugs quickly by eng builds.
Previously, DBG_BUGON is defined as ((void)0) if EROFS_FS_DEBUG is off, but some unused variable warnings as follows could occur:
drivers/staging/erofs/unzip_vle.c: In function `init_alway:':drivers/staging/erofs/unzip_vle.c:61:33: warning: unused variable `work' [-Wunused-variable]
struct z_erofs_vle_work *const work =
^~~~
Fix it to #define DBG_BUGON(x) ((void)(x)).
--- drivers/staging/erofs/internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/erofs/internal.h +1 -1
@@ -39,7 +39,7 @@#define debugln (x, ...) ((void)0)#define dbg_might_sleep () ((void)0) -#define DBG_BUGON (...) ((void)0) +#define DBG_BUGON (x) ((void)(x))#endif enum {
Signature · 1 line
2.11.0
PATCH v2 3/10 staging: erofs: fix race when the managed cache is enabled
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
When the managed cache is enabled, the last reference count of a workgroup must be used for its workstation.
Otherwise, it could lead to incorrect (un)freezes in the reclaim path, and it would be harmful.
A typical race as follows:
Thread 1 (In the reclaim path) Thread 2workgroup_freeze(grp, 1) refcnt = 1
...
workgroup_unfreeze(grp, 1) refcnt = 1
workgroup_get(grp) refcnt = 2 (x)
workgroup_put(grp) refcnt = 1 (x)...unexpected behaviors
* grp is detached but still used, which violates cache-managed freeze constraint.
--- drivers/staging/erofs/internal.h | 1 + drivers/staging/erofs/utils.c | 134 +++++++++++++++++++++++++++------------ 2 files changed, 96 insertions(+), 39 deletions(-)
drivers/staging/erofs/internal.h +1 -0
@@ -250,6 +250,7 @@ static inline bool erofs_workgroup_get(struct erofs_workgroup *grp, int *ocnt) }#define __erofs_workgroup_get (grp) atomic_inc(&(grp)->refcount) +#define __erofs_workgroup_put (grp) atomic_dec(&(grp)->refcount)extern int erofs_workgroup_put (struct erofs_workgroup *grp);
drivers/staging/erofs/utils.c +95 -39
168 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v2 4/10 staging: erofs: atomic_cond_read_relaxed on ref-locked workgroup
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
It's better to use atomic_cond_read_relaxed, which is implemented in hardware instructions to monitor a variable changes currently for ARM64, instead of open-coded busy waiting.
--- drivers/staging/erofs/internal.h | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/staging/erofs/internal.h +18 -12
@@ -221,23 +221,29 @@ static inline void erofs_workgroup_unfreeze(preempt_enable (); } +#if defined(CONFIG_SMP ) +static inline int erofs_wait_on_workgroup_freezed (struct erofs_workgroup *grp) +{ +return atomic_cond_read_relaxed (&grp->refcount, +VAL !=EROFS_LOCKED_MAGIC ); +} +#else +static inline int erofs_wait_on_workgroup_freezed (struct erofs_workgroup *grp) +{ +int v =atomic_read (&grp->refcount); + +/* workgroup is never freezed on uniprocessor systems */ +DBG_BUGON (v ==EROFS_LOCKED_MAGIC ); +return v; +} +#endif +static inline bool erofs_workgroup_get (struct erofs_workgroup *grp,int *ocnt) { -const int locked = (int )EROFS_LOCKED_MAGIC ;int o; repeat: - o =atomic_read (&grp->refcount); - -/* spin if it is temporarily locked at the reclaim path */ -if (unlikely (o == locked)) { -#if defined(CONFIG_SMP ) || defined(CONFIG_DEBUG_SPINLOCK ) -do -cpu_relax (); -while (atomic_read (&grp->refcount) == locked); -#endif - goto repeat; - } + o =erofs_wait_on_workgroup_freezed (grp);if (unlikely (o <=0 ))return -1 ;
Signature · 1 line
2.11.0
PATCH v2 5/10 staging: erofs: fix `erofs_workgroup_{try_to_freeze, unfreeze}'
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
There are two minor issues in the current freeze interface:
1) Freeze interfaces have not related with CONFIG_DEBUG_SPINLOCK,
therefore fix the incorrect conditions;
2) For SMP platforms, it should also disable preemption before doing atomic_cmpxchg in case that some high priority tasks preempt between atomic_cmpxchg and disable_preempt, then spin on the locked refcount later.
--- drivers/staging/erofs/internal.h | 41 ++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-)
drivers/staging/erofs/internal.h +25 -16
@@ -194,40 +194,49 @@ struct erofs_workgroup {#define EROFS_LOCKED_MAGIC (INT_MIN | 0xE0F510CCL) -static inline bool erofs_workgroup_try_to_freeze ( -struct erofs_workgroup *grp,int v) +#if defined(CONFIG_SMP ) +static inline bool erofs_workgroup_try_to_freeze (struct erofs_workgroup *grp, +int val) { -#if defined(CONFIG_SMP ) || defined(CONFIG_DEBUG_SPINLOCK ) -if (v !=atomic_cmpxchg (&grp->refcount, - v,EROFS_LOCKED_MAGIC )) -return false;preempt_disable (); -#else -preempt_disable (); -if (atomic_read (&grp->refcount) != v) { +if (val !=atomic_cmpxchg (&grp->refcount, val,EROFS_LOCKED_MAGIC )) {preempt_enable ();return false; } -#endif return true; } -static inline void erofs_workgroup_unfreeze ( -struct erofs_workgroup *grp,int v) +static inline void erofs_workgroup_unfreeze (struct erofs_workgroup *grp, +int orig_val) { -#if defined(CONFIG_SMP ) || defined(CONFIG_DEBUG_SPINLOCK ) -atomic_set (&grp->refcount, v); -#endif +atomic_set (&grp->refcount, orig_val);preempt_enable (); } -#if defined(CONFIG_SMP )static inline int erofs_wait_on_workgroup_freezed (struct erofs_workgroup *grp) {return atomic_cond_read_relaxed (&grp->refcount,VAL !=EROFS_LOCKED_MAGIC ); }#else +static inline bool erofs_workgroup_try_to_freeze (struct erofs_workgroup *grp, +int val) +{ +preempt_disable (); +/* no need to spin on UP platforms, let's just disable preemption. */ +if (val !=atomic_read (&grp->refcount)) { +preempt_enable (); +return false; + } +return true; +} + +static inline void erofs_workgroup_unfreeze (struct erofs_workgroup *grp, +int orig_val) +{ +preempt_enable (); +} +static inline int erofs_wait_on_workgroup_freezed (struct erofs_workgroup *grp) {int v =atomic_read (&grp->refcount);
Signature · 1 line
2.11.0
PATCH v2 6/10 staging: erofs: add a full barrier in erofs_workgroup_unfreeze
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
Just like other generic locks, insert a full barrier in case of memory reorder.
--- drivers/staging/erofs/internal.h | 5 +++++ 1 file changed, 5 insertions(+)
drivers/staging/erofs/internal.h +5 -0
@@ -209,6 +209,11 @@ static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,static inline void erofs_workgroup_unfreeze (struct erofs_workgroup *grp,int orig_val) { +/* +* other observers should notice all modifications +* in the freezing period. +*/ +smp_mb ();atomic_set (&grp->refcount, orig_val);preempt_enable (); }
Signature · 1 line
2.11.0
PATCH v2 7/10 staging: erofs: separate into init_once / always
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
`z_erofs_vle_workgroup' is heavily generated in the decompression, for example, it resets 32 bytes redundantly for 64-bit platforms even through Z_EROFS_VLE_INLINE_PAGEVECS + Z_EROFS_CLUSTER_MAX_PAGES, default 4, pages are stored in `z_erofs_vle_workgroup'.
As an another example, `struct mutex' takes 72 bytes for our kirin 64-bit platforms, it's unnecessary to be reseted at first and be initialized each time.
Let's avoid filling all `z_erofs_vle_workgroup' with 0 at first since most fields are reinitialized to meaningful values later, and pagevec is no need to initialized at all.
--- drivers/staging/erofs/unzip_vle.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-)
drivers/staging/erofs/unzip_vle.c +29 -5
@@ -43,12 +43,38 @@ static inline int init_unzip_workqueue(void)return z_erofs_workqueue ?0 : -ENOMEM ; } +static void init_once (void *ptr) +{ +struct z_erofs_vle_workgroup *grp = ptr; +struct z_erofs_vle_work *const work = +z_erofs_vle_grab_primary_work (grp); +unsigned int i; + +mutex_init (&work->lock); + work->nr_pages =0 ; + work->vcnt =0 ; +for (i =0 ; i <Z_EROFS_CLUSTER_MAX_PAGES ; ++i) + grp->compressed_pages[i] =NULL ; +} + +static void init_always (struct z_erofs_vle_workgroup *grp) +{ +struct z_erofs_vle_work *const work = +z_erofs_vle_grab_primary_work (grp); + +atomic_set (&grp->obj.refcount,1 ); + grp->flags =0 ; + +DBG_BUGON (work->nr_pages); +DBG_BUGON (work->vcnt); +} +int __init z_erofs_init_zip_subsystem (void ) { z_erofs_workgroup_cachep =kmem_cache_create ("erofs_compress" ,Z_EROFS_WORKGROUP_SIZE ,0 , -SLAB_RECLAIM_ACCOUNT ,NULL ); +SLAB_RECLAIM_ACCOUNT , init_once);if (z_erofs_workgroup_cachep) {if (!init_unzip_workqueue ())
@@ -370,10 +396,11 @@ z_erofs_vle_work_register(const struct z_erofs_vle_work_finder *f,BUG_ON (grp);/* no available workgroup, let's allocate one */ - grp =kmem_cache_zalloc (z_erofs_workgroup_cachep,GFP_NOFS ); + grp =kmem_cache_alloc (z_erofs_workgroup_cachep,GFP_NOFS );if (unlikely (!grp))return ERR_PTR (-ENOMEM ); +init_always (grp); grp->obj.index = f->idx; grp->llen = map->m_llen;
@@ -381,7 +408,6 @@ z_erofs_vle_work_register(const struct z_erofs_vle_work_finder *f, (map->m_flags &EROFS_MAP_ZIPPED ) ?Z_EROFS_VLE_WORKGRP_FMT_LZ4 : Z_EROFS_VLE_WORKGRP_FMT_PLAIN); -atomic_set (&grp->obj.refcount,1 );/* new workgrps have been claimed as type 1 */ WRITE_ONCE (grp->next, *f->owned_head);
@@ -394,8 +420,6 @@ z_erofs_vle_work_register(const struct z_erofs_vle_work_finder *f, work =z_erofs_vle_grab_primary_work (grp); work->pageofs = f->pageofs; -mutex_init (&work->lock); -if (gnew) {int err =erofs_register_workgroup (f->sb, &grp->obj,0 );
Signature · 1 line
2.11.0
PATCH v2 8/10 staging: erofs: locked before registering for all new workgroups
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
Let's make sure that the one registering a workgroup will also take the primary work lock at first for two reasons:
1) There's no need to introduce such a race window (and consequently overhead) between registering and locking, other tasks could break in by chance, and the race seems unnecessary (no benefit at all);
2) It's better to take the primary work when a workgroup is registered to apply the cache managed policy, for example, if some other tasks break in, it could turn into the in-place decompression rather than use as the cached decompression.
--- drivers/staging/erofs/unzip_vle.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/erofs/unzip_vle.c +7 -2
@@ -420,18 +420,23 @@ z_erofs_vle_work_register(const struct z_erofs_vle_work_finder *f, work =z_erofs_vle_grab_primary_work (grp); work->pageofs = f->pageofs; +/* +* lock all primary followed works before visible to others +* and mutex_trylock *never* fails for a new workgroup. +*/ +mutex_trylock (&work->lock); +if (gnew) {int err =erofs_register_workgroup (f->sb, &grp->obj,0 );if (err) { +mutex_unlock (&work->lock);kmem_cache_free (z_erofs_workgroup_cachep, grp);return ERR_PTR (-EAGAIN ); } } *f->owned_head = *f->grp_ret = grp; - -mutex_lock (&work->lock);return work; }
Signature · 1 line
2.11.0
PATCH v2 9/10 staging: erofs: decompress asynchronously if PG_readahead page at first
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
For the case of nr_to_read == lookahead_size, it is better to decompress asynchronously as well since no page will be needed immediately.
--- drivers/staging/erofs/unzip_vle.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/staging/erofs/unzip_vle.c +8 -1
@@ -1345,8 +1345,8 @@ static int z_erofs_vle_normalaccess_readpages(struct file *filp, {struct inode *const inode = mapping->host;struct erofs_sb_info *const sbi =EROFS_I_SB (inode); -const bool sync =__should_decompress_synchronously (sbi, nr_pages); +bool sync =__should_decompress_synchronously (sbi, nr_pages);struct z_erofs_vle_frontend f =VLE_FRONTEND_INIT (inode);gfp_t gfp =mapping_gfp_constraint (mapping,GFP_KERNEL );struct page *head =NULL ;
@@ -1364,6 +1364,13 @@ static int z_erofs_vle_normalaccess_readpages(struct file *filp,prefetchw (&page->flags);list_del (&page->lru); +/* +* A pure asynchronous readahead is indicated if +* a PG_readahead marked page is hitted at first. +* Let's also do asynchronous decompression for this case. +*/ + sync &= !(PageReadahead (page) && !head); +if (add_to_page_cache_lru (page, mapping, page->index, gfp)) {list_add (&page->lru, &pagepool);continue ;
Signature · 1 line
2.11.0
PATCH v2 10/10 staging: erofs: rename strange variable names in z_erofs_vle_frontend
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
Previously, 2 members called initial' and cachedzone_la' are used for applying caching policy (whether the workgroup is at either end), which are hard to understand, rename them to backmost' and headoffset'.
--- drivers/staging/erofs/unzip_vle.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
drivers/staging/erofs/unzip_vle.c +11 -14
@@ -587,10 +587,9 @@ struct z_erofs_vle_frontend {z_erofs_vle_owned_workgrp_t owned_head; -bool initial; -#if (EROFS_FS_ZIP_CACHE_LVL >=2 ) -erofs_off_t cachedzone_la; -#endif +/* used for applying cache strategy on the fly */ +bool backmost; +erofs_off_t headoffset; };#define VLE_FRONTEND_INIT (__i) { \
@@ -601,7 +600,7 @@ struct z_erofs_vle_frontend { }, \ .builder = VLE_WORK_BUILDER_INIT(), \ .owned_head = Z_EROFS_VLE_WORKGRP_TAIL, \ - .initial = true, } + .backmost = true, } static int z_erofs_do_read_page(struct z_erofs_vle_frontend *fe, struct page *page,
@@ -644,7 +643,7 @@ static int z_erofs_do_read_page(struct z_erofs_vle_frontend *fe,debugln ("%s: [out-of-range] pos %llu" , __func__, offset + cur);if (z_erofs_vle_work_iter_end (builder)) - fe->initial = false; + fe->backmost = false; map->m_la = offset + cur; map->m_llen =0 ;
@@ -670,8 +669,8 @@ static int z_erofs_do_read_page(struct z_erofs_vle_frontend *fe,erofs_blknr (map->m_pa), grp->compressed_pages,erofs_blknr (map->m_plen),/* compressed page caching selection strategy */ - fe->initial | (EROFS_FS_ZIP_CACHE_LVL >=2 ? - map->m_la < fe->cachedzone_la : 0)); + fe->backmost | (EROFS_FS_ZIP_CACHE_LVL >=2 ? + map->m_la < fe->headoffset : 0));if (noio_outoforder &&builder_is_followed (builder)) builder->role =Z_EROFS_VLE_WORK_PRIMARY ;
@@ -1317,9 +1316,8 @@ static int z_erofs_vle_normalaccess_readpage(struct file *file,trace_erofs_readpage (page, false); -#if (EROFS_FS_ZIP_CACHE_LVL >=2 ) - f.cachedzone_la = (erofs_off_t )page->index <<PAGE_SHIFT ; -#endif + f.headoffset = (erofs_off_t )page->index <<PAGE_SHIFT ; + err =z_erofs_do_read_page (&f, page, &pagepool); (void )z_erofs_vle_work_iter_end (&f.builder);
@@ -1355,9 +1353,8 @@ static int z_erofs_vle_normalaccess_readpages(struct file *filp,trace_erofs_readpages (mapping->host,lru_to_page (pages), nr_pages, false); -#if (EROFS_FS_ZIP_CACHE_LVL >=2 ) - f.cachedzone_la = (erofs_off_t )lru_to_page (pages)->index <<PAGE_SHIFT ; -#endif + f.headoffset = (erofs_off_t )lru_to_page (pages)->index <<PAGE_SHIFT ; +for (; nr_pages; --nr_pages) {struct page *page =lru_to_page (pages);
Signature · 1 line
2.11.0