intel_rdt_rdtgroup.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041
  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/cacheinfo.h>
  22. #include <linux/cpu.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/fs.h>
  25. #include <linux/sysfs.h>
  26. #include <linux/kernfs.h>
  27. #include <linux/seq_buf.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/sched/signal.h>
  30. #include <linux/sched/task.h>
  31. #include <linux/slab.h>
  32. #include <linux/task_work.h>
  33. #include <uapi/linux/magic.h>
  34. #include <asm/intel_rdt_sched.h>
  35. #include "intel_rdt.h"
  36. DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
  37. DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key);
  38. DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
  39. static struct kernfs_root *rdt_root;
  40. struct rdtgroup rdtgroup_default;
  41. LIST_HEAD(rdt_all_groups);
  42. /* Kernel fs node for "info" directory under root */
  43. static struct kernfs_node *kn_info;
  44. /* Kernel fs node for "mon_groups" directory under root */
  45. static struct kernfs_node *kn_mongrp;
  46. /* Kernel fs node for "mon_data" directory under root */
  47. static struct kernfs_node *kn_mondata;
  48. static struct seq_buf last_cmd_status;
  49. static char last_cmd_status_buf[512];
  50. struct dentry *debugfs_resctrl;
  51. void rdt_last_cmd_clear(void)
  52. {
  53. lockdep_assert_held(&rdtgroup_mutex);
  54. seq_buf_clear(&last_cmd_status);
  55. }
  56. void rdt_last_cmd_puts(const char *s)
  57. {
  58. lockdep_assert_held(&rdtgroup_mutex);
  59. seq_buf_puts(&last_cmd_status, s);
  60. }
  61. void rdt_last_cmd_printf(const char *fmt, ...)
  62. {
  63. va_list ap;
  64. va_start(ap, fmt);
  65. lockdep_assert_held(&rdtgroup_mutex);
  66. seq_buf_vprintf(&last_cmd_status, fmt, ap);
  67. va_end(ap);
  68. }
  69. /*
  70. * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
  71. * we can keep a bitmap of free CLOSIDs in a single integer.
  72. *
  73. * Using a global CLOSID across all resources has some advantages and
  74. * some drawbacks:
  75. * + We can simply set "current->closid" to assign a task to a resource
  76. * group.
  77. * + Context switch code can avoid extra memory references deciding which
  78. * CLOSID to load into the PQR_ASSOC MSR
  79. * - We give up some options in configuring resource groups across multi-socket
  80. * systems.
  81. * - Our choices on how to configure each resource become progressively more
  82. * limited as the number of resources grows.
  83. */
  84. static int closid_free_map;
  85. static int closid_free_map_len;
  86. int closids_supported(void)
  87. {
  88. return closid_free_map_len;
  89. }
  90. static void closid_init(void)
  91. {
  92. struct rdt_resource *r;
  93. int rdt_min_closid = 32;
  94. /* Compute rdt_min_closid across all resources */
  95. for_each_alloc_enabled_rdt_resource(r)
  96. rdt_min_closid = min(rdt_min_closid, r->num_closid);
  97. closid_free_map = BIT_MASK(rdt_min_closid) - 1;
  98. /* CLOSID 0 is always reserved for the default group */
  99. closid_free_map &= ~1;
  100. closid_free_map_len = rdt_min_closid;
  101. }
  102. static int closid_alloc(void)
  103. {
  104. u32 closid = ffs(closid_free_map);
  105. if (closid == 0)
  106. return -ENOSPC;
  107. closid--;
  108. closid_free_map &= ~(1 << closid);
  109. return closid;
  110. }
  111. void closid_free(int closid)
  112. {
  113. closid_free_map |= 1 << closid;
  114. }
  115. /**
  116. * closid_allocated - test if provided closid is in use
  117. * @closid: closid to be tested
  118. *
  119. * Return: true if @closid is currently associated with a resource group,
  120. * false if @closid is free
  121. */
  122. static bool closid_allocated(unsigned int closid)
  123. {
  124. return (closid_free_map & (1 << closid)) == 0;
  125. }
  126. /**
  127. * rdtgroup_mode_by_closid - Return mode of resource group with closid
  128. * @closid: closid if the resource group
  129. *
  130. * Each resource group is associated with a @closid. Here the mode
  131. * of a resource group can be queried by searching for it using its closid.
  132. *
  133. * Return: mode as &enum rdtgrp_mode of resource group with closid @closid
  134. */
  135. enum rdtgrp_mode rdtgroup_mode_by_closid(int closid)
  136. {
  137. struct rdtgroup *rdtgrp;
  138. list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
  139. if (rdtgrp->closid == closid)
  140. return rdtgrp->mode;
  141. }
  142. return RDT_NUM_MODES;
  143. }
  144. static const char * const rdt_mode_str[] = {
  145. [RDT_MODE_SHAREABLE] = "shareable",
  146. [RDT_MODE_EXCLUSIVE] = "exclusive",
  147. [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup",
  148. [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked",
  149. };
  150. /**
  151. * rdtgroup_mode_str - Return the string representation of mode
  152. * @mode: the resource group mode as &enum rdtgroup_mode
  153. *
  154. * Return: string representation of valid mode, "unknown" otherwise
  155. */
  156. static const char *rdtgroup_mode_str(enum rdtgrp_mode mode)
  157. {
  158. if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES)
  159. return "unknown";
  160. return rdt_mode_str[mode];
  161. }
  162. /* set uid and gid of rdtgroup dirs and files to that of the creator */
  163. static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
  164. {
  165. struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
  166. .ia_uid = current_fsuid(),
  167. .ia_gid = current_fsgid(), };
  168. if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
  169. gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
  170. return 0;
  171. return kernfs_setattr(kn, &iattr);
  172. }
  173. static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
  174. {
  175. struct kernfs_node *kn;
  176. int ret;
  177. kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
  178. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
  179. 0, rft->kf_ops, rft, NULL, NULL);
  180. if (IS_ERR(kn))
  181. return PTR_ERR(kn);
  182. ret = rdtgroup_kn_set_ugid(kn);
  183. if (ret) {
  184. kernfs_remove(kn);
  185. return ret;
  186. }
  187. return 0;
  188. }
  189. static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
  190. {
  191. struct kernfs_open_file *of = m->private;
  192. struct rftype *rft = of->kn->priv;
  193. if (rft->seq_show)
  194. return rft->seq_show(of, m, arg);
  195. return 0;
  196. }
  197. static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
  198. size_t nbytes, loff_t off)
  199. {
  200. struct rftype *rft = of->kn->priv;
  201. if (rft->write)
  202. return rft->write(of, buf, nbytes, off);
  203. return -EINVAL;
  204. }
  205. static struct kernfs_ops rdtgroup_kf_single_ops = {
  206. .atomic_write_len = PAGE_SIZE,
  207. .write = rdtgroup_file_write,
  208. .seq_show = rdtgroup_seqfile_show,
  209. };
  210. static struct kernfs_ops kf_mondata_ops = {
  211. .atomic_write_len = PAGE_SIZE,
  212. .seq_show = rdtgroup_mondata_show,
  213. };
  214. static bool is_cpu_list(struct kernfs_open_file *of)
  215. {
  216. struct rftype *rft = of->kn->priv;
  217. return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
  218. }
  219. static int rdtgroup_cpus_show(struct kernfs_open_file *of,
  220. struct seq_file *s, void *v)
  221. {
  222. struct rdtgroup *rdtgrp;
  223. struct cpumask *mask;
  224. int ret = 0;
  225. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  226. if (rdtgrp) {
  227. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  228. if (!rdtgrp->plr->d) {
  229. rdt_last_cmd_clear();
  230. rdt_last_cmd_puts("Cache domain offline\n");
  231. ret = -ENODEV;
  232. } else {
  233. mask = &rdtgrp->plr->d->cpu_mask;
  234. seq_printf(s, is_cpu_list(of) ?
  235. "%*pbl\n" : "%*pb\n",
  236. cpumask_pr_args(mask));
  237. }
  238. } else {
  239. seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
  240. cpumask_pr_args(&rdtgrp->cpu_mask));
  241. }
  242. } else {
  243. ret = -ENOENT;
  244. }
  245. rdtgroup_kn_unlock(of->kn);
  246. return ret;
  247. }
  248. /*
  249. * This is safe against intel_rdt_sched_in() called from __switch_to()
  250. * because __switch_to() is executed with interrupts disabled. A local call
  251. * from update_closid_rmid() is proteced against __switch_to() because
  252. * preemption is disabled.
  253. */
  254. static void update_cpu_closid_rmid(void *info)
  255. {
  256. struct rdtgroup *r = info;
  257. if (r) {
  258. this_cpu_write(pqr_state.default_closid, r->closid);
  259. this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
  260. }
  261. /*
  262. * We cannot unconditionally write the MSR because the current
  263. * executing task might have its own closid selected. Just reuse
  264. * the context switch code.
  265. */
  266. intel_rdt_sched_in();
  267. }
  268. /*
  269. * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
  270. *
  271. * Per task closids/rmids must have been set up before calling this function.
  272. */
  273. static void
  274. update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
  275. {
  276. int cpu = get_cpu();
  277. if (cpumask_test_cpu(cpu, cpu_mask))
  278. update_cpu_closid_rmid(r);
  279. smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1);
  280. put_cpu();
  281. }
  282. static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
  283. cpumask_var_t tmpmask)
  284. {
  285. struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
  286. struct list_head *head;
  287. /* Check whether cpus belong to parent ctrl group */
  288. cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
  289. if (cpumask_weight(tmpmask)) {
  290. rdt_last_cmd_puts("can only add CPUs to mongroup that belong to parent\n");
  291. return -EINVAL;
  292. }
  293. /* Check whether cpus are dropped from this group */
  294. cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
  295. if (cpumask_weight(tmpmask)) {
  296. /* Give any dropped cpus to parent rdtgroup */
  297. cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
  298. update_closid_rmid(tmpmask, prgrp);
  299. }
  300. /*
  301. * If we added cpus, remove them from previous group that owned them
  302. * and update per-cpu rmid
  303. */
  304. cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
  305. if (cpumask_weight(tmpmask)) {
  306. head = &prgrp->mon.crdtgrp_list;
  307. list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
  308. if (crgrp == rdtgrp)
  309. continue;
  310. cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
  311. tmpmask);
  312. }
  313. update_closid_rmid(tmpmask, rdtgrp);
  314. }
  315. /* Done pushing/pulling - update this group with new mask */
  316. cpumask_copy(&rdtgrp->cpu_mask, newmask);
  317. return 0;
  318. }
  319. static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
  320. {
  321. struct rdtgroup *crgrp;
  322. cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
  323. /* update the child mon group masks as well*/
  324. list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
  325. cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
  326. }
  327. static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
  328. cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
  329. {
  330. struct rdtgroup *r, *crgrp;
  331. struct list_head *head;
  332. /* Check whether cpus are dropped from this group */
  333. cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
  334. if (cpumask_weight(tmpmask)) {
  335. /* Can't drop from default group */
  336. if (rdtgrp == &rdtgroup_default) {
  337. rdt_last_cmd_puts("Can't drop CPUs from default group\n");
  338. return -EINVAL;
  339. }
  340. /* Give any dropped cpus to rdtgroup_default */
  341. cpumask_or(&rdtgroup_default.cpu_mask,
  342. &rdtgroup_default.cpu_mask, tmpmask);
  343. update_closid_rmid(tmpmask, &rdtgroup_default);
  344. }
  345. /*
  346. * If we added cpus, remove them from previous group and
  347. * the prev group's child groups that owned them
  348. * and update per-cpu closid/rmid.
  349. */
  350. cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
  351. if (cpumask_weight(tmpmask)) {
  352. list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
  353. if (r == rdtgrp)
  354. continue;
  355. cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
  356. if (cpumask_weight(tmpmask1))
  357. cpumask_rdtgrp_clear(r, tmpmask1);
  358. }
  359. update_closid_rmid(tmpmask, rdtgrp);
  360. }
  361. /* Done pushing/pulling - update this group with new mask */
  362. cpumask_copy(&rdtgrp->cpu_mask, newmask);
  363. /*
  364. * Clear child mon group masks since there is a new parent mask
  365. * now and update the rmid for the cpus the child lost.
  366. */
  367. head = &rdtgrp->mon.crdtgrp_list;
  368. list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
  369. cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
  370. update_closid_rmid(tmpmask, rdtgrp);
  371. cpumask_clear(&crgrp->cpu_mask);
  372. }
  373. return 0;
  374. }
  375. static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
  376. char *buf, size_t nbytes, loff_t off)
  377. {
  378. cpumask_var_t tmpmask, newmask, tmpmask1;
  379. struct rdtgroup *rdtgrp;
  380. int ret;
  381. if (!buf)
  382. return -EINVAL;
  383. if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
  384. return -ENOMEM;
  385. if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
  386. free_cpumask_var(tmpmask);
  387. return -ENOMEM;
  388. }
  389. if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
  390. free_cpumask_var(tmpmask);
  391. free_cpumask_var(newmask);
  392. return -ENOMEM;
  393. }
  394. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  395. rdt_last_cmd_clear();
  396. if (!rdtgrp) {
  397. ret = -ENOENT;
  398. rdt_last_cmd_puts("directory was removed\n");
  399. goto unlock;
  400. }
  401. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
  402. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  403. ret = -EINVAL;
  404. rdt_last_cmd_puts("pseudo-locking in progress\n");
  405. goto unlock;
  406. }
  407. if (is_cpu_list(of))
  408. ret = cpulist_parse(buf, newmask);
  409. else
  410. ret = cpumask_parse(buf, newmask);
  411. if (ret) {
  412. rdt_last_cmd_puts("bad cpu list/mask\n");
  413. goto unlock;
  414. }
  415. /* check that user didn't specify any offline cpus */
  416. cpumask_andnot(tmpmask, newmask, cpu_online_mask);
  417. if (cpumask_weight(tmpmask)) {
  418. ret = -EINVAL;
  419. rdt_last_cmd_puts("can only assign online cpus\n");
  420. goto unlock;
  421. }
  422. if (rdtgrp->type == RDTCTRL_GROUP)
  423. ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
  424. else if (rdtgrp->type == RDTMON_GROUP)
  425. ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
  426. else
  427. ret = -EINVAL;
  428. unlock:
  429. rdtgroup_kn_unlock(of->kn);
  430. free_cpumask_var(tmpmask);
  431. free_cpumask_var(newmask);
  432. free_cpumask_var(tmpmask1);
  433. return ret ?: nbytes;
  434. }
  435. /**
  436. * rdtgroup_remove - the helper to remove resource group safely
  437. * @rdtgrp: resource group to remove
  438. *
  439. * On resource group creation via a mkdir, an extra kernfs_node reference is
  440. * taken to ensure that the rdtgroup structure remains accessible for the
  441. * rdtgroup_kn_unlock() calls where it is removed.
  442. *
  443. * Drop the extra reference here, then free the rdtgroup structure.
  444. *
  445. * Return: void
  446. */
  447. static void rdtgroup_remove(struct rdtgroup *rdtgrp)
  448. {
  449. kernfs_put(rdtgrp->kn);
  450. kfree(rdtgrp);
  451. }
  452. static void _update_task_closid_rmid(void *task)
  453. {
  454. /*
  455. * If the task is still current on this CPU, update PQR_ASSOC MSR.
  456. * Otherwise, the MSR is updated when the task is scheduled in.
  457. */
  458. if (task == current)
  459. intel_rdt_sched_in();
  460. }
  461. static void update_task_closid_rmid(struct task_struct *t)
  462. {
  463. if (IS_ENABLED(CONFIG_SMP) && task_curr(t))
  464. smp_call_function_single(task_cpu(t), _update_task_closid_rmid, t, 1);
  465. else
  466. _update_task_closid_rmid(t);
  467. }
  468. static int __rdtgroup_move_task(struct task_struct *tsk,
  469. struct rdtgroup *rdtgrp)
  470. {
  471. /* If the task is already in rdtgrp, no need to move the task. */
  472. if ((rdtgrp->type == RDTCTRL_GROUP && tsk->closid == rdtgrp->closid &&
  473. tsk->rmid == rdtgrp->mon.rmid) ||
  474. (rdtgrp->type == RDTMON_GROUP && tsk->rmid == rdtgrp->mon.rmid &&
  475. tsk->closid == rdtgrp->mon.parent->closid))
  476. return 0;
  477. /*
  478. * Set the task's closid/rmid before the PQR_ASSOC MSR can be
  479. * updated by them.
  480. *
  481. * For ctrl_mon groups, move both closid and rmid.
  482. * For monitor groups, can move the tasks only from
  483. * their parent CTRL group.
  484. */
  485. if (rdtgrp->type == RDTCTRL_GROUP) {
  486. tsk->closid = rdtgrp->closid;
  487. tsk->rmid = rdtgrp->mon.rmid;
  488. } else if (rdtgrp->type == RDTMON_GROUP) {
  489. if (rdtgrp->mon.parent->closid == tsk->closid) {
  490. tsk->rmid = rdtgrp->mon.rmid;
  491. } else {
  492. rdt_last_cmd_puts("Can't move task to different control group\n");
  493. return -EINVAL;
  494. }
  495. }
  496. /*
  497. * Ensure the task's closid and rmid are written before determining if
  498. * the task is current that will decide if it will be interrupted.
  499. */
  500. barrier();
  501. /*
  502. * By now, the task's closid and rmid are set. If the task is current
  503. * on a CPU, the PQR_ASSOC MSR needs to be updated to make the resource
  504. * group go into effect. If the task is not current, the MSR will be
  505. * updated when the task is scheduled in.
  506. */
  507. update_task_closid_rmid(tsk);
  508. return 0;
  509. }
  510. /**
  511. * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group
  512. * @r: Resource group
  513. *
  514. * Return: 1 if tasks have been assigned to @r, 0 otherwise
  515. */
  516. int rdtgroup_tasks_assigned(struct rdtgroup *r)
  517. {
  518. struct task_struct *p, *t;
  519. int ret = 0;
  520. lockdep_assert_held(&rdtgroup_mutex);
  521. rcu_read_lock();
  522. for_each_process_thread(p, t) {
  523. if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) ||
  524. (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid)) {
  525. ret = 1;
  526. break;
  527. }
  528. }
  529. rcu_read_unlock();
  530. return ret;
  531. }
  532. static int rdtgroup_task_write_permission(struct task_struct *task,
  533. struct kernfs_open_file *of)
  534. {
  535. const struct cred *tcred = get_task_cred(task);
  536. const struct cred *cred = current_cred();
  537. int ret = 0;
  538. /*
  539. * Even if we're attaching all tasks in the thread group, we only
  540. * need to check permissions on one of them.
  541. */
  542. if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
  543. !uid_eq(cred->euid, tcred->uid) &&
  544. !uid_eq(cred->euid, tcred->suid)) {
  545. rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
  546. ret = -EPERM;
  547. }
  548. put_cred(tcred);
  549. return ret;
  550. }
  551. static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
  552. struct kernfs_open_file *of)
  553. {
  554. struct task_struct *tsk;
  555. int ret;
  556. rcu_read_lock();
  557. if (pid) {
  558. tsk = find_task_by_vpid(pid);
  559. if (!tsk) {
  560. rcu_read_unlock();
  561. rdt_last_cmd_printf("No task %d\n", pid);
  562. return -ESRCH;
  563. }
  564. } else {
  565. tsk = current;
  566. }
  567. get_task_struct(tsk);
  568. rcu_read_unlock();
  569. ret = rdtgroup_task_write_permission(tsk, of);
  570. if (!ret)
  571. ret = __rdtgroup_move_task(tsk, rdtgrp);
  572. put_task_struct(tsk);
  573. return ret;
  574. }
  575. static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
  576. char *buf, size_t nbytes, loff_t off)
  577. {
  578. struct rdtgroup *rdtgrp;
  579. int ret = 0;
  580. pid_t pid;
  581. if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
  582. return -EINVAL;
  583. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  584. if (!rdtgrp) {
  585. rdtgroup_kn_unlock(of->kn);
  586. return -ENOENT;
  587. }
  588. rdt_last_cmd_clear();
  589. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
  590. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  591. ret = -EINVAL;
  592. rdt_last_cmd_puts("pseudo-locking in progress\n");
  593. goto unlock;
  594. }
  595. ret = rdtgroup_move_task(pid, rdtgrp, of);
  596. unlock:
  597. rdtgroup_kn_unlock(of->kn);
  598. return ret ?: nbytes;
  599. }
  600. static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
  601. {
  602. struct task_struct *p, *t;
  603. rcu_read_lock();
  604. for_each_process_thread(p, t) {
  605. if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) ||
  606. (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid))
  607. seq_printf(s, "%d\n", t->pid);
  608. }
  609. rcu_read_unlock();
  610. }
  611. static int rdtgroup_tasks_show(struct kernfs_open_file *of,
  612. struct seq_file *s, void *v)
  613. {
  614. struct rdtgroup *rdtgrp;
  615. int ret = 0;
  616. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  617. if (rdtgrp)
  618. show_rdt_tasks(rdtgrp, s);
  619. else
  620. ret = -ENOENT;
  621. rdtgroup_kn_unlock(of->kn);
  622. return ret;
  623. }
  624. static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
  625. struct seq_file *seq, void *v)
  626. {
  627. int len;
  628. mutex_lock(&rdtgroup_mutex);
  629. len = seq_buf_used(&last_cmd_status);
  630. if (len)
  631. seq_printf(seq, "%.*s", len, last_cmd_status_buf);
  632. else
  633. seq_puts(seq, "ok\n");
  634. mutex_unlock(&rdtgroup_mutex);
  635. return 0;
  636. }
  637. static int rdt_num_closids_show(struct kernfs_open_file *of,
  638. struct seq_file *seq, void *v)
  639. {
  640. struct rdt_resource *r = of->kn->parent->priv;
  641. seq_printf(seq, "%d\n", r->num_closid);
  642. return 0;
  643. }
  644. static int rdt_default_ctrl_show(struct kernfs_open_file *of,
  645. struct seq_file *seq, void *v)
  646. {
  647. struct rdt_resource *r = of->kn->parent->priv;
  648. seq_printf(seq, "%x\n", r->default_ctrl);
  649. return 0;
  650. }
  651. static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
  652. struct seq_file *seq, void *v)
  653. {
  654. struct rdt_resource *r = of->kn->parent->priv;
  655. seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
  656. return 0;
  657. }
  658. static int rdt_shareable_bits_show(struct kernfs_open_file *of,
  659. struct seq_file *seq, void *v)
  660. {
  661. struct rdt_resource *r = of->kn->parent->priv;
  662. seq_printf(seq, "%x\n", r->cache.shareable_bits);
  663. return 0;
  664. }
  665. /**
  666. * rdt_bit_usage_show - Display current usage of resources
  667. *
  668. * A domain is a shared resource that can now be allocated differently. Here
  669. * we display the current regions of the domain as an annotated bitmask.
  670. * For each domain of this resource its allocation bitmask
  671. * is annotated as below to indicate the current usage of the corresponding bit:
  672. * 0 - currently unused
  673. * X - currently available for sharing and used by software and hardware
  674. * H - currently used by hardware only but available for software use
  675. * S - currently used and shareable by software only
  676. * E - currently used exclusively by one resource group
  677. * P - currently pseudo-locked by one resource group
  678. */
  679. static int rdt_bit_usage_show(struct kernfs_open_file *of,
  680. struct seq_file *seq, void *v)
  681. {
  682. struct rdt_resource *r = of->kn->parent->priv;
  683. /*
  684. * Use unsigned long even though only 32 bits are used to ensure
  685. * test_bit() is used safely.
  686. */
  687. unsigned long sw_shareable = 0, hw_shareable = 0;
  688. unsigned long exclusive = 0, pseudo_locked = 0;
  689. struct rdt_domain *dom;
  690. int i, hwb, swb, excl, psl;
  691. enum rdtgrp_mode mode;
  692. bool sep = false;
  693. u32 *ctrl;
  694. mutex_lock(&rdtgroup_mutex);
  695. hw_shareable = r->cache.shareable_bits;
  696. list_for_each_entry(dom, &r->domains, list) {
  697. if (sep)
  698. seq_putc(seq, ';');
  699. ctrl = dom->ctrl_val;
  700. sw_shareable = 0;
  701. exclusive = 0;
  702. seq_printf(seq, "%d=", dom->id);
  703. for (i = 0; i < closids_supported(); i++, ctrl++) {
  704. if (!closid_allocated(i))
  705. continue;
  706. mode = rdtgroup_mode_by_closid(i);
  707. switch (mode) {
  708. case RDT_MODE_SHAREABLE:
  709. sw_shareable |= *ctrl;
  710. break;
  711. case RDT_MODE_EXCLUSIVE:
  712. exclusive |= *ctrl;
  713. break;
  714. case RDT_MODE_PSEUDO_LOCKSETUP:
  715. /*
  716. * RDT_MODE_PSEUDO_LOCKSETUP is possible
  717. * here but not included since the CBM
  718. * associated with this CLOSID in this mode
  719. * is not initialized and no task or cpu can be
  720. * assigned this CLOSID.
  721. */
  722. break;
  723. case RDT_MODE_PSEUDO_LOCKED:
  724. case RDT_NUM_MODES:
  725. WARN(1,
  726. "invalid mode for closid %d\n", i);
  727. break;
  728. }
  729. }
  730. for (i = r->cache.cbm_len - 1; i >= 0; i--) {
  731. pseudo_locked = dom->plr ? dom->plr->cbm : 0;
  732. hwb = test_bit(i, &hw_shareable);
  733. swb = test_bit(i, &sw_shareable);
  734. excl = test_bit(i, &exclusive);
  735. psl = test_bit(i, &pseudo_locked);
  736. if (hwb && swb)
  737. seq_putc(seq, 'X');
  738. else if (hwb && !swb)
  739. seq_putc(seq, 'H');
  740. else if (!hwb && swb)
  741. seq_putc(seq, 'S');
  742. else if (excl)
  743. seq_putc(seq, 'E');
  744. else if (psl)
  745. seq_putc(seq, 'P');
  746. else /* Unused bits remain */
  747. seq_putc(seq, '0');
  748. }
  749. sep = true;
  750. }
  751. seq_putc(seq, '\n');
  752. mutex_unlock(&rdtgroup_mutex);
  753. return 0;
  754. }
  755. static int rdt_min_bw_show(struct kernfs_open_file *of,
  756. struct seq_file *seq, void *v)
  757. {
  758. struct rdt_resource *r = of->kn->parent->priv;
  759. seq_printf(seq, "%u\n", r->membw.min_bw);
  760. return 0;
  761. }
  762. static int rdt_num_rmids_show(struct kernfs_open_file *of,
  763. struct seq_file *seq, void *v)
  764. {
  765. struct rdt_resource *r = of->kn->parent->priv;
  766. seq_printf(seq, "%d\n", r->num_rmid);
  767. return 0;
  768. }
  769. static int rdt_mon_features_show(struct kernfs_open_file *of,
  770. struct seq_file *seq, void *v)
  771. {
  772. struct rdt_resource *r = of->kn->parent->priv;
  773. struct mon_evt *mevt;
  774. list_for_each_entry(mevt, &r->evt_list, list)
  775. seq_printf(seq, "%s\n", mevt->name);
  776. return 0;
  777. }
  778. static int rdt_bw_gran_show(struct kernfs_open_file *of,
  779. struct seq_file *seq, void *v)
  780. {
  781. struct rdt_resource *r = of->kn->parent->priv;
  782. seq_printf(seq, "%u\n", r->membw.bw_gran);
  783. return 0;
  784. }
  785. static int rdt_delay_linear_show(struct kernfs_open_file *of,
  786. struct seq_file *seq, void *v)
  787. {
  788. struct rdt_resource *r = of->kn->parent->priv;
  789. seq_printf(seq, "%u\n", r->membw.delay_linear);
  790. return 0;
  791. }
  792. static int max_threshold_occ_show(struct kernfs_open_file *of,
  793. struct seq_file *seq, void *v)
  794. {
  795. struct rdt_resource *r = of->kn->parent->priv;
  796. seq_printf(seq, "%u\n", intel_cqm_threshold * r->mon_scale);
  797. return 0;
  798. }
  799. static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
  800. char *buf, size_t nbytes, loff_t off)
  801. {
  802. struct rdt_resource *r = of->kn->parent->priv;
  803. unsigned int bytes;
  804. int ret;
  805. ret = kstrtouint(buf, 0, &bytes);
  806. if (ret)
  807. return ret;
  808. if (bytes > (boot_cpu_data.x86_cache_size * 1024))
  809. return -EINVAL;
  810. intel_cqm_threshold = bytes / r->mon_scale;
  811. return nbytes;
  812. }
  813. /*
  814. * rdtgroup_mode_show - Display mode of this resource group
  815. */
  816. static int rdtgroup_mode_show(struct kernfs_open_file *of,
  817. struct seq_file *s, void *v)
  818. {
  819. struct rdtgroup *rdtgrp;
  820. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  821. if (!rdtgrp) {
  822. rdtgroup_kn_unlock(of->kn);
  823. return -ENOENT;
  824. }
  825. seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode));
  826. rdtgroup_kn_unlock(of->kn);
  827. return 0;
  828. }
  829. /**
  830. * rdt_cdp_peer_get - Retrieve CDP peer if it exists
  831. * @r: RDT resource to which RDT domain @d belongs
  832. * @d: Cache instance for which a CDP peer is requested
  833. * @r_cdp: RDT resource that shares hardware with @r (RDT resource peer)
  834. * Used to return the result.
  835. * @d_cdp: RDT domain that shares hardware with @d (RDT domain peer)
  836. * Used to return the result.
  837. *
  838. * RDT resources are managed independently and by extension the RDT domains
  839. * (RDT resource instances) are managed independently also. The Code and
  840. * Data Prioritization (CDP) RDT resources, while managed independently,
  841. * could refer to the same underlying hardware. For example,
  842. * RDT_RESOURCE_L2CODE and RDT_RESOURCE_L2DATA both refer to the L2 cache.
  843. *
  844. * When provided with an RDT resource @r and an instance of that RDT
  845. * resource @d rdt_cdp_peer_get() will return if there is a peer RDT
  846. * resource and the exact instance that shares the same hardware.
  847. *
  848. * Return: 0 if a CDP peer was found, <0 on error or if no CDP peer exists.
  849. * If a CDP peer was found, @r_cdp will point to the peer RDT resource
  850. * and @d_cdp will point to the peer RDT domain.
  851. */
  852. static int rdt_cdp_peer_get(struct rdt_resource *r, struct rdt_domain *d,
  853. struct rdt_resource **r_cdp,
  854. struct rdt_domain **d_cdp)
  855. {
  856. struct rdt_resource *_r_cdp = NULL;
  857. struct rdt_domain *_d_cdp = NULL;
  858. int ret = 0;
  859. switch (r->rid) {
  860. case RDT_RESOURCE_L3DATA:
  861. _r_cdp = &rdt_resources_all[RDT_RESOURCE_L3CODE];
  862. break;
  863. case RDT_RESOURCE_L3CODE:
  864. _r_cdp = &rdt_resources_all[RDT_RESOURCE_L3DATA];
  865. break;
  866. case RDT_RESOURCE_L2DATA:
  867. _r_cdp = &rdt_resources_all[RDT_RESOURCE_L2CODE];
  868. break;
  869. case RDT_RESOURCE_L2CODE:
  870. _r_cdp = &rdt_resources_all[RDT_RESOURCE_L2DATA];
  871. break;
  872. default:
  873. ret = -ENOENT;
  874. goto out;
  875. }
  876. /*
  877. * When a new CPU comes online and CDP is enabled then the new
  878. * RDT domains (if any) associated with both CDP RDT resources
  879. * are added in the same CPU online routine while the
  880. * rdtgroup_mutex is held. It should thus not happen for one
  881. * RDT domain to exist and be associated with its RDT CDP
  882. * resource but there is no RDT domain associated with the
  883. * peer RDT CDP resource. Hence the WARN.
  884. */
  885. _d_cdp = rdt_find_domain(_r_cdp, d->id, NULL);
  886. if (WARN_ON(IS_ERR_OR_NULL(_d_cdp))) {
  887. _r_cdp = NULL;
  888. _d_cdp = NULL;
  889. ret = -EINVAL;
  890. }
  891. out:
  892. *r_cdp = _r_cdp;
  893. *d_cdp = _d_cdp;
  894. return ret;
  895. }
  896. /**
  897. * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other
  898. * @r: Resource to which domain instance @d belongs.
  899. * @d: The domain instance for which @closid is being tested.
  900. * @cbm: Capacity bitmask being tested.
  901. * @closid: Intended closid for @cbm.
  902. * @exclusive: Only check if overlaps with exclusive resource groups
  903. *
  904. * Checks if provided @cbm intended to be used for @closid on domain
  905. * @d overlaps with any other closids or other hardware usage associated
  906. * with this domain. If @exclusive is true then only overlaps with
  907. * resource groups in exclusive mode will be considered. If @exclusive
  908. * is false then overlaps with any resource group or hardware entities
  909. * will be considered.
  910. *
  911. * @cbm is unsigned long, even if only 32 bits are used, to make the
  912. * bitmap functions work correctly.
  913. *
  914. * Return: false if CBM does not overlap, true if it does.
  915. */
  916. static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
  917. unsigned long cbm, int closid, bool exclusive)
  918. {
  919. enum rdtgrp_mode mode;
  920. unsigned long ctrl_b;
  921. u32 *ctrl;
  922. int i;
  923. /* Check for any overlap with regions used by hardware directly */
  924. if (!exclusive) {
  925. ctrl_b = r->cache.shareable_bits;
  926. if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len))
  927. return true;
  928. }
  929. /* Check for overlap with other resource groups */
  930. ctrl = d->ctrl_val;
  931. for (i = 0; i < closids_supported(); i++, ctrl++) {
  932. ctrl_b = *ctrl;
  933. mode = rdtgroup_mode_by_closid(i);
  934. if (closid_allocated(i) && i != closid &&
  935. mode != RDT_MODE_PSEUDO_LOCKSETUP) {
  936. if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) {
  937. if (exclusive) {
  938. if (mode == RDT_MODE_EXCLUSIVE)
  939. return true;
  940. continue;
  941. }
  942. return true;
  943. }
  944. }
  945. }
  946. return false;
  947. }
  948. /**
  949. * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware
  950. * @r: Resource to which domain instance @d belongs.
  951. * @d: The domain instance for which @closid is being tested.
  952. * @cbm: Capacity bitmask being tested.
  953. * @closid: Intended closid for @cbm.
  954. * @exclusive: Only check if overlaps with exclusive resource groups
  955. *
  956. * Resources that can be allocated using a CBM can use the CBM to control
  957. * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test
  958. * for overlap. Overlap test is not limited to the specific resource for
  959. * which the CBM is intended though - when dealing with CDP resources that
  960. * share the underlying hardware the overlap check should be performed on
  961. * the CDP resource sharing the hardware also.
  962. *
  963. * Refer to description of __rdtgroup_cbm_overlaps() for the details of the
  964. * overlap test.
  965. *
  966. * Return: true if CBM overlap detected, false if there is no overlap
  967. */
  968. bool rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
  969. unsigned long cbm, int closid, bool exclusive)
  970. {
  971. struct rdt_resource *r_cdp;
  972. struct rdt_domain *d_cdp;
  973. if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, exclusive))
  974. return true;
  975. if (rdt_cdp_peer_get(r, d, &r_cdp, &d_cdp) < 0)
  976. return false;
  977. return __rdtgroup_cbm_overlaps(r_cdp, d_cdp, cbm, closid, exclusive);
  978. }
  979. /**
  980. * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive
  981. *
  982. * An exclusive resource group implies that there should be no sharing of
  983. * its allocated resources. At the time this group is considered to be
  984. * exclusive this test can determine if its current schemata supports this
  985. * setting by testing for overlap with all other resource groups.
  986. *
  987. * Return: true if resource group can be exclusive, false if there is overlap
  988. * with allocations of other resource groups and thus this resource group
  989. * cannot be exclusive.
  990. */
  991. static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
  992. {
  993. int closid = rdtgrp->closid;
  994. struct rdt_resource *r;
  995. bool has_cache = false;
  996. struct rdt_domain *d;
  997. for_each_alloc_enabled_rdt_resource(r) {
  998. if (r->rid == RDT_RESOURCE_MBA)
  999. continue;
  1000. has_cache = true;
  1001. list_for_each_entry(d, &r->domains, list) {
  1002. if (rdtgroup_cbm_overlaps(r, d, d->ctrl_val[closid],
  1003. rdtgrp->closid, false)) {
  1004. rdt_last_cmd_puts("schemata overlaps\n");
  1005. return false;
  1006. }
  1007. }
  1008. }
  1009. if (!has_cache) {
  1010. rdt_last_cmd_puts("cannot be exclusive without CAT/CDP\n");
  1011. return false;
  1012. }
  1013. return true;
  1014. }
  1015. /**
  1016. * rdtgroup_mode_write - Modify the resource group's mode
  1017. *
  1018. */
  1019. static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
  1020. char *buf, size_t nbytes, loff_t off)
  1021. {
  1022. struct rdtgroup *rdtgrp;
  1023. enum rdtgrp_mode mode;
  1024. int ret = 0;
  1025. /* Valid input requires a trailing newline */
  1026. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  1027. return -EINVAL;
  1028. buf[nbytes - 1] = '\0';
  1029. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  1030. if (!rdtgrp) {
  1031. rdtgroup_kn_unlock(of->kn);
  1032. return -ENOENT;
  1033. }
  1034. rdt_last_cmd_clear();
  1035. mode = rdtgrp->mode;
  1036. if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) ||
  1037. (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) ||
  1038. (!strcmp(buf, "pseudo-locksetup") &&
  1039. mode == RDT_MODE_PSEUDO_LOCKSETUP) ||
  1040. (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED))
  1041. goto out;
  1042. if (mode == RDT_MODE_PSEUDO_LOCKED) {
  1043. rdt_last_cmd_printf("cannot change pseudo-locked group\n");
  1044. ret = -EINVAL;
  1045. goto out;
  1046. }
  1047. if (!strcmp(buf, "shareable")) {
  1048. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  1049. ret = rdtgroup_locksetup_exit(rdtgrp);
  1050. if (ret)
  1051. goto out;
  1052. }
  1053. rdtgrp->mode = RDT_MODE_SHAREABLE;
  1054. } else if (!strcmp(buf, "exclusive")) {
  1055. if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
  1056. ret = -EINVAL;
  1057. goto out;
  1058. }
  1059. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  1060. ret = rdtgroup_locksetup_exit(rdtgrp);
  1061. if (ret)
  1062. goto out;
  1063. }
  1064. rdtgrp->mode = RDT_MODE_EXCLUSIVE;
  1065. } else if (!strcmp(buf, "pseudo-locksetup")) {
  1066. ret = rdtgroup_locksetup_enter(rdtgrp);
  1067. if (ret)
  1068. goto out;
  1069. rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP;
  1070. } else {
  1071. rdt_last_cmd_printf("unknown/unsupported mode\n");
  1072. ret = -EINVAL;
  1073. }
  1074. out:
  1075. rdtgroup_kn_unlock(of->kn);
  1076. return ret ?: nbytes;
  1077. }
  1078. /**
  1079. * rdtgroup_cbm_to_size - Translate CBM to size in bytes
  1080. * @r: RDT resource to which @d belongs.
  1081. * @d: RDT domain instance.
  1082. * @cbm: bitmask for which the size should be computed.
  1083. *
  1084. * The bitmask provided associated with the RDT domain instance @d will be
  1085. * translated into how many bytes it represents. The size in bytes is
  1086. * computed by first dividing the total cache size by the CBM length to
  1087. * determine how many bytes each bit in the bitmask represents. The result
  1088. * is multiplied with the number of bits set in the bitmask.
  1089. *
  1090. * @cbm is unsigned long, even if only 32 bits are used to make the
  1091. * bitmap functions work correctly.
  1092. */
  1093. unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
  1094. struct rdt_domain *d, unsigned long cbm)
  1095. {
  1096. struct cpu_cacheinfo *ci;
  1097. unsigned int size = 0;
  1098. int num_b, i;
  1099. num_b = bitmap_weight(&cbm, r->cache.cbm_len);
  1100. ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask));
  1101. for (i = 0; i < ci->num_leaves; i++) {
  1102. if (ci->info_list[i].level == r->cache_level) {
  1103. size = ci->info_list[i].size / r->cache.cbm_len * num_b;
  1104. break;
  1105. }
  1106. }
  1107. return size;
  1108. }
  1109. /**
  1110. * rdtgroup_size_show - Display size in bytes of allocated regions
  1111. *
  1112. * The "size" file mirrors the layout of the "schemata" file, printing the
  1113. * size in bytes of each region instead of the capacity bitmask.
  1114. *
  1115. */
  1116. static int rdtgroup_size_show(struct kernfs_open_file *of,
  1117. struct seq_file *s, void *v)
  1118. {
  1119. struct rdtgroup *rdtgrp;
  1120. struct rdt_resource *r;
  1121. struct rdt_domain *d;
  1122. unsigned int size;
  1123. int ret = 0;
  1124. bool sep;
  1125. u32 ctrl;
  1126. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  1127. if (!rdtgrp) {
  1128. rdtgroup_kn_unlock(of->kn);
  1129. return -ENOENT;
  1130. }
  1131. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  1132. if (!rdtgrp->plr->d) {
  1133. rdt_last_cmd_clear();
  1134. rdt_last_cmd_puts("Cache domain offline\n");
  1135. ret = -ENODEV;
  1136. } else {
  1137. seq_printf(s, "%*s:", max_name_width,
  1138. rdtgrp->plr->r->name);
  1139. size = rdtgroup_cbm_to_size(rdtgrp->plr->r,
  1140. rdtgrp->plr->d,
  1141. rdtgrp->plr->cbm);
  1142. seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size);
  1143. }
  1144. goto out;
  1145. }
  1146. for_each_alloc_enabled_rdt_resource(r) {
  1147. sep = false;
  1148. seq_printf(s, "%*s:", max_name_width, r->name);
  1149. list_for_each_entry(d, &r->domains, list) {
  1150. if (sep)
  1151. seq_putc(s, ';');
  1152. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  1153. size = 0;
  1154. } else {
  1155. ctrl = (!is_mba_sc(r) ?
  1156. d->ctrl_val[rdtgrp->closid] :
  1157. d->mbps_val[rdtgrp->closid]);
  1158. if (r->rid == RDT_RESOURCE_MBA)
  1159. size = ctrl;
  1160. else
  1161. size = rdtgroup_cbm_to_size(r, d, ctrl);
  1162. }
  1163. seq_printf(s, "%d=%u", d->id, size);
  1164. sep = true;
  1165. }
  1166. seq_putc(s, '\n');
  1167. }
  1168. out:
  1169. rdtgroup_kn_unlock(of->kn);
  1170. return ret;
  1171. }
  1172. /* rdtgroup information files for one cache resource. */
  1173. static struct rftype res_common_files[] = {
  1174. {
  1175. .name = "last_cmd_status",
  1176. .mode = 0444,
  1177. .kf_ops = &rdtgroup_kf_single_ops,
  1178. .seq_show = rdt_last_cmd_status_show,
  1179. .fflags = RF_TOP_INFO,
  1180. },
  1181. {
  1182. .name = "num_closids",
  1183. .mode = 0444,
  1184. .kf_ops = &rdtgroup_kf_single_ops,
  1185. .seq_show = rdt_num_closids_show,
  1186. .fflags = RF_CTRL_INFO,
  1187. },
  1188. {
  1189. .name = "mon_features",
  1190. .mode = 0444,
  1191. .kf_ops = &rdtgroup_kf_single_ops,
  1192. .seq_show = rdt_mon_features_show,
  1193. .fflags = RF_MON_INFO,
  1194. },
  1195. {
  1196. .name = "num_rmids",
  1197. .mode = 0444,
  1198. .kf_ops = &rdtgroup_kf_single_ops,
  1199. .seq_show = rdt_num_rmids_show,
  1200. .fflags = RF_MON_INFO,
  1201. },
  1202. {
  1203. .name = "cbm_mask",
  1204. .mode = 0444,
  1205. .kf_ops = &rdtgroup_kf_single_ops,
  1206. .seq_show = rdt_default_ctrl_show,
  1207. .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
  1208. },
  1209. {
  1210. .name = "min_cbm_bits",
  1211. .mode = 0444,
  1212. .kf_ops = &rdtgroup_kf_single_ops,
  1213. .seq_show = rdt_min_cbm_bits_show,
  1214. .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
  1215. },
  1216. {
  1217. .name = "shareable_bits",
  1218. .mode = 0444,
  1219. .kf_ops = &rdtgroup_kf_single_ops,
  1220. .seq_show = rdt_shareable_bits_show,
  1221. .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
  1222. },
  1223. {
  1224. .name = "bit_usage",
  1225. .mode = 0444,
  1226. .kf_ops = &rdtgroup_kf_single_ops,
  1227. .seq_show = rdt_bit_usage_show,
  1228. .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
  1229. },
  1230. {
  1231. .name = "min_bandwidth",
  1232. .mode = 0444,
  1233. .kf_ops = &rdtgroup_kf_single_ops,
  1234. .seq_show = rdt_min_bw_show,
  1235. .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
  1236. },
  1237. {
  1238. .name = "bandwidth_gran",
  1239. .mode = 0444,
  1240. .kf_ops = &rdtgroup_kf_single_ops,
  1241. .seq_show = rdt_bw_gran_show,
  1242. .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
  1243. },
  1244. {
  1245. .name = "delay_linear",
  1246. .mode = 0444,
  1247. .kf_ops = &rdtgroup_kf_single_ops,
  1248. .seq_show = rdt_delay_linear_show,
  1249. .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
  1250. },
  1251. {
  1252. .name = "max_threshold_occupancy",
  1253. .mode = 0644,
  1254. .kf_ops = &rdtgroup_kf_single_ops,
  1255. .write = max_threshold_occ_write,
  1256. .seq_show = max_threshold_occ_show,
  1257. .fflags = RF_MON_INFO | RFTYPE_RES_CACHE,
  1258. },
  1259. {
  1260. .name = "cpus",
  1261. .mode = 0644,
  1262. .kf_ops = &rdtgroup_kf_single_ops,
  1263. .write = rdtgroup_cpus_write,
  1264. .seq_show = rdtgroup_cpus_show,
  1265. .fflags = RFTYPE_BASE,
  1266. },
  1267. {
  1268. .name = "cpus_list",
  1269. .mode = 0644,
  1270. .kf_ops = &rdtgroup_kf_single_ops,
  1271. .write = rdtgroup_cpus_write,
  1272. .seq_show = rdtgroup_cpus_show,
  1273. .flags = RFTYPE_FLAGS_CPUS_LIST,
  1274. .fflags = RFTYPE_BASE,
  1275. },
  1276. {
  1277. .name = "tasks",
  1278. .mode = 0644,
  1279. .kf_ops = &rdtgroup_kf_single_ops,
  1280. .write = rdtgroup_tasks_write,
  1281. .seq_show = rdtgroup_tasks_show,
  1282. .fflags = RFTYPE_BASE,
  1283. },
  1284. {
  1285. .name = "schemata",
  1286. .mode = 0644,
  1287. .kf_ops = &rdtgroup_kf_single_ops,
  1288. .write = rdtgroup_schemata_write,
  1289. .seq_show = rdtgroup_schemata_show,
  1290. .fflags = RF_CTRL_BASE,
  1291. },
  1292. {
  1293. .name = "mode",
  1294. .mode = 0644,
  1295. .kf_ops = &rdtgroup_kf_single_ops,
  1296. .write = rdtgroup_mode_write,
  1297. .seq_show = rdtgroup_mode_show,
  1298. .fflags = RF_CTRL_BASE,
  1299. },
  1300. {
  1301. .name = "size",
  1302. .mode = 0444,
  1303. .kf_ops = &rdtgroup_kf_single_ops,
  1304. .seq_show = rdtgroup_size_show,
  1305. .fflags = RF_CTRL_BASE,
  1306. },
  1307. };
  1308. static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
  1309. {
  1310. struct rftype *rfts, *rft;
  1311. int ret, len;
  1312. rfts = res_common_files;
  1313. len = ARRAY_SIZE(res_common_files);
  1314. lockdep_assert_held(&rdtgroup_mutex);
  1315. for (rft = rfts; rft < rfts + len; rft++) {
  1316. if ((fflags & rft->fflags) == rft->fflags) {
  1317. ret = rdtgroup_add_file(kn, rft);
  1318. if (ret)
  1319. goto error;
  1320. }
  1321. }
  1322. return 0;
  1323. error:
  1324. pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
  1325. while (--rft >= rfts) {
  1326. if ((fflags & rft->fflags) == rft->fflags)
  1327. kernfs_remove_by_name(kn, rft->name);
  1328. }
  1329. return ret;
  1330. }
  1331. /**
  1332. * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file
  1333. * @r: The resource group with which the file is associated.
  1334. * @name: Name of the file
  1335. *
  1336. * The permissions of named resctrl file, directory, or link are modified
  1337. * to not allow read, write, or execute by any user.
  1338. *
  1339. * WARNING: This function is intended to communicate to the user that the
  1340. * resctrl file has been locked down - that it is not relevant to the
  1341. * particular state the system finds itself in. It should not be relied
  1342. * on to protect from user access because after the file's permissions
  1343. * are restricted the user can still change the permissions using chmod
  1344. * from the command line.
  1345. *
  1346. * Return: 0 on success, <0 on failure.
  1347. */
  1348. int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name)
  1349. {
  1350. struct iattr iattr = {.ia_valid = ATTR_MODE,};
  1351. struct kernfs_node *kn;
  1352. int ret = 0;
  1353. kn = kernfs_find_and_get_ns(r->kn, name, NULL);
  1354. if (!kn)
  1355. return -ENOENT;
  1356. switch (kernfs_type(kn)) {
  1357. case KERNFS_DIR:
  1358. iattr.ia_mode = S_IFDIR;
  1359. break;
  1360. case KERNFS_FILE:
  1361. iattr.ia_mode = S_IFREG;
  1362. break;
  1363. case KERNFS_LINK:
  1364. iattr.ia_mode = S_IFLNK;
  1365. break;
  1366. }
  1367. ret = kernfs_setattr(kn, &iattr);
  1368. kernfs_put(kn);
  1369. return ret;
  1370. }
  1371. /**
  1372. * rdtgroup_kn_mode_restore - Restore user access to named resctrl file
  1373. * @r: The resource group with which the file is associated.
  1374. * @name: Name of the file
  1375. * @mask: Mask of permissions that should be restored
  1376. *
  1377. * Restore the permissions of the named file. If @name is a directory the
  1378. * permissions of its parent will be used.
  1379. *
  1380. * Return: 0 on success, <0 on failure.
  1381. */
  1382. int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
  1383. umode_t mask)
  1384. {
  1385. struct iattr iattr = {.ia_valid = ATTR_MODE,};
  1386. struct kernfs_node *kn, *parent;
  1387. struct rftype *rfts, *rft;
  1388. int ret, len;
  1389. rfts = res_common_files;
  1390. len = ARRAY_SIZE(res_common_files);
  1391. for (rft = rfts; rft < rfts + len; rft++) {
  1392. if (!strcmp(rft->name, name))
  1393. iattr.ia_mode = rft->mode & mask;
  1394. }
  1395. kn = kernfs_find_and_get_ns(r->kn, name, NULL);
  1396. if (!kn)
  1397. return -ENOENT;
  1398. switch (kernfs_type(kn)) {
  1399. case KERNFS_DIR:
  1400. parent = kernfs_get_parent(kn);
  1401. if (parent) {
  1402. iattr.ia_mode |= parent->mode;
  1403. kernfs_put(parent);
  1404. }
  1405. iattr.ia_mode |= S_IFDIR;
  1406. break;
  1407. case KERNFS_FILE:
  1408. iattr.ia_mode |= S_IFREG;
  1409. break;
  1410. case KERNFS_LINK:
  1411. iattr.ia_mode |= S_IFLNK;
  1412. break;
  1413. }
  1414. ret = kernfs_setattr(kn, &iattr);
  1415. kernfs_put(kn);
  1416. return ret;
  1417. }
  1418. static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name,
  1419. unsigned long fflags)
  1420. {
  1421. struct kernfs_node *kn_subdir;
  1422. int ret;
  1423. kn_subdir = kernfs_create_dir(kn_info, name,
  1424. kn_info->mode, r);
  1425. if (IS_ERR(kn_subdir))
  1426. return PTR_ERR(kn_subdir);
  1427. ret = rdtgroup_kn_set_ugid(kn_subdir);
  1428. if (ret)
  1429. return ret;
  1430. ret = rdtgroup_add_files(kn_subdir, fflags);
  1431. if (!ret)
  1432. kernfs_activate(kn_subdir);
  1433. return ret;
  1434. }
  1435. static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
  1436. {
  1437. struct rdt_resource *r;
  1438. unsigned long fflags;
  1439. char name[32];
  1440. int ret;
  1441. /* create the directory */
  1442. kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
  1443. if (IS_ERR(kn_info))
  1444. return PTR_ERR(kn_info);
  1445. ret = rdtgroup_add_files(kn_info, RF_TOP_INFO);
  1446. if (ret)
  1447. goto out_destroy;
  1448. for_each_alloc_enabled_rdt_resource(r) {
  1449. fflags = r->fflags | RF_CTRL_INFO;
  1450. ret = rdtgroup_mkdir_info_resdir(r, r->name, fflags);
  1451. if (ret)
  1452. goto out_destroy;
  1453. }
  1454. for_each_mon_enabled_rdt_resource(r) {
  1455. fflags = r->fflags | RF_MON_INFO;
  1456. sprintf(name, "%s_MON", r->name);
  1457. ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
  1458. if (ret)
  1459. goto out_destroy;
  1460. }
  1461. ret = rdtgroup_kn_set_ugid(kn_info);
  1462. if (ret)
  1463. goto out_destroy;
  1464. kernfs_activate(kn_info);
  1465. return 0;
  1466. out_destroy:
  1467. kernfs_remove(kn_info);
  1468. return ret;
  1469. }
  1470. static int
  1471. mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
  1472. char *name, struct kernfs_node **dest_kn)
  1473. {
  1474. struct kernfs_node *kn;
  1475. int ret;
  1476. /* create the directory */
  1477. kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
  1478. if (IS_ERR(kn))
  1479. return PTR_ERR(kn);
  1480. if (dest_kn)
  1481. *dest_kn = kn;
  1482. ret = rdtgroup_kn_set_ugid(kn);
  1483. if (ret)
  1484. goto out_destroy;
  1485. kernfs_activate(kn);
  1486. return 0;
  1487. out_destroy:
  1488. kernfs_remove(kn);
  1489. return ret;
  1490. }
  1491. static void l3_qos_cfg_update(void *arg)
  1492. {
  1493. bool *enable = arg;
  1494. wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
  1495. }
  1496. static void l2_qos_cfg_update(void *arg)
  1497. {
  1498. bool *enable = arg;
  1499. wrmsrl(IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
  1500. }
  1501. static inline bool is_mba_linear(void)
  1502. {
  1503. return rdt_resources_all[RDT_RESOURCE_MBA].membw.delay_linear;
  1504. }
  1505. static int set_cache_qos_cfg(int level, bool enable)
  1506. {
  1507. void (*update)(void *arg);
  1508. struct rdt_resource *r_l;
  1509. cpumask_var_t cpu_mask;
  1510. struct rdt_domain *d;
  1511. int cpu;
  1512. if (level == RDT_RESOURCE_L3)
  1513. update = l3_qos_cfg_update;
  1514. else if (level == RDT_RESOURCE_L2)
  1515. update = l2_qos_cfg_update;
  1516. else
  1517. return -EINVAL;
  1518. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  1519. return -ENOMEM;
  1520. r_l = &rdt_resources_all[level];
  1521. list_for_each_entry(d, &r_l->domains, list) {
  1522. /* Pick one CPU from each domain instance to update MSR */
  1523. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  1524. }
  1525. cpu = get_cpu();
  1526. /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
  1527. if (cpumask_test_cpu(cpu, cpu_mask))
  1528. update(&enable);
  1529. /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
  1530. smp_call_function_many(cpu_mask, update, &enable, 1);
  1531. put_cpu();
  1532. free_cpumask_var(cpu_mask);
  1533. return 0;
  1534. }
  1535. /* Restore the qos cfg state when a domain comes online */
  1536. void rdt_domain_reconfigure_cdp(struct rdt_resource *r)
  1537. {
  1538. if (!r->alloc_capable)
  1539. return;
  1540. if (r == &rdt_resources_all[RDT_RESOURCE_L2DATA])
  1541. l2_qos_cfg_update(&r->alloc_enabled);
  1542. if (r == &rdt_resources_all[RDT_RESOURCE_L3DATA])
  1543. l3_qos_cfg_update(&r->alloc_enabled);
  1544. }
  1545. /*
  1546. * Enable or disable the MBA software controller
  1547. * which helps user specify bandwidth in MBps.
  1548. * MBA software controller is supported only if
  1549. * MBM is supported and MBA is in linear scale.
  1550. */
  1551. static int set_mba_sc(bool mba_sc)
  1552. {
  1553. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA];
  1554. struct rdt_domain *d;
  1555. if (!is_mbm_enabled() || !is_mba_linear() ||
  1556. mba_sc == is_mba_sc(r))
  1557. return -EINVAL;
  1558. r->membw.mba_sc = mba_sc;
  1559. list_for_each_entry(d, &r->domains, list)
  1560. setup_default_ctrlval(r, d->ctrl_val, d->mbps_val);
  1561. return 0;
  1562. }
  1563. static int cdp_enable(int level, int data_type, int code_type)
  1564. {
  1565. struct rdt_resource *r_ldata = &rdt_resources_all[data_type];
  1566. struct rdt_resource *r_lcode = &rdt_resources_all[code_type];
  1567. struct rdt_resource *r_l = &rdt_resources_all[level];
  1568. int ret;
  1569. if (!r_l->alloc_capable || !r_ldata->alloc_capable ||
  1570. !r_lcode->alloc_capable)
  1571. return -EINVAL;
  1572. ret = set_cache_qos_cfg(level, true);
  1573. if (!ret) {
  1574. r_l->alloc_enabled = false;
  1575. r_ldata->alloc_enabled = true;
  1576. r_lcode->alloc_enabled = true;
  1577. }
  1578. return ret;
  1579. }
  1580. static int cdpl3_enable(void)
  1581. {
  1582. return cdp_enable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA,
  1583. RDT_RESOURCE_L3CODE);
  1584. }
  1585. static int cdpl2_enable(void)
  1586. {
  1587. return cdp_enable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA,
  1588. RDT_RESOURCE_L2CODE);
  1589. }
  1590. static void cdp_disable(int level, int data_type, int code_type)
  1591. {
  1592. struct rdt_resource *r = &rdt_resources_all[level];
  1593. r->alloc_enabled = r->alloc_capable;
  1594. if (rdt_resources_all[data_type].alloc_enabled) {
  1595. rdt_resources_all[data_type].alloc_enabled = false;
  1596. rdt_resources_all[code_type].alloc_enabled = false;
  1597. set_cache_qos_cfg(level, false);
  1598. }
  1599. }
  1600. static void cdpl3_disable(void)
  1601. {
  1602. cdp_disable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA, RDT_RESOURCE_L3CODE);
  1603. }
  1604. static void cdpl2_disable(void)
  1605. {
  1606. cdp_disable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA, RDT_RESOURCE_L2CODE);
  1607. }
  1608. static void cdp_disable_all(void)
  1609. {
  1610. if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
  1611. cdpl3_disable();
  1612. if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled)
  1613. cdpl2_disable();
  1614. }
  1615. static int parse_rdtgroupfs_options(char *data)
  1616. {
  1617. char *token, *o = data;
  1618. int ret = 0;
  1619. while ((token = strsep(&o, ",")) != NULL) {
  1620. if (!*token) {
  1621. ret = -EINVAL;
  1622. goto out;
  1623. }
  1624. if (!strcmp(token, "cdp")) {
  1625. ret = cdpl3_enable();
  1626. if (ret)
  1627. goto out;
  1628. } else if (!strcmp(token, "cdpl2")) {
  1629. ret = cdpl2_enable();
  1630. if (ret)
  1631. goto out;
  1632. } else if (!strcmp(token, "mba_MBps")) {
  1633. ret = set_mba_sc(true);
  1634. if (ret)
  1635. goto out;
  1636. } else {
  1637. ret = -EINVAL;
  1638. goto out;
  1639. }
  1640. }
  1641. return 0;
  1642. out:
  1643. pr_err("Invalid mount option \"%s\"\n", token);
  1644. return ret;
  1645. }
  1646. /*
  1647. * We don't allow rdtgroup directories to be created anywhere
  1648. * except the root directory. Thus when looking for the rdtgroup
  1649. * structure for a kernfs node we are either looking at a directory,
  1650. * in which case the rdtgroup structure is pointed at by the "priv"
  1651. * field, otherwise we have a file, and need only look to the parent
  1652. * to find the rdtgroup.
  1653. */
  1654. static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
  1655. {
  1656. if (kernfs_type(kn) == KERNFS_DIR) {
  1657. /*
  1658. * All the resource directories use "kn->priv"
  1659. * to point to the "struct rdtgroup" for the
  1660. * resource. "info" and its subdirectories don't
  1661. * have rdtgroup structures, so return NULL here.
  1662. */
  1663. if (kn == kn_info || kn->parent == kn_info)
  1664. return NULL;
  1665. else
  1666. return kn->priv;
  1667. } else {
  1668. return kn->parent->priv;
  1669. }
  1670. }
  1671. struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
  1672. {
  1673. struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
  1674. if (!rdtgrp)
  1675. return NULL;
  1676. atomic_inc(&rdtgrp->waitcount);
  1677. kernfs_break_active_protection(kn);
  1678. mutex_lock(&rdtgroup_mutex);
  1679. /* Was this group deleted while we waited? */
  1680. if (rdtgrp->flags & RDT_DELETED)
  1681. return NULL;
  1682. return rdtgrp;
  1683. }
  1684. void rdtgroup_kn_unlock(struct kernfs_node *kn)
  1685. {
  1686. struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
  1687. if (!rdtgrp)
  1688. return;
  1689. mutex_unlock(&rdtgroup_mutex);
  1690. if (atomic_dec_and_test(&rdtgrp->waitcount) &&
  1691. (rdtgrp->flags & RDT_DELETED)) {
  1692. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
  1693. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
  1694. rdtgroup_pseudo_lock_remove(rdtgrp);
  1695. kernfs_unbreak_active_protection(kn);
  1696. rdtgroup_remove(rdtgrp);
  1697. } else {
  1698. kernfs_unbreak_active_protection(kn);
  1699. }
  1700. }
  1701. static int mkdir_mondata_all(struct kernfs_node *parent_kn,
  1702. struct rdtgroup *prgrp,
  1703. struct kernfs_node **mon_data_kn);
  1704. static struct dentry *rdt_mount(struct file_system_type *fs_type,
  1705. int flags, const char *unused_dev_name,
  1706. void *data)
  1707. {
  1708. struct rdt_domain *dom;
  1709. struct rdt_resource *r;
  1710. struct dentry *dentry;
  1711. int ret;
  1712. cpus_read_lock();
  1713. mutex_lock(&rdtgroup_mutex);
  1714. /*
  1715. * resctrl file system can only be mounted once.
  1716. */
  1717. if (static_branch_unlikely(&rdt_enable_key)) {
  1718. dentry = ERR_PTR(-EBUSY);
  1719. goto out;
  1720. }
  1721. ret = parse_rdtgroupfs_options(data);
  1722. if (ret) {
  1723. dentry = ERR_PTR(ret);
  1724. goto out_cdp;
  1725. }
  1726. closid_init();
  1727. ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
  1728. if (ret) {
  1729. dentry = ERR_PTR(ret);
  1730. goto out_cdp;
  1731. }
  1732. if (rdt_mon_capable) {
  1733. ret = mongroup_create_dir(rdtgroup_default.kn,
  1734. &rdtgroup_default, "mon_groups",
  1735. &kn_mongrp);
  1736. if (ret) {
  1737. dentry = ERR_PTR(ret);
  1738. goto out_info;
  1739. }
  1740. ret = mkdir_mondata_all(rdtgroup_default.kn,
  1741. &rdtgroup_default, &kn_mondata);
  1742. if (ret) {
  1743. dentry = ERR_PTR(ret);
  1744. goto out_mongrp;
  1745. }
  1746. rdtgroup_default.mon.mon_data_kn = kn_mondata;
  1747. }
  1748. ret = rdt_pseudo_lock_init();
  1749. if (ret) {
  1750. dentry = ERR_PTR(ret);
  1751. goto out_mondata;
  1752. }
  1753. dentry = kernfs_mount(fs_type, flags, rdt_root,
  1754. RDTGROUP_SUPER_MAGIC, NULL);
  1755. if (IS_ERR(dentry))
  1756. goto out_psl;
  1757. if (rdt_alloc_capable)
  1758. static_branch_enable_cpuslocked(&rdt_alloc_enable_key);
  1759. if (rdt_mon_capable)
  1760. static_branch_enable_cpuslocked(&rdt_mon_enable_key);
  1761. if (rdt_alloc_capable || rdt_mon_capable)
  1762. static_branch_enable_cpuslocked(&rdt_enable_key);
  1763. if (is_mbm_enabled()) {
  1764. r = &rdt_resources_all[RDT_RESOURCE_L3];
  1765. list_for_each_entry(dom, &r->domains, list)
  1766. mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL);
  1767. }
  1768. goto out;
  1769. out_psl:
  1770. rdt_pseudo_lock_release();
  1771. out_mondata:
  1772. if (rdt_mon_capable)
  1773. kernfs_remove(kn_mondata);
  1774. out_mongrp:
  1775. if (rdt_mon_capable)
  1776. kernfs_remove(kn_mongrp);
  1777. out_info:
  1778. kernfs_remove(kn_info);
  1779. out_cdp:
  1780. cdp_disable_all();
  1781. out:
  1782. rdt_last_cmd_clear();
  1783. mutex_unlock(&rdtgroup_mutex);
  1784. cpus_read_unlock();
  1785. return dentry;
  1786. }
  1787. static int reset_all_ctrls(struct rdt_resource *r)
  1788. {
  1789. struct msr_param msr_param;
  1790. cpumask_var_t cpu_mask;
  1791. struct rdt_domain *d;
  1792. int i, cpu;
  1793. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  1794. return -ENOMEM;
  1795. msr_param.res = r;
  1796. msr_param.low = 0;
  1797. msr_param.high = r->num_closid;
  1798. /*
  1799. * Disable resource control for this resource by setting all
  1800. * CBMs in all domains to the maximum mask value. Pick one CPU
  1801. * from each domain to update the MSRs below.
  1802. */
  1803. list_for_each_entry(d, &r->domains, list) {
  1804. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  1805. for (i = 0; i < r->num_closid; i++)
  1806. d->ctrl_val[i] = r->default_ctrl;
  1807. }
  1808. cpu = get_cpu();
  1809. /* Update CBM on this cpu if it's in cpu_mask. */
  1810. if (cpumask_test_cpu(cpu, cpu_mask))
  1811. rdt_ctrl_update(&msr_param);
  1812. /* Update CBM on all other cpus in cpu_mask. */
  1813. smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
  1814. put_cpu();
  1815. free_cpumask_var(cpu_mask);
  1816. return 0;
  1817. }
  1818. static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
  1819. {
  1820. return (rdt_alloc_capable &&
  1821. (r->type == RDTCTRL_GROUP) && (t->closid == r->closid));
  1822. }
  1823. static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
  1824. {
  1825. return (rdt_mon_capable &&
  1826. (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid));
  1827. }
  1828. /*
  1829. * Move tasks from one to the other group. If @from is NULL, then all tasks
  1830. * in the systems are moved unconditionally (used for teardown).
  1831. *
  1832. * If @mask is not NULL the cpus on which moved tasks are running are set
  1833. * in that mask so the update smp function call is restricted to affected
  1834. * cpus.
  1835. */
  1836. static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
  1837. struct cpumask *mask)
  1838. {
  1839. struct task_struct *p, *t;
  1840. read_lock(&tasklist_lock);
  1841. for_each_process_thread(p, t) {
  1842. if (!from || is_closid_match(t, from) ||
  1843. is_rmid_match(t, from)) {
  1844. t->closid = to->closid;
  1845. t->rmid = to->mon.rmid;
  1846. #ifdef CONFIG_SMP
  1847. /*
  1848. * This is safe on x86 w/o barriers as the ordering
  1849. * of writing to task_cpu() and t->on_cpu is
  1850. * reverse to the reading here. The detection is
  1851. * inaccurate as tasks might move or schedule
  1852. * before the smp function call takes place. In
  1853. * such a case the function call is pointless, but
  1854. * there is no other side effect.
  1855. */
  1856. if (mask && t->on_cpu)
  1857. cpumask_set_cpu(task_cpu(t), mask);
  1858. #endif
  1859. }
  1860. }
  1861. read_unlock(&tasklist_lock);
  1862. }
  1863. static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
  1864. {
  1865. struct rdtgroup *sentry, *stmp;
  1866. struct list_head *head;
  1867. head = &rdtgrp->mon.crdtgrp_list;
  1868. list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
  1869. free_rmid(sentry->mon.rmid);
  1870. list_del(&sentry->mon.crdtgrp_list);
  1871. if (atomic_read(&sentry->waitcount) != 0)
  1872. sentry->flags = RDT_DELETED;
  1873. else
  1874. rdtgroup_remove(sentry);
  1875. }
  1876. }
  1877. /*
  1878. * Forcibly remove all of subdirectories under root.
  1879. */
  1880. static void rmdir_all_sub(void)
  1881. {
  1882. struct rdtgroup *rdtgrp, *tmp;
  1883. /* Move all tasks to the default resource group */
  1884. rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
  1885. list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
  1886. /* Free any child rmids */
  1887. free_all_child_rdtgrp(rdtgrp);
  1888. /* Remove each rdtgroup other than root */
  1889. if (rdtgrp == &rdtgroup_default)
  1890. continue;
  1891. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
  1892. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
  1893. rdtgroup_pseudo_lock_remove(rdtgrp);
  1894. /*
  1895. * Give any CPUs back to the default group. We cannot copy
  1896. * cpu_online_mask because a CPU might have executed the
  1897. * offline callback already, but is still marked online.
  1898. */
  1899. cpumask_or(&rdtgroup_default.cpu_mask,
  1900. &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
  1901. free_rmid(rdtgrp->mon.rmid);
  1902. kernfs_remove(rdtgrp->kn);
  1903. list_del(&rdtgrp->rdtgroup_list);
  1904. if (atomic_read(&rdtgrp->waitcount) != 0)
  1905. rdtgrp->flags = RDT_DELETED;
  1906. else
  1907. rdtgroup_remove(rdtgrp);
  1908. }
  1909. /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
  1910. update_closid_rmid(cpu_online_mask, &rdtgroup_default);
  1911. kernfs_remove(kn_info);
  1912. kernfs_remove(kn_mongrp);
  1913. kernfs_remove(kn_mondata);
  1914. }
  1915. static void rdt_kill_sb(struct super_block *sb)
  1916. {
  1917. struct rdt_resource *r;
  1918. cpus_read_lock();
  1919. mutex_lock(&rdtgroup_mutex);
  1920. set_mba_sc(false);
  1921. /*Put everything back to default values. */
  1922. for_each_alloc_enabled_rdt_resource(r)
  1923. reset_all_ctrls(r);
  1924. cdp_disable_all();
  1925. rmdir_all_sub();
  1926. rdt_pseudo_lock_release();
  1927. rdtgroup_default.mode = RDT_MODE_SHAREABLE;
  1928. static_branch_disable_cpuslocked(&rdt_alloc_enable_key);
  1929. static_branch_disable_cpuslocked(&rdt_mon_enable_key);
  1930. static_branch_disable_cpuslocked(&rdt_enable_key);
  1931. kernfs_kill_sb(sb);
  1932. mutex_unlock(&rdtgroup_mutex);
  1933. cpus_read_unlock();
  1934. }
  1935. static struct file_system_type rdt_fs_type = {
  1936. .name = "resctrl",
  1937. .mount = rdt_mount,
  1938. .kill_sb = rdt_kill_sb,
  1939. };
  1940. static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
  1941. void *priv)
  1942. {
  1943. struct kernfs_node *kn;
  1944. int ret = 0;
  1945. kn = __kernfs_create_file(parent_kn, name, 0444,
  1946. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
  1947. &kf_mondata_ops, priv, NULL, NULL);
  1948. if (IS_ERR(kn))
  1949. return PTR_ERR(kn);
  1950. ret = rdtgroup_kn_set_ugid(kn);
  1951. if (ret) {
  1952. kernfs_remove(kn);
  1953. return ret;
  1954. }
  1955. return ret;
  1956. }
  1957. /*
  1958. * Remove all subdirectories of mon_data of ctrl_mon groups
  1959. * and monitor groups with given domain id.
  1960. */
  1961. void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id)
  1962. {
  1963. struct rdtgroup *prgrp, *crgrp;
  1964. char name[32];
  1965. if (!r->mon_enabled)
  1966. return;
  1967. list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
  1968. sprintf(name, "mon_%s_%02d", r->name, dom_id);
  1969. kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
  1970. list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
  1971. kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
  1972. }
  1973. }
  1974. static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
  1975. struct rdt_domain *d,
  1976. struct rdt_resource *r, struct rdtgroup *prgrp)
  1977. {
  1978. union mon_data_bits priv;
  1979. struct kernfs_node *kn;
  1980. struct mon_evt *mevt;
  1981. struct rmid_read rr;
  1982. char name[32];
  1983. int ret;
  1984. sprintf(name, "mon_%s_%02d", r->name, d->id);
  1985. /* create the directory */
  1986. kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
  1987. if (IS_ERR(kn))
  1988. return PTR_ERR(kn);
  1989. ret = rdtgroup_kn_set_ugid(kn);
  1990. if (ret)
  1991. goto out_destroy;
  1992. if (WARN_ON(list_empty(&r->evt_list))) {
  1993. ret = -EPERM;
  1994. goto out_destroy;
  1995. }
  1996. priv.u.rid = r->rid;
  1997. priv.u.domid = d->id;
  1998. list_for_each_entry(mevt, &r->evt_list, list) {
  1999. priv.u.evtid = mevt->evtid;
  2000. ret = mon_addfile(kn, mevt->name, priv.priv);
  2001. if (ret)
  2002. goto out_destroy;
  2003. if (is_mbm_event(mevt->evtid))
  2004. mon_event_read(&rr, d, prgrp, mevt->evtid, true);
  2005. }
  2006. kernfs_activate(kn);
  2007. return 0;
  2008. out_destroy:
  2009. kernfs_remove(kn);
  2010. return ret;
  2011. }
  2012. /*
  2013. * Add all subdirectories of mon_data for "ctrl_mon" groups
  2014. * and "monitor" groups with given domain id.
  2015. */
  2016. void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
  2017. struct rdt_domain *d)
  2018. {
  2019. struct kernfs_node *parent_kn;
  2020. struct rdtgroup *prgrp, *crgrp;
  2021. struct list_head *head;
  2022. if (!r->mon_enabled)
  2023. return;
  2024. list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
  2025. parent_kn = prgrp->mon.mon_data_kn;
  2026. mkdir_mondata_subdir(parent_kn, d, r, prgrp);
  2027. head = &prgrp->mon.crdtgrp_list;
  2028. list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
  2029. parent_kn = crgrp->mon.mon_data_kn;
  2030. mkdir_mondata_subdir(parent_kn, d, r, crgrp);
  2031. }
  2032. }
  2033. }
  2034. static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
  2035. struct rdt_resource *r,
  2036. struct rdtgroup *prgrp)
  2037. {
  2038. struct rdt_domain *dom;
  2039. int ret;
  2040. list_for_each_entry(dom, &r->domains, list) {
  2041. ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
  2042. if (ret)
  2043. return ret;
  2044. }
  2045. return 0;
  2046. }
  2047. /*
  2048. * This creates a directory mon_data which contains the monitored data.
  2049. *
  2050. * mon_data has one directory for each domain whic are named
  2051. * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
  2052. * with L3 domain looks as below:
  2053. * ./mon_data:
  2054. * mon_L3_00
  2055. * mon_L3_01
  2056. * mon_L3_02
  2057. * ...
  2058. *
  2059. * Each domain directory has one file per event:
  2060. * ./mon_L3_00/:
  2061. * llc_occupancy
  2062. *
  2063. */
  2064. static int mkdir_mondata_all(struct kernfs_node *parent_kn,
  2065. struct rdtgroup *prgrp,
  2066. struct kernfs_node **dest_kn)
  2067. {
  2068. struct rdt_resource *r;
  2069. struct kernfs_node *kn;
  2070. int ret;
  2071. /*
  2072. * Create the mon_data directory first.
  2073. */
  2074. ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn);
  2075. if (ret)
  2076. return ret;
  2077. if (dest_kn)
  2078. *dest_kn = kn;
  2079. /*
  2080. * Create the subdirectories for each domain. Note that all events
  2081. * in a domain like L3 are grouped into a resource whose domain is L3
  2082. */
  2083. for_each_mon_enabled_rdt_resource(r) {
  2084. ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
  2085. if (ret)
  2086. goto out_destroy;
  2087. }
  2088. return 0;
  2089. out_destroy:
  2090. kernfs_remove(kn);
  2091. return ret;
  2092. }
  2093. /**
  2094. * cbm_ensure_valid - Enforce validity on provided CBM
  2095. * @_val: Candidate CBM
  2096. * @r: RDT resource to which the CBM belongs
  2097. *
  2098. * The provided CBM represents all cache portions available for use. This
  2099. * may be represented by a bitmap that does not consist of contiguous ones
  2100. * and thus be an invalid CBM.
  2101. * Here the provided CBM is forced to be a valid CBM by only considering
  2102. * the first set of contiguous bits as valid and clearing all bits.
  2103. * The intention here is to provide a valid default CBM with which a new
  2104. * resource group is initialized. The user can follow this with a
  2105. * modification to the CBM if the default does not satisfy the
  2106. * requirements.
  2107. */
  2108. static void cbm_ensure_valid(u32 *_val, struct rdt_resource *r)
  2109. {
  2110. unsigned long val = *_val;
  2111. unsigned int cbm_len = r->cache.cbm_len;
  2112. unsigned long first_bit, zero_bit;
  2113. if (val == 0)
  2114. return;
  2115. first_bit = find_first_bit(&val, cbm_len);
  2116. zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
  2117. /* Clear any remaining bits to ensure contiguous region */
  2118. bitmap_clear(&val, zero_bit, cbm_len - zero_bit);
  2119. *_val = (u32)val;
  2120. }
  2121. /**
  2122. * rdtgroup_init_alloc - Initialize the new RDT group's allocations
  2123. *
  2124. * A new RDT group is being created on an allocation capable (CAT)
  2125. * supporting system. Set this group up to start off with all usable
  2126. * allocations. That is, all shareable and unused bits.
  2127. *
  2128. * All-zero CBM is invalid. If there are no more shareable bits available
  2129. * on any domain then the entire allocation will fail.
  2130. */
  2131. static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
  2132. {
  2133. u32 used_b = 0, unused_b = 0;
  2134. u32 closid = rdtgrp->closid;
  2135. struct rdt_resource *r;
  2136. unsigned long tmp_cbm;
  2137. enum rdtgrp_mode mode;
  2138. struct rdt_domain *d;
  2139. int i, ret;
  2140. u32 *ctrl;
  2141. for_each_alloc_enabled_rdt_resource(r) {
  2142. /*
  2143. * Only initialize default allocations for CBM cache
  2144. * resources
  2145. */
  2146. if (r->rid == RDT_RESOURCE_MBA)
  2147. continue;
  2148. list_for_each_entry(d, &r->domains, list) {
  2149. d->have_new_ctrl = false;
  2150. d->new_ctrl = r->cache.shareable_bits;
  2151. used_b = r->cache.shareable_bits;
  2152. ctrl = d->ctrl_val;
  2153. for (i = 0; i < closids_supported(); i++, ctrl++) {
  2154. if (closid_allocated(i) && i != closid) {
  2155. mode = rdtgroup_mode_by_closid(i);
  2156. if (mode == RDT_MODE_PSEUDO_LOCKSETUP)
  2157. continue;
  2158. used_b |= *ctrl;
  2159. if (mode == RDT_MODE_SHAREABLE)
  2160. d->new_ctrl |= *ctrl;
  2161. }
  2162. }
  2163. if (d->plr && d->plr->cbm > 0)
  2164. used_b |= d->plr->cbm;
  2165. unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1);
  2166. unused_b &= BIT_MASK(r->cache.cbm_len) - 1;
  2167. d->new_ctrl |= unused_b;
  2168. /*
  2169. * Force the initial CBM to be valid, user can
  2170. * modify the CBM based on system availability.
  2171. */
  2172. cbm_ensure_valid(&d->new_ctrl, r);
  2173. /*
  2174. * Assign the u32 CBM to an unsigned long to ensure
  2175. * that bitmap_weight() does not access out-of-bound
  2176. * memory.
  2177. */
  2178. tmp_cbm = d->new_ctrl;
  2179. if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) <
  2180. r->cache.min_cbm_bits) {
  2181. rdt_last_cmd_printf("no space on %s:%d\n",
  2182. r->name, d->id);
  2183. return -ENOSPC;
  2184. }
  2185. d->have_new_ctrl = true;
  2186. }
  2187. }
  2188. for_each_alloc_enabled_rdt_resource(r) {
  2189. /*
  2190. * Only initialize default allocations for CBM cache
  2191. * resources
  2192. */
  2193. if (r->rid == RDT_RESOURCE_MBA)
  2194. continue;
  2195. ret = update_domains(r, rdtgrp->closid);
  2196. if (ret < 0) {
  2197. rdt_last_cmd_puts("failed to initialize allocations\n");
  2198. return ret;
  2199. }
  2200. rdtgrp->mode = RDT_MODE_SHAREABLE;
  2201. }
  2202. return 0;
  2203. }
  2204. static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
  2205. struct kernfs_node *prgrp_kn,
  2206. const char *name, umode_t mode,
  2207. enum rdt_group_type rtype, struct rdtgroup **r)
  2208. {
  2209. struct rdtgroup *prdtgrp, *rdtgrp;
  2210. struct kernfs_node *kn;
  2211. uint files = 0;
  2212. int ret;
  2213. prdtgrp = rdtgroup_kn_lock_live(parent_kn);
  2214. rdt_last_cmd_clear();
  2215. if (!prdtgrp) {
  2216. ret = -ENODEV;
  2217. rdt_last_cmd_puts("directory was removed\n");
  2218. goto out_unlock;
  2219. }
  2220. if (rtype == RDTMON_GROUP &&
  2221. (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
  2222. prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
  2223. ret = -EINVAL;
  2224. rdt_last_cmd_puts("pseudo-locking in progress\n");
  2225. goto out_unlock;
  2226. }
  2227. /* allocate the rdtgroup. */
  2228. rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
  2229. if (!rdtgrp) {
  2230. ret = -ENOSPC;
  2231. rdt_last_cmd_puts("kernel out of memory\n");
  2232. goto out_unlock;
  2233. }
  2234. *r = rdtgrp;
  2235. rdtgrp->mon.parent = prdtgrp;
  2236. rdtgrp->type = rtype;
  2237. INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
  2238. /* kernfs creates the directory for rdtgrp */
  2239. kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
  2240. if (IS_ERR(kn)) {
  2241. ret = PTR_ERR(kn);
  2242. rdt_last_cmd_puts("kernfs create error\n");
  2243. goto out_free_rgrp;
  2244. }
  2245. rdtgrp->kn = kn;
  2246. /*
  2247. * kernfs_remove() will drop the reference count on "kn" which
  2248. * will free it. But we still need it to stick around for the
  2249. * rdtgroup_kn_unlock(kn) call. Take one extra reference here,
  2250. * which will be dropped by kernfs_put() in rdtgroup_remove().
  2251. */
  2252. kernfs_get(kn);
  2253. ret = rdtgroup_kn_set_ugid(kn);
  2254. if (ret) {
  2255. rdt_last_cmd_puts("kernfs perm error\n");
  2256. goto out_destroy;
  2257. }
  2258. files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype);
  2259. ret = rdtgroup_add_files(kn, files);
  2260. if (ret) {
  2261. rdt_last_cmd_puts("kernfs fill error\n");
  2262. goto out_destroy;
  2263. }
  2264. if (rdt_mon_capable) {
  2265. ret = alloc_rmid();
  2266. if (ret < 0) {
  2267. rdt_last_cmd_puts("out of RMIDs\n");
  2268. goto out_destroy;
  2269. }
  2270. rdtgrp->mon.rmid = ret;
  2271. ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
  2272. if (ret) {
  2273. rdt_last_cmd_puts("kernfs subdir error\n");
  2274. goto out_idfree;
  2275. }
  2276. }
  2277. kernfs_activate(kn);
  2278. /*
  2279. * The caller unlocks the parent_kn upon success.
  2280. */
  2281. return 0;
  2282. out_idfree:
  2283. free_rmid(rdtgrp->mon.rmid);
  2284. out_destroy:
  2285. kernfs_put(rdtgrp->kn);
  2286. kernfs_remove(rdtgrp->kn);
  2287. out_free_rgrp:
  2288. kfree(rdtgrp);
  2289. out_unlock:
  2290. rdtgroup_kn_unlock(parent_kn);
  2291. return ret;
  2292. }
  2293. static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
  2294. {
  2295. kernfs_remove(rgrp->kn);
  2296. free_rmid(rgrp->mon.rmid);
  2297. rdtgroup_remove(rgrp);
  2298. }
  2299. /*
  2300. * Create a monitor group under "mon_groups" directory of a control
  2301. * and monitor group(ctrl_mon). This is a resource group
  2302. * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
  2303. */
  2304. static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
  2305. struct kernfs_node *prgrp_kn,
  2306. const char *name,
  2307. umode_t mode)
  2308. {
  2309. struct rdtgroup *rdtgrp, *prgrp;
  2310. int ret;
  2311. ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTMON_GROUP,
  2312. &rdtgrp);
  2313. if (ret)
  2314. return ret;
  2315. prgrp = rdtgrp->mon.parent;
  2316. rdtgrp->closid = prgrp->closid;
  2317. /*
  2318. * Add the rdtgrp to the list of rdtgrps the parent
  2319. * ctrl_mon group has to track.
  2320. */
  2321. list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
  2322. rdtgroup_kn_unlock(parent_kn);
  2323. return ret;
  2324. }
  2325. /*
  2326. * These are rdtgroups created under the root directory. Can be used
  2327. * to allocate and monitor resources.
  2328. */
  2329. static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
  2330. struct kernfs_node *prgrp_kn,
  2331. const char *name, umode_t mode)
  2332. {
  2333. struct rdtgroup *rdtgrp;
  2334. struct kernfs_node *kn;
  2335. u32 closid;
  2336. int ret;
  2337. ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTCTRL_GROUP,
  2338. &rdtgrp);
  2339. if (ret)
  2340. return ret;
  2341. kn = rdtgrp->kn;
  2342. ret = closid_alloc();
  2343. if (ret < 0) {
  2344. rdt_last_cmd_puts("out of CLOSIDs\n");
  2345. goto out_common_fail;
  2346. }
  2347. closid = ret;
  2348. ret = 0;
  2349. rdtgrp->closid = closid;
  2350. ret = rdtgroup_init_alloc(rdtgrp);
  2351. if (ret < 0)
  2352. goto out_id_free;
  2353. list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
  2354. if (rdt_mon_capable) {
  2355. /*
  2356. * Create an empty mon_groups directory to hold the subset
  2357. * of tasks and cpus to monitor.
  2358. */
  2359. ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL);
  2360. if (ret) {
  2361. rdt_last_cmd_puts("kernfs subdir error\n");
  2362. goto out_del_list;
  2363. }
  2364. }
  2365. goto out_unlock;
  2366. out_del_list:
  2367. list_del(&rdtgrp->rdtgroup_list);
  2368. out_id_free:
  2369. closid_free(closid);
  2370. out_common_fail:
  2371. mkdir_rdt_prepare_clean(rdtgrp);
  2372. out_unlock:
  2373. rdtgroup_kn_unlock(parent_kn);
  2374. return ret;
  2375. }
  2376. /*
  2377. * We allow creating mon groups only with in a directory called "mon_groups"
  2378. * which is present in every ctrl_mon group. Check if this is a valid
  2379. * "mon_groups" directory.
  2380. *
  2381. * 1. The directory should be named "mon_groups".
  2382. * 2. The mon group itself should "not" be named "mon_groups".
  2383. * This makes sure "mon_groups" directory always has a ctrl_mon group
  2384. * as parent.
  2385. */
  2386. static bool is_mon_groups(struct kernfs_node *kn, const char *name)
  2387. {
  2388. return (!strcmp(kn->name, "mon_groups") &&
  2389. strcmp(name, "mon_groups"));
  2390. }
  2391. static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
  2392. umode_t mode)
  2393. {
  2394. /* Do not accept '\n' to avoid unparsable situation. */
  2395. if (strchr(name, '\n'))
  2396. return -EINVAL;
  2397. /*
  2398. * If the parent directory is the root directory and RDT
  2399. * allocation is supported, add a control and monitoring
  2400. * subdirectory
  2401. */
  2402. if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn)
  2403. return rdtgroup_mkdir_ctrl_mon(parent_kn, parent_kn, name, mode);
  2404. /*
  2405. * If RDT monitoring is supported and the parent directory is a valid
  2406. * "mon_groups" directory, add a monitoring subdirectory.
  2407. */
  2408. if (rdt_mon_capable && is_mon_groups(parent_kn, name))
  2409. return rdtgroup_mkdir_mon(parent_kn, parent_kn->parent, name, mode);
  2410. return -EPERM;
  2411. }
  2412. static int rdtgroup_rmdir_mon(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
  2413. cpumask_var_t tmpmask)
  2414. {
  2415. struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
  2416. int cpu;
  2417. /* Give any tasks back to the parent group */
  2418. rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
  2419. /* Update per cpu rmid of the moved CPUs first */
  2420. for_each_cpu(cpu, &rdtgrp->cpu_mask)
  2421. per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
  2422. /*
  2423. * Update the MSR on moved CPUs and CPUs which have moved
  2424. * task running on them.
  2425. */
  2426. cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
  2427. update_closid_rmid(tmpmask, NULL);
  2428. rdtgrp->flags = RDT_DELETED;
  2429. free_rmid(rdtgrp->mon.rmid);
  2430. /*
  2431. * Remove the rdtgrp from the parent ctrl_mon group's list
  2432. */
  2433. WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
  2434. list_del(&rdtgrp->mon.crdtgrp_list);
  2435. kernfs_remove(rdtgrp->kn);
  2436. return 0;
  2437. }
  2438. static int rdtgroup_ctrl_remove(struct kernfs_node *kn,
  2439. struct rdtgroup *rdtgrp)
  2440. {
  2441. rdtgrp->flags = RDT_DELETED;
  2442. list_del(&rdtgrp->rdtgroup_list);
  2443. kernfs_remove(rdtgrp->kn);
  2444. return 0;
  2445. }
  2446. static int rdtgroup_rmdir_ctrl(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
  2447. cpumask_var_t tmpmask)
  2448. {
  2449. int cpu;
  2450. /* Give any tasks back to the default group */
  2451. rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
  2452. /* Give any CPUs back to the default group */
  2453. cpumask_or(&rdtgroup_default.cpu_mask,
  2454. &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
  2455. /* Update per cpu closid and rmid of the moved CPUs first */
  2456. for_each_cpu(cpu, &rdtgrp->cpu_mask) {
  2457. per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
  2458. per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
  2459. }
  2460. /*
  2461. * Update the MSR on moved CPUs and CPUs which have moved
  2462. * task running on them.
  2463. */
  2464. cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
  2465. update_closid_rmid(tmpmask, NULL);
  2466. closid_free(rdtgrp->closid);
  2467. free_rmid(rdtgrp->mon.rmid);
  2468. rdtgroup_ctrl_remove(kn, rdtgrp);
  2469. /*
  2470. * Free all the child monitor group rmids.
  2471. */
  2472. free_all_child_rdtgrp(rdtgrp);
  2473. return 0;
  2474. }
  2475. static int rdtgroup_rmdir(struct kernfs_node *kn)
  2476. {
  2477. struct kernfs_node *parent_kn = kn->parent;
  2478. struct rdtgroup *rdtgrp;
  2479. cpumask_var_t tmpmask;
  2480. int ret = 0;
  2481. if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
  2482. return -ENOMEM;
  2483. rdtgrp = rdtgroup_kn_lock_live(kn);
  2484. if (!rdtgrp) {
  2485. ret = -EPERM;
  2486. goto out;
  2487. }
  2488. /*
  2489. * If the rdtgroup is a ctrl_mon group and parent directory
  2490. * is the root directory, remove the ctrl_mon group.
  2491. *
  2492. * If the rdtgroup is a mon group and parent directory
  2493. * is a valid "mon_groups" directory, remove the mon group.
  2494. */
  2495. if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn &&
  2496. rdtgrp != &rdtgroup_default) {
  2497. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
  2498. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  2499. ret = rdtgroup_ctrl_remove(kn, rdtgrp);
  2500. } else {
  2501. ret = rdtgroup_rmdir_ctrl(kn, rdtgrp, tmpmask);
  2502. }
  2503. } else if (rdtgrp->type == RDTMON_GROUP &&
  2504. is_mon_groups(parent_kn, kn->name)) {
  2505. ret = rdtgroup_rmdir_mon(kn, rdtgrp, tmpmask);
  2506. } else {
  2507. ret = -EPERM;
  2508. }
  2509. out:
  2510. rdtgroup_kn_unlock(kn);
  2511. free_cpumask_var(tmpmask);
  2512. return ret;
  2513. }
  2514. static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
  2515. {
  2516. if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
  2517. seq_puts(seq, ",cdp");
  2518. if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled)
  2519. seq_puts(seq, ",cdpl2");
  2520. if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA]))
  2521. seq_puts(seq, ",mba_MBps");
  2522. return 0;
  2523. }
  2524. static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
  2525. .mkdir = rdtgroup_mkdir,
  2526. .rmdir = rdtgroup_rmdir,
  2527. .show_options = rdtgroup_show_options,
  2528. };
  2529. static int __init rdtgroup_setup_root(void)
  2530. {
  2531. int ret;
  2532. rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
  2533. KERNFS_ROOT_CREATE_DEACTIVATED |
  2534. KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
  2535. &rdtgroup_default);
  2536. if (IS_ERR(rdt_root))
  2537. return PTR_ERR(rdt_root);
  2538. mutex_lock(&rdtgroup_mutex);
  2539. rdtgroup_default.closid = 0;
  2540. rdtgroup_default.mon.rmid = 0;
  2541. rdtgroup_default.type = RDTCTRL_GROUP;
  2542. INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
  2543. list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
  2544. ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE);
  2545. if (ret) {
  2546. kernfs_destroy_root(rdt_root);
  2547. goto out;
  2548. }
  2549. rdtgroup_default.kn = rdt_root->kn;
  2550. kernfs_activate(rdtgroup_default.kn);
  2551. out:
  2552. mutex_unlock(&rdtgroup_mutex);
  2553. return ret;
  2554. }
  2555. /*
  2556. * rdtgroup_init - rdtgroup initialization
  2557. *
  2558. * Setup resctrl file system including set up root, create mount point,
  2559. * register rdtgroup filesystem, and initialize files under root directory.
  2560. *
  2561. * Return: 0 on success or -errno
  2562. */
  2563. int __init rdtgroup_init(void)
  2564. {
  2565. int ret = 0;
  2566. seq_buf_init(&last_cmd_status, last_cmd_status_buf,
  2567. sizeof(last_cmd_status_buf));
  2568. ret = rdtgroup_setup_root();
  2569. if (ret)
  2570. return ret;
  2571. ret = sysfs_create_mount_point(fs_kobj, "resctrl");
  2572. if (ret)
  2573. goto cleanup_root;
  2574. ret = register_filesystem(&rdt_fs_type);
  2575. if (ret)
  2576. goto cleanup_mountpoint;
  2577. /*
  2578. * Adding the resctrl debugfs directory here may not be ideal since
  2579. * it would let the resctrl debugfs directory appear on the debugfs
  2580. * filesystem before the resctrl filesystem is mounted.
  2581. * It may also be ok since that would enable debugging of RDT before
  2582. * resctrl is mounted.
  2583. * The reason why the debugfs directory is created here and not in
  2584. * rdt_mount() is because rdt_mount() takes rdtgroup_mutex and
  2585. * during the debugfs directory creation also &sb->s_type->i_mutex_key
  2586. * (the lockdep class of inode->i_rwsem). Other filesystem
  2587. * interactions (eg. SyS_getdents) have the lock ordering:
  2588. * &sb->s_type->i_mutex_key --> &mm->mmap_sem
  2589. * During mmap(), called with &mm->mmap_sem, the rdtgroup_mutex
  2590. * is taken, thus creating dependency:
  2591. * &mm->mmap_sem --> rdtgroup_mutex for the latter that can cause
  2592. * issues considering the other two lock dependencies.
  2593. * By creating the debugfs directory here we avoid a dependency
  2594. * that may cause deadlock (even though file operations cannot
  2595. * occur until the filesystem is mounted, but I do not know how to
  2596. * tell lockdep that).
  2597. */
  2598. debugfs_resctrl = debugfs_create_dir("resctrl", NULL);
  2599. return 0;
  2600. cleanup_mountpoint:
  2601. sysfs_remove_mount_point(fs_kobj, "resctrl");
  2602. cleanup_root:
  2603. kernfs_destroy_root(rdt_root);
  2604. return ret;
  2605. }
  2606. void __exit rdtgroup_exit(void)
  2607. {
  2608. debugfs_remove_recursive(debugfs_resctrl);
  2609. unregister_filesystem(&rdt_fs_type);
  2610. sysfs_remove_mount_point(fs_kobj, "resctrl");
  2611. kernfs_destroy_root(rdt_root);
  2612. }