Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1441647rdb; Fri, 1 Dec 2023 17:38:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IFL48R7cO1qfKlyQ7nRQKyvTPFJl/upUTwr/DMKeLblWGolkNslfK9MdmpqeNkZOuBfLGvN X-Received: by 2002:a17:90a:e394:b0:286:544c:3af2 with SMTP id b20-20020a17090ae39400b00286544c3af2mr591378pjz.20.1701481124151; Fri, 01 Dec 2023 17:38:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701481124; cv=none; d=google.com; s=arc-20160816; b=F+68gxNT2z95YFiiNwdacrmzYSvPa9zlBB5pY45o7nu/nnMdD88Jn/JeBpU2hRm63b R8pwxJ7LCvEc8UuMofVz/z2zpMHFK2TxXXS5UAwPhEOgVDfaALsgIhGV1CeMy6m96f6H QTfVDIoxM2pn6rcmEKzWPWkgAVPTJS5lKPzDlCi2ijzLrupHs1ZTWGbyDhBcxwS0Ntbz MjX0nKDoSHJu/qWM8DtYKfN1JrNNJh0dH/Vzc/y4cRfa+xtGuWjwulxNu36OGn9NtDcV Op+4R2qmSxNOWh1nc2TvCDu6QvJ/GQKB981zmGJiccYvbBHr4FLOSGmJCSkVJO4pLDQL UI3w== 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=A0ufX68N1esanQTyLJA3w1Jj4v6Vxvaxep5R4BU75TU=; fh=1qb/7pfmFoF0q1JNupbCxIVVDmpkgJIDzAPZACxFnks=; b=s5WYtf8G87j47Vbq+P1SU5Ehgma/rUqq8BOVD4dUnT3lsW598nV1l6quIvWUSIzXQL wiyDHPAMCqnNMpUHVtQmT1oNwH4keNzkFPUY2soO5TbKcMdVX8fpfMwTk5Phz2j3YvgY qYhgr71hooNFzRft66R76zyeQ9WVOh5nVZLouQf8hEa6bgzlH6ZjuxswWNirZF3UeCBT QiY+Us+EG+q5rnZk2TFGEqn5x7A1hzVYsaPw+f7X/PHDhLvLFUZTGH29d6R5YTNZXDIZ WXKfh8GUqKv/7c8/RzDrmn4TvN8JZ00ne1cPKV7+0Ev7PJrv7N6j/3QWUZULT2mz77es JawQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YSG9Jt+O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id b9-20020a17090ae38900b0028654b61b78si2637595pjz.165.2023.12.01.17.38.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 17:38:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YSG9Jt+O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id A3CDE807BEE0; Fri, 1 Dec 2023 17:38:41 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231214AbjLBBhK (ORCPT + 99 others); Fri, 1 Dec 2023 20:37:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjLBBhJ (ORCPT ); Fri, 1 Dec 2023 20:37:09 -0500 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BC11116; Fri, 1 Dec 2023 17:37:15 -0800 (PST) Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-6cde14ff73bso2235851b3a.0; Fri, 01 Dec 2023 17:37:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701481035; x=1702085835; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=A0ufX68N1esanQTyLJA3w1Jj4v6Vxvaxep5R4BU75TU=; b=YSG9Jt+OOcZ4RFfgDHPjkbzkSC1YQ+O8GM/miYeTpUxnzWzgZjnSD2wjACjdDoATUn 2qr4tvlFIwVPe89rTiPpXbDhnu7wlTC5vQPNsSWkkV+EM3dGRDvhEf5BQOFHWxGBQ2hc cVuPYGIhRgsqtyzbmDXigm+xSCBiDIYtQQfLfzhCzwwJRaXFdWF9wAZQB8qw3gi8/+v6 KCnfAG/cVXjDRT0jpavGJSgYN2u0kNwmryXSMzvZrK5UskWbqsvRcUG8xoL+DsNMS05g MvXYFSqm+O84j8ZcJtxc6EZ7HU/jH2jYoi+0a8RGPMVUEwXnYADp2ANSGuJjGSLunJ/N LWWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701481035; x=1702085835; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=A0ufX68N1esanQTyLJA3w1Jj4v6Vxvaxep5R4BU75TU=; b=JMqDqM4RRSx+NdSwqjBlVm+e17aEuzf46LsSbUudKMFwkkz3hoT/Hu0VXAHkwytgdW mtem9d4PmjCHiaFrUyFXPbhYnAD2Ok/wan7vXbP7lR/ZXbPxt9ASXAoy9ej6oGJR60Lc FuOZq8/C/+ZKRTewOWDwH7dcI5Zvj38QHiXe0QOzJRmeD6U60a5hxLnGZpxGm7TJKpZg D7MavYBiBj1d6j9YbxSYsXlACKUAodxamXTAKweFDHz3vjIg0IAYurecCnfWIz/pFQ1z gcnWTjFnvoNAkLxjBUI8wh1Cy58PqpUIEhK/BWKArpMM4DXTBj54g1ZQSvixximcGTHz lybw== X-Gm-Message-State: AOJu0YwYOtzWOI6n95AIU/h3DMR1k1AulsaS6PJS23pkdZ5B8lTgBAGO olqpXnfvytB4nS3BrJQh/A7vZqR6cYfaWg== X-Received: by 2002:a05:6a00:1398:b0:6ce:2731:47bd with SMTP id t24-20020a056a00139800b006ce273147bdmr509085pfg.29.1701481034783; Fri, 01 Dec 2023 17:37:14 -0800 (PST) Received: from archie.me ([103.131.18.64]) by smtp.gmail.com with ESMTPSA id t10-20020a63dd0a000000b005c2422a1171sm3589542pgg.66.2023.12.01.17.37.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 17:37:13 -0800 (PST) Received: by archie.me (Postfix, from userid 1000) id 9A66710082367; Sat, 2 Dec 2023 08:37:10 +0700 (WIB) Date: Sat, 2 Dec 2023 08:37:10 +0700 From: Bagas Sanjaya To: Ingo Molnar , Jann Horn Cc: Waiman Long , Peter Zijlstra , Ingo Molnar , Will Deacon , Jonathan Corbet , Linux Kernel Mailing List , Linux Documentation Subject: Re: [PATCH -v2] locking/mutex: Document that mutex_unlock() is non-atomic Message-ID: References: <20231130204817.2031407-1-jannh@google.com> <2f17a9a6-5781-43ef-a09b-f39310843fe6@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="oz6cg3XNiAwQ353b" Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Fri, 01 Dec 2023 17:38:41 -0800 (PST) --oz6cg3XNiAwQ353b Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Dec 01, 2023 at 11:33:19AM +0100, Ingo Molnar wrote: > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D> > From: Jann Horn > Date: Thu, 30 Nov 2023 21:48:17 +0100 > Subject: [PATCH] locking/mutex: Document that mutex_unlock() is non-atomic >=20 > I have seen several cases of attempts to use mutex_unlock() to release an > object such that the object can then be freed by another task. >=20 > This is not safe because mutex_unlock(), in the > MUTEX_FLAG_WAITERS && !MUTEX_FLAG_HANDOFF case, accesses the mutex > structure after having marked it as unlocked; so mutex_unlock() requires > its caller to ensure that the mutex stays alive until mutex_unlock() > returns. >=20 > If MUTEX_FLAG_WAITERS is set and there are real waiters, those waiters > have to keep the mutex alive, but we could have a spurious > MUTEX_FLAG_WAITERS left if an interruptible/killable waiter bailed > between the points where __mutex_unlock_slowpath() did the cmpxchg > reading the flags and where it acquired the wait_lock. >=20 > ( With spinlocks, that kind of code pattern is allowed and, from what I > remember, used in several places in the kernel. ) >=20 > Document this, such a semantic difference between mutexes and spinlocks > is fairly unintuitive. >=20 > [ mingo: Made the changelog a bit more assertive, refined the comments. ] >=20 > Signed-off-by: Jann Horn > Signed-off-by: Ingo Molnar > Link: https://lore.kernel.org/r/20231130204817.2031407-1-jannh@google.com > --- > Documentation/locking/mutex-design.rst | 6 ++++++ > kernel/locking/mutex.c | 5 +++++ > 2 files changed, 11 insertions(+) >=20 > diff --git a/Documentation/locking/mutex-design.rst b/Documentation/locki= ng/mutex-design.rst > index 78540cd7f54b..7572339b2f12 100644 > --- a/Documentation/locking/mutex-design.rst > +++ b/Documentation/locking/mutex-design.rst > @@ -101,6 +101,12 @@ features that make lock debugging easier and faster: > - Detects multi-task circular deadlocks and prints out all affected > locks and tasks (and only those tasks). > =20 > +Releasing a mutex is not an atomic operation: Once a mutex release opera= tion > +has begun, another context may be able to acquire the mutex before the r= elease > +operation has fully completed. The mutex user must ensure that the mutex= is not > +destroyed while a release operation is still in progress - in other word= s, > +callers of mutex_unlock() must ensure that the mutex stays alive until > +mutex_unlock() has returned. > =20 > Interfaces > ---------- > diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c > index 2deeeca3e71b..cbae8c0b89ab 100644 > --- a/kernel/locking/mutex.c > +++ b/kernel/locking/mutex.c > @@ -532,6 +532,11 @@ static noinline void __sched __mutex_unlock_slowpath= (struct mutex *lock, unsigne > * This function must not be used in interrupt context. Unlocking > * of a not locked mutex is not allowed. > * > + * The caller must ensure that the mutex stays alive until this function= has > + * returned - mutex_unlock() can NOT directly be used to release an obje= ct such > + * that another concurrent task can free it. > + * Mutexes are different from spinlocks & refcounts in this aspect. > + * > * This function is similar to (but not equivalent to) up(). > */ > void __sched mutex_unlock(struct mutex *lock) Hi Ingo and Jann, thanks for the patch. The patch LGTM, thanks! Reviewed-by: Bagas Sanjaya --=20 An old man doll... just what I always wanted! - Clara --oz6cg3XNiAwQ353b Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYKAB0WIQSSYQ6Cy7oyFNCHrUH2uYlJVVFOowUCZWqKPwAKCRD2uYlJVVFO o/M0AQCWThPVVQ64FGDrGvqpOy6HkiUdyNm4B2y18fcJPzVeGAD/Yt1nlTW5XE+7 tvPu9yAzTY0HdYy/CVr/UJc7UzICbQU= =yca7 -----END PGP SIGNATURE----- --oz6cg3XNiAwQ353b--