Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1906568rdb; Sat, 2 Dec 2023 14:39:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IFW2v2XKr8TuzcPhYyWvWqiuMGXuyViSambHqffKxr/FYq5HZb4ssUTivXgiIGc8aIjJ/iA X-Received: by 2002:a17:902:ea08:b0:1cf:f7c3:1e22 with SMTP id s8-20020a170902ea0800b001cff7c31e22mr1902614plg.37.1701556795094; Sat, 02 Dec 2023 14:39:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701556795; cv=none; d=google.com; s=arc-20160816; b=HS7XWLnoADTxebg7uLL8peafoKGiiiPo7sqS4VDt5npYVmWzAng40fZ+qobk+EjkA3 nNTK9c8wc/J4Gds5miiYqBinbXRHLI9XkrBKWY1SaS1+FlV2OfP40p971lh3Htr2xGQy TVZZZm/niwoJcUmZx/4IARD7JdDOBebDmR6ftgoWh3hFgaZHaeIbdM99iH6JIoJvVwoi hm/ZI603ErYbgY31fi+sKy4wl+rIl4xhWLX3sXQ2D2X9EpSd+UeWNxhaYHjUZx5ParOy OBXmlRcOhiBl7m86Z1Ih3Ri5FW7Bigh11J6IW/EDauXNwl6vEIYVWzt2lrSs4E8B1Sem Gdlw== 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=W85mzbNqSUgzZhP20j/vWLOVU90yvilCaj5Tmfd4LAI=; fh=vnp/px2epNpDrIWNhiKEVYdacNA9JNWY/gz0HQAaZZw=; b=fcCb2JAUTia8fHVA3m5g+nvTwMQ3moj7nXIM666Ji3n4j0aZyff+BA/K2XSgontXJ4 76t6ulyLeY0UpOxnQhCMYi/ZpX1pFQ8Eb96a7njjp+WNlNpmaIWqdrIJYBTp5VMZNWKl W0cvUr+ZeDlWcNjhogCqGlxOSqrPREHgKo1p7u3r4RcfYVtHQF7rTc2v5aczJSB1Y0Ga ydk6Vbde/tJ9NTq6Clq8H/sgDuFDKPqvSzeXVCTLbnWQcxFjxTuyxxzYKBq+Ozba5vD0 DL1JDvVwkhwlhXSf4WmFo9kZaIaINBsMX+I1AH1E/cr+hsW1rnbFmgtCa90LrWroHwIN 6WvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ko+ATkd6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id ik26-20020a170902ab1a00b001d060d48fa7si2588969plb.399.2023.12.02.14.39.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 14:39:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ko+ATkd6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id BC71E805E478; Sat, 2 Dec 2023 14:39:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229531AbjLBWji (ORCPT + 99 others); Sat, 2 Dec 2023 17:39:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229450AbjLBWjh (ORCPT ); Sat, 2 Dec 2023 17:39:37 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F183119 for ; Sat, 2 Dec 2023 14:39:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701556783; 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=W85mzbNqSUgzZhP20j/vWLOVU90yvilCaj5Tmfd4LAI=; b=Ko+ATkd6AiRRW5MSIjo1bhFZNDjjeJRKIk+f0qdvOt3gfzq1rL5jZDYzeFCantTwQwWNHK dl0hVbqYcsVHE2uwtFxnmT6+rwUYWk9dSKT0n1p81NCxx/Ss0XHT6JPpjqonUu0/IXhYSA tpb/WF4EwF6AS3w5kwnYWuXJzfifSl4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-332-7ALDFyWlNruah0QHQ1fuyw-1; Sat, 02 Dec 2023 17:39:38 -0500 X-MC-Unique: 7ALDFyWlNruah0QHQ1fuyw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 99F2A185A780; Sat, 2 Dec 2023 22:39:37 +0000 (UTC) Received: from [10.22.8.11] (unknown [10.22.8.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2AF22492BE0; Sat, 2 Dec 2023 22:39:37 +0000 (UTC) Message-ID: Date: Sat, 2 Dec 2023 17:39:36 -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: David Laight , 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> <06c05c8b-9a3b-4c04-b898-0f82e98da70f@redhat.com> <28b147c3d7354d1a8ff0b903da9b54f4@AcuMS.aculab.com> <1bcee696-d751-413c-a2ec-4a8480bae00b@redhat.com> <780e652ff52044d4a213cacbd9276cf8@AcuMS.aculab.com> <501501ef-137a-4797-9d43-48ea68851147@redhat.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 agentk.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 (agentk.vger.email [0.0.0.0]); Sat, 02 Dec 2023 14:39:52 -0800 (PST) On 12/2/23 10:51, David Laight wrote: > From: Waiman Long >> Sent: 01 December 2023 19:16 >> >> On 12/1/23 13:44, David Laight wrote: >>> Pending waiters aren't the problem. >>> >> Pending waiters can still be a problem if code decides to free the lock >> containing object after a lock/unlock sequence as it may cause >> use-after-free. >>> You have to ensure there aren't any, but the mutex() can be held. >>> >> Using reference count to track the number of active users is one way to >> prevent that if you only release the reference count after >> mutex_unlock() returns but not in the lock critical section. > I suspect the documentation need to be more explicit than just saying > it is non-atomic. > Saying something like: > > The mutex structure may be accessed by mutex_unlock() after another > thread has locked and unlocked the mutex. > > So if a reference count is used to ensure a structure remains valid when > a lock is released (with the item being freed when the count becomes zero) > the reference count itself cannot be protected by a mutex in the structure. > So code like: > ... > count = --item->refcount; > mutex_unlock(item->mtx); > if (!count) > free(item); > can lead to a 'use after free' in mutex_unlock(). > However if the refcount is atomic and decremented without the > mutex held there isn't a problem. > > David That is definitely better than saying it is non-atomic which is vague in meaning. Cheers, Longman