appldata_base.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Base infrastructure for Linux-z/VM Monitor Stream, Stage 1.
  4. * Exports appldata_register_ops() and appldata_unregister_ops() for the
  5. * data gathering modules.
  6. *
  7. * Copyright IBM Corp. 2003, 2009
  8. *
  9. * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
  10. */
  11. #define KMSG_COMPONENT "appldata"
  12. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/sched/stat.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/errno.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/swap.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/sysctl.h>
  24. #include <linux/notifier.h>
  25. #include <linux/cpu.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/suspend.h>
  28. #include <linux/platform_device.h>
  29. #include <asm/appldata.h>
  30. #include <asm/vtimer.h>
  31. #include <linux/uaccess.h>
  32. #include <asm/io.h>
  33. #include <asm/smp.h>
  34. #include "appldata.h"
  35. #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
  36. sampling interval in
  37. milliseconds */
  38. #define TOD_MICRO 0x01000 /* nr. of TOD clock units
  39. for 1 microsecond */
  40. static struct platform_device *appldata_pdev;
  41. /*
  42. * /proc entries (sysctl)
  43. */
  44. static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
  45. static int appldata_timer_handler(struct ctl_table *ctl, int write,
  46. void __user *buffer, size_t *lenp, loff_t *ppos);
  47. static int appldata_interval_handler(struct ctl_table *ctl, int write,
  48. void __user *buffer,
  49. size_t *lenp, loff_t *ppos);
  50. static struct ctl_table_header *appldata_sysctl_header;
  51. static struct ctl_table appldata_table[] = {
  52. {
  53. .procname = "timer",
  54. .mode = S_IRUGO | S_IWUSR,
  55. .proc_handler = appldata_timer_handler,
  56. },
  57. {
  58. .procname = "interval",
  59. .mode = S_IRUGO | S_IWUSR,
  60. .proc_handler = appldata_interval_handler,
  61. },
  62. { },
  63. };
  64. static struct ctl_table appldata_dir_table[] = {
  65. {
  66. .procname = appldata_proc_name,
  67. .maxlen = 0,
  68. .mode = S_IRUGO | S_IXUGO,
  69. .child = appldata_table,
  70. },
  71. { },
  72. };
  73. /*
  74. * Timer
  75. */
  76. static struct vtimer_list appldata_timer;
  77. static DEFINE_SPINLOCK(appldata_timer_lock);
  78. static int appldata_interval = APPLDATA_CPU_INTERVAL;
  79. static int appldata_timer_active;
  80. static int appldata_timer_suspended = 0;
  81. /*
  82. * Work queue
  83. */
  84. static struct workqueue_struct *appldata_wq;
  85. static void appldata_work_fn(struct work_struct *work);
  86. static DECLARE_WORK(appldata_work, appldata_work_fn);
  87. /*
  88. * Ops list
  89. */
  90. static DEFINE_MUTEX(appldata_ops_mutex);
  91. static LIST_HEAD(appldata_ops_list);
  92. /*************************** timer, work, DIAG *******************************/
  93. /*
  94. * appldata_timer_function()
  95. *
  96. * schedule work and reschedule timer
  97. */
  98. static void appldata_timer_function(unsigned long data)
  99. {
  100. queue_work(appldata_wq, (struct work_struct *) data);
  101. }
  102. /*
  103. * appldata_work_fn()
  104. *
  105. * call data gathering function for each (active) module
  106. */
  107. static void appldata_work_fn(struct work_struct *work)
  108. {
  109. struct list_head *lh;
  110. struct appldata_ops *ops;
  111. mutex_lock(&appldata_ops_mutex);
  112. list_for_each(lh, &appldata_ops_list) {
  113. ops = list_entry(lh, struct appldata_ops, list);
  114. if (ops->active == 1) {
  115. ops->callback(ops->data);
  116. }
  117. }
  118. mutex_unlock(&appldata_ops_mutex);
  119. }
  120. /*
  121. * appldata_diag()
  122. *
  123. * prepare parameter list, issue DIAG 0xDC
  124. */
  125. int appldata_diag(char record_nr, u16 function, unsigned long buffer,
  126. u16 length, char *mod_lvl)
  127. {
  128. struct appldata_product_id id = {
  129. .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
  130. 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
  131. .prod_fn = 0xD5D3, /* "NL" */
  132. .version_nr = 0xF2F6, /* "26" */
  133. .release_nr = 0xF0F1, /* "01" */
  134. };
  135. id.record_nr = record_nr;
  136. id.mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
  137. return appldata_asm(&id, function, (void *) buffer, length);
  138. }
  139. /************************ timer, work, DIAG <END> ****************************/
  140. /****************************** /proc stuff **********************************/
  141. #define APPLDATA_ADD_TIMER 0
  142. #define APPLDATA_DEL_TIMER 1
  143. #define APPLDATA_MOD_TIMER 2
  144. /*
  145. * __appldata_vtimer_setup()
  146. *
  147. * Add, delete or modify virtual timers on all online cpus.
  148. * The caller needs to get the appldata_timer_lock spinlock.
  149. */
  150. static void __appldata_vtimer_setup(int cmd)
  151. {
  152. u64 timer_interval = (u64) appldata_interval * 1000 * TOD_MICRO;
  153. switch (cmd) {
  154. case APPLDATA_ADD_TIMER:
  155. if (appldata_timer_active)
  156. break;
  157. appldata_timer.expires = timer_interval;
  158. add_virt_timer_periodic(&appldata_timer);
  159. appldata_timer_active = 1;
  160. break;
  161. case APPLDATA_DEL_TIMER:
  162. del_virt_timer(&appldata_timer);
  163. if (!appldata_timer_active)
  164. break;
  165. appldata_timer_active = 0;
  166. break;
  167. case APPLDATA_MOD_TIMER:
  168. if (!appldata_timer_active)
  169. break;
  170. mod_virt_timer_periodic(&appldata_timer, timer_interval);
  171. }
  172. }
  173. /*
  174. * appldata_timer_handler()
  175. *
  176. * Start/Stop timer, show status of timer (0 = not active, 1 = active)
  177. */
  178. static int
  179. appldata_timer_handler(struct ctl_table *ctl, int write,
  180. void __user *buffer, size_t *lenp, loff_t *ppos)
  181. {
  182. int timer_active = appldata_timer_active;
  183. int zero = 0;
  184. int one = 1;
  185. int rc;
  186. struct ctl_table ctl_entry = {
  187. .procname = ctl->procname,
  188. .data = &timer_active,
  189. .maxlen = sizeof(int),
  190. .extra1 = &zero,
  191. .extra2 = &one,
  192. };
  193. rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
  194. if (rc < 0 || !write)
  195. return rc;
  196. spin_lock(&appldata_timer_lock);
  197. if (timer_active)
  198. __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
  199. else
  200. __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
  201. spin_unlock(&appldata_timer_lock);
  202. return 0;
  203. }
  204. /*
  205. * appldata_interval_handler()
  206. *
  207. * Set (CPU) timer interval for collection of data (in milliseconds), show
  208. * current timer interval.
  209. */
  210. static int
  211. appldata_interval_handler(struct ctl_table *ctl, int write,
  212. void __user *buffer, size_t *lenp, loff_t *ppos)
  213. {
  214. int interval = appldata_interval;
  215. int one = 1;
  216. int rc;
  217. struct ctl_table ctl_entry = {
  218. .procname = ctl->procname,
  219. .data = &interval,
  220. .maxlen = sizeof(int),
  221. .extra1 = &one,
  222. };
  223. rc = proc_dointvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
  224. if (rc < 0 || !write)
  225. return rc;
  226. spin_lock(&appldata_timer_lock);
  227. appldata_interval = interval;
  228. __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
  229. spin_unlock(&appldata_timer_lock);
  230. return 0;
  231. }
  232. /*
  233. * appldata_generic_handler()
  234. *
  235. * Generic start/stop monitoring and DIAG, show status of
  236. * monitoring (0 = not in process, 1 = in process)
  237. */
  238. static int
  239. appldata_generic_handler(struct ctl_table *ctl, int write,
  240. void __user *buffer, size_t *lenp, loff_t *ppos)
  241. {
  242. struct appldata_ops *ops = NULL, *tmp_ops;
  243. struct list_head *lh;
  244. int rc, found;
  245. int active;
  246. int zero = 0;
  247. int one = 1;
  248. struct ctl_table ctl_entry = {
  249. .data = &active,
  250. .maxlen = sizeof(int),
  251. .extra1 = &zero,
  252. .extra2 = &one,
  253. };
  254. found = 0;
  255. mutex_lock(&appldata_ops_mutex);
  256. list_for_each(lh, &appldata_ops_list) {
  257. tmp_ops = list_entry(lh, struct appldata_ops, list);
  258. if (&tmp_ops->ctl_table[2] == ctl) {
  259. found = 1;
  260. }
  261. }
  262. if (!found) {
  263. mutex_unlock(&appldata_ops_mutex);
  264. return -ENODEV;
  265. }
  266. ops = ctl->data;
  267. if (!try_module_get(ops->owner)) { // protect this function
  268. mutex_unlock(&appldata_ops_mutex);
  269. return -ENODEV;
  270. }
  271. mutex_unlock(&appldata_ops_mutex);
  272. active = ops->active;
  273. rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
  274. if (rc < 0 || !write) {
  275. module_put(ops->owner);
  276. return rc;
  277. }
  278. mutex_lock(&appldata_ops_mutex);
  279. if (active && (ops->active == 0)) {
  280. // protect work queue callback
  281. if (!try_module_get(ops->owner)) {
  282. mutex_unlock(&appldata_ops_mutex);
  283. module_put(ops->owner);
  284. return -ENODEV;
  285. }
  286. ops->callback(ops->data); // init record
  287. rc = appldata_diag(ops->record_nr,
  288. APPLDATA_START_INTERVAL_REC,
  289. (unsigned long) ops->data, ops->size,
  290. ops->mod_lvl);
  291. if (rc != 0) {
  292. pr_err("Starting the data collection for %s "
  293. "failed with rc=%d\n", ops->name, rc);
  294. module_put(ops->owner);
  295. } else
  296. ops->active = 1;
  297. } else if (!active && (ops->active == 1)) {
  298. ops->active = 0;
  299. rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
  300. (unsigned long) ops->data, ops->size,
  301. ops->mod_lvl);
  302. if (rc != 0)
  303. pr_err("Stopping the data collection for %s "
  304. "failed with rc=%d\n", ops->name, rc);
  305. module_put(ops->owner);
  306. }
  307. mutex_unlock(&appldata_ops_mutex);
  308. module_put(ops->owner);
  309. return 0;
  310. }
  311. /*************************** /proc stuff <END> *******************************/
  312. /************************* module-ops management *****************************/
  313. /*
  314. * appldata_register_ops()
  315. *
  316. * update ops list, register /proc/sys entries
  317. */
  318. int appldata_register_ops(struct appldata_ops *ops)
  319. {
  320. if (ops->size > APPLDATA_MAX_REC_SIZE)
  321. return -EINVAL;
  322. ops->ctl_table = kcalloc(4, sizeof(struct ctl_table), GFP_KERNEL);
  323. if (!ops->ctl_table)
  324. return -ENOMEM;
  325. mutex_lock(&appldata_ops_mutex);
  326. list_add(&ops->list, &appldata_ops_list);
  327. mutex_unlock(&appldata_ops_mutex);
  328. ops->ctl_table[0].procname = appldata_proc_name;
  329. ops->ctl_table[0].maxlen = 0;
  330. ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
  331. ops->ctl_table[0].child = &ops->ctl_table[2];
  332. ops->ctl_table[2].procname = ops->name;
  333. ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
  334. ops->ctl_table[2].proc_handler = appldata_generic_handler;
  335. ops->ctl_table[2].data = ops;
  336. ops->sysctl_header = register_sysctl_table(ops->ctl_table);
  337. if (!ops->sysctl_header)
  338. goto out;
  339. return 0;
  340. out:
  341. mutex_lock(&appldata_ops_mutex);
  342. list_del(&ops->list);
  343. mutex_unlock(&appldata_ops_mutex);
  344. kfree(ops->ctl_table);
  345. return -ENOMEM;
  346. }
  347. /*
  348. * appldata_unregister_ops()
  349. *
  350. * update ops list, unregister /proc entries, stop DIAG if necessary
  351. */
  352. void appldata_unregister_ops(struct appldata_ops *ops)
  353. {
  354. mutex_lock(&appldata_ops_mutex);
  355. list_del(&ops->list);
  356. mutex_unlock(&appldata_ops_mutex);
  357. unregister_sysctl_table(ops->sysctl_header);
  358. kfree(ops->ctl_table);
  359. }
  360. /********************** module-ops management <END> **************************/
  361. /**************************** suspend / resume *******************************/
  362. static int appldata_freeze(struct device *dev)
  363. {
  364. struct appldata_ops *ops;
  365. int rc;
  366. struct list_head *lh;
  367. spin_lock(&appldata_timer_lock);
  368. if (appldata_timer_active) {
  369. __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
  370. appldata_timer_suspended = 1;
  371. }
  372. spin_unlock(&appldata_timer_lock);
  373. mutex_lock(&appldata_ops_mutex);
  374. list_for_each(lh, &appldata_ops_list) {
  375. ops = list_entry(lh, struct appldata_ops, list);
  376. if (ops->active == 1) {
  377. rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
  378. (unsigned long) ops->data, ops->size,
  379. ops->mod_lvl);
  380. if (rc != 0)
  381. pr_err("Stopping the data collection for %s "
  382. "failed with rc=%d\n", ops->name, rc);
  383. }
  384. }
  385. mutex_unlock(&appldata_ops_mutex);
  386. return 0;
  387. }
  388. static int appldata_restore(struct device *dev)
  389. {
  390. struct appldata_ops *ops;
  391. int rc;
  392. struct list_head *lh;
  393. spin_lock(&appldata_timer_lock);
  394. if (appldata_timer_suspended) {
  395. __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
  396. appldata_timer_suspended = 0;
  397. }
  398. spin_unlock(&appldata_timer_lock);
  399. mutex_lock(&appldata_ops_mutex);
  400. list_for_each(lh, &appldata_ops_list) {
  401. ops = list_entry(lh, struct appldata_ops, list);
  402. if (ops->active == 1) {
  403. ops->callback(ops->data); // init record
  404. rc = appldata_diag(ops->record_nr,
  405. APPLDATA_START_INTERVAL_REC,
  406. (unsigned long) ops->data, ops->size,
  407. ops->mod_lvl);
  408. if (rc != 0) {
  409. pr_err("Starting the data collection for %s "
  410. "failed with rc=%d\n", ops->name, rc);
  411. }
  412. }
  413. }
  414. mutex_unlock(&appldata_ops_mutex);
  415. return 0;
  416. }
  417. static int appldata_thaw(struct device *dev)
  418. {
  419. return appldata_restore(dev);
  420. }
  421. static const struct dev_pm_ops appldata_pm_ops = {
  422. .freeze = appldata_freeze,
  423. .thaw = appldata_thaw,
  424. .restore = appldata_restore,
  425. };
  426. static struct platform_driver appldata_pdrv = {
  427. .driver = {
  428. .name = "appldata",
  429. .pm = &appldata_pm_ops,
  430. },
  431. };
  432. /************************* suspend / resume <END> ****************************/
  433. /******************************* init / exit *********************************/
  434. /*
  435. * appldata_init()
  436. *
  437. * init timer, register /proc entries
  438. */
  439. static int __init appldata_init(void)
  440. {
  441. int rc;
  442. init_virt_timer(&appldata_timer);
  443. appldata_timer.function = appldata_timer_function;
  444. appldata_timer.data = (unsigned long) &appldata_work;
  445. rc = platform_driver_register(&appldata_pdrv);
  446. if (rc)
  447. return rc;
  448. appldata_pdev = platform_device_register_simple("appldata", -1, NULL,
  449. 0);
  450. if (IS_ERR(appldata_pdev)) {
  451. rc = PTR_ERR(appldata_pdev);
  452. goto out_driver;
  453. }
  454. appldata_wq = alloc_ordered_workqueue("appldata", 0);
  455. if (!appldata_wq) {
  456. rc = -ENOMEM;
  457. goto out_device;
  458. }
  459. appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
  460. return 0;
  461. out_device:
  462. platform_device_unregister(appldata_pdev);
  463. out_driver:
  464. platform_driver_unregister(&appldata_pdrv);
  465. return rc;
  466. }
  467. __initcall(appldata_init);
  468. /**************************** init / exit <END> ******************************/
  469. EXPORT_SYMBOL_GPL(appldata_register_ops);
  470. EXPORT_SYMBOL_GPL(appldata_unregister_ops);
  471. EXPORT_SYMBOL_GPL(appldata_diag);
  472. #ifdef CONFIG_SWAP
  473. EXPORT_SYMBOL_GPL(si_swapinfo);
  474. #endif
  475. EXPORT_SYMBOL_GPL(nr_threads);
  476. EXPORT_SYMBOL_GPL(nr_running);
  477. EXPORT_SYMBOL_GPL(nr_iowait);