Skip to contentSkip to conversation

erofs: get rid of erofs_{find,insert}_workgroup

PATCH In review Gao Xiang 2 participants 8 messages Oct 21, 2024 – Nov 18, 2024 EROFS file system

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH v2 1/3 erofs: get rid of erofs_{find,insert}_workgroup

Just fold them into the only two callers since
they are simple enough.

---
v1: https://lore.kernel.org/r/[email protected]
change since v1:
- !grp case should be handled properly mentioned by Chunhai;

 fs/erofs/internal.h |  5 +----
 fs/erofs/zdata.c    | 38 +++++++++++++++++++++++++---------
 fs/erofs/zutil.c    | 50 +--------------------------------------------
 3 files changed, 30 insertions(+), 63 deletions(-)
fs/erofs/internal.h +1 -4
index 4efd578d7c62..8081ee43cd83 100644
@@ -457,10 +457,7 @@ void erofs_release_pages(struct page **pagepool);
 
 #ifdef CONFIG_EROFS_FS_ZIP
 void erofs_workgroup_put(struct erofs_workgroup *grp);
-struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
-					     pgoff_t index);
-struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
-					       struct erofs_workgroup *grp);
+bool erofs_workgroup_get(struct erofs_workgroup *grp);
 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
 void erofs_shrinker_register(struct super_block *sb);
 void erofs_shrinker_unregister(struct super_block *sb);
fs/erofs/zdata.c +28 -10
index a569ff9dfd04..bb1b73d99d07 100644
@@ -714,9 +714,10 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
 {
 	struct erofs_map_blocks *map = &fe->map;
 	struct super_block *sb = fe->inode->i_sb;
+	struct erofs_sb_info *sbi = EROFS_SB(sb);
 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
 	struct z_erofs_pcluster *pcl;
-	struct erofs_workgroup *grp;
+	struct erofs_workgroup *grp, *pre;
 	int err;
 
 	if (!(map->m_flags & EROFS_MAP_ENCODED) ||
@@ -752,15 +753,23 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
 		pcl->obj.index = 0;	/* which indicates ztailpacking */
 	} else {
 		pcl->obj.index = erofs_blknr(sb, map->m_pa);
-
-		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
-		if (IS_ERR(grp)) {
-			err = PTR_ERR(grp);
-			goto err_out;
+		while (1) {
+			xa_lock(&sbi->managed_pslots);
+			pre = __xa_cmpxchg(&sbi->managed_pslots, grp->index,
+					   NULL, grp, GFP_KERNEL);
+			if (!pre || xa_is_err(pre) || erofs_workgroup_get(pre)) {
+				xa_unlock(&sbi->managed_pslots);
+				break;
+			}
+			/* try to legitimize the current in-tree one */
+			xa_unlock(&sbi->managed_pslots);
+			cond_resched();
 		}
-
-		if (grp != &pcl->obj) {
-			fe->pcl = container_of(grp,
+		if (xa_is_err(pre)) {
+			err = xa_err(pre);
+			goto err_out;
+		} else if (pre) {
+			fe->pcl = container_of(pre,
 					struct z_erofs_pcluster, obj);
 			err = -EEXIST;
 			goto err_out;
@@ -789,7 +798,16 @@ static int z_erofs_pcluster_begin(struct z_erofs_decompress_frontend *fe)
 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
 
 	if (!(map->m_flags & EROFS_MAP_META)) {
-		grp = erofs_find_workgroup(sb, blknr);
+		while (1) {
+			rcu_read_lock();
+			grp = xa_load(&EROFS_SB(sb)->managed_pslots, blknr);
+			if (!grp || erofs_workgroup_get(grp)) {
+				DBG_BUGON(grp && blknr != grp->index);
+				rcu_read_unlock();
+				break;
+			}
+			rcu_read_unlock();
+		}
 	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
 		DBG_BUGON(1);
 		return -EFSCORRUPTED;
fs/erofs/zutil.c +1 -49

62 lines not on this page · raw message

Signature · 1 line
2.43.5
author
#1 lore raw

PATCH v2 2/3 erofs: move erofs_workgroup operations into zdata.c

Move related helpers into zdata.c as an intermediate step of getting rid of struct erofs_workgroup, and rename:

erofs_workgroup_put => z_erofs_put_pcluster erofs_workgroup_get => z_erofs_get_pcluster erofs_try_to_release_workgroup => erofs_try_to_release_pcluster erofs_shrink_workstation => z_erofs_shrink_scan

---
 fs/erofs/internal.h |   8 ++--
 fs/erofs/zdata.c    | 102 ++++++++++++++++++++++++++++++++++++++---
 fs/erofs/zutil.c    | 107 +++-----------------------------------------
 3 files changed, 105 insertions(+), 112 deletions(-)
fs/erofs/internal.h +3 -5
index 8081ee43cd83..5fa7ac0575b2 100644
@@ -456,17 +456,15 @@ static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
 void erofs_release_pages(struct page **pagepool);
 
 #ifdef CONFIG_EROFS_FS_ZIP
-void erofs_workgroup_put(struct erofs_workgroup *grp);
-bool erofs_workgroup_get(struct erofs_workgroup *grp);
-void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
+extern atomic_long_t erofs_global_shrink_cnt;
 void erofs_shrinker_register(struct super_block *sb);
 void erofs_shrinker_unregister(struct super_block *sb);
 int __init erofs_init_shrinker(void);
 void erofs_exit_shrinker(void);
 int __init z_erofs_init_subsystem(void);
 void z_erofs_exit_subsystem(void);
-int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,
-					struct erofs_workgroup *egrp);
+unsigned long z_erofs_shrink_scan(struct erofs_sb_info *sbi,
+				  unsigned long nr_shrink);
 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
 			    int flags);
 void *z_erofs_get_gbuf(unsigned int requiredpages);
fs/erofs/zdata.c +95 -7

150 lines not on this page · raw message

fs/erofs/zutil.c +7 -100

140 lines not on this page · raw message

Signature · 1 line
2.43.5
maintainer Reviewed-by on v2 2/3
#2 lore raw

Re: [PATCH v2 2/3] erofs: move erofs_workgroup operations into zdata.c

Quoted 9 lines · Gao Xiang

Move related helpers into zdata.c as an intermediate step of getting rid of struct erofs_workgroup, and rename:

erofs_workgroup_put => z_erofs_put_pcluster erofs_workgroup_get => z_erofs_get_pcluster erofs_try_to_release_workgroup => erofs_try_to_release_pcluster erofs_shrink_workstation => z_erofs_shrink_scan

Thanks,

author
#3 lore raw

PATCH v2 3/3 erofs: sunset `struct erofs_workgroup`

In reply to Gao Xiang

struct erofs_workgroup was introduced to provide a unique header for all physically indexed objects. However, after big pclusters and shared pclusters are implemented upstream, it seems that all EROFS encoded data (which requires transformation) can be represented with struct z_erofs_pcluster directly.

Move all members into struct z_erofs_pcluster for simplicity.

---
 fs/erofs/internal.h |   6 --
 fs/erofs/zdata.c    | 131 ++++++++++++++++++++------------------------
 2 files changed, 60 insertions(+), 77 deletions(-)
fs/erofs/internal.h +0 -6
index 5fa7ac0575b2..3905d991c49b 100644
@@ -208,12 +208,6 @@ enum {
 	EROFS_ZIP_CACHE_READAROUND
 };
 
-/* basic unit of the workstation of a super_block */
-struct erofs_workgroup {
-	pgoff_t index;
-	struct lockref lockref;
-};
-
 enum erofs_kmap_type {
 	EROFS_NO_KMAP,		/* don't map the buffer */
 	EROFS_KMAP,		/* use kmap_local_page() to map the buffer */
fs/erofs/zdata.c +60 -71

332 lines not on this page · raw message

Signature · 1 line
2.43.5
maintainer Reviewed-by on v2 3/3
#4 lore raw

Re: [PATCH v2 3/3] erofs: sunset `struct erofs_workgroup`

Quoted 9 lines · Gao Xiang

struct erofs_workgroup was introduced to provide a unique header for all physically indexed objects. However, after big pclusters and shared pclusters are implemented upstream, it seems that all EROFS encoded data (which requires transformation) can be represented with struct z_erofs_pcluster directly.

Move all members into struct z_erofs_pcluster for simplicity.

Thanks,

maintainer
#5 lore raw
In reply to Gao Xiang
Quoted 59 lines · Gao Xiang

Just fold them into the only two callers since
they are simple enough.

---
v1: https://lore.kernel.org/r/[email protected]
change since v1:
- !grp case should be handled properly mentioned by Chunhai;

  fs/erofs/internal.h |  5 +----
  fs/erofs/zdata.c    | 38 +++++++++++++++++++++++++---------
  fs/erofs/zutil.c    | 50 +--------------------------------------------
  3 files changed, 30 insertions(+), 63 deletions(-)
fs/erofs/internal.h +1 -4
index 4efd578d7c62..8081ee43cd83 100644
@@ -457,10 +457,7 @@ void erofs_release_pages(struct page **pagepool);
 
  #ifdef CONFIG_EROFS_FS_ZIP
  void erofs_workgroup_put(struct erofs_workgroup *grp);
-struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
-					     pgoff_t index);
-struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
-					       struct erofs_workgroup *grp);
+bool erofs_workgroup_get(struct erofs_workgroup *grp);
  void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
  void erofs_shrinker_register(struct super_block *sb);
  void erofs_shrinker_unregister(struct super_block *sb);
fs/erofs/zdata.c +6 -6
index a569ff9dfd04..bb1b73d99d07 100644
@@ -714,9 +714,10 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
  {
  	struct erofs_map_blocks *map = &fe->map;
  	struct super_block *sb = fe->inode->i_sb;
+	struct erofs_sb_info *sbi = EROFS_SB(sb);
  	bool ztailpacking = map->m_flags & EROFS_MAP_META;
  	struct z_erofs_pcluster *pcl;
-	struct erofs_workgroup *grp;
+	struct erofs_workgroup *grp, *pre;
  	int err;
 
  	if (!(map->m_flags & EROFS_MAP_ENCODED) ||
@@ -752,15 +753,23 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
  		pcl->obj.index = 0;	/* which indicates ztailpacking */
  	} else {
  		pcl->obj.index = erofs_blknr(sb, map->m_pa);
-
-		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
-		if (IS_ERR(grp)) {
-			err = PTR_ERR(grp);
-			goto err_out;
+		while (1) {
+			xa_lock(&sbi->managed_pslots);
+			pre = __xa_cmpxchg(&sbi->managed_pslots, grp->index,
+					   NULL, grp, GFP_KERNEL);

fs/erofs/zdata.c static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe) · line 762

Quoted 10 lines
fs/erofs/zdata.c +7 -2
+			if (!pre || xa_is_err(pre) || erofs_workgroup_get(pre)) {
+				xa_unlock(&sbi->managed_pslots);
+				break;
+			}
+			/* try to legitimize the current in-tree one */
+			xa_unlock(&sbi->managed_pslots);
+			cond_resched();
  		}
-
-		if (grp != &pcl->obj) {

Do we need to keep this logic?

Thanks,

Quoted 95 lines
fs/erofs/zdata.c +15 -2
-			fe->pcl = container_of(grp,
+		if (xa_is_err(pre)) {
+			err = xa_err(pre);
+			goto err_out;
+		} else if (pre) {
+			fe->pcl = container_of(pre,
  					struct z_erofs_pcluster, obj);
  			err = -EEXIST;
  			goto err_out;
@@ -789,7 +798,16 @@ static int z_erofs_pcluster_begin(struct z_erofs_decompress_frontend *fe)
  	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
 
  	if (!(map->m_flags & EROFS_MAP_META)) {
-		grp = erofs_find_workgroup(sb, blknr);
+		while (1) {
+			rcu_read_lock();
+			grp = xa_load(&EROFS_SB(sb)->managed_pslots, blknr);
+			if (!grp || erofs_workgroup_get(grp)) {
+				DBG_BUGON(grp && blknr != grp->index);
+				rcu_read_unlock();
+				break;
+			}
+			rcu_read_unlock();
+		}
  	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
  		DBG_BUGON(1);
  		return -EFSCORRUPTED;
fs/erofs/zutil.c +1 -49
index 37afe2024840..218b0249a482 100644
@@ -214,7 +214,7 @@ void erofs_release_pages(struct page **pagepool)
  	}
  }
 
-static bool erofs_workgroup_get(struct erofs_workgroup *grp)
+bool erofs_workgroup_get(struct erofs_workgroup *grp)
  {
  	if (lockref_get_not_zero(&grp->lockref))
  		return true;
@@ -231,54 +231,6 @@ static bool erofs_workgroup_get(struct erofs_workgroup *grp)
  	return true;
  }
 
-struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
-					     pgoff_t index)
-{
-	struct erofs_sb_info *sbi = EROFS_SB(sb);
-	struct erofs_workgroup *grp;
-
-repeat:
-	rcu_read_lock();
-	grp = xa_load(&sbi->managed_pslots, index);
-	if (grp) {
-		if (!erofs_workgroup_get(grp)) {
-			/* prefer to relax rcu read side */
-			rcu_read_unlock();
-			goto repeat;
-		}
-
-		DBG_BUGON(index != grp->index);
-	}
-	rcu_read_unlock();
-	return grp;
-}
-
-struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
-					       struct erofs_workgroup *grp)
-{
-	struct erofs_sb_info *const sbi = EROFS_SB(sb);
-	struct erofs_workgroup *pre;
-
-	DBG_BUGON(grp->lockref.count < 1);
-repeat:
-	xa_lock(&sbi->managed_pslots);
-	pre = __xa_cmpxchg(&sbi->managed_pslots, grp->index,
-			   NULL, grp, GFP_KERNEL);
-	if (pre) {
-		if (xa_is_err(pre)) {
-			pre = ERR_PTR(xa_err(pre));
-		} else if (!erofs_workgroup_get(pre)) {
-			/* try to legitimize the current in-tree one */
-			xa_unlock(&sbi->managed_pslots);
-			cond_resched();
-			goto repeat;
-		}
-		grp = pre;
-	}
-	xa_unlock(&sbi->managed_pslots);
-	return grp;
-}
-
  static void  __erofs_workgroup_free(struct erofs_workgroup *grp)
  {
  	atomic_long_dec(&erofs_global_shrink_cnt);
author
#6 lore raw

Hi Chao,

Quoted 62 lines · Chao Yu
Quoted 61 lines · Gao Xiang

Just fold them into the only two callers since
they are simple enough.

---
v1: https://lore.kernel.org/r/[email protected]
change since v1:
  - !grp case should be handled properly mentioned by Chunhai;

  fs/erofs/internal.h |  5 +----
  fs/erofs/zdata.c    | 38 +++++++++++++++++++++++++--------- fs/erofs/zutil.c    | 50 +--------------------------------------------

  3 files changed, 30 insertions(+), 63 deletions(-)
fs/erofs/internal.h +1 -4
index 4efd578d7c62..8081ee43cd83 100644
@@ -457,10 +457,7 @@ void erofs_release_pages(struct page **pagepool);
   #ifdef CONFIG_EROFS_FS_ZIP
   void erofs_workgroup_put(struct erofs_workgroup *grp);
-struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
-                         pgoff_t index);
-struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
-                           struct erofs_workgroup *grp);
+bool erofs_workgroup_get(struct erofs_workgroup *grp);
   void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
   void erofs_shrinker_register(struct super_block *sb);
   void erofs_shrinker_unregister(struct super_block *sb);
fs/erofs/zdata.c +2 -1
index a569ff9dfd04..bb1b73d99d07 100644
@@ -714,9 +714,10 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
   {
       struct erofs_map_blocks *map = &fe->map;
       struct super_block *sb = fe->inode->i_sb;
+    struct erofs_sb_info *sbi = EROFS_SB(sb);
       bool ztailpacking = map->m_flags & EROFS_MAP_META;
       struct z_erofs_pcluster *pcl;
-    struct erofs_workgroup *grp;
+    struct erofs_workgroup *grp, *pre;
       int err;

      if (!(map->m_flags & EROFS_MAP_ENCODED) ||

fs/erofs/zdata.c +0 -0
@@ -752,15 +753,23 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)

          pcl->obj.index = 0;    /* which indicates ztailpacking */ } else {
          pcl->obj.index = erofs_blknr(sb, map->m_pa);
-
-        grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj); -        if (IS_ERR(grp)) {
-            err = PTR_ERR(grp);
-            goto err_out;
+        while (1) {
+            xa_lock(&sbi->managed_pslots);
+            pre = __xa_cmpxchg(&sbi->managed_pslots, grp->index, +                       NULL, grp, GFP_KERNEL);
+            if (!pre || xa_is_err(pre) || erofs_workgroup_get(pre)) { +                xa_unlock(&sbi->managed_pslots);
+                break;
+            }

Quoted 8 lines
Quoted 6 lines

+            /* try to legitimize the current in-tree one */ +            xa_unlock(&sbi->managed_pslots);
+            cond_resched();
          }
-
-        if (grp != &pcl->obj) {

Do we need to keep this logic?

Thanks for the review. I think

		if (grp != &pcl->obj)

equals to (pre && erofs_workgroup_get(pre)) here, so

		} else if (pre) {
			fe->pcl = container_of(pre,
				struct z_erofs_pcluster, obj);
			err = -EEXIST;
			goto err_out;
		}

Handles this case.

Thanks,
Gao Xiang

maintainer Reviewed-by
#7 lore raw
Quoted 80 lines · Gao Xiang

Hi Chao,

Quoted 62 lines · Chao Yu
Quoted 61 lines · Gao Xiang

Just fold them into the only two callers since
they are simple enough.

---
v1: https://lore.kernel.org/r/[email protected]
change since v1:
  - !grp case should be handled properly mentioned by Chunhai;

  fs/erofs/internal.h |  5 +----
  fs/erofs/zdata.c    | 38 +++++++++++++++++++++++++--------- fs/erofs/zutil.c    | 50 +--------------------------------------------

  3 files changed, 30 insertions(+), 63 deletions(-)
fs/erofs/internal.h +1 -4
index 4efd578d7c62..8081ee43cd83 100644
@@ -457,10 +457,7 @@ void erofs_release_pages(struct page **pagepool);
   #ifdef CONFIG_EROFS_FS_ZIP
   void erofs_workgroup_put(struct erofs_workgroup *grp);
-struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
-                         pgoff_t index);
-struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
-                           struct erofs_workgroup *grp);
+bool erofs_workgroup_get(struct erofs_workgroup *grp);
   void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
   void erofs_shrinker_register(struct super_block *sb);
   void erofs_shrinker_unregister(struct super_block *sb);
fs/erofs/zdata.c +2 -1
index a569ff9dfd04..bb1b73d99d07 100644
@@ -714,9 +714,10 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
   {
       struct erofs_map_blocks *map = &fe->map;
       struct super_block *sb = fe->inode->i_sb;
+    struct erofs_sb_info *sbi = EROFS_SB(sb);
       bool ztailpacking = map->m_flags & EROFS_MAP_META;
       struct z_erofs_pcluster *pcl;
-    struct erofs_workgroup *grp;
+    struct erofs_workgroup *grp, *pre;
       int err;

      if (!(map->m_flags & EROFS_MAP_ENCODED) ||

fs/erofs/zdata.c +0 -0
@@ -752,15 +753,23 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)

          pcl->obj.index = 0;    /* which indicates ztailpacking */ } else {
          pcl->obj.index = erofs_blknr(sb, map->m_pa);
-
-        grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj); -        if (IS_ERR(grp)) {
-            err = PTR_ERR(grp);
-            goto err_out;
+        while (1) {
+            xa_lock(&sbi->managed_pslots);
+            pre = __xa_cmpxchg(&sbi->managed_pslots, grp->index, +                       NULL, grp, GFP_KERNEL);
+            if (!pre || xa_is_err(pre) || erofs_workgroup_get(pre)) { +                xa_unlock(&sbi->managed_pslots);
+                break;
+            }

Quoted 8 lines
Quoted 6 lines

+            /* try to legitimize the current in-tree one */ +            xa_unlock(&sbi->managed_pslots);
+            cond_resched();
          }
-
-        if (grp != &pcl->obj) {

Do we need to keep this logic?

Thanks for the review.  I think

        if (grp != &pcl->obj)

equals to (pre && erofs_workgroup_get(pre)) here, so

Quoted 8 lines

        } else if (pre) {
            fe->pcl = container_of(pre,
                struct z_erofs_pcluster, obj);
            err = -EEXIST;
            goto err_out;
        }

Handles this case.

Xiang, thanks for your explanation.

Thanks,

Quoted 3 lines

Thanks,
Gao Xiang