Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3000897ybi; Mon, 17 Jun 2019 14:24:28 -0700 (PDT) X-Google-Smtp-Source: APXvYqzCcjt1UMOXgxkEW9/HVeUNW1PKwDx9aKNLNs+1a2XD0ZiuWB+SO8j5Xh6tHkSZUAMBuspY X-Received: by 2002:a62:ce4f:: with SMTP id y76mr392605pfg.21.1560806668221; Mon, 17 Jun 2019 14:24:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560806668; cv=none; d=google.com; s=arc-20160816; b=lwqoo05yLj9tZ5lp3qAXGGyIkaiBPdnKzkOXJLPBsiPqhFCLke5IWYIg2ljeop81wc +Qm2vL72C8gmG3vu7K+t9DFmLUGxrRP6WYwdscSgcOFNOd8AtLqDd1hK7uW9LnITBE/2 acy/OQVWnvYZMkUljwUN0bSmA3O3WmSGdqqL5okSy8ozquSAuwmFAiiyTWyB57SK5HOg fSC5IDhEufD6LysbU6+512GnWi3qUBCyieryLYM1XFfn7psoDoL2xyEL3kclSbJ6sTDN 3fVHuRLYxdFyTg1pH3Yyk/Val4H/kRqL4AvmmjJU+2xQhWPYnr+t1DtJ4UJhmpAkgmul JCJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ZB42iOWAjHXgYApW+tWo4/OS8q3CHs1UYrHdMBcOK/k=; b=Ilxar3raFkV4sb8WM2YpiYlkWSBhXOGA9WrWoi3WKn5vljtJSOmgsnqiu6FtjzPwtB t1oEd0tED3ZoJPrn8m6fSULLT9vu9Ob+fgonMen9+FC+IeqkshepaboLNrOU4HqNE2O5 rHwrD2pwr+R/1ZdXLB/1RVcFf0BArN9Tk0CUsKKjpIcViWMnrretsoHX9K2ehmuge3Sc MFlYBoKH1g24RqJvRRZXub52H8HAM42LStQVeZ2tR+4jkUx0doOjeX4QZYX1vPKr6Th0 Qf6Xuz5gGJsqWX8ocDar4a/m/ZsPwGqaQEjY4SxPZqB4pvPmjHCmNSHV0MMbQp3i0OcN iJuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wiOOsKcf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s12si300212pji.93.2019.06.17.14.24.13; Mon, 17 Jun 2019 14:24:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wiOOsKcf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729695AbfFQVXQ (ORCPT + 99 others); Mon, 17 Jun 2019 17:23:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:48240 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728776AbfFQVXP (ORCPT ); Mon, 17 Jun 2019 17:23:15 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EBAA0208E4; Mon, 17 Jun 2019 21:23:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1560806594; bh=LoHlJZBEv0RrFbmc7NuM7XQI0oTKz8RtuWr9Qov2/a8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wiOOsKcfTlkoXaeB4OV4SGRQ9cXii+zNYs7wHD+ZBhKxds1TXepqzpCx5VQtB+EGl TVyEa3j4FMVhcYvGR1AvnXPuil9hAkGT6DZUyXwbffM108P8tYQlyXrMAgnpfv1crH 2gS+OGS1bOmmr5JRHc/Q3OCeEQg1CHn/Nq7U55VM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Cong Wang , Borislav Petkov , Tony Luck , linux-edac Subject: [PATCH 5.1 107/115] RAS/CEC: Fix binary search function Date: Mon, 17 Jun 2019 23:10:07 +0200 Message-Id: <20190617210805.428973654@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190617210759.929316339@linuxfoundation.org> References: <20190617210759.929316339@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov commit f3c74b38a55aefe1004200d15a83f109b510068c upstream. 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: 011d82611172 ("RAS: Add a Corrected Errors Collector") Reported-by: Cong Wang Signed-off-by: Borislav Petkov Cc: Tony Luck Cc: linux-edac Cc: Signed-off-by: Greg Kroah-Hartman --- drivers/ras/cec.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) --- a/drivers/ras/cec.c +++ b/drivers/ras/cec.c @@ -181,32 +181,38 @@ static void cec_work_fn(struct work_stru */ static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to) { + int min = 0, max = ca->n - 1; u64 this_pfn; - int min = 0, max = ca->n; - while (min < max) { - int tmp = (max + min) >> 1; + while (min <= max) { + int i = (min + max) >> 1; - this_pfn = PFN(ca->array[tmp]); + this_pfn = PFN(ca->array[i]); if (this_pfn < pfn) - min = tmp + 1; + min = i + 1; else if (this_pfn > pfn) - max = tmp; - else { - min = tmp; - break; + max = i - 1; + else if (this_pfn == pfn) { + if (to) + *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) *to = min; - this_pfn = PFN(ca->array[min]); - - if (this_pfn == pfn) - return min; - return -ENOKEY; }