Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp685961rdb; Thu, 30 Nov 2023 15:57:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IG9gXd3yqwqnnDDsT6MSKeAOM50tNA55SZQlTjlSJzoztPXuHzeffWgKjVfHGeNZ7itjX8m X-Received: by 2002:a17:90b:4d0a:b0:27f:bd9e:5a15 with SMTP id mw10-20020a17090b4d0a00b0027fbd9e5a15mr24192015pjb.28.1701388639327; Thu, 30 Nov 2023 15:57:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701388639; cv=none; d=google.com; s=arc-20160816; b=eG+t20+OQsyI9laY4+mD/retucko8T8ns1WhsOy3+H9/QrjTBJYfazD4GKgm3OQHnd 8w5NB2cQ4t4TGBXCxlij8TVTI6VbAB4t0Hz4BXXvIuI17wiih/BPTS1IJvJx850LX9gi EYm9xGEEIa+K7vNkYQOefEdtPuL2bdnBzGBBI+Ha2rxuioYno0SbDlOQ+BxLQVmb31jg f1z85AXZeXjZJTJlJMV3OuJypTpO7Rv0iVUUA1ylqajqAUfwiP8HfY8Szfi/4wYL6Iwa hgLiiYok5r+uF4ZIQpe0rvL2/jMle7Hf8IJv9HUtDSP51nB36QvQoB7ENSXi14lwGGPB Kxww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=isRSmHfA7wl0UpgP4f3nZ8cWSqDYWRF8DVcNYkEZCpE=; fh=mnUAgUxw8AHAmpuahe97Xu+LivddwHlFsXlyfSzaAAA=; b=QAUfehXgX6M1m0ED6l/L2ZHeMxL9raui9ziNcamxPFDKnc+z/blwKLh5Nhsqh36Sgx rVfvM6XtBdfQ1RtdUBhcRzLKiprWFQuS9Kv30j092fRnUwLofH/QEw4oHtamfDHIgEww Rw8chT+HRdNPP2cyaXZtz7bA3MTux1DNtLkTRRk7bPcu8ftHVlMGgs9grxYho7809ZBQ kB4+9klBuH0tN1bmxJL2n8CWTrgtV0Ni4OjkyeLM+PO5EgCPJRUNNVp+H23mZEU5do7I T2NpUTIZzrljcqNJWNno/hGEl6HOoADK6svps5GL6qH8ZbW3bR61TCIRhgJZy+rYhB7g +uSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DUswoKqR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id sk13-20020a17090b2dcd00b002861c962e15si2532926pjb.15.2023.11.30.15.57.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 15:57:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DUswoKqR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id DAA3A806A10D; Thu, 30 Nov 2023 15:57:02 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377326AbjK3X4w (ORCPT + 99 others); Thu, 30 Nov 2023 18:56:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377294AbjK3X4v (ORCPT ); Thu, 30 Nov 2023 18:56:51 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03A9810DF for ; Thu, 30 Nov 2023 15:56:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701388616; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=isRSmHfA7wl0UpgP4f3nZ8cWSqDYWRF8DVcNYkEZCpE=; b=DUswoKqRA+fdxyQTBJ/ORhk9v1ExBy374ToOi+b+a9lxmA2omzcUmZ2Ohdp47QcUZuRIL8 /qPeMLcXDepsr5ZG8qS6gW0babgVM4QhNhDi1+tgRM2jCO3mu+TWds1MdHHh1ZBoZMcK47 9rHTT3yONOyofKtM8yKrdPFqQLySNCg= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-333-y6qr_XQ5O52A4CV1P-MZsQ-1; Thu, 30 Nov 2023 18:56:54 -0500 X-MC-Unique: y6qr_XQ5O52A4CV1P-MZsQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 87785380450D; Thu, 30 Nov 2023 23:56:53 +0000 (UTC) Received: from [10.22.9.192] (unknown [10.22.9.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 234422026D4C; Thu, 30 Nov 2023 23:56:53 +0000 (UTC) Message-ID: <151f2b05-bcb7-4e69-866a-c76286383175@redhat.com> Date: Thu, 30 Nov 2023 18:56:52 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] locking: Document that mutex_unlock() is non-atomic Content-Language: en-US To: Jann Horn Cc: Peter Zijlstra , Ingo Molnar , Will Deacon , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org References: <20231130204817.2031407-1-jannh@google.com> <2f17a9a6-5781-43ef-a09b-f39310843fe6@redhat.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Thu, 30 Nov 2023 15:57:03 -0800 (PST) On 11/30/23 17:24, Jann Horn wrote: > On Thu, Nov 30, 2023 at 10:53 PM 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? > I mean a mutex_lock()+mutex_unlock() sequence. Because of optimistic spinning, a mutex_lock()/mutex_unlock() can succeed even if there are still waiters waiting for the lock. > >> mutex_is_locked() will return true if the mutex has waiter even if it >> is currently free. > I don't understand your point, and maybe I also don't understand what > you mean by "free". Isn't mutex_is_locked() defined such that it only > looks at whether a mutex has an owner, and doesn't look at the waiter > list? What I mean is that the mutex is in an unlocked state ready to be acquired by another locker. mutex_is_locked() considers the state of the mutex as locked if any of the owner flags is set. Beside the mutex_lock()/mutex_unlock() sequence, I will suggest adding a mutex_is_locked() check just to be sure. Cheers, Longman