From 6706664d92ea841913d5fcfd06c290fbe6d33bd2 Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Mon, 20 Jul 2015 16:02:49 -0700 Subject: rtc: class: fix double free in rtc_register_device() error path Commit 59cca865f21e ("drivers/rtc/class.c: fix device_register() error handling") correctly noted that naked kfree() should not be used after failed device_register() call, however, while it added the needed put_device() it forgot to remove the original kfree() causing double-free. Cc: Vasiliy Kulikov Signed-off-by: Dmitry Torokhov Reviewed-by: Krzysztof Kozlowski Signed-off-by: Alexandre Belloni --- drivers/rtc/class.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'drivers/rtc/class.c') diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c index ea2a315df6b7..eb82ec2a21bd 100644 --- a/drivers/rtc/class.c +++ b/drivers/rtc/class.c @@ -234,8 +234,9 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev, err = device_register(&rtc->dev); if (err) { + /* This will free both memory and the ID */ put_device(&rtc->dev); - goto exit_kfree; + goto exit; } rtc_dev_add_device(rtc); @@ -247,9 +248,6 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev, return rtc; -exit_kfree: - kfree(rtc); - exit_ida: ida_simple_remove(&rtc_ida, id); -- cgit v1.2.3 From c3b399a4b6703a04ef6eb3efe35ff12163e409e0 Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Mon, 20 Jul 2015 16:02:50 -0700 Subject: rtc: class: remove unnecessary device_get() in rtc_device_unregister Technically the address of rtc->dev can never be NULL, so get_device() can never fail. Also caller of rtc_device_unregister() supposed to be the owner of the device and thus have a valid reference. Therefore call to get_device() is not needed here. Signed-off-by: Dmitry Torokhov Reviewed-by: Krzysztof Kozlowski Signed-off-by: Alexandre Belloni --- drivers/rtc/class.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'drivers/rtc/class.c') diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c index eb82ec2a21bd..de7707f7e766 100644 --- a/drivers/rtc/class.c +++ b/drivers/rtc/class.c @@ -266,19 +266,18 @@ EXPORT_SYMBOL_GPL(rtc_device_register); */ void rtc_device_unregister(struct rtc_device *rtc) { - if (get_device(&rtc->dev) != NULL) { - mutex_lock(&rtc->ops_lock); - /* remove innards of this RTC, then disable it, before - * letting any rtc_class_open() users access it again - */ - rtc_sysfs_del_device(rtc); - rtc_dev_del_device(rtc); - rtc_proc_del_device(rtc); - device_unregister(&rtc->dev); - rtc->ops = NULL; - mutex_unlock(&rtc->ops_lock); - put_device(&rtc->dev); - } + mutex_lock(&rtc->ops_lock); + /* + * Remove innards of this RTC, then disable it, before + * letting any rtc_class_open() users access it again + */ + rtc_sysfs_del_device(rtc); + rtc_dev_del_device(rtc); + rtc_proc_del_device(rtc); + device_del(&rtc->dev); + rtc->ops = NULL; + mutex_unlock(&rtc->ops_lock); + put_device(&rtc->dev); } EXPORT_SYMBOL_GPL(rtc_device_unregister); -- cgit v1.2.3 From 3ee2c40b7ac2bf121aaa1176d8ac25b6a26e3a94 Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Thu, 23 Jul 2015 16:01:08 -0700 Subject: rtc: switch to using is_visible() to control sysfs attributes Instead of creating wakealarm attribute manually, after the device has been registered, let's rely on facilities provided by the attribute groups to control which attributes are visible and which are not. This allows to create all needed attributes at once, at the same time that we register RTC class device. Signed-off-by: Dmitry Torokhov Reviewed-by: Krzysztof Kozlowski Signed-off-by: Alexandre Belloni --- drivers/rtc/class.c | 4 +--- drivers/rtc/rtc-core.h | 19 +++------------- drivers/rtc/rtc-sysfs.c | 59 +++++++++++++++++++++++++------------------------ 3 files changed, 34 insertions(+), 48 deletions(-) (limited to 'drivers/rtc/class.c') diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c index de7707f7e766..de86578bcd6d 100644 --- a/drivers/rtc/class.c +++ b/drivers/rtc/class.c @@ -202,6 +202,7 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev, rtc->max_user_freq = 64; rtc->dev.parent = dev; rtc->dev.class = rtc_class; + rtc->dev.groups = rtc_get_dev_attribute_groups(); rtc->dev.release = rtc_device_release; mutex_init(&rtc->ops_lock); @@ -240,7 +241,6 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev, } rtc_dev_add_device(rtc); - rtc_sysfs_add_device(rtc); rtc_proc_add_device(rtc); dev_info(dev, "rtc core: registered %s as %s\n", @@ -271,7 +271,6 @@ void rtc_device_unregister(struct rtc_device *rtc) * Remove innards of this RTC, then disable it, before * letting any rtc_class_open() users access it again */ - rtc_sysfs_del_device(rtc); rtc_dev_del_device(rtc); rtc_proc_del_device(rtc); device_del(&rtc->dev); @@ -360,7 +359,6 @@ static int __init rtc_init(void) } rtc_class->pm = RTC_CLASS_DEV_PM_OPS; rtc_dev_init(); - rtc_sysfs_init(rtc_class); return 0; } diff --git a/drivers/rtc/rtc-core.h b/drivers/rtc/rtc-core.h index 5f9df7430a22..a098aea197fc 100644 --- a/drivers/rtc/rtc-core.h +++ b/drivers/rtc/rtc-core.h @@ -48,23 +48,10 @@ static inline void rtc_proc_del_device(struct rtc_device *rtc) #endif #ifdef CONFIG_RTC_INTF_SYSFS - -extern void __init rtc_sysfs_init(struct class *); -extern void rtc_sysfs_add_device(struct rtc_device *rtc); -extern void rtc_sysfs_del_device(struct rtc_device *rtc); - +const struct attribute_group **rtc_get_dev_attribute_groups(void); #else - -static inline void rtc_sysfs_init(struct class *rtc) -{ -} - -static inline void rtc_sysfs_add_device(struct rtc_device *rtc) +static inline const struct attribute_group **rtc_get_dev_attribute_groups(void) { + return NULL; } - -static inline void rtc_sysfs_del_device(struct rtc_device *rtc) -{ -} - #endif diff --git a/drivers/rtc/rtc-sysfs.c b/drivers/rtc/rtc-sysfs.c index e3ce1dc92b65..7273855ed02e 100644 --- a/drivers/rtc/rtc-sysfs.c +++ b/drivers/rtc/rtc-sysfs.c @@ -122,17 +122,6 @@ hctosys_show(struct device *dev, struct device_attribute *attr, char *buf) } static DEVICE_ATTR_RO(hctosys); -static struct attribute *rtc_attrs[] = { - &dev_attr_name.attr, - &dev_attr_date.attr, - &dev_attr_time.attr, - &dev_attr_since_epoch.attr, - &dev_attr_max_user_freq.attr, - &dev_attr_hctosys.attr, - NULL, -}; -ATTRIBUTE_GROUPS(rtc); - static ssize_t wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -222,6 +211,16 @@ wakealarm_store(struct device *dev, struct device_attribute *attr, } static DEVICE_ATTR_RW(wakealarm); +static struct attribute *rtc_attrs[] = { + &dev_attr_name.attr, + &dev_attr_date.attr, + &dev_attr_time.attr, + &dev_attr_since_epoch.attr, + &dev_attr_max_user_freq.attr, + &dev_attr_hctosys.attr, + &dev_attr_wakealarm.attr, + NULL, +}; /* The reason to trigger an alarm with no process watching it (via sysfs) * is its side effect: waking from a system state like suspend-to-RAM or @@ -236,29 +235,31 @@ static bool rtc_does_wakealarm(struct rtc_device *rtc) return rtc->ops->set_alarm != NULL; } - -void rtc_sysfs_add_device(struct rtc_device *rtc) +static umode_t rtc_attr_is_visible(struct kobject *kobj, + struct attribute *attr, int n) { - int err; + struct device *dev = container_of(kobj, struct device, kobj); + struct rtc_device *rtc = to_rtc_device(dev); + umode_t mode = attr->mode; - /* not all RTCs support both alarms and wakeup */ - if (!rtc_does_wakealarm(rtc)) - return; + if (attr == &dev_attr_wakealarm.attr) + if (!rtc_does_wakealarm(rtc)) + mode = 0; - err = device_create_file(&rtc->dev, &dev_attr_wakealarm); - if (err) - dev_err(rtc->dev.parent, - "failed to create alarm attribute, %d\n", err); + return mode; } -void rtc_sysfs_del_device(struct rtc_device *rtc) -{ - /* REVISIT did we add it successfully? */ - if (rtc_does_wakealarm(rtc)) - device_remove_file(&rtc->dev, &dev_attr_wakealarm); -} +static struct attribute_group rtc_attr_group = { + .is_visible = rtc_attr_is_visible, + .attrs = rtc_attrs, +}; + +static const struct attribute_group *rtc_attr_groups[] = { + &rtc_attr_group, + NULL +}; -void __init rtc_sysfs_init(struct class *rtc_class) +const struct attribute_group **rtc_get_dev_attribute_groups(void) { - rtc_class->dev_groups = rtc_groups; + return rtc_attr_groups; } -- cgit v1.2.3