Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1174230rwd; Thu, 8 Jun 2023 13:26:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5HzcWuqGguHX+XYJyv/bB5lk7khKtk5NnC6lnFAHzRCwgiLJZ66sfVNPS+iMhM6gTW8mJe X-Received: by 2002:a17:902:7c17:b0:1b0:61b9:bd7a with SMTP id x23-20020a1709027c1700b001b061b9bd7amr4821898pll.62.1686256013260; Thu, 08 Jun 2023 13:26:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686256013; cv=none; d=google.com; s=arc-20160816; b=V/uWCkIrdkCTYN7cHrhWA5dANnfZ+Uu22gnHI0sODU6MOlRrAxfsvUDhG+hcMJGN+9 elEXqKHjrx2cmZnqyXBzbEcD9rv3CmB/rUmw4MCmG2+NNjcZG8n8FWfNz79NSADzesLG cF+4nS4w4YIwaqWnr0FEqZ906lNWXZKsBJYWPTcvp6uLj7I5JatXyYMi+Rj/lKhtuCIz ErTshWJa0fsE8BqvdX3FzXL27bsR5kHLY2erWRIRwGxXZiz3OWY7SWchy78qRq1Oawmp UeqWQfe/gWEgDEVdFC0jyd2gJa9M8ZpKpYZN9/6N23a8l1PCqpUSUzcMXoZ1vyxYJOfH v3cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=aBWb6+eQE9WxD6vRX+GhZGTfIUuI+dRuiyxZYMV/GPQ=; b=t+QQACoVk2RQ7tqEgGeWyK6Q1KxDuux7fsuQenXd2tM9E1L9laIGiwHX5XHHR0KKaL 3QYCEGMOBikci48aQ9lioUJ9/Pt7xisfGYDmzw9xrHcinlqmLBku/Ise+sHsfBazBdrO G41juXD1Tr9O46iUwG5LbXCHRrUvWvlKsvArs4pzNHChCyI0h/lUdcFgZI8tTNnfj1Oo QOs+eGw7ae9ZHZKBImSTtxX6fE0YKowLFiJG66SERRedam68aCgkWKxgmpBjOXaXK7Yi 2ULFfbiQYa6q0RPkoZG4XrSxg73Wz9xfHA2+jTZ4HRPH87MULROJV8nJlRtLb0WSUJGQ PpWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=h7PislLI; 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 l12-20020a170903244c00b001ac94b7f2f0si1544939pls.523.2023.06.08.13.26.36; Thu, 08 Jun 2023 13:26:53 -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=@infradead.org header.s=casper.20170209 header.b=h7PislLI; 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 S236294AbjFHUG5 (ORCPT + 99 others); Thu, 8 Jun 2023 16:06:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236211AbjFHUGy (ORCPT ); Thu, 8 Jun 2023 16:06:54 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 043752733; Thu, 8 Jun 2023 13:06:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=aBWb6+eQE9WxD6vRX+GhZGTfIUuI+dRuiyxZYMV/GPQ=; b=h7PislLIipTcXw8+pW6DDmylyF kc2wuHtQDmcx1H5nVi+vDHDKy1hhMb5lXFVmdCurjG+Buu2562Z4LFsGy2WuvGpTyyCXjOLZUJpiN 5MISDg9Jwu9E+MB8M3nwERQhlj5MY/KlTUv2eAV4eIu1pgsM2YMBj7Xg4kvQNzlJ8FRasIHTe5Jo1 wbBhtXnTQBkaOA3zGHF6gHz20VkgF8X8msfu+S0/8dA7KafLcKCcmvr2Vm5F5r+TsNYaNPvGxSCgi Pn/x/Q6nv2k3jK5jaume4nu79eGBBxBiSAinVZk1YcbwnNGhx0lbSZnqus9fi7bzvoI4CV6p61ELM /EGI3GnA==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1q7Ltl-00Fo55-TI; Thu, 08 Jun 2023 20:06:22 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id F2A3830026A; Thu, 8 Jun 2023 22:06:18 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id CFC28245A9E28; Thu, 8 Jun 2023 22:06:18 +0200 (CEST) Date: Thu, 8 Jun 2023 22:06:18 +0200 From: Peter Zijlstra To: Linus Torvalds 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 Subject: Re: [PATCH v2 0/2] Lock and Pointer guards Message-ID: <20230608200618.GA1020752@hirez.programming.kicks-ass.net> References: <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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham 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 08, 2023 at 08:45:53AM -0700, Linus Torvalds wrote: > > DEFINE_CLASS(kfree, void *, kfree(THIS), p, void *p) > > > > smart_ptr(kfree, mem) = 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. Ah, I see what you did with the no_free_ptr(), that avoids having to have two pointers around, nice! > So for convenient automatic pointer freeing, you want an interface > much more akin to > > struct whatever *ptr __automatic_kfree = 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 = *(void **)pp; if (p) kfree(p); } > #define __automatic_kfree \ > __attribute__((__cleanup__(automatic_kfree_wrapper))) > #define no_free_ptr(p) \ > ({ __auto_type __ptr = (p); (p) = 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 = kmalloc(..); > > if (!do_something(p)) > return -ENOENT; > > return no_free_ptr(p); > > and it JustWorks(tm). OK, something like so then? #define DEFINE_FREE(name, type, free) \ static inline __free_##name(type *p) { type _P = *p; free; } #define __free(name) __cleanup(__free_##name) #define no_free_ptr(p) \ ({ __auto_type __ptr = (p); (p) = NULL; __ptr; }) DEFINE_FREE(kfree, void *, if (_P) kfree(_P)); struct obj *p __free(kfree) = kmalloc(...); if (!do_something(p)) return -ENOENT; return no_free_ptr(p); DEFINE_CLASS(find_get_context, struct perf_event_context *, if (!IS_ERR_OR_NULL(_C)) put_ctx(_C), find_get_context(task, event), struct task_struct *task, struct perf_event *event) DEFINE_FREE(free_event, struct perf_event *, if (!IS_ERR_OR_NULL(_P)) free_event(_P)); struct perf_event *event __free(free_event) = perf_event_alloc(...) if (IS_ERR(event)) return event; class(find_get_context, ctx)(task, event); if (IS_ERR(ctx)) return (void*)ctx; if (!task && !container_of(ctx, struct perf_cpu_context, ctx)->online) return -ENODEV; ... event->ctx = get_ctx(ctx); return no_free_ptr(event); works for me, I'll go make it happen.