Skip to content

Commit f1cbd03

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.dk/linux-block
Pull block fixes from Jens Axboe: "Been sitting on this for a while, but lets get this out the door. This fixes various important bugs for 3.3 final, along with a few more trivial ones. Please pull!" * 'for-linus' of git://git.kernel.dk/linux-block: block: fix ioc leak in put_io_context block, sx8: fix pointer math issue getting fw version Block: use a freezable workqueue for disk-event polling drivers/block/DAC960: fix -Wuninitialized warning drivers/block/DAC960: fix DAC960_V2_IOCTL_Opcode_T -Wenum-compare warning block: fix __blkdev_get and add_disk race condition block: Fix setting bio flags in drivers (sd_dif/floppy) block: Fix NULL pointer dereference in sd_revalidate_disk block: exit_io_context() should call elevator_exit_icq_fn() block: simplify ioc_release_fn() block: replace icq->changed with icq->flags
2 parents ff398c4 + ff8c147 commit f1cbd03

11 files changed

Lines changed: 199 additions & 96 deletions

File tree

‎block/blk-ioc.c‎

Lines changed: 88 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,22 @@ static void icq_free_icq_rcu(struct rcu_head *head)
3636
kmem_cache_free(icq->__rcu_icq_cache, icq);
3737
}
3838

39-
/*
40-
* Exit and free an icq. Called with both ioc and q locked.
41-
*/
39+
/* Exit an icq. Called with both ioc and q locked. */
4240
static void ioc_exit_icq(struct io_cq *icq)
41+
{
42+
struct elevator_type *et = icq->q->elevator->type;
43+
44+
if (icq->flags & ICQ_EXITED)
45+
return;
46+
47+
if (et->ops.elevator_exit_icq_fn)
48+
et->ops.elevator_exit_icq_fn(icq);
49+
50+
icq->flags |= ICQ_EXITED;
51+
}
52+
53+
/* Release an icq. Called with both ioc and q locked. */
54+
static void ioc_destroy_icq(struct io_cq *icq)
4355
{
4456
struct io_context *ioc = icq->ioc;
4557
struct request_queue *q = icq->q;
@@ -60,8 +72,7 @@ static void ioc_exit_icq(struct io_cq *icq)
6072
if (rcu_dereference_raw(ioc->icq_hint) == icq)
6173
rcu_assign_pointer(ioc->icq_hint, NULL);
6274

63-
if (et->ops.elevator_exit_icq_fn)
64-
et->ops.elevator_exit_icq_fn(icq);
75+
ioc_exit_icq(icq);
6576

6677
/*
6778
* @icq->q might have gone away by the time RCU callback runs
@@ -79,7 +90,6 @@ static void ioc_release_fn(struct work_struct *work)
7990
{
8091
struct io_context *ioc = container_of(work, struct io_context,
8192
release_work);
82-
struct request_queue *last_q = NULL;
8393
unsigned long flags;
8494

8595
/*
@@ -93,44 +103,19 @@ static void ioc_release_fn(struct work_struct *work)
93103
while (!hlist_empty(&ioc->icq_list)) {
94104
struct io_cq *icq = hlist_entry(ioc->icq_list.first,
95105
struct io_cq, ioc_node);
96-
struct request_queue *this_q = icq->q;
97-
98-
if (this_q != last_q) {
99-
/*
100-
* Need to switch to @this_q. Once we release
101-
* @ioc->lock, it can go away along with @cic.
102-
* Hold on to it.
103-
*/
104-
__blk_get_queue(this_q);
105-
106-
/*
107-
* blk_put_queue() might sleep thanks to kobject
108-
* idiocy. Always release both locks, put and
109-
* restart.
110-
*/
111-
if (last_q) {
112-
spin_unlock(last_q->queue_lock);
113-
spin_unlock_irqrestore(&ioc->lock, flags);
114-
blk_put_queue(last_q);
115-
} else {
116-
spin_unlock_irqrestore(&ioc->lock, flags);
117-
}
118-
119-
last_q = this_q;
120-
spin_lock_irqsave(this_q->queue_lock, flags);
121-
spin_lock_nested(&ioc->lock, 1);
122-
continue;
106+
struct request_queue *q = icq->q;
107+
108+
if (spin_trylock(q->queue_lock)) {
109+
ioc_destroy_icq(icq);
110+
spin_unlock(q->queue_lock);
111+
} else {
112+
spin_unlock_irqrestore(&ioc->lock, flags);
113+
cpu_relax();
114+
spin_lock_irqsave_nested(&ioc->lock, flags, 1);
123115
}
124-
ioc_exit_icq(icq);
125116
}
126117

127-
if (last_q) {
128-
spin_unlock(last_q->queue_lock);
129-
spin_unlock_irqrestore(&ioc->lock, flags);
130-
blk_put_queue(last_q);
131-
} else {
132-
spin_unlock_irqrestore(&ioc->lock, flags);
133-
}
118+
spin_unlock_irqrestore(&ioc->lock, flags);
134119

135120
kmem_cache_free(iocontext_cachep, ioc);
136121
}
@@ -145,6 +130,7 @@ static void ioc_release_fn(struct work_struct *work)
145130
void put_io_context(struct io_context *ioc)
146131
{
147132
unsigned long flags;
133+
bool free_ioc = false;
148134

149135
if (ioc == NULL)
150136
return;
@@ -159,22 +145,55 @@ void put_io_context(struct io_context *ioc)
159145
spin_lock_irqsave(&ioc->lock, flags);
160146
if (!hlist_empty(&ioc->icq_list))
161147
schedule_work(&ioc->release_work);
148+
else
149+
free_ioc = true;
162150
spin_unlock_irqrestore(&ioc->lock, flags);
163151
}
152+
153+
if (free_ioc)
154+
kmem_cache_free(iocontext_cachep, ioc);
164155
}
165156
EXPORT_SYMBOL(put_io_context);
166157

167158
/* Called by the exiting task */
168159
void exit_io_context(struct task_struct *task)
169160
{
170161
struct io_context *ioc;
162+
struct io_cq *icq;
163+
struct hlist_node *n;
164+
unsigned long flags;
171165

172166
task_lock(task);
173167
ioc = task->io_context;
174168
task->io_context = NULL;
175169
task_unlock(task);
176170

177-
atomic_dec(&ioc->nr_tasks);
171+
if (!atomic_dec_and_test(&ioc->nr_tasks)) {
172+
put_io_context(ioc);
173+
return;
174+
}
175+
176+
/*
177+
* Need ioc lock to walk icq_list and q lock to exit icq. Perform
178+
* reverse double locking. Read comment in ioc_release_fn() for
179+
* explanation on the nested locking annotation.
180+
*/
181+
retry:
182+
spin_lock_irqsave_nested(&ioc->lock, flags, 1);
183+
hlist_for_each_entry(icq, n, &ioc->icq_list, ioc_node) {
184+
if (icq->flags & ICQ_EXITED)
185+
continue;
186+
if (spin_trylock(icq->q->queue_lock)) {
187+
ioc_exit_icq(icq);
188+
spin_unlock(icq->q->queue_lock);
189+
} else {
190+
spin_unlock_irqrestore(&ioc->lock, flags);
191+
cpu_relax();
192+
goto retry;
193+
}
194+
}
195+
spin_unlock_irqrestore(&ioc->lock, flags);
196+
178197
put_io_context(ioc);
179198
}
180199

@@ -194,7 +213,7 @@ void ioc_clear_queue(struct request_queue *q)
194213
struct io_context *ioc = icq->ioc;
195214

196215
spin_lock(&ioc->lock);
197-
ioc_exit_icq(icq);
216+
ioc_destroy_icq(icq);
198217
spin_unlock(&ioc->lock);
199218
}
200219
}
@@ -363,13 +382,13 @@ struct io_cq *ioc_create_icq(struct request_queue *q, gfp_t gfp_mask)
363382
return icq;
364383
}
365384

366-
void ioc_set_changed(struct io_context *ioc, int which)
385+
void ioc_set_icq_flags(struct io_context *ioc, unsigned int flags)
367386
{
368387
struct io_cq *icq;
369388
struct hlist_node *n;
370389

371390
hlist_for_each_entry(icq, n, &ioc->icq_list, ioc_node)
372-
set_bit(which, &icq->changed);
391+
icq->flags |= flags;
373392
}
374393

375394
/**
@@ -387,7 +406,7 @@ void ioc_ioprio_changed(struct io_context *ioc, int ioprio)
387406

388407
spin_lock_irqsave(&ioc->lock, flags);
389408
ioc->ioprio = ioprio;
390-
ioc_set_changed(ioc, ICQ_IOPRIO_CHANGED);
409+
ioc_set_icq_flags(ioc, ICQ_IOPRIO_CHANGED);
391410
spin_unlock_irqrestore(&ioc->lock, flags);
392411
}
393412

@@ -404,11 +423,33 @@ void ioc_cgroup_changed(struct io_context *ioc)
404423
unsigned long flags;
405424

406425
spin_lock_irqsave(&ioc->lock, flags);
407-
ioc_set_changed(ioc, ICQ_CGROUP_CHANGED);
426+
ioc_set_icq_flags(ioc, ICQ_CGROUP_CHANGED);
408427
spin_unlock_irqrestore(&ioc->lock, flags);
409428
}
410429
EXPORT_SYMBOL(ioc_cgroup_changed);
411430

431+
/**
432+
* icq_get_changed - fetch and clear icq changed mask
433+
* @icq: icq of interest
434+
*
435+
* Fetch and clear ICQ_*_CHANGED bits from @icq. Grabs and releases
436+
* @icq->ioc->lock.
437+
*/
438+
unsigned icq_get_changed(struct io_cq *icq)
439+
{
440+
unsigned int changed = 0;
441+
unsigned long flags;
442+
443+
if (unlikely(icq->flags & ICQ_CHANGED_MASK)) {
444+
spin_lock_irqsave(&icq->ioc->lock, flags);
445+
changed = icq->flags & ICQ_CHANGED_MASK;
446+
icq->flags &= ~ICQ_CHANGED_MASK;
447+
spin_unlock_irqrestore(&icq->ioc->lock, flags);
448+
}
449+
return changed;
450+
}
451+
EXPORT_SYMBOL(icq_get_changed);
452+
412453
static int __init blk_ioc_init(void)
413454
{
414455
iocontext_cachep = kmem_cache_create("blkdev_ioc",

‎block/cfq-iosched.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3470,20 +3470,20 @@ cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
34703470
const int rw = rq_data_dir(rq);
34713471
const bool is_sync = rq_is_sync(rq);
34723472
struct cfq_queue *cfqq;
3473+
unsigned int changed;
34733474

34743475
might_sleep_if(gfp_mask & __GFP_WAIT);
34753476

34763477
spin_lock_irq(q->queue_lock);
34773478

34783479
/* handle changed notifications */
3479-
if (unlikely(cic->icq.changed)) {
3480-
if (test_and_clear_bit(ICQ_IOPRIO_CHANGED, &cic->icq.changed))
3481-
changed_ioprio(cic);
3480+
changed = icq_get_changed(&cic->icq);
3481+
if (unlikely(changed & ICQ_IOPRIO_CHANGED))
3482+
changed_ioprio(cic);
34823483
#ifdef CONFIG_CFQ_GROUP_IOSCHED
3483-
if (test_and_clear_bit(ICQ_CGROUP_CHANGED, &cic->icq.changed))
3484-
changed_cgroup(cic);
3484+
if (unlikely(changed & ICQ_CGROUP_CHANGED))
3485+
changed_cgroup(cic);
34853486
#endif
3486-
}
34873487

34883488
new_queue:
34893489
cfqq = cic_to_cfqq(cic, is_sync);

‎block/genhd.c‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static DEFINE_IDR(ext_devt_idr);
3535

3636
static struct device_type disk_type;
3737

38+
static void disk_alloc_events(struct gendisk *disk);
3839
static void disk_add_events(struct gendisk *disk);
3940
static void disk_del_events(struct gendisk *disk);
4041
static void disk_release_events(struct gendisk *disk);
@@ -601,6 +602,8 @@ void add_disk(struct gendisk *disk)
601602
disk->major = MAJOR(devt);
602603
disk->first_minor = MINOR(devt);
603604

605+
disk_alloc_events(disk);
606+
604607
/* Register BDI before referencing it from bdev */
605608
bdi = &disk->queue->backing_dev_info;
606609
bdi_register_dev(bdi, disk_devt(disk));
@@ -1475,9 +1478,9 @@ static void __disk_unblock_events(struct gendisk *disk, bool check_now)
14751478
intv = disk_events_poll_jiffies(disk);
14761479
set_timer_slack(&ev->dwork.timer, intv / 4);
14771480
if (check_now)
1478-
queue_delayed_work(system_nrt_wq, &ev->dwork, 0);
1481+
queue_delayed_work(system_nrt_freezable_wq, &ev->dwork, 0);
14791482
else if (intv)
1480-
queue_delayed_work(system_nrt_wq, &ev->dwork, intv);
1483+
queue_delayed_work(system_nrt_freezable_wq, &ev->dwork, intv);
14811484
out_unlock:
14821485
spin_unlock_irqrestore(&ev->lock, flags);
14831486
}
@@ -1521,7 +1524,7 @@ void disk_flush_events(struct gendisk *disk, unsigned int mask)
15211524
ev->clearing |= mask;
15221525
if (!ev->block) {
15231526
cancel_delayed_work(&ev->dwork);
1524-
queue_delayed_work(system_nrt_wq, &ev->dwork, 0);
1527+
queue_delayed_work(system_nrt_freezable_wq, &ev->dwork, 0);
15251528
}
15261529
spin_unlock_irq(&ev->lock);
15271530
}
@@ -1558,7 +1561,7 @@ unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
15581561

15591562
/* uncondtionally schedule event check and wait for it to finish */
15601563
disk_block_events(disk);
1561-
queue_delayed_work(system_nrt_wq, &ev->dwork, 0);
1564+
queue_delayed_work(system_nrt_freezable_wq, &ev->dwork, 0);
15621565
flush_delayed_work(&ev->dwork);
15631566
__disk_unblock_events(disk, false);
15641567

@@ -1595,7 +1598,7 @@ static void disk_events_workfn(struct work_struct *work)
15951598

15961599
intv = disk_events_poll_jiffies(disk);
15971600
if (!ev->block && intv)
1598-
queue_delayed_work(system_nrt_wq, &ev->dwork, intv);
1601+
queue_delayed_work(system_nrt_freezable_wq, &ev->dwork, intv);
15991602

16001603
spin_unlock_irq(&ev->lock);
16011604

@@ -1733,9 +1736,9 @@ module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
17331736
&disk_events_dfl_poll_msecs, 0644);
17341737

17351738
/*
1736-
* disk_{add|del|release}_events - initialize and destroy disk_events.
1739+
* disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
17371740
*/
1738-
static void disk_add_events(struct gendisk *disk)
1741+
static void disk_alloc_events(struct gendisk *disk)
17391742
{
17401743
struct disk_events *ev;
17411744

@@ -1748,16 +1751,6 @@ static void disk_add_events(struct gendisk *disk)
17481751
return;
17491752
}
17501753

1751-
if (sysfs_create_files(&disk_to_dev(disk)->kobj,
1752-
disk_events_attrs) < 0) {
1753-
pr_warn("%s: failed to create sysfs files for events\n",
1754-
disk->disk_name);
1755-
kfree(ev);
1756-
return;
1757-
}
1758-
1759-
disk->ev = ev;
1760-
17611754
INIT_LIST_HEAD(&ev->node);
17621755
ev->disk = disk;
17631756
spin_lock_init(&ev->lock);
@@ -1766,8 +1759,21 @@ static void disk_add_events(struct gendisk *disk)
17661759
ev->poll_msecs = -1;
17671760
INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
17681761

1762+
disk->ev = ev;
1763+
}
1764+
1765+
static void disk_add_events(struct gendisk *disk)
1766+
{
1767+
if (!disk->ev)
1768+
return;
1769+
1770+
/* FIXME: error handling */
1771+
if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
1772+
pr_warn("%s: failed to create sysfs files for events\n",
1773+
disk->disk_name);
1774+
17691775
mutex_lock(&disk_events_mutex);
1770-
list_add_tail(&ev->node, &disk_events);
1776+
list_add_tail(&disk->ev->node, &disk_events);
17711777
mutex_unlock(&disk_events_mutex);
17721778

17731779
/*

0 commit comments

Comments
 (0)