From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796371281; b=sGfz1nza7OJ+udrHOzdP7e/kg9fJX8kD3F/VdmZyrjtheVVilKlDnRiRnDoCrDETZ+UB HJFgzuKS2nXbUXEqy8r4y5CrTf7pwXPdOm+ISo5J2L60mCigPHkce0/i/kTpybqdwA6Jl 0mg0MFsqWmk7hLWwXcIBm7QXxfFKPlVEBl8MpzBC98PzHrXpBr7USEdJDw9XfAXhj5VsA y0j+JIQnM8ati78O3nO9brxJEjs4ubCzAbM8F/v9qozzescBRQ5KVSJivX9ue2yT3hH9n DTIb/Jm86mTxeeyG8PUytRdMaCxFOBbKdFnMcvgWkIuSEKUmgv6Z7XAoHsoe4al5xJg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796371281; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:Content-Language:From:Organization: In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=BofONh/JrIwFMsRD/fOUgH4ygRRyVxHbKfUf8uEEiTY=; b=q4ywcms0M2LkFIwOlhp96safNjjVsVf1oZf+chYuM6GJgwuyUW8nTn1nvri8bx7mT5o5 x/fGqovk9k8f+EuhYqYrR0+k5QojSYzAPgb+De0vj2nT3Y/ubudsRuQ12Okobgw75vQXy 5FeVIbn3vjaLh9ogMgZ0IV3PUuZtYqMhmrpcP3vJM/rB7ZEyKIrvTyFQ0c/LVjwkAMlk7 +JDbdva6hsnDC7LfH43FqN84qghMIY/Ii+Un3SivteeCiPRRrkJcMmNuimQHkKC/L4/gF e+A7vsBGnTKP5qSe5hFijqEoFCYkGX00TrWVfmX1cxnYYJE7MWO9TxmFE9/4/ZlNTwg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=IPiXG6TL; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BC25606A322 for ; Fri, 4 Dec 2026 08:01:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=IPiXG6TL Received: by smtp.kernel.org (Postfix) with ESMTPSA id 10A781F000C2; Fri, 4 Dec 2026 08:01:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796371280; bh=BofONh/JrIwFMsRD/fOUgH4ygRRyVxHbKfUf8uEEiTY=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=IPiXG6TLYWqAY5xO3E6/bTLnX8huAg4IFbgZk60bgw67y2FV956H7lqrptcNgtqFr dkIsNLDmM6jkEhM3fUbEJJPynuLHUU8EC0KsA6iT7w2F/1fjrYz+UHQfozIT0b1U1P QwERYlokEr+ijWHEd4fiAL92rsU+J1d+6KMfqTbN7U1NPiGCAR50i3CUjGcoo/s9dJ M6AO1w0AIwSleXIxnRfZAWZXhbMmiUeiOLQkkH8e/ij1vZKDwR0Olu3hUBabm4fPpJ H/7pXyiQf5s5SMJPnCRUpjXN4AcpYuBWq/j+OlhjZAwVUkRQivgkvdqyNSz8fNqgW5 OvmQjw4U7k0mw== Message-ID: <46091c59-5f4f-4821-8b71-170a340036cc@kernel.org> Date: Fri, 4 Dec 2026 17:01:18 +0900 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: Bart Van Assche , Jens Axboe Cc: linux-block@vger.kernel.org, Christoph Hellwig , Nathan Chancellor References: <3a07bc8f-5eae-4253-b2a4-7edece984b8d@kernel.org> <01c5ce64-3dc8-4b7f-b3f8-df55107d9545@acm.org> Content-Language: en-US From: Damien Le Moal Organization: Western Digital Research In-Reply-To: <01c5ce64-3dc8-4b7f-b3f8-df55107d9545@acm.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 12/4/26 02:40, Bart Van Assche wrote: > 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() This one is removed with: https://lore.kernel.org/linux-block/20261126-feat-blk-zoned-rcu-barrier-v4-2-a4ab1d2162e0@linux.dev/ But it looks like Jens has not picked up this series yet. The other 2 cases are indeed good catches. > 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. Yes, agree, this is not great. > 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. As mentioned, there is a patch pending on the list that gets rid of the problem in disk_destroy_zone_wplugs_hash_table(). But for disk_for_all_zone_wplugs(), we indeed need to call the actor function around a plug get/put to avoid dropping the last reference while holding the plug spinlock. > 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. Yes, please feel free to send the correction patch. But please split this giant series into manageable pieces. E.g. one series for block scheulers, one for zones, one for each block driver, and one for the core. That will be a lot easier to review and add fixes as needed. -- Damien Le Moal Western Digital Research