Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1095181rdb; Fri, 1 Dec 2023 07:02:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IE1C99A/xCqp4+18bWzII5ESkc6uCn66cKDFNz5+zMiSUnigZBt7i2zKuqb2ANB5LYOBCuE X-Received: by 2002:a05:6820:2291:b0:581:ed38:5506 with SMTP id ck17-20020a056820229100b00581ed385506mr3226597oob.4.1701442949924; Fri, 01 Dec 2023 07:02:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701442949; cv=none; d=google.com; s=arc-20160816; b=Qgw/iVVCp7hrDi/+gBOSsl2NT7eboNQi3yo/Gh6jJgc0KDQXgmzFIK9q30rb+5EOle WOEtKs26zZxRgeQIQsmnwFpsZfi+ii43rNOPxt0U30A2Tdeo7uc20qBqkLQ00GvWN+BW Cj0EP2+JsfeTHTn2E7Zg7wH0e7LOh1WCHXkNblrgiX03NAGaI3lqit1uRbbkuw4FLBfr eiQtAFcuisRD9Tc22WGeyOLX0CKzkFdjCg5YvQPeQEnbknua+RoihySF+xH3lk/OUNaj XTnAA93m+9qzRFXx2yWJVjED77KM1xK7RpPvOLetyQUM44vr5ryLw44rUumsceDGcnjN OG6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=bvS5+725FOUXFMMq/VbInwmdI+QK4gqPGaiLWv9jUGk=; fh=t1uXTGCC/v/dIMvdvPPQ6N8GgXP05V9O5loGC4lJlNo=; b=gBUmfvb1+Nef4vz5iEiU7s1vOlaKBj/hLb/swg8Xwx97DitPcAr5v5Qvqd06Tu8i2p yH3A0/ECb62gsfC8YEMxtiIO4TImlzb1ihdR2JWhYG/LWxRtBMyDMwvChS2EUW9vQ8YC 25Cz8+MsJOfrz8ZjqNC6aOtRE6/aYLunI1nmTma3DZCxlqzx5Dg6jre5MTfxSXPWlBSf hsJ/qQ7lvHcy+yKQvhcZRuU5IMEBDe0yVb53WRGiK5YE9fbP8WA/28bfKCaj8l3tXfaf 31Rh1VuUPZvWG1MiKbcFaoHkgP3oN7GSwJCClXzliGlnJDDjnINZLG+pbxpSbiQoUc9l v8PA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=dARl+zrX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id x24-20020a4a6218000000b0058a0d3fb332si1260053ooc.66.2023.12.01.07.02.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 07:02:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=dARl+zrX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id C774F8057E45; Fri, 1 Dec 2023 07:02:25 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379283AbjLAPCJ (ORCPT + 99 others); Fri, 1 Dec 2023 10:02:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379274AbjLAPCI (ORCPT ); Fri, 1 Dec 2023 10:02:08 -0500 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48BB61707 for ; Fri, 1 Dec 2023 07:02:14 -0800 (PST) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-54744e66d27so11874a12.0 for ; Fri, 01 Dec 2023 07:02:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701442933; x=1702047733; 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=bvS5+725FOUXFMMq/VbInwmdI+QK4gqPGaiLWv9jUGk=; b=dARl+zrXLvpcS/xiphPXSPqyZYY8eFofHIrNxUUHuKWtM4gEVnYvLSPTvefqumBeCK koAI8ZIFebWAJLLvnbPANIgQyXEwCGBRLWuMX0Wqr/S1UNIqYRARIDlVlGv89sm3XV+O NLkyvJPUllLlM/E22D3sGMcUaEGrLAUTXqnbYVq8lYnbRqr5efvnLxTTcJHHiuKIbi/k czu4HuZ6uTa2NSUdz+wx5rOl8xcoXQUsfBjae7GxRJO7Vbb2M1if884yIUcrscnhToDl 8c8X4TTbFwSoaB/Dvqazo9UP5XWk276Rxxc3HEv3lMvXJ1/7nFX1a0ZaBlLdz23QDXOY fX3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701442933; x=1702047733; 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=bvS5+725FOUXFMMq/VbInwmdI+QK4gqPGaiLWv9jUGk=; b=jyhJOgTNVdAnx1xo4oS+cQvJFLOtwnPGY241oA73861Pmd0CSbHZMwEdCaWYoWA4v/ 3RMS3YVYtyUZNxHSDOUzkXNTUO+aB/gPwfIJQHZjjVPTJftagSS9E8loCDtRB0k0nuP7 HL8Sv1xVA+L3FdN4IJ+piuMXib5ngks/yzCSADKQbctx7bXnpkMn6uvs90icUuEcaH9w zcRWHNN22tQmN8YGZ9gjFBv3F89zBHyhEi2yvzggiYNeQfw7eKu03IeR92T955lStIUQ DkUMiTsq83m+xDUIemRwtCFrAVNhP77Oj90+NgZIvKjYDgCrPIyL93+ShelSLYfGte+U Sp2g== X-Gm-Message-State: AOJu0YyZjf/Caukk2fXKIcKhOTQKbRoRADNRlm64a7BRS6YH8KLLmejX 0nR760dluT8I5igUkLMwieMfzi86t83k61L66NnoAQ== X-Received: by 2002:a50:9514:0:b0:544:e2b8:ba6a with SMTP id u20-20020a509514000000b00544e2b8ba6amr94399eda.3.1701442931362; Fri, 01 Dec 2023 07:02:11 -0800 (PST) MIME-Version: 1.0 References: <20231130204817.2031407-1-jannh@google.com> <06c05c8b-9a3b-4c04-b898-0f82e98da70f@redhat.com> In-Reply-To: <06c05c8b-9a3b-4c04-b898-0f82e98da70f@redhat.com> From: Jann Horn Date: Fri, 1 Dec 2023 16:01:33 +0100 Message-ID: Subject: Re: [PATCH] locking: Document that mutex_unlock() is non-atomic To: Waiman Long Cc: Peter Zijlstra , Ingo Molnar , Will Deacon , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.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 (fry.vger.email [0.0.0.0]); Fri, 01 Dec 2023 07:02:26 -0800 (PST) On Fri, Dec 1, 2023 at 1:33=E2=80=AFAM Waiman Long wro= te: > On 11/30/23 15:48, Jann Horn wrote: > > 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. > > My understanding is that this is not safe because mutex_unlock(), in th= e > > MUTEX_FLAG_WAITERS && !MUTEX_FLAG_HANDOFF case, accesses the mutex > > structure after having marked it as unlocked; so mutex_unlock() require= s > > its caller to ensure that the mutex stays alive until mutex_unlock() > > returns. > > > > If MUTEX_FLAG_WAITERS is set and there are real waiters, those waiters > > have to keep the mutex alive, I think; 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. > > > > (With spinlocks, that kind of code pattern is allowed and, from what I > > remember, used in several places in the kernel.) > > > > If my understanding of this is correct, we should probably document thi= s - > > I think such a semantic difference between mutexes and spinlocks is fai= rly > > unintuitive. > > Spinlocks are fair. So doing a lock/unlock sequence will make sure that > all the previously waiting waiters are done with the lock. Para-virtual > spinlocks, however, can be a bit unfair so doing a lock/unlock sequence > may not be enough to guarantee there is no waiter. The same is true for > mutex. Adding a spin_is_locked() or mutex_is_locked() check can make > sure that all the waiters are gone. I think this pattern anyway only works when you're only trying to wait for the current holder of the lock, not tasks that are queued up on the lock as waiters - so a task initially holds a stable reference to some object, then acquires the object's lock, then drops the original reference, and then later drops the lock. You can see an example of such mutex usage (which is explicitly legal with userspace POSIX mutexes, but is forbidden with kernel mutexes) at the bottom of the POSIX manpage for pthread_mutex_destroy() at , in the section "Destroying Mutexes". (I think trying to wait for pending waiters before destroying a mutex wouldn't make sense because if there can still be pending waiters, there can almost always also be tasks that are about to _become_ pending waiters but that haven't called mutex_lock() yet.)