mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RESEND PATCH v2 0/2] lib: clarify comparison function requirements
@ 2025-01-06 17:01 Kuan-Wei Chiu
  2025-01-06 17:01 ` [RESEND PATCH v2 1/2] lib/sort: clarify comparison function requirements in sort_r() Kuan-Wei Chiu
  2025-01-06 17:01 ` [RESEND PATCH v2 2/2] lib/list_sort: clarify comparison function requirements in list_sort() Kuan-Wei Chiu
  0 siblings, 2 replies; 3+ messages in thread
From: Kuan-Wei Chiu @ 2025-01-06 17:01 UTC (permalink / raw)
  To: akpm; +Cc: jserv, Chun-Ying Huang, linux-kernel, Kuan-Wei Chiu

Add a detailed explanation in the sort_r/list_sort kernel doc comment
specifying that the comparison function must satisfy antisymmetry and
transitivity. These properties are essential for the sorting algorithm
to produce correct results.

Issues have arisen in the past [1][2][3][4] where comparison functions
violated the transitivity property, causing sorting algorithms to fail
to correctly order elements. While these requirements may seem
straightforward, they are commonly misunderstood or overlooked, leading
to bugs. Highlighting these properties in the documentation will help
prevent such mistakes in the future.

Link: https://lore.kernel.org/lkml/20240701205639.117194-1-visitorckw@gmail.com [1]
Link: https://lore.kernel.org/lkml/20241203202228.1274403-1-visitorckw@gmail.com [2]
Link: https://lore.kernel.org/lkml/20241209134226.1939163-1-visitorckw@gmail.com [3]
Link: https://lore.kernel.org/lkml/20241209145728.1975311-1-visitorckw@gmail.com [4]

Changes in resend v2:
- Cc @lkml

Changes in v2:
- Fix typo in patch 2: sort_r() -> list_sort()
- Fix htmldocs build warnings reported by Stephen [5]

[5]: https://lore.kernel.org/all/20250106174236.5a74e94e@canb.auug.org.au/

Kuan-Wei Chiu (2):
  lib/sort: clarify comparison function requirements in sort_r()
  lib/list_sort: clarify comparison function requirements in list_sort()

 lib/list_sort.c | 7 +++++++
 lib/sort.c      | 7 +++++++
 2 files changed, 14 insertions(+)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RESEND PATCH v2 1/2] lib/sort: clarify comparison function requirements in sort_r()
  2025-01-06 17:01 [RESEND PATCH v2 0/2] lib: clarify comparison function requirements Kuan-Wei Chiu
@ 2025-01-06 17:01 ` Kuan-Wei Chiu
  2025-01-06 17:01 ` [RESEND PATCH v2 2/2] lib/list_sort: clarify comparison function requirements in list_sort() Kuan-Wei Chiu
  1 sibling, 0 replies; 3+ messages in thread
From: Kuan-Wei Chiu @ 2025-01-06 17:01 UTC (permalink / raw)
  To: akpm; +Cc: jserv, Chun-Ying Huang, linux-kernel, Kuan-Wei Chiu

Add a detailed explanation in the sort_r() kernel doc comment
specifying that the comparison function must satisfy antisymmetry and
transitivity. These properties are essential for the sorting algorithm
to produce correct results.

Issues have arisen in the past [1][2][3][4] where comparison functions
violated the transitivity property, causing sorting algorithms to fail
to correctly order elements. While these requirements may seem
straightforward, they are commonly misunderstood or overlooked, leading
to bugs. Highlighting these properties in the documentation will help
prevent such mistakes in the future.

Link: https://lore.kernel.org/lkml/20240701205639.117194-1-visitorckw@gmail.com [1]
Link: https://lore.kernel.org/lkml/20241203202228.1274403-1-visitorckw@gmail.com [2]
Link: https://lore.kernel.org/lkml/20241209134226.1939163-1-visitorckw@gmail.com [3]
Link: https://lore.kernel.org/lkml/20241209145728.1975311-1-visitorckw@gmail.com [4]
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
 lib/sort.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lib/sort.c b/lib/sort.c
index 048b7a6ef967..8e73dc55476b 100644
--- a/lib/sort.c
+++ b/lib/sort.c
@@ -200,6 +200,13 @@ static size_t parent(size_t i, unsigned int lsbit, size_t size)
  * copy (e.g. fix up pointers or auxiliary data), but the built-in swap
  * avoids a slow retpoline and so is significantly faster.
  *
+ * The comparison function must adhere to specific mathematical
+ * properties to ensure correct and stable sorting:
+ * - Antisymmetry: cmp_func(a, b) must return the opposite sign of
+ * cmp_func(b, a).
+ * - Transitivity: if cmp_func(a, b) <= 0 and cmp_func(b, c) <= 0, then
+ * cmp_func(a, c) <= 0.
+ *
  * Sorting time is O(n log n) both on average and worst-case. While
  * quicksort is slightly faster on average, it suffers from exploitable
  * O(n*n) worst-case behavior and extra memory requirements that make
-- 
2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RESEND PATCH v2 2/2] lib/list_sort: clarify comparison function requirements in list_sort()
  2025-01-06 17:01 [RESEND PATCH v2 0/2] lib: clarify comparison function requirements Kuan-Wei Chiu
  2025-01-06 17:01 ` [RESEND PATCH v2 1/2] lib/sort: clarify comparison function requirements in sort_r() Kuan-Wei Chiu
@ 2025-01-06 17:01 ` Kuan-Wei Chiu
  1 sibling, 0 replies; 3+ messages in thread
From: Kuan-Wei Chiu @ 2025-01-06 17:01 UTC (permalink / raw)
  To: akpm; +Cc: jserv, Chun-Ying Huang, linux-kernel, Kuan-Wei Chiu

Add a detailed explanation in the list_sort() kernel doc comment
specifying that the comparison function must satisfy antisymmetry and
transitivity. These properties are essential for the sorting algorithm
to produce correct results.

Issues have arisen in the past [1][2][3][4] where comparison functions
violated the transitivity property, causing sorting algorithms to fail
to correctly order elements. While these requirements may seem
straightforward, they are commonly misunderstood or overlooked, leading
to bugs. Highlighting these properties in the documentation will help
prevent such mistakes in the future.

Link: https://lore.kernel.org/lkml/20240701205639.117194-1-visitorckw@gmail.com [1]
Link: https://lore.kernel.org/lkml/20241203202228.1274403-1-visitorckw@gmail.com [2]
Link: https://lore.kernel.org/lkml/20241209134226.1939163-1-visitorckw@gmail.com [3]
Link: https://lore.kernel.org/lkml/20241209145728.1975311-1-visitorckw@gmail.com [4]
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
 lib/list_sort.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lib/list_sort.c b/lib/list_sort.c
index 8d3f623536fe..a310ecb7ccc0 100644
--- a/lib/list_sort.c
+++ b/lib/list_sort.c
@@ -108,6 +108,13 @@ static void merge_final(void *priv, list_cmp_func_t cmp, struct list_head *head,
  * and list_sort is a stable sort, so it is not necessary to distinguish
  * the @a < @b and @a == @b cases.
  *
+ * The comparison function must adhere to specific mathematical properties
+ * to ensure correct and stable sorting:
+ * - Antisymmetry: cmp(@a, @b) must return the opposite sign of
+ * cmp(@b, @a).
+ * - Transitivity: if cmp(@a, @b) <= 0 and cmp(@b, @c) <= 0, then
+ * cmp(@a, @c) <= 0.
+ *
  * This is compatible with two styles of @cmp function:
  * - The traditional style which returns <0 / =0 / >0, or
  * - Returning a boolean 0/1.
-- 
2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-01-06 17:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-06 17:01 [RESEND PATCH v2 0/2] lib: clarify comparison function requirements Kuan-Wei Chiu
2025-01-06 17:01 ` [RESEND PATCH v2 1/2] lib/sort: clarify comparison function requirements in sort_r() Kuan-Wei Chiu
2025-01-06 17:01 ` [RESEND PATCH v2 2/2] lib/list_sort: clarify comparison function requirements in list_sort() Kuan-Wei Chiu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®