Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3061535pxj; Sun, 23 May 2021 20:21:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwbgEpyLCVQCxHIfACWidSw79IUb6UnjxeYvKU4/C8GuOjOLgvYG/Ybv54wPclm8Sp2WKhV X-Received: by 2002:a05:6602:8d0:: with SMTP id h16mr13544803ioz.145.1621826506384; Sun, 23 May 2021 20:21:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621826506; cv=none; d=google.com; s=arc-20160816; b=rH35yvt24kruoMctE4qpDM2y9yiu0opiJG9K2Ot7KcGW8GwTU/GgJaDtQIa/M49KJh JpKTy616brTmEoG1w2wm61JOq/BMjxy5/gdCEMdO3+X6RPDhM1CkO+yp0phRWcM5jdhX T1ejXXqa1mbIFp8CuOyEJcVUiRkSZ8zLplQTR4Vp22iLVvMOyzhyoaAXSNPWV9OtObP/ Wjf/+7NvKZ7IT3SqmneX2shSzRSOJ+/TGUGHmYbl4V8s/mYyiMeNco+T/3YFcsn6xG7E +rs2JGry1Wp7KNvvKabMiBV7VeIAO6yDP0ggh1NPA8Mg5V205XeLeoth2zirm7CRu0OD 80pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=8UV+IIiaESw5plOu1kB4kNiRQoDlm7XC16kel3ldThc=; b=vRqhR9tP/yzJA5vUZmGceWGZvisbgnRR393AtWF8Sh9qAeM7GPNaHh4mxeRHUt1Hug XLQXKWkEMVbwD9Nd+uT5w1BZ8hjn9Wjs8z8DwiUQKwCAWWclcBCulGZCathLoQvHGjXy fLV86YsE8bKlP2/bLv6Sn6NU0qx6T5g8CvjN/yvdE4PTdR0EbQ9CZHDUw3w7D58dCzqg D/L9BZiRU2i0Zc41aG+mnCxPNJC7XJ/lOs335LG9BQejjAZ3KVT8RXJKD3XniGmPEtH6 mYhG34xpkwSVXxMon+ebfj+v0F9vFcsRK7wVB4xs5EaZVr4nfZLJJsTPCypAiOsUhTkM Zlhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=t4ZF0pX9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g9si14145936ila.105.2021.05.23.20.21.33; Sun, 23 May 2021 20:21:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=t4ZF0pX9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232208AbhEXDVl (ORCPT + 99 others); Sun, 23 May 2021 23:21:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232124AbhEXDVk (ORCPT ); Sun, 23 May 2021 23:21:40 -0400 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 529B1C061574 for ; Sun, 23 May 2021 20:20:12 -0700 (PDT) Received: by mail-ej1-x62a.google.com with SMTP id f18so9300471ejq.10 for ; Sun, 23 May 2021 20:20:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=8UV+IIiaESw5plOu1kB4kNiRQoDlm7XC16kel3ldThc=; b=t4ZF0pX91tbZPTG4a3RsnCUsR867kJhIsRLFK2FJvhqbrXneRMEuVHJKslabXH8ejX PB90Q3oadFOPaqrZNJCBOivSFJiKvRT72yty1H8/o2Z5Y0sGOPzVHtQ2pVVzPWFYZnri eko5ylTi32YLWCvWC3mtP1LMWFXUzC8Q6EtJtW4jl4ys26bhMIbBDOnshPR8L4vEAZww zqzDqvNIbRJ059UvvScw40Cy3efBzmejnA32TDgnm/OEgSBQbfJ++XvV4cPBK8To92SU lVtet/excg0XP/P5C4dg4Dws3TMZf0A9IX3Mq/KWsXoshd2x9q6nL/W45yF9Ok2UeayA lXIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=8UV+IIiaESw5plOu1kB4kNiRQoDlm7XC16kel3ldThc=; b=cTNQOnIQkZjF0AU6vmlqvSv6N987NrVVkt13ZqnVgbF8A0ZsVrb3cLHVUmPDSaLDVv agSYbg4yMHU2N2zDmml5SoR48tIE36IcDs2rZrOFyswthMRC/XLho8hICaxdWnlzKtNH N3L58M+bU1eAeoE3juL2uiilb7LnxjugoXM9c4kdw7FW1+MSAZu8D6Ti0aqzu+t0t0Ew i9gdFRowDjXO8YfEsR0hSe8kWcYGz/1M5cXosrBM1Xy1uErdnJJ/4iYo3TMgtc20lssT V+h0A+0hohL+n+SCwF5YHKmDZMbZJyawVGA3cXI8JQGq4PORkl+yp1s9mZfwxhzxFug6 H+sg== X-Gm-Message-State: AOAM531l7fDWcZD+UE/+n7hknLf9JpTc+GGQbcQYnlKLuPxNrT7O/EUN cAfnjRLQdgW5LYsMaPRw3CVrtp5Icxf0Q6VTC+MLDHFxPsMDZg== X-Received: by 2002:a17:906:1e15:: with SMTP id g21mr20998448ejj.241.1621826410849; Sun, 23 May 2021 20:20:10 -0700 (PDT) MIME-Version: 1.0 References: <20210522190759.GA9755@basil.scara.com> In-Reply-To: <20210522190759.GA9755@basil.scara.com> From: Trent Piepho Date: Sun, 23 May 2021 20:20:00 -0700 Message-ID: Subject: Re: A divide by zero bug in lib/math/rational.c (with triggering input) To: Oskar Schirmer Cc: Andy Shevchenko , Daniel Latypov , Yiyuan guo , "linux-kernel@vger.kernel.org" , "andy@kernel.org" , "akpm@linux-foundation.org" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, May 22, 2021 at 12:08 PM Oskar Schirmer wrote: > On Fri, May 21, 2021 at 12:53:27 +0300, Andy Shevchenko wrote: > > On Fri, May 21, 2021 at 12:20 PM Trent Piepho wrote: > > > On Fri, May 21, 2021 at 12:55 AM Yiyuan guo wrote: > > Sorry, it does not. E.g. with the given fraction of 31/1000 > and the registers restricted to 8 and 5 bits respectively, the > proposed fixed function would still divide by zero, because > n1 == 0. If it was for the division by d1, the test for !d1 Yes, values less than 1 less than the smallest allowed non-zero value will divide by zero will finish on the 2nd iteration, with n1 == 0, and divide by zero. The finished patch I've since sent fixes this. > Moreover, for a fraction of 33/1000, both the original and > the latest version would produce 1/30, which is off by some > 1.01%, but the proposed fixed version would result in 1/31, > which is worse: 2.24% off. Finished patch correctly produces 1/30 in this case. > I think the original function was not so bad. And the code it > produced was much shorter than the latest version, although > this might not be an argument in times, where a simple OS > kernel is beyond the 40MB. I measured this. I've compared the original, which did not consider semi-convergents nor out of range values, the current version, which does semi-convergents but fails on out of range, and the patched version, which handles that too. Size in bytes: X64 149 205 278 ARM 164 220 300 So 129 bytes on x64 and 136 bytes on ARM. Not all that much. I didn't try writing a special check for large/small inputs in the older code to see how large that was, for a more like-to-like comparison to my latest patch.