Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d8256d48 authored by Alex Dubov's avatar Alex Dubov Committed by Linus Torvalds
Browse files

memstick: avert possible race condition between idr_pre_get and idr_get_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: default avatarAlex Dubov <oakad@yahoo.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 8930c8aa
Loading
Loading
Loading
Loading
+11 −7
Original line number Diff line number Diff line
@@ -511,14 +511,18 @@ int memstick_add_host(struct memstick_host *host)
{
	int rc;

	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)
		if (!rc)
			break;
		else if (rc != -EAGAIN)
			return rc;
	}

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

+4 −2
Original line number Diff line number Diff line
@@ -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);