Commit 53d29e0a authored by Alexandre Belloni's avatar Alexandre Belloni

rtc: cmos: fix possible race condition

The probe function is not allowed to fail after registering the RTC because
the following may happen:

CPU0:                                CPU1:
sys_load_module()
 do_init_module()
  do_one_initcall()
   cmos_do_probe()
    rtc_device_register()
     __register_chrdev()
     cdev->owner = struct module*
                                     open("/dev/rtc0")
    rtc_device_unregister()
  module_put()
  free_module()
   module_free(mod->module_core)
   /* struct module *module is now
      freed */
                                      chrdev_open()
                                       spin_lock(cdev_lock)
                                       cdev_get()
                                        try_module_get()
                                         module_is_live()
                                         /* dereferences already
                                            freed struct module* */

Switch to devm_rtc_allocate_device/rtc_register_device to register the rtc
as late as possible.
Signed-off-by: default avatarAlexandre Belloni <alexandre.belloni@bootlin.com>
parent 1af7068d
...@@ -751,8 +751,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq) ...@@ -751,8 +751,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
cmos_rtc.dev = dev; cmos_rtc.dev = dev;
dev_set_drvdata(dev, &cmos_rtc); dev_set_drvdata(dev, &cmos_rtc);
cmos_rtc.rtc = rtc_device_register(driver_name, dev, cmos_rtc.rtc = devm_rtc_allocate_device(dev);
&cmos_rtc_ops, THIS_MODULE);
if (IS_ERR(cmos_rtc.rtc)) { if (IS_ERR(cmos_rtc.rtc)) {
retval = PTR_ERR(cmos_rtc.rtc); retval = PTR_ERR(cmos_rtc.rtc);
goto cleanup0; goto cleanup0;
...@@ -822,6 +821,11 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq) ...@@ -822,6 +821,11 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
goto cleanup2; goto cleanup2;
} }
cmos_rtc.rtc->ops = &cmos_rtc_ops;
retval = rtc_register_device(cmos_rtc.rtc);
if (retval)
goto cleanup3;
dev_info(dev, "%s%s, %zd bytes nvram%s\n", dev_info(dev, "%s%s, %zd bytes nvram%s\n",
!is_valid_irq(rtc_irq) ? "no alarms" : !is_valid_irq(rtc_irq) ? "no alarms" :
cmos_rtc.mon_alrm ? "alarms up to one year" : cmos_rtc.mon_alrm ? "alarms up to one year" :
...@@ -833,12 +837,13 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq) ...@@ -833,12 +837,13 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
return 0; return 0;
cleanup3:
sysfs_remove_bin_file(&dev->kobj, &nvram);
cleanup2: cleanup2:
if (is_valid_irq(rtc_irq)) if (is_valid_irq(rtc_irq))
free_irq(rtc_irq, cmos_rtc.rtc); free_irq(rtc_irq, cmos_rtc.rtc);
cleanup1: cleanup1:
cmos_rtc.dev = NULL; cmos_rtc.dev = NULL;
rtc_device_unregister(cmos_rtc.rtc);
cleanup0: cleanup0:
if (RTC_IOMAPPED) if (RTC_IOMAPPED)
release_region(ports->start, resource_size(ports)); release_region(ports->start, resource_size(ports));
...@@ -869,7 +874,6 @@ static void cmos_do_remove(struct device *dev) ...@@ -869,7 +874,6 @@ static void cmos_do_remove(struct device *dev)
hpet_unregister_irq_handler(cmos_interrupt); hpet_unregister_irq_handler(cmos_interrupt);
} }
rtc_device_unregister(cmos->rtc);
cmos->rtc = NULL; cmos->rtc = NULL;
ports = cmos->iomem; ports = cmos->iomem;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment