Skip to contentSkip to conversation

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

PATCH Superseded Kuan-Wei Chiu 5 participants 9 messages Jul 30 – Aug 6

Subsystems (5)
Library code BPF Tracing IOMMU +1 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

Remove the sort_nonatomic() and sort_r_nonatomic() APIs from the kernel library.

Currently, the arm-smmu-v3 driver is the sole in-tree user of sort_nonatomic(). Because the array size being sorted is small in practice, there is no real risk of triggering a soft lockup. Therefore, the periodic cond_resched() calls provided by the _nonatomic variant are unnecessary.

With the only in-tree user updated, the _nonatomic APIs are no longer needed anywhere in the kernel. Removing them effectively drops the wrapper function and eliminates the may_schedule branch from the innermost loop of the core sorting logic, slightly simplifying the code.

Kuan-Wei Chiu (2):
iommu/arm-smmu-v3: Replace sort_nonatomic() with sort() Revert "lib/sort.c: add _nonatomic() variants with cond_resched()"

 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c |   6 +-
 include/linux/sort.h                        |  11 --
 lib/sort.c                                  | 110 ++++++--------------
 3 files changed, 34 insertions(+), 93 deletions(-)
Signature · 1 line
2.55.0.508.g3f0d502094-goog
#1 lore raw

PATCH 1/2 iommu/arm-smmu-v3: Replace sort_nonatomic() with sort()

The number of master->num_streams per master device is typically very small in practice. Sorting this array takes a very small amount of time, so there is no practical risk of triggering a soft lockup that would necessitate calling cond_resched() during the sort.

Replace sort_nonatomic() with the standard sort(). Since this is the only remaining in-tree caller of sort_nonatomic(), this change paves the way to eventually remove the unused sort_nonatomic() API from the core library.

---
Build-tested only.

I'm not really familiar with this driver or smmu internals. Based on my limited knowledge, master->num_streams should be small enough to safely switch from sort_nonatomic() to sort(), but I could be wrong.

Please review carefully and let me know if there are any cases where num_streams could actually be large enough to cause issues.

 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -3
index a10affb483a4..dcb6be2df95e 100644
@@ -4047,9 +4047,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
 	}
 
 	/* Put the ids into order for sorted to_merge/to_unref arrays */
-	sort_nonatomic(master->streams, master->num_streams,
-		       sizeof(master->streams[0]), arm_smmu_stream_id_cmp,
-		       NULL);
+	sort(master->streams, master->num_streams,
+	     sizeof(master->streams[0]), arm_smmu_stream_id_cmp,
+	     NULL);
 
 	mutex_lock(&smmu->streams_mutex);
 	for (i = 0; i < fwspec->num_ids; i++) {
Signature · 1 line
2.55.0.508.g3f0d502094-goog
#6 lore raw

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

In reply to Kuan-Wei Chiu

This reverts commit e2a33a2a3258794891cdd6ca4b1318da6594d157.

After replacing the only in-tree user of sort_nonatomic() in the arm-smmu-v3 driver with the standard sort(), the _nonatomic() variants are no longer used anywhere in the kernel.

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.

---
 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.508.g3f0d502094-goog
#7 lore raw
In reply to Kuan-Wei Chiu

+Cc maintainers/reviewers of kernel/trace/bpf_trace.c

Quoted 5 lines · Kuan-Wei Chiu

Remove the sort_nonatomic() and sort_r_nonatomic() APIs from the kernel library.

Currently, the arm-smmu-v3 driver is the sole in-tree user of sort_nonatomic(). Because the array size being sorted is small in

I just realized that I missed another in-tree user: sort_r_nonatomic() is actually being used in kernel/trace/bpf_trace.c.

Looking at the bpf code, the array size there is bounded by MAX_TRACING_MULTI_CNT, which is set to (1u << 20). I guess sorting an array of this size in a single go could potentially cause scheduling latency spikes on certain configurations if we don't yield the cpu.

Because of this, it seems my proposal to completely remove sort_r_nonatomic() and sort_nonatomic() from the core library was premature.

Please let me know if you think otherwise, or if there is any alternative approach for check_dup_ids() that would allow us to safely drop sort_r_nonatomic(). Otherwise, please disregard this series.

Regards,
Kuan-Wei

Quoted 21 lines

practice, there is no real risk of triggering a soft lockup. Therefore, the periodic cond_resched() calls provided by the _nonatomic variant are unnecessary.

With the only in-tree user updated, the _nonatomic APIs are no longer needed anywhere in the kernel. Removing them effectively drops the wrapper function and eliminates the may_schedule branch from the innermost loop of the core sorting logic, slightly simplifying the code.

Kuan-Wei Chiu (2):
iommu/arm-smmu-v3: Replace sort_nonatomic() with sort() Revert "lib/sort.c: add _nonatomic() variants with cond_resched()"

 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c |   6 +-
 include/linux/sort.h                        |  11 --
 lib/sort.c                                  | 110 ++++++--------------
 3 files changed, 34 insertions(+), 93 deletions(-)
Signature · 1 line
2.55.0.508.g3f0d502094-goog
maintainer
#2 lore raw

Re: [PATCH 1/2] iommu/arm-smmu-v3: Replace sort_nonatomic() with sort()

In reply to Kuan-Wei Chiu
Quoted 30 lines · Kuan-Wei Chiu

The number of master->num_streams per master device is typically very small in practice. Sorting this array takes a very small amount of time, so there is no practical risk of triggering a soft lockup that would necessitate calling cond_resched() during the sort.

Replace sort_nonatomic() with the standard sort(). Since this is the only remaining in-tree caller of sort_nonatomic(), this change paves the way to eventually remove the unused sort_nonatomic() API from the core library.

---
Build-tested only.

I'm not really familiar with this driver or smmu internals. Based on my limited knowledge, master->num_streams should be small enough to safely switch from sort_nonatomic() to sort(), but I could be wrong.

Please review carefully and let me know if there are any cases where num_streams could actually be large enough to cause issues.

 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index a10affb483a4..dcb6be2df95e 100644

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_insert_master(struct arm_smmu_device *smmu, · line 4052

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -3
@@ -4047,9 +4047,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
 	}
 
 	/* Put the ids into order for sorted to_merge/to_unref arrays */
-	sort_nonatomic(master->streams, master->num_streams,
-		       sizeof(master->streams[0]), arm_smmu_stream_id_cmp,
-		       NULL);
+	sort(master->streams, master->num_streams,
+	     sizeof(master->streams[0]), arm_smmu_stream_id_cmp,
+	     NULL);

Makes sense to me. Nicolin, did you choose the nonatomic version specifically?

Will

#3 lore raw

Re: [PATCH 1/2] iommu/arm-smmu-v3: Replace sort_nonatomic() with sort()

Quoted 4 lines · Will Deacon
Quoted 3 lines · Kuan-Wei Chiu
(quoted diff) +0 -0
@@ -4047,9 +4047,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
 	}
 
Quoted 10 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_insert_master(struct arm_smmu_device *smmu, · line 4052

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -3
 	/* Put the ids into order for sorted to_merge/to_unref arrays */
-	sort_nonatomic(master->streams, master->num_streams,
-		       sizeof(master->streams[0]), arm_smmu_stream_id_cmp,
-		       NULL);
+	sort(master->streams, master->num_streams,
+	     sizeof(master->streams[0]), arm_smmu_stream_id_cmp,
+	     NULL);

Makes sense to me. Nicolin, did you choose the nonatomic version specifically?

It's probably rare to have a large multi-stream array, so I think sort() would be fine.

Nicolin

reviewer
#4 lore raw

Re: [PATCH 1/2] iommu/arm-smmu-v3: Replace sort_nonatomic() with sort()

Quoted 16 lines · Nicolin Chen
Quoted 4 lines · Will Deacon
Quoted 3 lines · Kuan-Wei Chiu
(quoted diff) +0 -0
@@ -4047,9 +4047,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
  	}
 
Quoted 10 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_insert_master(struct arm_smmu_device *smmu, · line 4052

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -3
  	/* Put the ids into order for sorted to_merge/to_unref arrays */
-	sort_nonatomic(master->streams, master->num_streams,
-		       sizeof(master->streams[0]), arm_smmu_stream_id_cmp,
-		       NULL);
+	sort(master->streams, master->num_streams,
+	     sizeof(master->streams[0]), arm_smmu_stream_id_cmp,
+	     NULL);

Makes sense to me. Nicolin, did you choose the nonatomic version specifically?

Quoted 2 lines

It's probably rare to have a large multi-stream array, so I think sort() would be fine.

Yeah, probably 99% of the time the size of this array will be 1. An exceptional value of "large" might be something like 64 for a massive (non-PCI) DMA engine with separate read and write IDs per channel for no good reason.

If and when someone ever were to report RCU stalls here then we can first ask them what the heck their client device is... and unless and until then I wouldn't think it's worth worrying about.

Thanks,
Robin.

#5 lore raw

Re: [PATCH 1/2] iommu/arm-smmu-v3: Replace sort_nonatomic() with sort()

Quoted 19 lines · Robin Murphy
Quoted 15 lines · Nicolin Chen
Quoted 13 lines · Will Deacon

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_insert_master(struct arm_smmu_device *smmu, · line 4051

Quoted 9 lines · Kuan-Wei Chiu
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -3
@@ -4047,9 +4047,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
  	}
  	/* Put the ids into order for sorted to_merge/to_unref arrays */
-	sort_nonatomic(master->streams, master->num_streams,
-		       sizeof(master->streams[0]), arm_smmu_stream_id_cmp,
-		       NULL);
+	sort(master->streams, master->num_streams,
+	     sizeof(master->streams[0]), arm_smmu_stream_id_cmp,
+	     NULL);

Makes sense to me. Nicolin, did you choose the nonatomic version specifically?

Quoted 2 lines

It's probably rare to have a large multi-stream array, so I think sort() would be fine.

Quoted 9 lines

Yeah, probably 99% of the time the size of this array will be 1. An exceptional value of "large" might be something like 64 for a massive (non-PCI) DMA engine with separate read and write IDs per channel for no good reason.

If and when someone ever were to report RCU stalls here then we can first ask them what the heck their client device is... and unless and until then I wouldn't think it's worth worrying about.

+1. Agreed, it should be fine to use sort() here..

Quoted 3 lines

Thanks,
Robin.

Thanks,
Praan

maintainer
#8 lore raw
In reply to Kuan-Wei Chiu
Quoted 10 lines · Kuan-Wei Chiu

Remove the sort_nonatomic() and sort_r_nonatomic() APIs from the kernel library.

Currently, the arm-smmu-v3 driver is the sole in-tree user of sort_nonatomic(). Because the array size being sorted is small in practice, there is no real risk of triggering a soft lockup. Therefore, the periodic cond_resched() calls provided by the _nonatomic variant are unnecessary.

[...]

Applied driver change to iommu (arm/smmu/updates), thanks!

[1/2] iommu/arm-smmu-v3: Replace sort_nonatomic() with sort()

      https://git.kernel.org/iommu/c/340e3c5165d4

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev