All of lore.kernel.org
 help / color / mirror / Atom feed
From: Damien Le Moal <dlemoal@kernel.org>
To: Bart Van Assche <bvanassche@acm.org>, Jens Axboe <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org,
	Christoph Hellwig <hch@infradead.org>,
	Nathan Chancellor <nathan@kernel.org>
Subject: Re: [PATCH 30/52] blk-zoned: Annotate struct blk_zone_wplug members with __guarded_by()
Date: Thu, 3 Dec 2026 16:50:26 +0900	[thread overview]
Message-ID: <3a07bc8f-5eae-4253-b2a4-7edece984b8d@kernel.org> (raw)
In-Reply-To: <e000f48dd1e486d8a5d1176087cf7c8780c550ba.1796261587.git.bvanassche@acm.org>

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 <bvanassche@acm.org>
> ---
>  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

  reply	other threads:[~2026-12-03  7:50 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  1:59 [PATCH 00/52] block: Add lock context annotations Bart Van Assche
2026-12-03  1:59 ` [PATCH 01/52] mq-deadline: Hold dd->lock in deadline_##name##_next_rq_show() Bart Van Assche
2026-12-03  1:59 ` [PATCH 02/52] mq-deadline: Access per_prio state in dd_exit_sched() with dd->lock held Bart Van Assche
2026-12-03  1:59 ` [PATCH 03/52] mq-deadline: Compute &dd->per_prio[prio] " Bart Van Assche
2026-12-03  1:59 ` [PATCH 04/52] mq-deadline: Mark the racy debugfs batching and starved reads Bart Van Assche
2026-12-03  1:59 ` [PATCH 05/52] mq-deadline: Add lock context annotations Bart Van Assche
2026-12-03  1:59 ` [PATCH 06/52] mq-deadline: Add __guarded_by() annotations Bart Van Assche
2026-12-03  1:59 ` [PATCH 07/52] badblocks: Add lock context annotations for bb->lock Bart Van Assche
2026-12-03  1:59 ` [PATCH 08/52] block, bfq: Replace an unreachable branch in bfq_exit_icq() Bart Van Assche
2026-12-03  1:59 ` [PATCH 09/52] block, bfq: Fold _bfq_exit_icq() into bfq_exit_icq() Bart Van Assche
2026-12-03  1:59 ` [PATCH 10/52] block, bfq: Pass bfqd to functions that are called with bfqd->lock held Bart Van Assche
2026-12-03  1:59 ` [PATCH 11/52] block, bfq: Add lock context annotations Bart Van Assche
2026-12-03  1:59 ` [PATCH 12/52] blk-cgroup: Pass q to blkg_create() and blkg_lookup_create() Bart Van Assche
2026-12-03  1:59 ` [PATCH 13/52] blk-cgroup: Add lock context annotations Bart Van Assche
2026-12-03  1:59 ` [PATCH 14/52] blk-cgroup: Annotate all_blkcgs with LIST_HEAD_GUARDED() Bart Van Assche
2026-12-03  1:59 ` [PATCH 15/52] blk-crypto: Add lock context annotations Bart Van Assche
2026-12-03  1:59 ` [PATCH 16/52] block: Add __must_hold() annotations for fq->mq_flush_lock Bart Van Assche
2026-12-03  1:59 ` [PATCH 17/52] block: Annotate struct blk_flush_queue members with __guarded_by() Bart Van Assche
2026-12-03  1:59 ` [PATCH 18/52] blk-iocost: Add lock context annotations Bart Van Assche
2026-12-03  6:50   ` Tejun Heo
2026-12-03  1:59 ` [PATCH 19/52] blk-iocost: Pass the ioc pointer to hweight_after_donation() Bart Van Assche
2026-12-03  1:59 ` [PATCH 20/52] blk-iocost: Annotate struct ioc fields protected by ioc->lock Bart Van Assche
2026-12-03  1:59 ` [PATCH 21/52] blk-iolatency: Add a __must_hold() annotation for lat_info->lock Bart Van Assche
2026-12-03  1:59 ` [PATCH 22/52] blk-iolatency: Use WRITE_ONCE() in iolatency_clear_scaling() Bart Van Assche
2026-12-03  1:59 ` [PATCH 23/52] blk-iolatency: Annotate child_latency_info with __guarded_by() Bart Van Assche
2026-12-03  1:59 ` [PATCH 24/52] blk-mq: Add lock context annotations Bart Van Assche
2026-12-03  1:59 ` [PATCH 25/52] blk-mq: Annotate blk_mq_ctx::rq_lists with __guarded_by() Bart Van Assche
2026-12-03  1:59 ` [PATCH 26/52] blk-mq-debugfs: Pass q to blk_mq_debugfs_unregister_sched_hctx() Bart Van Assche
2026-12-03  1:59 ` [PATCH 27/52] blk-mq-debugfs: Add __must_hold() annotations for q->debugfs_mutex Bart Van Assche
2026-12-03  1:59 ` [PATCH 28/52] blk-mq-sched: Add lock context annotations Bart Van Assche
2026-12-03  1:59 ` [PATCH 29/52] blk-zoned: " Bart Van Assche
2026-12-03  7:43   ` Damien Le Moal
2026-12-03 17:28     ` Bart Van Assche
2026-12-03  1:59 ` [PATCH 30/52] blk-zoned: Annotate struct blk_zone_wplug members with __guarded_by() Bart Van Assche
2026-12-03  7:50   ` Damien Le Moal [this message]
2026-12-03  7:56     ` Christoph Hellwig
2026-12-03  8:00       ` Damien Le Moal
2026-12-03  8:02         ` Christoph Hellwig
2026-12-03 17:40     ` Bart Van Assche
2026-12-04  8:01       ` Damien Le Moal
2026-12-04 17:46         ` Bart Van Assche
2026-12-03  1:59 ` [PATCH 31/52] block: Pass the gendisk pointer explicitly to several bdev.c functions Bart Van Assche
2026-12-03  1:59 ` [PATCH 32/52] block: Add __must_hold() annotations in block/bdev.c Bart Van Assche
2026-12-03  1:59 ` [PATCH 33/52] block: Annotate the block_device_operations open and release callbacks Bart Van Assche
2026-12-03  1:59 ` [PATCH 34/52] block: Add __must_hold() annotations for bdev_lock Bart Van Assche
2026-12-03  1:59 ` [PATCH 35/52] block: Add lock context annotations in block/elevator.c Bart Van Assche
2026-12-03  1:59 ` [PATCH 36/52] block: Annotate elv_list with LIST_HEAD_GUARDED() Bart Van Assche
2026-12-03  2:00 ` [PATCH 37/52] block: Annotate the queue_limits_*() definitions in blk-settings.c Bart Van Assche
2026-12-03  2:00 ` [PATCH 38/52] block: Add __must_hold() annotations for the IA ranges sysfs functions Bart Van Assche
2026-12-03  2:00 ` [PATCH 39/52] block: Add a __must_hold() annotation for blk_holder_mutex Bart Van Assche
2026-12-03  2:00 ` [PATCH 40/52] block: Add a __must_hold() annotation for bio_slab_lock Bart Van Assche
2026-12-03  2:00 ` [PATCH 41/52] block: Annotate bio_slabs and slab_ref with __guarded_by() Bart Van Assche
2026-12-03  2:00 ` [PATCH 42/52] sed-opal: Add __must_hold() annotations for dev->dev_lock Bart Van Assche
2026-12-03  2:00 ` [PATCH 43/52] block: Annotate the disk_events list with LIST_HEAD_GUARDED() Bart Van Assche
2026-12-03  2:00 ` [PATCH 44/52] block: Annotate struct disk_events members with __guarded_by() Bart Van Assche
2026-12-03  2:00 ` [PATCH 45/52] block: Simplify the major_names[] traversal in blk_probe_dev() Bart Van Assche
2026-12-03  2:00 ` [PATCH 46/52] block: Annotate major_names with __guarded_by() Bart Van Assche
2026-12-03  2:00 ` [PATCH 47/52] loop: Pass disk instead of lo to __loop_clr_fd() Bart Van Assche
2026-12-03  2:00 ` [PATCH 48/52] loop: Annotate lo_release() and __loop_clr_fd() with __must_hold() Bart Van Assche
2026-12-03  2:00 ` [PATCH 49/52] block: partitions: Add __must_hold() annotations Bart Van Assche
2026-12-03  2:00 ` [PATCH 50/52] block: partitions: Enable lock context analysis Bart Van Assche
2026-12-03  2:00 ` [PATCH 51/52] kyber: Add __must_hold() annotations for khd->lock Bart Van Assche
2026-12-03  2:00 ` [PATCH 52/52] kyber: Add __guarded_by() annotations to struct kyber_hctx_data Bart Van Assche
2026-12-03  2:26 ` [PATCH 00/52] block: Add lock context annotations Damien Le Moal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3a07bc8f-5eae-4253-b2a4-7edece984b8d@kernel.org \
    --to=dlemoal@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=bvanassche@acm.org \
    --cc=hch@infradead.org \
    --cc=linux-block@vger.kernel.org \
    --cc=nathan@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.