Annotate the block, pending and clearing members of struct disk_events with __guarded_by(&lock). The block counter is protected by the spinlock rather than by block_mutex since __disk_unblock_events() modifies it without holding block_mutex. Move the member comments above the members to keep the lines short. Use scoped_guard(spinlock_init) to initialize ev->block, wrap the existing lockless read of ev->clearing in disk_clear_events() with data_race(READ_ONCE(...)), and wrap passing &ev->clearing to disk_check_events() and reading ev->block during teardown with context_unsafe(). This lets Clang's context analysis verify that the annotated members are only accessed with ev->lock held. No functional change. Signed-off-by: Bart Van Assche --- block/disk-events.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/block/disk-events.c b/block/disk-events.c index ce31d62f6bbc..dc9098a95a10 100644 --- a/block/disk-events.c +++ b/block/disk-events.c @@ -13,9 +13,12 @@ struct disk_events { spinlock_t lock; struct mutex block_mutex; /* protects blocking */ - int block; /* event blocking depth */ - unsigned int pending; /* events already sent out */ - unsigned int clearing; /* events being cleared */ + /* event blocking depth */ + int block __guarded_by(&lock); + /* events already sent out */ + unsigned int pending __guarded_by(&lock); + /* events being cleared */ + unsigned int clearing __guarded_by(&lock); long poll_msecs; /* interval, -1 for default */ struct delayed_work dwork; @@ -250,7 +253,8 @@ static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask) * if ev->clearing is not 0, the disk_flush_events got called in the * middle of this function, so we want to run the workfn without delay. */ - __disk_unblock_events(disk, ev->clearing ? true : false); + /* Lockless read. */ + __disk_unblock_events(disk, data_race(READ_ONCE(ev->clearing))); /* then, fetch and clear pending events */ spin_lock_irq(&ev->lock); @@ -310,7 +314,11 @@ static void disk_events_workfn(struct work_struct *work) struct delayed_work *dwork = to_delayed_work(work); struct disk_events *ev = container_of(dwork, struct disk_events, dwork); - disk_check_events(ev, &ev->clearing); + /* + * disk_check_events() reads *clearing_ptr locklessly before calling + * ->check_events() and clears *clearing_ptr under ev->lock. + */ + disk_check_events(ev, context_unsafe(&ev->clearing)); } /* @@ -445,9 +453,9 @@ int disk_alloc_events(struct gendisk *disk) INIT_LIST_HEAD(&ev->node); ev->disk = disk; - spin_lock_init(&ev->lock); mutex_init(&ev->block_mutex); - ev->block = 1; + scoped_guard(spinlock_init, &ev->lock) + ev->block = 1; ev->poll_msecs = -1; INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn); @@ -484,7 +492,15 @@ void disk_del_events(struct gendisk *disk) void disk_release_events(struct gendisk *disk) { - /* the block count should be 1 from disk_del_events() */ - WARN_ON_ONCE(disk->ev && disk->ev->block != 1); + if (disk->ev) { + /* + * The block count should be 1 from disk_del_events() and no + * other context can access disk->ev once it is about to be + * freed. + */ + int block = context_unsafe(disk->ev->block); + + WARN_ON_ONCE(block != 1); + } kfree(disk->ev); }