Skip to content

Commit 72139df

Browse files
haokexinWim Van Sebroeck
authored andcommitted
watchdog: Fix the race between the release of watchdog_core_data and cdev
The struct cdev is embedded in the struct watchdog_core_data. In the current code, we manage the watchdog_core_data with a kref, but the cdev is manged by a kobject. There is no any relationship between this kref and kobject. So it is possible that the watchdog_core_data is freed before the cdev is entirely released. We can easily get the following call trace with CONFIG_DEBUG_KOBJECT_RELEASE and CONFIG_DEBUG_OBJECTS_TIMERS enabled. ODEBUG: free active (active state 0) object type: timer_list hint: delayed_work_timer_fn+0x0/0x38 WARNING: CPU: 23 PID: 1028 at lib/debugobjects.c:481 debug_print_object+0xb0/0xf0 Modules linked in: softdog(-) deflate ctr twofish_generic twofish_common camellia_generic serpent_generic blowfish_generic blowfish_common cast5_generic cast_common cmac xcbc af_key sch_fq_codel openvswitch nsh nf_conncount nf_nat nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 CPU: 23 PID: 1028 Comm: modprobe Not tainted 5.3.0-next-20190924-yoctodev-standard+ torvalds#180 Hardware name: Marvell OcteonTX CN96XX board (DT) pstate: 00400009 (nzcv daif +PAN -UAO) pc : debug_print_object+0xb0/0xf0 lr : debug_print_object+0xb0/0xf0 sp : ffff80001cbcfc70 x29: ffff80001cbcfc70 x28: ffff800010ea2128 x27: ffff800010bad000 x26: 0000000000000000 x25: ffff80001103c640 x24: ffff80001107b268 x23: ffff800010bad9e8 x22: ffff800010ea2128 x21: ffff000bc2c62af8 x20: ffff80001103c600 x19: ffff800010e867d8 x18: 0000000000000060 x17: 0000000000000000 x16: 0000000000000000 x15: ffff000bd7240470 x14: 6e6968207473696c x13: 5f72656d6974203a x12: 6570797420746365 x11: 6a626f2029302065 x10: 7461747320657669 x9 : 7463612820657669 x8 : 3378302f3078302b x7 : 0000000000001d7a x6 : ffff800010fd5889 x5 : 0000000000000000 x4 : 0000000000000000 x3 : 0000000000000000 x2 : ffff000bff948548 x1 : 276a1c9e1edc2300 x0 : 0000000000000000 Call trace: debug_print_object+0xb0/0xf0 debug_check_no_obj_freed+0x1e8/0x210 kfree+0x1b8/0x368 watchdog_cdev_unregister+0x88/0xc8 watchdog_dev_unregister+0x38/0x48 watchdog_unregister_device+0xa8/0x100 softdog_exit+0x18/0xfec4 [softdog] __arm64_sys_delete_module+0x174/0x200 el0_svc_handler+0xd0/0x1c8 el0_svc+0x8/0xc This is a common issue when using cdev embedded in a struct. Fortunately, we already have a mechanism to solve this kind of issue. Please see commit 233ed09 ("chardev: add helper function to register char devs with a struct device") for more detail. In this patch, we choose to embed the struct device into the watchdog_core_data, and use the API provided by the commit 233ed09 to make sure that the release of watchdog_core_data and cdev are in sequence. Signed-off-by: Kevin Hao <[email protected]> Reviewed-by: Guenter Roeck <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Guenter Roeck <[email protected]> Signed-off-by: Wim Van Sebroeck <[email protected]>
1 parent b6276d4 commit 72139df

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

drivers/watchdog/watchdog_dev.c

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <linux/init.h> /* For __init/__exit/... */
3535
#include <linux/hrtimer.h> /* For hrtimers */
3636
#include <linux/kernel.h> /* For printk/panic/... */
37-
#include <linux/kref.h> /* For data references */
3837
#include <linux/kthread.h> /* For kthread_work */
3938
#include <linux/miscdevice.h> /* For handling misc devices */
4039
#include <linux/module.h> /* For module stuff/... */
@@ -52,14 +51,14 @@
5251

5352
/*
5453
* struct watchdog_core_data - watchdog core internal data
55-
* @kref: Reference count.
54+
* @dev: The watchdog's internal device
5655
* @cdev: The watchdog's Character device.
5756
* @wdd: Pointer to watchdog device.
5857
* @lock: Lock for watchdog core.
5958
* @status: Watchdog core internal status bits.
6059
*/
6160
struct watchdog_core_data {
62-
struct kref kref;
61+
struct device dev;
6362
struct cdev cdev;
6463
struct watchdog_device *wdd;
6564
struct mutex lock;
@@ -839,7 +838,7 @@ static int watchdog_open(struct inode *inode, struct file *file)
839838
file->private_data = wd_data;
840839

841840
if (!hw_running)
842-
kref_get(&wd_data->kref);
841+
get_device(&wd_data->dev);
843842

844843
/*
845844
* open_timeout only applies for the first open from
@@ -860,11 +859,11 @@ static int watchdog_open(struct inode *inode, struct file *file)
860859
return err;
861860
}
862861

863-
static void watchdog_core_data_release(struct kref *kref)
862+
static void watchdog_core_data_release(struct device *dev)
864863
{
865864
struct watchdog_core_data *wd_data;
866865

867-
wd_data = container_of(kref, struct watchdog_core_data, kref);
866+
wd_data = container_of(dev, struct watchdog_core_data, dev);
868867

869868
kfree(wd_data);
870869
}
@@ -924,7 +923,7 @@ static int watchdog_release(struct inode *inode, struct file *file)
924923
*/
925924
if (!running) {
926925
module_put(wd_data->cdev.owner);
927-
kref_put(&wd_data->kref, watchdog_core_data_release);
926+
put_device(&wd_data->dev);
928927
}
929928
return 0;
930929
}
@@ -943,25 +942,29 @@ static struct miscdevice watchdog_miscdev = {
943942
.fops = &watchdog_fops,
944943
};
945944

945+
static struct class watchdog_class = {
946+
.name = "watchdog",
947+
.owner = THIS_MODULE,
948+
.dev_groups = wdt_groups,
949+
};
950+
946951
/*
947952
* watchdog_cdev_register: register watchdog character device
948953
* @wdd: watchdog device
949-
* @devno: character device number
950954
*
951955
* Register a watchdog character device including handling the legacy
952956
* /dev/watchdog node. /dev/watchdog is actually a miscdevice and
953957
* thus we set it up like that.
954958
*/
955959

956-
static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno)
960+
static int watchdog_cdev_register(struct watchdog_device *wdd)
957961
{
958962
struct watchdog_core_data *wd_data;
959963
int err;
960964

961965
wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);
962966
if (!wd_data)
963967
return -ENOMEM;
964-
kref_init(&wd_data->kref);
965968
mutex_init(&wd_data->lock);
966969

967970
wd_data->wdd = wdd;
@@ -990,23 +993,33 @@ static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno)
990993
}
991994
}
992995

996+
device_initialize(&wd_data->dev);
997+
wd_data->dev.devt = MKDEV(MAJOR(watchdog_devt), wdd->id);
998+
wd_data->dev.class = &watchdog_class;
999+
wd_data->dev.parent = wdd->parent;
1000+
wd_data->dev.groups = wdd->groups;
1001+
wd_data->dev.release = watchdog_core_data_release;
1002+
dev_set_drvdata(&wd_data->dev, wdd);
1003+
dev_set_name(&wd_data->dev, "watchdog%d", wdd->id);
1004+
9931005
/* Fill in the data structures */
9941006
cdev_init(&wd_data->cdev, &watchdog_fops);
995-
wd_data->cdev.owner = wdd->ops->owner;
9961007

9971008
/* Add the device */
998-
err = cdev_add(&wd_data->cdev, devno, 1);
1009+
err = cdev_device_add(&wd_data->cdev, &wd_data->dev);
9991010
if (err) {
10001011
pr_err("watchdog%d unable to add device %d:%d\n",
10011012
wdd->id, MAJOR(watchdog_devt), wdd->id);
10021013
if (wdd->id == 0) {
10031014
misc_deregister(&watchdog_miscdev);
10041015
old_wd_data = NULL;
1005-
kref_put(&wd_data->kref, watchdog_core_data_release);
1016+
put_device(&wd_data->dev);
10061017
}
10071018
return err;
10081019
}
10091020

1021+
wd_data->cdev.owner = wdd->ops->owner;
1022+
10101023
/* Record time of most recent heartbeat as 'just before now'. */
10111024
wd_data->last_hw_keepalive = ktime_sub(ktime_get(), 1);
10121025
watchdog_set_open_deadline(wd_data);
@@ -1017,7 +1030,7 @@ static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno)
10171030
*/
10181031
if (watchdog_hw_running(wdd)) {
10191032
__module_get(wdd->ops->owner);
1020-
kref_get(&wd_data->kref);
1033+
get_device(&wd_data->dev);
10211034
if (handle_boot_enabled)
10221035
hrtimer_start(&wd_data->timer, 0, HRTIMER_MODE_REL);
10231036
else
@@ -1040,7 +1053,7 @@ static void watchdog_cdev_unregister(struct watchdog_device *wdd)
10401053
{
10411054
struct watchdog_core_data *wd_data = wdd->wd_data;
10421055

1043-
cdev_del(&wd_data->cdev);
1056+
cdev_device_del(&wd_data->cdev, &wd_data->dev);
10441057
if (wdd->id == 0) {
10451058
misc_deregister(&watchdog_miscdev);
10461059
old_wd_data = NULL;
@@ -1059,15 +1072,9 @@ static void watchdog_cdev_unregister(struct watchdog_device *wdd)
10591072
hrtimer_cancel(&wd_data->timer);
10601073
kthread_cancel_work_sync(&wd_data->work);
10611074

1062-
kref_put(&wd_data->kref, watchdog_core_data_release);
1075+
put_device(&wd_data->dev);
10631076
}
10641077

1065-
static struct class watchdog_class = {
1066-
.name = "watchdog",
1067-
.owner = THIS_MODULE,
1068-
.dev_groups = wdt_groups,
1069-
};
1070-
10711078
static int watchdog_reboot_notifier(struct notifier_block *nb,
10721079
unsigned long code, void *data)
10731080
{
@@ -1098,35 +1105,23 @@ static int watchdog_reboot_notifier(struct notifier_block *nb,
10981105

10991106
int watchdog_dev_register(struct watchdog_device *wdd)
11001107
{
1101-
struct device *dev;
1102-
dev_t devno;
11031108
int ret;
11041109

1105-
devno = MKDEV(MAJOR(watchdog_devt), wdd->id);
1106-
1107-
ret = watchdog_cdev_register(wdd, devno);
1110+
ret = watchdog_cdev_register(wdd);
11081111
if (ret)
11091112
return ret;
11101113

1111-
dev = device_create_with_groups(&watchdog_class, wdd->parent,
1112-
devno, wdd, wdd->groups,
1113-
"watchdog%d", wdd->id);
1114-
if (IS_ERR(dev)) {
1115-
watchdog_cdev_unregister(wdd);
1116-
return PTR_ERR(dev);
1117-
}
1118-
11191114
ret = watchdog_register_pretimeout(wdd);
11201115
if (ret) {
1121-
device_destroy(&watchdog_class, devno);
11221116
watchdog_cdev_unregister(wdd);
11231117
return ret;
11241118
}
11251119

11261120
if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) {
11271121
wdd->reboot_nb.notifier_call = watchdog_reboot_notifier;
11281122

1129-
ret = devm_register_reboot_notifier(dev, &wdd->reboot_nb);
1123+
ret = devm_register_reboot_notifier(&wdd->wd_data->dev,
1124+
&wdd->reboot_nb);
11301125
if (ret) {
11311126
pr_err("watchdog%d: Cannot register reboot notifier (%d)\n",
11321127
wdd->id, ret);
@@ -1148,7 +1143,6 @@ int watchdog_dev_register(struct watchdog_device *wdd)
11481143
void watchdog_dev_unregister(struct watchdog_device *wdd)
11491144
{
11501145
watchdog_unregister_pretimeout(wdd);
1151-
device_destroy(&watchdog_class, wdd->wd_data->cdev.dev);
11521146
watchdog_cdev_unregister(wdd);
11531147
}
11541148

0 commit comments

Comments
 (0)