009-6-watchdog-Separate-and-maintain-variables-based-on-variable-lifetime.patch 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. From b4ffb1909843b28f3b1b60197d517b123b7a9b66 Mon Sep 17 00:00:00 2001
  2. From: Guenter Roeck <linux@roeck-us.net>
  3. Date: Fri, 25 Dec 2015 16:01:42 -0800
  4. Subject: watchdog: Separate and maintain variables based on variable lifetime
  5. All variables required by the watchdog core to manage a watchdog are
  6. currently stored in struct watchdog_device. The lifetime of those
  7. variables is determined by the watchdog driver. However, the lifetime
  8. of variables used by the watchdog core differs from the lifetime of
  9. struct watchdog_device. To remedy this situation, watchdog drivers
  10. can implement ref and unref callbacks, to be used by the watchdog
  11. core to lock struct watchdog_device in memory.
  12. While this solves the immediate problem, it depends on watchdog drivers
  13. to actually implement the ref/unref callbacks. This is error prone,
  14. often not implemented in the first place, or not implemented correctly.
  15. To solve the problem without requiring driver support, split the variables
  16. in struct watchdog_device into two data structures - one for variables
  17. associated with the watchdog driver, one for variables associated with
  18. the watchdog core. With this approach, the watchdog core can keep track
  19. of its variable lifetime and no longer depends on ref/unref callbacks
  20. in the driver. As a side effect, some of the variables originally in
  21. struct watchdog_driver are now private to the watchdog core and no longer
  22. visible in watchdog drivers.
  23. As a side effect of the changes made, an ioctl will now always fail
  24. with -ENODEV after a watchdog device was unregistered with the character
  25. device still open. Previously, it would only fail with -ENODEV in some
  26. situations. Also, ioctl operations are now atomic from driver perspective.
  27. With this change, it is now guaranteed that the driver will not unregister
  28. a watchdog between a timeout change and the subsequent ping.
  29. The 'ref' and 'unref' callbacks in struct watchdog_driver are no longer
  30. used and marked as deprecated.
  31. Signed-off-by: Guenter Roeck <linux@roeck-us.net>
  32. Signed-off-by: Wim Van Sebroeck <wim@iguana.be>
  33. ---
  34. Documentation/watchdog/watchdog-kernel-api.txt | 45 +--
  35. drivers/watchdog/watchdog_core.c | 2 -
  36. drivers/watchdog/watchdog_dev.c | 383 +++++++++++++------------
  37. include/linux/watchdog.h | 22 +-
  38. 4 files changed, 218 insertions(+), 234 deletions(-)
  39. --- a/Documentation/watchdog/watchdog-kernel-api.txt
  40. +++ b/Documentation/watchdog/watchdog-kernel-api.txt
  41. @@ -44,7 +44,6 @@ The watchdog device structure looks like
  42. struct watchdog_device {
  43. int id;
  44. - struct cdev cdev;
  45. struct device *dev;
  46. struct device *parent;
  47. const struct watchdog_info *info;
  48. @@ -56,7 +55,7 @@ struct watchdog_device {
  49. struct notifier_block reboot_nb;
  50. struct notifier_block restart_nb;
  51. void *driver_data;
  52. - struct mutex lock;
  53. + struct watchdog_core_data *wd_data;
  54. unsigned long status;
  55. struct list_head deferred;
  56. };
  57. @@ -66,8 +65,6 @@ It contains following fields:
  58. /dev/watchdog0 cdev (dynamic major, minor 0) as well as the old
  59. /dev/watchdog miscdev. The id is set automatically when calling
  60. watchdog_register_device.
  61. -* cdev: cdev for the dynamic /dev/watchdog<id> device nodes. This
  62. - field is also populated by watchdog_register_device.
  63. * dev: device under the watchdog class (created by watchdog_register_device).
  64. * parent: set this to the parent device (or NULL) before calling
  65. watchdog_register_device.
  66. @@ -89,11 +86,10 @@ It contains following fields:
  67. * driver_data: a pointer to the drivers private data of a watchdog device.
  68. This data should only be accessed via the watchdog_set_drvdata and
  69. watchdog_get_drvdata routines.
  70. -* lock: Mutex for WatchDog Timer Driver Core internal use only.
  71. +* wd_data: a pointer to watchdog core internal data.
  72. * status: this field contains a number of status bits that give extra
  73. information about the status of the device (Like: is the watchdog timer
  74. - running/active, is the nowayout bit set, is the device opened via
  75. - the /dev/watchdog interface or not, ...).
  76. + running/active, or is the nowayout bit set).
  77. * deferred: entry in wtd_deferred_reg_list which is used to
  78. register early initialized watchdogs.
  79. @@ -110,8 +106,8 @@ struct watchdog_ops {
  80. int (*set_timeout)(struct watchdog_device *, unsigned int);
  81. unsigned int (*get_timeleft)(struct watchdog_device *);
  82. int (*restart)(struct watchdog_device *);
  83. - void (*ref)(struct watchdog_device *);
  84. - void (*unref)(struct watchdog_device *);
  85. + void (*ref)(struct watchdog_device *) __deprecated;
  86. + void (*unref)(struct watchdog_device *) __deprecated;
  87. long (*ioctl)(struct watchdog_device *, unsigned int, unsigned long);
  88. };
  89. @@ -120,20 +116,6 @@ driver's operations. This module owner w
  90. the watchdog is active. (This to avoid a system crash when you unload the
  91. module and /dev/watchdog is still open).
  92. -If the watchdog_device struct is dynamically allocated, just locking the module
  93. -is not enough and a driver also needs to define the ref and unref operations to
  94. -ensure the structure holding the watchdog_device does not go away.
  95. -
  96. -The simplest (and usually sufficient) implementation of this is to:
  97. -1) Add a kref struct to the same structure which is holding the watchdog_device
  98. -2) Define a release callback for the kref which frees the struct holding both
  99. -3) Call kref_init on this kref *before* calling watchdog_register_device()
  100. -4) Define a ref operation calling kref_get on this kref
  101. -5) Define a unref operation calling kref_put on this kref
  102. -6) When it is time to cleanup:
  103. - * Do not kfree() the struct holding both, the last kref_put will do this!
  104. - * *After* calling watchdog_unregister_device() call kref_put on the kref
  105. -
  106. Some operations are mandatory and some are optional. The mandatory operations
  107. are:
  108. * start: this is a pointer to the routine that starts the watchdog timer
  109. @@ -176,34 +158,21 @@ they are supported. These optional routi
  110. * get_timeleft: this routines returns the time that's left before a reset.
  111. * restart: this routine restarts the machine. It returns 0 on success or a
  112. negative errno code for failure.
  113. -* ref: the operation that calls kref_get on the kref of a dynamically
  114. - allocated watchdog_device struct.
  115. -* unref: the operation that calls kref_put on the kref of a dynamically
  116. - allocated watchdog_device struct.
  117. * ioctl: if this routine is present then it will be called first before we do
  118. our own internal ioctl call handling. This routine should return -ENOIOCTLCMD
  119. if a command is not supported. The parameters that are passed to the ioctl
  120. call are: watchdog_device, cmd and arg.
  121. +The 'ref' and 'unref' operations are no longer used and deprecated.
  122. +
  123. The status bits should (preferably) be set with the set_bit and clear_bit alike
  124. bit-operations. The status bits that are defined are:
  125. * WDOG_ACTIVE: this status bit indicates whether or not a watchdog timer device
  126. is active or not. When the watchdog is active after booting, then you should
  127. set this status bit (Note: when you register the watchdog timer device with
  128. this bit set, then opening /dev/watchdog will skip the start operation)
  129. -* WDOG_DEV_OPEN: this status bit shows whether or not the watchdog device
  130. - was opened via /dev/watchdog.
  131. - (This bit should only be used by the WatchDog Timer Driver Core).
  132. -* WDOG_ALLOW_RELEASE: this bit stores whether or not the magic close character
  133. - has been sent (so that we can support the magic close feature).
  134. - (This bit should only be used by the WatchDog Timer Driver Core).
  135. * WDOG_NO_WAY_OUT: this bit stores the nowayout setting for the watchdog.
  136. If this bit is set then the watchdog timer will not be able to stop.
  137. -* WDOG_UNREGISTERED: this bit gets set by the WatchDog Timer Driver Core
  138. - after calling watchdog_unregister_device, and then checked before calling
  139. - any watchdog_ops, so that you can be sure that no operations (other then
  140. - unref) will get called after unregister, even if userspace still holds a
  141. - reference to /dev/watchdog
  142. To set the WDOG_NO_WAY_OUT status bit (before registering your watchdog
  143. timer device) you can either:
  144. --- a/drivers/watchdog/watchdog_core.c
  145. +++ b/drivers/watchdog/watchdog_core.c
  146. @@ -210,8 +210,6 @@ static int __watchdog_register_device(st
  147. * corrupted in a later stage then we expect a kernel panic!
  148. */
  149. - mutex_init(&wdd->lock);
  150. -
  151. /* Use alias for watchdog id if possible */
  152. if (wdd->parent) {
  153. ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
  154. --- a/drivers/watchdog/watchdog_dev.c
  155. +++ b/drivers/watchdog/watchdog_dev.c
  156. @@ -32,27 +32,51 @@
  157. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  158. -#include <linux/module.h> /* For module stuff/... */
  159. -#include <linux/types.h> /* For standard types (like size_t) */
  160. +#include <linux/cdev.h> /* For character device */
  161. #include <linux/errno.h> /* For the -ENODEV/... values */
  162. -#include <linux/kernel.h> /* For printk/panic/... */
  163. #include <linux/fs.h> /* For file operations */
  164. -#include <linux/watchdog.h> /* For watchdog specific items */
  165. -#include <linux/miscdevice.h> /* For handling misc devices */
  166. #include <linux/init.h> /* For __init/__exit/... */
  167. +#include <linux/kernel.h> /* For printk/panic/... */
  168. +#include <linux/kref.h> /* For data references */
  169. +#include <linux/miscdevice.h> /* For handling misc devices */
  170. +#include <linux/module.h> /* For module stuff/... */
  171. +#include <linux/mutex.h> /* For mutexes */
  172. +#include <linux/slab.h> /* For memory functions */
  173. +#include <linux/types.h> /* For standard types (like size_t) */
  174. +#include <linux/watchdog.h> /* For watchdog specific items */
  175. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  176. #include "watchdog_core.h"
  177. +/*
  178. + * struct watchdog_core_data - watchdog core internal data
  179. + * @kref: Reference count.
  180. + * @cdev: The watchdog's Character device.
  181. + * @wdd: Pointer to watchdog device.
  182. + * @lock: Lock for watchdog core.
  183. + * @status: Watchdog core internal status bits.
  184. + */
  185. +struct watchdog_core_data {
  186. + struct kref kref;
  187. + struct cdev cdev;
  188. + struct watchdog_device *wdd;
  189. + struct mutex lock;
  190. + unsigned long status; /* Internal status bits */
  191. +#define _WDOG_DEV_OPEN 0 /* Opened ? */
  192. +#define _WDOG_ALLOW_RELEASE 1 /* Did we receive the magic char ? */
  193. +};
  194. +
  195. /* the dev_t structure to store the dynamically allocated watchdog devices */
  196. static dev_t watchdog_devt;
  197. -/* the watchdog device behind /dev/watchdog */
  198. -static struct watchdog_device *old_wdd;
  199. +/* Reference to watchdog device behind /dev/watchdog */
  200. +static struct watchdog_core_data *old_wd_data;
  201. /*
  202. * watchdog_ping: ping the watchdog.
  203. * @wdd: the watchdog device to ping
  204. *
  205. + * The caller must hold wd_data->lock.
  206. + *
  207. * If the watchdog has no own ping operation then it needs to be
  208. * restarted via the start operation. This wrapper function does
  209. * exactly that.
  210. @@ -61,25 +85,16 @@ static struct watchdog_device *old_wdd;
  211. static int watchdog_ping(struct watchdog_device *wdd)
  212. {
  213. - int err = 0;
  214. -
  215. - mutex_lock(&wdd->lock);
  216. -
  217. - if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
  218. - err = -ENODEV;
  219. - goto out_ping;
  220. - }
  221. + int err;
  222. if (!watchdog_active(wdd))
  223. - goto out_ping;
  224. + return 0;
  225. if (wdd->ops->ping)
  226. err = wdd->ops->ping(wdd); /* ping the watchdog */
  227. else
  228. err = wdd->ops->start(wdd); /* restart watchdog */
  229. -out_ping:
  230. - mutex_unlock(&wdd->lock);
  231. return err;
  232. }
  233. @@ -87,6 +102,8 @@ out_ping:
  234. * watchdog_start: wrapper to start the watchdog.
  235. * @wdd: the watchdog device to start
  236. *
  237. + * The caller must hold wd_data->lock.
  238. + *
  239. * Start the watchdog if it is not active and mark it active.
  240. * This function returns zero on success or a negative errno code for
  241. * failure.
  242. @@ -94,24 +111,15 @@ out_ping:
  243. static int watchdog_start(struct watchdog_device *wdd)
  244. {
  245. - int err = 0;
  246. -
  247. - mutex_lock(&wdd->lock);
  248. -
  249. - if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
  250. - err = -ENODEV;
  251. - goto out_start;
  252. - }
  253. + int err;
  254. if (watchdog_active(wdd))
  255. - goto out_start;
  256. + return 0;
  257. err = wdd->ops->start(wdd);
  258. if (err == 0)
  259. set_bit(WDOG_ACTIVE, &wdd->status);
  260. -out_start:
  261. - mutex_unlock(&wdd->lock);
  262. return err;
  263. }
  264. @@ -119,6 +127,8 @@ out_start:
  265. * watchdog_stop: wrapper to stop the watchdog.
  266. * @wdd: the watchdog device to stop
  267. *
  268. + * The caller must hold wd_data->lock.
  269. + *
  270. * Stop the watchdog if it is still active and unmark it active.
  271. * This function returns zero on success or a negative errno code for
  272. * failure.
  273. @@ -127,93 +137,58 @@ out_start:
  274. static int watchdog_stop(struct watchdog_device *wdd)
  275. {
  276. - int err = 0;
  277. -
  278. - mutex_lock(&wdd->lock);
  279. -
  280. - if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
  281. - err = -ENODEV;
  282. - goto out_stop;
  283. - }
  284. + int err;
  285. if (!watchdog_active(wdd))
  286. - goto out_stop;
  287. + return 0;
  288. if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
  289. dev_info(wdd->dev, "nowayout prevents watchdog being stopped!\n");
  290. - err = -EBUSY;
  291. - goto out_stop;
  292. + return -EBUSY;
  293. }
  294. err = wdd->ops->stop(wdd);
  295. if (err == 0)
  296. clear_bit(WDOG_ACTIVE, &wdd->status);
  297. -out_stop:
  298. - mutex_unlock(&wdd->lock);
  299. return err;
  300. }
  301. /*
  302. * watchdog_get_status: wrapper to get the watchdog status
  303. * @wdd: the watchdog device to get the status from
  304. - * @status: the status of the watchdog device
  305. + *
  306. + * The caller must hold wd_data->lock.
  307. *
  308. * Get the watchdog's status flags.
  309. */
  310. -static int watchdog_get_status(struct watchdog_device *wdd,
  311. - unsigned int *status)
  312. +static unsigned int watchdog_get_status(struct watchdog_device *wdd)
  313. {
  314. - int err = 0;
  315. -
  316. - *status = 0;
  317. if (!wdd->ops->status)
  318. - return -EOPNOTSUPP;
  319. -
  320. - mutex_lock(&wdd->lock);
  321. -
  322. - if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
  323. - err = -ENODEV;
  324. - goto out_status;
  325. - }
  326. -
  327. - *status = wdd->ops->status(wdd);
  328. + return 0;
  329. -out_status:
  330. - mutex_unlock(&wdd->lock);
  331. - return err;
  332. + return wdd->ops->status(wdd);
  333. }
  334. /*
  335. * watchdog_set_timeout: set the watchdog timer timeout
  336. * @wdd: the watchdog device to set the timeout for
  337. * @timeout: timeout to set in seconds
  338. + *
  339. + * The caller must hold wd_data->lock.
  340. */
  341. static int watchdog_set_timeout(struct watchdog_device *wdd,
  342. unsigned int timeout)
  343. {
  344. - int err;
  345. -
  346. if (!wdd->ops->set_timeout || !(wdd->info->options & WDIOF_SETTIMEOUT))
  347. return -EOPNOTSUPP;
  348. if (watchdog_timeout_invalid(wdd, timeout))
  349. return -EINVAL;
  350. - mutex_lock(&wdd->lock);
  351. -
  352. - if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
  353. - err = -ENODEV;
  354. - goto out_timeout;
  355. - }
  356. -
  357. - err = wdd->ops->set_timeout(wdd, timeout);
  358. -
  359. -out_timeout:
  360. - mutex_unlock(&wdd->lock);
  361. - return err;
  362. + return wdd->ops->set_timeout(wdd, timeout);
  363. }
  364. /*
  365. @@ -221,30 +196,22 @@ out_timeout:
  366. * @wdd: the watchdog device to get the remaining time from
  367. * @timeleft: the time that's left
  368. *
  369. + * The caller must hold wd_data->lock.
  370. + *
  371. * Get the time before a watchdog will reboot (if not pinged).
  372. */
  373. static int watchdog_get_timeleft(struct watchdog_device *wdd,
  374. unsigned int *timeleft)
  375. {
  376. - int err = 0;
  377. -
  378. *timeleft = 0;
  379. +
  380. if (!wdd->ops->get_timeleft)
  381. return -EOPNOTSUPP;
  382. - mutex_lock(&wdd->lock);
  383. -
  384. - if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
  385. - err = -ENODEV;
  386. - goto out_timeleft;
  387. - }
  388. -
  389. *timeleft = wdd->ops->get_timeleft(wdd);
  390. -out_timeleft:
  391. - mutex_unlock(&wdd->lock);
  392. - return err;
  393. + return 0;
  394. }
  395. #ifdef CONFIG_WATCHDOG_SYSFS
  396. @@ -261,14 +228,14 @@ static ssize_t status_show(struct device
  397. char *buf)
  398. {
  399. struct watchdog_device *wdd = dev_get_drvdata(dev);
  400. - ssize_t status;
  401. - unsigned int val;
  402. + struct watchdog_core_data *wd_data = wdd->wd_data;
  403. + unsigned int status;
  404. - status = watchdog_get_status(wdd, &val);
  405. - if (!status)
  406. - status = sprintf(buf, "%u\n", val);
  407. + mutex_lock(&wd_data->lock);
  408. + status = watchdog_get_status(wdd);
  409. + mutex_unlock(&wd_data->lock);
  410. - return status;
  411. + return sprintf(buf, "%u\n", status);
  412. }
  413. static DEVICE_ATTR_RO(status);
  414. @@ -285,10 +252,13 @@ static ssize_t timeleft_show(struct devi
  415. char *buf)
  416. {
  417. struct watchdog_device *wdd = dev_get_drvdata(dev);
  418. + struct watchdog_core_data *wd_data = wdd->wd_data;
  419. ssize_t status;
  420. unsigned int val;
  421. + mutex_lock(&wd_data->lock);
  422. status = watchdog_get_timeleft(wdd, &val);
  423. + mutex_unlock(&wd_data->lock);
  424. if (!status)
  425. status = sprintf(buf, "%u\n", val);
  426. @@ -365,28 +335,17 @@ __ATTRIBUTE_GROUPS(wdt);
  427. * @wdd: the watchdog device to do the ioctl on
  428. * @cmd: watchdog command
  429. * @arg: argument pointer
  430. + *
  431. + * The caller must hold wd_data->lock.
  432. */
  433. static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
  434. unsigned long arg)
  435. {
  436. - int err;
  437. -
  438. if (!wdd->ops->ioctl)
  439. return -ENOIOCTLCMD;
  440. - mutex_lock(&wdd->lock);
  441. -
  442. - if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
  443. - err = -ENODEV;
  444. - goto out_ioctl;
  445. - }
  446. -
  447. - err = wdd->ops->ioctl(wdd, cmd, arg);
  448. -
  449. -out_ioctl:
  450. - mutex_unlock(&wdd->lock);
  451. - return err;
  452. + return wdd->ops->ioctl(wdd, cmd, arg);
  453. }
  454. /*
  455. @@ -404,10 +363,11 @@ out_ioctl:
  456. static ssize_t watchdog_write(struct file *file, const char __user *data,
  457. size_t len, loff_t *ppos)
  458. {
  459. - struct watchdog_device *wdd = file->private_data;
  460. + struct watchdog_core_data *wd_data = file->private_data;
  461. + struct watchdog_device *wdd;
  462. + int err;
  463. size_t i;
  464. char c;
  465. - int err;
  466. if (len == 0)
  467. return 0;
  468. @@ -416,18 +376,25 @@ static ssize_t watchdog_write(struct fil
  469. * Note: just in case someone wrote the magic character
  470. * five months ago...
  471. */
  472. - clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
  473. + clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
  474. /* scan to see whether or not we got the magic character */
  475. for (i = 0; i != len; i++) {
  476. if (get_user(c, data + i))
  477. return -EFAULT;
  478. if (c == 'V')
  479. - set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
  480. + set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
  481. }
  482. /* someone wrote to us, so we send the watchdog a keepalive ping */
  483. - err = watchdog_ping(wdd);
  484. +
  485. + err = -ENODEV;
  486. + mutex_lock(&wd_data->lock);
  487. + wdd = wd_data->wdd;
  488. + if (wdd)
  489. + err = watchdog_ping(wdd);
  490. + mutex_unlock(&wd_data->lock);
  491. +
  492. if (err < 0)
  493. return err;
  494. @@ -447,71 +414,94 @@ static ssize_t watchdog_write(struct fil
  495. static long watchdog_ioctl(struct file *file, unsigned int cmd,
  496. unsigned long arg)
  497. {
  498. - struct watchdog_device *wdd = file->private_data;
  499. + struct watchdog_core_data *wd_data = file->private_data;
  500. void __user *argp = (void __user *)arg;
  501. + struct watchdog_device *wdd;
  502. int __user *p = argp;
  503. unsigned int val;
  504. int err;
  505. + mutex_lock(&wd_data->lock);
  506. +
  507. + wdd = wd_data->wdd;
  508. + if (!wdd) {
  509. + err = -ENODEV;
  510. + goto out_ioctl;
  511. + }
  512. +
  513. err = watchdog_ioctl_op(wdd, cmd, arg);
  514. if (err != -ENOIOCTLCMD)
  515. - return err;
  516. + goto out_ioctl;
  517. switch (cmd) {
  518. case WDIOC_GETSUPPORT:
  519. - return copy_to_user(argp, wdd->info,
  520. + err = copy_to_user(argp, wdd->info,
  521. sizeof(struct watchdog_info)) ? -EFAULT : 0;
  522. + break;
  523. case WDIOC_GETSTATUS:
  524. - err = watchdog_get_status(wdd, &val);
  525. - if (err == -ENODEV)
  526. - return err;
  527. - return put_user(val, p);
  528. + val = watchdog_get_status(wdd);
  529. + err = put_user(val, p);
  530. + break;
  531. case WDIOC_GETBOOTSTATUS:
  532. - return put_user(wdd->bootstatus, p);
  533. + err = put_user(wdd->bootstatus, p);
  534. + break;
  535. case WDIOC_SETOPTIONS:
  536. - if (get_user(val, p))
  537. - return -EFAULT;
  538. + if (get_user(val, p)) {
  539. + err = -EFAULT;
  540. + break;
  541. + }
  542. if (val & WDIOS_DISABLECARD) {
  543. err = watchdog_stop(wdd);
  544. if (err < 0)
  545. - return err;
  546. + break;
  547. }
  548. - if (val & WDIOS_ENABLECARD) {
  549. + if (val & WDIOS_ENABLECARD)
  550. err = watchdog_start(wdd);
  551. - if (err < 0)
  552. - return err;
  553. - }
  554. - return 0;
  555. + break;
  556. case WDIOC_KEEPALIVE:
  557. - if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
  558. - return -EOPNOTSUPP;
  559. - return watchdog_ping(wdd);
  560. + if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) {
  561. + err = -EOPNOTSUPP;
  562. + break;
  563. + }
  564. + err = watchdog_ping(wdd);
  565. + break;
  566. case WDIOC_SETTIMEOUT:
  567. - if (get_user(val, p))
  568. - return -EFAULT;
  569. + if (get_user(val, p)) {
  570. + err = -EFAULT;
  571. + break;
  572. + }
  573. err = watchdog_set_timeout(wdd, val);
  574. if (err < 0)
  575. - return err;
  576. + break;
  577. /* If the watchdog is active then we send a keepalive ping
  578. * to make sure that the watchdog keep's running (and if
  579. * possible that it takes the new timeout) */
  580. err = watchdog_ping(wdd);
  581. if (err < 0)
  582. - return err;
  583. + break;
  584. /* Fall */
  585. case WDIOC_GETTIMEOUT:
  586. /* timeout == 0 means that we don't know the timeout */
  587. - if (wdd->timeout == 0)
  588. - return -EOPNOTSUPP;
  589. - return put_user(wdd->timeout, p);
  590. + if (wdd->timeout == 0) {
  591. + err = -EOPNOTSUPP;
  592. + break;
  593. + }
  594. + err = put_user(wdd->timeout, p);
  595. + break;
  596. case WDIOC_GETTIMELEFT:
  597. err = watchdog_get_timeleft(wdd, &val);
  598. - if (err)
  599. - return err;
  600. - return put_user(val, p);
  601. + if (err < 0)
  602. + break;
  603. + err = put_user(val, p);
  604. + break;
  605. default:
  606. - return -ENOTTY;
  607. + err = -ENOTTY;
  608. + break;
  609. }
  610. +
  611. +out_ioctl:
  612. + mutex_unlock(&wd_data->lock);
  613. + return err;
  614. }
  615. /*
  616. @@ -526,45 +516,59 @@ static long watchdog_ioctl(struct file *
  617. static int watchdog_open(struct inode *inode, struct file *file)
  618. {
  619. - int err = -EBUSY;
  620. + struct watchdog_core_data *wd_data;
  621. struct watchdog_device *wdd;
  622. + int err;
  623. /* Get the corresponding watchdog device */
  624. if (imajor(inode) == MISC_MAJOR)
  625. - wdd = old_wdd;
  626. + wd_data = old_wd_data;
  627. else
  628. - wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
  629. + wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
  630. + cdev);
  631. /* the watchdog is single open! */
  632. - if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
  633. + if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
  634. return -EBUSY;
  635. + wdd = wd_data->wdd;
  636. +
  637. /*
  638. * If the /dev/watchdog device is open, we don't want the module
  639. * to be unloaded.
  640. */
  641. - if (!try_module_get(wdd->ops->owner))
  642. - goto out;
  643. + if (!try_module_get(wdd->ops->owner)) {
  644. + err = -EBUSY;
  645. + goto out_clear;
  646. + }
  647. err = watchdog_start(wdd);
  648. if (err < 0)
  649. goto out_mod;
  650. - file->private_data = wdd;
  651. + file->private_data = wd_data;
  652. - if (wdd->ops->ref)
  653. - wdd->ops->ref(wdd);
  654. + kref_get(&wd_data->kref);
  655. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  656. return nonseekable_open(inode, file);
  657. out_mod:
  658. - module_put(wdd->ops->owner);
  659. -out:
  660. - clear_bit(WDOG_DEV_OPEN, &wdd->status);
  661. + module_put(wd_data->wdd->ops->owner);
  662. +out_clear:
  663. + clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
  664. return err;
  665. }
  666. +static void watchdog_core_data_release(struct kref *kref)
  667. +{
  668. + struct watchdog_core_data *wd_data;
  669. +
  670. + wd_data = container_of(kref, struct watchdog_core_data, kref);
  671. +
  672. + kfree(wd_data);
  673. +}
  674. +
  675. /*
  676. * watchdog_release: release the watchdog device.
  677. * @inode: inode of device
  678. @@ -577,9 +581,16 @@ out:
  679. static int watchdog_release(struct inode *inode, struct file *file)
  680. {
  681. - struct watchdog_device *wdd = file->private_data;
  682. + struct watchdog_core_data *wd_data = file->private_data;
  683. + struct watchdog_device *wdd;
  684. int err = -EBUSY;
  685. + mutex_lock(&wd_data->lock);
  686. +
  687. + wdd = wd_data->wdd;
  688. + if (!wdd)
  689. + goto done;
  690. +
  691. /*
  692. * We only stop the watchdog if we received the magic character
  693. * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
  694. @@ -587,29 +598,24 @@ static int watchdog_release(struct inode
  695. */
  696. if (!test_bit(WDOG_ACTIVE, &wdd->status))
  697. err = 0;
  698. - else if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
  699. + else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
  700. !(wdd->info->options & WDIOF_MAGICCLOSE))
  701. err = watchdog_stop(wdd);
  702. /* If the watchdog was not stopped, send a keepalive ping */
  703. if (err < 0) {
  704. - mutex_lock(&wdd->lock);
  705. - if (!test_bit(WDOG_UNREGISTERED, &wdd->status))
  706. - dev_crit(wdd->dev, "watchdog did not stop!\n");
  707. - mutex_unlock(&wdd->lock);
  708. + dev_crit(wdd->dev, "watchdog did not stop!\n");
  709. watchdog_ping(wdd);
  710. }
  711. - /* Allow the owner module to be unloaded again */
  712. - module_put(wdd->ops->owner);
  713. -
  714. /* make sure that /dev/watchdog can be re-opened */
  715. - clear_bit(WDOG_DEV_OPEN, &wdd->status);
  716. -
  717. - /* Note wdd may be gone after this, do not use after this! */
  718. - if (wdd->ops->unref)
  719. - wdd->ops->unref(wdd);
  720. + clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
  721. +done:
  722. + mutex_unlock(&wd_data->lock);
  723. + /* Allow the owner module to be unloaded again */
  724. + module_put(wd_data->cdev.owner);
  725. + kref_put(&wd_data->kref, watchdog_core_data_release);
  726. return 0;
  727. }
  728. @@ -639,10 +645,20 @@ static struct miscdevice watchdog_miscde
  729. static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno)
  730. {
  731. + struct watchdog_core_data *wd_data;
  732. int err;
  733. + wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);
  734. + if (!wd_data)
  735. + return -ENOMEM;
  736. + kref_init(&wd_data->kref);
  737. + mutex_init(&wd_data->lock);
  738. +
  739. + wd_data->wdd = wdd;
  740. + wdd->wd_data = wd_data;
  741. +
  742. if (wdd->id == 0) {
  743. - old_wdd = wdd;
  744. + old_wd_data = wd_data;
  745. watchdog_miscdev.parent = wdd->parent;
  746. err = misc_register(&watchdog_miscdev);
  747. if (err != 0) {
  748. @@ -651,23 +667,25 @@ static int watchdog_cdev_register(struct
  749. if (err == -EBUSY)
  750. pr_err("%s: a legacy watchdog module is probably present.\n",
  751. wdd->info->identity);
  752. - old_wdd = NULL;
  753. + old_wd_data = NULL;
  754. + kfree(wd_data);
  755. return err;
  756. }
  757. }
  758. /* Fill in the data structures */
  759. - cdev_init(&wdd->cdev, &watchdog_fops);
  760. - wdd->cdev.owner = wdd->ops->owner;
  761. + cdev_init(&wd_data->cdev, &watchdog_fops);
  762. + wd_data->cdev.owner = wdd->ops->owner;
  763. /* Add the device */
  764. - err = cdev_add(&wdd->cdev, devno, 1);
  765. + err = cdev_add(&wd_data->cdev, devno, 1);
  766. if (err) {
  767. pr_err("watchdog%d unable to add device %d:%d\n",
  768. wdd->id, MAJOR(watchdog_devt), wdd->id);
  769. if (wdd->id == 0) {
  770. misc_deregister(&watchdog_miscdev);
  771. - old_wdd = NULL;
  772. + old_wd_data = NULL;
  773. + kref_put(&wd_data->kref, watchdog_core_data_release);
  774. }
  775. }
  776. return err;
  777. @@ -683,15 +701,20 @@ static int watchdog_cdev_register(struct
  778. static void watchdog_cdev_unregister(struct watchdog_device *wdd)
  779. {
  780. - mutex_lock(&wdd->lock);
  781. - set_bit(WDOG_UNREGISTERED, &wdd->status);
  782. - mutex_unlock(&wdd->lock);
  783. + struct watchdog_core_data *wd_data = wdd->wd_data;
  784. - cdev_del(&wdd->cdev);
  785. + cdev_del(&wd_data->cdev);
  786. if (wdd->id == 0) {
  787. misc_deregister(&watchdog_miscdev);
  788. - old_wdd = NULL;
  789. + old_wd_data = NULL;
  790. }
  791. +
  792. + mutex_lock(&wd_data->lock);
  793. + wd_data->wdd = NULL;
  794. + wdd->wd_data = NULL;
  795. + mutex_unlock(&wd_data->lock);
  796. +
  797. + kref_put(&wd_data->kref, watchdog_core_data_release);
  798. }
  799. static struct class watchdog_class = {
  800. @@ -742,9 +765,9 @@ int watchdog_dev_register(struct watchdo
  801. void watchdog_dev_unregister(struct watchdog_device *wdd)
  802. {
  803. - watchdog_cdev_unregister(wdd);
  804. device_destroy(&watchdog_class, wdd->dev->devt);
  805. wdd->dev = NULL;
  806. + watchdog_cdev_unregister(wdd);
  807. }
  808. /*
  809. --- a/include/linux/watchdog.h
  810. +++ b/include/linux/watchdog.h
  811. @@ -17,6 +17,7 @@
  812. struct watchdog_ops;
  813. struct watchdog_device;
  814. +struct watchdog_core_data;
  815. /** struct watchdog_ops - The watchdog-devices operations
  816. *
  817. @@ -28,8 +29,6 @@ struct watchdog_device;
  818. * @set_timeout:The routine for setting the watchdog devices timeout value (in seconds).
  819. * @get_timeleft:The routine that gets the time left before a reset (in seconds).
  820. * @restart: The routine for restarting the machine.
  821. - * @ref: The ref operation for dyn. allocated watchdog_device structs
  822. - * @unref: The unref operation for dyn. allocated watchdog_device structs
  823. * @ioctl: The routines that handles extra ioctl calls.
  824. *
  825. * The watchdog_ops structure contains a list of low-level operations
  826. @@ -48,15 +47,14 @@ struct watchdog_ops {
  827. int (*set_timeout)(struct watchdog_device *, unsigned int);
  828. unsigned int (*get_timeleft)(struct watchdog_device *);
  829. int (*restart)(struct watchdog_device *);
  830. - void (*ref)(struct watchdog_device *);
  831. - void (*unref)(struct watchdog_device *);
  832. + void (*ref)(struct watchdog_device *) __deprecated;
  833. + void (*unref)(struct watchdog_device *) __deprecated;
  834. long (*ioctl)(struct watchdog_device *, unsigned int, unsigned long);
  835. };
  836. /** struct watchdog_device - The structure that defines a watchdog device
  837. *
  838. * @id: The watchdog's ID. (Allocated by watchdog_register_device)
  839. - * @cdev: The watchdog's Character device.
  840. * @dev: The device for our watchdog
  841. * @parent: The parent bus device
  842. * @info: Pointer to a watchdog_info structure.
  843. @@ -67,8 +65,8 @@ struct watchdog_ops {
  844. * @max_timeout:The watchdog devices maximum timeout value (in seconds).
  845. * @reboot_nb: The notifier block to stop watchdog on reboot.
  846. * @restart_nb: The notifier block to register a restart function.
  847. - * @driver-data:Pointer to the drivers private data.
  848. - * @lock: Lock for watchdog core internal use only.
  849. + * @driver_data:Pointer to the drivers private data.
  850. + * @wd_data: Pointer to watchdog core internal data.
  851. * @status: Field that contains the devices internal status bits.
  852. * @deferred: entry in wtd_deferred_reg_list which is used to
  853. * register early initialized watchdogs.
  854. @@ -84,7 +82,6 @@ struct watchdog_ops {
  855. */
  856. struct watchdog_device {
  857. int id;
  858. - struct cdev cdev;
  859. struct device *dev;
  860. struct device *parent;
  861. const struct watchdog_info *info;
  862. @@ -96,15 +93,12 @@ struct watchdog_device {
  863. struct notifier_block reboot_nb;
  864. struct notifier_block restart_nb;
  865. void *driver_data;
  866. - struct mutex lock;
  867. + struct watchdog_core_data *wd_data;
  868. unsigned long status;
  869. /* Bit numbers for status flags */
  870. #define WDOG_ACTIVE 0 /* Is the watchdog running/active */
  871. -#define WDOG_DEV_OPEN 1 /* Opened via /dev/watchdog ? */
  872. -#define WDOG_ALLOW_RELEASE 2 /* Did we receive the magic char ? */
  873. -#define WDOG_NO_WAY_OUT 3 /* Is 'nowayout' feature set ? */
  874. -#define WDOG_UNREGISTERED 4 /* Has the device been unregistered */
  875. -#define WDOG_STOP_ON_REBOOT 5 /* Should be stopped on reboot */
  876. +#define WDOG_NO_WAY_OUT 1 /* Is 'nowayout' feature set ? */
  877. +#define WDOG_STOP_ON_REBOOT 2 /* Should be stopped on reboot */
  878. struct list_head deferred;
  879. };