intel_rdt_rdtgroup.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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.h>
  27. #include <linux/slab.h>
  28. #include <linux/cpu.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 int rdtgroup_cpus_show(struct kernfs_open_file *of,
  145. struct seq_file *s, void *v)
  146. {
  147. struct rdtgroup *rdtgrp;
  148. int ret = 0;
  149. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  150. if (rdtgrp)
  151. seq_printf(s, "%*pb\n", cpumask_pr_args(&rdtgrp->cpu_mask));
  152. else
  153. ret = -ENOENT;
  154. rdtgroup_kn_unlock(of->kn);
  155. return ret;
  156. }
  157. /*
  158. * This is safe against intel_rdt_sched_in() called from __switch_to()
  159. * because __switch_to() is executed with interrupts disabled. A local call
  160. * from rdt_update_closid() is proteced against __switch_to() because
  161. * preemption is disabled.
  162. */
  163. static void rdt_update_cpu_closid(void *closid)
  164. {
  165. if (closid)
  166. this_cpu_write(cpu_closid, *(int *)closid);
  167. /*
  168. * We cannot unconditionally write the MSR because the current
  169. * executing task might have its own closid selected. Just reuse
  170. * the context switch code.
  171. */
  172. intel_rdt_sched_in();
  173. }
  174. /*
  175. * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
  176. *
  177. * Per task closids must have been set up before calling this function.
  178. *
  179. * The per cpu closids are updated with the smp function call, when @closid
  180. * is not NULL. If @closid is NULL then all affected percpu closids must
  181. * have been set up before calling this function.
  182. */
  183. static void
  184. rdt_update_closid(const struct cpumask *cpu_mask, int *closid)
  185. {
  186. int cpu = get_cpu();
  187. if (cpumask_test_cpu(cpu, cpu_mask))
  188. rdt_update_cpu_closid(closid);
  189. smp_call_function_many(cpu_mask, rdt_update_cpu_closid, closid, 1);
  190. put_cpu();
  191. }
  192. static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
  193. char *buf, size_t nbytes, loff_t off)
  194. {
  195. cpumask_var_t tmpmask, newmask;
  196. struct rdtgroup *rdtgrp, *r;
  197. int ret;
  198. if (!buf)
  199. return -EINVAL;
  200. if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
  201. return -ENOMEM;
  202. if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
  203. free_cpumask_var(tmpmask);
  204. return -ENOMEM;
  205. }
  206. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  207. if (!rdtgrp) {
  208. ret = -ENOENT;
  209. goto unlock;
  210. }
  211. ret = cpumask_parse(buf, newmask);
  212. if (ret)
  213. goto unlock;
  214. /* check that user didn't specify any offline cpus */
  215. cpumask_andnot(tmpmask, newmask, cpu_online_mask);
  216. if (cpumask_weight(tmpmask)) {
  217. ret = -EINVAL;
  218. goto unlock;
  219. }
  220. /* Check whether cpus are dropped from this group */
  221. cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
  222. if (cpumask_weight(tmpmask)) {
  223. /* Can't drop from default group */
  224. if (rdtgrp == &rdtgroup_default) {
  225. ret = -EINVAL;
  226. goto unlock;
  227. }
  228. /* Give any dropped cpus to rdtgroup_default */
  229. cpumask_or(&rdtgroup_default.cpu_mask,
  230. &rdtgroup_default.cpu_mask, tmpmask);
  231. rdt_update_closid(tmpmask, &rdtgroup_default.closid);
  232. }
  233. /*
  234. * If we added cpus, remove them from previous group that owned them
  235. * and update per-cpu closid
  236. */
  237. cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
  238. if (cpumask_weight(tmpmask)) {
  239. list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
  240. if (r == rdtgrp)
  241. continue;
  242. cpumask_andnot(&r->cpu_mask, &r->cpu_mask, tmpmask);
  243. }
  244. rdt_update_closid(tmpmask, &rdtgrp->closid);
  245. }
  246. /* Done pushing/pulling - update this group with new mask */
  247. cpumask_copy(&rdtgrp->cpu_mask, newmask);
  248. unlock:
  249. rdtgroup_kn_unlock(of->kn);
  250. free_cpumask_var(tmpmask);
  251. free_cpumask_var(newmask);
  252. return ret ?: nbytes;
  253. }
  254. struct task_move_callback {
  255. struct callback_head work;
  256. struct rdtgroup *rdtgrp;
  257. };
  258. static void move_myself(struct callback_head *head)
  259. {
  260. struct task_move_callback *callback;
  261. struct rdtgroup *rdtgrp;
  262. callback = container_of(head, struct task_move_callback, work);
  263. rdtgrp = callback->rdtgrp;
  264. /*
  265. * If resource group was deleted before this task work callback
  266. * was invoked, then assign the task to root group and free the
  267. * resource group.
  268. */
  269. if (atomic_dec_and_test(&rdtgrp->waitcount) &&
  270. (rdtgrp->flags & RDT_DELETED)) {
  271. current->closid = 0;
  272. kfree(rdtgrp);
  273. }
  274. preempt_disable();
  275. /* update PQR_ASSOC MSR to make resource group go into effect */
  276. intel_rdt_sched_in();
  277. preempt_enable();
  278. kfree(callback);
  279. }
  280. static int __rdtgroup_move_task(struct task_struct *tsk,
  281. struct rdtgroup *rdtgrp)
  282. {
  283. struct task_move_callback *callback;
  284. int ret;
  285. callback = kzalloc(sizeof(*callback), GFP_KERNEL);
  286. if (!callback)
  287. return -ENOMEM;
  288. callback->work.func = move_myself;
  289. callback->rdtgrp = rdtgrp;
  290. /*
  291. * Take a refcount, so rdtgrp cannot be freed before the
  292. * callback has been invoked.
  293. */
  294. atomic_inc(&rdtgrp->waitcount);
  295. ret = task_work_add(tsk, &callback->work, true);
  296. if (ret) {
  297. /*
  298. * Task is exiting. Drop the refcount and free the callback.
  299. * No need to check the refcount as the group cannot be
  300. * deleted before the write function unlocks rdtgroup_mutex.
  301. */
  302. atomic_dec(&rdtgrp->waitcount);
  303. kfree(callback);
  304. } else {
  305. tsk->closid = rdtgrp->closid;
  306. }
  307. return ret;
  308. }
  309. static int rdtgroup_task_write_permission(struct task_struct *task,
  310. struct kernfs_open_file *of)
  311. {
  312. const struct cred *tcred = get_task_cred(task);
  313. const struct cred *cred = current_cred();
  314. int ret = 0;
  315. /*
  316. * Even if we're attaching all tasks in the thread group, we only
  317. * need to check permissions on one of them.
  318. */
  319. if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
  320. !uid_eq(cred->euid, tcred->uid) &&
  321. !uid_eq(cred->euid, tcred->suid))
  322. ret = -EPERM;
  323. put_cred(tcred);
  324. return ret;
  325. }
  326. static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
  327. struct kernfs_open_file *of)
  328. {
  329. struct task_struct *tsk;
  330. int ret;
  331. rcu_read_lock();
  332. if (pid) {
  333. tsk = find_task_by_vpid(pid);
  334. if (!tsk) {
  335. rcu_read_unlock();
  336. return -ESRCH;
  337. }
  338. } else {
  339. tsk = current;
  340. }
  341. get_task_struct(tsk);
  342. rcu_read_unlock();
  343. ret = rdtgroup_task_write_permission(tsk, of);
  344. if (!ret)
  345. ret = __rdtgroup_move_task(tsk, rdtgrp);
  346. put_task_struct(tsk);
  347. return ret;
  348. }
  349. static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
  350. char *buf, size_t nbytes, loff_t off)
  351. {
  352. struct rdtgroup *rdtgrp;
  353. int ret = 0;
  354. pid_t pid;
  355. if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
  356. return -EINVAL;
  357. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  358. if (rdtgrp)
  359. ret = rdtgroup_move_task(pid, rdtgrp, of);
  360. else
  361. ret = -ENOENT;
  362. rdtgroup_kn_unlock(of->kn);
  363. return ret ?: nbytes;
  364. }
  365. static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
  366. {
  367. struct task_struct *p, *t;
  368. rcu_read_lock();
  369. for_each_process_thread(p, t) {
  370. if (t->closid == r->closid)
  371. seq_printf(s, "%d\n", t->pid);
  372. }
  373. rcu_read_unlock();
  374. }
  375. static int rdtgroup_tasks_show(struct kernfs_open_file *of,
  376. struct seq_file *s, void *v)
  377. {
  378. struct rdtgroup *rdtgrp;
  379. int ret = 0;
  380. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  381. if (rdtgrp)
  382. show_rdt_tasks(rdtgrp, s);
  383. else
  384. ret = -ENOENT;
  385. rdtgroup_kn_unlock(of->kn);
  386. return ret;
  387. }
  388. /* Files in each rdtgroup */
  389. static struct rftype rdtgroup_base_files[] = {
  390. {
  391. .name = "cpus",
  392. .mode = 0644,
  393. .kf_ops = &rdtgroup_kf_single_ops,
  394. .write = rdtgroup_cpus_write,
  395. .seq_show = rdtgroup_cpus_show,
  396. },
  397. {
  398. .name = "tasks",
  399. .mode = 0644,
  400. .kf_ops = &rdtgroup_kf_single_ops,
  401. .write = rdtgroup_tasks_write,
  402. .seq_show = rdtgroup_tasks_show,
  403. },
  404. {
  405. .name = "schemata",
  406. .mode = 0644,
  407. .kf_ops = &rdtgroup_kf_single_ops,
  408. .write = rdtgroup_schemata_write,
  409. .seq_show = rdtgroup_schemata_show,
  410. },
  411. };
  412. static int rdt_num_closids_show(struct kernfs_open_file *of,
  413. struct seq_file *seq, void *v)
  414. {
  415. struct rdt_resource *r = of->kn->parent->priv;
  416. seq_printf(seq, "%d\n", r->num_closid);
  417. return 0;
  418. }
  419. static int rdt_cbm_mask_show(struct kernfs_open_file *of,
  420. struct seq_file *seq, void *v)
  421. {
  422. struct rdt_resource *r = of->kn->parent->priv;
  423. seq_printf(seq, "%x\n", r->max_cbm);
  424. return 0;
  425. }
  426. static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
  427. struct seq_file *seq, void *v)
  428. {
  429. struct rdt_resource *r = of->kn->parent->priv;
  430. seq_printf(seq, "%d\n", r->min_cbm_bits);
  431. return 0;
  432. }
  433. /* rdtgroup information files for one cache resource. */
  434. static struct rftype res_info_files[] = {
  435. {
  436. .name = "num_closids",
  437. .mode = 0444,
  438. .kf_ops = &rdtgroup_kf_single_ops,
  439. .seq_show = rdt_num_closids_show,
  440. },
  441. {
  442. .name = "cbm_mask",
  443. .mode = 0444,
  444. .kf_ops = &rdtgroup_kf_single_ops,
  445. .seq_show = rdt_cbm_mask_show,
  446. },
  447. {
  448. .name = "min_cbm_bits",
  449. .mode = 0444,
  450. .kf_ops = &rdtgroup_kf_single_ops,
  451. .seq_show = rdt_min_cbm_bits_show,
  452. },
  453. };
  454. static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
  455. {
  456. struct kernfs_node *kn_subdir;
  457. struct rdt_resource *r;
  458. int ret;
  459. /* create the directory */
  460. kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
  461. if (IS_ERR(kn_info))
  462. return PTR_ERR(kn_info);
  463. kernfs_get(kn_info);
  464. for_each_enabled_rdt_resource(r) {
  465. kn_subdir = kernfs_create_dir(kn_info, r->name,
  466. kn_info->mode, r);
  467. if (IS_ERR(kn_subdir)) {
  468. ret = PTR_ERR(kn_subdir);
  469. goto out_destroy;
  470. }
  471. kernfs_get(kn_subdir);
  472. ret = rdtgroup_kn_set_ugid(kn_subdir);
  473. if (ret)
  474. goto out_destroy;
  475. ret = rdtgroup_add_files(kn_subdir, res_info_files,
  476. ARRAY_SIZE(res_info_files));
  477. if (ret)
  478. goto out_destroy;
  479. kernfs_activate(kn_subdir);
  480. }
  481. /*
  482. * This extra ref will be put in kernfs_remove() and guarantees
  483. * that @rdtgrp->kn is always accessible.
  484. */
  485. kernfs_get(kn_info);
  486. ret = rdtgroup_kn_set_ugid(kn_info);
  487. if (ret)
  488. goto out_destroy;
  489. kernfs_activate(kn_info);
  490. return 0;
  491. out_destroy:
  492. kernfs_remove(kn_info);
  493. return ret;
  494. }
  495. static void l3_qos_cfg_update(void *arg)
  496. {
  497. bool *enable = arg;
  498. wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
  499. }
  500. static int set_l3_qos_cfg(struct rdt_resource *r, bool enable)
  501. {
  502. cpumask_var_t cpu_mask;
  503. struct rdt_domain *d;
  504. int cpu;
  505. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  506. return -ENOMEM;
  507. list_for_each_entry(d, &r->domains, list) {
  508. /* Pick one CPU from each domain instance to update MSR */
  509. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  510. }
  511. cpu = get_cpu();
  512. /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
  513. if (cpumask_test_cpu(cpu, cpu_mask))
  514. l3_qos_cfg_update(&enable);
  515. /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
  516. smp_call_function_many(cpu_mask, l3_qos_cfg_update, &enable, 1);
  517. put_cpu();
  518. free_cpumask_var(cpu_mask);
  519. return 0;
  520. }
  521. static int cdp_enable(void)
  522. {
  523. struct rdt_resource *r_l3data = &rdt_resources_all[RDT_RESOURCE_L3DATA];
  524. struct rdt_resource *r_l3code = &rdt_resources_all[RDT_RESOURCE_L3CODE];
  525. struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
  526. int ret;
  527. if (!r_l3->capable || !r_l3data->capable || !r_l3code->capable)
  528. return -EINVAL;
  529. ret = set_l3_qos_cfg(r_l3, true);
  530. if (!ret) {
  531. r_l3->enabled = false;
  532. r_l3data->enabled = true;
  533. r_l3code->enabled = true;
  534. }
  535. return ret;
  536. }
  537. static void cdp_disable(void)
  538. {
  539. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
  540. r->enabled = r->capable;
  541. if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled) {
  542. rdt_resources_all[RDT_RESOURCE_L3DATA].enabled = false;
  543. rdt_resources_all[RDT_RESOURCE_L3CODE].enabled = false;
  544. set_l3_qos_cfg(r, false);
  545. }
  546. }
  547. static int parse_rdtgroupfs_options(char *data)
  548. {
  549. char *token, *o = data;
  550. int ret = 0;
  551. while ((token = strsep(&o, ",")) != NULL) {
  552. if (!*token)
  553. return -EINVAL;
  554. if (!strcmp(token, "cdp"))
  555. ret = cdp_enable();
  556. }
  557. return ret;
  558. }
  559. /*
  560. * We don't allow rdtgroup directories to be created anywhere
  561. * except the root directory. Thus when looking for the rdtgroup
  562. * structure for a kernfs node we are either looking at a directory,
  563. * in which case the rdtgroup structure is pointed at by the "priv"
  564. * field, otherwise we have a file, and need only look to the parent
  565. * to find the rdtgroup.
  566. */
  567. static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
  568. {
  569. if (kernfs_type(kn) == KERNFS_DIR) {
  570. /*
  571. * All the resource directories use "kn->priv"
  572. * to point to the "struct rdtgroup" for the
  573. * resource. "info" and its subdirectories don't
  574. * have rdtgroup structures, so return NULL here.
  575. */
  576. if (kn == kn_info || kn->parent == kn_info)
  577. return NULL;
  578. else
  579. return kn->priv;
  580. } else {
  581. return kn->parent->priv;
  582. }
  583. }
  584. struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
  585. {
  586. struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
  587. if (!rdtgrp)
  588. return NULL;
  589. atomic_inc(&rdtgrp->waitcount);
  590. kernfs_break_active_protection(kn);
  591. mutex_lock(&rdtgroup_mutex);
  592. /* Was this group deleted while we waited? */
  593. if (rdtgrp->flags & RDT_DELETED)
  594. return NULL;
  595. return rdtgrp;
  596. }
  597. void rdtgroup_kn_unlock(struct kernfs_node *kn)
  598. {
  599. struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
  600. if (!rdtgrp)
  601. return;
  602. mutex_unlock(&rdtgroup_mutex);
  603. if (atomic_dec_and_test(&rdtgrp->waitcount) &&
  604. (rdtgrp->flags & RDT_DELETED)) {
  605. kernfs_unbreak_active_protection(kn);
  606. kernfs_put(kn);
  607. kfree(rdtgrp);
  608. } else {
  609. kernfs_unbreak_active_protection(kn);
  610. }
  611. }
  612. static struct dentry *rdt_mount(struct file_system_type *fs_type,
  613. int flags, const char *unused_dev_name,
  614. void *data)
  615. {
  616. struct dentry *dentry;
  617. int ret;
  618. mutex_lock(&rdtgroup_mutex);
  619. /*
  620. * resctrl file system can only be mounted once.
  621. */
  622. if (static_branch_unlikely(&rdt_enable_key)) {
  623. dentry = ERR_PTR(-EBUSY);
  624. goto out;
  625. }
  626. ret = parse_rdtgroupfs_options(data);
  627. if (ret) {
  628. dentry = ERR_PTR(ret);
  629. goto out_cdp;
  630. }
  631. closid_init();
  632. ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
  633. if (ret) {
  634. dentry = ERR_PTR(ret);
  635. goto out_cdp;
  636. }
  637. dentry = kernfs_mount(fs_type, flags, rdt_root,
  638. RDTGROUP_SUPER_MAGIC, NULL);
  639. if (IS_ERR(dentry))
  640. goto out_cdp;
  641. static_branch_enable(&rdt_enable_key);
  642. goto out;
  643. out_cdp:
  644. cdp_disable();
  645. out:
  646. mutex_unlock(&rdtgroup_mutex);
  647. return dentry;
  648. }
  649. static int reset_all_cbms(struct rdt_resource *r)
  650. {
  651. struct msr_param msr_param;
  652. cpumask_var_t cpu_mask;
  653. struct rdt_domain *d;
  654. int i, cpu;
  655. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  656. return -ENOMEM;
  657. msr_param.res = r;
  658. msr_param.low = 0;
  659. msr_param.high = r->num_closid;
  660. /*
  661. * Disable resource control for this resource by setting all
  662. * CBMs in all domains to the maximum mask value. Pick one CPU
  663. * from each domain to update the MSRs below.
  664. */
  665. list_for_each_entry(d, &r->domains, list) {
  666. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  667. for (i = 0; i < r->num_closid; i++)
  668. d->cbm[i] = r->max_cbm;
  669. }
  670. cpu = get_cpu();
  671. /* Update CBM on this cpu if it's in cpu_mask. */
  672. if (cpumask_test_cpu(cpu, cpu_mask))
  673. rdt_cbm_update(&msr_param);
  674. /* Update CBM on all other cpus in cpu_mask. */
  675. smp_call_function_many(cpu_mask, rdt_cbm_update, &msr_param, 1);
  676. put_cpu();
  677. free_cpumask_var(cpu_mask);
  678. return 0;
  679. }
  680. /*
  681. * Move tasks from one to the other group. If @from is NULL, then all tasks
  682. * in the systems are moved unconditionally (used for teardown).
  683. *
  684. * If @mask is not NULL the cpus on which moved tasks are running are set
  685. * in that mask so the update smp function call is restricted to affected
  686. * cpus.
  687. */
  688. static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
  689. struct cpumask *mask)
  690. {
  691. struct task_struct *p, *t;
  692. read_lock(&tasklist_lock);
  693. for_each_process_thread(p, t) {
  694. if (!from || t->closid == from->closid) {
  695. t->closid = to->closid;
  696. #ifdef CONFIG_SMP
  697. /*
  698. * This is safe on x86 w/o barriers as the ordering
  699. * of writing to task_cpu() and t->on_cpu is
  700. * reverse to the reading here. The detection is
  701. * inaccurate as tasks might move or schedule
  702. * before the smp function call takes place. In
  703. * such a case the function call is pointless, but
  704. * there is no other side effect.
  705. */
  706. if (mask && t->on_cpu)
  707. cpumask_set_cpu(task_cpu(t), mask);
  708. #endif
  709. }
  710. }
  711. read_unlock(&tasklist_lock);
  712. }
  713. /*
  714. * Forcibly remove all of subdirectories under root.
  715. */
  716. static void rmdir_all_sub(void)
  717. {
  718. struct rdtgroup *rdtgrp, *tmp;
  719. /* Move all tasks to the default resource group */
  720. rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
  721. list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
  722. /* Remove each rdtgroup other than root */
  723. if (rdtgrp == &rdtgroup_default)
  724. continue;
  725. /*
  726. * Give any CPUs back to the default group. We cannot copy
  727. * cpu_online_mask because a CPU might have executed the
  728. * offline callback already, but is still marked online.
  729. */
  730. cpumask_or(&rdtgroup_default.cpu_mask,
  731. &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
  732. kernfs_remove(rdtgrp->kn);
  733. list_del(&rdtgrp->rdtgroup_list);
  734. kfree(rdtgrp);
  735. }
  736. /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
  737. get_online_cpus();
  738. rdt_update_closid(cpu_online_mask, &rdtgroup_default.closid);
  739. put_online_cpus();
  740. kernfs_remove(kn_info);
  741. }
  742. static void rdt_kill_sb(struct super_block *sb)
  743. {
  744. struct rdt_resource *r;
  745. mutex_lock(&rdtgroup_mutex);
  746. /*Put everything back to default values. */
  747. for_each_enabled_rdt_resource(r)
  748. reset_all_cbms(r);
  749. cdp_disable();
  750. rmdir_all_sub();
  751. static_branch_disable(&rdt_enable_key);
  752. kernfs_kill_sb(sb);
  753. mutex_unlock(&rdtgroup_mutex);
  754. }
  755. static struct file_system_type rdt_fs_type = {
  756. .name = "resctrl",
  757. .mount = rdt_mount,
  758. .kill_sb = rdt_kill_sb,
  759. };
  760. static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
  761. umode_t mode)
  762. {
  763. struct rdtgroup *parent, *rdtgrp;
  764. struct kernfs_node *kn;
  765. int ret, closid;
  766. /* Only allow mkdir in the root directory */
  767. if (parent_kn != rdtgroup_default.kn)
  768. return -EPERM;
  769. /* Do not accept '\n' to avoid unparsable situation. */
  770. if (strchr(name, '\n'))
  771. return -EINVAL;
  772. parent = rdtgroup_kn_lock_live(parent_kn);
  773. if (!parent) {
  774. ret = -ENODEV;
  775. goto out_unlock;
  776. }
  777. ret = closid_alloc();
  778. if (ret < 0)
  779. goto out_unlock;
  780. closid = ret;
  781. /* allocate the rdtgroup. */
  782. rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
  783. if (!rdtgrp) {
  784. ret = -ENOSPC;
  785. goto out_closid_free;
  786. }
  787. rdtgrp->closid = closid;
  788. list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
  789. /* kernfs creates the directory for rdtgrp */
  790. kn = kernfs_create_dir(parent->kn, name, mode, rdtgrp);
  791. if (IS_ERR(kn)) {
  792. ret = PTR_ERR(kn);
  793. goto out_cancel_ref;
  794. }
  795. rdtgrp->kn = kn;
  796. /*
  797. * kernfs_remove() will drop the reference count on "kn" which
  798. * will free it. But we still need it to stick around for the
  799. * rdtgroup_kn_unlock(kn} call below. Take one extra reference
  800. * here, which will be dropped inside rdtgroup_kn_unlock().
  801. */
  802. kernfs_get(kn);
  803. ret = rdtgroup_kn_set_ugid(kn);
  804. if (ret)
  805. goto out_destroy;
  806. ret = rdtgroup_add_files(kn, rdtgroup_base_files,
  807. ARRAY_SIZE(rdtgroup_base_files));
  808. if (ret)
  809. goto out_destroy;
  810. kernfs_activate(kn);
  811. ret = 0;
  812. goto out_unlock;
  813. out_destroy:
  814. kernfs_remove(rdtgrp->kn);
  815. out_cancel_ref:
  816. list_del(&rdtgrp->rdtgroup_list);
  817. kfree(rdtgrp);
  818. out_closid_free:
  819. closid_free(closid);
  820. out_unlock:
  821. rdtgroup_kn_unlock(parent_kn);
  822. return ret;
  823. }
  824. static int rdtgroup_rmdir(struct kernfs_node *kn)
  825. {
  826. int ret, cpu, closid = rdtgroup_default.closid;
  827. struct rdtgroup *rdtgrp;
  828. cpumask_var_t tmpmask;
  829. if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
  830. return -ENOMEM;
  831. rdtgrp = rdtgroup_kn_lock_live(kn);
  832. if (!rdtgrp) {
  833. ret = -EPERM;
  834. goto out;
  835. }
  836. /* Give any tasks back to the default group */
  837. rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
  838. /* Give any CPUs back to the default group */
  839. cpumask_or(&rdtgroup_default.cpu_mask,
  840. &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
  841. /* Update per cpu closid of the moved CPUs first */
  842. for_each_cpu(cpu, &rdtgrp->cpu_mask)
  843. per_cpu(cpu_closid, cpu) = closid;
  844. /*
  845. * Update the MSR on moved CPUs and CPUs which have moved
  846. * task running on them.
  847. */
  848. cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
  849. rdt_update_closid(tmpmask, NULL);
  850. rdtgrp->flags = RDT_DELETED;
  851. closid_free(rdtgrp->closid);
  852. list_del(&rdtgrp->rdtgroup_list);
  853. /*
  854. * one extra hold on this, will drop when we kfree(rdtgrp)
  855. * in rdtgroup_kn_unlock()
  856. */
  857. kernfs_get(kn);
  858. kernfs_remove(rdtgrp->kn);
  859. ret = 0;
  860. out:
  861. rdtgroup_kn_unlock(kn);
  862. free_cpumask_var(tmpmask);
  863. return ret;
  864. }
  865. static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
  866. {
  867. if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled)
  868. seq_puts(seq, ",cdp");
  869. return 0;
  870. }
  871. static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
  872. .mkdir = rdtgroup_mkdir,
  873. .rmdir = rdtgroup_rmdir,
  874. .show_options = rdtgroup_show_options,
  875. };
  876. static int __init rdtgroup_setup_root(void)
  877. {
  878. int ret;
  879. rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
  880. KERNFS_ROOT_CREATE_DEACTIVATED,
  881. &rdtgroup_default);
  882. if (IS_ERR(rdt_root))
  883. return PTR_ERR(rdt_root);
  884. mutex_lock(&rdtgroup_mutex);
  885. rdtgroup_default.closid = 0;
  886. list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
  887. ret = rdtgroup_add_files(rdt_root->kn, rdtgroup_base_files,
  888. ARRAY_SIZE(rdtgroup_base_files));
  889. if (ret) {
  890. kernfs_destroy_root(rdt_root);
  891. goto out;
  892. }
  893. rdtgroup_default.kn = rdt_root->kn;
  894. kernfs_activate(rdtgroup_default.kn);
  895. out:
  896. mutex_unlock(&rdtgroup_mutex);
  897. return ret;
  898. }
  899. /*
  900. * rdtgroup_init - rdtgroup initialization
  901. *
  902. * Setup resctrl file system including set up root, create mount point,
  903. * register rdtgroup filesystem, and initialize files under root directory.
  904. *
  905. * Return: 0 on success or -errno
  906. */
  907. int __init rdtgroup_init(void)
  908. {
  909. int ret = 0;
  910. ret = rdtgroup_setup_root();
  911. if (ret)
  912. return ret;
  913. ret = sysfs_create_mount_point(fs_kobj, "resctrl");
  914. if (ret)
  915. goto cleanup_root;
  916. ret = register_filesystem(&rdt_fs_type);
  917. if (ret)
  918. goto cleanup_mountpoint;
  919. return 0;
  920. cleanup_mountpoint:
  921. sysfs_remove_mount_point(fs_kobj, "resctrl");
  922. cleanup_root:
  923. kernfs_destroy_root(rdt_root);
  924. return ret;
  925. }