Skip to contentSkip to conversation

locking/ww_mutex: use swap() in get_random_order()

PATCH In review Jiapeng Chong 2 participants 2 messages Oct 25, 2024 Locking primitives

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 locking/ww_mutex: use swap() in get_random_order()

Use existing swap() function rather than duplicating its implementation.

./kernel/locking/test-ww_mutex.c:420:22-23: WARNING opportunity for swap().

---
 kernel/locking/test-ww_mutex.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/locking/test-ww_mutex.c +3 -6
index 5d58b2c0ef98..bcb1b9fea588 100644
@@ -404,7 +404,7 @@ static inline u32 prandom_u32_below(u32 ceil)
 static int *get_random_order(int count)
 {
 	int *order;
-	int n, r, tmp;
+	int n, r;
 
 	order = kmalloc_array(count, sizeof(*order), GFP_KERNEL);
 	if (!order)
@@ -415,11 +415,8 @@ static int *get_random_order(int count)
 
 	for (n = count - 1; n > 1; n--) {
 		r = prandom_u32_below(n + 1);
-		if (r != n) {
-			tmp = order[n];
-			order[n] = order[r];
-			order[r] = tmp;
-		}
+		if (r != n)
+			swap(order[n], order[r]);
 	}
 
 	return order;
Signature · 1 line
2.32.0.3.g01195cf9f
#1 lore raw
Quoted 7 lines · Jiapeng Chong

Use existing swap() function rather than duplicating its implementation.

./kernel/locking/test-ww_mutex.c:420:22-23: WARNING opportunity for swap().

Thanks for the patch. However, I think Thorsten Blum beat you to it [1]. Would it be Ok if I queue that patch with you as a Reported-by (plus the other Reported-by tag and Close tag)?

[1]: https://lore.kernel.org/r/[email protected]

Regards,
Boqun

Quoted 34 lines
---
 kernel/locking/test-ww_mutex.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/locking/test-ww_mutex.c +3 -6
index 5d58b2c0ef98..bcb1b9fea588 100644
@@ -404,7 +404,7 @@ static inline u32 prandom_u32_below(u32 ceil)
 static int *get_random_order(int count)
 {
 	int *order;
-	int n, r, tmp;
+	int n, r;
 
 	order = kmalloc_array(count, sizeof(*order), GFP_KERNEL);
 	if (!order)
@@ -415,11 +415,8 @@ static int *get_random_order(int count)
 
 	for (n = count - 1; n > 1; n--) {
 		r = prandom_u32_below(n + 1);
-		if (r != n) {
-			tmp = order[n];
-			order[n] = order[r];
-			order[r] = tmp;
-		}
+		if (r != n)
+			swap(order[n], order[r]);
 	}
 
 	return order;
Signature · 1 line
2.32.0.3.g01195cf9f