Skip to content

Handle device_create failure #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions simrupt.c
Original file line number Diff line number Diff line change
@@ -324,6 +324,7 @@ static const struct file_operations simrupt_fops = {
static int __init simrupt_init(void)
{
dev_t dev_id;
struct device *device;
int ret;

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

/* Register the device with sysfs */
device_create(simrupt_class, NULL, MKDEV(major, 0), NULL, DEV_NAME);
device = device_create(simrupt_class, NULL, dev_id, NULL, DEV_NAME);
if (IS_ERR(device)) {
printk(KERN_ERR "Failed to create device.\n");
ret = PTR_ERR(device);
goto error_device;
}

/* Allocate fast circular buffer */
fast_buf.buf = vmalloc(PAGE_SIZE);
if (!fast_buf.buf) {
device_destroy(simrupt_class, dev_id);
class_destroy(simrupt_class);
ret = -ENOMEM;
goto error_cdev;
goto error_vmalloc;
}

/* Create the workqueue */
simrupt_workqueue = alloc_workqueue("simruptd", WQ_UNBOUND, WQ_MAX_ACTIVE);
if (!simrupt_workqueue) {
vfree(fast_buf.buf);
device_destroy(simrupt_class, dev_id);
class_destroy(simrupt_class);
ret = -ENOMEM;
goto error_cdev;
goto error_workqueue;
}

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