rdtgroup.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * User interface for Resource Allocation in Resource Director Technology(RDT)
  4. *
  5. * Copyright (C) 2016 Intel Corporation
  6. *
  7. * Author: Fenghua Yu <fenghua.yu@intel.com>
  8. *
  9. * More information about RDT be found in the Intel (R) x86 Architecture
  10. * Software Developer Manual.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/cpu.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/fs.h>
  16. #include <linux/fs_parser.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/kernfs.h>
  19. #include <linux/seq_buf.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/sched/signal.h>
  22. #include <linux/sched/task.h>
  23. #include <linux/slab.h>
  24. #include <linux/task_work.h>
  25. #include <linux/user_namespace.h>
  26. #include <uapi/linux/magic.h>
  27. #include <asm/resctrl.h>
  28. #include "internal.h"
  29. DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
  30. DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key);
  31. DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
  32. /* Mutex to protect rdtgroup access. */
  33. DEFINE_MUTEX(rdtgroup_mutex);
  34. static struct kernfs_root *rdt_root;
  35. struct rdtgroup rdtgroup_default;
  36. LIST_HEAD(rdt_all_groups);
  37. /* list of entries for the schemata file */
  38. LIST_HEAD(resctrl_schema_all);
  39. /* The filesystem can only be mounted once. */
  40. bool resctrl_mounted;
  41. /* Kernel fs node for "info" directory under root */
  42. static struct kernfs_node *kn_info;
  43. /* Kernel fs node for "mon_groups" directory under root */
  44. static struct kernfs_node *kn_mongrp;
  45. /* Kernel fs node for "mon_data" directory under root */
  46. static struct kernfs_node *kn_mondata;
  47. static struct seq_buf last_cmd_status;
  48. static char last_cmd_status_buf[512];
  49. static int rdtgroup_setup_root(struct rdt_fs_context *ctx);
  50. static void rdtgroup_destroy_root(void);
  51. struct dentry *debugfs_resctrl;
  52. static bool resctrl_debug;
  53. void rdt_last_cmd_clear(void)
  54. {
  55. lockdep_assert_held(&rdtgroup_mutex);
  56. seq_buf_clear(&last_cmd_status);
  57. }
  58. void rdt_last_cmd_puts(const char *s)
  59. {
  60. lockdep_assert_held(&rdtgroup_mutex);
  61. seq_buf_puts(&last_cmd_status, s);
  62. }
  63. void rdt_last_cmd_printf(const char *fmt, ...)
  64. {
  65. va_list ap;
  66. va_start(ap, fmt);
  67. lockdep_assert_held(&rdtgroup_mutex);
  68. seq_buf_vprintf(&last_cmd_status, fmt, ap);
  69. va_end(ap);
  70. }
  71. void rdt_staged_configs_clear(void)
  72. {
  73. struct rdt_ctrl_domain *dom;
  74. struct rdt_resource *r;
  75. lockdep_assert_held(&rdtgroup_mutex);
  76. for_each_alloc_capable_rdt_resource(r) {
  77. list_for_each_entry(dom, &r->ctrl_domains, hdr.list)
  78. memset(dom->staged_config, 0, sizeof(dom->staged_config));
  79. }
  80. }
  81. /*
  82. * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
  83. * we can keep a bitmap of free CLOSIDs in a single integer.
  84. *
  85. * Using a global CLOSID across all resources has some advantages and
  86. * some drawbacks:
  87. * + We can simply set current's closid to assign a task to a resource
  88. * group.
  89. * + Context switch code can avoid extra memory references deciding which
  90. * CLOSID to load into the PQR_ASSOC MSR
  91. * - We give up some options in configuring resource groups across multi-socket
  92. * systems.
  93. * - Our choices on how to configure each resource become progressively more
  94. * limited as the number of resources grows.
  95. */
  96. static unsigned long closid_free_map;
  97. static int closid_free_map_len;
  98. int closids_supported(void)
  99. {
  100. return closid_free_map_len;
  101. }
  102. static void closid_init(void)
  103. {
  104. struct resctrl_schema *s;
  105. u32 rdt_min_closid = 32;
  106. /* Compute rdt_min_closid across all resources */
  107. list_for_each_entry(s, &resctrl_schema_all, list)
  108. rdt_min_closid = min(rdt_min_closid, s->num_closid);
  109. closid_free_map = BIT_MASK(rdt_min_closid) - 1;
  110. /* RESCTRL_RESERVED_CLOSID is always reserved for the default group */
  111. __clear_bit(RESCTRL_RESERVED_CLOSID, &closid_free_map);
  112. closid_free_map_len = rdt_min_closid;
  113. }
  114. static int closid_alloc(void)
  115. {
  116. int cleanest_closid;
  117. u32 closid;
  118. lockdep_assert_held(&rdtgroup_mutex);
  119. if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID) &&
  120. is_llc_occupancy_enabled()) {
  121. cleanest_closid = resctrl_find_cleanest_closid();
  122. if (cleanest_closid < 0)
  123. return cleanest_closid;
  124. closid = cleanest_closid;
  125. } else {
  126. closid = ffs(closid_free_map);
  127. if (closid == 0)
  128. return -ENOSPC;
  129. closid--;
  130. }
  131. __clear_bit(closid, &closid_free_map);
  132. return closid;
  133. }
  134. void closid_free(int closid)
  135. {
  136. lockdep_assert_held(&rdtgroup_mutex);
  137. __set_bit(closid, &closid_free_map);
  138. }
  139. /**
  140. * closid_allocated - test if provided closid is in use
  141. * @closid: closid to be tested
  142. *
  143. * Return: true if @closid is currently associated with a resource group,
  144. * false if @closid is free
  145. */
  146. bool closid_allocated(unsigned int closid)
  147. {
  148. lockdep_assert_held(&rdtgroup_mutex);
  149. return !test_bit(closid, &closid_free_map);
  150. }
  151. /**
  152. * rdtgroup_mode_by_closid - Return mode of resource group with closid
  153. * @closid: closid if the resource group
  154. *
  155. * Each resource group is associated with a @closid. Here the mode
  156. * of a resource group can be queried by searching for it using its closid.
  157. *
  158. * Return: mode as &enum rdtgrp_mode of resource group with closid @closid
  159. */
  160. enum rdtgrp_mode rdtgroup_mode_by_closid(int closid)
  161. {
  162. struct rdtgroup *rdtgrp;
  163. list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
  164. if (rdtgrp->closid == closid)
  165. return rdtgrp->mode;
  166. }
  167. return RDT_NUM_MODES;
  168. }
  169. static const char * const rdt_mode_str[] = {
  170. [RDT_MODE_SHAREABLE] = "shareable",
  171. [RDT_MODE_EXCLUSIVE] = "exclusive",
  172. [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup",
  173. [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked",
  174. };
  175. /**
  176. * rdtgroup_mode_str - Return the string representation of mode
  177. * @mode: the resource group mode as &enum rdtgroup_mode
  178. *
  179. * Return: string representation of valid mode, "unknown" otherwise
  180. */
  181. static const char *rdtgroup_mode_str(enum rdtgrp_mode mode)
  182. {
  183. if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES)
  184. return "unknown";
  185. return rdt_mode_str[mode];
  186. }
  187. /* set uid and gid of rdtgroup dirs and files to that of the creator */
  188. static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
  189. {
  190. struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
  191. .ia_uid = current_fsuid(),
  192. .ia_gid = current_fsgid(), };
  193. if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
  194. gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
  195. return 0;
  196. return kernfs_setattr(kn, &iattr);
  197. }
  198. static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
  199. {
  200. struct kernfs_node *kn;
  201. int ret;
  202. kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
  203. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
  204. 0, rft->kf_ops, rft, NULL, NULL);
  205. if (IS_ERR(kn))
  206. return PTR_ERR(kn);
  207. ret = rdtgroup_kn_set_ugid(kn);
  208. if (ret) {
  209. kernfs_remove(kn);
  210. return ret;
  211. }
  212. return 0;
  213. }
  214. static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
  215. {
  216. struct kernfs_open_file *of = m->private;
  217. struct rftype *rft = of->kn->priv;
  218. if (rft->seq_show)
  219. return rft->seq_show(of, m, arg);
  220. return 0;
  221. }
  222. static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
  223. size_t nbytes, loff_t off)
  224. {
  225. struct rftype *rft = of->kn->priv;
  226. if (rft->write)
  227. return rft->write(of, buf, nbytes, off);
  228. return -EINVAL;
  229. }
  230. static const struct kernfs_ops rdtgroup_kf_single_ops = {
  231. .atomic_write_len = PAGE_SIZE,
  232. .write = rdtgroup_file_write,
  233. .seq_show = rdtgroup_seqfile_show,
  234. };
  235. static const struct kernfs_ops kf_mondata_ops = {
  236. .atomic_write_len = PAGE_SIZE,
  237. .seq_show = rdtgroup_mondata_show,
  238. };
  239. static bool is_cpu_list(struct kernfs_open_file *of)
  240. {
  241. struct rftype *rft = of->kn->priv;
  242. return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
  243. }
  244. static int rdtgroup_cpus_show(struct kernfs_open_file *of,
  245. struct seq_file *s, void *v)
  246. {
  247. struct rdtgroup *rdtgrp;
  248. struct cpumask *mask;
  249. int ret = 0;
  250. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  251. if (rdtgrp) {
  252. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  253. if (!rdtgrp->plr->d) {
  254. rdt_last_cmd_clear();
  255. rdt_last_cmd_puts("Cache domain offline\n");
  256. ret = -ENODEV;
  257. } else {
  258. mask = &rdtgrp->plr->d->hdr.cpu_mask;
  259. seq_printf(s, is_cpu_list(of) ?
  260. "%*pbl\n" : "%*pb\n",
  261. cpumask_pr_args(mask));
  262. }
  263. } else {
  264. seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
  265. cpumask_pr_args(&rdtgrp->cpu_mask));
  266. }
  267. } else {
  268. ret = -ENOENT;
  269. }
  270. rdtgroup_kn_unlock(of->kn);
  271. return ret;
  272. }
  273. /*
  274. * This is safe against resctrl_sched_in() called from __switch_to()
  275. * because __switch_to() is executed with interrupts disabled. A local call
  276. * from update_closid_rmid() is protected against __switch_to() because
  277. * preemption is disabled.
  278. */
  279. static void update_cpu_closid_rmid(void *info)
  280. {
  281. struct rdtgroup *r = info;
  282. if (r) {
  283. this_cpu_write(pqr_state.default_closid, r->closid);
  284. this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
  285. }
  286. /*
  287. * We cannot unconditionally write the MSR because the current
  288. * executing task might have its own closid selected. Just reuse
  289. * the context switch code.
  290. */
  291. resctrl_sched_in(current);
  292. }
  293. /*
  294. * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
  295. *
  296. * Per task closids/rmids must have been set up before calling this function.
  297. */
  298. static void
  299. update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
  300. {
  301. on_each_cpu_mask(cpu_mask, update_cpu_closid_rmid, r, 1);
  302. }
  303. static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
  304. cpumask_var_t tmpmask)
  305. {
  306. struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
  307. struct list_head *head;
  308. /* Check whether cpus belong to parent ctrl group */
  309. cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
  310. if (!cpumask_empty(tmpmask)) {
  311. rdt_last_cmd_puts("Can only add CPUs to mongroup that belong to parent\n");
  312. return -EINVAL;
  313. }
  314. /* Check whether cpus are dropped from this group */
  315. cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
  316. if (!cpumask_empty(tmpmask)) {
  317. /* Give any dropped cpus to parent rdtgroup */
  318. cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
  319. update_closid_rmid(tmpmask, prgrp);
  320. }
  321. /*
  322. * If we added cpus, remove them from previous group that owned them
  323. * and update per-cpu rmid
  324. */
  325. cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
  326. if (!cpumask_empty(tmpmask)) {
  327. head = &prgrp->mon.crdtgrp_list;
  328. list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
  329. if (crgrp == rdtgrp)
  330. continue;
  331. cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
  332. tmpmask);
  333. }
  334. update_closid_rmid(tmpmask, rdtgrp);
  335. }
  336. /* Done pushing/pulling - update this group with new mask */
  337. cpumask_copy(&rdtgrp->cpu_mask, newmask);
  338. return 0;
  339. }
  340. static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
  341. {
  342. struct rdtgroup *crgrp;
  343. cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
  344. /* update the child mon group masks as well*/
  345. list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
  346. cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
  347. }
  348. static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
  349. cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
  350. {
  351. struct rdtgroup *r, *crgrp;
  352. struct list_head *head;
  353. /* Check whether cpus are dropped from this group */
  354. cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
  355. if (!cpumask_empty(tmpmask)) {
  356. /* Can't drop from default group */
  357. if (rdtgrp == &rdtgroup_default) {
  358. rdt_last_cmd_puts("Can't drop CPUs from default group\n");
  359. return -EINVAL;
  360. }
  361. /* Give any dropped cpus to rdtgroup_default */
  362. cpumask_or(&rdtgroup_default.cpu_mask,
  363. &rdtgroup_default.cpu_mask, tmpmask);
  364. update_closid_rmid(tmpmask, &rdtgroup_default);
  365. }
  366. /*
  367. * If we added cpus, remove them from previous group and
  368. * the prev group's child groups that owned them
  369. * and update per-cpu closid/rmid.
  370. */
  371. cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
  372. if (!cpumask_empty(tmpmask)) {
  373. list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
  374. if (r == rdtgrp)
  375. continue;
  376. cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
  377. if (!cpumask_empty(tmpmask1))
  378. cpumask_rdtgrp_clear(r, tmpmask1);
  379. }
  380. update_closid_rmid(tmpmask, rdtgrp);
  381. }
  382. /* Done pushing/pulling - update this group with new mask */
  383. cpumask_copy(&rdtgrp->cpu_mask, newmask);
  384. /*
  385. * Clear child mon group masks since there is a new parent mask
  386. * now and update the rmid for the cpus the child lost.
  387. */
  388. head = &rdtgrp->mon.crdtgrp_list;
  389. list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
  390. cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
  391. update_closid_rmid(tmpmask, rdtgrp);
  392. cpumask_clear(&crgrp->cpu_mask);
  393. }
  394. return 0;
  395. }
  396. static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
  397. char *buf, size_t nbytes, loff_t off)
  398. {
  399. cpumask_var_t tmpmask, newmask, tmpmask1;
  400. struct rdtgroup *rdtgrp;
  401. int ret;
  402. if (!buf)
  403. return -EINVAL;
  404. if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
  405. return -ENOMEM;
  406. if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
  407. free_cpumask_var(tmpmask);
  408. return -ENOMEM;
  409. }
  410. if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
  411. free_cpumask_var(tmpmask);
  412. free_cpumask_var(newmask);
  413. return -ENOMEM;
  414. }
  415. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  416. if (!rdtgrp) {
  417. ret = -ENOENT;
  418. goto unlock;
  419. }
  420. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
  421. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  422. ret = -EINVAL;
  423. rdt_last_cmd_puts("Pseudo-locking in progress\n");
  424. goto unlock;
  425. }
  426. if (is_cpu_list(of))
  427. ret = cpulist_parse(buf, newmask);
  428. else
  429. ret = cpumask_parse(buf, newmask);
  430. if (ret) {
  431. rdt_last_cmd_puts("Bad CPU list/mask\n");
  432. goto unlock;
  433. }
  434. /* check that user didn't specify any offline cpus */
  435. cpumask_andnot(tmpmask, newmask, cpu_online_mask);
  436. if (!cpumask_empty(tmpmask)) {
  437. ret = -EINVAL;
  438. rdt_last_cmd_puts("Can only assign online CPUs\n");
  439. goto unlock;
  440. }
  441. if (rdtgrp->type == RDTCTRL_GROUP)
  442. ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
  443. else if (rdtgrp->type == RDTMON_GROUP)
  444. ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
  445. else
  446. ret = -EINVAL;
  447. unlock:
  448. rdtgroup_kn_unlock(of->kn);
  449. free_cpumask_var(tmpmask);
  450. free_cpumask_var(newmask);
  451. free_cpumask_var(tmpmask1);
  452. return ret ?: nbytes;
  453. }
  454. /**
  455. * rdtgroup_remove - the helper to remove resource group safely
  456. * @rdtgrp: resource group to remove
  457. *
  458. * On resource group creation via a mkdir, an extra kernfs_node reference is
  459. * taken to ensure that the rdtgroup structure remains accessible for the
  460. * rdtgroup_kn_unlock() calls where it is removed.
  461. *
  462. * Drop the extra reference here, then free the rdtgroup structure.
  463. *
  464. * Return: void
  465. */
  466. static void rdtgroup_remove(struct rdtgroup *rdtgrp)
  467. {
  468. kernfs_put(rdtgrp->kn);
  469. kfree(rdtgrp);
  470. }
  471. static void _update_task_closid_rmid(void *task)
  472. {
  473. /*
  474. * If the task is still current on this CPU, update PQR_ASSOC MSR.
  475. * Otherwise, the MSR is updated when the task is scheduled in.
  476. */
  477. if (task == current)
  478. resctrl_sched_in(task);
  479. }
  480. static void update_task_closid_rmid(struct task_struct *t)
  481. {
  482. if (IS_ENABLED(CONFIG_SMP) && task_curr(t))
  483. smp_call_function_single(task_cpu(t), _update_task_closid_rmid, t, 1);
  484. else
  485. _update_task_closid_rmid(t);
  486. }
  487. static bool task_in_rdtgroup(struct task_struct *tsk, struct rdtgroup *rdtgrp)
  488. {
  489. u32 closid, rmid = rdtgrp->mon.rmid;
  490. if (rdtgrp->type == RDTCTRL_GROUP)
  491. closid = rdtgrp->closid;
  492. else if (rdtgrp->type == RDTMON_GROUP)
  493. closid = rdtgrp->mon.parent->closid;
  494. else
  495. return false;
  496. return resctrl_arch_match_closid(tsk, closid) &&
  497. resctrl_arch_match_rmid(tsk, closid, rmid);
  498. }
  499. static int __rdtgroup_move_task(struct task_struct *tsk,
  500. struct rdtgroup *rdtgrp)
  501. {
  502. /* If the task is already in rdtgrp, no need to move the task. */
  503. if (task_in_rdtgroup(tsk, rdtgrp))
  504. return 0;
  505. /*
  506. * Set the task's closid/rmid before the PQR_ASSOC MSR can be
  507. * updated by them.
  508. *
  509. * For ctrl_mon groups, move both closid and rmid.
  510. * For monitor groups, can move the tasks only from
  511. * their parent CTRL group.
  512. */
  513. if (rdtgrp->type == RDTMON_GROUP &&
  514. !resctrl_arch_match_closid(tsk, rdtgrp->mon.parent->closid)) {
  515. rdt_last_cmd_puts("Can't move task to different control group\n");
  516. return -EINVAL;
  517. }
  518. if (rdtgrp->type == RDTMON_GROUP)
  519. resctrl_arch_set_closid_rmid(tsk, rdtgrp->mon.parent->closid,
  520. rdtgrp->mon.rmid);
  521. else
  522. resctrl_arch_set_closid_rmid(tsk, rdtgrp->closid,
  523. rdtgrp->mon.rmid);
  524. /*
  525. * Ensure the task's closid and rmid are written before determining if
  526. * the task is current that will decide if it will be interrupted.
  527. * This pairs with the full barrier between the rq->curr update and
  528. * resctrl_sched_in() during context switch.
  529. */
  530. smp_mb();
  531. /*
  532. * By now, the task's closid and rmid are set. If the task is current
  533. * on a CPU, the PQR_ASSOC MSR needs to be updated to make the resource
  534. * group go into effect. If the task is not current, the MSR will be
  535. * updated when the task is scheduled in.
  536. */
  537. update_task_closid_rmid(tsk);
  538. return 0;
  539. }
  540. static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
  541. {
  542. return (resctrl_arch_alloc_capable() && (r->type == RDTCTRL_GROUP) &&
  543. resctrl_arch_match_closid(t, r->closid));
  544. }
  545. static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
  546. {
  547. return (resctrl_arch_mon_capable() && (r->type == RDTMON_GROUP) &&
  548. resctrl_arch_match_rmid(t, r->mon.parent->closid,
  549. r->mon.rmid));
  550. }
  551. /**
  552. * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group
  553. * @r: Resource group
  554. *
  555. * Return: 1 if tasks have been assigned to @r, 0 otherwise
  556. */
  557. int rdtgroup_tasks_assigned(struct rdtgroup *r)
  558. {
  559. struct task_struct *p, *t;
  560. int ret = 0;
  561. lockdep_assert_held(&rdtgroup_mutex);
  562. rcu_read_lock();
  563. for_each_process_thread(p, t) {
  564. if (is_closid_match(t, r) || is_rmid_match(t, r)) {
  565. ret = 1;
  566. break;
  567. }
  568. }
  569. rcu_read_unlock();
  570. return ret;
  571. }
  572. static int rdtgroup_task_write_permission(struct task_struct *task,
  573. struct kernfs_open_file *of)
  574. {
  575. const struct cred *tcred = get_task_cred(task);
  576. const struct cred *cred = current_cred();
  577. int ret = 0;
  578. /*
  579. * Even if we're attaching all tasks in the thread group, we only
  580. * need to check permissions on one of them.
  581. */
  582. if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
  583. !uid_eq(cred->euid, tcred->uid) &&
  584. !uid_eq(cred->euid, tcred->suid)) {
  585. rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
  586. ret = -EPERM;
  587. }
  588. put_cred(tcred);
  589. return ret;
  590. }
  591. static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
  592. struct kernfs_open_file *of)
  593. {
  594. struct task_struct *tsk;
  595. int ret;
  596. rcu_read_lock();
  597. if (pid) {
  598. tsk = find_task_by_vpid(pid);
  599. if (!tsk) {
  600. rcu_read_unlock();
  601. rdt_last_cmd_printf("No task %d\n", pid);
  602. return -ESRCH;
  603. }
  604. } else {
  605. tsk = current;
  606. }
  607. get_task_struct(tsk);
  608. rcu_read_unlock();
  609. ret = rdtgroup_task_write_permission(tsk, of);
  610. if (!ret)
  611. ret = __rdtgroup_move_task(tsk, rdtgrp);
  612. put_task_struct(tsk);
  613. return ret;
  614. }
  615. static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
  616. char *buf, size_t nbytes, loff_t off)
  617. {
  618. struct rdtgroup *rdtgrp;
  619. char *pid_str;
  620. int ret = 0;
  621. pid_t pid;
  622. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  623. if (!rdtgrp) {
  624. rdtgroup_kn_unlock(of->kn);
  625. return -ENOENT;
  626. }
  627. rdt_last_cmd_clear();
  628. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
  629. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  630. ret = -EINVAL;
  631. rdt_last_cmd_puts("Pseudo-locking in progress\n");
  632. goto unlock;
  633. }
  634. while (buf && buf[0] != '\0' && buf[0] != '\n') {
  635. pid_str = strim(strsep(&buf, ","));
  636. if (kstrtoint(pid_str, 0, &pid)) {
  637. rdt_last_cmd_printf("Task list parsing error pid %s\n", pid_str);
  638. ret = -EINVAL;
  639. break;
  640. }
  641. if (pid < 0) {
  642. rdt_last_cmd_printf("Invalid pid %d\n", pid);
  643. ret = -EINVAL;
  644. break;
  645. }
  646. ret = rdtgroup_move_task(pid, rdtgrp, of);
  647. if (ret) {
  648. rdt_last_cmd_printf("Error while processing task %d\n", pid);
  649. break;
  650. }
  651. }
  652. unlock:
  653. rdtgroup_kn_unlock(of->kn);
  654. return ret ?: nbytes;
  655. }
  656. static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
  657. {
  658. struct task_struct *p, *t;
  659. pid_t pid;
  660. rcu_read_lock();
  661. for_each_process_thread(p, t) {
  662. if (is_closid_match(t, r) || is_rmid_match(t, r)) {
  663. pid = task_pid_vnr(t);
  664. if (pid)
  665. seq_printf(s, "%d\n", pid);
  666. }
  667. }
  668. rcu_read_unlock();
  669. }
  670. static int rdtgroup_tasks_show(struct kernfs_open_file *of,
  671. struct seq_file *s, void *v)
  672. {
  673. struct rdtgroup *rdtgrp;
  674. int ret = 0;
  675. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  676. if (rdtgrp)
  677. show_rdt_tasks(rdtgrp, s);
  678. else
  679. ret = -ENOENT;
  680. rdtgroup_kn_unlock(of->kn);
  681. return ret;
  682. }
  683. static int rdtgroup_closid_show(struct kernfs_open_file *of,
  684. struct seq_file *s, void *v)
  685. {
  686. struct rdtgroup *rdtgrp;
  687. int ret = 0;
  688. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  689. if (rdtgrp)
  690. seq_printf(s, "%u\n", rdtgrp->closid);
  691. else
  692. ret = -ENOENT;
  693. rdtgroup_kn_unlock(of->kn);
  694. return ret;
  695. }
  696. static int rdtgroup_rmid_show(struct kernfs_open_file *of,
  697. struct seq_file *s, void *v)
  698. {
  699. struct rdtgroup *rdtgrp;
  700. int ret = 0;
  701. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  702. if (rdtgrp)
  703. seq_printf(s, "%u\n", rdtgrp->mon.rmid);
  704. else
  705. ret = -ENOENT;
  706. rdtgroup_kn_unlock(of->kn);
  707. return ret;
  708. }
  709. #ifdef CONFIG_PROC_CPU_RESCTRL
  710. /*
  711. * A task can only be part of one resctrl control group and of one monitor
  712. * group which is associated to that control group.
  713. *
  714. * 1) res:
  715. * mon:
  716. *
  717. * resctrl is not available.
  718. *
  719. * 2) res:/
  720. * mon:
  721. *
  722. * Task is part of the root resctrl control group, and it is not associated
  723. * to any monitor group.
  724. *
  725. * 3) res:/
  726. * mon:mon0
  727. *
  728. * Task is part of the root resctrl control group and monitor group mon0.
  729. *
  730. * 4) res:group0
  731. * mon:
  732. *
  733. * Task is part of resctrl control group group0, and it is not associated
  734. * to any monitor group.
  735. *
  736. * 5) res:group0
  737. * mon:mon1
  738. *
  739. * Task is part of resctrl control group group0 and monitor group mon1.
  740. */
  741. int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns,
  742. struct pid *pid, struct task_struct *tsk)
  743. {
  744. struct rdtgroup *rdtg;
  745. int ret = 0;
  746. mutex_lock(&rdtgroup_mutex);
  747. /* Return empty if resctrl has not been mounted. */
  748. if (!resctrl_mounted) {
  749. seq_puts(s, "res:\nmon:\n");
  750. goto unlock;
  751. }
  752. list_for_each_entry(rdtg, &rdt_all_groups, rdtgroup_list) {
  753. struct rdtgroup *crg;
  754. /*
  755. * Task information is only relevant for shareable
  756. * and exclusive groups.
  757. */
  758. if (rdtg->mode != RDT_MODE_SHAREABLE &&
  759. rdtg->mode != RDT_MODE_EXCLUSIVE)
  760. continue;
  761. if (!resctrl_arch_match_closid(tsk, rdtg->closid))
  762. continue;
  763. seq_printf(s, "res:%s%s\n", (rdtg == &rdtgroup_default) ? "/" : "",
  764. rdtg->kn->name);
  765. seq_puts(s, "mon:");
  766. list_for_each_entry(crg, &rdtg->mon.crdtgrp_list,
  767. mon.crdtgrp_list) {
  768. if (!resctrl_arch_match_rmid(tsk, crg->mon.parent->closid,
  769. crg->mon.rmid))
  770. continue;
  771. seq_printf(s, "%s", crg->kn->name);
  772. break;
  773. }
  774. seq_putc(s, '\n');
  775. goto unlock;
  776. }
  777. /*
  778. * The above search should succeed. Otherwise return
  779. * with an error.
  780. */
  781. ret = -ENOENT;
  782. unlock:
  783. mutex_unlock(&rdtgroup_mutex);
  784. return ret;
  785. }
  786. #endif
  787. static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
  788. struct seq_file *seq, void *v)
  789. {
  790. int len;
  791. mutex_lock(&rdtgroup_mutex);
  792. len = seq_buf_used(&last_cmd_status);
  793. if (len)
  794. seq_printf(seq, "%.*s", len, last_cmd_status_buf);
  795. else
  796. seq_puts(seq, "ok\n");
  797. mutex_unlock(&rdtgroup_mutex);
  798. return 0;
  799. }
  800. static int rdt_num_closids_show(struct kernfs_open_file *of,
  801. struct seq_file *seq, void *v)
  802. {
  803. struct resctrl_schema *s = of->kn->parent->priv;
  804. seq_printf(seq, "%u\n", s->num_closid);
  805. return 0;
  806. }
  807. static int rdt_default_ctrl_show(struct kernfs_open_file *of,
  808. struct seq_file *seq, void *v)
  809. {
  810. struct resctrl_schema *s = of->kn->parent->priv;
  811. struct rdt_resource *r = s->res;
  812. seq_printf(seq, "%x\n", r->default_ctrl);
  813. return 0;
  814. }
  815. static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
  816. struct seq_file *seq, void *v)
  817. {
  818. struct resctrl_schema *s = of->kn->parent->priv;
  819. struct rdt_resource *r = s->res;
  820. seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
  821. return 0;
  822. }
  823. static int rdt_shareable_bits_show(struct kernfs_open_file *of,
  824. struct seq_file *seq, void *v)
  825. {
  826. struct resctrl_schema *s = of->kn->parent->priv;
  827. struct rdt_resource *r = s->res;
  828. seq_printf(seq, "%x\n", r->cache.shareable_bits);
  829. return 0;
  830. }
  831. /*
  832. * rdt_bit_usage_show - Display current usage of resources
  833. *
  834. * A domain is a shared resource that can now be allocated differently. Here
  835. * we display the current regions of the domain as an annotated bitmask.
  836. * For each domain of this resource its allocation bitmask
  837. * is annotated as below to indicate the current usage of the corresponding bit:
  838. * 0 - currently unused
  839. * X - currently available for sharing and used by software and hardware
  840. * H - currently used by hardware only but available for software use
  841. * S - currently used and shareable by software only
  842. * E - currently used exclusively by one resource group
  843. * P - currently pseudo-locked by one resource group
  844. */
  845. static int rdt_bit_usage_show(struct kernfs_open_file *of,
  846. struct seq_file *seq, void *v)
  847. {
  848. struct resctrl_schema *s = of->kn->parent->priv;
  849. /*
  850. * Use unsigned long even though only 32 bits are used to ensure
  851. * test_bit() is used safely.
  852. */
  853. unsigned long sw_shareable = 0, hw_shareable = 0;
  854. unsigned long exclusive = 0, pseudo_locked = 0;
  855. struct rdt_resource *r = s->res;
  856. struct rdt_ctrl_domain *dom;
  857. int i, hwb, swb, excl, psl;
  858. enum rdtgrp_mode mode;
  859. bool sep = false;
  860. u32 ctrl_val;
  861. cpus_read_lock();
  862. mutex_lock(&rdtgroup_mutex);
  863. hw_shareable = r->cache.shareable_bits;
  864. list_for_each_entry(dom, &r->ctrl_domains, hdr.list) {
  865. if (sep)
  866. seq_putc(seq, ';');
  867. sw_shareable = 0;
  868. exclusive = 0;
  869. seq_printf(seq, "%d=", dom->hdr.id);
  870. for (i = 0; i < closids_supported(); i++) {
  871. if (!closid_allocated(i))
  872. continue;
  873. ctrl_val = resctrl_arch_get_config(r, dom, i,
  874. s->conf_type);
  875. mode = rdtgroup_mode_by_closid(i);
  876. switch (mode) {
  877. case RDT_MODE_SHAREABLE:
  878. sw_shareable |= ctrl_val;
  879. break;
  880. case RDT_MODE_EXCLUSIVE:
  881. exclusive |= ctrl_val;
  882. break;
  883. case RDT_MODE_PSEUDO_LOCKSETUP:
  884. /*
  885. * RDT_MODE_PSEUDO_LOCKSETUP is possible
  886. * here but not included since the CBM
  887. * associated with this CLOSID in this mode
  888. * is not initialized and no task or cpu can be
  889. * assigned this CLOSID.
  890. */
  891. break;
  892. case RDT_MODE_PSEUDO_LOCKED:
  893. case RDT_NUM_MODES:
  894. WARN(1,
  895. "invalid mode for closid %d\n", i);
  896. break;
  897. }
  898. }
  899. for (i = r->cache.cbm_len - 1; i >= 0; i--) {
  900. pseudo_locked = dom->plr ? dom->plr->cbm : 0;
  901. hwb = test_bit(i, &hw_shareable);
  902. swb = test_bit(i, &sw_shareable);
  903. excl = test_bit(i, &exclusive);
  904. psl = test_bit(i, &pseudo_locked);
  905. if (hwb && swb)
  906. seq_putc(seq, 'X');
  907. else if (hwb && !swb)
  908. seq_putc(seq, 'H');
  909. else if (!hwb && swb)
  910. seq_putc(seq, 'S');
  911. else if (excl)
  912. seq_putc(seq, 'E');
  913. else if (psl)
  914. seq_putc(seq, 'P');
  915. else /* Unused bits remain */
  916. seq_putc(seq, '0');
  917. }
  918. sep = true;
  919. }
  920. seq_putc(seq, '\n');
  921. mutex_unlock(&rdtgroup_mutex);
  922. cpus_read_unlock();
  923. return 0;
  924. }
  925. static int rdt_min_bw_show(struct kernfs_open_file *of,
  926. struct seq_file *seq, void *v)
  927. {
  928. struct resctrl_schema *s = of->kn->parent->priv;
  929. struct rdt_resource *r = s->res;
  930. seq_printf(seq, "%u\n", r->membw.min_bw);
  931. return 0;
  932. }
  933. static int rdt_num_rmids_show(struct kernfs_open_file *of,
  934. struct seq_file *seq, void *v)
  935. {
  936. struct rdt_resource *r = of->kn->parent->priv;
  937. seq_printf(seq, "%d\n", r->num_rmid);
  938. return 0;
  939. }
  940. static int rdt_mon_features_show(struct kernfs_open_file *of,
  941. struct seq_file *seq, void *v)
  942. {
  943. struct rdt_resource *r = of->kn->parent->priv;
  944. struct mon_evt *mevt;
  945. list_for_each_entry(mevt, &r->evt_list, list) {
  946. seq_printf(seq, "%s\n", mevt->name);
  947. if (mevt->configurable)
  948. seq_printf(seq, "%s_config\n", mevt->name);
  949. }
  950. return 0;
  951. }
  952. static int rdt_bw_gran_show(struct kernfs_open_file *of,
  953. struct seq_file *seq, void *v)
  954. {
  955. struct resctrl_schema *s = of->kn->parent->priv;
  956. struct rdt_resource *r = s->res;
  957. seq_printf(seq, "%u\n", r->membw.bw_gran);
  958. return 0;
  959. }
  960. static int rdt_delay_linear_show(struct kernfs_open_file *of,
  961. struct seq_file *seq, void *v)
  962. {
  963. struct resctrl_schema *s = of->kn->parent->priv;
  964. struct rdt_resource *r = s->res;
  965. seq_printf(seq, "%u\n", r->membw.delay_linear);
  966. return 0;
  967. }
  968. static int max_threshold_occ_show(struct kernfs_open_file *of,
  969. struct seq_file *seq, void *v)
  970. {
  971. seq_printf(seq, "%u\n", resctrl_rmid_realloc_threshold);
  972. return 0;
  973. }
  974. static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of,
  975. struct seq_file *seq, void *v)
  976. {
  977. struct resctrl_schema *s = of->kn->parent->priv;
  978. struct rdt_resource *r = s->res;
  979. if (r->membw.throttle_mode == THREAD_THROTTLE_PER_THREAD)
  980. seq_puts(seq, "per-thread\n");
  981. else
  982. seq_puts(seq, "max\n");
  983. return 0;
  984. }
  985. static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
  986. char *buf, size_t nbytes, loff_t off)
  987. {
  988. unsigned int bytes;
  989. int ret;
  990. ret = kstrtouint(buf, 0, &bytes);
  991. if (ret)
  992. return ret;
  993. if (bytes > resctrl_rmid_realloc_limit)
  994. return -EINVAL;
  995. resctrl_rmid_realloc_threshold = resctrl_arch_round_mon_val(bytes);
  996. return nbytes;
  997. }
  998. /*
  999. * rdtgroup_mode_show - Display mode of this resource group
  1000. */
  1001. static int rdtgroup_mode_show(struct kernfs_open_file *of,
  1002. struct seq_file *s, void *v)
  1003. {
  1004. struct rdtgroup *rdtgrp;
  1005. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  1006. if (!rdtgrp) {
  1007. rdtgroup_kn_unlock(of->kn);
  1008. return -ENOENT;
  1009. }
  1010. seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode));
  1011. rdtgroup_kn_unlock(of->kn);
  1012. return 0;
  1013. }
  1014. static enum resctrl_conf_type resctrl_peer_type(enum resctrl_conf_type my_type)
  1015. {
  1016. switch (my_type) {
  1017. case CDP_CODE:
  1018. return CDP_DATA;
  1019. case CDP_DATA:
  1020. return CDP_CODE;
  1021. default:
  1022. case CDP_NONE:
  1023. return CDP_NONE;
  1024. }
  1025. }
  1026. static int rdt_has_sparse_bitmasks_show(struct kernfs_open_file *of,
  1027. struct seq_file *seq, void *v)
  1028. {
  1029. struct resctrl_schema *s = of->kn->parent->priv;
  1030. struct rdt_resource *r = s->res;
  1031. seq_printf(seq, "%u\n", r->cache.arch_has_sparse_bitmasks);
  1032. return 0;
  1033. }
  1034. /**
  1035. * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other
  1036. * @r: Resource to which domain instance @d belongs.
  1037. * @d: The domain instance for which @closid is being tested.
  1038. * @cbm: Capacity bitmask being tested.
  1039. * @closid: Intended closid for @cbm.
  1040. * @type: CDP type of @r.
  1041. * @exclusive: Only check if overlaps with exclusive resource groups
  1042. *
  1043. * Checks if provided @cbm intended to be used for @closid on domain
  1044. * @d overlaps with any other closids or other hardware usage associated
  1045. * with this domain. If @exclusive is true then only overlaps with
  1046. * resource groups in exclusive mode will be considered. If @exclusive
  1047. * is false then overlaps with any resource group or hardware entities
  1048. * will be considered.
  1049. *
  1050. * @cbm is unsigned long, even if only 32 bits are used, to make the
  1051. * bitmap functions work correctly.
  1052. *
  1053. * Return: false if CBM does not overlap, true if it does.
  1054. */
  1055. static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_ctrl_domain *d,
  1056. unsigned long cbm, int closid,
  1057. enum resctrl_conf_type type, bool exclusive)
  1058. {
  1059. enum rdtgrp_mode mode;
  1060. unsigned long ctrl_b;
  1061. int i;
  1062. /* Check for any overlap with regions used by hardware directly */
  1063. if (!exclusive) {
  1064. ctrl_b = r->cache.shareable_bits;
  1065. if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len))
  1066. return true;
  1067. }
  1068. /* Check for overlap with other resource groups */
  1069. for (i = 0; i < closids_supported(); i++) {
  1070. ctrl_b = resctrl_arch_get_config(r, d, i, type);
  1071. mode = rdtgroup_mode_by_closid(i);
  1072. if (closid_allocated(i) && i != closid &&
  1073. mode != RDT_MODE_PSEUDO_LOCKSETUP) {
  1074. if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) {
  1075. if (exclusive) {
  1076. if (mode == RDT_MODE_EXCLUSIVE)
  1077. return true;
  1078. continue;
  1079. }
  1080. return true;
  1081. }
  1082. }
  1083. }
  1084. return false;
  1085. }
  1086. /**
  1087. * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware
  1088. * @s: Schema for the resource to which domain instance @d belongs.
  1089. * @d: The domain instance for which @closid is being tested.
  1090. * @cbm: Capacity bitmask being tested.
  1091. * @closid: Intended closid for @cbm.
  1092. * @exclusive: Only check if overlaps with exclusive resource groups
  1093. *
  1094. * Resources that can be allocated using a CBM can use the CBM to control
  1095. * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test
  1096. * for overlap. Overlap test is not limited to the specific resource for
  1097. * which the CBM is intended though - when dealing with CDP resources that
  1098. * share the underlying hardware the overlap check should be performed on
  1099. * the CDP resource sharing the hardware also.
  1100. *
  1101. * Refer to description of __rdtgroup_cbm_overlaps() for the details of the
  1102. * overlap test.
  1103. *
  1104. * Return: true if CBM overlap detected, false if there is no overlap
  1105. */
  1106. bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_ctrl_domain *d,
  1107. unsigned long cbm, int closid, bool exclusive)
  1108. {
  1109. enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
  1110. struct rdt_resource *r = s->res;
  1111. if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, s->conf_type,
  1112. exclusive))
  1113. return true;
  1114. if (!resctrl_arch_get_cdp_enabled(r->rid))
  1115. return false;
  1116. return __rdtgroup_cbm_overlaps(r, d, cbm, closid, peer_type, exclusive);
  1117. }
  1118. /**
  1119. * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive
  1120. * @rdtgrp: Resource group identified through its closid.
  1121. *
  1122. * An exclusive resource group implies that there should be no sharing of
  1123. * its allocated resources. At the time this group is considered to be
  1124. * exclusive this test can determine if its current schemata supports this
  1125. * setting by testing for overlap with all other resource groups.
  1126. *
  1127. * Return: true if resource group can be exclusive, false if there is overlap
  1128. * with allocations of other resource groups and thus this resource group
  1129. * cannot be exclusive.
  1130. */
  1131. static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
  1132. {
  1133. int closid = rdtgrp->closid;
  1134. struct rdt_ctrl_domain *d;
  1135. struct resctrl_schema *s;
  1136. struct rdt_resource *r;
  1137. bool has_cache = false;
  1138. u32 ctrl;
  1139. /* Walking r->domains, ensure it can't race with cpuhp */
  1140. lockdep_assert_cpus_held();
  1141. list_for_each_entry(s, &resctrl_schema_all, list) {
  1142. r = s->res;
  1143. if (r->rid == RDT_RESOURCE_MBA || r->rid == RDT_RESOURCE_SMBA)
  1144. continue;
  1145. has_cache = true;
  1146. list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
  1147. ctrl = resctrl_arch_get_config(r, d, closid,
  1148. s->conf_type);
  1149. if (rdtgroup_cbm_overlaps(s, d, ctrl, closid, false)) {
  1150. rdt_last_cmd_puts("Schemata overlaps\n");
  1151. return false;
  1152. }
  1153. }
  1154. }
  1155. if (!has_cache) {
  1156. rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n");
  1157. return false;
  1158. }
  1159. return true;
  1160. }
  1161. /*
  1162. * rdtgroup_mode_write - Modify the resource group's mode
  1163. */
  1164. static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
  1165. char *buf, size_t nbytes, loff_t off)
  1166. {
  1167. struct rdtgroup *rdtgrp;
  1168. enum rdtgrp_mode mode;
  1169. int ret = 0;
  1170. /* Valid input requires a trailing newline */
  1171. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  1172. return -EINVAL;
  1173. buf[nbytes - 1] = '\0';
  1174. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  1175. if (!rdtgrp) {
  1176. rdtgroup_kn_unlock(of->kn);
  1177. return -ENOENT;
  1178. }
  1179. rdt_last_cmd_clear();
  1180. mode = rdtgrp->mode;
  1181. if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) ||
  1182. (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) ||
  1183. (!strcmp(buf, "pseudo-locksetup") &&
  1184. mode == RDT_MODE_PSEUDO_LOCKSETUP) ||
  1185. (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED))
  1186. goto out;
  1187. if (mode == RDT_MODE_PSEUDO_LOCKED) {
  1188. rdt_last_cmd_puts("Cannot change pseudo-locked group\n");
  1189. ret = -EINVAL;
  1190. goto out;
  1191. }
  1192. if (!strcmp(buf, "shareable")) {
  1193. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  1194. ret = rdtgroup_locksetup_exit(rdtgrp);
  1195. if (ret)
  1196. goto out;
  1197. }
  1198. rdtgrp->mode = RDT_MODE_SHAREABLE;
  1199. } else if (!strcmp(buf, "exclusive")) {
  1200. if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
  1201. ret = -EINVAL;
  1202. goto out;
  1203. }
  1204. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  1205. ret = rdtgroup_locksetup_exit(rdtgrp);
  1206. if (ret)
  1207. goto out;
  1208. }
  1209. rdtgrp->mode = RDT_MODE_EXCLUSIVE;
  1210. } else if (!strcmp(buf, "pseudo-locksetup")) {
  1211. ret = rdtgroup_locksetup_enter(rdtgrp);
  1212. if (ret)
  1213. goto out;
  1214. rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP;
  1215. } else {
  1216. rdt_last_cmd_puts("Unknown or unsupported mode\n");
  1217. ret = -EINVAL;
  1218. }
  1219. out:
  1220. rdtgroup_kn_unlock(of->kn);
  1221. return ret ?: nbytes;
  1222. }
  1223. /**
  1224. * rdtgroup_cbm_to_size - Translate CBM to size in bytes
  1225. * @r: RDT resource to which @d belongs.
  1226. * @d: RDT domain instance.
  1227. * @cbm: bitmask for which the size should be computed.
  1228. *
  1229. * The bitmask provided associated with the RDT domain instance @d will be
  1230. * translated into how many bytes it represents. The size in bytes is
  1231. * computed by first dividing the total cache size by the CBM length to
  1232. * determine how many bytes each bit in the bitmask represents. The result
  1233. * is multiplied with the number of bits set in the bitmask.
  1234. *
  1235. * @cbm is unsigned long, even if only 32 bits are used to make the
  1236. * bitmap functions work correctly.
  1237. */
  1238. unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
  1239. struct rdt_ctrl_domain *d, unsigned long cbm)
  1240. {
  1241. unsigned int size = 0;
  1242. struct cacheinfo *ci;
  1243. int num_b;
  1244. if (WARN_ON_ONCE(r->ctrl_scope != RESCTRL_L2_CACHE && r->ctrl_scope != RESCTRL_L3_CACHE))
  1245. return size;
  1246. num_b = bitmap_weight(&cbm, r->cache.cbm_len);
  1247. ci = get_cpu_cacheinfo_level(cpumask_any(&d->hdr.cpu_mask), r->ctrl_scope);
  1248. if (ci)
  1249. size = ci->size / r->cache.cbm_len * num_b;
  1250. return size;
  1251. }
  1252. /*
  1253. * rdtgroup_size_show - Display size in bytes of allocated regions
  1254. *
  1255. * The "size" file mirrors the layout of the "schemata" file, printing the
  1256. * size in bytes of each region instead of the capacity bitmask.
  1257. */
  1258. static int rdtgroup_size_show(struct kernfs_open_file *of,
  1259. struct seq_file *s, void *v)
  1260. {
  1261. struct resctrl_schema *schema;
  1262. enum resctrl_conf_type type;
  1263. struct rdt_ctrl_domain *d;
  1264. struct rdtgroup *rdtgrp;
  1265. struct rdt_resource *r;
  1266. unsigned int size;
  1267. int ret = 0;
  1268. u32 closid;
  1269. bool sep;
  1270. u32 ctrl;
  1271. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  1272. if (!rdtgrp) {
  1273. rdtgroup_kn_unlock(of->kn);
  1274. return -ENOENT;
  1275. }
  1276. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  1277. if (!rdtgrp->plr->d) {
  1278. rdt_last_cmd_clear();
  1279. rdt_last_cmd_puts("Cache domain offline\n");
  1280. ret = -ENODEV;
  1281. } else {
  1282. seq_printf(s, "%*s:", max_name_width,
  1283. rdtgrp->plr->s->name);
  1284. size = rdtgroup_cbm_to_size(rdtgrp->plr->s->res,
  1285. rdtgrp->plr->d,
  1286. rdtgrp->plr->cbm);
  1287. seq_printf(s, "%d=%u\n", rdtgrp->plr->d->hdr.id, size);
  1288. }
  1289. goto out;
  1290. }
  1291. closid = rdtgrp->closid;
  1292. list_for_each_entry(schema, &resctrl_schema_all, list) {
  1293. r = schema->res;
  1294. type = schema->conf_type;
  1295. sep = false;
  1296. seq_printf(s, "%*s:", max_name_width, schema->name);
  1297. list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
  1298. if (sep)
  1299. seq_putc(s, ';');
  1300. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  1301. size = 0;
  1302. } else {
  1303. if (is_mba_sc(r))
  1304. ctrl = d->mbps_val[closid];
  1305. else
  1306. ctrl = resctrl_arch_get_config(r, d,
  1307. closid,
  1308. type);
  1309. if (r->rid == RDT_RESOURCE_MBA ||
  1310. r->rid == RDT_RESOURCE_SMBA)
  1311. size = ctrl;
  1312. else
  1313. size = rdtgroup_cbm_to_size(r, d, ctrl);
  1314. }
  1315. seq_printf(s, "%d=%u", d->hdr.id, size);
  1316. sep = true;
  1317. }
  1318. seq_putc(s, '\n');
  1319. }
  1320. out:
  1321. rdtgroup_kn_unlock(of->kn);
  1322. return ret;
  1323. }
  1324. struct mon_config_info {
  1325. u32 evtid;
  1326. u32 mon_config;
  1327. };
  1328. #define INVALID_CONFIG_INDEX UINT_MAX
  1329. /**
  1330. * mon_event_config_index_get - get the hardware index for the
  1331. * configurable event
  1332. * @evtid: event id.
  1333. *
  1334. * Return: 0 for evtid == QOS_L3_MBM_TOTAL_EVENT_ID
  1335. * 1 for evtid == QOS_L3_MBM_LOCAL_EVENT_ID
  1336. * INVALID_CONFIG_INDEX for invalid evtid
  1337. */
  1338. static inline unsigned int mon_event_config_index_get(u32 evtid)
  1339. {
  1340. switch (evtid) {
  1341. case QOS_L3_MBM_TOTAL_EVENT_ID:
  1342. return 0;
  1343. case QOS_L3_MBM_LOCAL_EVENT_ID:
  1344. return 1;
  1345. default:
  1346. /* Should never reach here */
  1347. return INVALID_CONFIG_INDEX;
  1348. }
  1349. }
  1350. static void mon_event_config_read(void *info)
  1351. {
  1352. struct mon_config_info *mon_info = info;
  1353. unsigned int index;
  1354. u64 msrval;
  1355. index = mon_event_config_index_get(mon_info->evtid);
  1356. if (index == INVALID_CONFIG_INDEX) {
  1357. pr_warn_once("Invalid event id %d\n", mon_info->evtid);
  1358. return;
  1359. }
  1360. rdmsrl(MSR_IA32_EVT_CFG_BASE + index, msrval);
  1361. /* Report only the valid event configuration bits */
  1362. mon_info->mon_config = msrval & MAX_EVT_CONFIG_BITS;
  1363. }
  1364. static void mondata_config_read(struct rdt_mon_domain *d, struct mon_config_info *mon_info)
  1365. {
  1366. smp_call_function_any(&d->hdr.cpu_mask, mon_event_config_read, mon_info, 1);
  1367. }
  1368. static int mbm_config_show(struct seq_file *s, struct rdt_resource *r, u32 evtid)
  1369. {
  1370. struct mon_config_info mon_info = {0};
  1371. struct rdt_mon_domain *dom;
  1372. bool sep = false;
  1373. cpus_read_lock();
  1374. mutex_lock(&rdtgroup_mutex);
  1375. list_for_each_entry(dom, &r->mon_domains, hdr.list) {
  1376. if (sep)
  1377. seq_puts(s, ";");
  1378. memset(&mon_info, 0, sizeof(struct mon_config_info));
  1379. mon_info.evtid = evtid;
  1380. mondata_config_read(dom, &mon_info);
  1381. seq_printf(s, "%d=0x%02x", dom->hdr.id, mon_info.mon_config);
  1382. sep = true;
  1383. }
  1384. seq_puts(s, "\n");
  1385. mutex_unlock(&rdtgroup_mutex);
  1386. cpus_read_unlock();
  1387. return 0;
  1388. }
  1389. static int mbm_total_bytes_config_show(struct kernfs_open_file *of,
  1390. struct seq_file *seq, void *v)
  1391. {
  1392. struct rdt_resource *r = of->kn->parent->priv;
  1393. mbm_config_show(seq, r, QOS_L3_MBM_TOTAL_EVENT_ID);
  1394. return 0;
  1395. }
  1396. static int mbm_local_bytes_config_show(struct kernfs_open_file *of,
  1397. struct seq_file *seq, void *v)
  1398. {
  1399. struct rdt_resource *r = of->kn->parent->priv;
  1400. mbm_config_show(seq, r, QOS_L3_MBM_LOCAL_EVENT_ID);
  1401. return 0;
  1402. }
  1403. static void mon_event_config_write(void *info)
  1404. {
  1405. struct mon_config_info *mon_info = info;
  1406. unsigned int index;
  1407. index = mon_event_config_index_get(mon_info->evtid);
  1408. if (index == INVALID_CONFIG_INDEX) {
  1409. pr_warn_once("Invalid event id %d\n", mon_info->evtid);
  1410. return;
  1411. }
  1412. wrmsr(MSR_IA32_EVT_CFG_BASE + index, mon_info->mon_config, 0);
  1413. }
  1414. static void mbm_config_write_domain(struct rdt_resource *r,
  1415. struct rdt_mon_domain *d, u32 evtid, u32 val)
  1416. {
  1417. struct mon_config_info mon_info = {0};
  1418. /*
  1419. * Read the current config value first. If both are the same then
  1420. * no need to write it again.
  1421. */
  1422. mon_info.evtid = evtid;
  1423. mondata_config_read(d, &mon_info);
  1424. if (mon_info.mon_config == val)
  1425. return;
  1426. mon_info.mon_config = val;
  1427. /*
  1428. * Update MSR_IA32_EVT_CFG_BASE MSR on one of the CPUs in the
  1429. * domain. The MSRs offset from MSR MSR_IA32_EVT_CFG_BASE
  1430. * are scoped at the domain level. Writing any of these MSRs
  1431. * on one CPU is observed by all the CPUs in the domain.
  1432. */
  1433. smp_call_function_any(&d->hdr.cpu_mask, mon_event_config_write,
  1434. &mon_info, 1);
  1435. /*
  1436. * When an Event Configuration is changed, the bandwidth counters
  1437. * for all RMIDs and Events will be cleared by the hardware. The
  1438. * hardware also sets MSR_IA32_QM_CTR.Unavailable (bit 62) for
  1439. * every RMID on the next read to any event for every RMID.
  1440. * Subsequent reads will have MSR_IA32_QM_CTR.Unavailable (bit 62)
  1441. * cleared while it is tracked by the hardware. Clear the
  1442. * mbm_local and mbm_total counts for all the RMIDs.
  1443. */
  1444. resctrl_arch_reset_rmid_all(r, d);
  1445. }
  1446. static int mon_config_write(struct rdt_resource *r, char *tok, u32 evtid)
  1447. {
  1448. struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
  1449. char *dom_str = NULL, *id_str;
  1450. unsigned long dom_id, val;
  1451. struct rdt_mon_domain *d;
  1452. /* Walking r->domains, ensure it can't race with cpuhp */
  1453. lockdep_assert_cpus_held();
  1454. next:
  1455. if (!tok || tok[0] == '\0')
  1456. return 0;
  1457. /* Start processing the strings for each domain */
  1458. dom_str = strim(strsep(&tok, ";"));
  1459. id_str = strsep(&dom_str, "=");
  1460. if (!id_str || kstrtoul(id_str, 10, &dom_id)) {
  1461. rdt_last_cmd_puts("Missing '=' or non-numeric domain id\n");
  1462. return -EINVAL;
  1463. }
  1464. if (!dom_str || kstrtoul(dom_str, 16, &val)) {
  1465. rdt_last_cmd_puts("Non-numeric event configuration value\n");
  1466. return -EINVAL;
  1467. }
  1468. /* Value from user cannot be more than the supported set of events */
  1469. if ((val & hw_res->mbm_cfg_mask) != val) {
  1470. rdt_last_cmd_printf("Invalid event configuration: max valid mask is 0x%02x\n",
  1471. hw_res->mbm_cfg_mask);
  1472. return -EINVAL;
  1473. }
  1474. list_for_each_entry(d, &r->mon_domains, hdr.list) {
  1475. if (d->hdr.id == dom_id) {
  1476. mbm_config_write_domain(r, d, evtid, val);
  1477. goto next;
  1478. }
  1479. }
  1480. return -EINVAL;
  1481. }
  1482. static ssize_t mbm_total_bytes_config_write(struct kernfs_open_file *of,
  1483. char *buf, size_t nbytes,
  1484. loff_t off)
  1485. {
  1486. struct rdt_resource *r = of->kn->parent->priv;
  1487. int ret;
  1488. /* Valid input requires a trailing newline */
  1489. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  1490. return -EINVAL;
  1491. cpus_read_lock();
  1492. mutex_lock(&rdtgroup_mutex);
  1493. rdt_last_cmd_clear();
  1494. buf[nbytes - 1] = '\0';
  1495. ret = mon_config_write(r, buf, QOS_L3_MBM_TOTAL_EVENT_ID);
  1496. mutex_unlock(&rdtgroup_mutex);
  1497. cpus_read_unlock();
  1498. return ret ?: nbytes;
  1499. }
  1500. static ssize_t mbm_local_bytes_config_write(struct kernfs_open_file *of,
  1501. char *buf, size_t nbytes,
  1502. loff_t off)
  1503. {
  1504. struct rdt_resource *r = of->kn->parent->priv;
  1505. int ret;
  1506. /* Valid input requires a trailing newline */
  1507. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  1508. return -EINVAL;
  1509. cpus_read_lock();
  1510. mutex_lock(&rdtgroup_mutex);
  1511. rdt_last_cmd_clear();
  1512. buf[nbytes - 1] = '\0';
  1513. ret = mon_config_write(r, buf, QOS_L3_MBM_LOCAL_EVENT_ID);
  1514. mutex_unlock(&rdtgroup_mutex);
  1515. cpus_read_unlock();
  1516. return ret ?: nbytes;
  1517. }
  1518. /* rdtgroup information files for one cache resource. */
  1519. static struct rftype res_common_files[] = {
  1520. {
  1521. .name = "last_cmd_status",
  1522. .mode = 0444,
  1523. .kf_ops = &rdtgroup_kf_single_ops,
  1524. .seq_show = rdt_last_cmd_status_show,
  1525. .fflags = RFTYPE_TOP_INFO,
  1526. },
  1527. {
  1528. .name = "num_closids",
  1529. .mode = 0444,
  1530. .kf_ops = &rdtgroup_kf_single_ops,
  1531. .seq_show = rdt_num_closids_show,
  1532. .fflags = RFTYPE_CTRL_INFO,
  1533. },
  1534. {
  1535. .name = "mon_features",
  1536. .mode = 0444,
  1537. .kf_ops = &rdtgroup_kf_single_ops,
  1538. .seq_show = rdt_mon_features_show,
  1539. .fflags = RFTYPE_MON_INFO,
  1540. },
  1541. {
  1542. .name = "num_rmids",
  1543. .mode = 0444,
  1544. .kf_ops = &rdtgroup_kf_single_ops,
  1545. .seq_show = rdt_num_rmids_show,
  1546. .fflags = RFTYPE_MON_INFO,
  1547. },
  1548. {
  1549. .name = "cbm_mask",
  1550. .mode = 0444,
  1551. .kf_ops = &rdtgroup_kf_single_ops,
  1552. .seq_show = rdt_default_ctrl_show,
  1553. .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
  1554. },
  1555. {
  1556. .name = "min_cbm_bits",
  1557. .mode = 0444,
  1558. .kf_ops = &rdtgroup_kf_single_ops,
  1559. .seq_show = rdt_min_cbm_bits_show,
  1560. .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
  1561. },
  1562. {
  1563. .name = "shareable_bits",
  1564. .mode = 0444,
  1565. .kf_ops = &rdtgroup_kf_single_ops,
  1566. .seq_show = rdt_shareable_bits_show,
  1567. .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
  1568. },
  1569. {
  1570. .name = "bit_usage",
  1571. .mode = 0444,
  1572. .kf_ops = &rdtgroup_kf_single_ops,
  1573. .seq_show = rdt_bit_usage_show,
  1574. .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
  1575. },
  1576. {
  1577. .name = "min_bandwidth",
  1578. .mode = 0444,
  1579. .kf_ops = &rdtgroup_kf_single_ops,
  1580. .seq_show = rdt_min_bw_show,
  1581. .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB,
  1582. },
  1583. {
  1584. .name = "bandwidth_gran",
  1585. .mode = 0444,
  1586. .kf_ops = &rdtgroup_kf_single_ops,
  1587. .seq_show = rdt_bw_gran_show,
  1588. .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB,
  1589. },
  1590. {
  1591. .name = "delay_linear",
  1592. .mode = 0444,
  1593. .kf_ops = &rdtgroup_kf_single_ops,
  1594. .seq_show = rdt_delay_linear_show,
  1595. .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB,
  1596. },
  1597. /*
  1598. * Platform specific which (if any) capabilities are provided by
  1599. * thread_throttle_mode. Defer "fflags" initialization to platform
  1600. * discovery.
  1601. */
  1602. {
  1603. .name = "thread_throttle_mode",
  1604. .mode = 0444,
  1605. .kf_ops = &rdtgroup_kf_single_ops,
  1606. .seq_show = rdt_thread_throttle_mode_show,
  1607. },
  1608. {
  1609. .name = "max_threshold_occupancy",
  1610. .mode = 0644,
  1611. .kf_ops = &rdtgroup_kf_single_ops,
  1612. .write = max_threshold_occ_write,
  1613. .seq_show = max_threshold_occ_show,
  1614. .fflags = RFTYPE_MON_INFO | RFTYPE_RES_CACHE,
  1615. },
  1616. {
  1617. .name = "mbm_total_bytes_config",
  1618. .mode = 0644,
  1619. .kf_ops = &rdtgroup_kf_single_ops,
  1620. .seq_show = mbm_total_bytes_config_show,
  1621. .write = mbm_total_bytes_config_write,
  1622. },
  1623. {
  1624. .name = "mbm_local_bytes_config",
  1625. .mode = 0644,
  1626. .kf_ops = &rdtgroup_kf_single_ops,
  1627. .seq_show = mbm_local_bytes_config_show,
  1628. .write = mbm_local_bytes_config_write,
  1629. },
  1630. {
  1631. .name = "cpus",
  1632. .mode = 0644,
  1633. .kf_ops = &rdtgroup_kf_single_ops,
  1634. .write = rdtgroup_cpus_write,
  1635. .seq_show = rdtgroup_cpus_show,
  1636. .fflags = RFTYPE_BASE,
  1637. },
  1638. {
  1639. .name = "cpus_list",
  1640. .mode = 0644,
  1641. .kf_ops = &rdtgroup_kf_single_ops,
  1642. .write = rdtgroup_cpus_write,
  1643. .seq_show = rdtgroup_cpus_show,
  1644. .flags = RFTYPE_FLAGS_CPUS_LIST,
  1645. .fflags = RFTYPE_BASE,
  1646. },
  1647. {
  1648. .name = "tasks",
  1649. .mode = 0644,
  1650. .kf_ops = &rdtgroup_kf_single_ops,
  1651. .write = rdtgroup_tasks_write,
  1652. .seq_show = rdtgroup_tasks_show,
  1653. .fflags = RFTYPE_BASE,
  1654. },
  1655. {
  1656. .name = "mon_hw_id",
  1657. .mode = 0444,
  1658. .kf_ops = &rdtgroup_kf_single_ops,
  1659. .seq_show = rdtgroup_rmid_show,
  1660. .fflags = RFTYPE_MON_BASE | RFTYPE_DEBUG,
  1661. },
  1662. {
  1663. .name = "schemata",
  1664. .mode = 0644,
  1665. .kf_ops = &rdtgroup_kf_single_ops,
  1666. .write = rdtgroup_schemata_write,
  1667. .seq_show = rdtgroup_schemata_show,
  1668. .fflags = RFTYPE_CTRL_BASE,
  1669. },
  1670. {
  1671. .name = "mode",
  1672. .mode = 0644,
  1673. .kf_ops = &rdtgroup_kf_single_ops,
  1674. .write = rdtgroup_mode_write,
  1675. .seq_show = rdtgroup_mode_show,
  1676. .fflags = RFTYPE_CTRL_BASE,
  1677. },
  1678. {
  1679. .name = "size",
  1680. .mode = 0444,
  1681. .kf_ops = &rdtgroup_kf_single_ops,
  1682. .seq_show = rdtgroup_size_show,
  1683. .fflags = RFTYPE_CTRL_BASE,
  1684. },
  1685. {
  1686. .name = "sparse_masks",
  1687. .mode = 0444,
  1688. .kf_ops = &rdtgroup_kf_single_ops,
  1689. .seq_show = rdt_has_sparse_bitmasks_show,
  1690. .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
  1691. },
  1692. {
  1693. .name = "ctrl_hw_id",
  1694. .mode = 0444,
  1695. .kf_ops = &rdtgroup_kf_single_ops,
  1696. .seq_show = rdtgroup_closid_show,
  1697. .fflags = RFTYPE_CTRL_BASE | RFTYPE_DEBUG,
  1698. },
  1699. };
  1700. static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
  1701. {
  1702. struct rftype *rfts, *rft;
  1703. int ret, len;
  1704. rfts = res_common_files;
  1705. len = ARRAY_SIZE(res_common_files);
  1706. lockdep_assert_held(&rdtgroup_mutex);
  1707. if (resctrl_debug)
  1708. fflags |= RFTYPE_DEBUG;
  1709. for (rft = rfts; rft < rfts + len; rft++) {
  1710. if (rft->fflags && ((fflags & rft->fflags) == rft->fflags)) {
  1711. ret = rdtgroup_add_file(kn, rft);
  1712. if (ret)
  1713. goto error;
  1714. }
  1715. }
  1716. return 0;
  1717. error:
  1718. pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
  1719. while (--rft >= rfts) {
  1720. if ((fflags & rft->fflags) == rft->fflags)
  1721. kernfs_remove_by_name(kn, rft->name);
  1722. }
  1723. return ret;
  1724. }
  1725. static struct rftype *rdtgroup_get_rftype_by_name(const char *name)
  1726. {
  1727. struct rftype *rfts, *rft;
  1728. int len;
  1729. rfts = res_common_files;
  1730. len = ARRAY_SIZE(res_common_files);
  1731. for (rft = rfts; rft < rfts + len; rft++) {
  1732. if (!strcmp(rft->name, name))
  1733. return rft;
  1734. }
  1735. return NULL;
  1736. }
  1737. void __init thread_throttle_mode_init(void)
  1738. {
  1739. struct rftype *rft;
  1740. rft = rdtgroup_get_rftype_by_name("thread_throttle_mode");
  1741. if (!rft)
  1742. return;
  1743. rft->fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB;
  1744. }
  1745. void __init mbm_config_rftype_init(const char *config)
  1746. {
  1747. struct rftype *rft;
  1748. rft = rdtgroup_get_rftype_by_name(config);
  1749. if (rft)
  1750. rft->fflags = RFTYPE_MON_INFO | RFTYPE_RES_CACHE;
  1751. }
  1752. /**
  1753. * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file
  1754. * @r: The resource group with which the file is associated.
  1755. * @name: Name of the file
  1756. *
  1757. * The permissions of named resctrl file, directory, or link are modified
  1758. * to not allow read, write, or execute by any user.
  1759. *
  1760. * WARNING: This function is intended to communicate to the user that the
  1761. * resctrl file has been locked down - that it is not relevant to the
  1762. * particular state the system finds itself in. It should not be relied
  1763. * on to protect from user access because after the file's permissions
  1764. * are restricted the user can still change the permissions using chmod
  1765. * from the command line.
  1766. *
  1767. * Return: 0 on success, <0 on failure.
  1768. */
  1769. int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name)
  1770. {
  1771. struct iattr iattr = {.ia_valid = ATTR_MODE,};
  1772. struct kernfs_node *kn;
  1773. int ret = 0;
  1774. kn = kernfs_find_and_get_ns(r->kn, name, NULL);
  1775. if (!kn)
  1776. return -ENOENT;
  1777. switch (kernfs_type(kn)) {
  1778. case KERNFS_DIR:
  1779. iattr.ia_mode = S_IFDIR;
  1780. break;
  1781. case KERNFS_FILE:
  1782. iattr.ia_mode = S_IFREG;
  1783. break;
  1784. case KERNFS_LINK:
  1785. iattr.ia_mode = S_IFLNK;
  1786. break;
  1787. }
  1788. ret = kernfs_setattr(kn, &iattr);
  1789. kernfs_put(kn);
  1790. return ret;
  1791. }
  1792. /**
  1793. * rdtgroup_kn_mode_restore - Restore user access to named resctrl file
  1794. * @r: The resource group with which the file is associated.
  1795. * @name: Name of the file
  1796. * @mask: Mask of permissions that should be restored
  1797. *
  1798. * Restore the permissions of the named file. If @name is a directory the
  1799. * permissions of its parent will be used.
  1800. *
  1801. * Return: 0 on success, <0 on failure.
  1802. */
  1803. int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
  1804. umode_t mask)
  1805. {
  1806. struct iattr iattr = {.ia_valid = ATTR_MODE,};
  1807. struct kernfs_node *kn, *parent;
  1808. struct rftype *rfts, *rft;
  1809. int ret, len;
  1810. rfts = res_common_files;
  1811. len = ARRAY_SIZE(res_common_files);
  1812. for (rft = rfts; rft < rfts + len; rft++) {
  1813. if (!strcmp(rft->name, name))
  1814. iattr.ia_mode = rft->mode & mask;
  1815. }
  1816. kn = kernfs_find_and_get_ns(r->kn, name, NULL);
  1817. if (!kn)
  1818. return -ENOENT;
  1819. switch (kernfs_type(kn)) {
  1820. case KERNFS_DIR:
  1821. parent = kernfs_get_parent(kn);
  1822. if (parent) {
  1823. iattr.ia_mode |= parent->mode;
  1824. kernfs_put(parent);
  1825. }
  1826. iattr.ia_mode |= S_IFDIR;
  1827. break;
  1828. case KERNFS_FILE:
  1829. iattr.ia_mode |= S_IFREG;
  1830. break;
  1831. case KERNFS_LINK:
  1832. iattr.ia_mode |= S_IFLNK;
  1833. break;
  1834. }
  1835. ret = kernfs_setattr(kn, &iattr);
  1836. kernfs_put(kn);
  1837. return ret;
  1838. }
  1839. static int rdtgroup_mkdir_info_resdir(void *priv, char *name,
  1840. unsigned long fflags)
  1841. {
  1842. struct kernfs_node *kn_subdir;
  1843. int ret;
  1844. kn_subdir = kernfs_create_dir(kn_info, name,
  1845. kn_info->mode, priv);
  1846. if (IS_ERR(kn_subdir))
  1847. return PTR_ERR(kn_subdir);
  1848. ret = rdtgroup_kn_set_ugid(kn_subdir);
  1849. if (ret)
  1850. return ret;
  1851. ret = rdtgroup_add_files(kn_subdir, fflags);
  1852. if (!ret)
  1853. kernfs_activate(kn_subdir);
  1854. return ret;
  1855. }
  1856. static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
  1857. {
  1858. struct resctrl_schema *s;
  1859. struct rdt_resource *r;
  1860. unsigned long fflags;
  1861. char name[32];
  1862. int ret;
  1863. /* create the directory */
  1864. kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
  1865. if (IS_ERR(kn_info))
  1866. return PTR_ERR(kn_info);
  1867. ret = rdtgroup_add_files(kn_info, RFTYPE_TOP_INFO);
  1868. if (ret)
  1869. goto out_destroy;
  1870. /* loop over enabled controls, these are all alloc_capable */
  1871. list_for_each_entry(s, &resctrl_schema_all, list) {
  1872. r = s->res;
  1873. fflags = r->fflags | RFTYPE_CTRL_INFO;
  1874. ret = rdtgroup_mkdir_info_resdir(s, s->name, fflags);
  1875. if (ret)
  1876. goto out_destroy;
  1877. }
  1878. for_each_mon_capable_rdt_resource(r) {
  1879. fflags = r->fflags | RFTYPE_MON_INFO;
  1880. sprintf(name, "%s_MON", r->name);
  1881. ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
  1882. if (ret)
  1883. goto out_destroy;
  1884. }
  1885. ret = rdtgroup_kn_set_ugid(kn_info);
  1886. if (ret)
  1887. goto out_destroy;
  1888. kernfs_activate(kn_info);
  1889. return 0;
  1890. out_destroy:
  1891. kernfs_remove(kn_info);
  1892. return ret;
  1893. }
  1894. static int
  1895. mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
  1896. char *name, struct kernfs_node **dest_kn)
  1897. {
  1898. struct kernfs_node *kn;
  1899. int ret;
  1900. /* create the directory */
  1901. kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
  1902. if (IS_ERR(kn))
  1903. return PTR_ERR(kn);
  1904. if (dest_kn)
  1905. *dest_kn = kn;
  1906. ret = rdtgroup_kn_set_ugid(kn);
  1907. if (ret)
  1908. goto out_destroy;
  1909. kernfs_activate(kn);
  1910. return 0;
  1911. out_destroy:
  1912. kernfs_remove(kn);
  1913. return ret;
  1914. }
  1915. static void l3_qos_cfg_update(void *arg)
  1916. {
  1917. bool *enable = arg;
  1918. wrmsrl(MSR_IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
  1919. }
  1920. static void l2_qos_cfg_update(void *arg)
  1921. {
  1922. bool *enable = arg;
  1923. wrmsrl(MSR_IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
  1924. }
  1925. static inline bool is_mba_linear(void)
  1926. {
  1927. return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.delay_linear;
  1928. }
  1929. static int set_cache_qos_cfg(int level, bool enable)
  1930. {
  1931. void (*update)(void *arg);
  1932. struct rdt_ctrl_domain *d;
  1933. struct rdt_resource *r_l;
  1934. cpumask_var_t cpu_mask;
  1935. int cpu;
  1936. /* Walking r->domains, ensure it can't race with cpuhp */
  1937. lockdep_assert_cpus_held();
  1938. if (level == RDT_RESOURCE_L3)
  1939. update = l3_qos_cfg_update;
  1940. else if (level == RDT_RESOURCE_L2)
  1941. update = l2_qos_cfg_update;
  1942. else
  1943. return -EINVAL;
  1944. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  1945. return -ENOMEM;
  1946. r_l = &rdt_resources_all[level].r_resctrl;
  1947. list_for_each_entry(d, &r_l->ctrl_domains, hdr.list) {
  1948. if (r_l->cache.arch_has_per_cpu_cfg)
  1949. /* Pick all the CPUs in the domain instance */
  1950. for_each_cpu(cpu, &d->hdr.cpu_mask)
  1951. cpumask_set_cpu(cpu, cpu_mask);
  1952. else
  1953. /* Pick one CPU from each domain instance to update MSR */
  1954. cpumask_set_cpu(cpumask_any(&d->hdr.cpu_mask), cpu_mask);
  1955. }
  1956. /* Update QOS_CFG MSR on all the CPUs in cpu_mask */
  1957. on_each_cpu_mask(cpu_mask, update, &enable, 1);
  1958. free_cpumask_var(cpu_mask);
  1959. return 0;
  1960. }
  1961. /* Restore the qos cfg state when a domain comes online */
  1962. void rdt_domain_reconfigure_cdp(struct rdt_resource *r)
  1963. {
  1964. struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
  1965. if (!r->cdp_capable)
  1966. return;
  1967. if (r->rid == RDT_RESOURCE_L2)
  1968. l2_qos_cfg_update(&hw_res->cdp_enabled);
  1969. if (r->rid == RDT_RESOURCE_L3)
  1970. l3_qos_cfg_update(&hw_res->cdp_enabled);
  1971. }
  1972. static int mba_sc_domain_allocate(struct rdt_resource *r, struct rdt_ctrl_domain *d)
  1973. {
  1974. u32 num_closid = resctrl_arch_get_num_closid(r);
  1975. int cpu = cpumask_any(&d->hdr.cpu_mask);
  1976. int i;
  1977. d->mbps_val = kcalloc_node(num_closid, sizeof(*d->mbps_val),
  1978. GFP_KERNEL, cpu_to_node(cpu));
  1979. if (!d->mbps_val)
  1980. return -ENOMEM;
  1981. for (i = 0; i < num_closid; i++)
  1982. d->mbps_val[i] = MBA_MAX_MBPS;
  1983. return 0;
  1984. }
  1985. static void mba_sc_domain_destroy(struct rdt_resource *r,
  1986. struct rdt_ctrl_domain *d)
  1987. {
  1988. kfree(d->mbps_val);
  1989. d->mbps_val = NULL;
  1990. }
  1991. /*
  1992. * MBA software controller is supported only if
  1993. * MBM is supported and MBA is in linear scale,
  1994. * and the MBM monitor scope is the same as MBA
  1995. * control scope.
  1996. */
  1997. static bool supports_mba_mbps(void)
  1998. {
  1999. struct rdt_resource *rmbm = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
  2000. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
  2001. return (is_mbm_local_enabled() &&
  2002. r->alloc_capable && is_mba_linear() &&
  2003. r->ctrl_scope == rmbm->mon_scope);
  2004. }
  2005. /*
  2006. * Enable or disable the MBA software controller
  2007. * which helps user specify bandwidth in MBps.
  2008. */
  2009. static int set_mba_sc(bool mba_sc)
  2010. {
  2011. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
  2012. u32 num_closid = resctrl_arch_get_num_closid(r);
  2013. struct rdt_ctrl_domain *d;
  2014. int i;
  2015. if (!supports_mba_mbps() || mba_sc == is_mba_sc(r))
  2016. return -EINVAL;
  2017. r->membw.mba_sc = mba_sc;
  2018. list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
  2019. for (i = 0; i < num_closid; i++)
  2020. d->mbps_val[i] = MBA_MAX_MBPS;
  2021. }
  2022. return 0;
  2023. }
  2024. static int cdp_enable(int level)
  2025. {
  2026. struct rdt_resource *r_l = &rdt_resources_all[level].r_resctrl;
  2027. int ret;
  2028. if (!r_l->alloc_capable)
  2029. return -EINVAL;
  2030. ret = set_cache_qos_cfg(level, true);
  2031. if (!ret)
  2032. rdt_resources_all[level].cdp_enabled = true;
  2033. return ret;
  2034. }
  2035. static void cdp_disable(int level)
  2036. {
  2037. struct rdt_hw_resource *r_hw = &rdt_resources_all[level];
  2038. if (r_hw->cdp_enabled) {
  2039. set_cache_qos_cfg(level, false);
  2040. r_hw->cdp_enabled = false;
  2041. }
  2042. }
  2043. int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable)
  2044. {
  2045. struct rdt_hw_resource *hw_res = &rdt_resources_all[l];
  2046. if (!hw_res->r_resctrl.cdp_capable)
  2047. return -EINVAL;
  2048. if (enable)
  2049. return cdp_enable(l);
  2050. cdp_disable(l);
  2051. return 0;
  2052. }
  2053. /*
  2054. * We don't allow rdtgroup directories to be created anywhere
  2055. * except the root directory. Thus when looking for the rdtgroup
  2056. * structure for a kernfs node we are either looking at a directory,
  2057. * in which case the rdtgroup structure is pointed at by the "priv"
  2058. * field, otherwise we have a file, and need only look to the parent
  2059. * to find the rdtgroup.
  2060. */
  2061. static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
  2062. {
  2063. if (kernfs_type(kn) == KERNFS_DIR) {
  2064. /*
  2065. * All the resource directories use "kn->priv"
  2066. * to point to the "struct rdtgroup" for the
  2067. * resource. "info" and its subdirectories don't
  2068. * have rdtgroup structures, so return NULL here.
  2069. */
  2070. if (kn == kn_info || kn->parent == kn_info)
  2071. return NULL;
  2072. else
  2073. return kn->priv;
  2074. } else {
  2075. return kn->parent->priv;
  2076. }
  2077. }
  2078. static void rdtgroup_kn_get(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
  2079. {
  2080. atomic_inc(&rdtgrp->waitcount);
  2081. kernfs_break_active_protection(kn);
  2082. }
  2083. static void rdtgroup_kn_put(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
  2084. {
  2085. if (atomic_dec_and_test(&rdtgrp->waitcount) &&
  2086. (rdtgrp->flags & RDT_DELETED)) {
  2087. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
  2088. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
  2089. rdtgroup_pseudo_lock_remove(rdtgrp);
  2090. kernfs_unbreak_active_protection(kn);
  2091. rdtgroup_remove(rdtgrp);
  2092. } else {
  2093. kernfs_unbreak_active_protection(kn);
  2094. }
  2095. }
  2096. struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
  2097. {
  2098. struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
  2099. if (!rdtgrp)
  2100. return NULL;
  2101. rdtgroup_kn_get(rdtgrp, kn);
  2102. cpus_read_lock();
  2103. mutex_lock(&rdtgroup_mutex);
  2104. /* Was this group deleted while we waited? */
  2105. if (rdtgrp->flags & RDT_DELETED)
  2106. return NULL;
  2107. return rdtgrp;
  2108. }
  2109. void rdtgroup_kn_unlock(struct kernfs_node *kn)
  2110. {
  2111. struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
  2112. if (!rdtgrp)
  2113. return;
  2114. mutex_unlock(&rdtgroup_mutex);
  2115. cpus_read_unlock();
  2116. rdtgroup_kn_put(rdtgrp, kn);
  2117. }
  2118. static int mkdir_mondata_all(struct kernfs_node *parent_kn,
  2119. struct rdtgroup *prgrp,
  2120. struct kernfs_node **mon_data_kn);
  2121. static void rdt_disable_ctx(void)
  2122. {
  2123. resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false);
  2124. resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false);
  2125. set_mba_sc(false);
  2126. resctrl_debug = false;
  2127. }
  2128. static int rdt_enable_ctx(struct rdt_fs_context *ctx)
  2129. {
  2130. int ret = 0;
  2131. if (ctx->enable_cdpl2) {
  2132. ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, true);
  2133. if (ret)
  2134. goto out_done;
  2135. }
  2136. if (ctx->enable_cdpl3) {
  2137. ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, true);
  2138. if (ret)
  2139. goto out_cdpl2;
  2140. }
  2141. if (ctx->enable_mba_mbps) {
  2142. ret = set_mba_sc(true);
  2143. if (ret)
  2144. goto out_cdpl3;
  2145. }
  2146. if (ctx->enable_debug)
  2147. resctrl_debug = true;
  2148. return 0;
  2149. out_cdpl3:
  2150. resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false);
  2151. out_cdpl2:
  2152. resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false);
  2153. out_done:
  2154. return ret;
  2155. }
  2156. static int schemata_list_add(struct rdt_resource *r, enum resctrl_conf_type type)
  2157. {
  2158. struct resctrl_schema *s;
  2159. const char *suffix = "";
  2160. int ret, cl;
  2161. s = kzalloc(sizeof(*s), GFP_KERNEL);
  2162. if (!s)
  2163. return -ENOMEM;
  2164. s->res = r;
  2165. s->num_closid = resctrl_arch_get_num_closid(r);
  2166. if (resctrl_arch_get_cdp_enabled(r->rid))
  2167. s->num_closid /= 2;
  2168. s->conf_type = type;
  2169. switch (type) {
  2170. case CDP_CODE:
  2171. suffix = "CODE";
  2172. break;
  2173. case CDP_DATA:
  2174. suffix = "DATA";
  2175. break;
  2176. case CDP_NONE:
  2177. suffix = "";
  2178. break;
  2179. }
  2180. ret = snprintf(s->name, sizeof(s->name), "%s%s", r->name, suffix);
  2181. if (ret >= sizeof(s->name)) {
  2182. kfree(s);
  2183. return -EINVAL;
  2184. }
  2185. cl = strlen(s->name);
  2186. /*
  2187. * If CDP is supported by this resource, but not enabled,
  2188. * include the suffix. This ensures the tabular format of the
  2189. * schemata file does not change between mounts of the filesystem.
  2190. */
  2191. if (r->cdp_capable && !resctrl_arch_get_cdp_enabled(r->rid))
  2192. cl += 4;
  2193. if (cl > max_name_width)
  2194. max_name_width = cl;
  2195. INIT_LIST_HEAD(&s->list);
  2196. list_add(&s->list, &resctrl_schema_all);
  2197. return 0;
  2198. }
  2199. static int schemata_list_create(void)
  2200. {
  2201. struct rdt_resource *r;
  2202. int ret = 0;
  2203. for_each_alloc_capable_rdt_resource(r) {
  2204. if (resctrl_arch_get_cdp_enabled(r->rid)) {
  2205. ret = schemata_list_add(r, CDP_CODE);
  2206. if (ret)
  2207. break;
  2208. ret = schemata_list_add(r, CDP_DATA);
  2209. } else {
  2210. ret = schemata_list_add(r, CDP_NONE);
  2211. }
  2212. if (ret)
  2213. break;
  2214. }
  2215. return ret;
  2216. }
  2217. static void schemata_list_destroy(void)
  2218. {
  2219. struct resctrl_schema *s, *tmp;
  2220. list_for_each_entry_safe(s, tmp, &resctrl_schema_all, list) {
  2221. list_del(&s->list);
  2222. kfree(s);
  2223. }
  2224. }
  2225. static int rdt_get_tree(struct fs_context *fc)
  2226. {
  2227. struct rdt_fs_context *ctx = rdt_fc2context(fc);
  2228. unsigned long flags = RFTYPE_CTRL_BASE;
  2229. struct rdt_mon_domain *dom;
  2230. struct rdt_resource *r;
  2231. int ret;
  2232. cpus_read_lock();
  2233. mutex_lock(&rdtgroup_mutex);
  2234. /*
  2235. * resctrl file system can only be mounted once.
  2236. */
  2237. if (resctrl_mounted) {
  2238. ret = -EBUSY;
  2239. goto out;
  2240. }
  2241. ret = rdtgroup_setup_root(ctx);
  2242. if (ret)
  2243. goto out;
  2244. ret = rdt_enable_ctx(ctx);
  2245. if (ret)
  2246. goto out_root;
  2247. ret = schemata_list_create();
  2248. if (ret) {
  2249. schemata_list_destroy();
  2250. goto out_ctx;
  2251. }
  2252. closid_init();
  2253. if (resctrl_arch_mon_capable())
  2254. flags |= RFTYPE_MON;
  2255. ret = rdtgroup_add_files(rdtgroup_default.kn, flags);
  2256. if (ret)
  2257. goto out_schemata_free;
  2258. kernfs_activate(rdtgroup_default.kn);
  2259. ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
  2260. if (ret < 0)
  2261. goto out_schemata_free;
  2262. if (resctrl_arch_mon_capable()) {
  2263. ret = mongroup_create_dir(rdtgroup_default.kn,
  2264. &rdtgroup_default, "mon_groups",
  2265. &kn_mongrp);
  2266. if (ret < 0)
  2267. goto out_info;
  2268. ret = mkdir_mondata_all(rdtgroup_default.kn,
  2269. &rdtgroup_default, &kn_mondata);
  2270. if (ret < 0)
  2271. goto out_mongrp;
  2272. rdtgroup_default.mon.mon_data_kn = kn_mondata;
  2273. }
  2274. ret = rdt_pseudo_lock_init();
  2275. if (ret)
  2276. goto out_mondata;
  2277. ret = kernfs_get_tree(fc);
  2278. if (ret < 0)
  2279. goto out_psl;
  2280. if (resctrl_arch_alloc_capable())
  2281. resctrl_arch_enable_alloc();
  2282. if (resctrl_arch_mon_capable())
  2283. resctrl_arch_enable_mon();
  2284. if (resctrl_arch_alloc_capable() || resctrl_arch_mon_capable())
  2285. resctrl_mounted = true;
  2286. if (is_mbm_enabled()) {
  2287. r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
  2288. list_for_each_entry(dom, &r->mon_domains, hdr.list)
  2289. mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL,
  2290. RESCTRL_PICK_ANY_CPU);
  2291. }
  2292. goto out;
  2293. out_psl:
  2294. rdt_pseudo_lock_release();
  2295. out_mondata:
  2296. if (resctrl_arch_mon_capable())
  2297. kernfs_remove(kn_mondata);
  2298. out_mongrp:
  2299. if (resctrl_arch_mon_capable())
  2300. kernfs_remove(kn_mongrp);
  2301. out_info:
  2302. kernfs_remove(kn_info);
  2303. out_schemata_free:
  2304. schemata_list_destroy();
  2305. out_ctx:
  2306. rdt_disable_ctx();
  2307. out_root:
  2308. rdtgroup_destroy_root();
  2309. out:
  2310. rdt_last_cmd_clear();
  2311. mutex_unlock(&rdtgroup_mutex);
  2312. cpus_read_unlock();
  2313. return ret;
  2314. }
  2315. enum rdt_param {
  2316. Opt_cdp,
  2317. Opt_cdpl2,
  2318. Opt_mba_mbps,
  2319. Opt_debug,
  2320. nr__rdt_params
  2321. };
  2322. static const struct fs_parameter_spec rdt_fs_parameters[] = {
  2323. fsparam_flag("cdp", Opt_cdp),
  2324. fsparam_flag("cdpl2", Opt_cdpl2),
  2325. fsparam_flag("mba_MBps", Opt_mba_mbps),
  2326. fsparam_flag("debug", Opt_debug),
  2327. {}
  2328. };
  2329. static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param)
  2330. {
  2331. struct rdt_fs_context *ctx = rdt_fc2context(fc);
  2332. struct fs_parse_result result;
  2333. const char *msg;
  2334. int opt;
  2335. opt = fs_parse(fc, rdt_fs_parameters, param, &result);
  2336. if (opt < 0)
  2337. return opt;
  2338. switch (opt) {
  2339. case Opt_cdp:
  2340. ctx->enable_cdpl3 = true;
  2341. return 0;
  2342. case Opt_cdpl2:
  2343. ctx->enable_cdpl2 = true;
  2344. return 0;
  2345. case Opt_mba_mbps:
  2346. msg = "mba_MBps requires local MBM and linear scale MBA at L3 scope";
  2347. if (!supports_mba_mbps())
  2348. return invalfc(fc, msg);
  2349. ctx->enable_mba_mbps = true;
  2350. return 0;
  2351. case Opt_debug:
  2352. ctx->enable_debug = true;
  2353. return 0;
  2354. }
  2355. return -EINVAL;
  2356. }
  2357. static void rdt_fs_context_free(struct fs_context *fc)
  2358. {
  2359. struct rdt_fs_context *ctx = rdt_fc2context(fc);
  2360. kernfs_free_fs_context(fc);
  2361. kfree(ctx);
  2362. }
  2363. static const struct fs_context_operations rdt_fs_context_ops = {
  2364. .free = rdt_fs_context_free,
  2365. .parse_param = rdt_parse_param,
  2366. .get_tree = rdt_get_tree,
  2367. };
  2368. static int rdt_init_fs_context(struct fs_context *fc)
  2369. {
  2370. struct rdt_fs_context *ctx;
  2371. ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL);
  2372. if (!ctx)
  2373. return -ENOMEM;
  2374. ctx->kfc.magic = RDTGROUP_SUPER_MAGIC;
  2375. fc->fs_private = &ctx->kfc;
  2376. fc->ops = &rdt_fs_context_ops;
  2377. put_user_ns(fc->user_ns);
  2378. fc->user_ns = get_user_ns(&init_user_ns);
  2379. fc->global = true;
  2380. return 0;
  2381. }
  2382. static int reset_all_ctrls(struct rdt_resource *r)
  2383. {
  2384. struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
  2385. struct rdt_hw_ctrl_domain *hw_dom;
  2386. struct msr_param msr_param;
  2387. struct rdt_ctrl_domain *d;
  2388. int i;
  2389. /* Walking r->domains, ensure it can't race with cpuhp */
  2390. lockdep_assert_cpus_held();
  2391. msr_param.res = r;
  2392. msr_param.low = 0;
  2393. msr_param.high = hw_res->num_closid;
  2394. /*
  2395. * Disable resource control for this resource by setting all
  2396. * CBMs in all ctrl_domains to the maximum mask value. Pick one CPU
  2397. * from each domain to update the MSRs below.
  2398. */
  2399. list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
  2400. hw_dom = resctrl_to_arch_ctrl_dom(d);
  2401. for (i = 0; i < hw_res->num_closid; i++)
  2402. hw_dom->ctrl_val[i] = r->default_ctrl;
  2403. msr_param.dom = d;
  2404. smp_call_function_any(&d->hdr.cpu_mask, rdt_ctrl_update, &msr_param, 1);
  2405. }
  2406. return 0;
  2407. }
  2408. /*
  2409. * Move tasks from one to the other group. If @from is NULL, then all tasks
  2410. * in the systems are moved unconditionally (used for teardown).
  2411. *
  2412. * If @mask is not NULL the cpus on which moved tasks are running are set
  2413. * in that mask so the update smp function call is restricted to affected
  2414. * cpus.
  2415. */
  2416. static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
  2417. struct cpumask *mask)
  2418. {
  2419. struct task_struct *p, *t;
  2420. read_lock(&tasklist_lock);
  2421. for_each_process_thread(p, t) {
  2422. if (!from || is_closid_match(t, from) ||
  2423. is_rmid_match(t, from)) {
  2424. resctrl_arch_set_closid_rmid(t, to->closid,
  2425. to->mon.rmid);
  2426. /*
  2427. * Order the closid/rmid stores above before the loads
  2428. * in task_curr(). This pairs with the full barrier
  2429. * between the rq->curr update and resctrl_sched_in()
  2430. * during context switch.
  2431. */
  2432. smp_mb();
  2433. /*
  2434. * If the task is on a CPU, set the CPU in the mask.
  2435. * The detection is inaccurate as tasks might move or
  2436. * schedule before the smp function call takes place.
  2437. * In such a case the function call is pointless, but
  2438. * there is no other side effect.
  2439. */
  2440. if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t))
  2441. cpumask_set_cpu(task_cpu(t), mask);
  2442. }
  2443. }
  2444. read_unlock(&tasklist_lock);
  2445. }
  2446. static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
  2447. {
  2448. struct rdtgroup *sentry, *stmp;
  2449. struct list_head *head;
  2450. head = &rdtgrp->mon.crdtgrp_list;
  2451. list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
  2452. free_rmid(sentry->closid, sentry->mon.rmid);
  2453. list_del(&sentry->mon.crdtgrp_list);
  2454. if (atomic_read(&sentry->waitcount) != 0)
  2455. sentry->flags = RDT_DELETED;
  2456. else
  2457. rdtgroup_remove(sentry);
  2458. }
  2459. }
  2460. /*
  2461. * Forcibly remove all of subdirectories under root.
  2462. */
  2463. static void rmdir_all_sub(void)
  2464. {
  2465. struct rdtgroup *rdtgrp, *tmp;
  2466. /* Move all tasks to the default resource group */
  2467. rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
  2468. list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
  2469. /* Free any child rmids */
  2470. free_all_child_rdtgrp(rdtgrp);
  2471. /* Remove each rdtgroup other than root */
  2472. if (rdtgrp == &rdtgroup_default)
  2473. continue;
  2474. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
  2475. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
  2476. rdtgroup_pseudo_lock_remove(rdtgrp);
  2477. /*
  2478. * Give any CPUs back to the default group. We cannot copy
  2479. * cpu_online_mask because a CPU might have executed the
  2480. * offline callback already, but is still marked online.
  2481. */
  2482. cpumask_or(&rdtgroup_default.cpu_mask,
  2483. &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
  2484. free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
  2485. kernfs_remove(rdtgrp->kn);
  2486. list_del(&rdtgrp->rdtgroup_list);
  2487. if (atomic_read(&rdtgrp->waitcount) != 0)
  2488. rdtgrp->flags = RDT_DELETED;
  2489. else
  2490. rdtgroup_remove(rdtgrp);
  2491. }
  2492. /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
  2493. update_closid_rmid(cpu_online_mask, &rdtgroup_default);
  2494. kernfs_remove(kn_info);
  2495. kernfs_remove(kn_mongrp);
  2496. kernfs_remove(kn_mondata);
  2497. }
  2498. static void rdt_kill_sb(struct super_block *sb)
  2499. {
  2500. struct rdt_resource *r;
  2501. cpus_read_lock();
  2502. mutex_lock(&rdtgroup_mutex);
  2503. rdt_disable_ctx();
  2504. /*Put everything back to default values. */
  2505. for_each_alloc_capable_rdt_resource(r)
  2506. reset_all_ctrls(r);
  2507. rmdir_all_sub();
  2508. rdt_pseudo_lock_release();
  2509. rdtgroup_default.mode = RDT_MODE_SHAREABLE;
  2510. schemata_list_destroy();
  2511. rdtgroup_destroy_root();
  2512. if (resctrl_arch_alloc_capable())
  2513. resctrl_arch_disable_alloc();
  2514. if (resctrl_arch_mon_capable())
  2515. resctrl_arch_disable_mon();
  2516. resctrl_mounted = false;
  2517. kernfs_kill_sb(sb);
  2518. mutex_unlock(&rdtgroup_mutex);
  2519. cpus_read_unlock();
  2520. }
  2521. static struct file_system_type rdt_fs_type = {
  2522. .name = "resctrl",
  2523. .init_fs_context = rdt_init_fs_context,
  2524. .parameters = rdt_fs_parameters,
  2525. .kill_sb = rdt_kill_sb,
  2526. };
  2527. static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
  2528. void *priv)
  2529. {
  2530. struct kernfs_node *kn;
  2531. int ret = 0;
  2532. kn = __kernfs_create_file(parent_kn, name, 0444,
  2533. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
  2534. &kf_mondata_ops, priv, NULL, NULL);
  2535. if (IS_ERR(kn))
  2536. return PTR_ERR(kn);
  2537. ret = rdtgroup_kn_set_ugid(kn);
  2538. if (ret) {
  2539. kernfs_remove(kn);
  2540. return ret;
  2541. }
  2542. return ret;
  2543. }
  2544. static void mon_rmdir_one_subdir(struct kernfs_node *pkn, char *name, char *subname)
  2545. {
  2546. struct kernfs_node *kn;
  2547. kn = kernfs_find_and_get(pkn, name);
  2548. if (!kn)
  2549. return;
  2550. kernfs_put(kn);
  2551. if (kn->dir.subdirs <= 1)
  2552. kernfs_remove(kn);
  2553. else
  2554. kernfs_remove_by_name(kn, subname);
  2555. }
  2556. /*
  2557. * Remove all subdirectories of mon_data of ctrl_mon groups
  2558. * and monitor groups for the given domain.
  2559. * Remove files and directories containing "sum" of domain data
  2560. * when last domain being summed is removed.
  2561. */
  2562. static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
  2563. struct rdt_mon_domain *d)
  2564. {
  2565. struct rdtgroup *prgrp, *crgrp;
  2566. char subname[32];
  2567. bool snc_mode;
  2568. char name[32];
  2569. snc_mode = r->mon_scope == RESCTRL_L3_NODE;
  2570. sprintf(name, "mon_%s_%02d", r->name, snc_mode ? d->ci->id : d->hdr.id);
  2571. if (snc_mode)
  2572. sprintf(subname, "mon_sub_%s_%02d", r->name, d->hdr.id);
  2573. list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
  2574. mon_rmdir_one_subdir(prgrp->mon.mon_data_kn, name, subname);
  2575. list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
  2576. mon_rmdir_one_subdir(crgrp->mon.mon_data_kn, name, subname);
  2577. }
  2578. }
  2579. static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d,
  2580. struct rdt_resource *r, struct rdtgroup *prgrp,
  2581. bool do_sum)
  2582. {
  2583. struct rmid_read rr = {0};
  2584. union mon_data_bits priv;
  2585. struct mon_evt *mevt;
  2586. int ret;
  2587. if (WARN_ON(list_empty(&r->evt_list)))
  2588. return -EPERM;
  2589. priv.u.rid = r->rid;
  2590. priv.u.domid = do_sum ? d->ci->id : d->hdr.id;
  2591. priv.u.sum = do_sum;
  2592. list_for_each_entry(mevt, &r->evt_list, list) {
  2593. priv.u.evtid = mevt->evtid;
  2594. ret = mon_addfile(kn, mevt->name, priv.priv);
  2595. if (ret)
  2596. return ret;
  2597. if (!do_sum && is_mbm_event(mevt->evtid))
  2598. mon_event_read(&rr, r, d, prgrp, &d->hdr.cpu_mask, mevt->evtid, true);
  2599. }
  2600. return 0;
  2601. }
  2602. static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
  2603. struct rdt_mon_domain *d,
  2604. struct rdt_resource *r, struct rdtgroup *prgrp)
  2605. {
  2606. struct kernfs_node *kn, *ckn;
  2607. char name[32];
  2608. bool snc_mode;
  2609. int ret = 0;
  2610. lockdep_assert_held(&rdtgroup_mutex);
  2611. snc_mode = r->mon_scope == RESCTRL_L3_NODE;
  2612. sprintf(name, "mon_%s_%02d", r->name, snc_mode ? d->ci->id : d->hdr.id);
  2613. kn = kernfs_find_and_get(parent_kn, name);
  2614. if (kn) {
  2615. /*
  2616. * rdtgroup_mutex will prevent this directory from being
  2617. * removed. No need to keep this hold.
  2618. */
  2619. kernfs_put(kn);
  2620. } else {
  2621. kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
  2622. if (IS_ERR(kn))
  2623. return PTR_ERR(kn);
  2624. ret = rdtgroup_kn_set_ugid(kn);
  2625. if (ret)
  2626. goto out_destroy;
  2627. ret = mon_add_all_files(kn, d, r, prgrp, snc_mode);
  2628. if (ret)
  2629. goto out_destroy;
  2630. }
  2631. if (snc_mode) {
  2632. sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
  2633. ckn = kernfs_create_dir(kn, name, parent_kn->mode, prgrp);
  2634. if (IS_ERR(ckn)) {
  2635. ret = -EINVAL;
  2636. goto out_destroy;
  2637. }
  2638. ret = rdtgroup_kn_set_ugid(ckn);
  2639. if (ret)
  2640. goto out_destroy;
  2641. ret = mon_add_all_files(ckn, d, r, prgrp, false);
  2642. if (ret)
  2643. goto out_destroy;
  2644. }
  2645. kernfs_activate(kn);
  2646. return 0;
  2647. out_destroy:
  2648. kernfs_remove(kn);
  2649. return ret;
  2650. }
  2651. /*
  2652. * Add all subdirectories of mon_data for "ctrl_mon" groups
  2653. * and "monitor" groups with given domain id.
  2654. */
  2655. static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
  2656. struct rdt_mon_domain *d)
  2657. {
  2658. struct kernfs_node *parent_kn;
  2659. struct rdtgroup *prgrp, *crgrp;
  2660. struct list_head *head;
  2661. list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
  2662. parent_kn = prgrp->mon.mon_data_kn;
  2663. mkdir_mondata_subdir(parent_kn, d, r, prgrp);
  2664. head = &prgrp->mon.crdtgrp_list;
  2665. list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
  2666. parent_kn = crgrp->mon.mon_data_kn;
  2667. mkdir_mondata_subdir(parent_kn, d, r, crgrp);
  2668. }
  2669. }
  2670. }
  2671. static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
  2672. struct rdt_resource *r,
  2673. struct rdtgroup *prgrp)
  2674. {
  2675. struct rdt_mon_domain *dom;
  2676. int ret;
  2677. /* Walking r->domains, ensure it can't race with cpuhp */
  2678. lockdep_assert_cpus_held();
  2679. list_for_each_entry(dom, &r->mon_domains, hdr.list) {
  2680. ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
  2681. if (ret)
  2682. return ret;
  2683. }
  2684. return 0;
  2685. }
  2686. /*
  2687. * This creates a directory mon_data which contains the monitored data.
  2688. *
  2689. * mon_data has one directory for each domain which are named
  2690. * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
  2691. * with L3 domain looks as below:
  2692. * ./mon_data:
  2693. * mon_L3_00
  2694. * mon_L3_01
  2695. * mon_L3_02
  2696. * ...
  2697. *
  2698. * Each domain directory has one file per event:
  2699. * ./mon_L3_00/:
  2700. * llc_occupancy
  2701. *
  2702. */
  2703. static int mkdir_mondata_all(struct kernfs_node *parent_kn,
  2704. struct rdtgroup *prgrp,
  2705. struct kernfs_node **dest_kn)
  2706. {
  2707. struct rdt_resource *r;
  2708. struct kernfs_node *kn;
  2709. int ret;
  2710. /*
  2711. * Create the mon_data directory first.
  2712. */
  2713. ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn);
  2714. if (ret)
  2715. return ret;
  2716. if (dest_kn)
  2717. *dest_kn = kn;
  2718. /*
  2719. * Create the subdirectories for each domain. Note that all events
  2720. * in a domain like L3 are grouped into a resource whose domain is L3
  2721. */
  2722. for_each_mon_capable_rdt_resource(r) {
  2723. ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
  2724. if (ret)
  2725. goto out_destroy;
  2726. }
  2727. return 0;
  2728. out_destroy:
  2729. kernfs_remove(kn);
  2730. return ret;
  2731. }
  2732. /**
  2733. * cbm_ensure_valid - Enforce validity on provided CBM
  2734. * @_val: Candidate CBM
  2735. * @r: RDT resource to which the CBM belongs
  2736. *
  2737. * The provided CBM represents all cache portions available for use. This
  2738. * may be represented by a bitmap that does not consist of contiguous ones
  2739. * and thus be an invalid CBM.
  2740. * Here the provided CBM is forced to be a valid CBM by only considering
  2741. * the first set of contiguous bits as valid and clearing all bits.
  2742. * The intention here is to provide a valid default CBM with which a new
  2743. * resource group is initialized. The user can follow this with a
  2744. * modification to the CBM if the default does not satisfy the
  2745. * requirements.
  2746. */
  2747. static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r)
  2748. {
  2749. unsigned int cbm_len = r->cache.cbm_len;
  2750. unsigned long first_bit, zero_bit;
  2751. unsigned long val = _val;
  2752. if (!val)
  2753. return 0;
  2754. first_bit = find_first_bit(&val, cbm_len);
  2755. zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
  2756. /* Clear any remaining bits to ensure contiguous region */
  2757. bitmap_clear(&val, zero_bit, cbm_len - zero_bit);
  2758. return (u32)val;
  2759. }
  2760. /*
  2761. * Initialize cache resources per RDT domain
  2762. *
  2763. * Set the RDT domain up to start off with all usable allocations. That is,
  2764. * all shareable and unused bits. All-zero CBM is invalid.
  2765. */
  2766. static int __init_one_rdt_domain(struct rdt_ctrl_domain *d, struct resctrl_schema *s,
  2767. u32 closid)
  2768. {
  2769. enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
  2770. enum resctrl_conf_type t = s->conf_type;
  2771. struct resctrl_staged_config *cfg;
  2772. struct rdt_resource *r = s->res;
  2773. u32 used_b = 0, unused_b = 0;
  2774. unsigned long tmp_cbm;
  2775. enum rdtgrp_mode mode;
  2776. u32 peer_ctl, ctrl_val;
  2777. int i;
  2778. cfg = &d->staged_config[t];
  2779. cfg->have_new_ctrl = false;
  2780. cfg->new_ctrl = r->cache.shareable_bits;
  2781. used_b = r->cache.shareable_bits;
  2782. for (i = 0; i < closids_supported(); i++) {
  2783. if (closid_allocated(i) && i != closid) {
  2784. mode = rdtgroup_mode_by_closid(i);
  2785. if (mode == RDT_MODE_PSEUDO_LOCKSETUP)
  2786. /*
  2787. * ctrl values for locksetup aren't relevant
  2788. * until the schemata is written, and the mode
  2789. * becomes RDT_MODE_PSEUDO_LOCKED.
  2790. */
  2791. continue;
  2792. /*
  2793. * If CDP is active include peer domain's
  2794. * usage to ensure there is no overlap
  2795. * with an exclusive group.
  2796. */
  2797. if (resctrl_arch_get_cdp_enabled(r->rid))
  2798. peer_ctl = resctrl_arch_get_config(r, d, i,
  2799. peer_type);
  2800. else
  2801. peer_ctl = 0;
  2802. ctrl_val = resctrl_arch_get_config(r, d, i,
  2803. s->conf_type);
  2804. used_b |= ctrl_val | peer_ctl;
  2805. if (mode == RDT_MODE_SHAREABLE)
  2806. cfg->new_ctrl |= ctrl_val | peer_ctl;
  2807. }
  2808. }
  2809. if (d->plr && d->plr->cbm > 0)
  2810. used_b |= d->plr->cbm;
  2811. unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1);
  2812. unused_b &= BIT_MASK(r->cache.cbm_len) - 1;
  2813. cfg->new_ctrl |= unused_b;
  2814. /*
  2815. * Force the initial CBM to be valid, user can
  2816. * modify the CBM based on system availability.
  2817. */
  2818. cfg->new_ctrl = cbm_ensure_valid(cfg->new_ctrl, r);
  2819. /*
  2820. * Assign the u32 CBM to an unsigned long to ensure that
  2821. * bitmap_weight() does not access out-of-bound memory.
  2822. */
  2823. tmp_cbm = cfg->new_ctrl;
  2824. if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) {
  2825. rdt_last_cmd_printf("No space on %s:%d\n", s->name, d->hdr.id);
  2826. return -ENOSPC;
  2827. }
  2828. cfg->have_new_ctrl = true;
  2829. return 0;
  2830. }
  2831. /*
  2832. * Initialize cache resources with default values.
  2833. *
  2834. * A new RDT group is being created on an allocation capable (CAT)
  2835. * supporting system. Set this group up to start off with all usable
  2836. * allocations.
  2837. *
  2838. * If there are no more shareable bits available on any domain then
  2839. * the entire allocation will fail.
  2840. */
  2841. static int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid)
  2842. {
  2843. struct rdt_ctrl_domain *d;
  2844. int ret;
  2845. list_for_each_entry(d, &s->res->ctrl_domains, hdr.list) {
  2846. ret = __init_one_rdt_domain(d, s, closid);
  2847. if (ret < 0)
  2848. return ret;
  2849. }
  2850. return 0;
  2851. }
  2852. /* Initialize MBA resource with default values. */
  2853. static void rdtgroup_init_mba(struct rdt_resource *r, u32 closid)
  2854. {
  2855. struct resctrl_staged_config *cfg;
  2856. struct rdt_ctrl_domain *d;
  2857. list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
  2858. if (is_mba_sc(r)) {
  2859. d->mbps_val[closid] = MBA_MAX_MBPS;
  2860. continue;
  2861. }
  2862. cfg = &d->staged_config[CDP_NONE];
  2863. cfg->new_ctrl = r->default_ctrl;
  2864. cfg->have_new_ctrl = true;
  2865. }
  2866. }
  2867. /* Initialize the RDT group's allocations. */
  2868. static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
  2869. {
  2870. struct resctrl_schema *s;
  2871. struct rdt_resource *r;
  2872. int ret = 0;
  2873. rdt_staged_configs_clear();
  2874. list_for_each_entry(s, &resctrl_schema_all, list) {
  2875. r = s->res;
  2876. if (r->rid == RDT_RESOURCE_MBA ||
  2877. r->rid == RDT_RESOURCE_SMBA) {
  2878. rdtgroup_init_mba(r, rdtgrp->closid);
  2879. if (is_mba_sc(r))
  2880. continue;
  2881. } else {
  2882. ret = rdtgroup_init_cat(s, rdtgrp->closid);
  2883. if (ret < 0)
  2884. goto out;
  2885. }
  2886. ret = resctrl_arch_update_domains(r, rdtgrp->closid);
  2887. if (ret < 0) {
  2888. rdt_last_cmd_puts("Failed to initialize allocations\n");
  2889. goto out;
  2890. }
  2891. }
  2892. rdtgrp->mode = RDT_MODE_SHAREABLE;
  2893. out:
  2894. rdt_staged_configs_clear();
  2895. return ret;
  2896. }
  2897. static int mkdir_rdt_prepare_rmid_alloc(struct rdtgroup *rdtgrp)
  2898. {
  2899. int ret;
  2900. if (!resctrl_arch_mon_capable())
  2901. return 0;
  2902. ret = alloc_rmid(rdtgrp->closid);
  2903. if (ret < 0) {
  2904. rdt_last_cmd_puts("Out of RMIDs\n");
  2905. return ret;
  2906. }
  2907. rdtgrp->mon.rmid = ret;
  2908. ret = mkdir_mondata_all(rdtgrp->kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
  2909. if (ret) {
  2910. rdt_last_cmd_puts("kernfs subdir error\n");
  2911. free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
  2912. return ret;
  2913. }
  2914. return 0;
  2915. }
  2916. static void mkdir_rdt_prepare_rmid_free(struct rdtgroup *rgrp)
  2917. {
  2918. if (resctrl_arch_mon_capable())
  2919. free_rmid(rgrp->closid, rgrp->mon.rmid);
  2920. }
  2921. static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
  2922. const char *name, umode_t mode,
  2923. enum rdt_group_type rtype, struct rdtgroup **r)
  2924. {
  2925. struct rdtgroup *prdtgrp, *rdtgrp;
  2926. unsigned long files = 0;
  2927. struct kernfs_node *kn;
  2928. int ret;
  2929. prdtgrp = rdtgroup_kn_lock_live(parent_kn);
  2930. if (!prdtgrp) {
  2931. ret = -ENODEV;
  2932. goto out_unlock;
  2933. }
  2934. if (rtype == RDTMON_GROUP &&
  2935. (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
  2936. prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
  2937. ret = -EINVAL;
  2938. rdt_last_cmd_puts("Pseudo-locking in progress\n");
  2939. goto out_unlock;
  2940. }
  2941. /* allocate the rdtgroup. */
  2942. rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
  2943. if (!rdtgrp) {
  2944. ret = -ENOSPC;
  2945. rdt_last_cmd_puts("Kernel out of memory\n");
  2946. goto out_unlock;
  2947. }
  2948. *r = rdtgrp;
  2949. rdtgrp->mon.parent = prdtgrp;
  2950. rdtgrp->type = rtype;
  2951. INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
  2952. /* kernfs creates the directory for rdtgrp */
  2953. kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
  2954. if (IS_ERR(kn)) {
  2955. ret = PTR_ERR(kn);
  2956. rdt_last_cmd_puts("kernfs create error\n");
  2957. goto out_free_rgrp;
  2958. }
  2959. rdtgrp->kn = kn;
  2960. /*
  2961. * kernfs_remove() will drop the reference count on "kn" which
  2962. * will free it. But we still need it to stick around for the
  2963. * rdtgroup_kn_unlock(kn) call. Take one extra reference here,
  2964. * which will be dropped by kernfs_put() in rdtgroup_remove().
  2965. */
  2966. kernfs_get(kn);
  2967. ret = rdtgroup_kn_set_ugid(kn);
  2968. if (ret) {
  2969. rdt_last_cmd_puts("kernfs perm error\n");
  2970. goto out_destroy;
  2971. }
  2972. if (rtype == RDTCTRL_GROUP) {
  2973. files = RFTYPE_BASE | RFTYPE_CTRL;
  2974. if (resctrl_arch_mon_capable())
  2975. files |= RFTYPE_MON;
  2976. } else {
  2977. files = RFTYPE_BASE | RFTYPE_MON;
  2978. }
  2979. ret = rdtgroup_add_files(kn, files);
  2980. if (ret) {
  2981. rdt_last_cmd_puts("kernfs fill error\n");
  2982. goto out_destroy;
  2983. }
  2984. /*
  2985. * The caller unlocks the parent_kn upon success.
  2986. */
  2987. return 0;
  2988. out_destroy:
  2989. kernfs_put(rdtgrp->kn);
  2990. kernfs_remove(rdtgrp->kn);
  2991. out_free_rgrp:
  2992. kfree(rdtgrp);
  2993. out_unlock:
  2994. rdtgroup_kn_unlock(parent_kn);
  2995. return ret;
  2996. }
  2997. static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
  2998. {
  2999. kernfs_remove(rgrp->kn);
  3000. rdtgroup_remove(rgrp);
  3001. }
  3002. /*
  3003. * Create a monitor group under "mon_groups" directory of a control
  3004. * and monitor group(ctrl_mon). This is a resource group
  3005. * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
  3006. */
  3007. static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
  3008. const char *name, umode_t mode)
  3009. {
  3010. struct rdtgroup *rdtgrp, *prgrp;
  3011. int ret;
  3012. ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp);
  3013. if (ret)
  3014. return ret;
  3015. prgrp = rdtgrp->mon.parent;
  3016. rdtgrp->closid = prgrp->closid;
  3017. ret = mkdir_rdt_prepare_rmid_alloc(rdtgrp);
  3018. if (ret) {
  3019. mkdir_rdt_prepare_clean(rdtgrp);
  3020. goto out_unlock;
  3021. }
  3022. kernfs_activate(rdtgrp->kn);
  3023. /*
  3024. * Add the rdtgrp to the list of rdtgrps the parent
  3025. * ctrl_mon group has to track.
  3026. */
  3027. list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
  3028. out_unlock:
  3029. rdtgroup_kn_unlock(parent_kn);
  3030. return ret;
  3031. }
  3032. /*
  3033. * These are rdtgroups created under the root directory. Can be used
  3034. * to allocate and monitor resources.
  3035. */
  3036. static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
  3037. const char *name, umode_t mode)
  3038. {
  3039. struct rdtgroup *rdtgrp;
  3040. struct kernfs_node *kn;
  3041. u32 closid;
  3042. int ret;
  3043. ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp);
  3044. if (ret)
  3045. return ret;
  3046. kn = rdtgrp->kn;
  3047. ret = closid_alloc();
  3048. if (ret < 0) {
  3049. rdt_last_cmd_puts("Out of CLOSIDs\n");
  3050. goto out_common_fail;
  3051. }
  3052. closid = ret;
  3053. ret = 0;
  3054. rdtgrp->closid = closid;
  3055. ret = mkdir_rdt_prepare_rmid_alloc(rdtgrp);
  3056. if (ret)
  3057. goto out_closid_free;
  3058. kernfs_activate(rdtgrp->kn);
  3059. ret = rdtgroup_init_alloc(rdtgrp);
  3060. if (ret < 0)
  3061. goto out_rmid_free;
  3062. list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
  3063. if (resctrl_arch_mon_capable()) {
  3064. /*
  3065. * Create an empty mon_groups directory to hold the subset
  3066. * of tasks and cpus to monitor.
  3067. */
  3068. ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL);
  3069. if (ret) {
  3070. rdt_last_cmd_puts("kernfs subdir error\n");
  3071. goto out_del_list;
  3072. }
  3073. }
  3074. goto out_unlock;
  3075. out_del_list:
  3076. list_del(&rdtgrp->rdtgroup_list);
  3077. out_rmid_free:
  3078. mkdir_rdt_prepare_rmid_free(rdtgrp);
  3079. out_closid_free:
  3080. closid_free(closid);
  3081. out_common_fail:
  3082. mkdir_rdt_prepare_clean(rdtgrp);
  3083. out_unlock:
  3084. rdtgroup_kn_unlock(parent_kn);
  3085. return ret;
  3086. }
  3087. /*
  3088. * We allow creating mon groups only with in a directory called "mon_groups"
  3089. * which is present in every ctrl_mon group. Check if this is a valid
  3090. * "mon_groups" directory.
  3091. *
  3092. * 1. The directory should be named "mon_groups".
  3093. * 2. The mon group itself should "not" be named "mon_groups".
  3094. * This makes sure "mon_groups" directory always has a ctrl_mon group
  3095. * as parent.
  3096. */
  3097. static bool is_mon_groups(struct kernfs_node *kn, const char *name)
  3098. {
  3099. return (!strcmp(kn->name, "mon_groups") &&
  3100. strcmp(name, "mon_groups"));
  3101. }
  3102. static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
  3103. umode_t mode)
  3104. {
  3105. /* Do not accept '\n' to avoid unparsable situation. */
  3106. if (strchr(name, '\n'))
  3107. return -EINVAL;
  3108. /*
  3109. * If the parent directory is the root directory and RDT
  3110. * allocation is supported, add a control and monitoring
  3111. * subdirectory
  3112. */
  3113. if (resctrl_arch_alloc_capable() && parent_kn == rdtgroup_default.kn)
  3114. return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode);
  3115. /*
  3116. * If RDT monitoring is supported and the parent directory is a valid
  3117. * "mon_groups" directory, add a monitoring subdirectory.
  3118. */
  3119. if (resctrl_arch_mon_capable() && is_mon_groups(parent_kn, name))
  3120. return rdtgroup_mkdir_mon(parent_kn, name, mode);
  3121. return -EPERM;
  3122. }
  3123. static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
  3124. {
  3125. struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
  3126. int cpu;
  3127. /* Give any tasks back to the parent group */
  3128. rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
  3129. /* Update per cpu rmid of the moved CPUs first */
  3130. for_each_cpu(cpu, &rdtgrp->cpu_mask)
  3131. per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
  3132. /*
  3133. * Update the MSR on moved CPUs and CPUs which have moved
  3134. * task running on them.
  3135. */
  3136. cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
  3137. update_closid_rmid(tmpmask, NULL);
  3138. rdtgrp->flags = RDT_DELETED;
  3139. free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
  3140. /*
  3141. * Remove the rdtgrp from the parent ctrl_mon group's list
  3142. */
  3143. WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
  3144. list_del(&rdtgrp->mon.crdtgrp_list);
  3145. kernfs_remove(rdtgrp->kn);
  3146. return 0;
  3147. }
  3148. static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp)
  3149. {
  3150. rdtgrp->flags = RDT_DELETED;
  3151. list_del(&rdtgrp->rdtgroup_list);
  3152. kernfs_remove(rdtgrp->kn);
  3153. return 0;
  3154. }
  3155. static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
  3156. {
  3157. int cpu;
  3158. /* Give any tasks back to the default group */
  3159. rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
  3160. /* Give any CPUs back to the default group */
  3161. cpumask_or(&rdtgroup_default.cpu_mask,
  3162. &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
  3163. /* Update per cpu closid and rmid of the moved CPUs first */
  3164. for_each_cpu(cpu, &rdtgrp->cpu_mask) {
  3165. per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
  3166. per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
  3167. }
  3168. /*
  3169. * Update the MSR on moved CPUs and CPUs which have moved
  3170. * task running on them.
  3171. */
  3172. cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
  3173. update_closid_rmid(tmpmask, NULL);
  3174. free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
  3175. closid_free(rdtgrp->closid);
  3176. rdtgroup_ctrl_remove(rdtgrp);
  3177. /*
  3178. * Free all the child monitor group rmids.
  3179. */
  3180. free_all_child_rdtgrp(rdtgrp);
  3181. return 0;
  3182. }
  3183. static int rdtgroup_rmdir(struct kernfs_node *kn)
  3184. {
  3185. struct kernfs_node *parent_kn = kn->parent;
  3186. struct rdtgroup *rdtgrp;
  3187. cpumask_var_t tmpmask;
  3188. int ret = 0;
  3189. if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
  3190. return -ENOMEM;
  3191. rdtgrp = rdtgroup_kn_lock_live(kn);
  3192. if (!rdtgrp) {
  3193. ret = -EPERM;
  3194. goto out;
  3195. }
  3196. /*
  3197. * If the rdtgroup is a ctrl_mon group and parent directory
  3198. * is the root directory, remove the ctrl_mon group.
  3199. *
  3200. * If the rdtgroup is a mon group and parent directory
  3201. * is a valid "mon_groups" directory, remove the mon group.
  3202. */
  3203. if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn &&
  3204. rdtgrp != &rdtgroup_default) {
  3205. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
  3206. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  3207. ret = rdtgroup_ctrl_remove(rdtgrp);
  3208. } else {
  3209. ret = rdtgroup_rmdir_ctrl(rdtgrp, tmpmask);
  3210. }
  3211. } else if (rdtgrp->type == RDTMON_GROUP &&
  3212. is_mon_groups(parent_kn, kn->name)) {
  3213. ret = rdtgroup_rmdir_mon(rdtgrp, tmpmask);
  3214. } else {
  3215. ret = -EPERM;
  3216. }
  3217. out:
  3218. rdtgroup_kn_unlock(kn);
  3219. free_cpumask_var(tmpmask);
  3220. return ret;
  3221. }
  3222. /**
  3223. * mongrp_reparent() - replace parent CTRL_MON group of a MON group
  3224. * @rdtgrp: the MON group whose parent should be replaced
  3225. * @new_prdtgrp: replacement parent CTRL_MON group for @rdtgrp
  3226. * @cpus: cpumask provided by the caller for use during this call
  3227. *
  3228. * Replaces the parent CTRL_MON group for a MON group, resulting in all member
  3229. * tasks' CLOSID immediately changing to that of the new parent group.
  3230. * Monitoring data for the group is unaffected by this operation.
  3231. */
  3232. static void mongrp_reparent(struct rdtgroup *rdtgrp,
  3233. struct rdtgroup *new_prdtgrp,
  3234. cpumask_var_t cpus)
  3235. {
  3236. struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
  3237. WARN_ON(rdtgrp->type != RDTMON_GROUP);
  3238. WARN_ON(new_prdtgrp->type != RDTCTRL_GROUP);
  3239. /* Nothing to do when simply renaming a MON group. */
  3240. if (prdtgrp == new_prdtgrp)
  3241. return;
  3242. WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
  3243. list_move_tail(&rdtgrp->mon.crdtgrp_list,
  3244. &new_prdtgrp->mon.crdtgrp_list);
  3245. rdtgrp->mon.parent = new_prdtgrp;
  3246. rdtgrp->closid = new_prdtgrp->closid;
  3247. /* Propagate updated closid to all tasks in this group. */
  3248. rdt_move_group_tasks(rdtgrp, rdtgrp, cpus);
  3249. update_closid_rmid(cpus, NULL);
  3250. }
  3251. static int rdtgroup_rename(struct kernfs_node *kn,
  3252. struct kernfs_node *new_parent, const char *new_name)
  3253. {
  3254. struct rdtgroup *new_prdtgrp;
  3255. struct rdtgroup *rdtgrp;
  3256. cpumask_var_t tmpmask;
  3257. int ret;
  3258. rdtgrp = kernfs_to_rdtgroup(kn);
  3259. new_prdtgrp = kernfs_to_rdtgroup(new_parent);
  3260. if (!rdtgrp || !new_prdtgrp)
  3261. return -ENOENT;
  3262. /* Release both kernfs active_refs before obtaining rdtgroup mutex. */
  3263. rdtgroup_kn_get(rdtgrp, kn);
  3264. rdtgroup_kn_get(new_prdtgrp, new_parent);
  3265. mutex_lock(&rdtgroup_mutex);
  3266. rdt_last_cmd_clear();
  3267. /*
  3268. * Don't allow kernfs_to_rdtgroup() to return a parent rdtgroup if
  3269. * either kernfs_node is a file.
  3270. */
  3271. if (kernfs_type(kn) != KERNFS_DIR ||
  3272. kernfs_type(new_parent) != KERNFS_DIR) {
  3273. rdt_last_cmd_puts("Source and destination must be directories");
  3274. ret = -EPERM;
  3275. goto out;
  3276. }
  3277. if ((rdtgrp->flags & RDT_DELETED) || (new_prdtgrp->flags & RDT_DELETED)) {
  3278. ret = -ENOENT;
  3279. goto out;
  3280. }
  3281. if (rdtgrp->type != RDTMON_GROUP || !kn->parent ||
  3282. !is_mon_groups(kn->parent, kn->name)) {
  3283. rdt_last_cmd_puts("Source must be a MON group\n");
  3284. ret = -EPERM;
  3285. goto out;
  3286. }
  3287. if (!is_mon_groups(new_parent, new_name)) {
  3288. rdt_last_cmd_puts("Destination must be a mon_groups subdirectory\n");
  3289. ret = -EPERM;
  3290. goto out;
  3291. }
  3292. /*
  3293. * If the MON group is monitoring CPUs, the CPUs must be assigned to the
  3294. * current parent CTRL_MON group and therefore cannot be assigned to
  3295. * the new parent, making the move illegal.
  3296. */
  3297. if (!cpumask_empty(&rdtgrp->cpu_mask) &&
  3298. rdtgrp->mon.parent != new_prdtgrp) {
  3299. rdt_last_cmd_puts("Cannot move a MON group that monitors CPUs\n");
  3300. ret = -EPERM;
  3301. goto out;
  3302. }
  3303. /*
  3304. * Allocate the cpumask for use in mongrp_reparent() to avoid the
  3305. * possibility of failing to allocate it after kernfs_rename() has
  3306. * succeeded.
  3307. */
  3308. if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) {
  3309. ret = -ENOMEM;
  3310. goto out;
  3311. }
  3312. /*
  3313. * Perform all input validation and allocations needed to ensure
  3314. * mongrp_reparent() will succeed before calling kernfs_rename(),
  3315. * otherwise it would be necessary to revert this call if
  3316. * mongrp_reparent() failed.
  3317. */
  3318. ret = kernfs_rename(kn, new_parent, new_name);
  3319. if (!ret)
  3320. mongrp_reparent(rdtgrp, new_prdtgrp, tmpmask);
  3321. free_cpumask_var(tmpmask);
  3322. out:
  3323. mutex_unlock(&rdtgroup_mutex);
  3324. rdtgroup_kn_put(rdtgrp, kn);
  3325. rdtgroup_kn_put(new_prdtgrp, new_parent);
  3326. return ret;
  3327. }
  3328. static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
  3329. {
  3330. if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3))
  3331. seq_puts(seq, ",cdp");
  3332. if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
  3333. seq_puts(seq, ",cdpl2");
  3334. if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl))
  3335. seq_puts(seq, ",mba_MBps");
  3336. if (resctrl_debug)
  3337. seq_puts(seq, ",debug");
  3338. return 0;
  3339. }
  3340. static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
  3341. .mkdir = rdtgroup_mkdir,
  3342. .rmdir = rdtgroup_rmdir,
  3343. .rename = rdtgroup_rename,
  3344. .show_options = rdtgroup_show_options,
  3345. };
  3346. static int rdtgroup_setup_root(struct rdt_fs_context *ctx)
  3347. {
  3348. rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
  3349. KERNFS_ROOT_CREATE_DEACTIVATED |
  3350. KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
  3351. &rdtgroup_default);
  3352. if (IS_ERR(rdt_root))
  3353. return PTR_ERR(rdt_root);
  3354. ctx->kfc.root = rdt_root;
  3355. rdtgroup_default.kn = kernfs_root_to_node(rdt_root);
  3356. return 0;
  3357. }
  3358. static void rdtgroup_destroy_root(void)
  3359. {
  3360. kernfs_destroy_root(rdt_root);
  3361. rdtgroup_default.kn = NULL;
  3362. }
  3363. static void __init rdtgroup_setup_default(void)
  3364. {
  3365. mutex_lock(&rdtgroup_mutex);
  3366. rdtgroup_default.closid = RESCTRL_RESERVED_CLOSID;
  3367. rdtgroup_default.mon.rmid = RESCTRL_RESERVED_RMID;
  3368. rdtgroup_default.type = RDTCTRL_GROUP;
  3369. INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
  3370. list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
  3371. mutex_unlock(&rdtgroup_mutex);
  3372. }
  3373. static void domain_destroy_mon_state(struct rdt_mon_domain *d)
  3374. {
  3375. bitmap_free(d->rmid_busy_llc);
  3376. kfree(d->mbm_total);
  3377. kfree(d->mbm_local);
  3378. }
  3379. void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d)
  3380. {
  3381. mutex_lock(&rdtgroup_mutex);
  3382. if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA)
  3383. mba_sc_domain_destroy(r, d);
  3384. mutex_unlock(&rdtgroup_mutex);
  3385. }
  3386. void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d)
  3387. {
  3388. mutex_lock(&rdtgroup_mutex);
  3389. /*
  3390. * If resctrl is mounted, remove all the
  3391. * per domain monitor data directories.
  3392. */
  3393. if (resctrl_mounted && resctrl_arch_mon_capable())
  3394. rmdir_mondata_subdir_allrdtgrp(r, d);
  3395. if (is_mbm_enabled())
  3396. cancel_delayed_work(&d->mbm_over);
  3397. if (is_llc_occupancy_enabled() && has_busy_rmid(d)) {
  3398. /*
  3399. * When a package is going down, forcefully
  3400. * decrement rmid->ebusy. There is no way to know
  3401. * that the L3 was flushed and hence may lead to
  3402. * incorrect counts in rare scenarios, but leaving
  3403. * the RMID as busy creates RMID leaks if the
  3404. * package never comes back.
  3405. */
  3406. __check_limbo(d, true);
  3407. cancel_delayed_work(&d->cqm_limbo);
  3408. }
  3409. domain_destroy_mon_state(d);
  3410. mutex_unlock(&rdtgroup_mutex);
  3411. }
  3412. static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_mon_domain *d)
  3413. {
  3414. u32 idx_limit = resctrl_arch_system_num_rmid_idx();
  3415. size_t tsize;
  3416. if (is_llc_occupancy_enabled()) {
  3417. d->rmid_busy_llc = bitmap_zalloc(idx_limit, GFP_KERNEL);
  3418. if (!d->rmid_busy_llc)
  3419. return -ENOMEM;
  3420. }
  3421. if (is_mbm_total_enabled()) {
  3422. tsize = sizeof(*d->mbm_total);
  3423. d->mbm_total = kcalloc(idx_limit, tsize, GFP_KERNEL);
  3424. if (!d->mbm_total) {
  3425. bitmap_free(d->rmid_busy_llc);
  3426. return -ENOMEM;
  3427. }
  3428. }
  3429. if (is_mbm_local_enabled()) {
  3430. tsize = sizeof(*d->mbm_local);
  3431. d->mbm_local = kcalloc(idx_limit, tsize, GFP_KERNEL);
  3432. if (!d->mbm_local) {
  3433. bitmap_free(d->rmid_busy_llc);
  3434. kfree(d->mbm_total);
  3435. return -ENOMEM;
  3436. }
  3437. }
  3438. return 0;
  3439. }
  3440. int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d)
  3441. {
  3442. int err = 0;
  3443. mutex_lock(&rdtgroup_mutex);
  3444. if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA) {
  3445. /* RDT_RESOURCE_MBA is never mon_capable */
  3446. err = mba_sc_domain_allocate(r, d);
  3447. }
  3448. mutex_unlock(&rdtgroup_mutex);
  3449. return err;
  3450. }
  3451. int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d)
  3452. {
  3453. int err;
  3454. mutex_lock(&rdtgroup_mutex);
  3455. err = domain_setup_mon_state(r, d);
  3456. if (err)
  3457. goto out_unlock;
  3458. if (is_mbm_enabled()) {
  3459. INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow);
  3460. mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL,
  3461. RESCTRL_PICK_ANY_CPU);
  3462. }
  3463. if (is_llc_occupancy_enabled())
  3464. INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo);
  3465. /*
  3466. * If the filesystem is not mounted then only the default resource group
  3467. * exists. Creation of its directories is deferred until mount time
  3468. * by rdt_get_tree() calling mkdir_mondata_all().
  3469. * If resctrl is mounted, add per domain monitor data directories.
  3470. */
  3471. if (resctrl_mounted && resctrl_arch_mon_capable())
  3472. mkdir_mondata_subdir_allrdtgrp(r, d);
  3473. out_unlock:
  3474. mutex_unlock(&rdtgroup_mutex);
  3475. return err;
  3476. }
  3477. void resctrl_online_cpu(unsigned int cpu)
  3478. {
  3479. mutex_lock(&rdtgroup_mutex);
  3480. /* The CPU is set in default rdtgroup after online. */
  3481. cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
  3482. mutex_unlock(&rdtgroup_mutex);
  3483. }
  3484. static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)
  3485. {
  3486. struct rdtgroup *cr;
  3487. list_for_each_entry(cr, &r->mon.crdtgrp_list, mon.crdtgrp_list) {
  3488. if (cpumask_test_and_clear_cpu(cpu, &cr->cpu_mask))
  3489. break;
  3490. }
  3491. }
  3492. void resctrl_offline_cpu(unsigned int cpu)
  3493. {
  3494. struct rdt_resource *l3 = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
  3495. struct rdt_mon_domain *d;
  3496. struct rdtgroup *rdtgrp;
  3497. mutex_lock(&rdtgroup_mutex);
  3498. list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
  3499. if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask)) {
  3500. clear_childcpus(rdtgrp, cpu);
  3501. break;
  3502. }
  3503. }
  3504. if (!l3->mon_capable)
  3505. goto out_unlock;
  3506. d = get_mon_domain_from_cpu(cpu, l3);
  3507. if (d) {
  3508. if (is_mbm_enabled() && cpu == d->mbm_work_cpu) {
  3509. cancel_delayed_work(&d->mbm_over);
  3510. mbm_setup_overflow_handler(d, 0, cpu);
  3511. }
  3512. if (is_llc_occupancy_enabled() && cpu == d->cqm_work_cpu &&
  3513. has_busy_rmid(d)) {
  3514. cancel_delayed_work(&d->cqm_limbo);
  3515. cqm_setup_limbo_handler(d, 0, cpu);
  3516. }
  3517. }
  3518. out_unlock:
  3519. mutex_unlock(&rdtgroup_mutex);
  3520. }
  3521. /*
  3522. * rdtgroup_init - rdtgroup initialization
  3523. *
  3524. * Setup resctrl file system including set up root, create mount point,
  3525. * register rdtgroup filesystem, and initialize files under root directory.
  3526. *
  3527. * Return: 0 on success or -errno
  3528. */
  3529. int __init rdtgroup_init(void)
  3530. {
  3531. int ret = 0;
  3532. seq_buf_init(&last_cmd_status, last_cmd_status_buf,
  3533. sizeof(last_cmd_status_buf));
  3534. rdtgroup_setup_default();
  3535. ret = sysfs_create_mount_point(fs_kobj, "resctrl");
  3536. if (ret)
  3537. return ret;
  3538. ret = register_filesystem(&rdt_fs_type);
  3539. if (ret)
  3540. goto cleanup_mountpoint;
  3541. /*
  3542. * Adding the resctrl debugfs directory here may not be ideal since
  3543. * it would let the resctrl debugfs directory appear on the debugfs
  3544. * filesystem before the resctrl filesystem is mounted.
  3545. * It may also be ok since that would enable debugging of RDT before
  3546. * resctrl is mounted.
  3547. * The reason why the debugfs directory is created here and not in
  3548. * rdt_get_tree() is because rdt_get_tree() takes rdtgroup_mutex and
  3549. * during the debugfs directory creation also &sb->s_type->i_mutex_key
  3550. * (the lockdep class of inode->i_rwsem). Other filesystem
  3551. * interactions (eg. SyS_getdents) have the lock ordering:
  3552. * &sb->s_type->i_mutex_key --> &mm->mmap_lock
  3553. * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex
  3554. * is taken, thus creating dependency:
  3555. * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause
  3556. * issues considering the other two lock dependencies.
  3557. * By creating the debugfs directory here we avoid a dependency
  3558. * that may cause deadlock (even though file operations cannot
  3559. * occur until the filesystem is mounted, but I do not know how to
  3560. * tell lockdep that).
  3561. */
  3562. debugfs_resctrl = debugfs_create_dir("resctrl", NULL);
  3563. return 0;
  3564. cleanup_mountpoint:
  3565. sysfs_remove_mount_point(fs_kobj, "resctrl");
  3566. return ret;
  3567. }
  3568. void __exit rdtgroup_exit(void)
  3569. {
  3570. debugfs_remove_recursive(debugfs_resctrl);
  3571. unregister_filesystem(&rdt_fs_type);
  3572. sysfs_remove_mount_point(fs_kobj, "resctrl");
  3573. }