intel_rdt_rdtgroup.c 76 KB

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