Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=199.89.1.16 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796319627; b=q9hVGGPpwizkgwAzTuKlyvtLM4EBpvrdqtvP+xH1Yp0ciChVtN4Kk3o7MgA7I6LrNbj0 hTt3hLRvqSmB+OJshjVOltsl3GZrATNl3g6OCD90fx7bRdGCd8X5zM/W3gZACyi7t1gSA PC40O2nLf8AV5bUfxSc00zZiDAYpheDUuuX+r4Tc/TM42zomdsk6qPlUKx6+eKIlqhBb+ Oh+w3nkw1ZYcZeAKjWxQUVA2wtywfNPgmcvNIkLFhGupDcYxqeuwCC0XIE9Ac9aObRIer cb+W29bjBmIN7wXz3VqlDS5KenhQmM5rOT4dteS9M5BxNLicNAcYhKzbymSh/b4coFA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796319627; h=DMARC-Filter:Received:DKIM-Signature:X-Virus-Scanned:Received: Received:Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc: References:Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=jSArvkL/vD0HKexAg3K/QTe6gry7d++WVGJHGOYDyoE=; b=2s2D4KjCAE+uif47YdvCNkqeqXMvINRBkWiGAmhNgyPjyUTAecHxRSqFAalf7QdzzptM PAAyaBpUJIQB0ueBoST7OMDB9AI9uttnsZzDNduo4CnA7y2akR9B0zXugUV+awbTdslax ppPhBMCzHRU905QpUVTRbi//Fmvk5v7ihYdwBzbs7Qh83277lDZLZlJ0U0JsCjA4mli03 9BCstf/UeWARF0SBbsKkvEQgIPgbiHaOQXHgGLREbi118Gmg6lu9AlS9oha7M0fMjt8R4 EJ/y3TZPo2j+keiARr0WfhPss2d+sL/JhUbp/3OSavSbm1Frycri49NtqQN5003a/cw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=acm.org; spf=pass smtp.mailfrom=acm.org; dkim=pass header.d=acm.org header.i=@acm.org header.a=rsa-sha256 header.s=mr01 header.b=NZRZ4aOy; arc=none smtp.remote-ip=199.89.1.16 Received: from 013.lax.mailroute.net (013.lax.mailroute.net [199.89.1.16]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 47341606A322 for ; Thu, 3 Dec 2026 17:40:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 47341606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=acm.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=acm.org header.i=@acm.org header.a=rsa-sha256 header.s=mr01 header.b=NZRZ4aOy Received: from localhost (localhost [127.0.0.1]) by 013.lax.mailroute.net (Postfix) with ESMTP id 4jgMNB5V3czlfvqD; Thu, 3 Dec 2026 17:40:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=acm.org; h= content-transfer-encoding:content-type:content-type:in-reply-to :from:from:content-language:references:subject:subject :user-agent:mime-version:date:date:message-id:received:received; s=mr01; t=1796319623; x=1798911624; bh=jSArvkL/vD0HKexAg3K/QTe6 gry7d++WVGJHGOYDyoE=; b=NZRZ4aOyZwtQu6MjaJiepLwVnDA/2QQ5j6t3fIyg gbFq7n6HIU2IHwmOWVL5TQCCGHUTUYYo1fWHxau2JgJ1FSxf/zrzu/qWYdnWek4A bVobKue01zfJVoM8qEJnRgrOr0U18smKvEsVYr/QxDcwK0GDJ2CKCOHjR5O5ZhJJ biWQasy6aF73/15gsAnGaAsv03+G3eR1GX+vHzFPpYxxogqt+tDt8cIwfOD+Pxp0 OR8MsAxs62v/+UftDqtvUaiOP2HGOa9CClATLmkiISvAa4xuqnfZIrX8B41pX1PJ uuPLRK+PkIFzlJkKGJBFxWjijRbn4WWFvciTYQhqn+kGmA== X-Virus-Scanned: by MailRoute Received: from 013.lax.mailroute.net ([127.0.0.1]) by localhost (013.lax [127.0.0.1]) (mroute_mailscanner, port 10029) with LMTP id AyJlH3QxJ3Fa; Thu, 3 Dec 2026 17:40:23 +0000 (UTC) Received: from [100.77.36.205] (unknown [104.135.183.85]) (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) (Authenticated sender: bvanassche@acm.org) by 013.lax.mailroute.net (Postfix) with ESMTPSA id 4jgMN742Lvzlfvq2; Thu, 3 Dec 2026 17:40:23 +0000 (UTC) Message-ID: <01c5ce64-3dc8-4b7f-b3f8-df55107d9545@acm.org> Date: Thu, 3 Dec 2026 09:40:22 -0800 X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 30/52] blk-zoned: Annotate struct blk_zone_wplug members with __guarded_by() To: Damien Le Moal , Jens Axboe Cc: linux-block@vger.kernel.org, Christoph Hellwig , Nathan Chancellor References: <3a07bc8f-5eae-4253-b2a4-7edece984b8d@kernel.org> Content-Language: en-US From: Bart Van Assche In-Reply-To: <3a07bc8f-5eae-4253-b2a4-7edece984b8d@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 12/2/26 11:50 PM, Damien Le Moal wrote: > On 2026/12/03 10:59, Bart Van Assche wrote: >> + /* >> + * The last reference has been dropped and disk_free_zone_wplug() may >> + * be called either with or without zwplug->lock held, so zwplug->lock >> + * cannot be acquired here. >> + */ > > Calling disk_free_zone_wplug() with the zwplug spinlock still held would be a > really big problem since that potentially could result in a use after free. So > what path are you talking about here? Hi Damien, There are three code paths in which a zone write plug reference is dropped with zwplug->lock held by a caller that does not hold a zone write plug reference itself. If the dropped reference is the last one, disk_free_zone_wplug() is called with zwplug->lock held: (1) Dropping all zone write plugs during revalidation: disk_revalidate_zones(disk, true) or disk_revalidate_capacity() disk_for_all_zone_wplugs(disk, disk_drop_zone_wplug, NULL) disk_drop_zone_wplug() spin_lock_irqsave(&zwplug->lock, flags) disk_zone_wplug_abort() blk_zone_wplug_bio_io_error() disk_put_zone_wplug() disk_mark_zone_wplug_dead() disk_put_zone_wplug() disk_free_zone_wplug() (2) Completion of a REQ_OP_ZONE_RESET_ALL operation: blk_zone_mgmt_bio_endio() blk_zone_reset_all_bio_endio() disk_for_all_zone_wplugs(disk, disk_zone_wplug_reset_wp, NULL) disk_zone_wplug_reset_wp() spin_lock_irqsave(&zwplug->lock, flags) disk_zone_wplug_set_wp_offset(zwplug->disk, zwplug, 0) disk_mark_zone_wplug_dead() disk_put_zone_wplug() disk_free_zone_wplug() (3) Releasing a disk: disk_release() disk_release_zone_resources() disk_destroy_zone_wplugs_hash_table() spin_lock_irq(&zwplug->lock) disk_mark_zone_wplug_dead() disk_put_zone_wplug() disk_free_zone_wplug() disk_for_all_zone_wplugs() does not take a zone write plug reference, and for an idle zone write plug the initial reference is the only reference. Hence, in (1) and (2), disk_mark_zone_wplug_dead() drops the last reference for every idle zone write plug. In (3), the while loop even relies on disk_free_zone_wplug() removing the zone write plug from the hash table. I don't think that this results in a use-after-free today because disk_free_zone_wplug() frees the zone write plug with call_rcu() and because the spin_unlock() call that follows happens before the RCU grace period ends: (1) and (2) run inside the rcu_read_lock() section of disk_for_all_zone_wplugs(), and in all three cases interrupts are disabled while zwplug->lock is held. But I agree that it is fragile. How about making disk_for_all_zone_wplugs() and disk_destroy_zone_wplugs_hash_table() hold a zone write plug reference while zwplug->lock is held, and dropping that reference after zwplug->lock has been released? With that change, all code paths that drop a reference while holding zwplug->lock hold another reference, so these paths can use refcount_dec() instead of disk_put_zone_wplug(). disk_free_zone_wplug() and disk_put_zone_wplug() can then be annotated with __must_not_hold(&zwplug->lock) such that the compiler verifies that disk_free_zone_wplug() is never called with zwplug->lock held. I have a patch that implements this and can include it in v2, just before this patch. > This entire annotation thing makes the code really ugly... With the above change, the comments in disk_free_zone_wplug() can be simplified into "the last reference has been dropped so no other context can access *zwplug". I will also try to minimize the number of context_unsafe() calls in v2. In my opinion the remaining annotations are worth it since they let the compiler catch locking bugs, e.g. calling disk_free_zone_wplug() with zwplug->lock held. Thanks, Bart.