Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp928248rdb; Fri, 1 Dec 2023 02:21:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IH4b5ttzf6L7+ot/mrxNQxvqJLGubVMgcZaj02qH27rune1TPSSq8TCvjrXL4OiCLiacK6q X-Received: by 2002:a17:902:e881:b0:1cf:cf34:d4cf with SMTP id w1-20020a170902e88100b001cfcf34d4cfmr17455924plg.18.1701426107694; Fri, 01 Dec 2023 02:21:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701426107; cv=none; d=google.com; s=arc-20160816; b=qPvtFQmyqO/stCwhGAxRiMltrYH0sdqWjwRcBrgP1xza3pi7K2QKBmyo5TQq+zQ2RX ODeyXC2+cb3fFPFfXTTEXsJuo7BcztFVmwvRj58/e6wOFYpnWP6H7IfLLEQBcxJ8ktqm y8YSTORuxDjDyBBAmCeTXK8lxd/+8DUqns+eFSHcPn+tIA6nNttueCaXnIMkRIWalNDx cvT91A+vA5T//Pui/cuLBGwRyWVveCmbzoUK7b3EQ/HjOfk2cxOdnZ50o6k8ACF2Vvvb 1n5codudF2VDZ9gaSA9hNR2aYTH0oU45bHKlK4R2wsfmBOzKHgQ+gjKZ9hxNNIZAL0wG Jciw== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:sender:dkim-signature; bh=bcLd+/AJDrWXWUBdneBB4ElCvmlFWT+bhpoV46xZoYY=; fh=0FV2Tt1b5rI4l4KVg2G6F9yrY5oKypsRtfqVJPPlD38=; b=AJj+5URLmnPHo0gRYUooVqiiVVR98+J+j8CV4yMurCS/G0NPVM2sEEjhgWBPg0+Zsb pr2RriLsIAcN2nPiXhMSSZoIhbu1UCr8fJRKxTWhwU3BqhTgymyzoRQ76ZCTY1/TEXYw GCP76z7N0Jf9RB+fTPJMLlCdPgfj+tEILFODUdulh4C+pIC1DtcrUq3GXZp7aixk/4oT dJHHATY8wJRc0AKfd6VLq8fHWkpPvNGF7nkaJKfbvHu/DUnbcp4HAyDedjHmgxEatM0y URp3DnEOvyD6llt0UUayEMeaJj0XsrZCxIqNMH8L8DhkSZdzmXYIDttj+Vs3sDNQ2Dzx RQLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=IFDgkbi8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id f18-20020a170902e99200b001cfd0fe5125si968700plb.290.2023.12.01.02.21.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 02:21:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=IFDgkbi8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 4CA248087519; Fri, 1 Dec 2023 02:21:08 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378227AbjLAKUt (ORCPT + 99 others); Fri, 1 Dec 2023 05:20:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378208AbjLAKUs (ORCPT ); Fri, 1 Dec 2023 05:20:48 -0500 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACF12DC; Fri, 1 Dec 2023 02:20:54 -0800 (PST) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-40b479ec4a3so17272955e9.2; Fri, 01 Dec 2023 02:20:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701426053; x=1702030853; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:sender :from:to:cc:subject:date:message-id:reply-to; bh=bcLd+/AJDrWXWUBdneBB4ElCvmlFWT+bhpoV46xZoYY=; b=IFDgkbi899dkUMdmFQXO0XXe+bgztgzxvLd+Ec0EQLQoj8zQrEZ+hqk9q7zn0UsS/8 Vfy+MjD342rXKr7dL8rZ3m7RrUIEpdbUf/ubCC6v6yKn7TYWVb+WhRRRNbsa/9vdFtB2 wgtbzVF2gmvkOzObSsN9mqw1vfHkVbIFsZdJgVQDVbNb0TPvaK03x80WaRvRAWPMuIEE wRC1UtAFzLurwpTE6CBQdQ2kd4yxtUdmm3MPAQImFeNtIHZv64/FDZ9rPbfSOd+4bdtQ 48aG2+VxyvGrLvbkwD0PtgmD96x626nGS2wqRcwpiDQWIl11RGbrsVlKEo60VK/IURIZ d0uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701426053; x=1702030853; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:sender :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bcLd+/AJDrWXWUBdneBB4ElCvmlFWT+bhpoV46xZoYY=; b=Wl5c4lNKSl9Ty35PKsMOpp/MMLza1hxzvHNK0WOTEuyKOd5KNibIdr4NKjy/ikfUxb khsBZvuU1CstM7Ghcym1d5XQEAlmXqsn4XizO+YSsFGDIjhBlPmofxGfywQ4dD6xe/wj 5DGzFw2VU6UDfenXmogGPDslSYP4N0u2eBux5wanjEHvQquSN+FHI4Ya3YfnjWaogJYY by4xrOleQLJym42859Qyj4iZkagWjUcWOanUVYGKkBhqFxZ8qYLqSAd/BS4hsgQ82JdT qEL25bh3NHF1s8ulxz0/75V1Wwd6+dCs6fMlFTFFyAWDUeTi9kA/LCqAxtgjA5KRpGDp Fctg== X-Gm-Message-State: AOJu0Yw9OpVCXbFFqD/3YBu63SkalNjPn72g5PwBKK1fWCW5yHUAzENg zTpax5GIO52CA0Jph5OxI4bMlnhJZ5s= X-Received: by 2002:a05:600c:3584:b0:40b:5e22:954 with SMTP id p4-20020a05600c358400b0040b5e220954mr427812wmq.67.1701426052971; Fri, 01 Dec 2023 02:20:52 -0800 (PST) Received: from gmail.com (1F2EF126.nat.pool.telekom.hu. [31.46.241.38]) by smtp.gmail.com with ESMTPSA id fk14-20020a05600c0cce00b0040b2a52ecaasm8715622wmb.2.2023.12.01.02.20.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 02:20:52 -0800 (PST) Sender: Ingo Molnar Date: Fri, 1 Dec 2023 11:20:50 +0100 From: Ingo Molnar To: Waiman Long Cc: Jann Horn , Peter Zijlstra , Ingo Molnar , Will Deacon , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: Re: [PATCH] locking: 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: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <2f17a9a6-5781-43ef-a09b-f39310843fe6@redhat.com> X-Spam-Status: No, score=0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, FSL_HELO_FAKE,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Fri, 01 Dec 2023 02:21:08 -0800 (PST) * Waiman Long wrote: > 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 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. > > > > 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. > > Could you clarify under what condition a concurrent task can decide to free > the object holding the mutex? Is it !mutex_is_locked() or after a > mutex_lock()/mutex_unlock sequence? > > mutex_is_locked() will return true if the mutex has waiter even if it? is > currently free. I believe the correct condition is what the changelog already says: "until mutex_unlock() returns". What happens within mutex_unlock() is kernel implementation specific and once a caller has called mutex_unlock(), the mutex must remain alive until it returns. No other call can substitute for this: neither mutex_is_locked(), nor some sort of mutex_lock()+mutex_unlock() sequence. Thanks, Ingo