Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015-2016 Mentor Graphics |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/list.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/spinlock.h> |
Vladimir Zapolskiy | 53f96ce | 2016-10-07 15:37:00 +0300 | [diff] [blame] | 9 | #include <linux/string.h> |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 10 | #include <linux/watchdog.h> |
| 11 | |
Curtis Klein | 7b7d2fd | 2021-02-03 12:11:30 -0800 | [diff] [blame] | 12 | #include "watchdog_core.h" |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 13 | #include "watchdog_pretimeout.h" |
| 14 | |
| 15 | /* Default watchdog pretimeout governor */ |
| 16 | static struct watchdog_governor *default_gov; |
| 17 | |
| 18 | /* The spinlock protects default_gov, wdd->gov and pretimeout_list */ |
| 19 | static DEFINE_SPINLOCK(pretimeout_lock); |
| 20 | |
| 21 | /* List of watchdog devices, which can generate a pretimeout event */ |
| 22 | static LIST_HEAD(pretimeout_list); |
| 23 | |
| 24 | struct watchdog_pretimeout { |
| 25 | struct watchdog_device *wdd; |
| 26 | struct list_head entry; |
| 27 | }; |
| 28 | |
Vladimir Zapolskiy | 53f96ce | 2016-10-07 15:37:00 +0300 | [diff] [blame] | 29 | /* The mutex protects governor list and serializes external interfaces */ |
| 30 | static DEFINE_MUTEX(governor_lock); |
| 31 | |
| 32 | /* List of the registered watchdog pretimeout governors */ |
| 33 | static LIST_HEAD(governor_list); |
| 34 | |
| 35 | struct governor_priv { |
| 36 | struct watchdog_governor *gov; |
| 37 | struct list_head entry; |
| 38 | }; |
| 39 | |
| 40 | static struct governor_priv *find_governor_by_name(const char *gov_name) |
| 41 | { |
| 42 | struct governor_priv *priv; |
| 43 | |
| 44 | list_for_each_entry(priv, &governor_list, entry) |
| 45 | if (sysfs_streq(gov_name, priv->gov->name)) |
| 46 | return priv; |
| 47 | |
| 48 | return NULL; |
| 49 | } |
| 50 | |
Vladimir Zapolskiy | 89873a71 | 2016-10-07 15:39:57 +0300 | [diff] [blame] | 51 | int watchdog_pretimeout_available_governors_get(char *buf) |
| 52 | { |
| 53 | struct governor_priv *priv; |
| 54 | int count = 0; |
| 55 | |
| 56 | mutex_lock(&governor_lock); |
| 57 | |
| 58 | list_for_each_entry(priv, &governor_list, entry) |
Juerg Haefliger | 3bb2178 | 2021-05-11 08:18:12 +0200 | [diff] [blame] | 59 | count += sysfs_emit_at(buf, count, "%s\n", priv->gov->name); |
Vladimir Zapolskiy | 89873a71 | 2016-10-07 15:39:57 +0300 | [diff] [blame] | 60 | |
| 61 | mutex_unlock(&governor_lock); |
| 62 | |
| 63 | return count; |
| 64 | } |
| 65 | |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 66 | int watchdog_pretimeout_governor_get(struct watchdog_device *wdd, char *buf) |
| 67 | { |
| 68 | int count = 0; |
| 69 | |
| 70 | spin_lock_irq(&pretimeout_lock); |
| 71 | if (wdd->gov) |
Juerg Haefliger | 3bb2178 | 2021-05-11 08:18:12 +0200 | [diff] [blame] | 72 | count = sysfs_emit(buf, "%s\n", wdd->gov->name); |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 73 | spin_unlock_irq(&pretimeout_lock); |
| 74 | |
| 75 | return count; |
| 76 | } |
| 77 | |
Vladimir Zapolskiy | 53f96ce | 2016-10-07 15:37:00 +0300 | [diff] [blame] | 78 | int watchdog_pretimeout_governor_set(struct watchdog_device *wdd, |
| 79 | const char *buf) |
| 80 | { |
| 81 | struct governor_priv *priv; |
| 82 | |
| 83 | mutex_lock(&governor_lock); |
| 84 | |
| 85 | priv = find_governor_by_name(buf); |
| 86 | if (!priv) { |
| 87 | mutex_unlock(&governor_lock); |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | |
| 91 | spin_lock_irq(&pretimeout_lock); |
| 92 | wdd->gov = priv->gov; |
| 93 | spin_unlock_irq(&pretimeout_lock); |
| 94 | |
| 95 | mutex_unlock(&governor_lock); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 100 | void watchdog_notify_pretimeout(struct watchdog_device *wdd) |
| 101 | { |
| 102 | unsigned long flags; |
| 103 | |
| 104 | spin_lock_irqsave(&pretimeout_lock, flags); |
| 105 | if (!wdd->gov) { |
| 106 | spin_unlock_irqrestore(&pretimeout_lock, flags); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | wdd->gov->pretimeout(wdd); |
| 111 | spin_unlock_irqrestore(&pretimeout_lock, flags); |
| 112 | } |
| 113 | EXPORT_SYMBOL_GPL(watchdog_notify_pretimeout); |
| 114 | |
| 115 | int watchdog_register_governor(struct watchdog_governor *gov) |
| 116 | { |
| 117 | struct watchdog_pretimeout *p; |
Vladimir Zapolskiy | 53f96ce | 2016-10-07 15:37:00 +0300 | [diff] [blame] | 118 | struct governor_priv *priv; |
| 119 | |
| 120 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 121 | if (!priv) |
| 122 | return -ENOMEM; |
| 123 | |
| 124 | mutex_lock(&governor_lock); |
| 125 | |
| 126 | if (find_governor_by_name(gov->name)) { |
| 127 | mutex_unlock(&governor_lock); |
| 128 | kfree(priv); |
| 129 | return -EBUSY; |
| 130 | } |
| 131 | |
| 132 | priv->gov = gov; |
| 133 | list_add(&priv->entry, &governor_list); |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 134 | |
Vladimir Zapolskiy | da0d12f | 2016-10-07 15:39:56 +0300 | [diff] [blame] | 135 | if (!strncmp(gov->name, WATCHDOG_PRETIMEOUT_DEFAULT_GOV, |
| 136 | WATCHDOG_GOV_NAME_MAXLEN)) { |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 137 | spin_lock_irq(&pretimeout_lock); |
| 138 | default_gov = gov; |
| 139 | |
| 140 | list_for_each_entry(p, &pretimeout_list, entry) |
| 141 | if (!p->wdd->gov) |
| 142 | p->wdd->gov = default_gov; |
| 143 | spin_unlock_irq(&pretimeout_lock); |
| 144 | } |
| 145 | |
Vladimir Zapolskiy | 53f96ce | 2016-10-07 15:37:00 +0300 | [diff] [blame] | 146 | mutex_unlock(&governor_lock); |
| 147 | |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | EXPORT_SYMBOL(watchdog_register_governor); |
| 151 | |
| 152 | void watchdog_unregister_governor(struct watchdog_governor *gov) |
| 153 | { |
| 154 | struct watchdog_pretimeout *p; |
Vladimir Zapolskiy | 53f96ce | 2016-10-07 15:37:00 +0300 | [diff] [blame] | 155 | struct governor_priv *priv, *t; |
| 156 | |
| 157 | mutex_lock(&governor_lock); |
| 158 | |
| 159 | list_for_each_entry_safe(priv, t, &governor_list, entry) { |
| 160 | if (priv->gov == gov) { |
| 161 | list_del(&priv->entry); |
| 162 | kfree(priv); |
| 163 | break; |
| 164 | } |
| 165 | } |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 166 | |
| 167 | spin_lock_irq(&pretimeout_lock); |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 168 | list_for_each_entry(p, &pretimeout_list, entry) |
| 169 | if (p->wdd->gov == gov) |
| 170 | p->wdd->gov = default_gov; |
| 171 | spin_unlock_irq(&pretimeout_lock); |
Vladimir Zapolskiy | 53f96ce | 2016-10-07 15:37:00 +0300 | [diff] [blame] | 172 | |
| 173 | mutex_unlock(&governor_lock); |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 174 | } |
| 175 | EXPORT_SYMBOL(watchdog_unregister_governor); |
| 176 | |
| 177 | int watchdog_register_pretimeout(struct watchdog_device *wdd) |
| 178 | { |
| 179 | struct watchdog_pretimeout *p; |
| 180 | |
Curtis Klein | 7b7d2fd | 2021-02-03 12:11:30 -0800 | [diff] [blame] | 181 | if (!watchdog_have_pretimeout(wdd)) |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 182 | return 0; |
| 183 | |
| 184 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 185 | if (!p) |
| 186 | return -ENOMEM; |
| 187 | |
| 188 | spin_lock_irq(&pretimeout_lock); |
| 189 | list_add(&p->entry, &pretimeout_list); |
| 190 | p->wdd = wdd; |
| 191 | wdd->gov = default_gov; |
| 192 | spin_unlock_irq(&pretimeout_lock); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | void watchdog_unregister_pretimeout(struct watchdog_device *wdd) |
| 198 | { |
| 199 | struct watchdog_pretimeout *p, *t; |
| 200 | |
Curtis Klein | 7b7d2fd | 2021-02-03 12:11:30 -0800 | [diff] [blame] | 201 | if (!watchdog_have_pretimeout(wdd)) |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 202 | return; |
| 203 | |
| 204 | spin_lock_irq(&pretimeout_lock); |
| 205 | wdd->gov = NULL; |
| 206 | |
| 207 | list_for_each_entry_safe(p, t, &pretimeout_list, entry) { |
| 208 | if (p->wdd == wdd) { |
| 209 | list_del(&p->entry); |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | spin_unlock_irq(&pretimeout_lock); |
| 214 | |
| 215 | kfree(p); |
| 216 | } |