policy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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
  69. stp_policy_node_masters_show(struct config_item *item, char *page)
  70. {
  71. struct stp_policy_node *policy_node = to_stp_policy_node(item);
  72. ssize_t count;
  73. count = sprintf(page, "%u %u\n", policy_node->first_master,
  74. policy_node->last_master);
  75. return count;
  76. }
  77. static ssize_t
  78. stp_policy_node_masters_store(struct config_item *item, const char *page,
  79. size_t count)
  80. {
  81. struct stp_policy_node *policy_node = to_stp_policy_node(item);
  82. unsigned int first, last;
  83. struct stm_device *stm;
  84. char *p = (char *)page;
  85. ssize_t ret = -ENODEV;
  86. if (sscanf(p, "%u %u", &first, &last) != 2)
  87. return -EINVAL;
  88. mutex_lock(&stp_policy_subsys.su_mutex);
  89. stm = policy_node->policy->stm;
  90. if (!stm)
  91. goto unlock;
  92. /* must be within [sw_start..sw_end], which is an inclusive range */
  93. if (first > last || first < stm->data->sw_start ||
  94. last > stm->data->sw_end) {
  95. ret = -ERANGE;
  96. goto unlock;
  97. }
  98. ret = count;
  99. policy_node->first_master = first;
  100. policy_node->last_master = last;
  101. unlock:
  102. mutex_unlock(&stp_policy_subsys.su_mutex);
  103. return ret;
  104. }
  105. static ssize_t
  106. stp_policy_node_channels_show(struct config_item *item, char *page)
  107. {
  108. struct stp_policy_node *policy_node = to_stp_policy_node(item);
  109. ssize_t count;
  110. count = sprintf(page, "%u %u\n", policy_node->first_channel,
  111. policy_node->last_channel);
  112. return count;
  113. }
  114. static ssize_t
  115. stp_policy_node_channels_store(struct config_item *item, const char *page,
  116. size_t count)
  117. {
  118. struct stp_policy_node *policy_node = to_stp_policy_node(item);
  119. unsigned int first, last;
  120. struct stm_device *stm;
  121. char *p = (char *)page;
  122. ssize_t ret = -ENODEV;
  123. if (sscanf(p, "%u %u", &first, &last) != 2)
  124. return -EINVAL;
  125. mutex_lock(&stp_policy_subsys.su_mutex);
  126. stm = policy_node->policy->stm;
  127. if (!stm)
  128. goto unlock;
  129. if (first > INT_MAX || last > INT_MAX || first > last ||
  130. last >= stm->data->sw_nchannels) {
  131. ret = -ERANGE;
  132. goto unlock;
  133. }
  134. ret = count;
  135. policy_node->first_channel = first;
  136. policy_node->last_channel = last;
  137. unlock:
  138. mutex_unlock(&stp_policy_subsys.su_mutex);
  139. return ret;
  140. }
  141. static void stp_policy_node_release(struct config_item *item)
  142. {
  143. kfree(to_stp_policy_node(item));
  144. }
  145. static struct configfs_item_operations stp_policy_node_item_ops = {
  146. .release = stp_policy_node_release,
  147. };
  148. CONFIGFS_ATTR(stp_policy_node_, masters);
  149. CONFIGFS_ATTR(stp_policy_node_, channels);
  150. static struct configfs_attribute *stp_policy_node_attrs[] = {
  151. &stp_policy_node_attr_masters,
  152. &stp_policy_node_attr_channels,
  153. NULL,
  154. };
  155. static struct config_item_type stp_policy_type;
  156. static struct config_item_type stp_policy_node_type;
  157. static struct config_group *
  158. stp_policy_node_make(struct config_group *group, const char *name)
  159. {
  160. struct stp_policy_node *policy_node, *parent_node;
  161. struct stp_policy *policy;
  162. if (group->cg_item.ci_type == &stp_policy_type) {
  163. policy = container_of(group, struct stp_policy, group);
  164. } else {
  165. parent_node = container_of(group, struct stp_policy_node,
  166. group);
  167. policy = parent_node->policy;
  168. }
  169. if (!policy->stm)
  170. return ERR_PTR(-ENODEV);
  171. policy_node = kzalloc(sizeof(struct stp_policy_node), GFP_KERNEL);
  172. if (!policy_node)
  173. return ERR_PTR(-ENOMEM);
  174. config_group_init_type_name(&policy_node->group, name,
  175. &stp_policy_node_type);
  176. policy_node->policy = policy;
  177. /* default values for the attributes */
  178. policy_node->first_master = policy->stm->data->sw_start;
  179. policy_node->last_master = policy->stm->data->sw_end;
  180. policy_node->first_channel = 0;
  181. policy_node->last_channel = policy->stm->data->sw_nchannels - 1;
  182. return &policy_node->group;
  183. }
  184. static void
  185. stp_policy_node_drop(struct config_group *group, struct config_item *item)
  186. {
  187. config_item_put(item);
  188. }
  189. static struct configfs_group_operations stp_policy_node_group_ops = {
  190. .make_group = stp_policy_node_make,
  191. .drop_item = stp_policy_node_drop,
  192. };
  193. static struct config_item_type stp_policy_node_type = {
  194. .ct_item_ops = &stp_policy_node_item_ops,
  195. .ct_group_ops = &stp_policy_node_group_ops,
  196. .ct_attrs = stp_policy_node_attrs,
  197. .ct_owner = THIS_MODULE,
  198. };
  199. /*
  200. * Root group: policies.
  201. */
  202. static ssize_t stp_policy_device_show(struct config_item *item,
  203. char *page)
  204. {
  205. struct stp_policy *policy = to_stp_policy(item);
  206. ssize_t count;
  207. count = sprintf(page, "%s\n",
  208. (policy && policy->stm) ?
  209. policy->stm->data->name :
  210. "<none>");
  211. return count;
  212. }
  213. CONFIGFS_ATTR_RO(stp_policy_, device);
  214. static struct configfs_attribute *stp_policy_attrs[] = {
  215. &stp_policy_attr_device,
  216. NULL,
  217. };
  218. void stp_policy_unbind(struct stp_policy *policy)
  219. {
  220. struct stm_device *stm = policy->stm;
  221. /*
  222. * stp_policy_release() will not call here if the policy is already
  223. * unbound; other users should not either, as no link exists between
  224. * this policy and anything else in that case
  225. */
  226. if (WARN_ON_ONCE(!policy->stm))
  227. return;
  228. lockdep_assert_held(&stm->policy_mutex);
  229. stm->policy = NULL;
  230. policy->stm = NULL;
  231. stm_put_device(stm);
  232. }
  233. static void stp_policy_release(struct config_item *item)
  234. {
  235. struct stp_policy *policy = to_stp_policy(item);
  236. struct stm_device *stm = policy->stm;
  237. /* a policy *can* be unbound and still exist in configfs tree */
  238. if (!stm)
  239. return;
  240. mutex_lock(&stm->policy_mutex);
  241. stp_policy_unbind(policy);
  242. mutex_unlock(&stm->policy_mutex);
  243. kfree(policy);
  244. }
  245. static struct configfs_item_operations stp_policy_item_ops = {
  246. .release = stp_policy_release,
  247. };
  248. static struct configfs_group_operations stp_policy_group_ops = {
  249. .make_group = stp_policy_node_make,
  250. };
  251. static struct config_item_type stp_policy_type = {
  252. .ct_item_ops = &stp_policy_item_ops,
  253. .ct_group_ops = &stp_policy_group_ops,
  254. .ct_attrs = stp_policy_attrs,
  255. .ct_owner = THIS_MODULE,
  256. };
  257. static struct config_group *
  258. stp_policies_make(struct config_group *group, const char *name)
  259. {
  260. struct config_group *ret;
  261. struct stm_device *stm;
  262. char *devname, *p;
  263. devname = kasprintf(GFP_KERNEL, "%s", name);
  264. if (!devname)
  265. return ERR_PTR(-ENOMEM);
  266. /*
  267. * node must look like <device_name>.<policy_name>, where
  268. * <device_name> is the name of an existing stm device; may
  269. * contain dots;
  270. * <policy_name> is an arbitrary string; may not contain dots
  271. */
  272. p = strrchr(devname, '.');
  273. if (!p) {
  274. kfree(devname);
  275. return ERR_PTR(-EINVAL);
  276. }
  277. *p = '\0';
  278. stm = stm_find_device(devname);
  279. kfree(devname);
  280. if (!stm)
  281. return ERR_PTR(-ENODEV);
  282. mutex_lock(&stm->policy_mutex);
  283. if (stm->policy) {
  284. ret = ERR_PTR(-EBUSY);
  285. goto unlock_policy;
  286. }
  287. stm->policy = kzalloc(sizeof(*stm->policy), GFP_KERNEL);
  288. if (!stm->policy) {
  289. ret = ERR_PTR(-ENOMEM);
  290. goto unlock_policy;
  291. }
  292. config_group_init_type_name(&stm->policy->group, name,
  293. &stp_policy_type);
  294. stm->policy->stm = stm;
  295. ret = &stm->policy->group;
  296. unlock_policy:
  297. mutex_unlock(&stm->policy_mutex);
  298. if (IS_ERR(ret))
  299. stm_put_device(stm);
  300. return ret;
  301. }
  302. static struct configfs_group_operations stp_policies_group_ops = {
  303. .make_group = stp_policies_make,
  304. };
  305. static struct config_item_type stp_policies_type = {
  306. .ct_group_ops = &stp_policies_group_ops,
  307. .ct_owner = THIS_MODULE,
  308. };
  309. static struct configfs_subsystem stp_policy_subsys = {
  310. .su_group = {
  311. .cg_item = {
  312. .ci_namebuf = "stp-policy",
  313. .ci_type = &stp_policies_type,
  314. },
  315. },
  316. };
  317. /*
  318. * Lock the policy mutex from the outside
  319. */
  320. static struct stp_policy_node *
  321. __stp_policy_node_lookup(struct stp_policy *policy, char *s)
  322. {
  323. struct stp_policy_node *policy_node, *ret;
  324. struct list_head *head = &policy->group.cg_children;
  325. struct config_item *item;
  326. char *start, *end = s;
  327. if (list_empty(head))
  328. return NULL;
  329. /* return the first entry if everything else fails */
  330. item = list_entry(head->next, struct config_item, ci_entry);
  331. ret = to_stp_policy_node(item);
  332. next:
  333. for (;;) {
  334. start = strsep(&end, "/");
  335. if (!start)
  336. break;
  337. if (!*start)
  338. continue;
  339. list_for_each_entry(item, head, ci_entry) {
  340. policy_node = to_stp_policy_node(item);
  341. if (!strcmp(start,
  342. policy_node->group.cg_item.ci_name)) {
  343. ret = policy_node;
  344. if (!end)
  345. goto out;
  346. head = &policy_node->group.cg_children;
  347. goto next;
  348. }
  349. }
  350. break;
  351. }
  352. out:
  353. return ret;
  354. }
  355. struct stp_policy_node *
  356. stp_policy_node_lookup(struct stm_device *stm, char *s)
  357. {
  358. struct stp_policy_node *policy_node = NULL;
  359. mutex_lock(&stp_policy_subsys.su_mutex);
  360. mutex_lock(&stm->policy_mutex);
  361. if (stm->policy)
  362. policy_node = __stp_policy_node_lookup(stm->policy, s);
  363. mutex_unlock(&stm->policy_mutex);
  364. if (policy_node)
  365. config_item_get(&policy_node->group.cg_item);
  366. mutex_unlock(&stp_policy_subsys.su_mutex);
  367. return policy_node;
  368. }
  369. void stp_policy_node_put(struct stp_policy_node *policy_node)
  370. {
  371. config_item_put(&policy_node->group.cg_item);
  372. }
  373. int __init stp_configfs_init(void)
  374. {
  375. int err;
  376. config_group_init(&stp_policy_subsys.su_group);
  377. mutex_init(&stp_policy_subsys.su_mutex);
  378. err = configfs_register_subsystem(&stp_policy_subsys);
  379. return err;
  380. }
  381. void __exit stp_configfs_exit(void)
  382. {
  383. configfs_unregister_subsystem(&stp_policy_subsys);
  384. }