intel_rdt_rdtgroup.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /*
  2. * User interface for Resource Alloction in Resource Director Technology(RDT)
  3. *
  4. * Copyright (C) 2016 Intel Corporation
  5. *
  6. * Author: Fenghua Yu <fenghua.yu@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * More information about RDT be found in the Intel (R) x86 Architecture
  18. * Software Developer Manual.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/cpu.h>
  22. #include <linux/fs.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/kernfs.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/sched/signal.h>
  27. #include <linux/sched/task.h>
  28. #include <linux/slab.h>
  29. #include <linux/task_work.h>
  30. #include <uapi/linux/magic.h>
  31. #include <asm/intel_rdt.h>
  32. #include <asm/intel_rdt_common.h>
  33. DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
  34. struct kernfs_root *rdt_root;
  35. struct rdtgroup rdtgroup_default;
  36. LIST_HEAD(rdt_all_groups);
  37. /* Kernel fs node for "info" directory under root */
  38. static struct kernfs_node *kn_info;
  39. /*
  40. * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
  41. * we can keep a bitmap of free CLOSIDs in a single integer.
  42. *
  43. * Using a global CLOSID across all resources has some advantages and
  44. * some drawbacks:
  45. * + We can simply set "current->closid" to assign a task to a resource
  46. * group.
  47. * + Context switch code can avoid extra memory references deciding which
  48. * CLOSID to load into the PQR_ASSOC MSR
  49. * - We give up some options in configuring resource groups across multi-socket
  50. * systems.
  51. * - Our choices on how to configure each resource become progressively more
  52. * limited as the number of resources grows.
  53. */
  54. static int closid_free_map;
  55. static void closid_init(void)
  56. {
  57. struct rdt_resource *r;
  58. int rdt_min_closid = 32;
  59. /* Compute rdt_min_closid across all resources */
  60. for_each_enabled_rdt_resource(r)
  61. rdt_min_closid = min(rdt_min_closid, r->num_closid);
  62. closid_free_map = BIT_MASK(rdt_min_closid) - 1;
  63. /* CLOSID 0 is always reserved for the default group */
  64. closid_free_map &= ~1;
  65. }
  66. int closid_alloc(void)
  67. {
  68. int closid = ffs(closid_free_map);
  69. if (closid == 0)
  70. return -ENOSPC;
  71. closid--;
  72. closid_free_map &= ~(1 << closid);
  73. return closid;
  74. }
  75. static void closid_free(int closid)
  76. {
  77. closid_free_map |= 1 << closid;
  78. }
  79. /* set uid and gid of rdtgroup dirs and files to that of the creator */
  80. static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
  81. {
  82. struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
  83. .ia_uid = current_fsuid(),
  84. .ia_gid = current_fsgid(), };
  85. if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
  86. gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
  87. return 0;
  88. return kernfs_setattr(kn, &iattr);
  89. }
  90. static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
  91. {
  92. struct kernfs_node *kn;
  93. int ret;
  94. kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
  95. 0, rft->kf_ops, rft, NULL, NULL);
  96. if (IS_ERR(kn))
  97. return PTR_ERR(kn);
  98. ret = rdtgroup_kn_set_ugid(kn);
  99. if (ret) {
  100. kernfs_remove(kn);
  101. return ret;
  102. }
  103. return 0;
  104. }
  105. static int rdtgroup_add_files(struct kernfs_node *kn, struct rftype *rfts,
  106. int len)
  107. {
  108. struct rftype *rft;
  109. int ret;
  110. lockdep_assert_held(&rdtgroup_mutex);
  111. for (rft = rfts; rft < rfts + len; rft++) {
  112. ret = rdtgroup_add_file(kn, rft);
  113. if (ret)
  114. goto error;
  115. }
  116. return 0;
  117. error:
  118. pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
  119. while (--rft >= rfts)
  120. kernfs_remove_by_name(kn, rft->name);
  121. return ret;
  122. }
  123. static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
  124. {
  125. struct kernfs_open_file *of = m->private;
  126. struct rftype *rft = of->kn->priv;
  127. if (rft->seq_show)
  128. return rft->seq_show(of, m, arg);
  129. return 0;
  130. }
  131. static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
  132. size_t nbytes, loff_t off)
  133. {
  134. struct rftype *rft = of->kn->priv;
  135. if (rft->write)
  136. return rft->write(of, buf, nbytes, off);
  137. return -EINVAL;
  138. }
  139. static struct kernfs_ops rdtgroup_kf_single_ops = {
  140. .atomic_write_len = PAGE_SIZE,
  141. .write = rdtgroup_file_write,
  142. .seq_show = rdtgroup_seqfile_show,
  143. };
  144. static bool is_cpu_list(struct kernfs_open_file *of)
  145. {
  146. struct rftype *rft = of->kn->priv;
  147. return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
  148. }
  149. static int rdtgroup_cpus_show(struct kernfs_open_file *of,
  150. struct seq_file *s, void *v)
  151. {
  152. struct rdtgroup *rdtgrp;
  153. int ret = 0;
  154. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  155. if (rdtgrp) {
  156. seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
  157. cpumask_pr_args(&rdtgrp->cpu_mask));
  158. } else {
  159. ret = -ENOENT;
  160. }
  161. rdtgroup_kn_unlock(of->kn);
  162. return ret;
  163. }
  164. /*
  165. * This is safe against intel_rdt_sched_in() called from __switch_to()
  166. * because __switch_to() is executed with interrupts disabled. A local call
  167. * from rdt_update_closid() is proteced against __switch_to() because
  168. * preemption is disabled.
  169. */
  170. static void rdt_update_cpu_closid(void *closid)
  171. {
  172. if (closid)
  173. this_cpu_write(cpu_closid, *(int *)closid);
  174. /*
  175. * We cannot unconditionally write the MSR because the current
  176. * executing task might have its own closid selected. Just reuse
  177. * the context switch code.
  178. */
  179. intel_rdt_sched_in();
  180. }
  181. /*
  182. * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
  183. *
  184. * Per task closids must have been set up before calling this function.
  185. *
  186. * The per cpu closids are updated with the smp function call, when @closid
  187. * is not NULL. If @closid is NULL then all affected percpu closids must
  188. * have been set up before calling this function.
  189. */
  190. static void
  191. rdt_update_closid(const struct cpumask *cpu_mask, int *closid)
  192. {
  193. int cpu = get_cpu();
  194. if (cpumask_test_cpu(cpu, cpu_mask))
  195. rdt_update_cpu_closid(closid);
  196. smp_call_function_many(cpu_mask, rdt_update_cpu_closid, closid, 1);
  197. put_cpu();
  198. }
  199. static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
  200. char *buf, size_t nbytes, loff_t off)
  201. {
  202. cpumask_var_t tmpmask, newmask;
  203. struct rdtgroup *rdtgrp, *r;
  204. int ret;
  205. if (!buf)
  206. return -EINVAL;
  207. if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
  208. return -ENOMEM;
  209. if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
  210. free_cpumask_var(tmpmask);
  211. return -ENOMEM;
  212. }
  213. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  214. if (!rdtgrp) {
  215. ret = -ENOENT;
  216. goto unlock;
  217. }
  218. if (is_cpu_list(of))
  219. ret = cpulist_parse(buf, newmask);
  220. else
  221. ret = cpumask_parse(buf, newmask);
  222. if (ret)
  223. goto unlock;
  224. /* check that user didn't specify any offline cpus */
  225. cpumask_andnot(tmpmask, newmask, cpu_online_mask);
  226. if (cpumask_weight(tmpmask)) {
  227. ret = -EINVAL;
  228. goto unlock;
  229. }
  230. /* Check whether cpus are dropped from this group */
  231. cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
  232. if (cpumask_weight(tmpmask)) {
  233. /* Can't drop from default group */
  234. if (rdtgrp == &rdtgroup_default) {
  235. ret = -EINVAL;
  236. goto unlock;
  237. }
  238. /* Give any dropped cpus to rdtgroup_default */
  239. cpumask_or(&rdtgroup_default.cpu_mask,
  240. &rdtgroup_default.cpu_mask, tmpmask);
  241. rdt_update_closid(tmpmask, &rdtgroup_default.closid);
  242. }
  243. /*
  244. * If we added cpus, remove them from previous group that owned them
  245. * and update per-cpu closid
  246. */
  247. cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
  248. if (cpumask_weight(tmpmask)) {
  249. list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
  250. if (r == rdtgrp)
  251. continue;
  252. cpumask_andnot(&r->cpu_mask, &r->cpu_mask, tmpmask);
  253. }
  254. rdt_update_closid(tmpmask, &rdtgrp->closid);
  255. }
  256. /* Done pushing/pulling - update this group with new mask */
  257. cpumask_copy(&rdtgrp->cpu_mask, newmask);
  258. unlock:
  259. rdtgroup_kn_unlock(of->kn);
  260. free_cpumask_var(tmpmask);
  261. free_cpumask_var(newmask);
  262. return ret ?: nbytes;
  263. }
  264. struct task_move_callback {
  265. struct callback_head work;
  266. struct rdtgroup *rdtgrp;
  267. };
  268. static void move_myself(struct callback_head *head)
  269. {
  270. struct task_move_callback *callback;
  271. struct rdtgroup *rdtgrp;
  272. callback = container_of(head, struct task_move_callback, work);
  273. rdtgrp = callback->rdtgrp;
  274. /*
  275. * If resource group was deleted before this task work callback
  276. * was invoked, then assign the task to root group and free the
  277. * resource group.
  278. */
  279. if (atomic_dec_and_test(&rdtgrp->waitcount) &&
  280. (rdtgrp->flags & RDT_DELETED)) {
  281. current->closid = 0;
  282. kfree(rdtgrp);
  283. }
  284. preempt_disable();
  285. /* update PQR_ASSOC MSR to make resource group go into effect */
  286. intel_rdt_sched_in();
  287. preempt_enable();
  288. kfree(callback);
  289. }
  290. static int __rdtgroup_move_task(struct task_struct *tsk,
  291. struct rdtgroup *rdtgrp)
  292. {
  293. struct task_move_callback *callback;
  294. int ret;
  295. callback = kzalloc(sizeof(*callback), GFP_KERNEL);
  296. if (!callback)
  297. return -ENOMEM;
  298. callback->work.func = move_myself;
  299. callback->rdtgrp = rdtgrp;
  300. /*
  301. * Take a refcount, so rdtgrp cannot be freed before the
  302. * callback has been invoked.
  303. */
  304. atomic_inc(&rdtgrp->waitcount);
  305. ret = task_work_add(tsk, &callback->work, true);
  306. if (ret) {
  307. /*
  308. * Task is exiting. Drop the refcount and free the callback.
  309. * No need to check the refcount as the group cannot be
  310. * deleted before the write function unlocks rdtgroup_mutex.
  311. */
  312. atomic_dec(&rdtgrp->waitcount);
  313. kfree(callback);
  314. } else {
  315. tsk->closid = rdtgrp->closid;
  316. }
  317. return ret;
  318. }
  319. static int rdtgroup_task_write_permission(struct task_struct *task,
  320. struct kernfs_open_file *of)
  321. {
  322. const struct cred *tcred = get_task_cred(task);
  323. const struct cred *cred = current_cred();
  324. int ret = 0;
  325. /*
  326. * Even if we're attaching all tasks in the thread group, we only
  327. * need to check permissions on one of them.
  328. */
  329. if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
  330. !uid_eq(cred->euid, tcred->uid) &&
  331. !uid_eq(cred->euid, tcred->suid))
  332. ret = -EPERM;
  333. put_cred(tcred);
  334. return ret;
  335. }
  336. static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
  337. struct kernfs_open_file *of)
  338. {
  339. struct task_struct *tsk;
  340. int ret;
  341. rcu_read_lock();
  342. if (pid) {
  343. tsk = find_task_by_vpid(pid);
  344. if (!tsk) {
  345. rcu_read_unlock();
  346. return -ESRCH;
  347. }
  348. } else {
  349. tsk = current;
  350. }
  351. get_task_struct(tsk);
  352. rcu_read_unlock();
  353. ret = rdtgroup_task_write_permission(tsk, of);
  354. if (!ret)
  355. ret = __rdtgroup_move_task(tsk, rdtgrp);
  356. put_task_struct(tsk);
  357. return ret;
  358. }
  359. static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
  360. char *buf, size_t nbytes, loff_t off)
  361. {
  362. struct rdtgroup *rdtgrp;
  363. int ret = 0;
  364. pid_t pid;
  365. if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
  366. return -EINVAL;
  367. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  368. if (rdtgrp)
  369. ret = rdtgroup_move_task(pid, rdtgrp, of);
  370. else
  371. ret = -ENOENT;
  372. rdtgroup_kn_unlock(of->kn);
  373. return ret ?: nbytes;
  374. }
  375. static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
  376. {
  377. struct task_struct *p, *t;
  378. rcu_read_lock();
  379. for_each_process_thread(p, t) {
  380. if (t->closid == r->closid)
  381. seq_printf(s, "%d\n", t->pid);
  382. }
  383. rcu_read_unlock();
  384. }
  385. static int rdtgroup_tasks_show(struct kernfs_open_file *of,
  386. struct seq_file *s, void *v)
  387. {
  388. struct rdtgroup *rdtgrp;
  389. int ret = 0;
  390. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  391. if (rdtgrp)
  392. show_rdt_tasks(rdtgrp, s);
  393. else
  394. ret = -ENOENT;
  395. rdtgroup_kn_unlock(of->kn);
  396. return ret;
  397. }
  398. /* Files in each rdtgroup */
  399. static struct rftype rdtgroup_base_files[] = {
  400. {
  401. .name = "cpus",
  402. .mode = 0644,
  403. .kf_ops = &rdtgroup_kf_single_ops,
  404. .write = rdtgroup_cpus_write,
  405. .seq_show = rdtgroup_cpus_show,
  406. },
  407. {
  408. .name = "cpus_list",
  409. .mode = 0644,
  410. .kf_ops = &rdtgroup_kf_single_ops,
  411. .write = rdtgroup_cpus_write,
  412. .seq_show = rdtgroup_cpus_show,
  413. .flags = RFTYPE_FLAGS_CPUS_LIST,
  414. },
  415. {
  416. .name = "tasks",
  417. .mode = 0644,
  418. .kf_ops = &rdtgroup_kf_single_ops,
  419. .write = rdtgroup_tasks_write,
  420. .seq_show = rdtgroup_tasks_show,
  421. },
  422. {
  423. .name = "schemata",
  424. .mode = 0644,
  425. .kf_ops = &rdtgroup_kf_single_ops,
  426. .write = rdtgroup_schemata_write,
  427. .seq_show = rdtgroup_schemata_show,
  428. },
  429. };
  430. static int rdt_num_closids_show(struct kernfs_open_file *of,
  431. struct seq_file *seq, void *v)
  432. {
  433. struct rdt_resource *r = of->kn->parent->priv;
  434. seq_printf(seq, "%d\n", r->num_closid);
  435. return 0;
  436. }
  437. static int rdt_default_ctrl_show(struct kernfs_open_file *of,
  438. struct seq_file *seq, void *v)
  439. {
  440. struct rdt_resource *r = of->kn->parent->priv;
  441. seq_printf(seq, "%x\n", r->default_ctrl);
  442. return 0;
  443. }
  444. static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
  445. struct seq_file *seq, void *v)
  446. {
  447. struct rdt_resource *r = of->kn->parent->priv;
  448. seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
  449. return 0;
  450. }
  451. static int rdt_min_bw_show(struct kernfs_open_file *of,
  452. struct seq_file *seq, void *v)
  453. {
  454. struct rdt_resource *r = of->kn->parent->priv;
  455. seq_printf(seq, "%u\n", r->membw.min_bw);
  456. return 0;
  457. }
  458. static int rdt_bw_gran_show(struct kernfs_open_file *of,
  459. struct seq_file *seq, void *v)
  460. {
  461. struct rdt_resource *r = of->kn->parent->priv;
  462. seq_printf(seq, "%u\n", r->membw.bw_gran);
  463. return 0;
  464. }
  465. static int rdt_delay_linear_show(struct kernfs_open_file *of,
  466. struct seq_file *seq, void *v)
  467. {
  468. struct rdt_resource *r = of->kn->parent->priv;
  469. seq_printf(seq, "%u\n", r->membw.delay_linear);
  470. return 0;
  471. }
  472. /* rdtgroup information files for one cache resource. */
  473. static struct rftype res_cache_info_files[] = {
  474. {
  475. .name = "num_closids",
  476. .mode = 0444,
  477. .kf_ops = &rdtgroup_kf_single_ops,
  478. .seq_show = rdt_num_closids_show,
  479. },
  480. {
  481. .name = "cbm_mask",
  482. .mode = 0444,
  483. .kf_ops = &rdtgroup_kf_single_ops,
  484. .seq_show = rdt_default_ctrl_show,
  485. },
  486. {
  487. .name = "min_cbm_bits",
  488. .mode = 0444,
  489. .kf_ops = &rdtgroup_kf_single_ops,
  490. .seq_show = rdt_min_cbm_bits_show,
  491. },
  492. };
  493. /* rdtgroup information files for memory bandwidth. */
  494. static struct rftype res_mba_info_files[] = {
  495. {
  496. .name = "num_closids",
  497. .mode = 0444,
  498. .kf_ops = &rdtgroup_kf_single_ops,
  499. .seq_show = rdt_num_closids_show,
  500. },
  501. {
  502. .name = "min_bandwidth",
  503. .mode = 0444,
  504. .kf_ops = &rdtgroup_kf_single_ops,
  505. .seq_show = rdt_min_bw_show,
  506. },
  507. {
  508. .name = "bandwidth_gran",
  509. .mode = 0444,
  510. .kf_ops = &rdtgroup_kf_single_ops,
  511. .seq_show = rdt_bw_gran_show,
  512. },
  513. {
  514. .name = "delay_linear",
  515. .mode = 0444,
  516. .kf_ops = &rdtgroup_kf_single_ops,
  517. .seq_show = rdt_delay_linear_show,
  518. },
  519. };
  520. void rdt_get_mba_infofile(struct rdt_resource *r)
  521. {
  522. r->info_files = res_mba_info_files;
  523. r->nr_info_files = ARRAY_SIZE(res_mba_info_files);
  524. }
  525. void rdt_get_cache_infofile(struct rdt_resource *r)
  526. {
  527. r->info_files = res_cache_info_files;
  528. r->nr_info_files = ARRAY_SIZE(res_cache_info_files);
  529. }
  530. static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
  531. {
  532. struct kernfs_node *kn_subdir;
  533. struct rftype *res_info_files;
  534. struct rdt_resource *r;
  535. int ret, len;
  536. /* create the directory */
  537. kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
  538. if (IS_ERR(kn_info))
  539. return PTR_ERR(kn_info);
  540. kernfs_get(kn_info);
  541. for_each_enabled_rdt_resource(r) {
  542. kn_subdir = kernfs_create_dir(kn_info, r->name,
  543. kn_info->mode, r);
  544. if (IS_ERR(kn_subdir)) {
  545. ret = PTR_ERR(kn_subdir);
  546. goto out_destroy;
  547. }
  548. kernfs_get(kn_subdir);
  549. ret = rdtgroup_kn_set_ugid(kn_subdir);
  550. if (ret)
  551. goto out_destroy;
  552. res_info_files = r->info_files;
  553. len = r->nr_info_files;
  554. ret = rdtgroup_add_files(kn_subdir, res_info_files, len);
  555. if (ret)
  556. goto out_destroy;
  557. kernfs_activate(kn_subdir);
  558. }
  559. /*
  560. * This extra ref will be put in kernfs_remove() and guarantees
  561. * that @rdtgrp->kn is always accessible.
  562. */
  563. kernfs_get(kn_info);
  564. ret = rdtgroup_kn_set_ugid(kn_info);
  565. if (ret)
  566. goto out_destroy;
  567. kernfs_activate(kn_info);
  568. return 0;
  569. out_destroy:
  570. kernfs_remove(kn_info);
  571. return ret;
  572. }
  573. static void l3_qos_cfg_update(void *arg)
  574. {
  575. bool *enable = arg;
  576. wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
  577. }
  578. static int set_l3_qos_cfg(struct rdt_resource *r, bool enable)
  579. {
  580. cpumask_var_t cpu_mask;
  581. struct rdt_domain *d;
  582. int cpu;
  583. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  584. return -ENOMEM;
  585. list_for_each_entry(d, &r->domains, list) {
  586. /* Pick one CPU from each domain instance to update MSR */
  587. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  588. }
  589. cpu = get_cpu();
  590. /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
  591. if (cpumask_test_cpu(cpu, cpu_mask))
  592. l3_qos_cfg_update(&enable);
  593. /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
  594. smp_call_function_many(cpu_mask, l3_qos_cfg_update, &enable, 1);
  595. put_cpu();
  596. free_cpumask_var(cpu_mask);
  597. return 0;
  598. }
  599. static int cdp_enable(void)
  600. {
  601. struct rdt_resource *r_l3data = &rdt_resources_all[RDT_RESOURCE_L3DATA];
  602. struct rdt_resource *r_l3code = &rdt_resources_all[RDT_RESOURCE_L3CODE];
  603. struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
  604. int ret;
  605. if (!r_l3->capable || !r_l3data->capable || !r_l3code->capable)
  606. return -EINVAL;
  607. ret = set_l3_qos_cfg(r_l3, true);
  608. if (!ret) {
  609. r_l3->enabled = false;
  610. r_l3data->enabled = true;
  611. r_l3code->enabled = true;
  612. }
  613. return ret;
  614. }
  615. static void cdp_disable(void)
  616. {
  617. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
  618. r->enabled = r->capable;
  619. if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled) {
  620. rdt_resources_all[RDT_RESOURCE_L3DATA].enabled = false;
  621. rdt_resources_all[RDT_RESOURCE_L3CODE].enabled = false;
  622. set_l3_qos_cfg(r, false);
  623. }
  624. }
  625. static int parse_rdtgroupfs_options(char *data)
  626. {
  627. char *token, *o = data;
  628. int ret = 0;
  629. while ((token = strsep(&o, ",")) != NULL) {
  630. if (!*token)
  631. return -EINVAL;
  632. if (!strcmp(token, "cdp"))
  633. ret = cdp_enable();
  634. }
  635. return ret;
  636. }
  637. /*
  638. * We don't allow rdtgroup directories to be created anywhere
  639. * except the root directory. Thus when looking for the rdtgroup
  640. * structure for a kernfs node we are either looking at a directory,
  641. * in which case the rdtgroup structure is pointed at by the "priv"
  642. * field, otherwise we have a file, and need only look to the parent
  643. * to find the rdtgroup.
  644. */
  645. static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
  646. {
  647. if (kernfs_type(kn) == KERNFS_DIR) {
  648. /*
  649. * All the resource directories use "kn->priv"
  650. * to point to the "struct rdtgroup" for the
  651. * resource. "info" and its subdirectories don't
  652. * have rdtgroup structures, so return NULL here.
  653. */
  654. if (kn == kn_info || kn->parent == kn_info)
  655. return NULL;
  656. else
  657. return kn->priv;
  658. } else {
  659. return kn->parent->priv;
  660. }
  661. }
  662. struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
  663. {
  664. struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
  665. if (!rdtgrp)
  666. return NULL;
  667. atomic_inc(&rdtgrp->waitcount);
  668. kernfs_break_active_protection(kn);
  669. mutex_lock(&rdtgroup_mutex);
  670. /* Was this group deleted while we waited? */
  671. if (rdtgrp->flags & RDT_DELETED)
  672. return NULL;
  673. return rdtgrp;
  674. }
  675. void rdtgroup_kn_unlock(struct kernfs_node *kn)
  676. {
  677. struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
  678. if (!rdtgrp)
  679. return;
  680. mutex_unlock(&rdtgroup_mutex);
  681. if (atomic_dec_and_test(&rdtgrp->waitcount) &&
  682. (rdtgrp->flags & RDT_DELETED)) {
  683. kernfs_unbreak_active_protection(kn);
  684. kernfs_put(rdtgrp->kn);
  685. kfree(rdtgrp);
  686. } else {
  687. kernfs_unbreak_active_protection(kn);
  688. }
  689. }
  690. static struct dentry *rdt_mount(struct file_system_type *fs_type,
  691. int flags, const char *unused_dev_name,
  692. void *data)
  693. {
  694. struct dentry *dentry;
  695. int ret;
  696. mutex_lock(&rdtgroup_mutex);
  697. /*
  698. * resctrl file system can only be mounted once.
  699. */
  700. if (static_branch_unlikely(&rdt_enable_key)) {
  701. dentry = ERR_PTR(-EBUSY);
  702. goto out;
  703. }
  704. ret = parse_rdtgroupfs_options(data);
  705. if (ret) {
  706. dentry = ERR_PTR(ret);
  707. goto out_cdp;
  708. }
  709. closid_init();
  710. ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
  711. if (ret) {
  712. dentry = ERR_PTR(ret);
  713. goto out_cdp;
  714. }
  715. dentry = kernfs_mount(fs_type, flags, rdt_root,
  716. RDTGROUP_SUPER_MAGIC, NULL);
  717. if (IS_ERR(dentry))
  718. goto out_cdp;
  719. static_branch_enable(&rdt_enable_key);
  720. goto out;
  721. out_cdp:
  722. cdp_disable();
  723. out:
  724. mutex_unlock(&rdtgroup_mutex);
  725. return dentry;
  726. }
  727. static int reset_all_ctrls(struct rdt_resource *r)
  728. {
  729. struct msr_param msr_param;
  730. cpumask_var_t cpu_mask;
  731. struct rdt_domain *d;
  732. int i, cpu;
  733. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  734. return -ENOMEM;
  735. msr_param.res = r;
  736. msr_param.low = 0;
  737. msr_param.high = r->num_closid;
  738. /*
  739. * Disable resource control for this resource by setting all
  740. * CBMs in all domains to the maximum mask value. Pick one CPU
  741. * from each domain to update the MSRs below.
  742. */
  743. list_for_each_entry(d, &r->domains, list) {
  744. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  745. for (i = 0; i < r->num_closid; i++)
  746. d->ctrl_val[i] = r->default_ctrl;
  747. }
  748. cpu = get_cpu();
  749. /* Update CBM on this cpu if it's in cpu_mask. */
  750. if (cpumask_test_cpu(cpu, cpu_mask))
  751. rdt_ctrl_update(&msr_param);
  752. /* Update CBM on all other cpus in cpu_mask. */
  753. smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
  754. put_cpu();
  755. free_cpumask_var(cpu_mask);
  756. return 0;
  757. }
  758. /*
  759. * Move tasks from one to the other group. If @from is NULL, then all tasks
  760. * in the systems are moved unconditionally (used for teardown).
  761. *
  762. * If @mask is not NULL the cpus on which moved tasks are running are set
  763. * in that mask so the update smp function call is restricted to affected
  764. * cpus.
  765. */
  766. static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
  767. struct cpumask *mask)
  768. {
  769. struct task_struct *p, *t;
  770. read_lock(&tasklist_lock);
  771. for_each_process_thread(p, t) {
  772. if (!from || t->closid == from->closid) {
  773. t->closid = to->closid;
  774. #ifdef CONFIG_SMP
  775. /*
  776. * This is safe on x86 w/o barriers as the ordering
  777. * of writing to task_cpu() and t->on_cpu is
  778. * reverse to the reading here. The detection is
  779. * inaccurate as tasks might move or schedule
  780. * before the smp function call takes place. In
  781. * such a case the function call is pointless, but
  782. * there is no other side effect.
  783. */
  784. if (mask && t->on_cpu)
  785. cpumask_set_cpu(task_cpu(t), mask);
  786. #endif
  787. }
  788. }
  789. read_unlock(&tasklist_lock);
  790. }
  791. /*
  792. * Forcibly remove all of subdirectories under root.
  793. */
  794. static void rmdir_all_sub(void)
  795. {
  796. struct rdtgroup *rdtgrp, *tmp;
  797. /* Move all tasks to the default resource group */
  798. rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
  799. list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
  800. /* Remove each rdtgroup other than root */
  801. if (rdtgrp == &rdtgroup_default)
  802. continue;
  803. /*
  804. * Give any CPUs back to the default group. We cannot copy
  805. * cpu_online_mask because a CPU might have executed the
  806. * offline callback already, but is still marked online.
  807. */
  808. cpumask_or(&rdtgroup_default.cpu_mask,
  809. &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
  810. kernfs_remove(rdtgrp->kn);
  811. list_del(&rdtgrp->rdtgroup_list);
  812. kfree(rdtgrp);
  813. }
  814. /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
  815. get_online_cpus();
  816. rdt_update_closid(cpu_online_mask, &rdtgroup_default.closid);
  817. put_online_cpus();
  818. kernfs_remove(kn_info);
  819. }
  820. static void rdt_kill_sb(struct super_block *sb)
  821. {
  822. struct rdt_resource *r;
  823. mutex_lock(&rdtgroup_mutex);
  824. /*Put everything back to default values. */
  825. for_each_enabled_rdt_resource(r)
  826. reset_all_ctrls(r);
  827. cdp_disable();
  828. rmdir_all_sub();
  829. static_branch_disable(&rdt_enable_key);
  830. kernfs_kill_sb(sb);
  831. mutex_unlock(&rdtgroup_mutex);
  832. }
  833. static struct file_system_type rdt_fs_type = {
  834. .name = "resctrl",
  835. .mount = rdt_mount,
  836. .kill_sb = rdt_kill_sb,
  837. };
  838. static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
  839. umode_t mode)
  840. {
  841. struct rdtgroup *parent, *rdtgrp;
  842. struct kernfs_node *kn;
  843. int ret, closid;
  844. /* Only allow mkdir in the root directory */
  845. if (parent_kn != rdtgroup_default.kn)
  846. return -EPERM;
  847. /* Do not accept '\n' to avoid unparsable situation. */
  848. if (strchr(name, '\n'))
  849. return -EINVAL;
  850. parent = rdtgroup_kn_lock_live(parent_kn);
  851. if (!parent) {
  852. ret = -ENODEV;
  853. goto out_unlock;
  854. }
  855. ret = closid_alloc();
  856. if (ret < 0)
  857. goto out_unlock;
  858. closid = ret;
  859. /* allocate the rdtgroup. */
  860. rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
  861. if (!rdtgrp) {
  862. ret = -ENOSPC;
  863. goto out_closid_free;
  864. }
  865. rdtgrp->closid = closid;
  866. list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
  867. /* kernfs creates the directory for rdtgrp */
  868. kn = kernfs_create_dir(parent->kn, name, mode, rdtgrp);
  869. if (IS_ERR(kn)) {
  870. ret = PTR_ERR(kn);
  871. goto out_cancel_ref;
  872. }
  873. rdtgrp->kn = kn;
  874. /*
  875. * kernfs_remove() will drop the reference count on "kn" which
  876. * will free it. But we still need it to stick around for the
  877. * rdtgroup_kn_unlock(kn} call below. Take one extra reference
  878. * here, which will be dropped inside rdtgroup_kn_unlock().
  879. */
  880. kernfs_get(kn);
  881. ret = rdtgroup_kn_set_ugid(kn);
  882. if (ret)
  883. goto out_destroy;
  884. ret = rdtgroup_add_files(kn, rdtgroup_base_files,
  885. ARRAY_SIZE(rdtgroup_base_files));
  886. if (ret)
  887. goto out_destroy;
  888. kernfs_activate(kn);
  889. ret = 0;
  890. goto out_unlock;
  891. out_destroy:
  892. kernfs_remove(rdtgrp->kn);
  893. out_cancel_ref:
  894. list_del(&rdtgrp->rdtgroup_list);
  895. kfree(rdtgrp);
  896. out_closid_free:
  897. closid_free(closid);
  898. out_unlock:
  899. rdtgroup_kn_unlock(parent_kn);
  900. return ret;
  901. }
  902. static int rdtgroup_rmdir(struct kernfs_node *kn)
  903. {
  904. int ret, cpu, closid = rdtgroup_default.closid;
  905. struct rdtgroup *rdtgrp;
  906. cpumask_var_t tmpmask;
  907. if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
  908. return -ENOMEM;
  909. rdtgrp = rdtgroup_kn_lock_live(kn);
  910. if (!rdtgrp) {
  911. ret = -EPERM;
  912. goto out;
  913. }
  914. /* Give any tasks back to the default group */
  915. rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
  916. /* Give any CPUs back to the default group */
  917. cpumask_or(&rdtgroup_default.cpu_mask,
  918. &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
  919. /* Update per cpu closid of the moved CPUs first */
  920. for_each_cpu(cpu, &rdtgrp->cpu_mask)
  921. per_cpu(cpu_closid, cpu) = closid;
  922. /*
  923. * Update the MSR on moved CPUs and CPUs which have moved
  924. * task running on them.
  925. */
  926. cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
  927. rdt_update_closid(tmpmask, NULL);
  928. rdtgrp->flags = RDT_DELETED;
  929. closid_free(rdtgrp->closid);
  930. list_del(&rdtgrp->rdtgroup_list);
  931. /*
  932. * one extra hold on this, will drop when we kfree(rdtgrp)
  933. * in rdtgroup_kn_unlock()
  934. */
  935. kernfs_get(kn);
  936. kernfs_remove(rdtgrp->kn);
  937. ret = 0;
  938. out:
  939. rdtgroup_kn_unlock(kn);
  940. free_cpumask_var(tmpmask);
  941. return ret;
  942. }
  943. static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
  944. {
  945. if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled)
  946. seq_puts(seq, ",cdp");
  947. return 0;
  948. }
  949. static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
  950. .mkdir = rdtgroup_mkdir,
  951. .rmdir = rdtgroup_rmdir,
  952. .show_options = rdtgroup_show_options,
  953. };
  954. static int __init rdtgroup_setup_root(void)
  955. {
  956. int ret;
  957. rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
  958. KERNFS_ROOT_CREATE_DEACTIVATED,
  959. &rdtgroup_default);
  960. if (IS_ERR(rdt_root))
  961. return PTR_ERR(rdt_root);
  962. mutex_lock(&rdtgroup_mutex);
  963. rdtgroup_default.closid = 0;
  964. list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
  965. ret = rdtgroup_add_files(rdt_root->kn, rdtgroup_base_files,
  966. ARRAY_SIZE(rdtgroup_base_files));
  967. if (ret) {
  968. kernfs_destroy_root(rdt_root);
  969. goto out;
  970. }
  971. rdtgroup_default.kn = rdt_root->kn;
  972. kernfs_activate(rdtgroup_default.kn);
  973. out:
  974. mutex_unlock(&rdtgroup_mutex);
  975. return ret;
  976. }
  977. /*
  978. * rdtgroup_init - rdtgroup initialization
  979. *
  980. * Setup resctrl file system including set up root, create mount point,
  981. * register rdtgroup filesystem, and initialize files under root directory.
  982. *
  983. * Return: 0 on success or -errno
  984. */
  985. int __init rdtgroup_init(void)
  986. {
  987. int ret = 0;
  988. ret = rdtgroup_setup_root();
  989. if (ret)
  990. return ret;
  991. ret = sysfs_create_mount_point(fs_kobj, "resctrl");
  992. if (ret)
  993. goto cleanup_root;
  994. ret = register_filesystem(&rdt_fs_type);
  995. if (ret)
  996. goto cleanup_mountpoint;
  997. return 0;
  998. cleanup_mountpoint:
  999. sysfs_remove_mount_point(fs_kobj, "resctrl");
  1000. cleanup_root:
  1001. kernfs_destroy_root(rdt_root);
  1002. return ret;
  1003. }