struct my_struct { … struct tasklet_struct *tasklet; int irq; }; void tasklet_handler(…) { do_the_things_right(…); } irqreturn_t irq_handler(void *param) { struct my_struct *ms = param; … tasklet_schedule(&ms->tasklet); return IRQ_HANDLED; } int probe(…) { struct my_struct *ms; int err; ms = devm_kzalloc(…); … tasklet_init(&ms->tasklet, tasklet_handler, (unsigned long)ms); … err = devm_request_irq(ms->irq, irq_handler, …, ms); if (err) return err; return 0; } int remove(…) { struct my_struct *ms = …; … tasklet_kill(&ms->tasklet); }
tasklet_schedule()
) and the execution of a task. At this time, the removal of the driver from memory can occur, the user called rmmod my_module
. Of course, we explicitly call deleting a tasklet, see tasklet_kill()
, but the interrupt handler is still active , since we used the devres API and planned to remove it in the order of the queue after the execution ->remove()
! int remove(…) { struct my_struct *ms = …; … devm_free_irq(ms->irq, ms); tasklet_kill(&ms->tasklet); }
int closecb(…) { struct my_struct *ms = …; do_something_on_close(ms, …); } struct file_ops fops = { .close = closecb, … }; int probe(…) { struct my_struct *ms; int err; ms = devm_kzalloc(…); … err = register_char_device(ms, "node_served_by_driver", &fops, …); if (err) return err; return 0; } int remove(…) { struct my_struct *ms = …; … }
/dev/node_served_by_driver
, and make it so that the device remains open. echo our_device_name > /sys/bus/platform/drivers/our_driver_name/unbind
or simply by disconnecting the device from the bus, if possible, for example, by disconnecting the USB drive.->probe()
released at the time of unlinking the device. And we still use this room! In this case, the device driver is not deleted and cannot be removed, because held by the program that opened the device, and remains in memory until the moment of explicit closing and deletion.devm_kzalloc()
in file operations in the driver, carefully monitor the lifetime of objects. According to the devres API author, the prefix dev is not just there, but with the goal of indicating that resources are directly related to hardware, and not to handling events from the user.Source: https://habr.com/ru/post/265111/
All Articles