From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4BDD4C04FF3 for ; Mon, 24 May 2021 03:20:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1DBC36115C for ; Mon, 24 May 2021 03:20:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232221AbhEXDVl (ORCPT ); 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-Google-Smtp-Source: ABdhPJxSO6yeIC0VFxiyHU8Q5cmEiF0vtZyk4fopXUN0/UTJvOUzfGpEs7UI4BzBgrcLzpjnfEaXYNmFUfMszv4G7zM= 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.