Skip to contentSkip to conversation

watchdog: dw_wdt: Use a mutex, not a spinlock

PATCH In review Doug Anderson 3 participants 7 messages May 7, 2015 – May 8, 2015 Watchdog device drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 watchdog: dw_wdt: Use a mutex, not a spinlock

Right now the dw_wdt uses a spinlock to protect dw_wdt_open(). The problem is that while holding the spinlock we call:
-> dw_wdt_set_top()
-> dw_wdt_top_in_seconds()

      -> clk_get_rate()
         -> clk_prepare_lock()
            -> mutex_lock()

Locking a mutex while holding a spinlock is not allowed and leads to warnings like "BUG: spinlock wrong CPU on CPU#1", among other problems.

There's no reason to use a spinlock, so switch to a mutex.

---
 drivers/watchdog/dw_wdt.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/watchdog/dw_wdt.c +5 -5
index d0bb949..3fa2f19 100644
@@ -30,12 +30,12 @@
 #include <linux/miscdevice.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
+#include <linux/mutex.h>
 #include <linux/notifier.h>
 #include <linux/of.h>
 #include <linux/pm.h>
 #include <linux/platform_device.h>
 #include <linux/reboot.h>
-#include <linux/spinlock.h>
 #include <linux/timer.h>
 #include <linux/uaccess.h>
 #include <linux/watchdog.h>
@@ -61,7 +61,7 @@ MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
 #define WDT_TIMEOUT		(HZ / 2)
 
 static struct {
-	spinlock_t		lock;
+	struct mutex		lock;
 	void __iomem		*regs;
 	struct clk		*clk;
 	unsigned long		in_use;
@@ -177,7 +177,7 @@ static int dw_wdt_open(struct inode *inode, struct file *filp)
 	/* Make sure we don't get unloaded. */
 	__module_get(THIS_MODULE);
 
-	spin_lock(&dw_wdt.lock);
+	mutex_lock(&dw_wdt.lock);
 	if (!dw_wdt_is_enabled()) {
 		/*
 		 * The watchdog is not currently enabled. Set the timeout to
@@ -190,7 +190,7 @@ static int dw_wdt_open(struct inode *inode, struct file *filp)
 
 	dw_wdt_set_next_heartbeat();
 
-	spin_unlock(&dw_wdt.lock);
+	mutex_unlock(&dw_wdt.lock);
 
 	return nonseekable_open(inode, filp);
 }
@@ -348,7 +348,7 @@ static int dw_wdt_drv_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	spin_lock_init(&dw_wdt.lock);
+	mutex_init(&dw_wdt.lock);
 
 	ret = misc_register(&dw_wdt_miscdev);
 	if (ret)
Signature · 1 line
2.2.0.rc0.207.ga3a616c
#1 lore raw

PATCH 2/2 watchdog: dw_wdt: keepalive the watchdog at write time

If you've got code that does this in a tight loop
1. Open watchdog
2. Send 'expect close'
3. Close watchdog
...you'll eventually trigger a watchdog reset. You can reproduce this by using daisydog (1) and running:
while true; do daisydog -c > /dev/null; done

The problem is that each time you write to the watchdog for 'expect close' it moves the timer .5 seconds out. The timer thus never fires and never pats the watchdog for you.

1: http://git.chromium.org/gitweb/?p=chromiumos/third_party/daisydog.git

---
 drivers/watchdog/dw_wdt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/watchdog/dw_wdt.c +1 -0
index 3fa2f19..ff5d734 100644
@@ -220,6 +220,7 @@ static ssize_t dw_wdt_write(struct file *filp, const char __user *buf,
 	}
 
 	dw_wdt_set_next_heartbeat();
+	dw_wdt_keepalive();
 	mod_timer(&dw_wdt.timer, jiffies + WDT_TIMEOUT);
 
 	return len;
Signature · 1 line
2.2.0.rc0.207.ga3a616c
maintainer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] watchdog: dw_wdt: keepalive the watchdog at write time

Quoted 8 lines · Doug Anderson

If you've got code that does this in a tight loop
1. Open watchdog
2. Send 'expect close'
3. Close watchdog
...you'll eventually trigger a watchdog reset. You can reproduce this by using daisydog (1) and running:
while true; do daisydog -c > /dev/null; done

Quoted 7 lines

The problem is that each time you write to the watchdog for 'expect close' it moves the timer .5 seconds out. The timer thus never fires and never pats the watchdog for you.

1: http://git.chromium.org/gitweb/?p=chromiumos/third_party/daisydog.git

Tested-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] watchdog: dw_wdt: keepalive the watchdog at write time

In reply to Doug Anderson
Quoted 24 lines · On Thu, 7 May 2015 15:09:24 -0700 Doug Anderson <[email protected]> wrote:

If you've got code that does this in a tight loop
1. Open watchdog
2. Send 'expect close'
3. Close watchdog
...you'll eventually trigger a watchdog reset. You can reproduce this by using daisydog (1) and running:
while true; do daisydog -c > /dev/null; done

The problem is that each time you write to the watchdog for 'expect close' it moves the timer .5 seconds out. The timer thus never fires and never pats the watchdog for you.

1: http://git.chromium.org/gitweb/?p=chromiumos/third_party/daisydog.git

---
 drivers/watchdog/dw_wdt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/watchdog/dw_wdt.c +0 -0
index 3fa2f19..ff5d734 100644

drivers/watchdog/dw_wdt.c static ssize_t dw_wdt_write(struct file *filp, const char __user *buf, · line 226

Quoted 7 lines
drivers/watchdog/dw_wdt.c +1 -0
@@ -220,6 +220,7 @@ static ssize_t dw_wdt_write(struct file *filp, const char __user *buf,
 	}
 
 	dw_wdt_set_next_heartbeat();
+	dw_wdt_keepalive();
 	mod_timer(&dw_wdt.timer, jiffies + WDT_TIMEOUT);
 
 	return len;
maintainer Reviewed-by on v1 1/2
#4 lore raw
In reply to Doug Anderson
Quoted 8 lines · Doug Anderson

Right now the dw_wdt uses a spinlock to protect dw_wdt_open(). The problem is that while holding the spinlock we call:
-> dw_wdt_set_top()

    -> dw_wdt_top_in_seconds()
       -> clk_get_rate()
          -> clk_prepare_lock()
             -> mutex_lock()
Quoted 7 lines

Locking a mutex while holding a spinlock is not allowed and leads to warnings like "BUG: spinlock wrong CPU on CPU#1", among other problems.

There's no reason to use a spinlock, so switch to a mutex.

#5 lore raw

Hi,

Quoted 14 lines · Guenter Roeck
Quoted 13 lines · Doug Anderson

Right now the dw_wdt uses a spinlock to protect dw_wdt_open(). The problem is that while holding the spinlock we call:
-> dw_wdt_set_top()

    -> dw_wdt_top_in_seconds()
       -> clk_get_rate()
          -> clk_prepare_lock()
             -> mutex_lock()

Locking a mutex while holding a spinlock is not allowed and leads to warnings like "BUG: spinlock wrong CPU on CPU#1", among other problems.

Quoted 6 lines
Quoted 3 lines

There's no reason to use a spinlock, so switch to a mutex.

As Dmitry pointed out in another context, and even better fix is to just remove the spinlock altogether. I'll send up v2...

-Doug

Tested-by on v1 1/2
#6 lore raw
In reply to Doug Anderson
Quoted 65 lines · On Thu, 7 May 2015 15:09:23 -0700 Doug Anderson <[email protected]> wrote:

Right now the dw_wdt uses a spinlock to protect dw_wdt_open(). The problem is that while holding the spinlock we call:
-> dw_wdt_set_top()
-> dw_wdt_top_in_seconds()

      -> clk_get_rate()
         -> clk_prepare_lock()
            -> mutex_lock()

Locking a mutex while holding a spinlock is not allowed and leads to warnings like "BUG: spinlock wrong CPU on CPU#1", among other problems.

There's no reason to use a spinlock, so switch to a mutex.

---
 drivers/watchdog/dw_wdt.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/watchdog/dw_wdt.c +4 -4
index d0bb949..3fa2f19 100644
@@ -30,12 +30,12 @@
 #include <linux/miscdevice.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
+#include <linux/mutex.h>
 #include <linux/notifier.h>
 #include <linux/of.h>
 #include <linux/pm.h>
 #include <linux/platform_device.h>
 #include <linux/reboot.h>
-#include <linux/spinlock.h>
 #include <linux/timer.h>
 #include <linux/uaccess.h>
 #include <linux/watchdog.h>
@@ -61,7 +61,7 @@ MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
 #define WDT_TIMEOUT		(HZ / 2)
 
 static struct {
-	spinlock_t		lock;
+	struct mutex		lock;
 	void __iomem		*regs;
 	struct clk		*clk;
 	unsigned long		in_use;
@@ -177,7 +177,7 @@ static int dw_wdt_open(struct inode *inode, struct file *filp)
 	/* Make sure we don't get unloaded. */
 	__module_get(THIS_MODULE);
 
-	spin_lock(&dw_wdt.lock);
+	mutex_lock(&dw_wdt.lock);
 	if (!dw_wdt_is_enabled()) {
 		/*
 		 * The watchdog is not currently enabled. Set the timeout to
@@ -190,7 +190,7 @@ static int dw_wdt_open(struct inode *inode, struct file *filp)
 
 	dw_wdt_set_next_heartbeat();
 
-	spin_unlock(&dw_wdt.lock);
+	mutex_unlock(&dw_wdt.lock);
 
 	return nonseekable_open(inode, filp);
 }

drivers/watchdog/dw_wdt.c static int dw_wdt_drv_probe(struct platform_device *pdev) · line 354

Quoted 8 lines
drivers/watchdog/dw_wdt.c +1 -1
@@ -348,7 +348,7 @@ static int dw_wdt_drv_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	spin_lock_init(&dw_wdt.lock);
+	mutex_init(&dw_wdt.lock);
 
 	ret = misc_register(&dw_wdt_miscdev);
 	if (ret)

Tested on marvell BG2Q SoC, no issue found. So