Received: by 2002:a05:7412:98c1:b0:fa:551:50a7 with SMTP id kc1csp1702871rdb; Mon, 8 Jan 2024 07:40:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IHjLVtHWq5b6FSWLErBWTdOD2idXtV532M/E7xmmDgpBkhbeo3mRA2HDZNN24Rsj9vVYzzU X-Received: by 2002:a05:6a00:2392:b0:6d9:a073:b21f with SMTP id f18-20020a056a00239200b006d9a073b21fmr1487519pfc.7.1704728449406; Mon, 08 Jan 2024 07:40:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704728449; cv=none; d=google.com; s=arc-20160816; b=IDgn7Y6buOJ7O7weZaWktTHw4J9lGR039jyhNPujixp+EmUdNMxHtUOA9hPtSgwCP0 tuH5pI4eklV6EbXANRP+av/9zQB5tLvUWvh/u1P7g315w/zCosyw6RsCXSQ8ovvPGLXX Klxbj0ZLouT1o/IxaGNg/RUhKGfL+5zC3NQVRM0fqzyyZgvAo7iI1+4xfgKNnb4KGp8E K72Mpl6cnCaar9/FTroxHgyyIOwMW6g7HMYmoxWmURZvDS9swiuvMBipCQjno5Ob8Ens VcU53MD0qjMMyGBB+xUnTX4JkVXj4kszi9IgBy/RSDf22/SdWKGcZtcbM7OYx6mHRq4a uqMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=Isb5ut/bgR2/r/5F9YVy69h9kXC52tbAkHmnKiVEMAY=; fh=Lr9XvX136OcEDZnCyYBH7LNHrIAD6WrzUI23J+/nKRE=; b=UM8urnoLI3OSNjMSY7pUvwQ/vhH+0m5PoOI9PfZhsIwVe79RbP0CcJzpWAnrLHdnMl kM+pD/sJs45ShmI3jkodmOau2JzyWb9HpEtqgTrJYuzBPAYeMwyoaBR+Z+nF7uh+Gool 3jmSxen0Gopc8xG7WESUbd7KwChBjx0IB7yJ/chqYl3vlPvSmGv6N6eNY6vmu+G2AzWH JHbqVx9A+5GU85gVxcnRq/5tvG08quAHa6OyA8pBjlGXheHArDCxJn4onbNSktbwbojQ zWQ6fVfC54brUYyX4fVyV/sxGl/C2PbRxn7lKu0QmDqx/sUSvyH6Wcue63HDjsD528iJ dyqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=L8XvTMFz; spf=pass (google.com: domain of linux-kernel+bounces-19770-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-19770-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id s1-20020a625e01000000b006dabf5c26b5si11430pfb.224.2024.01.08.07.40.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 07:40:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-19770-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=L8XvTMFz; spf=pass (google.com: domain of linux-kernel+bounces-19770-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-19770-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 200BB284399 for ; Mon, 8 Jan 2024 15:29:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E41E85102E; Mon, 8 Jan 2024 15:28:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="L8XvTMFz" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 869C051015 for ; Mon, 8 Jan 2024 15:28:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-ed1-f44.google.com with SMTP id 4fb4d7f45d1cf-5534180f0e9so13973a12.1 for ; Mon, 08 Jan 2024 07:28:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704727729; x=1705332529; darn=vger.kernel.org; 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=Isb5ut/bgR2/r/5F9YVy69h9kXC52tbAkHmnKiVEMAY=; b=L8XvTMFz+FqT5IYD+cdWuhRKyF3Ar0k6TDArT+GP5KsonDmtexmzwyMVYZLMEln0Qd YUVRsk8BPr0gNjDIugXnsoaKxr7zRz1WKiqjixe5jEMU42V+FjfpRflOXURylYL+yOq4 0uVk4gxuSV5203Rp9qIWFrEie8slfuDjbArzC7G/T4RJfSL24MF8A68ZLuPB4ky9wXlI SjiBjlFOYshvPC5P2K/JKpMUsqTwrGIi0SwCZhmSMeCCynu1XrDe6eXOH5nA0CANshNr tHwWIW+deNMZCtLdpG0siNJdojg7hT+Y+wrzguNBKOJrwqkBPCTIXe+qcIEAxLcKii+G 6BtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704727729; x=1705332529; 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=Isb5ut/bgR2/r/5F9YVy69h9kXC52tbAkHmnKiVEMAY=; b=smI7404JfAJGqs/rY5I21q4D93uByTB5rXKRhAmcrCE3oPLp1eogkbLw0QRl90K8w7 bnaW2aJeyY4oieoJx9MHH0fXRN3m4ZIC8y5bGJiSl27z043KIVk2aVPBx5kjK9ryEVgG 6j7uQz8nZ6sMRR0pl2vhNQloXjXKvazR9nIZhU8EZ7YEv+QUsc+sNDWFKeuJCdFmCA2d WwcWB4s51mAiha4ZEeHSC1CX+/PMqoUkQfQ8Tqo20603FK7JS/eYCku76VVk2f+MR8Ym rBQ9IJjGDzx6rghinwCU+SU4kFptTI/Fp5ZugPuiBCvYgCNz3onPQSGwPB3AjGWnM6tX PFYw== X-Gm-Message-State: AOJu0YxrHJCBAsgAIUZ3akcZLTjR+E36xJRjAyVBerKEzjHXWYgELgGu KxrS/qowxAmUJJijX9pdwtsrIeDsCVfuRGw8JXa2aMFsZi6/ X-Received: by 2002:a50:8a93:0:b0:554:2501:cc8e with SMTP id j19-20020a508a93000000b005542501cc8emr282070edj.6.1704727728768; Mon, 08 Jan 2024 07:28:48 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231130204817.2031407-1-jannh@google.com> <170142744948.398.4203675877225809071.tip-bot2@tip-bot2> <20231201121808.GL3818@noisy.programming.kicks-ass.net> In-Reply-To: From: Jann Horn Date: Mon, 8 Jan 2024 16:28:11 +0100 Message-ID: Subject: Re: [PATCH] locking/mutex: Clarify that mutex_unlock(), and most other sleeping locks, cannot be used to reference-count objects To: Ingo Molnar Cc: Peter Zijlstra , linux-kernel@vger.kernel.org, linux-tip-commits@vger.kernel.org, x86@kernel.org, Linus Torvalds Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jan 8, 2024 at 9:45=E2=80=AFAM Ingo Molnar wrote= : > * Peter Zijlstra wrote: > > > On Fri, Dec 01, 2023 at 10:44:09AM -0000, tip-bot2 for Jann Horn wrote: > > > > > --- a/Documentation/locking/mutex-design.rst > > > +++ b/Documentation/locking/mutex-design.rst > > > @@ -101,6 +101,12 @@ features that make lock debugging easier and fas= ter: > > > - Detects multi-task circular deadlocks and prints out all affec= ted > > > locks and tasks (and only those tasks). > > > > > > +Releasing a mutex is not an atomic operation: Once a mutex release o= peration > > > > I still object to this confusing usage of atomic. Also all this also > > applies to all sleeping locks, rwsem etc. I don't see why we need to > > special case mutex here. > > > > Also completion_done() has an explicit lock+unlock on wait.lock to > > deal with this there. > > Fair enough - but Jan's original observation stands: mutexes are the > sleeping locks most similar to spinlocks, so the locking & object lifetim= e > pattern that works under spinlocks cannot be carried over to mutexes in a= ll > cases, and it's fair to warn about this pitfall. > > We single out mutex_lock(), because they are the most similar in behavior > to spinlocks, and because this concern isn't hypothethical, it has been > observed in the wild with mutex users. > > How about the language in the attached patch? In case you missed it, I sent this rewritten documentation patch in response to the feedback I got, intended to replace the patch that is now sitting in the tip tree (but I don't know how that works procedurally for something that's already in the tip tree, whether you'd want to just swap out the patch with a forced update, or revert out the old version, or something else): Since there were comments on how this is really a more general rule than a mutex-specific one, that version doesn't touch Documentation/locking/mutex-design.rst and instead documents the rule in Documentation/locking/locktypes.rst; and then it adds comments above some of the most common unlock-type functions that would be affected.