Skip to content

Commit d5213fb

Browse files
Handle device_create failure
Add proper error handling for device_create() in simrupt_init. This patch checks the return value and adds cleanup logic to ensure consistent error handling. Co-authored-by: Po-Ying Chiu <[email protected]>
1 parent 69efd79 commit d5213fb

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

simrupt.c

+15-7
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ static const struct file_operations simrupt_fops = {
324324
static int __init simrupt_init(void)
325325
{
326326
dev_t dev_id;
327+
struct device *device;
327328
int ret;
328329

329330
if (kfifo_alloc(&rx_fifo, PAGE_SIZE, GFP_KERNEL) < 0)
@@ -356,25 +357,26 @@ static int __init simrupt_init(void)
356357
}
357358

358359
/* Register the device with sysfs */
359-
device_create(simrupt_class, NULL, MKDEV(major, 0), NULL, DEV_NAME);
360+
device = device_create(simrupt_class, NULL, dev_id, NULL, DEV_NAME);
361+
if (IS_ERR(device)) {
362+
printk(KERN_ERR "error creating simrupt device\n");
363+
ret = PTR_ERR(device);
364+
goto error_device;
365+
}
360366

361367
/* Allocate fast circular buffer */
362368
fast_buf.buf = vmalloc(PAGE_SIZE);
363369
if (!fast_buf.buf) {
364-
device_destroy(simrupt_class, dev_id);
365-
class_destroy(simrupt_class);
366370
ret = -ENOMEM;
367-
goto error_cdev;
371+
goto error_vmalloc;
368372
}
369373

370374
/* Create the workqueue */
371375
simrupt_workqueue = alloc_workqueue("simruptd", WQ_UNBOUND, WQ_MAX_ACTIVE);
372376
if (!simrupt_workqueue) {
373377
vfree(fast_buf.buf);
374-
device_destroy(simrupt_class, dev_id);
375-
class_destroy(simrupt_class);
376378
ret = -ENOMEM;
377-
goto error_cdev;
379+
goto error_workqueue;
378380
}
379381

380382
/* Setup the timer */
@@ -384,6 +386,12 @@ static int __init simrupt_init(void)
384386
pr_info("simrupt: registered new simrupt device: %d,%d\n", major, 0);
385387
out:
386388
return ret;
389+
error_workqueue:
390+
vfree(fast_buf.buf);
391+
error_vmalloc:
392+
device_destroy(simrupt_class, dev_id);
393+
error_device:
394+
class_destroy(simrupt_class);
387395
error_cdev:
388396
cdev_del(&simrupt_cdev);
389397
error_region:

0 commit comments

Comments
 (0)