internal.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _FS_RESCTRL_INTERNAL_H
  3. #define _FS_RESCTRL_INTERNAL_H
  4. #include <linux/resctrl.h>
  5. #include <linux/kernfs.h>
  6. #include <linux/fs_context.h>
  7. #include <linux/tick.h>
  8. #define CQM_LIMBOCHECK_INTERVAL 1000
  9. /**
  10. * cpumask_any_housekeeping() - Choose any CPU in @mask, preferring those that
  11. * aren't marked nohz_full
  12. * @mask: The mask to pick a CPU from.
  13. * @exclude_cpu:The CPU to avoid picking.
  14. *
  15. * Returns a CPU from @mask, but not @exclude_cpu. If there are housekeeping
  16. * CPUs that don't use nohz_full, these are preferred. Pass
  17. * RESCTRL_PICK_ANY_CPU to avoid excluding any CPUs.
  18. *
  19. * When a CPU is excluded, returns >= nr_cpu_ids if no CPUs are available.
  20. */
  21. static inline unsigned int
  22. cpumask_any_housekeeping(const struct cpumask *mask, int exclude_cpu)
  23. {
  24. unsigned int cpu;
  25. /* Try to find a CPU that isn't nohz_full to use in preference */
  26. if (tick_nohz_full_enabled()) {
  27. cpu = cpumask_any_andnot_but(mask, tick_nohz_full_mask, exclude_cpu);
  28. if (cpu < nr_cpu_ids)
  29. return cpu;
  30. }
  31. return cpumask_any_but(mask, exclude_cpu);
  32. }
  33. struct rdt_fs_context {
  34. struct kernfs_fs_context kfc;
  35. bool enable_cdpl2;
  36. bool enable_cdpl3;
  37. bool enable_mba_mbps;
  38. bool enable_debug;
  39. };
  40. static inline struct rdt_fs_context *rdt_fc2context(struct fs_context *fc)
  41. {
  42. struct kernfs_fs_context *kfc = fc->fs_private;
  43. return container_of(kfc, struct rdt_fs_context, kfc);
  44. }
  45. /**
  46. * struct mon_evt - Properties of a monitor event
  47. * @evtid: event id
  48. * @rid: resource id for this event
  49. * @name: name of the event
  50. * @evt_cfg: Event configuration value that represents the
  51. * memory transactions (e.g., READS_TO_LOCAL_MEM,
  52. * READS_TO_REMOTE_MEM) being tracked by @evtid.
  53. * Only valid if @evtid is an MBM event.
  54. * @configurable: true if the event is configurable
  55. * @any_cpu: true if the event can be read from any CPU
  56. * @is_floating_point: event values are displayed in floating point format
  57. * @binary_bits: number of fixed-point binary bits from architecture,
  58. * only valid if @is_floating_point is true
  59. * @enabled: true if the event is enabled
  60. * @arch_priv: Architecture private data for this event.
  61. * The @arch_priv provided by the architecture via
  62. * resctrl_enable_mon_event().
  63. */
  64. struct mon_evt {
  65. enum resctrl_event_id evtid;
  66. enum resctrl_res_level rid;
  67. char *name;
  68. u32 evt_cfg;
  69. bool configurable;
  70. bool any_cpu;
  71. bool is_floating_point;
  72. unsigned int binary_bits;
  73. bool enabled;
  74. void *arch_priv;
  75. };
  76. extern struct mon_evt mon_event_all[QOS_NUM_EVENTS];
  77. #define for_each_mon_event(mevt) for (mevt = &mon_event_all[QOS_FIRST_EVENT]; \
  78. mevt < &mon_event_all[QOS_NUM_EVENTS]; mevt++)
  79. /* Limit for mon_evt::binary_bits */
  80. #define MAX_BINARY_BITS 27
  81. /**
  82. * struct mon_data - Monitoring details for each event file.
  83. * @list: Member of the global @mon_data_kn_priv_list list.
  84. * @rid: Resource id associated with the event file.
  85. * @evt: Event structure associated with the event file.
  86. * @sum: Set for RDT_RESOURCE_L3 when event must be summed
  87. * across multiple domains.
  88. * @domid: When @sum is zero this is the domain to which
  89. * the event file belongs. When @sum is one this
  90. * is the id of the L3 cache that all domains to be
  91. * summed share.
  92. *
  93. * Pointed to by the kernfs kn->priv field of monitoring event files.
  94. * Readers and writers must hold rdtgroup_mutex.
  95. */
  96. struct mon_data {
  97. struct list_head list;
  98. enum resctrl_res_level rid;
  99. struct mon_evt *evt;
  100. int domid;
  101. bool sum;
  102. };
  103. /**
  104. * struct rmid_read - Data passed across smp_call*() to read event count.
  105. * @rgrp: Resource group for which the counter is being read. If it is a parent
  106. * resource group then its event count is summed with the count from all
  107. * its child resource groups.
  108. * @r: Resource describing the properties of the event being read.
  109. * @hdr: Header of domain that the counter should be read from. If NULL then
  110. * sum all domains in @r sharing L3 @ci.id
  111. * @evt: Which monitor event to read.
  112. * @first: Initialize MBM counter when true.
  113. * @ci: Cacheinfo for L3. Only set when @hdr is NULL. Used when summing
  114. * domains.
  115. * @is_mbm_cntr: true if "mbm_event" counter assignment mode is enabled and it
  116. * is an MBM event.
  117. * @err: Error encountered when reading counter.
  118. * @val: Returned value of event counter. If @rgrp is a parent resource
  119. * group, @val includes the sum of event counts from its child
  120. * resource groups. If @hdr is NULL, @val includes the sum of all
  121. * domains in @r sharing @ci.id, (summed across child resource groups
  122. * if @rgrp is a parent resource group).
  123. * @arch_mon_ctx: Hardware monitor allocated for this read request (MPAM only).
  124. */
  125. struct rmid_read {
  126. struct rdtgroup *rgrp;
  127. struct rdt_resource *r;
  128. struct rdt_domain_hdr *hdr;
  129. struct mon_evt *evt;
  130. bool first;
  131. struct cacheinfo *ci;
  132. bool is_mbm_cntr;
  133. int err;
  134. u64 val;
  135. void *arch_mon_ctx;
  136. };
  137. extern struct list_head resctrl_schema_all;
  138. extern bool resctrl_mounted;
  139. enum rdt_group_type {
  140. RDTCTRL_GROUP = 0,
  141. RDTMON_GROUP,
  142. RDT_NUM_GROUP,
  143. };
  144. /**
  145. * enum rdtgrp_mode - Mode of a RDT resource group
  146. * @RDT_MODE_SHAREABLE: This resource group allows sharing of its allocations
  147. * @RDT_MODE_EXCLUSIVE: No sharing of this resource group's allocations allowed
  148. * @RDT_MODE_PSEUDO_LOCKSETUP: Resource group will be used for Pseudo-Locking
  149. * @RDT_MODE_PSEUDO_LOCKED: No sharing of this resource group's allocations
  150. * allowed AND the allocations are Cache Pseudo-Locked
  151. * @RDT_NUM_MODES: Total number of modes
  152. *
  153. * The mode of a resource group enables control over the allowed overlap
  154. * between allocations associated with different resource groups (classes
  155. * of service). User is able to modify the mode of a resource group by
  156. * writing to the "mode" resctrl file associated with the resource group.
  157. *
  158. * The "shareable", "exclusive", and "pseudo-locksetup" modes are set by
  159. * writing the appropriate text to the "mode" file. A resource group enters
  160. * "pseudo-locked" mode after the schemata is written while the resource
  161. * group is in "pseudo-locksetup" mode.
  162. */
  163. enum rdtgrp_mode {
  164. RDT_MODE_SHAREABLE = 0,
  165. RDT_MODE_EXCLUSIVE,
  166. RDT_MODE_PSEUDO_LOCKSETUP,
  167. RDT_MODE_PSEUDO_LOCKED,
  168. /* Must be last */
  169. RDT_NUM_MODES,
  170. };
  171. /**
  172. * struct mongroup - store mon group's data in resctrl fs.
  173. * @mon_data_kn: kernfs node for the mon_data directory
  174. * @parent: parent rdtgrp
  175. * @crdtgrp_list: child rdtgroup node list
  176. * @rmid: rmid for this rdtgroup
  177. */
  178. struct mongroup {
  179. struct kernfs_node *mon_data_kn;
  180. struct rdtgroup *parent;
  181. struct list_head crdtgrp_list;
  182. u32 rmid;
  183. };
  184. /**
  185. * struct rdtgroup - store rdtgroup's data in resctrl file system.
  186. * @kn: kernfs node
  187. * @rdtgroup_list: linked list for all rdtgroups
  188. * @closid: closid for this rdtgroup
  189. * @cpu_mask: CPUs assigned to this rdtgroup
  190. * @flags: status bits
  191. * @waitcount: how many cpus expect to find this
  192. * group when they acquire rdtgroup_mutex
  193. * @type: indicates type of this rdtgroup - either
  194. * monitor only or ctrl_mon group
  195. * @mon: mongroup related data
  196. * @mode: mode of resource group
  197. * @mba_mbps_event: input monitoring event id when mba_sc is enabled
  198. * @plr: pseudo-locked region
  199. */
  200. struct rdtgroup {
  201. struct kernfs_node *kn;
  202. struct list_head rdtgroup_list;
  203. u32 closid;
  204. struct cpumask cpu_mask;
  205. int flags;
  206. atomic_t waitcount;
  207. enum rdt_group_type type;
  208. struct mongroup mon;
  209. enum rdtgrp_mode mode;
  210. enum resctrl_event_id mba_mbps_event;
  211. struct pseudo_lock_region *plr;
  212. };
  213. /* rdtgroup.flags */
  214. #define RDT_DELETED 1
  215. /* rftype.flags */
  216. #define RFTYPE_FLAGS_CPUS_LIST 1
  217. /*
  218. * Define the file type flags for base and info directories.
  219. */
  220. #define RFTYPE_INFO BIT(0)
  221. #define RFTYPE_BASE BIT(1)
  222. #define RFTYPE_CTRL BIT(4)
  223. #define RFTYPE_MON BIT(5)
  224. #define RFTYPE_TOP BIT(6)
  225. #define RFTYPE_RES_CACHE BIT(8)
  226. #define RFTYPE_RES_MB BIT(9)
  227. #define RFTYPE_DEBUG BIT(10)
  228. #define RFTYPE_ASSIGN_CONFIG BIT(11)
  229. #define RFTYPE_RES_PERF_PKG BIT(12)
  230. #define RFTYPE_CTRL_INFO (RFTYPE_INFO | RFTYPE_CTRL)
  231. #define RFTYPE_MON_INFO (RFTYPE_INFO | RFTYPE_MON)
  232. #define RFTYPE_TOP_INFO (RFTYPE_INFO | RFTYPE_TOP)
  233. #define RFTYPE_CTRL_BASE (RFTYPE_BASE | RFTYPE_CTRL)
  234. #define RFTYPE_MON_BASE (RFTYPE_BASE | RFTYPE_MON)
  235. /* List of all resource groups */
  236. extern struct list_head rdt_all_groups;
  237. extern int max_name_width;
  238. /**
  239. * struct rftype - describe each file in the resctrl file system
  240. * @name: File name
  241. * @mode: Access mode
  242. * @kf_ops: File operations
  243. * @flags: File specific RFTYPE_FLAGS_* flags
  244. * @fflags: File specific RFTYPE_* flags
  245. * @seq_show: Show content of the file
  246. * @write: Write to the file
  247. */
  248. struct rftype {
  249. char *name;
  250. umode_t mode;
  251. const struct kernfs_ops *kf_ops;
  252. unsigned long flags;
  253. unsigned long fflags;
  254. int (*seq_show)(struct kernfs_open_file *of,
  255. struct seq_file *sf, void *v);
  256. /*
  257. * write() is the generic write callback which maps directly to
  258. * kernfs write operation and overrides all other operations.
  259. * Maximum write size is determined by ->max_write_len.
  260. */
  261. ssize_t (*write)(struct kernfs_open_file *of,
  262. char *buf, size_t nbytes, loff_t off);
  263. };
  264. /**
  265. * struct mbm_state - status for each MBM counter in each domain
  266. * @prev_bw_bytes: Previous bytes value read for bandwidth calculation
  267. * @prev_bw: The most recent bandwidth in MBps
  268. */
  269. struct mbm_state {
  270. u64 prev_bw_bytes;
  271. u32 prev_bw;
  272. };
  273. extern struct mutex rdtgroup_mutex;
  274. static inline const char *rdt_kn_name(const struct kernfs_node *kn)
  275. {
  276. return rcu_dereference_check(kn->name, lockdep_is_held(&rdtgroup_mutex));
  277. }
  278. extern struct rdtgroup rdtgroup_default;
  279. extern struct dentry *debugfs_resctrl;
  280. extern enum resctrl_event_id mba_mbps_default_event;
  281. void rdt_last_cmd_clear(void);
  282. void rdt_last_cmd_puts(const char *s);
  283. __printf(1, 2)
  284. void rdt_last_cmd_printf(const char *fmt, ...);
  285. struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn);
  286. void rdtgroup_kn_unlock(struct kernfs_node *kn);
  287. int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name);
  288. int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
  289. umode_t mask);
  290. ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
  291. char *buf, size_t nbytes, loff_t off);
  292. int rdtgroup_schemata_show(struct kernfs_open_file *of,
  293. struct seq_file *s, void *v);
  294. ssize_t rdtgroup_mba_mbps_event_write(struct kernfs_open_file *of,
  295. char *buf, size_t nbytes, loff_t off);
  296. int rdtgroup_mba_mbps_event_show(struct kernfs_open_file *of,
  297. struct seq_file *s, void *v);
  298. bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_ctrl_domain *d,
  299. unsigned long cbm, int closid, bool exclusive);
  300. unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r, struct rdt_ctrl_domain *d,
  301. unsigned long cbm);
  302. enum rdtgrp_mode rdtgroup_mode_by_closid(int closid);
  303. int rdtgroup_tasks_assigned(struct rdtgroup *r);
  304. int closids_supported(void);
  305. void closid_free(int closid);
  306. int setup_rmid_lru_list(void);
  307. void free_rmid_lru_list(void);
  308. int alloc_rmid(u32 closid);
  309. void free_rmid(u32 closid, u32 rmid);
  310. int resctrl_l3_mon_resource_init(void);
  311. void resctrl_l3_mon_resource_exit(void);
  312. void mon_event_count(void *info);
  313. int rdtgroup_mondata_show(struct seq_file *m, void *arg);
  314. void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
  315. struct rdt_domain_hdr *hdr, struct rdtgroup *rdtgrp,
  316. cpumask_t *cpumask, struct mon_evt *evt, int first);
  317. void mbm_setup_overflow_handler(struct rdt_l3_mon_domain *dom,
  318. unsigned long delay_ms,
  319. int exclude_cpu);
  320. void mbm_handle_overflow(struct work_struct *work);
  321. bool is_mba_sc(struct rdt_resource *r);
  322. void cqm_setup_limbo_handler(struct rdt_l3_mon_domain *dom, unsigned long delay_ms,
  323. int exclude_cpu);
  324. void cqm_handle_limbo(struct work_struct *work);
  325. bool has_busy_rmid(struct rdt_l3_mon_domain *d);
  326. void __check_limbo(struct rdt_l3_mon_domain *d, bool force_free);
  327. void resctrl_file_fflags_init(const char *config, unsigned long fflags);
  328. void rdt_staged_configs_clear(void);
  329. bool closid_allocated(unsigned int closid);
  330. bool closid_alloc_fixed(u32 closid);
  331. int resctrl_find_cleanest_closid(void);
  332. void *rdt_kn_parent_priv(struct kernfs_node *kn);
  333. int resctrl_mbm_assign_mode_show(struct kernfs_open_file *of, struct seq_file *s, void *v);
  334. ssize_t resctrl_mbm_assign_mode_write(struct kernfs_open_file *of, char *buf,
  335. size_t nbytes, loff_t off);
  336. void resctrl_bmec_files_show(struct rdt_resource *r, struct kernfs_node *l3_mon_kn,
  337. bool show);
  338. int resctrl_num_mbm_cntrs_show(struct kernfs_open_file *of, struct seq_file *s, void *v);
  339. int resctrl_available_mbm_cntrs_show(struct kernfs_open_file *of, struct seq_file *s,
  340. void *v);
  341. void rdtgroup_assign_cntrs(struct rdtgroup *rdtgrp);
  342. void rdtgroup_unassign_cntrs(struct rdtgroup *rdtgrp);
  343. int event_filter_show(struct kernfs_open_file *of, struct seq_file *seq, void *v);
  344. ssize_t event_filter_write(struct kernfs_open_file *of, char *buf, size_t nbytes,
  345. loff_t off);
  346. int resctrl_mbm_assign_on_mkdir_show(struct kernfs_open_file *of,
  347. struct seq_file *s, void *v);
  348. ssize_t resctrl_mbm_assign_on_mkdir_write(struct kernfs_open_file *of, char *buf,
  349. size_t nbytes, loff_t off);
  350. int mbm_L3_assignments_show(struct kernfs_open_file *of, struct seq_file *s, void *v);
  351. ssize_t mbm_L3_assignments_write(struct kernfs_open_file *of, char *buf, size_t nbytes,
  352. loff_t off);
  353. int resctrl_io_alloc_show(struct kernfs_open_file *of, struct seq_file *seq, void *v);
  354. int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid);
  355. enum resctrl_conf_type resctrl_peer_type(enum resctrl_conf_type my_type);
  356. ssize_t resctrl_io_alloc_write(struct kernfs_open_file *of, char *buf,
  357. size_t nbytes, loff_t off);
  358. const char *rdtgroup_name_by_closid(u32 closid);
  359. int resctrl_io_alloc_cbm_show(struct kernfs_open_file *of, struct seq_file *seq,
  360. void *v);
  361. ssize_t resctrl_io_alloc_cbm_write(struct kernfs_open_file *of, char *buf,
  362. size_t nbytes, loff_t off);
  363. u32 resctrl_io_alloc_closid(struct rdt_resource *r);
  364. #ifdef CONFIG_RESCTRL_FS_PSEUDO_LOCK
  365. int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp);
  366. int rdtgroup_locksetup_exit(struct rdtgroup *rdtgrp);
  367. bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_ctrl_domain *d, unsigned long cbm);
  368. bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_ctrl_domain *d);
  369. int rdt_pseudo_lock_init(void);
  370. void rdt_pseudo_lock_release(void);
  371. int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp);
  372. void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp);
  373. #else
  374. static inline int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp)
  375. {
  376. return -EOPNOTSUPP;
  377. }
  378. static inline int rdtgroup_locksetup_exit(struct rdtgroup *rdtgrp)
  379. {
  380. return -EOPNOTSUPP;
  381. }
  382. static inline bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_ctrl_domain *d, unsigned long cbm)
  383. {
  384. return false;
  385. }
  386. static inline bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_ctrl_domain *d)
  387. {
  388. return false;
  389. }
  390. static inline int rdt_pseudo_lock_init(void) { return 0; }
  391. static inline void rdt_pseudo_lock_release(void) { }
  392. static inline int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
  393. {
  394. return -EOPNOTSUPP;
  395. }
  396. static inline void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp) { }
  397. #endif /* CONFIG_RESCTRL_FS_PSEUDO_LOCK */
  398. #endif /* _FS_RESCTRL_INTERNAL_H */