policy.c 13 KB

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