Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp889887rwd; Thu, 8 Jun 2023 09:04:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Wzki/MhbX0Udjur09rv1/8S5tkC8biBi7Kt3+qfSfO2blzX/HdGviC8Q3fPHHVSlJfaft X-Received: by 2002:a17:90b:4b83:b0:256:5895:a2f9 with SMTP id lr3-20020a17090b4b8300b002565895a2f9mr4961315pjb.28.1686240257188; Thu, 08 Jun 2023 09:04:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686240257; cv=none; d=google.com; s=arc-20160816; b=EgGIf2YMugGYvERAujJVCz2bqP5jisncTS1moszrXepFbM8RzabXSlqT/Nhjp1RSh+ HFerZLa5vlC48aR4+VwS60GTvA6zGTpn6V7Ui7hFbiFrasWWLo9DS9okQWq2EDjao3dt jlMpS/vvVddnCKmpKRbnTzHZW5cl2UMq6dzWSlMPCjefmIjm1qyrCjh8ecdVpboRxITF JA/cPmCcxOafoU9HmzaSJkPZ+yFqd1j5vnIU6gxa4UuduhjOfP2ofDEubtQ9wsNJ32pd 5DRuAy2LOl37wq7W4zP38qVDJCGnRu4LVvx46FDOEaA7gUV5nu7IPNdhyIIjhoL/Msvc AaYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ikLyVGzKcEMuSqUp/nI13OAR5DV5p124KJuoTG/oIAE=; b=CCjuGF+pLpJZtZ18wjFk/ldzbz3Qv+QBy7UJ3gwEV1jTbGcFV5h45Vpxzu70L2PuC1 0/tESvJj3Uk+u+ADVEMbgIVCxb2DXk0TIfaF0SaiQ5ONCOMXQ0+mWOpwnms/RoGufeyN r/eMoCrEbBPYQVqmh4U3TLud7SVN8HiBMoNLsw8E8iCmXn/1q8EOZ+M7sW9nDb7oEjD0 wkQPFFZ3uKbfWm2vYSXLdNkyVTKD4rUn6dw7RK+vrGW5r+JgTeCID0H82w4EgUeyCAJy aA4V0rPi+hbdUrTK6x4BrAypZ6/B6NPjZRa1HsuWuFeafC+/h49YlG4oWVZVTIBSyR6L maeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=GYKQIuQg; 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 bg6-20020a17090b0d8600b00256df359e79si1274279pjb.33.2023.06.08.09.04.04; Thu, 08 Jun 2023 09:04:17 -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; dkim=pass header.i=@linux-foundation.org header.s=google header.b=GYKQIuQg; 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 S236082AbjFHPrt (ORCPT + 99 others); Thu, 8 Jun 2023 11:47:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234372AbjFHPrr (ORCPT ); Thu, 8 Jun 2023 11:47:47 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E19F435BC for ; Thu, 8 Jun 2023 08:47:17 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-510d6b939bfso1404618a12.0 for ; Thu, 08 Jun 2023 08:47:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; t=1686239171; x=1688831171; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ikLyVGzKcEMuSqUp/nI13OAR5DV5p124KJuoTG/oIAE=; b=GYKQIuQgjsVrmT3tlSDLb2UFU7H6aZhNmRSdQLE3cqdlTHsPgAzy0sD98ItbZQOz42 oSThFhAsKJB6ohC03mananbZfmtFI8OpYtuzh3P4b7qS1g4INhWNz5I2zJM7RgEvTXM+ 8NE6DQBw858AqBhOQDeTtfwuWYQmum1fWawa8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686239171; x=1688831171; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ikLyVGzKcEMuSqUp/nI13OAR5DV5p124KJuoTG/oIAE=; b=D8priT2J02uGrvSMT4QxSkf8XFoCST8aYJkMCxgWWNYFHJN/uYKpJV39D0whHpdGdK RiayQe/OdIG2LHkn0U/kp+NVnUC8Z/n5Lsv4gy3PxR6l0BiVvu4+XL7/ojeyx9E4wPXD LRWRxDWxwq7Kt2yUlA7Dcfqj+xYSh2G7npAdDSAOYjQdQU95mMabK3hgesQ61Zwu2A3A ksQkRfCIXvoWupPDeJTUG3n6h4y5nY4KN29c97FnBWsjfE7tg9FiulCihqUuNkgdI5tL lG1C61/RYMCeGrs2pZdX3AAq4kkjU5zn2cHgGdcmJT8kQR9Z6I9mIsEWtTnk23D8A/ix ujfg== X-Gm-Message-State: AC+VfDzcR2Uh2pRntBV0J5yt7LrBdi3X5+8Y5p/MebelL5BNC9tV8CTu ECSEMBmW04eUziHMQvVLpgKXeCA8wVVLj+OrWTV8Mw== X-Received: by 2002:a17:907:26cc:b0:974:1c90:b3d3 with SMTP id bp12-20020a17090726cc00b009741c90b3d3mr194169ejc.12.1686239170820; Thu, 08 Jun 2023 08:46:10 -0700 (PDT) Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com. [209.85.218.51]) by smtp.gmail.com with ESMTPSA id lj12-20020a170906f9cc00b009786b73974fsm843306ejb.145.2023.06.08.08.46.10 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 08 Jun 2023 08:46:10 -0700 (PDT) Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-977d6aa3758so146757766b.0 for ; Thu, 08 Jun 2023 08:46:10 -0700 (PDT) X-Received: by 2002:a17:907:8a15:b0:961:2956:2ed9 with SMTP id sc21-20020a1709078a1500b0096129562ed9mr197548ejc.25.1686239169847; Thu, 08 Jun 2023 08:46:09 -0700 (PDT) MIME-Version: 1.0 References: <20230526205204.861311518@infradead.org> <20230530092342.GA149947@hirez.programming.kicks-ass.net> <20230606094251.GA907347@hirez.programming.kicks-ass.net> <20230606134005.GE905437@hirez.programming.kicks-ass.net> <20230606180806.GA942082@hirez.programming.kicks-ass.net> <20230607094101.GA964354@hirez.programming.kicks-ass.net> <20230608085248.GA1002251@hirez.programming.kicks-ass.net> In-Reply-To: <20230608085248.GA1002251@hirez.programming.kicks-ass.net> From: Linus Torvalds Date: Thu, 8 Jun 2023 08:45:53 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2 0/2] Lock and Pointer guards To: Peter Zijlstra Cc: keescook@chromium.org, gregkh@linuxfoundation.org, pbonzini@redhat.com, linux-kernel@vger.kernel.org, ojeda@kernel.org, ndesaulniers@google.com, mingo@redhat.com, will@kernel.org, longman@redhat.com, boqun.feng@gmail.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, paulmck@kernel.org, frederic@kernel.org, quic_neeraju@quicinc.com, joel@joelfernandes.org, josh@joshtriplett.org, mathieu.desnoyers@efficios.com, jiangshanlai@gmail.com, rcu@vger.kernel.org, tj@kernel.org, tglx@linutronix.de, linux-toolchains@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,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 Thu, Jun 8, 2023 at 1:53=E2=80=AFAM Peter Zijlstra wrote: > > Or perhaps use the smart-pointer concept applied to our classes like: > > #define smart_ptr(name, var) \ > __INSTANTIATE_VAR(name, var) So this is the only situation where I think "ptr" makes sense (your "fat pointer" argument is nonsensical - sure, you can treat anything as a pointer if you're brave enough, but that just means that "pointer" becomes a meaningless word). However, I think that for "smart pointers", we don't need any of this complexity at all, and we don't need that ugly syntax. > Then we can write: > > DEFINE_CLASS(kfree, void *, kfree(THIS), p, void *p) > > smart_ptr(kfree, mem) =3D kzalloc_node(...); > if (!mem) > return -ENOMEM; No, the above is broken, and would result in us using "void *" in situations where we really *really* don't want that. For automatic freeing, you want something that can handle different types properly, and without having to constantly declare the types somewhere else before use. And no, you do *not* want that "kfree(THIS)" kind of interface, because you want the compiler to inline the freeing function wrapper, and notice _statically_ when somebody zeroed the variable and not even call "kfree()", because otherwise you'd have a pointless call to kfree(NULL) in the success path too. So for convenient automatic pointer freeing, you want an interface much more akin to struct whatever *ptr __automatic_kfree =3D kmalloc(...); which is much more legible, doesn't have any type mis-use issues, and is also just trivially dealt with by a static inline void automatic_kfree_wrapper(void *pp) { void *p =3D *(void **)pp; if (p) kfree(p); } #define __automatic_kfree \ __attribute__((__cleanup__(automatic_kfree_wrapper))) #define no_free_ptr(p) \ ({ __auto_type __ptr =3D (p); (p) =3D NULL; __ptr; }) which I just tested generates the sane code even for the "set the ptr to NULL and return success" case. The above allows you to trivially do things like struct whatever *p __automatic_kfree =3D kmalloc(..); if (!do_something(p)) return -ENOENT; return no_free_ptr(p); and it JustWorks(tm). And yes, it needs a couple of different versions of that "__automatic_kfree()" wrapper for the different freeing cases (kvfree, rcu_free, whatever), but those are all trivial one-liners. And no, I didn't think too much about those names. "__automatic_kfree" is too damn long to type, but you hated "auto". And "no_free_ptr()" is not wonderful name either. But I tried to make the naming at least be obvious, if not wonderful. Linus