Skip to contentSkip to conversation

ocfs2: Remove custom swap functions in favor of built-in sort swap

PATCH Applied Kuan-Wei Chiu 3 participants 4 messages Aug 10, 2024 – Aug 19, 2024 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: Remove custom swap functions in favor of built-in sort swap

The custom swap functions used in ocfs2 do not perform any special operations and can be replaced with the built-in swap function of sort. This change not only reduces code size but also improves efficiency, especially in scenarios where CONFIG_RETPOLINE is enabled, as it makes indirect function calls more expensive.

By using the built-in swap, we avoid these costly indirect function calls, leading to better performance.

---
Note: Build test only.

 fs/ocfs2/dir.c          | 12 +-----------
 fs/ocfs2/refcounttree.c | 13 +++----------
 fs/ocfs2/xattr.c        | 15 +++------------
 3 files changed, 7 insertions(+), 33 deletions(-)
fs/ocfs2/dir.c +1 -11
index f0beb173dbba..fa5d0819a997 100644
@@ -3511,16 +3511,6 @@ static int dx_leaf_sort_cmp(const void *a, const void *b)
 	return 0;
 }
 
-static void dx_leaf_sort_swap(void *a, void *b, int size)
-{
-	struct ocfs2_dx_entry *entry1 = a;
-	struct ocfs2_dx_entry *entry2 = b;
-
-	BUG_ON(size != sizeof(*entry1));
-
-	swap(*entry1, *entry2);
-}
-
 static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
 {
 	struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
@@ -3781,7 +3771,7 @@ static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
 	 */
 	sort(dx_leaf->dl_list.de_entries, num_used,
 	     sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
-	     dx_leaf_sort_swap);
+	     NULL);
 
 	ocfs2_journal_dirty(handle, dx_leaf_bh);
 
fs/ocfs2/refcounttree.c +3 -10
index 1f303b1adf1a..4f85508538fc 100644
@@ -1392,13 +1392,6 @@ static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
 	return 0;
 }
 
-static void swap_refcount_rec(void *a, void *b, int size)
-{
-	struct ocfs2_refcount_rec *l = a, *r = b;
-
-	swap(*l, *r);
-}
-
 /*
  * The refcount cpos are ordered by their 64bit cpos,
  * But we will use the low 32 bit to be the e_cpos in the b-tree.
@@ -1474,7 +1467,7 @@ static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
 	 */
 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
 	     sizeof(struct ocfs2_refcount_rec),
-	     cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
+	     cmp_refcount_rec_by_low_cpos, NULL);
 
 	ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
 	if (ret) {
@@ -1499,11 +1492,11 @@ static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
 
 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
 	     sizeof(struct ocfs2_refcount_rec),
-	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
+	     cmp_refcount_rec_by_cpos, NULL);
 
 	sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
 	     sizeof(struct ocfs2_refcount_rec),
-	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
+	     cmp_refcount_rec_by_cpos, NULL);
 
 	*split_cpos = cpos;
 	return 0;
fs/ocfs2/xattr.c +3 -12
index 35c0cc2a51af..0e58a5ce539e 100644
@@ -4167,15 +4167,6 @@ static int cmp_xe(const void *a, const void *b)
 	return 0;
 }
 
-static void swap_xe(void *a, void *b, int size)
-{
-	struct ocfs2_xattr_entry *l = a, *r = b, tmp;
-
-	tmp = *l;
-	memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
-	memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
-}
-
 /*
  * When the ocfs2_xattr_block is filled up, new bucket will be created
  * and all the xattr entries will be moved to the new bucket.
@@ -4241,7 +4232,7 @@ static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
 	trace_ocfs2_cp_xattr_block_to_bucket_end(offset, size, off_change);
 
 	sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
-	     cmp_xe, swap_xe);
+	     cmp_xe, NULL);
 }
 
 /*
@@ -4436,7 +4427,7 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
 	 */
 	sort(entries, le16_to_cpu(xh->xh_count),
 	     sizeof(struct ocfs2_xattr_entry),
-	     cmp_xe_offset, swap_xe);
+	     cmp_xe_offset, NULL);
 
 	/* Move all name/values to the end of the bucket. */
 	xe = xh->xh_entries;
@@ -4478,7 +4469,7 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
 	/* sort the entries by their name_hash. */
 	sort(entries, le16_to_cpu(xh->xh_count),
 	     sizeof(struct ocfs2_xattr_entry),
-	     cmp_xe, swap_xe);
+	     cmp_xe, NULL);
 
 	buf = bucket_buf;
 	for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 10 lines · Kuan-Wei Chiu

The custom swap functions used in ocfs2 do not perform any special operations and can be replaced with the built-in swap function of sort. This change not only reduces code size but also improves efficiency, especially in scenarios where CONFIG_RETPOLINE is enabled, as it makes indirect function calls more expensive.

By using the built-in swap, we avoid these costly indirect function calls, leading to better performance.

Looks good to me

Quoted 126 lines

---
Note: Build test only.

  fs/ocfs2/dir.c          | 12 +-----------
  fs/ocfs2/refcounttree.c | 13 +++----------
  fs/ocfs2/xattr.c        | 15 +++------------
  3 files changed, 7 insertions(+), 33 deletions(-)
fs/ocfs2/dir.c +1 -11
index f0beb173dbba..fa5d0819a997 100644
@@ -3511,16 +3511,6 @@ static int dx_leaf_sort_cmp(const void *a, const void *b)
  	return 0;
  }
 
-static void dx_leaf_sort_swap(void *a, void *b, int size)
-{
-	struct ocfs2_dx_entry *entry1 = a;
-	struct ocfs2_dx_entry *entry2 = b;
-
-	BUG_ON(size != sizeof(*entry1));
-
-	swap(*entry1, *entry2);
-}
-
  static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
  {
  	struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
@@ -3781,7 +3771,7 @@ static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
  	 */
  	sort(dx_leaf->dl_list.de_entries, num_used,
  	     sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
-	     dx_leaf_sort_swap);
+	     NULL);
 
  	ocfs2_journal_dirty(handle, dx_leaf_bh);
 
fs/ocfs2/refcounttree.c +3 -10
index 1f303b1adf1a..4f85508538fc 100644
@@ -1392,13 +1392,6 @@ static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
  	return 0;
  }
 
-static void swap_refcount_rec(void *a, void *b, int size)
-{
-	struct ocfs2_refcount_rec *l = a, *r = b;
-
-	swap(*l, *r);
-}
-
  /*
   * The refcount cpos are ordered by their 64bit cpos,
   * But we will use the low 32 bit to be the e_cpos in the b-tree.
@@ -1474,7 +1467,7 @@ static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
  	 */
  	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
  	     sizeof(struct ocfs2_refcount_rec),
-	     cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
+	     cmp_refcount_rec_by_low_cpos, NULL);
 
  	ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
  	if (ret) {
@@ -1499,11 +1492,11 @@ static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
 
  	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
  	     sizeof(struct ocfs2_refcount_rec),
-	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
+	     cmp_refcount_rec_by_cpos, NULL);
 
  	sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
  	     sizeof(struct ocfs2_refcount_rec),
-	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
+	     cmp_refcount_rec_by_cpos, NULL);
 
  	*split_cpos = cpos;
  	return 0;
fs/ocfs2/xattr.c +3 -12
index 35c0cc2a51af..0e58a5ce539e 100644
@@ -4167,15 +4167,6 @@ static int cmp_xe(const void *a, const void *b)
  	return 0;
  }
 
-static void swap_xe(void *a, void *b, int size)
-{
-	struct ocfs2_xattr_entry *l = a, *r = b, tmp;
-
-	tmp = *l;
-	memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
-	memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
-}
-
  /*
   * When the ocfs2_xattr_block is filled up, new bucket will be created
   * and all the xattr entries will be moved to the new bucket.
@@ -4241,7 +4232,7 @@ static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
  	trace_ocfs2_cp_xattr_block_to_bucket_end(offset, size, off_change);
 
  	sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
-	     cmp_xe, swap_xe);
+	     cmp_xe, NULL);
  }
 
  /*
@@ -4436,7 +4427,7 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
  	 */
  	sort(entries, le16_to_cpu(xh->xh_count),
  	     sizeof(struct ocfs2_xattr_entry),
-	     cmp_xe_offset, swap_xe);
+	     cmp_xe_offset, NULL);
 
  	/* Move all name/values to the end of the bucket. */
  	xe = xh->xh_entries;
@@ -4478,7 +4469,7 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
  	/* sort the entries by their name_hash. */
  	sort(entries, le16_to_cpu(xh->xh_count),
  	     sizeof(struct ocfs2_xattr_entry),
-	     cmp_xe, swap_xe);
+	     cmp_xe, NULL);
 
  	buf = bucket_buf;
  	for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
maintainer Reviewed-by
#2 lore raw
Quoted 7 lines · Heming Zhao
Quoted 6 lines · Kuan-Wei Chiu

The custom swap functions used in ocfs2 do not perform any special operations and can be replaced with the built-in swap function of sort. This change not only reduces code size but also improves efficiency, especially in scenarios where CONFIG_RETPOLINE is enabled, as it makes indirect function calls more expensive.

Quoted 8 lines
Quoted 4 lines

By using the built-in swap, we avoid these costly indirect function calls, leading to better performance.

Looks good to me

Quoted 113 lines
Quoted 113 lines

---
Note: Build test only.

  fs/ocfs2/dir.c          | 12 +-----------
  fs/ocfs2/refcounttree.c | 13 +++----------
  fs/ocfs2/xattr.c        | 15 +++------------

  3 files changed, 7 insertions(+), 33 deletions(-)
fs/ocfs2/dir.c +1 -10
index f0beb173dbba..fa5d0819a997 100644
@@ -3511,16 +3511,6 @@ static int dx_leaf_sort_cmp(const void *a, const void *b)
       return 0;
   }
   -static void dx_leaf_sort_swap(void *a, void *b, int size)
-{
-    struct ocfs2_dx_entry *entry1 = a;
-    struct ocfs2_dx_entry *entry2 = b;
-
-    BUG_ON(size != sizeof(*entry1));
-
-    swap(*entry1, *entry2);
-}
-
   static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
   {
       struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
@@ -3781,7 +3771,7 @@ static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
        */
       sort(dx_leaf->dl_list.de_entries, num_used,
            sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
-         dx_leaf_sort_swap);
+         NULL);
         ocfs2_journal_dirty(handle, dx_leaf_bh);

  diff --git a/fs/ocfs2/refcounttree.c b/fs/ocfs2/refcounttree.c index 1f303b1adf1a..4f85508538fc 100644

fs/ocfs2/refcounttree.c +3 -9
@@ -1392,13 +1392,6 @@ static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
       return 0;
   }
   -static void swap_refcount_rec(void *a, void *b, int size)
-{
-    struct ocfs2_refcount_rec *l = a, *r = b;
-
-    swap(*l, *r);
-}
-
   /*
    * The refcount cpos are ordered by their 64bit cpos,
    * But we will use the low 32 bit to be the e_cpos in the b-tree.
@@ -1474,7 +1467,7 @@ static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
        */
       sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
            sizeof(struct ocfs2_refcount_rec),
-         cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
+         cmp_refcount_rec_by_low_cpos, NULL);
         ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
       if (ret) {
@@ -1499,11 +1492,11 @@ static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
         sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
            sizeof(struct ocfs2_refcount_rec),
-         cmp_refcount_rec_by_cpos, swap_refcount_rec);
+         cmp_refcount_rec_by_cpos, NULL);
         sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
            sizeof(struct ocfs2_refcount_rec),
-         cmp_refcount_rec_by_cpos, swap_refcount_rec);
+         cmp_refcount_rec_by_cpos, NULL);
         *split_cpos = cpos;
       return 0;
fs/ocfs2/xattr.c +3 -11
index 35c0cc2a51af..0e58a5ce539e 100644
@@ -4167,15 +4167,6 @@ static int cmp_xe(const void *a, const void *b)
       return 0;
   }
   -static void swap_xe(void *a, void *b, int size)
-{
-    struct ocfs2_xattr_entry *l = a, *r = b, tmp;
-
-    tmp = *l;
-    memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
-    memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
-}
-
   /*
    * When the ocfs2_xattr_block is filled up, new bucket will be created
    * and all the xattr entries will be moved to the new bucket.
@@ -4241,7 +4232,7 @@ static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
       trace_ocfs2_cp_xattr_block_to_bucket_end(offset, size, off_change);
         sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
-         cmp_xe, swap_xe);
+         cmp_xe, NULL);
   }
     /*
@@ -4436,7 +4427,7 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
        */
       sort(entries, le16_to_cpu(xh->xh_count),
            sizeof(struct ocfs2_xattr_entry),
-         cmp_xe_offset, swap_xe);
+         cmp_xe_offset, NULL);
         /* Move all name/values to the end of the bucket. */
       xe = xh->xh_entries;
@@ -4478,7 +4469,7 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
       /* sort the entries by their name_hash. */
       sort(entries, le16_to_cpu(xh->xh_count),
            sizeof(struct ocfs2_xattr_entry),
-         cmp_xe, swap_xe);
+         cmp_xe, NULL);
         buf = bucket_buf;
       for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
reviewer Reviewed-by
#3 lore raw
Quoted 9 lines · Joseph Qi
Quoted 7 lines · Heming Zhao
Quoted 6 lines · Kuan-Wei Chiu

The custom swap functions used in ocfs2 do not perform any special operations and can be replaced with the built-in swap function of sort. This change not only reduces code size but also improves efficiency, especially in scenarios where CONFIG_RETPOLINE is enabled, as it makes indirect function calls more expensive.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

By using the built-in swap, we avoid these costly indirect function calls, leading to better performance.

Looks good to me

Sorry for my mistake, above tag should be:

Quoted 117 lines
Quoted 1 line
Quoted 113 lines
Quoted 113 lines

---
Note: Build test only.

   fs/ocfs2/dir.c          | 12 +-----------
   fs/ocfs2/refcounttree.c | 13 +++----------
   fs/ocfs2/xattr.c        | 15 +++------------
   3 files changed, 7 insertions(+), 33 deletions(-)
fs/ocfs2/dir.c +1 -10
index f0beb173dbba..fa5d0819a997 100644
@@ -3511,16 +3511,6 @@ static int dx_leaf_sort_cmp(const void *a, const void *b)
       return 0;
   }
   -static void dx_leaf_sort_swap(void *a, void *b, int size)
-{
-    struct ocfs2_dx_entry *entry1 = a;
-    struct ocfs2_dx_entry *entry2 = b;
-
-    BUG_ON(size != sizeof(*entry1));
-
-    swap(*entry1, *entry2);
-}
-
   static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
   {
       struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
@@ -3781,7 +3771,7 @@ static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
        */
       sort(dx_leaf->dl_list.de_entries, num_used,
            sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
-         dx_leaf_sort_swap);
+         NULL);
         ocfs2_journal_dirty(handle, dx_leaf_bh);
   diff --git a/fs/ocfs2/refcounttree.c b/fs/ocfs2/refcounttree.c

index 1f303b1adf1a..4f85508538fc 100644

fs/ocfs2/refcounttree.c +3 -9
@@ -1392,13 +1392,6 @@ static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
       return 0;
   }
   -static void swap_refcount_rec(void *a, void *b, int size)
-{
-    struct ocfs2_refcount_rec *l = a, *r = b;
-
-    swap(*l, *r);
-}
-
   /*
    * The refcount cpos are ordered by their 64bit cpos,
    * But we will use the low 32 bit to be the e_cpos in the b-tree.
@@ -1474,7 +1467,7 @@ static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
        */
       sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
            sizeof(struct ocfs2_refcount_rec),
-         cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
+         cmp_refcount_rec_by_low_cpos, NULL);
         ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
       if (ret) {
@@ -1499,11 +1492,11 @@ static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
         sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
            sizeof(struct ocfs2_refcount_rec),
-         cmp_refcount_rec_by_cpos, swap_refcount_rec);
+         cmp_refcount_rec_by_cpos, NULL);
         sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
            sizeof(struct ocfs2_refcount_rec),
-         cmp_refcount_rec_by_cpos, swap_refcount_rec);
+         cmp_refcount_rec_by_cpos, NULL);
         *split_cpos = cpos;
       return 0;
fs/ocfs2/xattr.c +3 -11
index 35c0cc2a51af..0e58a5ce539e 100644
@@ -4167,15 +4167,6 @@ static int cmp_xe(const void *a, const void *b)
       return 0;
   }
   -static void swap_xe(void *a, void *b, int size)
-{
-    struct ocfs2_xattr_entry *l = a, *r = b, tmp;
-
-    tmp = *l;
-    memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
-    memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
-}
-
   /*
    * When the ocfs2_xattr_block is filled up, new bucket will be created
    * and all the xattr entries will be moved to the new bucket.
@@ -4241,7 +4232,7 @@ static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
       trace_ocfs2_cp_xattr_block_to_bucket_end(offset, size, off_change);
         sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
-         cmp_xe, swap_xe);
+         cmp_xe, NULL);
   }
     /*
@@ -4436,7 +4427,7 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
        */
       sort(entries, le16_to_cpu(xh->xh_count),
            sizeof(struct ocfs2_xattr_entry),
-         cmp_xe_offset, swap_xe);
+         cmp_xe_offset, NULL);
         /* Move all name/values to the end of the bucket. */
       xe = xh->xh_entries;
@@ -4478,7 +4469,7 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
       /* sort the entries by their name_hash. */
       sort(entries, le16_to_cpu(xh->xh_count),
            sizeof(struct ocfs2_xattr_entry),
-         cmp_xe, swap_xe);
+         cmp_xe, NULL);
         buf = bucket_buf;
       for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)