Commit f3c74b38 authored by Borislav Petkov's avatar Borislav Petkov

RAS/CEC: Fix binary search function

Switch to using Donald Knuth's binary search algorithm (The Art of
Computer Programming, vol. 3, section 6.2.1). This should've been done
from the very beginning but the author must've been smoking something
very potent at the time.

The problem with the current one was that it would return the wrong
element index in certain situations:

  https://lkml.kernel.org/r/CAM_iQpVd02zkVJ846cj-Fg1yUNuz6tY5q1Vpj4LrXmE06dPYYg@mail.gmail.com

and the noodling code after the loop was fishy at best.

So switch to using Knuth's binary search. The final result is much
cleaner and straightforward.

Fixes: 011d8261 ("RAS: Add a Corrected Errors Collector")
Reported-by: default avatarCong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
Cc: Tony Luck <tony.luck@intel.com>
Cc: linux-edac <linux-edac@vger.kernel.org>
Cc: <stable@vger.kernel.org>
parent f2c7c76c
...@@ -183,32 +183,38 @@ static void cec_timer_fn(struct timer_list *unused) ...@@ -183,32 +183,38 @@ static void cec_timer_fn(struct timer_list *unused)
*/ */
static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to) static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
{ {
int min = 0, max = ca->n - 1;
u64 this_pfn; u64 this_pfn;
int min = 0, max = ca->n;
while (min < max) { while (min <= max) {
int tmp = (max + min) >> 1; int i = (min + max) >> 1;
this_pfn = PFN(ca->array[tmp]); this_pfn = PFN(ca->array[i]);
if (this_pfn < pfn) if (this_pfn < pfn)
min = tmp + 1; min = i + 1;
else if (this_pfn > pfn) else if (this_pfn > pfn)
max = tmp; max = i - 1;
else { else if (this_pfn == pfn) {
min = tmp; if (to)
break; *to = i;
return i;
} }
} }
/*
* When the loop terminates without finding @pfn, min has the index of
* the element slot where the new @pfn should be inserted. The loop
* terminates when min > max, which means the min index points to the
* bigger element while the max index to the smaller element, in-between
* which the new @pfn belongs to.
*
* For more details, see exercise 1, Section 6.2.1 in TAOCP, vol. 3.
*/
if (to) if (to)
*to = min; *to = min;
this_pfn = PFN(ca->array[min]);
if (this_pfn == pfn)
return min;
return -ENOKEY; return -ENOKEY;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment