policy.c 11 KB

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