policy.c 11 KB

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