Skip to content

Commit a58d37b

Browse files
fbarratmpe
authored andcommitted
ocxl: Fix concurrent AFU open and device removal
If an ocxl device is unbound through sysfs at the same time its AFU is being opened by a user process, the open code may dereference freed stuctures, which can lead to kernel oops messages. You'd have to hit a tiny time window, but it's possible. It's fairly easy to test by making the time window bigger artificially. Fix it with a combination of 2 changes: - when an AFU device is found in the IDR by looking for the device minor number, we should hold a reference on the device until after the context is allocated. A reference on the AFU structure is kept when the context is allocated, so we can release the reference on the device after the context allocation. - with the fix above, there's still another even tinier window, between the time the AFU device is found in the IDR and the reference on the device is taken. We can fix this one by removing the IDR entry earlier, when the device setup is removed, instead of waiting for the 'release' device callback. With proper locking around the IDR. Fixes: 75ca758 ("ocxl: Create a clear delineation between ocxl backend & frontend") Cc: stable@vger.kernel.org # v5.2+ Signed-off-by: Frederic Barrat <fbarrat@linux.ibm.com> Reviewed-by: Greg Kurz <groug@kaod.org> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/20190624144148.32022-1-fbarrat@linux.ibm.com
1 parent e42617b commit a58d37b

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

‎drivers/misc/ocxl/file.c‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ static struct class *ocxl_class;
1818
static struct mutex minors_idr_lock;
1919
static struct idr minors_idr;
2020

21-
static struct ocxl_file_info *find_file_info(dev_t devno)
21+
static struct ocxl_file_info *find_and_get_file_info(dev_t devno)
2222
{
2323
struct ocxl_file_info *info;
2424

25-
/*
26-
* We don't declare an RCU critical section here, as our AFU
27-
* is protected by a reference counter on the device. By the time the
28-
* info reference is removed from the idr, the ref count of
29-
* the device is already at 0, so no user API will access that AFU and
30-
* this function can't return it.
31-
*/
25+
mutex_lock(&minors_idr_lock);
3226
info = idr_find(&minors_idr, MINOR(devno));
27+
if (info)
28+
get_device(&info->dev);
29+
mutex_unlock(&minors_idr_lock);
3330
return info;
3431
}
3532

@@ -58,14 +55,16 @@ static int afu_open(struct inode *inode, struct file *file)
5855

5956
pr_debug("%s for device %x\n", __func__, inode->i_rdev);
6057

61-
info = find_file_info(inode->i_rdev);
58+
info = find_and_get_file_info(inode->i_rdev);
6259
if (!info)
6360
return -ENODEV;
6461

6562
rc = ocxl_context_alloc(&ctx, info->afu, inode->i_mapping);
66-
if (rc)
63+
if (rc) {
64+
put_device(&info->dev);
6765
return rc;
68-
66+
}
67+
put_device(&info->dev);
6968
file->private_data = ctx;
7069
return 0;
7170
}
@@ -487,7 +486,6 @@ static void info_release(struct device *dev)
487486
{
488487
struct ocxl_file_info *info = container_of(dev, struct ocxl_file_info, dev);
489488

490-
free_minor(info);
491489
ocxl_afu_put(info->afu);
492490
kfree(info);
493491
}
@@ -577,6 +575,7 @@ void ocxl_file_unregister_afu(struct ocxl_afu *afu)
577575

578576
ocxl_file_make_invisible(info);
579577
ocxl_sysfs_unregister_afu(info);
578+
free_minor(info);
580579
device_unregister(&info->dev);
581580
}
582581

0 commit comments

Comments
 (0)