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=1796284229; b=XtXyxBRXeFS/UThjtOthraROzbkESu5s1w9nUci75Qr2DWgzcK6XZkHOeUNQiQ6mLgXS t0aSWNVxqm0UiUGi5fIQTvlCtvIZ4ODwlakUwHRyzfw7x6plQRgjeSlAePbGcFQjLzcFe Q81MzRDb8gS9D0mNdgzNDTE7KwfJGGqTFbIDl9Y77RH1oZYdeUHHak0z5x0XHGqVnAG/D y8Pk09bjt7UMcrQ1JiWvUoBiK66Fg3MFumo2OM3VjuJx3jWdShrb3kaX8uuPVHq/iH46/ qdsLuzLGBcRjEympVBQNOvOPFSvRBvhzEXGYnY3yZHmHnL20AoLLs0elZWHce+RaXXw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796284229; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:Content-Language:Organization: In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=gAgI8KgPExqpWVp0OvLcWteaHRNlSr8VmNSy1fKb8tQ=; b=ClL05SS/SnhGQCTtIS7FwBLttiWSo1o6mNDVeNe6HUYPJvPdAprIPDdSSPEOwP/rjd+/ UNzprW552xbfuLdTWMcwaEo8QMz70FH6HnXFSUxIPm00G2w0qe+LdmOw3h6J62E+N7Wgc CqpB0g8DlIh6f9eEw5dPLKK+V/7YYbxv4CFBoK8L9UCD1qhOtG4NY1EumU1+ZvpHu0QLI EDVwabuvcNZgKimFkbSZoHMIJCsIviuu0nB2xPzWwzehRnWWsOxDzN+kpQZPsemqUDFtf Rdj5iXhOqkW5HRkiSkWA1brt9duosD8bsOFb+49sltMj2/EXTqkQBxr4LuNOcWnZ2qw== 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=gefbiAo9; 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 4CA92606A681 for ; Thu, 3 Dec 2026 07:50:29 +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=gefbiAo9 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 39D8A1F00893; Thu, 3 Dec 2026 07:50:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796284229; bh=gAgI8KgPExqpWVp0OvLcWteaHRNlSr8VmNSy1fKb8tQ=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=gefbiAo93wse9h/Y6I84x0i3S0BxcfmeHbnSXNtoiRiDjS2smPVlrXjqNbu6fCWLI yod+n/dlOT4SLjOran4lSOY1vooPOJsxL3CrKmAbPXpo4eUM2wYTN9UilfIQumcukI 1HLf8L9HAv5lCUwma5Wyt0uuaNwSzqKj29Yxi7tYAhDTaLl8K49YIHFgkW4v1y8Wv6 mg8I6K0g3yCDZHKiOtMjsA8Zb9gV7roI6SRB0lWrvC/OtsRA4zzhf4Bv1sLJBol4pb cuhVa7PWoMKySXgp3hpx+l4EcWsziekoQ6bhFPC0TwHat11EbHgwv2Cpabz5x5SE28 GIascfhUkkZAw== Message-ID: <3a07bc8f-5eae-4253-b2a4-7edece984b8d@kernel.org> Date: Thu, 3 Dec 2026 16:50:26 +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: From: Damien Le Moal Content-Language: en-US Organization: Western Digital Research In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 2026/12/03 10:59, Bart Van Assche wrote: > The bio_list, flags, wp_offset and cond members of struct > blk_zone_wplug are protected by the zone write plug spinlock. Annotate > these members with __guarded_by() such that Clang's context analysis > can verify that the lock is held when these members are accessed. > > Add __must_hold() annotations to disk_zone_wplug_is_full() and > disk_zone_wplug_is_offline_or_readonly() since all callers hold the > zone write plug lock. Use scoped_guard(spinlock_init, ...) to > initialize the guarded members of a newly allocated zone write plug. > Use context_unsafe() for setting the zone condition before the zone > write plug is published in the hash table and for the accesses in > disk_free_zone_wplug(), which runs when the last reference has been > dropped and may be called either with or without zwplug->lock held. > > Signed-off-by: Bart Van Assche > --- > block/blk-zoned.c | 49 +++++++++++++++++++++++++++++++---------------- > 1 file changed, 32 insertions(+), 17 deletions(-) > > diff --git a/block/blk-zoned.c b/block/blk-zoned.c > index f19ad4816d12..8f8fb88fa7d8 100644 > --- a/block/blk-zoned.c > +++ b/block/blk-zoned.c > @@ -165,16 +165,16 @@ static inline bool blk_zstate_is_conv(enum blk_zstate zs) > struct blk_zone_wplug { > struct hlist_node node; > struct list_head entry; > - struct bio_list bio_list; > + struct bio_list bio_list __guarded_by(&lock); > struct work_struct bio_work; > struct rcu_head rcu_head; > struct gendisk *disk; > spinlock_t lock; > refcount_t ref; > - unsigned int flags; > + unsigned int flags __guarded_by(&lock); > unsigned int zone_no; > - unsigned int wp_offset; > - enum blk_zone_cond cond; > + unsigned int wp_offset __guarded_by(&lock); > + enum blk_zone_cond cond __guarded_by(&lock); > }; > > static inline bool disk_need_zone_resources(struct gendisk *disk) > @@ -633,6 +633,7 @@ static bool disk_zone_is_last(struct gendisk *disk, struct blk_zone *zone) > > static bool disk_zone_wplug_is_full(struct gendisk *disk, > struct blk_zone_wplug *zwplug) > + __must_hold(&zwplug->lock) > { > if (zwplug->zone_no < disk->nr_zones - 1) > return zwplug->wp_offset >= disk->zone_capacity; > @@ -640,6 +641,7 @@ static bool disk_zone_wplug_is_full(struct gendisk *disk, > } > > static bool disk_zone_wplug_is_offline_or_readonly(struct blk_zone_wplug *zwplug) > + __must_hold(&zwplug->lock) > { > return disk_zone_cond_is_offline_or_readonly(zwplug->cond); > } > @@ -675,11 +677,10 @@ static bool disk_insert_zone_wplug(struct gendisk *disk, > */ > zones_state = rcu_dereference_check(disk->zones_state, > lockdep_is_held(&disk->zone_wplugs_hash_lock)); > - if (zones_state) > - zwplug->cond = > - blk_zstate_to_zone_cond(zones_state[zwplug->zone_no]); > - else > - zwplug->cond = BLK_ZONE_COND_ACTIVE; > + /* Not yet published in zone_wplugs_hash, so zwplug->lock is unneeded. */ > + context_unsafe(zwplug->cond = zones_state ? > + blk_zstate_to_zone_cond(zones_state[zwplug->zone_no]) : > + BLK_ZONE_COND_ACTIVE); > > hlist_add_head_rcu(&zwplug->node, &disk->zone_wplugs_hash[idx]); > atomic_inc(&disk->nr_zone_wplugs); > @@ -751,15 +752,27 @@ static void disk_free_zone_wplug(struct blk_zone_wplug *zwplug) > { > struct gendisk *disk = zwplug->disk; > unsigned long flags; > + unsigned int zwp_flags; > + bool empty; > > - WARN_ON_ONCE(!(zwplug->flags & BLK_ZONE_WPLUG_DEAD)); > - WARN_ON_ONCE(zwplug->flags & BLK_ZONE_WPLUG_PLUGGED); > - WARN_ON_ONCE(!bio_list_empty(&zwplug->bio_list)); > + /* > + * 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? This entire annotation thing makes the code really ugly... > + zwp_flags = context_unsafe(zwplug->flags); > + /* Same as above: zwplug->lock may or may not be held. */ > + empty = context_unsafe(bio_list_empty(&zwplug->bio_list)); > + WARN_ON_ONCE(!(zwp_flags & BLK_ZONE_WPLUG_DEAD)); > + WARN_ON_ONCE(zwp_flags & BLK_ZONE_WPLUG_PLUGGED); > + WARN_ON_ONCE(!empty); > > spin_lock_irqsave(&disk->zone_wplugs_hash_lock, flags); > blk_zstate_set(rcu_dereference_check(disk->zones_state, > lockdep_is_held(&disk->zone_wplugs_hash_lock)), > - disk->nr_zones, zwplug->zone_no, zwplug->cond, 0); > + disk->nr_zones, zwplug->zone_no, > + /* May be called with or without zwplug->lock held. */ > + context_unsafe(zwplug->cond), 0); > hlist_del_init_rcu(&zwplug->node); > atomic_dec(&disk->nr_zone_wplugs); > spin_unlock_irqrestore(&disk->zone_wplugs_hash_lock, flags); > @@ -907,11 +920,13 @@ static struct blk_zone_wplug *disk_get_or_alloc_zone_wplug(struct gendisk *disk, > > INIT_HLIST_NODE(&zwplug->node); > refcount_set(&zwplug->ref, 2); > - spin_lock_init(&zwplug->lock); > - zwplug->flags = 0; > + scoped_guard(spinlock_init, &zwplug->lock) { > + zwplug->flags = 0; > + zwplug->wp_offset = > + bdev_offset_from_zone_start(disk->part0, sector); > + bio_list_init(&zwplug->bio_list); > + } > zwplug->zone_no = zno; > - zwplug->wp_offset = bdev_offset_from_zone_start(disk->part0, sector); > - bio_list_init(&zwplug->bio_list); > INIT_WORK(&zwplug->bio_work, blk_zone_wplug_bio_work); > INIT_LIST_HEAD(&zwplug->entry); > zwplug->disk = disk; -- Damien Le Moal Western Digital Research