Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1250511rdb; Fri, 1 Dec 2023 10:40:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IFDRYCq97y582aK5NOXxsjISdjfXkzWstxztvKPSNkYBmScxhxhxvav+s8TlNr+sdUDnPNT X-Received: by 2002:a05:6a20:4388:b0:187:78ef:e3f1 with SMTP id i8-20020a056a20438800b0018778efe3f1mr35922446pzl.20.1701456056108; Fri, 01 Dec 2023 10:40:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701456056; cv=none; d=google.com; s=arc-20160816; b=kakkvSU1baFMPHL7XCNcupRz1bsg7fuPyzlUMidet2Z9vj5idqUYrJpt1QA+o6bV9Q NeCAx6aAVRMIEW3qIJkpnMEtZ/mFx5ljGHBt23AUDoBj7eb6iamil0sgj8ra6w1qWHfW 3+lnV6H0TKYjB1DWew2RGb3nDQsMKxoEAwoHfYTJMe41fMAUNLT9zx7hB20uVz/4tS8N dP/ITCUQd9rV5e9CkQu2NL+rYunM05Eua6cQaznoODioogwi/WReYBwK1aE3x60tnzpK lFMPMAcFEQl00TGzHlwJ+1bKFiY+cY+skdVr2nO8O0pITZwPu0EY4CH4DDuIUVP/bKMo BGlA== 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=WkVHqNvonNn8A5RdXCNM0KGca3KT1K9/6epU/Ala7ns=; fh=Qm+brRYt3Hbp+YaYGw1m8ZipY2nkzJc9MkyaLbt5Avk=; b=p9ksq3+SifPmEBq93aHlFUaThSi//eUQCWYlGRpnt9Q3L7IP9fyO0n+0F3yWaL64aC no34Z5JleJg5c0Cf8RwCepEbB3y2Dt0Ra9ETjE6irU+bMoVF5XqM8Yf8eM87Z1X/Le4R hMOxAnUY0+w8TbWRFjaIi7to3dZQjTXC0BmCNo3XVFHggRekxO00RP7SE56avEPfVslr wa7SmYv29OjpyT5NC56rvyMn9AjxOlCjF23sFokyOHErB4A/g5aBkgaj3yaZYSRUOBzt p7BmFbPaKpMNqbkhre8/H8t5lFSkLDpzWgjF6nJPC+vZ6q/eQ4cAqJq2jDCFuhwE9fDc Dj9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gJh5Bp2G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id ch4-20020a056a0208c400b005be03f0da78si4072646pgb.157.2023.12.01.10.40.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 10:40:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gJh5Bp2G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id 9248E827A71F; Fri, 1 Dec 2023 10:40:53 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230207AbjLASkj (ORCPT + 99 others); Fri, 1 Dec 2023 13:40:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229468AbjLASki (ORCPT ); Fri, 1 Dec 2023 13:40:38 -0500 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 755A3D63 for ; Fri, 1 Dec 2023 10:40:44 -0800 (PST) Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-54c77d011acso1070a12.1 for ; Fri, 01 Dec 2023 10:40:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701456043; x=1702060843; 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=WkVHqNvonNn8A5RdXCNM0KGca3KT1K9/6epU/Ala7ns=; b=gJh5Bp2Gq+zevHlFu7FzvXMLs/Dr6P8fJ86RAC/FgyO8UzEXlxo3OY+HPn29N9cUDM I5Gryuusox6WflgyVQ92N7v2DHj1Xg6utmR6NCKn0gr353JVS7t5I6UBgQnZDib4o1Fi fMPGeHpCxOq1adthQw/URIRsEupe3BqbGUJhyFNUcqI//xtjXDd/HF2Fe8Odqj6xxe1m uhUVMsUugsdltseN4pY3aMz87z1t2QpRo03OYfAe9cs6KlWRZzgBPNLQrVAou9DIRAuy YYsFXM3CkHHGuszmOII0pAqWD/UJAVV1YwcoVtgxy4MU1dAL42q9YBZ39ZzhH+2f2nRR jx7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701456043; x=1702060843; 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=WkVHqNvonNn8A5RdXCNM0KGca3KT1K9/6epU/Ala7ns=; b=en0AOm2p/A1mwwLZwXHfP7UlXyQ25kwM1mqO7Fgx39CX5PSs9vGeyNF9uF34y90GrI j/TFG9X4ZeDx3gxtPpm7iKvwVUOGk+0pfdNfhnpqU0zEV6xWJ9YSQyrPsBoxVc1qa9yS e1Ze9GPAW+cywog0b+iGcHfX8dGPzPFoUkAJklVjW/xCv6/jn53Pcbat7NEJxSDVZ19M ajgeTV9HddHvcxmPZRPpd3KS/J79+p3cj0MGkhMN0BnkOrDl19S9dzy0waKK7UlCQNa0 TBFLLiFZTlJWgjOzYPllrnY8QY3IV/lka8zJji6iKjY7eLRopeI/LR4BlvUuElrfC6fB 2ZDw== X-Gm-Message-State: AOJu0YyqrcczOFh5MAWcT5ZxGc7uw84SW7iSxg8ACn/gR5mGQBFRAM5I VEYnZN964Yfs2aZ94tkbZ/OdG4m2fPreMun/Q0jp/g== X-Received: by 2002:a50:aacf:0:b0:54b:321:ef1a with SMTP id r15-20020a50aacf000000b0054b0321ef1amr160337edc.6.1701456042587; Fri, 01 Dec 2023 10:40:42 -0800 (PST) MIME-Version: 1.0 References: <811a97651e144b83a35fd7eb713aeeae@AcuMS.aculab.com> In-Reply-To: <811a97651e144b83a35fd7eb713aeeae@AcuMS.aculab.com> From: Jann Horn Date: Fri, 1 Dec 2023 19:40:06 +0100 Message-ID: Subject: mutex/spinlock semantics [was: Re: io_uring: incorrect assumption about mutex behavior on unlock?] To: David Laight Cc: Jens Axboe , Pavel Begunkov , io-uring , kernel list , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Thomas Gleixner 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 groat.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 (groat.vger.email [0.0.0.0]); Fri, 01 Dec 2023 10:40:53 -0800 (PST) On Fri, Dec 1, 2023 at 7:30=E2=80=AFPM David Laight wrote: > > From: Jann Horn > > Sent: 01 December 2023 16:41 > > > > mutex_unlock() has a different API contract compared to spin_unlock(). > > spin_unlock() can be used to release ownership of an object, so that > > as soon as the spinlock is unlocked, another task is allowed to free > > the object containing the spinlock. > > mutex_unlock() does not support this kind of usage: The caller of > > mutex_unlock() must ensure that the mutex stays alive until > > mutex_unlock() has returned. > > The problem sequence might be: > Thread A Thread B > mutex_lock() > code to stop mutex being requested > ... > mutex_lock() - sleeps > mutex_unlock()... > Waiters woken... > isr and/or pre-empted > - wakes up > mutex_unlock() > free() > ... more kernel code access the mutex > BOOOM > > What happens in a PREEMPT_RT kernel where most of the spin_unlock() > get replaced by mutex_unlock(). > Seems like they can potentially access a freed mutex? RT spinlocks don't use mutexes, they use rtmutexes, and I think those explicitly support this usecase. See the call path: spin_unlock -> rt_spin_unlock -> rt_mutex_slowunlock rt_mutex_slowunlock() has a comment, added in commit 27e35715df54 ("rtmutex: Plug slow unlock race"): * We must be careful here if the fast path is enabled. If we * have no waiters queued we cannot set owner to NULL here * because of: * * foo->lock->owner =3D NULL; * rtmutex_lock(foo->lock); <- fast path * free =3D atomic_dec_and_test(foo->refcnt); * rtmutex_unlock(foo->lock); <- fast path * if (free) * kfree(foo); * raw_spin_unlock(foo->lock->wait_lock); That commit also explicitly refers to wanting to support this pattern with spin_unlock() in the commit message.