Commit 53f96cee authored by Vladimir Zapolskiy's avatar Vladimir Zapolskiy Committed by Wim Van Sebroeck

watchdog: pretimeout: add option to select a pretimeout governor in runtime

The change converts watchdog device attribute "pretimeout_governor" from
read-only to read-write type to allow users to select a desirable
watchdog pretimeout governor in runtime, e.g.

  % echo -n panic > /sys/..../watchdog/watchdog0/pretimeout

To get this working a list of registered pretimeout governors is created
and a new helper function watchdog_pretimeout_governor_set() is exported
to watchdog_dev.c.

If a selected governor is gone, a watchdog device pretimeout notification
is delegated to a default built-in pretimeout governor.
Signed-off-by: default avatarVladimir Zapolskiy <vladimir_zapolskiy@mentor.com>
Reviewed-by: default avatarGuenter Roeck <linux@roeck-us.net>
Reviewed-by: default avatarWolfram Sang <wsa+renesas@sang-engineering.com>
Tested-by: default avatarWolfram Sang <wsa+renesas@sang-engineering.com>
Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
Signed-off-by: default avatarWim Van Sebroeck <wim@iguana.be>
parent da0d12ff
......@@ -497,7 +497,20 @@ static ssize_t pretimeout_governor_show(struct device *dev,
return watchdog_pretimeout_governor_get(wdd, buf);
}
static DEVICE_ATTR_RO(pretimeout_governor);
static ssize_t pretimeout_governor_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct watchdog_device *wdd = dev_get_drvdata(dev);
int ret = watchdog_pretimeout_governor_set(wdd, buf);
if (!ret)
ret = count;
return ret;
}
static DEVICE_ATTR_RW(pretimeout_governor);
static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr,
int n)
......
......@@ -11,6 +11,7 @@
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/watchdog.h>
#include "watchdog_pretimeout.h"
......@@ -29,6 +30,28 @@ struct watchdog_pretimeout {
struct list_head entry;
};
/* The mutex protects governor list and serializes external interfaces */
static DEFINE_MUTEX(governor_lock);
/* List of the registered watchdog pretimeout governors */
static LIST_HEAD(governor_list);
struct governor_priv {
struct watchdog_governor *gov;
struct list_head entry;
};
static struct governor_priv *find_governor_by_name(const char *gov_name)
{
struct governor_priv *priv;
list_for_each_entry(priv, &governor_list, entry)
if (sysfs_streq(gov_name, priv->gov->name))
return priv;
return NULL;
}
int watchdog_pretimeout_governor_get(struct watchdog_device *wdd, char *buf)
{
int count = 0;
......@@ -41,6 +64,28 @@ int watchdog_pretimeout_governor_get(struct watchdog_device *wdd, char *buf)
return count;
}
int watchdog_pretimeout_governor_set(struct watchdog_device *wdd,
const char *buf)
{
struct governor_priv *priv;
mutex_lock(&governor_lock);
priv = find_governor_by_name(buf);
if (!priv) {
mutex_unlock(&governor_lock);
return -EINVAL;
}
spin_lock_irq(&pretimeout_lock);
wdd->gov = priv->gov;
spin_unlock_irq(&pretimeout_lock);
mutex_unlock(&governor_lock);
return 0;
}
void watchdog_notify_pretimeout(struct watchdog_device *wdd)
{
unsigned long flags;
......@@ -59,6 +104,22 @@ EXPORT_SYMBOL_GPL(watchdog_notify_pretimeout);
int watchdog_register_governor(struct watchdog_governor *gov)
{
struct watchdog_pretimeout *p;
struct governor_priv *priv;
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
mutex_lock(&governor_lock);
if (find_governor_by_name(gov->name)) {
mutex_unlock(&governor_lock);
kfree(priv);
return -EBUSY;
}
priv->gov = gov;
list_add(&priv->entry, &governor_list);
if (!strncmp(gov->name, WATCHDOG_PRETIMEOUT_DEFAULT_GOV,
WATCHDOG_GOV_NAME_MAXLEN)) {
......@@ -71,6 +132,8 @@ int watchdog_register_governor(struct watchdog_governor *gov)
spin_unlock_irq(&pretimeout_lock);
}
mutex_unlock(&governor_lock);
return 0;
}
EXPORT_SYMBOL(watchdog_register_governor);
......@@ -78,12 +141,25 @@ EXPORT_SYMBOL(watchdog_register_governor);
void watchdog_unregister_governor(struct watchdog_governor *gov)
{
struct watchdog_pretimeout *p;
struct governor_priv *priv, *t;
mutex_lock(&governor_lock);
list_for_each_entry_safe(priv, t, &governor_list, entry) {
if (priv->gov == gov) {
list_del(&priv->entry);
kfree(priv);
break;
}
}
spin_lock_irq(&pretimeout_lock);
list_for_each_entry(p, &pretimeout_list, entry)
if (p->wdd->gov == gov)
p->wdd->gov = default_gov;
spin_unlock_irq(&pretimeout_lock);
mutex_unlock(&governor_lock);
}
EXPORT_SYMBOL(watchdog_unregister_governor);
......
......@@ -19,6 +19,8 @@ void watchdog_unregister_governor(struct watchdog_governor *gov);
int watchdog_register_pretimeout(struct watchdog_device *wdd);
void watchdog_unregister_pretimeout(struct watchdog_device *wdd);
int watchdog_pretimeout_governor_get(struct watchdog_device *wdd, char *buf);
int watchdog_pretimeout_governor_set(struct watchdog_device *wdd,
const char *buf);
#if IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_DEFAULT_GOV_NOOP)
#define WATCHDOG_PRETIMEOUT_DEFAULT_GOV "noop"
......@@ -41,6 +43,12 @@ static inline int watchdog_pretimeout_governor_get(struct watchdog_device *wdd,
{
return -EINVAL;
}
static inline int watchdog_pretimeout_governor_set(struct watchdog_device *wdd,
const char *buf)
{
return -EINVAL;
}
#endif
#endif
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