Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4818266imu; Sat, 19 Jan 2019 19:53:34 -0800 (PST) X-Google-Smtp-Source: ALg8bN4pDpS4iv8PZV5cqEk+/dv/NoQcU0WFI4IlReZ3iRGAO9pppyYs1+qCNVASvY0kQVS5OmFb X-Received: by 2002:a63:e74b:: with SMTP id j11mr23655343pgk.397.1547956414704; Sat, 19 Jan 2019 19:53:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547956414; cv=none; d=google.com; s=arc-20160816; b=bh+RRDjv+yAIbiEyP1uz3YhmcuFGcCEGGiKfXlo+Uty6QUdPfXPZ+t3aOE7tqOLcOU 6nlUxKWsMEpgj/cN4FW08kEujWIlkuJNKYlG5DgGn2anQ/vPjwP+rJKdf6dDEdtEp38C ZidrGLGa2BB0jG53v/7tMcpOQ1lOAt1/tKGw1H4KpkrzbedAbT7Jgwx6cIWL7dTk0cVB p/PnKdpF+grbeF0jXhRmeKcQi/sYBTKial8YadpTD61caMaK46vh2iUqgpiqaKn5Z6u3 JjphnwQxIZa+s+l2/wbRX/kN8b6/bV6QfuUSnu2jOZkuu7DAtrrKu7llBKOZU/yQSLv6 +GQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=BAQVJsI8UcngSUtxBuv3IE4zDR2rqjZLY2mbROvFgrE=; b=pKGlJtNs6amNW1oFaUCi7Bj/PvfYN6h/iCY0DAE0evITSDRJUsMbW/McHgojLSEJtd 6AW0kURpO0Mx3Q9HlDjTCte3vx7MfOH/aN15yKf3aWpku7pGChLLeHncSC02ZjMK144Y kFEvR4xRI1xqb9mt54INGOCEWqDaE3dpcXfvkMt88TIm7YyRsDVMLQPO/2Lh+TvQVLWw WTiMYq2wtlg/9s8qHqN7r0oXcjzt9WFAWMUU89ox2ChlunetYZbAuNsdGrQEbO04lK4P 3iwv9juZrYpRgxHW2ZQunKJ2OFNJm6ZdTxV/CwXX7xWeACm6QD+MkWMNNpl+xErjfZyw nFsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=fs9fM+bX; 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 e67si8690378pfa.15.2019.01.19.19.53.04; Sat, 19 Jan 2019 19:53:34 -0800 (PST) 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=@linux-foundation.org header.s=google header.b=fs9fM+bX; 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 S1730113AbfATDtK (ORCPT + 99 others); Sat, 19 Jan 2019 22:49:10 -0500 Received: from mail-lf1-f49.google.com ([209.85.167.49]:38665 "EHLO mail-lf1-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730048AbfATDtJ (ORCPT ); Sat, 19 Jan 2019 22:49:09 -0500 Received: by mail-lf1-f49.google.com with SMTP id a8so13192265lfk.5 for ; Sat, 19 Jan 2019 19:49:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=BAQVJsI8UcngSUtxBuv3IE4zDR2rqjZLY2mbROvFgrE=; b=fs9fM+bXDqo459fQjYUbIrEPfhpoqy+np7j+EBfrd+reL2M/3+AhgR4i8gU66p7dmy LqVoqbUSmqO2TOtHHtk5h2bn3SUDYkNN56jURwxWRy1bPn55Ih5JTcKO3qwepgs1TVrS d45rlQdJYONPIpNAlp7PbAtTV6bTkabwMsOR8= 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=BAQVJsI8UcngSUtxBuv3IE4zDR2rqjZLY2mbROvFgrE=; b=RBwATyXk3KlvSFKPQmxQn4cc+u6YOtvTskgmoJdaQkK+sNfhGlsLbviszTrP58IVz4 H2Nb0rCt6FKAfGxmRIhtF84+Wxi2njlZHaYQFH0gMJ+Dgn+CYsHWcH1tl3kjnPb8SkOh C8ngBUWmfBeIDZHDo9poe53E8YmehmrOKs/i5zsZDS78ADBbULN6cpdZYxSEMfaBmpaU OlWA9X7kK+gu7q+sxxbcDI40xnukBLi44Hjwa5O89VwDJ4SgVdC5hhTJ9b9ra4BijIb4 DCtM6Mh/7ChX/zdhXICWqYIdn4/sYqGf3UmsOh/HEL5cq6xTFInekHU8CBIR42akczhJ qs5Q== X-Gm-Message-State: AJcUukc2w1SlbggZ0IR+04JfvPaZlKD8Wk7LKeH+0bTWfeUlsgg0USWh 84qCir1GZEB2bJu3+lAZti0Vv/X+kUfVqA== X-Received: by 2002:a19:945b:: with SMTP id w88mr14782979lfd.15.1547956147124; Sat, 19 Jan 2019 19:49:07 -0800 (PST) Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com. [209.85.167.43]) by smtp.gmail.com with ESMTPSA id 185-v6sm1465590ljj.49.2019.01.19.19.49.06 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Jan 2019 19:49:06 -0800 (PST) Received: by mail-lf1-f43.google.com with SMTP id e26so13189744lfc.2 for ; Sat, 19 Jan 2019 19:49:06 -0800 (PST) X-Received: by 2002:a19:c014:: with SMTP id q20mr14711802lff.16.1547956145556; Sat, 19 Jan 2019 19:49:05 -0800 (PST) MIME-Version: 1.0 References: <20190119151450.26879-1-Florian.LaRoche@googlemail.com> <20190120000138.GI26876@brain-police> In-Reply-To: <20190120000138.GI26876@brain-police> From: Linus Torvalds Date: Sun, 20 Jan 2019 15:48:49 +1200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: fix int_sqrt() for very large numbers To: Will Deacon Cc: Florian La Roche , Linux List Kernel Mailing , Crt Mori , Joe Perches , Davidlohr Bueso , Peter Zijlstra Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Jan 20, 2019 at 12:01 PM Will Deacon wrote: > > > @@ -52,7 +52,7 @@ u32 int_sqrt64(u64 x) > > if (x <= ULONG_MAX) > > return int_sqrt((unsigned long) x); > > > > - m = 1ULL << (fls64(x) & ~1ULL); > > + m = 1ULL << ((fls64(x) - 1) & ~1ULL); > > This just looks like a copy-paste error because there isn't an __fls64(). > But I think your suggestion here is ok, given the previous check against > ULONG_MAX. Hmm. We probably *should* add a __fls64(). There looks to be only one user of int_sqrt64(), and that one is confused. It does int_sqrt64() twice, but since the inner one will reduce the range to 32 bits, the outer one is just silly. That one user also had better not be overflowing into the high bit - it uses "s64" as a type and does seem to use signed operatons, so high bit set really means negative. sqrt() returning something odd for a negative number wouldn't be all that odd in that context. But yes, our current int_sqrt64() does seem buggy as-is, because it's *supposed* to work on u64's, even if I don't think we really have any users that care. And as Will mentioned, the regular int_sqrt() looks perfectly fine, and subtracting 1 from the __fls() return value would actually _introduce_ a bug. Linus