Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3005148ybi; Mon, 17 Jun 2019 14:30:28 -0700 (PDT) X-Google-Smtp-Source: APXvYqwIC1SfxSdZsNpQWky30bVweHJjEUNDg3RmB6VBTMWuxSam7BZRUkaoWwVX2LRMfACikqcU X-Received: by 2002:aa7:818b:: with SMTP id g11mr386579pfi.122.1560807028055; Mon, 17 Jun 2019 14:30:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560807028; cv=none; d=google.com; s=arc-20160816; b=ttnuoErJqaPzqLlxW+4gnpQBiyaQmv2nLBY6L86GAzKNpXnP1Z/R03JfTeZtd0sfoW BGoOSzHC0RzOT1ZXvxCLJ6erlCZe87VVzz4I1urpQpJYJ206g5M2S+LtI+EP78BgnQoK dwhvCStxtbtMqFF5x2+LZdoeahVfA7kmOtqfE4N1CaM2Wi73Voe4I3No6LhNx3+RyicD 7l1NdoItQwUBdG0awrc69O9vn3G7BRgSYpTXgIzZGimRH6JnOi9Iv+0cUqDaPGx/vx5V UC5p6fLGXx45T9W9YV6s7R4lPFhUD9Exj+FXCp28JBoMFR4sPBnuNA3w2+lWtatEOCOM B1DA== 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=+ALubHBZflzWh3Ok7qg/FO9TM+jXUCo4iH4B81SMgN8=; b=g84FZ9X7g/oOcqImdafc1ZHa48EAEnFRXWEUATwi9U/ZBHOKxNLW/ajMIOouzSBVal tXClbTIPW1HtHczKNxH6LwDv/ii2gpzDrqZ0Ze4x9+GITlOkKdzC8IbLxfk1k51VSqHZ RI9ZCCyV87yNo1C8TwnTudgJE+Zl9EfiKUd7GvbQz/mTtmCngScGkwsP6DEoSEOEBnyf yig56kgmS+l4qAic0vVl5p9TreiNiq/6jbTJE2K/3a0NqSQbWKnWeVVIhNd4a0MvWQMA eVjWmIlN9XmX7aGYS/vpSbi7p8NL68GIpN99x7GOvIETkwRm2AHt/1skF4dha7zBjr4+ 42wQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=hcTgmcIM; 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 a4si10717084plm.209.2019.06.17.14.30.13; Mon, 17 Jun 2019 14:30: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=hcTgmcIM; 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 S1730376AbfFQV3y (ORCPT + 99 others); Mon, 17 Jun 2019 17:29:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:56956 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729963AbfFQV3q (ORCPT ); Mon, 17 Jun 2019 17:29:46 -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 E69A7204FD; Mon, 17 Jun 2019 21:29:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1560806986; bh=PUNNahiVj8y47dRYhB1/uNkyF/LxS2ZbcuAPzyHaIyY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hcTgmcIMxdIYNr9x5AeghKhACW+HbWdZVg5uT/WV+VR69rp7dFX5dAqzbXDrXKXs0 bEhXSQz27CXioLw43004/wa3nl+OGAQwfCIJqdE3CohUbGSOW1sfaMl5omkQRAB/7k BXqqZCcbqten8HQC7Stb/lHzayfy56uey/yU0AFo= 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 4.14 50/53] RAS/CEC: Fix binary search function Date: Mon, 17 Jun 2019 23:10:33 +0200 Message-Id: <20190617210752.550132533@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190617210745.104187490@linuxfoundation.org> References: <20190617210745.104187490@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 @@ -185,32 +185,38 @@ static void cec_timer_fn(unsigned long d */ 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; }