Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp21722315ybl; Mon, 6 Jan 2020 09:55:57 -0800 (PST) X-Google-Smtp-Source: APXvYqx/K0KIc0BOWPLj8JETYfkz+wz44VJW27NRRomiHm3xsdjK93UDdQVx82t18dS2CSq+VAiI X-Received: by 2002:a9d:590d:: with SMTP id t13mr8966945oth.290.1578333357345; Mon, 06 Jan 2020 09:55:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578333357; cv=none; d=google.com; s=arc-20160816; b=uruS18bIiXNF3Fx7cvKH37c7lT8ZGSyBAGD0mhTlQic4Kh8YDUlFf6fyJ2h3hcWV7B +FGuV5fm5nQgx4NsRp5Kv81j5B5YorKoSOV8Fb7FSeFnmphnmb6YS5iTVlqt9eY5yQIz 3k6Sp56rTcKAoXdZrDTHJfPStfmwlZCDwAnQEJfmncAzYgn/2E4JuZ2tCBmUqmlAk4iN d6DAui8nyGeEnRp/iEuWjykZEsc6SNUEoQR5WxRQFBHnUd1EfIVtclBwGeduGlU2duti AjdOj2gSh7KiZRSL0zpR07IuiCl7a67wM0mc//MzejkJlRQNnmpZkP9S37dOWDDIeg+3 Umww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=vjAnTqnyYDsojY81CidHO5cyS8TO9xjXn3TKtmGerFk=; b=gZlSAxVoWnHCrm+jprj1lA/zc5bP7gnIr/tNAZFJQcXLaUDaAHW3Ua2nOFtEoKPZxy JItgx8HbX50sZTt2q6CYfFrAeeZ25ec2/2lYCFr9POfm8cWWQDK9z6HQd2PUWEdEg4UZ mPf2TllJ+B4Q+hcbZtxPmTCLoyk0MLSbIABSvc7BwwFkvGH0G/2Jk/msK3B0FVtqVx8k U0Zryz+fFEoSQsHanUPXdIbo9P4L/xtbuAlCSTpBuEdnI9CsITVQXAVpx+DdMVBKarTs k67bIC/krAlM45Is+IsBehXbdaS14T5dGwoAUodxSMALe1y175iLcw/bcVZDJwH1zwJC TwXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=T6z20UaR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r1si36415738otq.298.2020.01.06.09.55.45; Mon, 06 Jan 2020 09:55:57 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=T6z20UaR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726710AbgAFRzE (ORCPT + 99 others); Mon, 6 Jan 2020 12:55:04 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:52518 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726690AbgAFRzE (ORCPT ); Mon, 6 Jan 2020 12:55:04 -0500 Received: by mail-wm1-f65.google.com with SMTP id p9so15911903wmc.2; Mon, 06 Jan 2020 09:55:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=vjAnTqnyYDsojY81CidHO5cyS8TO9xjXn3TKtmGerFk=; b=T6z20UaRrFmnrbUyiy+mHxDezojhLXlh7hgeFh/aF2hkWRPxHeVPthL/Ew0DWiQy/s IQlul1zUabH9uKrZKnUepn2r4SaHnN1Kg2G0At5fCXvhl7IHgDpvEKLl4AXT6vHfzDaq Yo0c+qCBLyQhSAB6dqLtlNGhJlmr7rpANLwmzDUWHKE+hgZgavVFd9BnKIt+y44qLxAb VdSL1VfCz3xLmcYNXnDSyXV6byjOI946tysgOrGGCQSVgfZVauTf0J+3Rq68zV+OGSU1 8UN11wmOktzxjq15Ck1OG1f5jKAgurQX7Qa3PbM+wwDDeASN+KnbV5Iq1uV0zr9pzoz7 q8Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=vjAnTqnyYDsojY81CidHO5cyS8TO9xjXn3TKtmGerFk=; b=STkDYvABIrvKXbMAmjlPF4HqBjtStg3/hJjHYvM4u5GtOoZzMlXhWCy3nYHvbzejOX 3hEwn1QB1ZvrKk2BxEBL13zQJHcl4aEMmuxLkSSZRMQIhiY+vEAZmGh8dKSJ19q0oSDy 0geOCAAIdPLlGhEIZO/enn1JQF5RffYBKZ4l55++kxFL00l3g6hjy2Ou4T9fsPU+9xgs pt/EdThHDUR6MXfZQIrkJUZfvvctXR5vQcYkrnsLnPZYZ7MPcoFi3IbGdQ2fWlxCijOn dMLL9pI8vr2VIaTwYMV1mdBweJrd0fkfupxeGTV5dWLp38f9HWLjOSmpN0jlbfayE6bm /D+Q== X-Gm-Message-State: APjAAAVC5zKm7acI/lpjJmS140MeaOKNKnmoiFgVIjjLcCfotqUCEgo+ W9CvDzvSibQ5foJugWq0RPQ= X-Received: by 2002:a1c:f407:: with SMTP id z7mr34005535wma.72.1578333301903; Mon, 06 Jan 2020 09:55:01 -0800 (PST) Received: from ltop.local ([2a02:a03f:40c7:f800:685b:86a3:90af:d97]) by smtp.gmail.com with ESMTPSA id s1sm24420828wmc.23.2020.01.06.09.55.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2020 09:55:01 -0800 (PST) Date: Mon, 6 Jan 2020 18:54:59 +0100 From: Luc Van Oostenryck To: Peter Zijlstra Cc: Kees Cook , Eric Biggers , linux-kernel@vger.kernel.org, Ingo Molnar , Will Deacon , Elena Reshetova , Thomas Gleixner , Anna-Maria Gleixner , Sebastian Andrzej Siewior , linux-sparse@vger.kernel.org Subject: Re: [PATCH] locking/refcount: add sparse annotations to dec-and-lock functions Message-ID: <20200106175459.tjuhmdrsusax3s4z@ltop.local> References: <20191226152922.2034-1-ebiggers@kernel.org> <20191228114918.GU2827@hirez.programming.kicks-ass.net> <201912301042.FB806E1133@keescook> <20191230191547.GA1501@zzz.localdomain> <201912301131.2C7C51E8C6@keescook> <20191230233814.2fgmsgtnhruhklnu@ltop.local> <20200106154119.GV2810@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200106154119.GV2810@hirez.programming.kicks-ass.net> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 06, 2020 at 04:41:19PM +0100, Peter Zijlstra wrote: > On Tue, Dec 31, 2019 at 12:38:14AM +0100, Luc Van Oostenryck wrote: ... > Not quite what we're talking about. Instead consider this: > > The normal flow would be something like: > > extern void spin_lock(spinlock_t *lock) __acquires(lock); > extern void spin_unlock(spinlock_t *lock) __releases(lock); > > extern bool _spin_trylock(spinlock_t *lock) __acquires(lock); > > #define __cond_lock(x, c) ((c) ? ({ __acquire(x); 1; }) : 0) > #define spin_trylock(lock) __cond_lock(lock, _spin_lock) > > > if (spin_trylock(lock)) { > > /* do crap */ > > spin_unlock(); > } > > > So the proposal here: > > https://markmail.org/message/4obybcgqscznnx63 > > would have us write: > > extern bool spin_trylock(spinlock_t *lock) __attribute__((context(lock, 0, spin_trylock(lock)); Well, allowing arbitrary conditions would be hard/impossible but you're only asking to have the *return value* as condition, right? That looks as reasonably feasible. > Basically have sparse do a transform on its own expression tree and > inject the very same crud we now do manually. This avoids cluttering the > kernel tree with this nonsense. So, a call of a function declared with __acquires() or releases() is interpreted by Sparse as if the call is immediately followed by an increase or a decrease of the context. It wouldn't be very hard to add a new attribute (something like __cond_context) and let Sparse do as if a call to a function with such attribute is directly followed by a test of its return value and a corresponding change in the context. It would boil down to: extern bool spin_trylock(lock) __cond_context(lock); if (spin_trylock(lock)) { /* do crap */ spin_unlock(); } behaving like the following code currently would: extern bool spin_trylock(lock); if (spin_trylock(lock)) { __acquire(lock); /* do crap */ spin_unlock(); } Would something like this be satisfactory? -- Luc