Skip to content

Commit

Permalink
memstick: avert possible race condition between idr_pre_get and idr_g…
Browse files Browse the repository at this point in the history
…et_new

Implement the usual pattern around idr_pre_get() and idr_get_new() to
handlethe situation where another thread concurrently steals this thread's
idr_pre_get() preallocation.

Signed-off-by: Alex Dubov <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
oakad authored and torvalds committed Jan 13, 2011
1 parent 8930c8a commit d8256d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
18 changes: 11 additions & 7 deletions drivers/memstick/core/memstick.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,18 @@ int memstick_add_host(struct memstick_host *host)
{
int rc;

if (!idr_pre_get(&memstick_host_idr, GFP_KERNEL))
return -ENOMEM;
while (1) {
if (!idr_pre_get(&memstick_host_idr, GFP_KERNEL))
return -ENOMEM;

spin_lock(&memstick_host_lock);
rc = idr_get_new(&memstick_host_idr, host, &host->id);
spin_unlock(&memstick_host_lock);
if (rc)
return rc;
spin_lock(&memstick_host_lock);
rc = idr_get_new(&memstick_host_idr, host, &host->id);
spin_unlock(&memstick_host_lock);
if (!rc)
break;
else if (rc != -EAGAIN)
return rc;
}

dev_set_name(&host->dev, "memstick%u", host->id);

Expand Down
6 changes: 4 additions & 2 deletions drivers/memstick/core/mspro_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -1206,10 +1206,12 @@ static int mspro_block_init_disk(struct memstick_dev *card)

msb->page_size = be16_to_cpu(sys_info->unit_size);

if (!idr_pre_get(&mspro_block_disk_idr, GFP_KERNEL))
mutex_lock(&mspro_block_disk_lock);
if (!idr_pre_get(&mspro_block_disk_idr, GFP_KERNEL)) {
mutex_unlock(&mspro_block_disk_lock);
return -ENOMEM;
}

mutex_lock(&mspro_block_disk_lock);
rc = idr_get_new(&mspro_block_disk_idr, card, &disk_id);
mutex_unlock(&mspro_block_disk_lock);

Expand Down

0 comments on commit d8256d4

Please sign in to comment.