policy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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[];
  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 struct stp_policy *to_stp_policy(struct config_item *item)
  51. {
  52. return item ?
  53. container_of(to_config_group(item), struct stp_policy, group) :
  54. NULL;
  55. }
  56. static inline struct stp_policy_node *
  57. to_stp_policy_node(struct config_item *item)
  58. {
  59. return item ?
  60. container_of(to_config_group(item), struct stp_policy_node,
  61. group) :
  62. NULL;
  63. }
  64. void *to_pdrv_policy_node(struct config_item *item)
  65. {
  66. struct stp_policy_node *node = to_stp_policy_node(item);
  67. return stp_policy_node_priv(node);
  68. }
  69. EXPORT_SYMBOL_GPL(to_pdrv_policy_node);
  70. static ssize_t
  71. stp_policy_node_masters_show(struct config_item *item, char *page)
  72. {
  73. struct stp_policy_node *policy_node = to_stp_policy_node(item);
  74. ssize_t count;
  75. count = sprintf(page, "%u %u\n", policy_node->first_master,
  76. policy_node->last_master);
  77. return count;
  78. }
  79. static ssize_t
  80. stp_policy_node_masters_store(struct config_item *item, const char *page,
  81. size_t count)
  82. {
  83. struct stp_policy_node *policy_node = to_stp_policy_node(item);
  84. unsigned int first, last;
  85. struct stm_device *stm;
  86. char *p = (char *)page;
  87. ssize_t ret = -ENODEV;
  88. if (sscanf(p, "%u %u", &first, &last) != 2)
  89. return -EINVAL;
  90. mutex_lock(&stp_policy_subsys.su_mutex);
  91. stm = policy_node->policy->stm;
  92. if (!stm)
  93. goto unlock;
  94. /* must be within [sw_start..sw_end], which is an inclusive range */
  95. if (first > last || first < stm->data->sw_start ||
  96. last > stm->data->sw_end) {
  97. ret = -ERANGE;
  98. goto unlock;
  99. }
  100. ret = count;
  101. policy_node->first_master = first;
  102. policy_node->last_master = last;
  103. unlock:
  104. mutex_unlock(&stp_policy_subsys.su_mutex);
  105. return ret;
  106. }
  107. static ssize_t
  108. stp_policy_node_channels_show(struct config_item *item, char *page)
  109. {
  110. struct stp_policy_node *policy_node = to_stp_policy_node(item);
  111. ssize_t count;
  112. count = sprintf(page, "%u %u\n", policy_node->first_channel,
  113. policy_node->last_channel);
  114. return count;
  115. }
  116. static ssize_t
  117. stp_policy_node_channels_store(struct config_item *item, const char *page,
  118. size_t count)
  119. {
  120. struct stp_policy_node *policy_node = to_stp_policy_node(item);
  121. unsigned int first, last;
  122. struct stm_device *stm;
  123. char *p = (char *)page;
  124. ssize_t ret = -ENODEV;
  125. if (sscanf(p, "%u %u", &first, &last) != 2)
  126. return -EINVAL;
  127. mutex_lock(&stp_policy_subsys.su_mutex);
  128. stm = policy_node->policy->stm;
  129. if (!stm)
  130. goto unlock;
  131. if (first > INT_MAX || last > INT_MAX || first > last ||
  132. last >= stm->data->sw_nchannels) {
  133. ret = -ERANGE;
  134. goto unlock;
  135. }
  136. ret = count;
  137. policy_node->first_channel = first;
  138. policy_node->last_channel = last;
  139. unlock:
  140. mutex_unlock(&stp_policy_subsys.su_mutex);
  141. return ret;
  142. }
  143. static void stp_policy_node_release(struct config_item *item)
  144. {
  145. struct stp_policy_node *node = to_stp_policy_node(item);
  146. kfree(node);
  147. }
  148. static struct configfs_item_operations stp_policy_node_item_ops = {
  149. .release = stp_policy_node_release,
  150. };
  151. CONFIGFS_ATTR(stp_policy_node_, masters);
  152. CONFIGFS_ATTR(stp_policy_node_, channels);
  153. static struct configfs_attribute *stp_policy_node_attrs[] = {
  154. &stp_policy_node_attr_masters,
  155. &stp_policy_node_attr_channels,
  156. NULL,
  157. };
  158. static const struct config_item_type stp_policy_type;
  159. static const struct config_item_type stp_policy_node_type;
  160. const struct config_item_type *
  161. get_policy_node_type(struct configfs_attribute **attrs)
  162. {
  163. struct config_item_type *type;
  164. struct configfs_attribute **merged;
  165. type = kmemdup(&stp_policy_node_type, sizeof(stp_policy_node_type),
  166. GFP_KERNEL);
  167. if (!type)
  168. return NULL;
  169. merged = memcat_p(stp_policy_node_attrs, attrs);
  170. if (!merged) {
  171. kfree(type);
  172. return NULL;
  173. }
  174. type->ct_attrs = merged;
  175. return type;
  176. }
  177. static struct config_group *
  178. stp_policy_node_make(struct config_group *group, const char *name)
  179. {
  180. const struct config_item_type *type = &stp_policy_node_type;
  181. struct stp_policy_node *policy_node, *parent_node;
  182. const struct stm_protocol_driver *pdrv;
  183. struct stp_policy *policy;
  184. if (group->cg_item.ci_type == &stp_policy_type) {
  185. policy = container_of(group, struct stp_policy, group);
  186. } else {
  187. parent_node = container_of(group, struct stp_policy_node,
  188. group);
  189. policy = parent_node->policy;
  190. }
  191. if (!policy->stm)
  192. return ERR_PTR(-ENODEV);
  193. pdrv = policy->stm->pdrv;
  194. policy_node =
  195. kzalloc(offsetof(struct stp_policy_node, priv[pdrv->priv_sz]),
  196. GFP_KERNEL);
  197. if (!policy_node)
  198. return ERR_PTR(-ENOMEM);
  199. if (pdrv->policy_node_init)
  200. pdrv->policy_node_init((void *)policy_node->priv);
  201. if (policy->stm->pdrv_node_type)
  202. type = policy->stm->pdrv_node_type;
  203. config_group_init_type_name(&policy_node->group, name, type);
  204. policy_node->policy = policy;
  205. /* default values for the attributes */
  206. policy_node->first_master = policy->stm->data->sw_start;
  207. policy_node->last_master = policy->stm->data->sw_end;
  208. policy_node->first_channel = 0;
  209. policy_node->last_channel = policy->stm->data->sw_nchannels - 1;
  210. return &policy_node->group;
  211. }
  212. static void
  213. stp_policy_node_drop(struct config_group *group, struct config_item *item)
  214. {
  215. config_item_put(item);
  216. }
  217. static struct configfs_group_operations stp_policy_node_group_ops = {
  218. .make_group = stp_policy_node_make,
  219. .drop_item = stp_policy_node_drop,
  220. };
  221. static const struct config_item_type stp_policy_node_type = {
  222. .ct_item_ops = &stp_policy_node_item_ops,
  223. .ct_group_ops = &stp_policy_node_group_ops,
  224. .ct_attrs = stp_policy_node_attrs,
  225. .ct_owner = THIS_MODULE,
  226. };
  227. /*
  228. * Root group: policies.
  229. */
  230. static ssize_t stp_policy_device_show(struct config_item *item,
  231. char *page)
  232. {
  233. struct stp_policy *policy = to_stp_policy(item);
  234. ssize_t count;
  235. count = sprintf(page, "%s\n",
  236. (policy && policy->stm) ?
  237. policy->stm->data->name :
  238. "<none>");
  239. return count;
  240. }
  241. CONFIGFS_ATTR_RO(stp_policy_, device);
  242. static ssize_t stp_policy_protocol_show(struct config_item *item,
  243. char *page)
  244. {
  245. struct stp_policy *policy = to_stp_policy(item);
  246. ssize_t count;
  247. count = sprintf(page, "%s\n",
  248. (policy && policy->stm) ?
  249. policy->stm->pdrv->name :
  250. "<none>");
  251. return count;
  252. }
  253. CONFIGFS_ATTR_RO(stp_policy_, protocol);
  254. static struct configfs_attribute *stp_policy_attrs[] = {
  255. &stp_policy_attr_device,
  256. &stp_policy_attr_protocol,
  257. NULL,
  258. };
  259. void stp_policy_unbind(struct stp_policy *policy)
  260. {
  261. struct stm_device *stm = policy->stm;
  262. /*
  263. * stp_policy_release() will not call here if the policy is already
  264. * unbound; other users should not either, as no link exists between
  265. * this policy and anything else in that case
  266. */
  267. if (WARN_ON_ONCE(!policy->stm))
  268. return;
  269. lockdep_assert_held(&stm->policy_mutex);
  270. stm->policy = NULL;
  271. policy->stm = NULL;
  272. /*
  273. * Drop the reference on the protocol driver and lose the link.
  274. */
  275. stm_put_protocol(stm->pdrv);
  276. stm->pdrv = NULL;
  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. ret = ERR_PTR(-ENOMEM);
  355. goto unlock_policy;
  356. }
  357. config_group_init_type_name(&stm->policy->group, name,
  358. &stp_policy_type);
  359. stm->pdrv = pdrv;
  360. stm->pdrv_node_type = pdrv_node_type;
  361. stm->policy->stm = stm;
  362. ret = &stm->policy->group;
  363. unlock_policy:
  364. mutex_unlock(&stm->policy_mutex);
  365. if (IS_ERR(ret)) {
  366. /*
  367. * pdrv and stm->pdrv at this point can be quite different,
  368. * and only one of them needs to be 'put'
  369. */
  370. stm_put_protocol(pdrv);
  371. stm_put_device(stm);
  372. }
  373. return ret;
  374. }
  375. static struct configfs_group_operations stp_policy_root_group_ops = {
  376. .make_group = stp_policy_make,
  377. };
  378. static const struct config_item_type stp_policy_root_type = {
  379. .ct_group_ops = &stp_policy_root_group_ops,
  380. .ct_owner = THIS_MODULE,
  381. };
  382. static struct configfs_subsystem stp_policy_subsys = {
  383. .su_group = {
  384. .cg_item = {
  385. .ci_namebuf = "stp-policy",
  386. .ci_type = &stp_policy_root_type,
  387. },
  388. },
  389. };
  390. /*
  391. * Lock the policy mutex from the outside
  392. */
  393. static struct stp_policy_node *
  394. __stp_policy_node_lookup(struct stp_policy *policy, char *s)
  395. {
  396. struct stp_policy_node *policy_node, *ret = NULL;
  397. struct list_head *head = &policy->group.cg_children;
  398. struct config_item *item;
  399. char *start, *end = s;
  400. if (list_empty(head))
  401. return NULL;
  402. next:
  403. for (;;) {
  404. start = strsep(&end, "/");
  405. if (!start)
  406. break;
  407. if (!*start)
  408. continue;
  409. list_for_each_entry(item, head, ci_entry) {
  410. policy_node = to_stp_policy_node(item);
  411. if (!strcmp(start,
  412. policy_node->group.cg_item.ci_name)) {
  413. ret = policy_node;
  414. if (!end)
  415. goto out;
  416. head = &policy_node->group.cg_children;
  417. goto next;
  418. }
  419. }
  420. break;
  421. }
  422. out:
  423. return ret;
  424. }
  425. struct stp_policy_node *
  426. stp_policy_node_lookup(struct stm_device *stm, char *s)
  427. {
  428. struct stp_policy_node *policy_node = NULL;
  429. mutex_lock(&stp_policy_subsys.su_mutex);
  430. mutex_lock(&stm->policy_mutex);
  431. if (stm->policy)
  432. policy_node = __stp_policy_node_lookup(stm->policy, s);
  433. mutex_unlock(&stm->policy_mutex);
  434. if (policy_node)
  435. config_item_get(&policy_node->group.cg_item);
  436. else
  437. mutex_unlock(&stp_policy_subsys.su_mutex);
  438. return policy_node;
  439. }
  440. void stp_policy_node_put(struct stp_policy_node *policy_node)
  441. {
  442. lockdep_assert_held(&stp_policy_subsys.su_mutex);
  443. mutex_unlock(&stp_policy_subsys.su_mutex);
  444. config_item_put(&policy_node->group.cg_item);
  445. }
  446. int __init stp_configfs_init(void)
  447. {
  448. config_group_init(&stp_policy_subsys.su_group);
  449. mutex_init(&stp_policy_subsys.su_mutex);
  450. return configfs_register_subsystem(&stp_policy_subsys);
  451. }
  452. void __exit stp_configfs_exit(void)
  453. {
  454. configfs_unregister_subsystem(&stp_policy_subsys);
  455. }