Skip to contentSkip to conversation

lib/sort: Clean up sort_nonatomic() and sort_r_nonatomic()

PATCH In review Kuan-Wei Chiu 2 participants 5 messages Sep 10 – Oct 5

Subsystems (5)
Library code BPF Tracing BPF tracing +1 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover lib/sort: Clean up sort_nonatomic() and sort_r_nonatomic()

kernel/trace/bpf_trace.c is the sole remaining user of sort_r_nonatomic(). Switch check_dup_ids() to sort_r() to match bpf_kprobe_multi_link_attach() in the same file, which has safely used sort_r() under the same limit (1U << 20) for 4 years.

With no users left in the tree, remove the unused sort_nonatomic() and sort_r_nonatomic() APIs. This drops the __sort_r() wrapper and eliminates the may_schedule branch and cond_resched() from the inner sorting loop.
---
Changes in v2:
- Drop the already accepted arm-smmu-v3 patch.
- Convert bpf_trace.c to sort_r() instead.

Kuan-Wei Chiu (2):
bpf: Replace sort_r_nonatomic() with sort_r() in check_dup_ids() Revert "lib/sort.c: add _nonatomic() variants with cond_resched()"

 include/linux/sort.h     |  11 ----
 kernel/trace/bpf_trace.c |   2 +-
 lib/sort.c               | 110 +++++++++++----------------------------
 3 files changed, 32 insertions(+), 91 deletions(-)
Signature · 1 line
2.55.0.1003.g10538fe699-goog
#1 lore raw

PATCH v2 1/2 bpf: Replace sort_r_nonatomic() with sort_r() in check_dup_ids()

bpf_kprobe_multi_link_attach() in the same file has been using sort_r() under the exact same limit (1U << 20) for 4 years without issues. Switch check_dup_ids() from sort_r_nonatomic() to sort_r() to match it.

This removes the last in-tree user of sort_r_nonatomic(), allowing the unused _nonatomic() sort variants to be dropped from the core library.

---
Build test only

 kernel/trace/bpf_trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/bpf_trace.c +1 -1
index 29260951aa87..1d7e73ddbafb 100644
@@ -3826,7 +3826,7 @@ static int check_dup_ids(u32 *ids, u64 *cookies, u32 cnt)
 	 * and check it for duplicates. The ids and cookies arrays
 	 * are left sorted.
 	 */
-	sort_r_nonatomic(ids, cnt, sizeof(ids[0]), ids_cmp_r, ids_swap_r, data);
+	sort_r(ids, cnt, sizeof(ids[0]), ids_cmp_r, ids_swap_r, data);
 
 	for (int i = 1; i < cnt; i++) {
 		if (ids[i] == ids[i - 1]) {
Signature · 1 line
2.55.0.1003.g10538fe699-goog
Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] bpf: Replace sort_r_nonatomic() with sort_r() in check_dup_ids()

Quoted 2 lines · Kuan-Wei Chiu

bpf_kprobe_multi_link_attach() in the same file has been using sort_r() under the exact same limit (1U << 20) for 4 years without issues.

true

Quoted 1 line

Switch check_dup_ids() from sort_r_nonatomic() to sort_r() to match it.

I looked it up and I used that based on sashiko comment, it seems like switching to sort_r sould be fine

Quoted 7 lines

This removes the last in-tree user of sort_r_nonatomic(), allowing the unused _nonatomic() sort variants to be dropped from the core library.

---
Build test only

I ran bpf ci on this, it passed

jirka

Quoted 20 lines
 kernel/trace/bpf_trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/bpf_trace.c +1 -1
index 29260951aa87..1d7e73ddbafb 100644
@@ -3826,7 +3826,7 @@ static int check_dup_ids(u32 *ids, u64 *cookies, u32 cnt)
 	 * and check it for duplicates. The ids and cookies arrays
 	 * are left sorted.
 	 */
-	sort_r_nonatomic(ids, cnt, sizeof(ids[0]), ids_cmp_r, ids_swap_r, data);
+	sort_r(ids, cnt, sizeof(ids[0]), ids_cmp_r, ids_swap_r, data);
 
 	for (int i = 1; i < cnt; i++) {
 		if (ids[i] == ids[i - 1]) {
Signature · 1 line
2.55.0.1003.g10538fe699-goog
#3 lore raw

PATCH v2 2/2 Revert "lib/sort.c: add _nonatomic() variants with cond_resched()"

In reply to Kuan-Wei Chiu

This reverts commit e2a33a2a3258794891cdd6ca4b1318da6594d157.

With no remaining users in the kernel tree, remove sort_nonatomic() and sort_r_nonatomic() to clean up dead code. This effectively drops the wrapper function __sort_r() and eliminates the may_schedule branch and cond_resched() call from the inner loop of the core sorting routine, slightly simplifying and optimizing the code.

---
Build test only

 include/linux/sort.h |  11 -----
 lib/sort.c           | 110 ++++++++++++-------------------------------
 2 files changed, 31 insertions(+), 90 deletions(-)
include/linux/sort.h +0 -11
index c01ef804a0eb..871775978af0 100644
@@ -23,15 +23,4 @@ void sort(void *base, size_t num, size_t size,
 	  cmp_func_t cmp_func,
 	  swap_func_t swap_func);
 
-/* Versions that periodically call cond_resched(): */
-
-void sort_r_nonatomic(void *base, size_t num, size_t size,
-		      cmp_r_func_t cmp_func,
-		      swap_r_func_t swap_func,
-		      const void *priv);
-
-void sort_nonatomic(void *base, size_t num, size_t size,
-		    cmp_func_t cmp_func,
-		    swap_func_t swap_func);
-
 #endif
lib/sort.c +31 -79

135 lines not on this page · raw message

Signature · 1 line
2.55.0.1003.g10538fe699-goog
#4 lore raw
In reply to Kuan-Wei Chiu
Quoted 9 lines · Kuan-Wei Chiu

kernel/trace/bpf_trace.c is the sole remaining user of sort_r_nonatomic(). Switch check_dup_ids() to sort_r() to match bpf_kprobe_multi_link_attach() in the same file, which has safely used sort_r() under the same limit (1U << 20) for 4 years.

With no users left in the tree, remove the unused sort_nonatomic() and sort_r_nonatomic() APIs. This drops the __sort_r() wrapper and eliminates the may_schedule branch and cond_resched() from the inner sorting loop.

Anything else I need to do for this patchset to land?

Regards,
Kuan-Wei

Quoted 17 lines

---
Changes in v2:
- Drop the already accepted arm-smmu-v3 patch.
- Convert bpf_trace.c to sort_r() instead.

Kuan-Wei Chiu (2):
bpf: Replace sort_r_nonatomic() with sort_r() in check_dup_ids() Revert "lib/sort.c: add _nonatomic() variants with cond_resched()"

 include/linux/sort.h     |  11 ----
 kernel/trace/bpf_trace.c |   2 +-
 lib/sort.c               | 110 +++++++++++----------------------------
 3 files changed, 32 insertions(+), 91 deletions(-)
Signature · 1 line
2.55.0.1003.g10538fe699-goog