policy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * System Trace Module (STM) master/channel allocation policy management
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * A master/channel allocation policy allows mapping string identifiers to
  15. * master and channel ranges, where allocation can be done.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/types.h>
  19. #include <linux/module.h>
  20. #include <linux/device.h>
  21. #include <linux/configfs.h>
  22. #include <linux/slab.h>
  23. #include <linux/stm.h>
  24. #include "stm.h"
  25. /*
  26. * STP Master/Channel allocation policy configfs layout.
  27. */
  28. struct stp_policy {
  29. struct config_group group;
  30. struct stm_device *stm;
  31. };
  32. struct stp_policy_node {
  33. struct config_group group;
  34. struct stp_policy *policy;
  35. unsigned int first_master;
  36. unsigned int last_master;
  37. unsigned int first_channel;
  38. unsigned int last_channel;
  39. };
  40. static struct configfs_subsystem stp_policy_subsys;
  41. void stp_policy_node_get_ranges(struct stp_policy_node *policy_node,
  42. unsigned int *mstart, unsigned int *mend,
  43. unsigned int *cstart, unsigned int *cend)
  44. {
  45. *mstart = policy_node->first_master;
  46. *mend = policy_node->last_master;
  47. *cstart = policy_node->first_channel;
  48. *cend = policy_node->last_channel;
  49. }
  50. static inline char *stp_policy_node_name(struct stp_policy_node *policy_node)
  51. {
  52. return policy_node->group.cg_item.ci_name ? : "<none>";
  53. }
  54. static inline struct stp_policy *to_stp_policy(struct config_item *item)
  55. {
  56. return item ?
  57. container_of(to_config_group(item), struct stp_policy, group) :
  58. NULL;
  59. }
  60. static inline struct stp_policy_node *
  61. to_stp_policy_node(struct config_item *item)
  62. {
  63. return item ?
  64. container_of(to_config_group(item), struct stp_policy_node,
  65. group) :
  66. NULL;
  67. }
  68. static ssize_t stp_policy_node_masters_show(struct stp_policy_node *policy_node,
  69. char *page)
  70. {
  71. ssize_t count;
  72. count = sprintf(page, "%u %u\n", policy_node->first_master,
  73. policy_node->last_master);
  74. return count;
  75. }
  76. static ssize_t
  77. stp_policy_node_masters_store(struct stp_policy_node *policy_node,
  78. const char *page, size_t count)
  79. {
  80. unsigned int first, last;
  81. struct stm_device *stm;
  82. char *p = (char *)page;
  83. ssize_t ret = -ENODEV;
  84. if (sscanf(p, "%u %u", &first, &last) != 2)
  85. return -EINVAL;
  86. mutex_lock(&stp_policy_subsys.su_mutex);
  87. stm = policy_node->policy->stm;
  88. if (!stm)
  89. goto unlock;
  90. /* must be within [sw_start..sw_end], which is an inclusive range */
  91. if (first > INT_MAX || last > INT_MAX || first > last ||
  92. first < stm->data->sw_start ||
  93. last > stm->data->sw_end) {
  94. ret = -ERANGE;
  95. goto unlock;
  96. }
  97. ret = count;
  98. policy_node->first_master = first;
  99. policy_node->last_master = last;
  100. unlock:
  101. mutex_unlock(&stp_policy_subsys.su_mutex);
  102. return ret;
  103. }
  104. static ssize_t
  105. stp_policy_node_channels_show(struct stp_policy_node *policy_node, char *page)
  106. {
  107. ssize_t count;
  108. count = sprintf(page, "%u %u\n", policy_node->first_channel,
  109. policy_node->last_channel);
  110. return count;
  111. }
  112. static ssize_t
  113. stp_policy_node_channels_store(struct stp_policy_node *policy_node,
  114. const char *page, size_t count)
  115. {
  116. unsigned int first, last;
  117. struct stm_device *stm;
  118. char *p = (char *)page;
  119. ssize_t ret = -ENODEV;
  120. if (sscanf(p, "%u %u", &first, &last) != 2)
  121. return -EINVAL;
  122. mutex_lock(&stp_policy_subsys.su_mutex);
  123. stm = policy_node->policy->stm;
  124. if (!stm)
  125. goto unlock;
  126. if (first > INT_MAX || last > INT_MAX || first > last ||
  127. last >= stm->data->sw_nchannels) {
  128. ret = -ERANGE;
  129. goto unlock;
  130. }
  131. ret = count;
  132. policy_node->first_channel = first;
  133. policy_node->last_channel = last;
  134. unlock:
  135. mutex_unlock(&stp_policy_subsys.su_mutex);
  136. return ret;
  137. }
  138. static void stp_policy_node_release(struct config_item *item)
  139. {
  140. kfree(to_stp_policy_node(item));
  141. }
  142. struct stp_policy_node_attribute {
  143. struct configfs_attribute attr;
  144. ssize_t (*show)(struct stp_policy_node *, char *);
  145. ssize_t (*store)(struct stp_policy_node *, const char *, size_t);
  146. };
  147. static ssize_t stp_policy_node_attr_show(struct config_item *item,
  148. struct configfs_attribute *attr,
  149. char *page)
  150. {
  151. struct stp_policy_node *policy_node = to_stp_policy_node(item);
  152. struct stp_policy_node_attribute *pn_attr =
  153. container_of(attr, struct stp_policy_node_attribute, attr);
  154. ssize_t count = 0;
  155. if (pn_attr->show)
  156. count = pn_attr->show(policy_node, page);
  157. return count;
  158. }
  159. static ssize_t stp_policy_node_attr_store(struct config_item *item,
  160. struct configfs_attribute *attr,
  161. const char *page, size_t len)
  162. {
  163. struct stp_policy_node *policy_node = to_stp_policy_node(item);
  164. struct stp_policy_node_attribute *pn_attr =
  165. container_of(attr, struct stp_policy_node_attribute, attr);
  166. ssize_t count = -EINVAL;
  167. if (pn_attr->store)
  168. count = pn_attr->store(policy_node, page, len);
  169. return count;
  170. }
  171. static struct configfs_item_operations stp_policy_node_item_ops = {
  172. .release = stp_policy_node_release,
  173. .show_attribute = stp_policy_node_attr_show,
  174. .store_attribute = stp_policy_node_attr_store,
  175. };
  176. static struct stp_policy_node_attribute stp_policy_node_attr_range = {
  177. .attr = {
  178. .ca_owner = THIS_MODULE,
  179. .ca_name = "masters",
  180. .ca_mode = S_IRUGO | S_IWUSR,
  181. },
  182. .show = stp_policy_node_masters_show,
  183. .store = stp_policy_node_masters_store,
  184. };
  185. static struct stp_policy_node_attribute stp_policy_node_attr_channels = {
  186. .attr = {
  187. .ca_owner = THIS_MODULE,
  188. .ca_name = "channels",
  189. .ca_mode = S_IRUGO | S_IWUSR,
  190. },
  191. .show = stp_policy_node_channels_show,
  192. .store = stp_policy_node_channels_store,
  193. };
  194. static struct configfs_attribute *stp_policy_node_attrs[] = {
  195. &stp_policy_node_attr_range.attr,
  196. &stp_policy_node_attr_channels.attr,
  197. NULL,
  198. };
  199. static struct config_item_type stp_policy_type;
  200. static struct config_item_type stp_policy_node_type;
  201. static struct config_group *
  202. stp_policy_node_make(struct config_group *group, const char *name)
  203. {
  204. struct stp_policy_node *policy_node, *parent_node;
  205. struct stp_policy *policy;
  206. if (group->cg_item.ci_type == &stp_policy_type) {
  207. policy = container_of(group, struct stp_policy, group);
  208. } else {
  209. parent_node = container_of(group, struct stp_policy_node,
  210. group);
  211. policy = parent_node->policy;
  212. }
  213. if (!policy->stm)
  214. return ERR_PTR(-ENODEV);
  215. policy_node = kzalloc(sizeof(struct stp_policy_node), GFP_KERNEL);
  216. if (!policy_node)
  217. return ERR_PTR(-ENOMEM);
  218. config_group_init_type_name(&policy_node->group, name,
  219. &stp_policy_node_type);
  220. policy_node->policy = policy;
  221. /* default values for the attributes */
  222. policy_node->first_master = policy->stm->data->sw_start;
  223. policy_node->last_master = policy->stm->data->sw_end;
  224. policy_node->first_channel = 0;
  225. policy_node->last_channel = policy->stm->data->sw_nchannels - 1;
  226. return &policy_node->group;
  227. }
  228. static void
  229. stp_policy_node_drop(struct config_group *group, struct config_item *item)
  230. {
  231. config_item_put(item);
  232. }
  233. static struct configfs_group_operations stp_policy_node_group_ops = {
  234. .make_group = stp_policy_node_make,
  235. .drop_item = stp_policy_node_drop,
  236. };
  237. static struct config_item_type stp_policy_node_type = {
  238. .ct_item_ops = &stp_policy_node_item_ops,
  239. .ct_group_ops = &stp_policy_node_group_ops,
  240. .ct_attrs = stp_policy_node_attrs,
  241. .ct_owner = THIS_MODULE,
  242. };
  243. /*
  244. * Root group: policies.
  245. */
  246. static struct configfs_attribute stp_policy_attr_device = {
  247. .ca_owner = THIS_MODULE,
  248. .ca_name = "device",
  249. .ca_mode = S_IRUGO,
  250. };
  251. static struct configfs_attribute *stp_policy_attrs[] = {
  252. &stp_policy_attr_device,
  253. NULL,
  254. };
  255. static ssize_t stp_policy_attr_show(struct config_item *item,
  256. struct configfs_attribute *attr,
  257. char *page)
  258. {
  259. struct stp_policy *policy = to_stp_policy(item);
  260. ssize_t count;
  261. count = sprintf(page, "%s\n",
  262. (policy && policy->stm) ?
  263. policy->stm->data->name :
  264. "<none>");
  265. return count;
  266. }
  267. void stp_policy_unbind(struct stp_policy *policy)
  268. {
  269. struct stm_device *stm = policy->stm;
  270. if (WARN_ON_ONCE(!policy->stm))
  271. return;
  272. mutex_lock(&stm->policy_mutex);
  273. stm->policy = NULL;
  274. mutex_unlock(&stm->policy_mutex);
  275. policy->stm = NULL;
  276. stm_put_device(stm);
  277. }
  278. static void stp_policy_release(struct config_item *item)
  279. {
  280. struct stp_policy *policy = to_stp_policy(item);
  281. stp_policy_unbind(policy);
  282. kfree(policy);
  283. }
  284. static struct configfs_item_operations stp_policy_item_ops = {
  285. .release = stp_policy_release,
  286. .show_attribute = stp_policy_attr_show,
  287. };
  288. static struct configfs_group_operations stp_policy_group_ops = {
  289. .make_group = stp_policy_node_make,
  290. };
  291. static struct config_item_type stp_policy_type = {
  292. .ct_item_ops = &stp_policy_item_ops,
  293. .ct_group_ops = &stp_policy_group_ops,
  294. .ct_attrs = stp_policy_attrs,
  295. .ct_owner = THIS_MODULE,
  296. };
  297. static struct config_group *
  298. stp_policies_make(struct config_group *group, const char *name)
  299. {
  300. struct config_group *ret;
  301. struct stm_device *stm;
  302. char *devname, *p;
  303. devname = kasprintf(GFP_KERNEL, "%s", name);
  304. if (!devname)
  305. return ERR_PTR(-ENOMEM);
  306. /*
  307. * node must look like <device_name>.<policy_name>, where
  308. * <device_name> is the name of an existing stm device and
  309. * <policy_name> is an arbitrary string
  310. */
  311. p = strchr(devname, '.');
  312. if (!p) {
  313. kfree(devname);
  314. return ERR_PTR(-EINVAL);
  315. }
  316. *p++ = '\0';
  317. stm = stm_find_device(devname);
  318. kfree(devname);
  319. if (!stm)
  320. return ERR_PTR(-ENODEV);
  321. mutex_lock(&stm->policy_mutex);
  322. if (stm->policy) {
  323. ret = ERR_PTR(-EBUSY);
  324. goto unlock_policy;
  325. }
  326. stm->policy = kzalloc(sizeof(*stm->policy), GFP_KERNEL);
  327. if (!stm->policy) {
  328. ret = ERR_PTR(-ENOMEM);
  329. goto unlock_policy;
  330. }
  331. config_group_init_type_name(&stm->policy->group, name,
  332. &stp_policy_type);
  333. stm->policy->stm = stm;
  334. ret = &stm->policy->group;
  335. unlock_policy:
  336. mutex_unlock(&stm->policy_mutex);
  337. if (IS_ERR(ret))
  338. stm_put_device(stm);
  339. return ret;
  340. }
  341. static struct configfs_group_operations stp_policies_group_ops = {
  342. .make_group = stp_policies_make,
  343. };
  344. static struct config_item_type stp_policies_type = {
  345. .ct_group_ops = &stp_policies_group_ops,
  346. .ct_owner = THIS_MODULE,
  347. };
  348. static struct configfs_subsystem stp_policy_subsys = {
  349. .su_group = {
  350. .cg_item = {
  351. .ci_namebuf = "stp-policy",
  352. .ci_type = &stp_policies_type,
  353. },
  354. },
  355. };
  356. /*
  357. * Lock the policy mutex from the outside
  358. */
  359. static struct stp_policy_node *
  360. __stp_policy_node_lookup(struct stp_policy *policy, char *s)
  361. {
  362. struct stp_policy_node *policy_node, *ret;
  363. struct list_head *head = &policy->group.cg_children;
  364. struct config_item *item;
  365. char *start, *end = s;
  366. if (list_empty(head))
  367. return NULL;
  368. /* return the first entry if everything else fails */
  369. item = list_entry(head->next, struct config_item, ci_entry);
  370. ret = to_stp_policy_node(item);
  371. next:
  372. for (;;) {
  373. start = strsep(&end, "/");
  374. if (!start)
  375. break;
  376. if (!*start)
  377. continue;
  378. list_for_each_entry(item, head, ci_entry) {
  379. policy_node = to_stp_policy_node(item);
  380. if (!strcmp(start,
  381. policy_node->group.cg_item.ci_name)) {
  382. ret = policy_node;
  383. if (!end)
  384. goto out;
  385. head = &policy_node->group.cg_children;
  386. goto next;
  387. }
  388. }
  389. break;
  390. }
  391. out:
  392. return ret;
  393. }
  394. struct stp_policy_node *
  395. stp_policy_node_lookup(struct stm_device *stm, char *s)
  396. {
  397. struct stp_policy_node *policy_node = NULL;
  398. mutex_lock(&stp_policy_subsys.su_mutex);
  399. mutex_lock(&stm->policy_mutex);
  400. if (stm->policy)
  401. policy_node = __stp_policy_node_lookup(stm->policy, s);
  402. mutex_unlock(&stm->policy_mutex);
  403. if (policy_node)
  404. config_item_get(&policy_node->group.cg_item);
  405. mutex_unlock(&stp_policy_subsys.su_mutex);
  406. return policy_node;
  407. }
  408. void stp_policy_node_put(struct stp_policy_node *policy_node)
  409. {
  410. config_item_put(&policy_node->group.cg_item);
  411. }
  412. int __init stp_configfs_init(void)
  413. {
  414. int err;
  415. config_group_init(&stp_policy_subsys.su_group);
  416. mutex_init(&stp_policy_subsys.su_mutex);
  417. err = configfs_register_subsystem(&stp_policy_subsys);
  418. return err;
  419. }
  420. void __exit stp_configfs_exit(void)
  421. {
  422. configfs_unregister_subsystem(&stp_policy_subsys);
  423. }