Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4965156rdb; Fri, 15 Sep 2023 19:35:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFqecEG+omkEAIZhX0cBKIMO6H7Nfgggw+Dw8GDrBUduIzKSgyrUlyMxvOchWv8Pb7cF7rw X-Received: by 2002:a05:6a20:4423:b0:153:353e:5e39 with SMTP id ce35-20020a056a20442300b00153353e5e39mr4191952pzb.51.1694831753248; Fri, 15 Sep 2023 19:35:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694831753; cv=none; d=google.com; s=arc-20160816; b=hrWFrmAmJNZ6JtkKH2E48NJI5JZRAo1p5DqFVDf4w0pSJAvEzJ7n3hgXdYdksIAf/J HVnJT+rLQEBvaFuH/N9PXKoO05RzZq9hNnH+c5I/JSTNdgU+qJ/sZO0zZRVhKAC+6VaB Xl8UaUMiC5sNMwIulZvKls0uzxJnlvzJY3z927TMDJz0mc34eEtJrJV9CL00usHvdGLy EIBwEirClqVw/DK4lr5Oxuk2IPxieYfVrinzrLK8ZApXIjEoI6fE4FNK1I95vrpKeTVj +1R2PK7Yx698GosgNrd2o8TRT6jlkPqGTeJwtrg62Zp/P24+cSo5V2Md+j1oknZqHLc3 edbw== 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=nTcrc1WI4o6opPx+ch/98DWgJ1PnGcifLTiurtvgQxc=; fh=0T/Cz/6kfzyq4MPg6wrhuDkAsXxv4Wnv+2jNEKRU/s0=; b=ph2Uz2aPIx99bn3EeLGOwD44htvzvd6pen37RA7zBzQraPvZkJoWKpww9G8b8Emjg2 fGHcP6ajQRQQRItH6EuxliiyvQZQIl6s5IOAx8h9wsWe45wW5CUd6sCOqHEKTFm29I9C rjtKUboC72RiGXJd6hYPPDK/SLiuf+FLUhNSZNQOIQ0NX1iK2iBs0DMTBF9TBEYYpIDG hFxfrWyiLDRYRbKmvlw53EUXXtD6b3pB7LUn5J/iaLnI2Xpr4jPytQ7l8uofJDAMwWVM GPYhp3NVbhuHl5jdcnvtY1zqgp9ICeDBntOfEcuq48lQxEEww+bhkq1LDU/m6KLkB+eI +Wfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=lub14jPr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id l10-20020a170903120a00b001c3e13018ffsi4424711plh.51.2023.09.15.19.35.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 19:35:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=lub14jPr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id B39B382DFB84; Fri, 15 Sep 2023 15:14:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236653AbjIOWOI (ORCPT + 99 others); Fri, 15 Sep 2023 18:14:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231444AbjIOWNo (ORCPT ); Fri, 15 Sep 2023 18:13:44 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6E58B7 for ; Fri, 15 Sep 2023 15:13:36 -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=nTcrc1WI4o6opPx+ch/98DWgJ1PnGcifLTiurtvgQxc=; b=lub14jPrTEx+0tuussJqRGSykT OboUeGnY5+kd6utFHiv5AFJY3QeT91ddyUKUO2LqRkLqEFNSMz387BC92Q2XzMLagLtWpDIVhCiWU ufL9qda9jDZ1Ea/6a1cmbCeYv73E4MIU0zqDx+2LhveuTxsM3SYuJnKe4bL1zjWLNy9SvDyUc/Yyp +rk3BESsqhGmiAvxndPrnALHUkgvUP5DhiWz2Q7yLEe0jRrnogCyj3glVbmEtJN1/MPDqH82fHdd/ 9YPq+34iQ9ZwXIwvJyjDXIsFrYkAh/E3F3g603e1Hs8catbWno1ywoAfaOLHy2eh0o7kqlz78/xJe lSBlD5IQ==; 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 1qhH48-00CNVR-S8; Fri, 15 Sep 2023 22:13:32 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id 871903003F2; Sat, 16 Sep 2023 00:13:32 +0200 (CEST) Date: Sat, 16 Sep 2023 00:13:32 +0200 From: Peter Zijlstra To: Linus Torvalds Cc: Bartosz Golaszewski , Alexey Dobriyan , linux-kernel@vger.kernel.org, Linus Walleij , akpm@linux-foundation.org Subject: Re: Buggy __free(kfree) usage pattern already in tree Message-ID: <20230915221332.GC23174@noisy.programming.kicks-ass.net> References: <20230915210851.GA23174@noisy.programming.kicks-ass.net> <20230915213231.GB23174@noisy.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=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Fri, 15 Sep 2023 15:14:41 -0700 (PDT) On Fri, Sep 15, 2023 at 02:50:48PM -0700, Linus Torvalds wrote: > On Fri, 15 Sept 2023 at 14:32, Peter Zijlstra wrote: > > > > > > It also got me thinking about named_guard() for the myriad of > > conditional locks we have. > > > > named_guard(try_mutex, foo_guard)(&foo->lock); > > if (foo_guard) { > > // we got the lock, do our thing > > } > > Hmm. It looks ugly to me. I really hate the "named_guard" thing. One > of the reasons I liked the guard/scoped_guard() macros was because how > it created _anonymous_ guards, and made it completely unnecessary to > make up a pointless name. Yeah, the anonymous thing is very nice. My ulterior motive for the above was perhaps extending it to allow the lock argument to be NULL. Which would give us a handy conditional pattern. struct rw_semaphore *exec_update_lock = task ? &task->exec_update_lock : NULL; named_guard(rwsem_read_interruptible, exec_lock_guard)(exec_update_lock); if (task && !exec_lock_guard) return -EINTR; And yes, that is definitely not pretty, but it does provide a fairly wide array of options. > If trylock ends up being a common pattern, I think we should strive to > make it a lot easier to use. > > Can we make it act like "scoped_guard()", except the lock function is > fundamentally conditional? > > Call it "cond_guard()", and make the syntax otherwise be the same as > "scoped_guard()", iow, using a unique ID for the guard name. > > So > > cond_guard(try_mutex)(&foo->lock) { > .. this is the "we got the lock" region .. > } > > would I think be a much better syntax. > > Could we live with that? For the trypical use-case that is definitely the more appealing syntax. Something like: #define cond_guard(_name, args...) \ for (CLASS(_name, scope)(args), *done = NULL; \ !done && scope; done = (void *)1) works for the simple cases, but something like: try_spinlock_irqsave would be a bit of a challenge because that would end up with one of those structs that is not a pointer and they don't cast to a boolean :/ I think I can make it work, I'll go have a play, but perhaps not now, it's past midnight ;-)