Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp244506pxb; Mon, 13 Sep 2021 18:12:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxt74AROgeaIMU2EhKuysO36xFfH7MmgehyxVjmr/eLXpaeR9TpGKu6htCcrJfBLwMuE9Lg X-Received: by 2002:a02:1942:: with SMTP id b63mr12287077jab.49.1631581929466; Mon, 13 Sep 2021 18:12:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631581929; cv=none; d=google.com; s=arc-20160816; b=fJ5GglW+EXzKQ+arHxaVKiOdVaqzV+yqLWZWNMeKZqUQRhwJ6H119IjCQSLEK1BkXk duZeaaFO5K7Y36RQzH9KdZ6MNkH2QZ9N+KOUhCSDI3VMJb2HjbPy4vPoXqXa6qnAlCjF F6UtgvDFMV80lHNZ34cH6nUe79OzyMD7fAWld8H5fgSNW/qrl/p4/z10J70SiNazK43h n8qtNQ1sxccha7uyvC3wHf3gNFfThZhdakI0BEtbPNEylAffJHGB9y7PSPpINn8+KTKY GZO/D0pjCBRFJAjEhGc0Ep/G37s78NB5i/xMn6m6fA4LADorEU6n7jK1ogRoaDdTT6xC aDPw== 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=KsP30ov9xl9zhULjcWutgVa5WGmRNrz9j2ugExJGuYo=; b=OoIOLcl8Azos1Oa/msuDT1k3Ha6AmtFUQIStlZdoigx4mEtgvjtFh1+aT/SMRkjRrb +cgVEORUbW8egW1Na7oZGPx51Q2YrB3ko2ANmt453C/wbh9xf16dfNXsYWJx6djq1tT7 47tpqvJ6FIum4yIzQt5xD6z2CK9J7QusqUwKQx6LWtKDJy6NUq03DoV5nlesz/K48te6 SXKyfyYEWGRauJ3JCQDPHdzZuSgA4+7O22saDLonqBvKPuxO/i5dcEeV36ixayFJIcCR LugnsiPq4G2sxIeAPMazEQhm1Ub4ebesWQ4iJphqpXu0WvouPRyAqMilsL0NiASM6/kb fOJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=mygUPtbJ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d12si9858840ilg.105.2021.09.13.18.11.58; Mon, 13 Sep 2021 18:12:09 -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=@google.com header.s=20210112 header.b=mygUPtbJ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348271AbhIMVRC (ORCPT + 99 others); Mon, 13 Sep 2021 17:17:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348256AbhIMVQu (ORCPT ); Mon, 13 Sep 2021 17:16:50 -0400 Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C540C061764 for ; Mon, 13 Sep 2021 14:15:34 -0700 (PDT) Received: by mail-lj1-x236.google.com with SMTP id f2so19715424ljn.1 for ; Mon, 13 Sep 2021 14:15:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=KsP30ov9xl9zhULjcWutgVa5WGmRNrz9j2ugExJGuYo=; b=mygUPtbJXKFBf25Ytt4weIVj36Yui1nGLTd+cq+lXbA73j0/0gxaMT0z1qklKDwZjP oYzbIV8uJoXpVLqWOm88+JSCNtqV5oJK1ekAwjAFKDkYP2JMkh38dpX8b729gelnHLCM 4SLlGe56m3hX+Trb53z9qVBgR0utshgzVDurHqGX35l9QYyRHVZ4owOVp9qrVetiNWAH pynFZ5/s5LpdhpR6CWHyBac4qRy9kt1zN5ZHmylBp8QQfwqrhjs8KvmsdTDHTSHvSc74 zOsfV7AcfseUdFrfEaskaPe6x85pgcP3h2+G/UI382g+ahpXXdnlQghyJXOzrP7ZNEhA GV5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=KsP30ov9xl9zhULjcWutgVa5WGmRNrz9j2ugExJGuYo=; b=aT3L4AwGJ/wdxd1kTaEDVE1z1Fzo31bNKVwDchXqpOfXCZp018cW0jNy43ccTf/Fd8 FEr72wfawI761S2o8zf1rvgqn219ysT0WhU8lz7pvXrUFVOZder+sRLeVDjyx2vYkCui ZO1x4TxCZod3mYsYtWiv/NVV0zifIW0UI+HkkZL4TvjmY3aZlOlzuEF/yrmoIZEg0hyD 5qubFMeol4XxMFbjjo6qCQ8OjF715JrbKDDLQLDLqD1c2XGpK1uMDjh8FXfl/bZC6Vqp l2zICEPXtUn7usNRV8I7+oaEZDlX+Ejoz98yfp/xFC0e5A88Z2lD5qSKv8aPKoQM21NO v+uA== X-Gm-Message-State: AOAM531ak3m/1UYFVgKUbuMrqmVZoT/nuXhbbOrd61n2VtvRFk5di3kt f8BfgUdKb6ELaLZHkUHuzjC2CZJNvKTbn5d0u/ZeoQ== X-Received: by 2002:a2e:b551:: with SMTP id a17mr12026583ljn.128.1631567732248; Mon, 13 Sep 2021 14:15:32 -0700 (PDT) MIME-Version: 1.0 References: <20210913131113.390368911@linuxfoundation.org> <20210913131114.028340332@linuxfoundation.org> In-Reply-To: From: Nick Desaulniers Date: Mon, 13 Sep 2021 14:15:21 -0700 Message-ID: Subject: Re: [PATCH 5.14 018/334] nbd: add the check to prevent overflow in __nbd_ioctl() To: Linus Torvalds Cc: Arnd Bergmann , Sedat Dilek , Greg Kroah-Hartman , Naresh Kamboju , Nathan Chancellor , Baokun Li , open list , linux-stable , Hulk Robot , Josef Bacik , Jens Axboe , Sasha Levin , clang-built-linux , lkft-triage@lists.linaro.org, llvm@lists.linux.dev, Kees Cook Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 13, 2021 at 2:13 PM Nick Desaulniers wrote: > > On Mon, Sep 13, 2021 at 1:50 PM Nick Desaulniers > wrote: > > > > On Mon, Sep 13, 2021 at 1:42 PM Linus Torvalds > > wrote: > > > > > > On Mon, Sep 13, 2021 at 1:16 PM Nick Desaulniers > > > wrote: > > > > > > > > Do we have access to _Generic in GCC 4.9? > > > > > > We've ended up using it unconditionally since last year, so yes. > > > > Sorry, grepping would have taken < 1s. I'm very lazy. > > http://threevirtues.com/ > > > > > > > > In fact, the compiler version tests got removed when we raised the gcc > > > version requirement to 4.9 in commit 6ec4476ac825 ("Raise gcc version > > > requirement to 4.9"): > > > > > > "In particular, raising the minimum to 4.9 means that we can now just > > > assume _Generic() exists, which is likely the much better replacement > > > for a lot of very convoluted built-time magic with conditionals on > > > sizeof and/or __builtin_choose_expr() with same_type() etc" > > > > > > but we haven't used it much since. > > > > > > The "seqprop" code for picking the right lock for seqlock is perhaps > > > the main example, and staring at that code will make you go blind, so > > > look away. > > > > Looking at my patch: > > https://lore.kernel.org/stable/20210913203201.1844253-1-ndesaulniers@google.com/ > > I don't think _Generic helps us in the case of dispatching based on > > the result of is_signed_type() (the operands could undergo type > > promotion, so we'd need lots of cases that are more concisely covered > > by is_signed_type()). It could replace the nested checks in div_64 > > with nested _Generics, I think. Not sure it's a huge win for > > readability. Maybe cut the number of expansions of the parameters in > > half though. Let me give it a try just to see what it looks like. > > Is this more readable? Same line count. I'm not sure if there's such > a thing as "fallthrough" between the "cases" of _Generic to minimize > duplication, or whether this could be factored further. Needs lots > more () around macro param use and tab'ed out line endings... Sorry wrong diff: diff --git a/include/linux/math64.h b/include/linux/math64.h index bc9c12c168d0..8fc4d56a45b9 100644 --- a/include/linux/math64.h +++ b/include/linux/math64.h @@ -162,18 +162,18 @@ static inline s64 div_s64(s64 dividend, s32 divisor) div_u64(dividend, divisor)); \ }) +#define __div_64(dividend, divisor) _Generic((divisor), \ + s64: div64_x64(dividend, divisor), \ + u64: div64_x64(dividend, divisor), \ + default: div_x64(dividend, divisor)) + #define div_64(dividend, divisor) ({ \ BUILD_BUG_ON_MSG(sizeof(dividend) > sizeof(u64), \ "128b div unsupported"); \ - __builtin_choose_expr( \ - __builtin_types_compatible_p(typeof(dividend), s64) || \ - __builtin_types_compatible_p(typeof(dividend), u64), \ - __builtin_choose_expr( \ - __builtin_types_compatible_p(typeof(divisor), s64) || \ - __builtin_types_compatible_p(typeof(divisor), u64), \ - div64_x64(dividend, divisor), \ - div_x64(dividend, divisor)), \ - dividend / divisor); \ + _Generic((dividend), \ + s64: __div_64(dividend, divisor), \ + u64: __div_64(dividend, divisor), \ + default: dividend / divisor); \ }) -- Thanks, ~Nick Desaulniers