loop: defer the queue limits clear to a workqueue
PATCH v7 1/1 loop: defer the queue limits clear to a workqueue
From: Tao Cui <[email protected]>
loop_clear_limits() calls queue_limits_commit_update() directly from the loop workqueue that processes the request. That does a non-atomic struct assignment to q->limits without freezing the queue, which races with lockless readers of q->limits on other CPUs, such as bio splitting reading max_hw_sectors and the discard path reading max_hw_discard_sectors, letting them observe torn values. The trigger is a discard or write-zeroes request on a loop device whose backing file does not support the corresponding fallocate operation.
The code already has an XXX comment saying this should move to a workqueue. Do that: schedule a work item on the system workqueue, where it is safe to freeze the queue around the limits update. The pending modes live under a new mutex, lo->clear_limits_lock, guarded by a backing-file check that drops records from a racing unbind or rebind. Requests that arrive while the clear is in flight fail quietly, so the console report stays at the first failure. The work item is cancelled before the backing file changes and before the device is freed.
--- drivers/block/loop.c | 91 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 9 deletions(-)
drivers/block/loop.c +82 -9
@@ -67,6 +67,10 @@ struct loop_device {struct list_head rootcg_cmd_list;struct list_head idle_worker_list;struct rb_root worker_tree; +struct work_struct clear_limits_work; +struct mutex clear_limits_lock; +unsigned int clear_limits_mode; +bool clear_limits_pending;struct timer_list timer;bool sysfs_inited;
@@ -222,9 +226,25 @@ static void loop_set_size(struct loop_device *lo, loff_t size)kobject_uevent (&disk_to_dev (lo->lo_disk)->kobj,KOBJ_CHANGE ); } -static void loop_clear_limits (struct loop_device *lo,int mode) +static void loop_clear_limits_workfn (struct work_struct *work) { +struct loop_device *lo = + container_of(work, struct loop_device, clear_limits_work);struct queue_limits lim =queue_limits_start_update (lo->lo_queue); +unsigned int memflags; +int mode; + +/* +* Requests that arrive while this clear is in flight are failed +* quietly (see loop_queue_rq); once the limits have been +* committed, further ones are rejected early at submission. +*/ + memflags =blk_mq_freeze_queue (lo->lo_queue); +mutex_lock (&lo->clear_limits_lock); + mode = lo->clear_limits_mode; + lo->clear_limits_mode =0 ; +WRITE_ONCE (lo->clear_limits_pending, false); +mutex_unlock (&lo->clear_limits_lock);if (mode &FALLOC_FL_ZERO_RANGE ) lim.max_write_zeroes_sectors =0 ;
@@ -234,14 +254,27 @@ static void loop_clear_limits(struct loop_device *lo, int mode) lim.discard_granularity =0 ; } -/* -* XXX: this updates the queue limits without freezing the queue, which -* is against the locking protocol and dangerous. But we can't just -* freeze the queue as we're inside the ->queue_rq method here. So this -* should move out into a workqueue unless we get the file operations to -* advertise if they support specific fallocate operations. -*/ queue_limits_commit_update (lo->lo_queue, &lim); +blk_mq_unfreeze_queue (lo->lo_queue, memflags); +} + +static void loop_clear_limits (struct loop_device *lo,struct file *file, +int mode) +{ +mutex_lock (&lo->clear_limits_lock); +if (lo->lo_backing_file != file) { +/* +* The recording request was racing with an unbind or a +* rebinding; its verdict is about a backing file that is +* no longer current. Drop it. +*/ +mutex_unlock (&lo->clear_limits_lock); +return ; + } + lo->clear_limits_mode |= mode; +WRITE_ONCE (lo->clear_limits_pending, true); +mutex_unlock (&lo->clear_limits_lock); +schedule_work (&lo->clear_limits_work); } static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
@@ -269,7 +302,7 @@ static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,* supported and clear them here if that turns out not to be true. */ if (unlikely (ret == -EOPNOTSUPP )) -loop_clear_limits (lo, mode); +loop_clear_limits (lo, file, mode);return ret; }
@@ -514,6 +547,15 @@ static int loop_validate_file(struct file *file, struct block_device *bdev)static void loop_assign_backing_file (struct loop_device *lo,struct file *file) { lo->lo_backing_file = file; +/* +* Discard any modes recorded against the old backing file. The +* paths that can race with a pending work item cancel it before +* they get here. +*/ +mutex_lock (&lo->clear_limits_lock); + lo->clear_limits_mode =0 ; +WRITE_ONCE (lo->clear_limits_pending, false); +mutex_unlock (&lo->clear_limits_lock); lo->old_gfp_mask =mapping_gfp_mask (file->f_mapping);mapping_set_gfp_mask (file->f_mapping, lo->old_gfp_mask & ~(__GFP_IO | __GFP_FS));
@@ -1035,6 +1077,21 @@ static void __loop_clr_fd(struct loop_device *lo) lo->lo_backing_file =NULL ;spin_unlock_irq (&lo->lo_lock); +/* +* Discard and write-zeroes requests are submitted synchronously, +* but a fatal signal can kill the submitting task between the +* per-bio completions of a multi-bio range, leaving requests in +* flight across this unbind. Cancel any clear they have +* scheduled and reset the modes; loop_clear_limits() also drops +* records whose backing file is no longer current, so stragglers +* arriving after a later loop_configure() cannot affect it. +*/ +cancel_work_sync (&lo->clear_limits_work); +mutex_lock (&lo->clear_limits_lock); + lo->clear_limits_mode =0 ; +WRITE_ONCE (lo->clear_limits_pending, false); +mutex_unlock (&lo->clear_limits_lock); + lo->lo_device =NULL ; lo->lo_offset =0 ; lo->lo_sizelimit =0 ;
@@ -1700,6 +1757,7 @@ static void lo_free_disk(struct gendisk *disk)loop_free_idle_workers (lo, true);timer_shutdown_sync (&lo->timer);mutex_destroy (&lo->lo_mutex); +mutex_destroy (&lo->clear_limits_lock);kfree (lo); }
@@ -1802,9 +1860,19 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,switch (req_op (rq)) {case REQ_OP_FLUSH : + cmd->use_aio = false; +break ;case REQ_OP_DISCARD :case REQ_OP_WRITE_ZEROES : cmd->use_aio = false; +/* +* A pending limits clear means this operation is already +* known to be unsupported and the commit will make later +* attempts fail early at submission. Fail quietly so only +* the first failure reports to the console. +*/ +if (READ_ONCE (lo->clear_limits_pending)) + rq->rq_flags |=RQF_QUIET ;break ;default : cmd->use_aio = lo->lo_flags &LO_FLAGS_DIRECT_IO ;
@@ -2018,6 +2086,8 @@ static int loop_add(int i)spin_lock_init (&lo->lo_lock);spin_lock_init (&lo->lo_work_lock);INIT_WORK (&lo->rootcg_work, loop_rootcg_workfn); +INIT_WORK (&lo->clear_limits_work, loop_clear_limits_workfn); +mutex_init (&lo->clear_limits_lock);INIT_LIST_HEAD (&lo->rootcg_cmd_list); disk->major =LOOP_MAJOR ; disk->first_minor = i << part_shift;
@@ -2056,6 +2126,9 @@ static int loop_add(int i)static void loop_remove (struct loop_device *lo) { +/* Cancel before the queue becomes unreachable. */ +cancel_work_sync (&lo->clear_limits_work); +/* Make this loop device unreachable from pathname. */ del_gendisk (lo->lo_disk);blk_mq_free_tag_set (&lo->tag_set);
Signature · 1 line
2.53.0