appldata_base.c 13 KB

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