Skip to content

Commit 5eb4685

Browse files
Yasuaki Ishimatsuaxboe
authored andcommitted
cfq-iosched: fix cfq_cic_link() race confition
cfq_cic_link() has race condition. When some processes which shared ioc issue I/O to same block device simultaneously, cfq_cic_link() returns -EEXIST sometimes. The race condition might stop I/O by following steps: step 1: Process A: Issue an I/O to /dev/sda step 2: Process A: Get an ioc (iocA here) in get_io_context() which does not linked with a cic for the device step 3: Process A: Get a new cic for the device (cicA here) in cfq_alloc_io_context() step 4: Process B: Issue an I/O to /dev/sda step 5: Process B: Get iocA in get_io_context() since process A and B share the same ioc step 6: Process B: Get a new cic for the device (cicB here) in cfq_alloc_io_context() since iocA has not been linked with a cic for the device yet step 7: Process A: Link cicA to iocA in cfq_cic_link() step 8: Process A: Dispatch I/O to driver and finish it step 9: Process B: Try to link cicB to iocA in cfq_cic_link() But it fails with showing "cfq: cic link failed!" kernel message, since iocA has already linked with cicA at step 7. step 10: Process B: Wait for finishig I/O in get_request_wait() The function does not wake up, when there is no I/O to the device. When cfq_cic_link() returns -EEXIST, it means ioc has already linked with cic. So when cfq_cic_link() return -EEXIST, retry cfq_cic_lookup(). Signed-off-by: Yasuaki Ishimatsu <isimatu.yasuaki@jp.fujitsu.com> Cc: stable@kernel.org Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 2984ff3 commit 5eb4685

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

‎block/cfq-iosched.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,7 +3184,7 @@ static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
31843184
}
31853185
}
31863186

3187-
if (ret)
3187+
if (ret && ret != -EEXIST)
31883188
printk(KERN_ERR "cfq: cic link failed!\n");
31893189

31903190
return ret;
@@ -3200,13 +3200,15 @@ cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
32003200
{
32013201
struct io_context *ioc = NULL;
32023202
struct cfq_io_context *cic;
3203+
int ret;
32033204

32043205
might_sleep_if(gfp_mask & __GFP_WAIT);
32053206

32063207
ioc = get_io_context(gfp_mask, cfqd->queue->node);
32073208
if (!ioc)
32083209
return NULL;
32093210

3211+
retry:
32103212
cic = cfq_cic_lookup(cfqd, ioc);
32113213
if (cic)
32123214
goto out;
@@ -3215,7 +3217,12 @@ cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
32153217
if (cic == NULL)
32163218
goto err;
32173219

3218-
if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
3220+
ret = cfq_cic_link(cfqd, ioc, cic, gfp_mask);
3221+
if (ret == -EEXIST) {
3222+
/* someone has linked cic to ioc already */
3223+
cfq_cic_free(cic);
3224+
goto retry;
3225+
} else if (ret)
32193226
goto err_free;
32203227

32213228
out:

0 commit comments

Comments
 (0)