Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7B7BC64EC4 for ; Fri, 3 Mar 2023 17:36:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231128AbjCCRg2 (ORCPT ); Fri, 3 Mar 2023 12:36:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231180AbjCCRgY (ORCPT ); Fri, 3 Mar 2023 12:36:24 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 802E51A662 for ; Fri, 3 Mar 2023 09:36:23 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id 6-20020a17090a190600b00237c5b6ecd7so6872733pjg.4 for ; Fri, 03 Mar 2023 09:36:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1677864983; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=UjNXU1tEXsuTG4huv7r5UMJSNLOXXYqAwRyDV6iA0nY=; b=s9e/fu31hth4h2Ezrh++sCZEz7apyCzhh/ecfgZdCSv3b1e+JENsCuofnmp09mrVjb pLww16Zs2kSFitsAi0qb3RHZlU2GofisOBInmnPL/l1FDgQ1Bg12c2oPMvNUtVIiNU3p l8XFzb5HGZCekKW01QSWvD0VW768QdWuqh5g4eLGo1XCrAsuAqT/PLG1XOtxZaZcH7qj M7ZWdyC3sLFRnsMybpfu6AspGCkBwDkNOjxb2wBcVLI9kJAfd7StdtDX4AWnlZdC7jmi iCRQQ6VsJNu8vkh+Lf49Q3V5z5IsUV5JC5ojhGaBywaV8oV2sEF3cLrK6UdINl3nNJWd tSiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677864983; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UjNXU1tEXsuTG4huv7r5UMJSNLOXXYqAwRyDV6iA0nY=; b=r7uomkJlyjiDg7prOr2E+3kw1YQeRIW46YTL8qnwBNsOxCv+eJAB6TZ8KFjFe9EyWP 4CYOCV1iIjK1ufiPy0NU2rrdfTSPthBmNLQMzQPeqHxObH2gFJi5++fXN7HWTyS8Ea06 /40nFhhDp4h7rdv6PmYanYdpnQc8aZszg4BUx98salJgGdiKDRRlHkn/+HGiXHkW3HL3 kyV/8b6TbIwJdIhC+rnII9UoiPxu++gvbcR8vfsDD/A2VOBOP1Lcuoy5P4kCb3OqpZxd iyXpI0Evl2aYMEfXrXZauhkVut1K6uuFviA6PAraS0IB5T24OoSS3g10xm2cGpkUOWMY Hw1Q== X-Gm-Message-State: AO0yUKXDYUmU6sRc2DuV+uNc41+Xf0r/I0RIZVKO7AAJ0RCnuhaqS2+h WAqsyp9g0wLm6+mdNOejYS8TXg== X-Google-Smtp-Source: AK7set8thLIQH+7wkXPgqf3SnXnzYHJkd3YmLbiqXBcBE1bnZIf/qgdzipF1B7Rgs/q/mlvoGHpmTg== X-Received: by 2002:a17:90b:4f8c:b0:237:6178:33b1 with SMTP id qe12-20020a17090b4f8c00b00237617833b1mr2565701pjb.19.1677864982773; Fri, 03 Mar 2023 09:36:22 -0800 (PST) Received: from google.com (223.103.125.34.bc.googleusercontent.com. [34.125.103.223]) by smtp.gmail.com with ESMTPSA id fv24-20020a17090b0e9800b002340f58e19bsm1763623pjb.45.2023.03.03.09.36.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 09:36:21 -0800 (PST) Date: Fri, 3 Mar 2023 09:36:17 -0800 From: David Matlack To: Mingwei Zhang Cc: "Wang, Wei W" , "pbonzini@redhat.com" , "Christopherson,, Sean" , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH v1] KVM: allow KVM_BUG/KVM_BUG_ON to handle 64-bit cond Message-ID: References: <20230301133841.18007-1-wei.w.wang@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 02, 2023 at 09:53:35PM -0800, Mingwei Zhang wrote: > On Thu, Mar 2, 2023 at 5:50 PM Wang, Wei W wrote: > > > > On Friday, March 3, 2023 2:12 AM, Mingwei Zhang wrote: > > > > On Thursday, March 2, 2023 12:55 PM, Mingwei Zhang wrote: > > > > > I don't get it. Why bothering the type if we just do this? > > > > > > > > > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > > > > > index 4f26b244f6d0..10455253c6ea 100644 > > > > > --- a/include/linux/kvm_host.h > > > > > +++ b/include/linux/kvm_host.h > > > > > @@ -848,7 +848,7 @@ static inline void kvm_vm_bugged(struct kvm > > > > > *kvm) > > > > > > > > > > #define KVM_BUG(cond, kvm, fmt...) \ > > > > > ({ \ > > > > > - int __ret = (cond); \ > > > > > + int __ret = !!(cond); \ > > > > > > > > This is essentially "bool __ret". No biggie to change it this way. > > > > > > !! will return an int, not a boolean, but it is used as a boolean. > > > > What's the point of defining it as an int when actually being used as a Boolean? > > Original returning of an 'int' is a bug in this sense. Either returning a Boolean or > > the same type (length) as cond is good way to me. > > What's the point of using an integer? I think we need to ask the > original author. But I think one of the reasons might be convenience > as the return value. I am not sure if we can return a boolean in the > function. But it should be fine here since it is a macro. > > Anyway, returning an 'int' is not a bug. The bug is the casting from > 'cond' to the integer that may lose information and this is what you > have captured. typeof() won't work if cond is a bitfield. See commit 8d4fbcfbe0a4 ("Fix WARN_ON() on bitfield ops") from Linus from back in 2007: commit 8d4fbcfbe0a4bfc73e7f0297c59ae514e1f1436f Author: Linus Torvalds Date: Tue Jul 31 21:12:07 2007 -0700 Fix WARN_ON() on bitfield ops Alexey Dobriyan noticed that the new WARN_ON() semantics that were introduced by commit 684f978347deb42d180373ac4c427f82ef963171 (to also return the value to be warned on) didn't compile when given a bitfield, because the typeof doesn't work for bitfields. So instead of the typeof trick, use an "int" variable together with a "!!(x)" expression, as suggested by Al Viro. To make matters more interesting, Paul Mackerras points out that that is sub-optimal on Power, but the old asm-coded comparison seems to be buggy anyway on 32-bit Power if the conditional was 64-bit, so I think there are more problems there. Regardless, the new WARN_ON() semantics may have been a bad idea. But this at least avoids the more serious complications. Cc: Alexey Dobriyan Cc: Herbert Xu Cc: Paul Mackerras Cc: Al Viro Cc: Ingo Molnar Cc: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h index 344e3091af24..d56fedbb457a 100644 --- a/include/asm-generic/bug.h +++ b/include/asm-generic/bug.h @@ -33,7 +33,7 @@ struct bug_entry { #ifndef HAVE_ARCH_WARN_ON #define WARN_ON(condition) ({ \ - typeof(condition) __ret_warn_on = (condition); \ + int __ret_warn_on = !!(condition); \ if (unlikely(__ret_warn_on)) { \ printk("WARNING: at %s:%d %s()\n", __FILE__, \ __LINE__, __FUNCTION__); \ @ [...] As for int versus bool, I don't see a strong argument for either. So let's stick with int since that's what the current code is using and that aligns with the generic kernel WARN_ON(). If someone wants to propose using a bool instead of an int that should be a separate commit anyway and needs an actual justification.