Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3975076rwa; Tue, 23 Aug 2022 13:42:50 -0700 (PDT) X-Google-Smtp-Source: AA6agR7IUwNvV2lR/ZpO0cltQdJZ/7fvWWuaOih4C3zxOLheEUyHvqaHW/8AZSHm7+95vnqXVdO5 X-Received: by 2002:a63:f4d:0:b0:41c:5b90:f643 with SMTP id 13-20020a630f4d000000b0041c5b90f643mr22452892pgp.537.1661287369796; Tue, 23 Aug 2022 13:42:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661287369; cv=none; d=google.com; s=arc-20160816; b=pM5C2IiJxdcmv+swo+1CHIV7ZYXUQVIQCTIRxrTLZ5GXEVZk5RY8p0eIZRN7r56rAg xrRcA98sQupdsJ2Bea7CIpU/1t7tp9XsfG0n/+r1RhGU7f97bQyXpMp+n5e6ETUCvh8/ IQyK5nQJaunZ+G3oNzhgynf2zbrbwfKNszgUjSIkt6yN0Mi0cmadAGlxDtPa8i21nnMa oepzBJYFMrH2Ql59yncEfG+K5HCb7G9LM3ZZeHaHr8aL9aqrzWh6tH/hy+iJDBgt1BBk 7rW+w70fpg3LcW4ZHff/TqpEpyszYRcZHrl0n7MaxYKy2/ZOew8kqPY/88OgjIM0lKuL O7lA== 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; bh=FvOQiT0TsbXNUArqe4thB3SqDZo6X/Lj2phpd0Hb6sc=; b=xGdZiQVi7eIX0ffN1+R9DVNgf0wSAZnjWpfeOuOC7IXNr+jtHjqhg1LlbI31XUjL3s zVxgJ0m+WS9hnIcbRDMC8USC08Yks2R8gWpyoPg1Ip970FuzP3HZqBbgxOdzEiShxMO7 FMH1ndDZFIHuLgmJXwVPXtd3gh/IWNQdy17M3euAMrHcaEXHiL7b97Eef+ltKWgAOJa8 2EybGI9By1B88AnnDfBIXZgrKuN5WbOKTDOrDoSV3+mEUlKWrlDiZGReOppoElcHL6g1 FLDolYHtb0c+zNVrrHCVbRr3iGEI68KPMCs9MlFddclpATw1sIllE8T/W1uigkV4ESpe ey6A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p12-20020a63fe0c000000b00429f6569910si16126746pgh.348.2022.08.23.13.42.38; Tue, 23 Aug 2022 13:42:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233757AbiHWUlg (ORCPT + 99 others); Tue, 23 Aug 2022 16:41:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231351AbiHWUlC (ORCPT ); Tue, 23 Aug 2022 16:41:02 -0400 Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 901E01014 for ; Tue, 23 Aug 2022 13:31:32 -0700 (PDT) Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-335624d1e26so410455737b3.4 for ; Tue, 23 Aug 2022 13:31:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=FvOQiT0TsbXNUArqe4thB3SqDZo6X/Lj2phpd0Hb6sc=; b=tsUki6rLVe8OJMkrVaW2RbKF20cfN9cd+DVRnoWuMuIJ9ybwhjRmbxome3lclrjolT HvF5SzKGpxkmjscEdc6LYSjWcXQ71HZVrHJvhfAWN3lEUNZhwjOyRRRbCcimjWf5Vv/a Nk8m75nTB7lGMyp6U4vyVNM2J6umDdxq8qbrHDV5xjzbhkIL35NE0R9s+I0YwAz6063x dlf8Hc8/D7SG6iISN7Ic4ffsbM/kr/gCph/9NY+OaRRDe0sq2VIRgIJmRJ3dLnW5Gvq6 NVFLMVj8KtS7cUypmU052vVRbF8P2wcg8R3P3cHifjyhucqqhaoSmj3QYidDaZN26qq7 gXtA== X-Gm-Message-State: ACgBeo0qEz+MzbI0JDceodzoN2dGl0s52ashicnDbbPp7UIex0IGp8tr 6xzBUZaq/t93FMuRzvA8fMFtikPMv0BjMWt6/9g= X-Received: by 2002:a25:7455:0:b0:68e:e73f:5fd5 with SMTP id p82-20020a257455000000b0068ee73f5fd5mr24329275ybc.20.1661286691491; Tue, 23 Aug 2022 13:31:31 -0700 (PDT) MIME-Version: 1.0 References: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> <20220812114438.1574-1-mailhol.vincent@wanadoo.fr> <20220812114438.1574-3-mailhol.vincent@wanadoo.fr> In-Reply-To: From: Vincent MAILHOL Date: Wed, 24 Aug 2022 05:31:20 +0900 Message-ID: Subject: Re: [PATCH v5 2/2] x86/asm/bitops: __ffs,ffz: use __builtin_ctzl to evaluate constant expressions To: Borislav Petkov Cc: Nick Desaulniers , Thomas Gleixner , Ingo Molnar , x86@kernel.org, Peter Zijlstra , Dave Hansen , "H . Peter Anvin" , Nathan Chancellor , Tom Rix , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, David Howells , Jan Beulich , Christophe Jaillet , Joe Perches , Josh Poimboeuf Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed. 24 Aug. 2022 at 02:43, Borislav Petkov wrote: > On Tue, Aug 23, 2022 at 10:12:17AM -0700, Nick Desaulniers wrote: > > Callers of these need to guard against zero input, as the pre-existing > > comment notes: > > > > >> Undefined if no bit exists, so code should check against 0 first. If the fact that __ffs(0) is undefined is a concern, I can add a safety net: #define __ffs(word) \ (__builtin_constant_p(word) ? \ (unsigned long)__builtin_ctzl(word) + BUILD_BUG_ON_ZERO(word): \ variable___ffs(word)) It will only catch the constant expression but still better than nothing (this comment also applies to the other functions undefined when the argument is zero). Also, if this aspect was unclear, I can add a comment in the commit log to explain. > This is just silly. > > And then there's > > * ffs(value) returns 0 if value is 0 or the position of the first > * set bit if value is nonzero. The first (least significant) bit > * is at position 1. > */ > static __always_inline int ffs(int x) > > Can we unify the two and move the guard against 0 inside the function > or, like ffs() does, have it return 0 if value is 0? There is an index issue. __ffs() starts at 0 but ffs() starts at one. i.e.: __ffs(0x01) is 0 but ffs(0x01) is 1. Aside from the zero edge case, ffs(x) equals __ffs(x) + 1. This explains why __fss(0) is undefined. Yours sincerely, Vincent Mailhol