Merge remote-tracking branches 'asoc/fix/ak4613', 'asoc/fix/atmel', 'asoc/fix/compres...
[sfrench/cifs-2.6.git] / arch / x86 / kernel / cpu / intel_rdt_rdtgroup.c
1 /*
2  * User interface for Resource Alloction in Resource Director Technology(RDT)
3  *
4  * Copyright (C) 2016 Intel Corporation
5  *
6  * Author: Fenghua Yu <fenghua.yu@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * More information about RDT be found in the Intel (R) x86 Architecture
18  * Software Developer Manual.
19  */
20
21 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
22
23 #include <linux/cpu.h>
24 #include <linux/fs.h>
25 #include <linux/sysfs.h>
26 #include <linux/kernfs.h>
27 #include <linux/seq_file.h>
28 #include <linux/sched/signal.h>
29 #include <linux/sched/task.h>
30 #include <linux/slab.h>
31 #include <linux/task_work.h>
32
33 #include <uapi/linux/magic.h>
34
35 #include <asm/intel_rdt.h>
36 #include <asm/intel_rdt_common.h>
37
38 DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
39 struct kernfs_root *rdt_root;
40 struct rdtgroup rdtgroup_default;
41 LIST_HEAD(rdt_all_groups);
42
43 /* Kernel fs node for "info" directory under root */
44 static struct kernfs_node *kn_info;
45
46 /*
47  * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
48  * we can keep a bitmap of free CLOSIDs in a single integer.
49  *
50  * Using a global CLOSID across all resources has some advantages and
51  * some drawbacks:
52  * + We can simply set "current->closid" to assign a task to a resource
53  *   group.
54  * + Context switch code can avoid extra memory references deciding which
55  *   CLOSID to load into the PQR_ASSOC MSR
56  * - We give up some options in configuring resource groups across multi-socket
57  *   systems.
58  * - Our choices on how to configure each resource become progressively more
59  *   limited as the number of resources grows.
60  */
61 static int closid_free_map;
62
63 static void closid_init(void)
64 {
65         struct rdt_resource *r;
66         int rdt_min_closid = 32;
67
68         /* Compute rdt_min_closid across all resources */
69         for_each_enabled_rdt_resource(r)
70                 rdt_min_closid = min(rdt_min_closid, r->num_closid);
71
72         closid_free_map = BIT_MASK(rdt_min_closid) - 1;
73
74         /* CLOSID 0 is always reserved for the default group */
75         closid_free_map &= ~1;
76 }
77
78 int closid_alloc(void)
79 {
80         int closid = ffs(closid_free_map);
81
82         if (closid == 0)
83                 return -ENOSPC;
84         closid--;
85         closid_free_map &= ~(1 << closid);
86
87         return closid;
88 }
89
90 static void closid_free(int closid)
91 {
92         closid_free_map |= 1 << closid;
93 }
94
95 /* set uid and gid of rdtgroup dirs and files to that of the creator */
96 static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
97 {
98         struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
99                                 .ia_uid = current_fsuid(),
100                                 .ia_gid = current_fsgid(), };
101
102         if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
103             gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
104                 return 0;
105
106         return kernfs_setattr(kn, &iattr);
107 }
108
109 static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
110 {
111         struct kernfs_node *kn;
112         int ret;
113
114         kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
115                                   0, rft->kf_ops, rft, NULL, NULL);
116         if (IS_ERR(kn))
117                 return PTR_ERR(kn);
118
119         ret = rdtgroup_kn_set_ugid(kn);
120         if (ret) {
121                 kernfs_remove(kn);
122                 return ret;
123         }
124
125         return 0;
126 }
127
128 static int rdtgroup_add_files(struct kernfs_node *kn, struct rftype *rfts,
129                               int len)
130 {
131         struct rftype *rft;
132         int ret;
133
134         lockdep_assert_held(&rdtgroup_mutex);
135
136         for (rft = rfts; rft < rfts + len; rft++) {
137                 ret = rdtgroup_add_file(kn, rft);
138                 if (ret)
139                         goto error;
140         }
141
142         return 0;
143 error:
144         pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
145         while (--rft >= rfts)
146                 kernfs_remove_by_name(kn, rft->name);
147         return ret;
148 }
149
150 static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
151 {
152         struct kernfs_open_file *of = m->private;
153         struct rftype *rft = of->kn->priv;
154
155         if (rft->seq_show)
156                 return rft->seq_show(of, m, arg);
157         return 0;
158 }
159
160 static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
161                                    size_t nbytes, loff_t off)
162 {
163         struct rftype *rft = of->kn->priv;
164
165         if (rft->write)
166                 return rft->write(of, buf, nbytes, off);
167
168         return -EINVAL;
169 }
170
171 static struct kernfs_ops rdtgroup_kf_single_ops = {
172         .atomic_write_len       = PAGE_SIZE,
173         .write                  = rdtgroup_file_write,
174         .seq_show               = rdtgroup_seqfile_show,
175 };
176
177 static bool is_cpu_list(struct kernfs_open_file *of)
178 {
179         struct rftype *rft = of->kn->priv;
180
181         return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
182 }
183
184 static int rdtgroup_cpus_show(struct kernfs_open_file *of,
185                               struct seq_file *s, void *v)
186 {
187         struct rdtgroup *rdtgrp;
188         int ret = 0;
189
190         rdtgrp = rdtgroup_kn_lock_live(of->kn);
191
192         if (rdtgrp) {
193                 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
194                            cpumask_pr_args(&rdtgrp->cpu_mask));
195         } else {
196                 ret = -ENOENT;
197         }
198         rdtgroup_kn_unlock(of->kn);
199
200         return ret;
201 }
202
203 /*
204  * This is safe against intel_rdt_sched_in() called from __switch_to()
205  * because __switch_to() is executed with interrupts disabled. A local call
206  * from rdt_update_closid() is proteced against __switch_to() because
207  * preemption is disabled.
208  */
209 static void rdt_update_cpu_closid(void *closid)
210 {
211         if (closid)
212                 this_cpu_write(cpu_closid, *(int *)closid);
213         /*
214          * We cannot unconditionally write the MSR because the current
215          * executing task might have its own closid selected. Just reuse
216          * the context switch code.
217          */
218         intel_rdt_sched_in();
219 }
220
221 /*
222  * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
223  *
224  * Per task closids must have been set up before calling this function.
225  *
226  * The per cpu closids are updated with the smp function call, when @closid
227  * is not NULL. If @closid is NULL then all affected percpu closids must
228  * have been set up before calling this function.
229  */
230 static void
231 rdt_update_closid(const struct cpumask *cpu_mask, int *closid)
232 {
233         int cpu = get_cpu();
234
235         if (cpumask_test_cpu(cpu, cpu_mask))
236                 rdt_update_cpu_closid(closid);
237         smp_call_function_many(cpu_mask, rdt_update_cpu_closid, closid, 1);
238         put_cpu();
239 }
240
241 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
242                                    char *buf, size_t nbytes, loff_t off)
243 {
244         cpumask_var_t tmpmask, newmask;
245         struct rdtgroup *rdtgrp, *r;
246         int ret;
247
248         if (!buf)
249                 return -EINVAL;
250
251         if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
252                 return -ENOMEM;
253         if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
254                 free_cpumask_var(tmpmask);
255                 return -ENOMEM;
256         }
257
258         rdtgrp = rdtgroup_kn_lock_live(of->kn);
259         if (!rdtgrp) {
260                 ret = -ENOENT;
261                 goto unlock;
262         }
263
264         if (is_cpu_list(of))
265                 ret = cpulist_parse(buf, newmask);
266         else
267                 ret = cpumask_parse(buf, newmask);
268
269         if (ret)
270                 goto unlock;
271
272         /* check that user didn't specify any offline cpus */
273         cpumask_andnot(tmpmask, newmask, cpu_online_mask);
274         if (cpumask_weight(tmpmask)) {
275                 ret = -EINVAL;
276                 goto unlock;
277         }
278
279         /* Check whether cpus are dropped from this group */
280         cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
281         if (cpumask_weight(tmpmask)) {
282                 /* Can't drop from default group */
283                 if (rdtgrp == &rdtgroup_default) {
284                         ret = -EINVAL;
285                         goto unlock;
286                 }
287                 /* Give any dropped cpus to rdtgroup_default */
288                 cpumask_or(&rdtgroup_default.cpu_mask,
289                            &rdtgroup_default.cpu_mask, tmpmask);
290                 rdt_update_closid(tmpmask, &rdtgroup_default.closid);
291         }
292
293         /*
294          * If we added cpus, remove them from previous group that owned them
295          * and update per-cpu closid
296          */
297         cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
298         if (cpumask_weight(tmpmask)) {
299                 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
300                         if (r == rdtgrp)
301                                 continue;
302                         cpumask_andnot(&r->cpu_mask, &r->cpu_mask, tmpmask);
303                 }
304                 rdt_update_closid(tmpmask, &rdtgrp->closid);
305         }
306
307         /* Done pushing/pulling - update this group with new mask */
308         cpumask_copy(&rdtgrp->cpu_mask, newmask);
309
310 unlock:
311         rdtgroup_kn_unlock(of->kn);
312         free_cpumask_var(tmpmask);
313         free_cpumask_var(newmask);
314
315         return ret ?: nbytes;
316 }
317
318 struct task_move_callback {
319         struct callback_head    work;
320         struct rdtgroup         *rdtgrp;
321 };
322
323 static void move_myself(struct callback_head *head)
324 {
325         struct task_move_callback *callback;
326         struct rdtgroup *rdtgrp;
327
328         callback = container_of(head, struct task_move_callback, work);
329         rdtgrp = callback->rdtgrp;
330
331         /*
332          * If resource group was deleted before this task work callback
333          * was invoked, then assign the task to root group and free the
334          * resource group.
335          */
336         if (atomic_dec_and_test(&rdtgrp->waitcount) &&
337             (rdtgrp->flags & RDT_DELETED)) {
338                 current->closid = 0;
339                 kfree(rdtgrp);
340         }
341
342         preempt_disable();
343         /* update PQR_ASSOC MSR to make resource group go into effect */
344         intel_rdt_sched_in();
345         preempt_enable();
346
347         kfree(callback);
348 }
349
350 static int __rdtgroup_move_task(struct task_struct *tsk,
351                                 struct rdtgroup *rdtgrp)
352 {
353         struct task_move_callback *callback;
354         int ret;
355
356         callback = kzalloc(sizeof(*callback), GFP_KERNEL);
357         if (!callback)
358                 return -ENOMEM;
359         callback->work.func = move_myself;
360         callback->rdtgrp = rdtgrp;
361
362         /*
363          * Take a refcount, so rdtgrp cannot be freed before the
364          * callback has been invoked.
365          */
366         atomic_inc(&rdtgrp->waitcount);
367         ret = task_work_add(tsk, &callback->work, true);
368         if (ret) {
369                 /*
370                  * Task is exiting. Drop the refcount and free the callback.
371                  * No need to check the refcount as the group cannot be
372                  * deleted before the write function unlocks rdtgroup_mutex.
373                  */
374                 atomic_dec(&rdtgrp->waitcount);
375                 kfree(callback);
376         } else {
377                 tsk->closid = rdtgrp->closid;
378         }
379         return ret;
380 }
381
382 static int rdtgroup_task_write_permission(struct task_struct *task,
383                                           struct kernfs_open_file *of)
384 {
385         const struct cred *tcred = get_task_cred(task);
386         const struct cred *cred = current_cred();
387         int ret = 0;
388
389         /*
390          * Even if we're attaching all tasks in the thread group, we only
391          * need to check permissions on one of them.
392          */
393         if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
394             !uid_eq(cred->euid, tcred->uid) &&
395             !uid_eq(cred->euid, tcred->suid))
396                 ret = -EPERM;
397
398         put_cred(tcred);
399         return ret;
400 }
401
402 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
403                               struct kernfs_open_file *of)
404 {
405         struct task_struct *tsk;
406         int ret;
407
408         rcu_read_lock();
409         if (pid) {
410                 tsk = find_task_by_vpid(pid);
411                 if (!tsk) {
412                         rcu_read_unlock();
413                         return -ESRCH;
414                 }
415         } else {
416                 tsk = current;
417         }
418
419         get_task_struct(tsk);
420         rcu_read_unlock();
421
422         ret = rdtgroup_task_write_permission(tsk, of);
423         if (!ret)
424                 ret = __rdtgroup_move_task(tsk, rdtgrp);
425
426         put_task_struct(tsk);
427         return ret;
428 }
429
430 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
431                                     char *buf, size_t nbytes, loff_t off)
432 {
433         struct rdtgroup *rdtgrp;
434         int ret = 0;
435         pid_t pid;
436
437         if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
438                 return -EINVAL;
439         rdtgrp = rdtgroup_kn_lock_live(of->kn);
440
441         if (rdtgrp)
442                 ret = rdtgroup_move_task(pid, rdtgrp, of);
443         else
444                 ret = -ENOENT;
445
446         rdtgroup_kn_unlock(of->kn);
447
448         return ret ?: nbytes;
449 }
450
451 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
452 {
453         struct task_struct *p, *t;
454
455         rcu_read_lock();
456         for_each_process_thread(p, t) {
457                 if (t->closid == r->closid)
458                         seq_printf(s, "%d\n", t->pid);
459         }
460         rcu_read_unlock();
461 }
462
463 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
464                                struct seq_file *s, void *v)
465 {
466         struct rdtgroup *rdtgrp;
467         int ret = 0;
468
469         rdtgrp = rdtgroup_kn_lock_live(of->kn);
470         if (rdtgrp)
471                 show_rdt_tasks(rdtgrp, s);
472         else
473                 ret = -ENOENT;
474         rdtgroup_kn_unlock(of->kn);
475
476         return ret;
477 }
478
479 /* Files in each rdtgroup */
480 static struct rftype rdtgroup_base_files[] = {
481         {
482                 .name           = "cpus",
483                 .mode           = 0644,
484                 .kf_ops         = &rdtgroup_kf_single_ops,
485                 .write          = rdtgroup_cpus_write,
486                 .seq_show       = rdtgroup_cpus_show,
487         },
488         {
489                 .name           = "cpus_list",
490                 .mode           = 0644,
491                 .kf_ops         = &rdtgroup_kf_single_ops,
492                 .write          = rdtgroup_cpus_write,
493                 .seq_show       = rdtgroup_cpus_show,
494                 .flags          = RFTYPE_FLAGS_CPUS_LIST,
495         },
496         {
497                 .name           = "tasks",
498                 .mode           = 0644,
499                 .kf_ops         = &rdtgroup_kf_single_ops,
500                 .write          = rdtgroup_tasks_write,
501                 .seq_show       = rdtgroup_tasks_show,
502         },
503         {
504                 .name           = "schemata",
505                 .mode           = 0644,
506                 .kf_ops         = &rdtgroup_kf_single_ops,
507                 .write          = rdtgroup_schemata_write,
508                 .seq_show       = rdtgroup_schemata_show,
509         },
510 };
511
512 static int rdt_num_closids_show(struct kernfs_open_file *of,
513                                 struct seq_file *seq, void *v)
514 {
515         struct rdt_resource *r = of->kn->parent->priv;
516
517         seq_printf(seq, "%d\n", r->num_closid);
518         return 0;
519 }
520
521 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
522                              struct seq_file *seq, void *v)
523 {
524         struct rdt_resource *r = of->kn->parent->priv;
525
526         seq_printf(seq, "%x\n", r->default_ctrl);
527         return 0;
528 }
529
530 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
531                              struct seq_file *seq, void *v)
532 {
533         struct rdt_resource *r = of->kn->parent->priv;
534
535         seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
536         return 0;
537 }
538
539 static int rdt_min_bw_show(struct kernfs_open_file *of,
540                              struct seq_file *seq, void *v)
541 {
542         struct rdt_resource *r = of->kn->parent->priv;
543
544         seq_printf(seq, "%u\n", r->membw.min_bw);
545         return 0;
546 }
547
548 static int rdt_bw_gran_show(struct kernfs_open_file *of,
549                              struct seq_file *seq, void *v)
550 {
551         struct rdt_resource *r = of->kn->parent->priv;
552
553         seq_printf(seq, "%u\n", r->membw.bw_gran);
554         return 0;
555 }
556
557 static int rdt_delay_linear_show(struct kernfs_open_file *of,
558                              struct seq_file *seq, void *v)
559 {
560         struct rdt_resource *r = of->kn->parent->priv;
561
562         seq_printf(seq, "%u\n", r->membw.delay_linear);
563         return 0;
564 }
565
566 /* rdtgroup information files for one cache resource. */
567 static struct rftype res_cache_info_files[] = {
568         {
569                 .name           = "num_closids",
570                 .mode           = 0444,
571                 .kf_ops         = &rdtgroup_kf_single_ops,
572                 .seq_show       = rdt_num_closids_show,
573         },
574         {
575                 .name           = "cbm_mask",
576                 .mode           = 0444,
577                 .kf_ops         = &rdtgroup_kf_single_ops,
578                 .seq_show       = rdt_default_ctrl_show,
579         },
580         {
581                 .name           = "min_cbm_bits",
582                 .mode           = 0444,
583                 .kf_ops         = &rdtgroup_kf_single_ops,
584                 .seq_show       = rdt_min_cbm_bits_show,
585         },
586 };
587
588 /* rdtgroup information files for memory bandwidth. */
589 static struct rftype res_mba_info_files[] = {
590         {
591                 .name           = "num_closids",
592                 .mode           = 0444,
593                 .kf_ops         = &rdtgroup_kf_single_ops,
594                 .seq_show       = rdt_num_closids_show,
595         },
596         {
597                 .name           = "min_bandwidth",
598                 .mode           = 0444,
599                 .kf_ops         = &rdtgroup_kf_single_ops,
600                 .seq_show       = rdt_min_bw_show,
601         },
602         {
603                 .name           = "bandwidth_gran",
604                 .mode           = 0444,
605                 .kf_ops         = &rdtgroup_kf_single_ops,
606                 .seq_show       = rdt_bw_gran_show,
607         },
608         {
609                 .name           = "delay_linear",
610                 .mode           = 0444,
611                 .kf_ops         = &rdtgroup_kf_single_ops,
612                 .seq_show       = rdt_delay_linear_show,
613         },
614 };
615
616 void rdt_get_mba_infofile(struct rdt_resource *r)
617 {
618         r->info_files = res_mba_info_files;
619         r->nr_info_files = ARRAY_SIZE(res_mba_info_files);
620 }
621
622 void rdt_get_cache_infofile(struct rdt_resource *r)
623 {
624         r->info_files = res_cache_info_files;
625         r->nr_info_files = ARRAY_SIZE(res_cache_info_files);
626 }
627
628 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
629 {
630         struct kernfs_node *kn_subdir;
631         struct rftype *res_info_files;
632         struct rdt_resource *r;
633         int ret, len;
634
635         /* create the directory */
636         kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
637         if (IS_ERR(kn_info))
638                 return PTR_ERR(kn_info);
639         kernfs_get(kn_info);
640
641         for_each_enabled_rdt_resource(r) {
642                 kn_subdir = kernfs_create_dir(kn_info, r->name,
643                                               kn_info->mode, r);
644                 if (IS_ERR(kn_subdir)) {
645                         ret = PTR_ERR(kn_subdir);
646                         goto out_destroy;
647                 }
648                 kernfs_get(kn_subdir);
649                 ret = rdtgroup_kn_set_ugid(kn_subdir);
650                 if (ret)
651                         goto out_destroy;
652
653                 res_info_files = r->info_files;
654                 len = r->nr_info_files;
655
656                 ret = rdtgroup_add_files(kn_subdir, res_info_files, len);
657                 if (ret)
658                         goto out_destroy;
659                 kernfs_activate(kn_subdir);
660         }
661
662         /*
663          * This extra ref will be put in kernfs_remove() and guarantees
664          * that @rdtgrp->kn is always accessible.
665          */
666         kernfs_get(kn_info);
667
668         ret = rdtgroup_kn_set_ugid(kn_info);
669         if (ret)
670                 goto out_destroy;
671
672         kernfs_activate(kn_info);
673
674         return 0;
675
676 out_destroy:
677         kernfs_remove(kn_info);
678         return ret;
679 }
680
681 static void l3_qos_cfg_update(void *arg)
682 {
683         bool *enable = arg;
684
685         wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
686 }
687
688 static int set_l3_qos_cfg(struct rdt_resource *r, bool enable)
689 {
690         cpumask_var_t cpu_mask;
691         struct rdt_domain *d;
692         int cpu;
693
694         if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
695                 return -ENOMEM;
696
697         list_for_each_entry(d, &r->domains, list) {
698                 /* Pick one CPU from each domain instance to update MSR */
699                 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
700         }
701         cpu = get_cpu();
702         /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
703         if (cpumask_test_cpu(cpu, cpu_mask))
704                 l3_qos_cfg_update(&enable);
705         /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
706         smp_call_function_many(cpu_mask, l3_qos_cfg_update, &enable, 1);
707         put_cpu();
708
709         free_cpumask_var(cpu_mask);
710
711         return 0;
712 }
713
714 static int cdp_enable(void)
715 {
716         struct rdt_resource *r_l3data = &rdt_resources_all[RDT_RESOURCE_L3DATA];
717         struct rdt_resource *r_l3code = &rdt_resources_all[RDT_RESOURCE_L3CODE];
718         struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
719         int ret;
720
721         if (!r_l3->capable || !r_l3data->capable || !r_l3code->capable)
722                 return -EINVAL;
723
724         ret = set_l3_qos_cfg(r_l3, true);
725         if (!ret) {
726                 r_l3->enabled = false;
727                 r_l3data->enabled = true;
728                 r_l3code->enabled = true;
729         }
730         return ret;
731 }
732
733 static void cdp_disable(void)
734 {
735         struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
736
737         r->enabled = r->capable;
738
739         if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled) {
740                 rdt_resources_all[RDT_RESOURCE_L3DATA].enabled = false;
741                 rdt_resources_all[RDT_RESOURCE_L3CODE].enabled = false;
742                 set_l3_qos_cfg(r, false);
743         }
744 }
745
746 static int parse_rdtgroupfs_options(char *data)
747 {
748         char *token, *o = data;
749         int ret = 0;
750
751         while ((token = strsep(&o, ",")) != NULL) {
752                 if (!*token)
753                         return -EINVAL;
754
755                 if (!strcmp(token, "cdp"))
756                         ret = cdp_enable();
757         }
758
759         return ret;
760 }
761
762 /*
763  * We don't allow rdtgroup directories to be created anywhere
764  * except the root directory. Thus when looking for the rdtgroup
765  * structure for a kernfs node we are either looking at a directory,
766  * in which case the rdtgroup structure is pointed at by the "priv"
767  * field, otherwise we have a file, and need only look to the parent
768  * to find the rdtgroup.
769  */
770 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
771 {
772         if (kernfs_type(kn) == KERNFS_DIR) {
773                 /*
774                  * All the resource directories use "kn->priv"
775                  * to point to the "struct rdtgroup" for the
776                  * resource. "info" and its subdirectories don't
777                  * have rdtgroup structures, so return NULL here.
778                  */
779                 if (kn == kn_info || kn->parent == kn_info)
780                         return NULL;
781                 else
782                         return kn->priv;
783         } else {
784                 return kn->parent->priv;
785         }
786 }
787
788 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
789 {
790         struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
791
792         if (!rdtgrp)
793                 return NULL;
794
795         atomic_inc(&rdtgrp->waitcount);
796         kernfs_break_active_protection(kn);
797
798         mutex_lock(&rdtgroup_mutex);
799
800         /* Was this group deleted while we waited? */
801         if (rdtgrp->flags & RDT_DELETED)
802                 return NULL;
803
804         return rdtgrp;
805 }
806
807 void rdtgroup_kn_unlock(struct kernfs_node *kn)
808 {
809         struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
810
811         if (!rdtgrp)
812                 return;
813
814         mutex_unlock(&rdtgroup_mutex);
815
816         if (atomic_dec_and_test(&rdtgrp->waitcount) &&
817             (rdtgrp->flags & RDT_DELETED)) {
818                 kernfs_unbreak_active_protection(kn);
819                 kernfs_put(rdtgrp->kn);
820                 kfree(rdtgrp);
821         } else {
822                 kernfs_unbreak_active_protection(kn);
823         }
824 }
825
826 static struct dentry *rdt_mount(struct file_system_type *fs_type,
827                                 int flags, const char *unused_dev_name,
828                                 void *data)
829 {
830         struct dentry *dentry;
831         int ret;
832
833         mutex_lock(&rdtgroup_mutex);
834         /*
835          * resctrl file system can only be mounted once.
836          */
837         if (static_branch_unlikely(&rdt_enable_key)) {
838                 dentry = ERR_PTR(-EBUSY);
839                 goto out;
840         }
841
842         ret = parse_rdtgroupfs_options(data);
843         if (ret) {
844                 dentry = ERR_PTR(ret);
845                 goto out_cdp;
846         }
847
848         closid_init();
849
850         ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
851         if (ret) {
852                 dentry = ERR_PTR(ret);
853                 goto out_cdp;
854         }
855
856         dentry = kernfs_mount(fs_type, flags, rdt_root,
857                               RDTGROUP_SUPER_MAGIC, NULL);
858         if (IS_ERR(dentry))
859                 goto out_destroy;
860
861         static_branch_enable(&rdt_enable_key);
862         goto out;
863
864 out_destroy:
865         kernfs_remove(kn_info);
866 out_cdp:
867         cdp_disable();
868 out:
869         mutex_unlock(&rdtgroup_mutex);
870
871         return dentry;
872 }
873
874 static int reset_all_ctrls(struct rdt_resource *r)
875 {
876         struct msr_param msr_param;
877         cpumask_var_t cpu_mask;
878         struct rdt_domain *d;
879         int i, cpu;
880
881         if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
882                 return -ENOMEM;
883
884         msr_param.res = r;
885         msr_param.low = 0;
886         msr_param.high = r->num_closid;
887
888         /*
889          * Disable resource control for this resource by setting all
890          * CBMs in all domains to the maximum mask value. Pick one CPU
891          * from each domain to update the MSRs below.
892          */
893         list_for_each_entry(d, &r->domains, list) {
894                 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
895
896                 for (i = 0; i < r->num_closid; i++)
897                         d->ctrl_val[i] = r->default_ctrl;
898         }
899         cpu = get_cpu();
900         /* Update CBM on this cpu if it's in cpu_mask. */
901         if (cpumask_test_cpu(cpu, cpu_mask))
902                 rdt_ctrl_update(&msr_param);
903         /* Update CBM on all other cpus in cpu_mask. */
904         smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
905         put_cpu();
906
907         free_cpumask_var(cpu_mask);
908
909         return 0;
910 }
911
912 /*
913  * Move tasks from one to the other group. If @from is NULL, then all tasks
914  * in the systems are moved unconditionally (used for teardown).
915  *
916  * If @mask is not NULL the cpus on which moved tasks are running are set
917  * in that mask so the update smp function call is restricted to affected
918  * cpus.
919  */
920 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
921                                  struct cpumask *mask)
922 {
923         struct task_struct *p, *t;
924
925         read_lock(&tasklist_lock);
926         for_each_process_thread(p, t) {
927                 if (!from || t->closid == from->closid) {
928                         t->closid = to->closid;
929 #ifdef CONFIG_SMP
930                         /*
931                          * This is safe on x86 w/o barriers as the ordering
932                          * of writing to task_cpu() and t->on_cpu is
933                          * reverse to the reading here. The detection is
934                          * inaccurate as tasks might move or schedule
935                          * before the smp function call takes place. In
936                          * such a case the function call is pointless, but
937                          * there is no other side effect.
938                          */
939                         if (mask && t->on_cpu)
940                                 cpumask_set_cpu(task_cpu(t), mask);
941 #endif
942                 }
943         }
944         read_unlock(&tasklist_lock);
945 }
946
947 /*
948  * Forcibly remove all of subdirectories under root.
949  */
950 static void rmdir_all_sub(void)
951 {
952         struct rdtgroup *rdtgrp, *tmp;
953
954         /* Move all tasks to the default resource group */
955         rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
956
957         list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
958                 /* Remove each rdtgroup other than root */
959                 if (rdtgrp == &rdtgroup_default)
960                         continue;
961
962                 /*
963                  * Give any CPUs back to the default group. We cannot copy
964                  * cpu_online_mask because a CPU might have executed the
965                  * offline callback already, but is still marked online.
966                  */
967                 cpumask_or(&rdtgroup_default.cpu_mask,
968                            &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
969
970                 kernfs_remove(rdtgrp->kn);
971                 list_del(&rdtgrp->rdtgroup_list);
972                 kfree(rdtgrp);
973         }
974         /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
975         get_online_cpus();
976         rdt_update_closid(cpu_online_mask, &rdtgroup_default.closid);
977         put_online_cpus();
978
979         kernfs_remove(kn_info);
980 }
981
982 static void rdt_kill_sb(struct super_block *sb)
983 {
984         struct rdt_resource *r;
985
986         mutex_lock(&rdtgroup_mutex);
987
988         /*Put everything back to default values. */
989         for_each_enabled_rdt_resource(r)
990                 reset_all_ctrls(r);
991         cdp_disable();
992         rmdir_all_sub();
993         static_branch_disable(&rdt_enable_key);
994         kernfs_kill_sb(sb);
995         mutex_unlock(&rdtgroup_mutex);
996 }
997
998 static struct file_system_type rdt_fs_type = {
999         .name    = "resctrl",
1000         .mount   = rdt_mount,
1001         .kill_sb = rdt_kill_sb,
1002 };
1003
1004 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
1005                           umode_t mode)
1006 {
1007         struct rdtgroup *parent, *rdtgrp;
1008         struct kernfs_node *kn;
1009         int ret, closid;
1010
1011         /* Only allow mkdir in the root directory */
1012         if (parent_kn != rdtgroup_default.kn)
1013                 return -EPERM;
1014
1015         /* Do not accept '\n' to avoid unparsable situation. */
1016         if (strchr(name, '\n'))
1017                 return -EINVAL;
1018
1019         parent = rdtgroup_kn_lock_live(parent_kn);
1020         if (!parent) {
1021                 ret = -ENODEV;
1022                 goto out_unlock;
1023         }
1024
1025         ret = closid_alloc();
1026         if (ret < 0)
1027                 goto out_unlock;
1028         closid = ret;
1029
1030         /* allocate the rdtgroup. */
1031         rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
1032         if (!rdtgrp) {
1033                 ret = -ENOSPC;
1034                 goto out_closid_free;
1035         }
1036         rdtgrp->closid = closid;
1037         list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
1038
1039         /* kernfs creates the directory for rdtgrp */
1040         kn = kernfs_create_dir(parent->kn, name, mode, rdtgrp);
1041         if (IS_ERR(kn)) {
1042                 ret = PTR_ERR(kn);
1043                 goto out_cancel_ref;
1044         }
1045         rdtgrp->kn = kn;
1046
1047         /*
1048          * kernfs_remove() will drop the reference count on "kn" which
1049          * will free it. But we still need it to stick around for the
1050          * rdtgroup_kn_unlock(kn} call below. Take one extra reference
1051          * here, which will be dropped inside rdtgroup_kn_unlock().
1052          */
1053         kernfs_get(kn);
1054
1055         ret = rdtgroup_kn_set_ugid(kn);
1056         if (ret)
1057                 goto out_destroy;
1058
1059         ret = rdtgroup_add_files(kn, rdtgroup_base_files,
1060                                  ARRAY_SIZE(rdtgroup_base_files));
1061         if (ret)
1062                 goto out_destroy;
1063
1064         kernfs_activate(kn);
1065
1066         ret = 0;
1067         goto out_unlock;
1068
1069 out_destroy:
1070         kernfs_remove(rdtgrp->kn);
1071 out_cancel_ref:
1072         list_del(&rdtgrp->rdtgroup_list);
1073         kfree(rdtgrp);
1074 out_closid_free:
1075         closid_free(closid);
1076 out_unlock:
1077         rdtgroup_kn_unlock(parent_kn);
1078         return ret;
1079 }
1080
1081 static int rdtgroup_rmdir(struct kernfs_node *kn)
1082 {
1083         int ret, cpu, closid = rdtgroup_default.closid;
1084         struct rdtgroup *rdtgrp;
1085         cpumask_var_t tmpmask;
1086
1087         if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
1088                 return -ENOMEM;
1089
1090         rdtgrp = rdtgroup_kn_lock_live(kn);
1091         if (!rdtgrp) {
1092                 ret = -EPERM;
1093                 goto out;
1094         }
1095
1096         /* Give any tasks back to the default group */
1097         rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
1098
1099         /* Give any CPUs back to the default group */
1100         cpumask_or(&rdtgroup_default.cpu_mask,
1101                    &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
1102
1103         /* Update per cpu closid of the moved CPUs first */
1104         for_each_cpu(cpu, &rdtgrp->cpu_mask)
1105                 per_cpu(cpu_closid, cpu) = closid;
1106         /*
1107          * Update the MSR on moved CPUs and CPUs which have moved
1108          * task running on them.
1109          */
1110         cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
1111         rdt_update_closid(tmpmask, NULL);
1112
1113         rdtgrp->flags = RDT_DELETED;
1114         closid_free(rdtgrp->closid);
1115         list_del(&rdtgrp->rdtgroup_list);
1116
1117         /*
1118          * one extra hold on this, will drop when we kfree(rdtgrp)
1119          * in rdtgroup_kn_unlock()
1120          */
1121         kernfs_get(kn);
1122         kernfs_remove(rdtgrp->kn);
1123         ret = 0;
1124 out:
1125         rdtgroup_kn_unlock(kn);
1126         free_cpumask_var(tmpmask);
1127         return ret;
1128 }
1129
1130 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
1131 {
1132         if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled)
1133                 seq_puts(seq, ",cdp");
1134         return 0;
1135 }
1136
1137 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
1138         .mkdir          = rdtgroup_mkdir,
1139         .rmdir          = rdtgroup_rmdir,
1140         .show_options   = rdtgroup_show_options,
1141 };
1142
1143 static int __init rdtgroup_setup_root(void)
1144 {
1145         int ret;
1146
1147         rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
1148                                       KERNFS_ROOT_CREATE_DEACTIVATED,
1149                                       &rdtgroup_default);
1150         if (IS_ERR(rdt_root))
1151                 return PTR_ERR(rdt_root);
1152
1153         mutex_lock(&rdtgroup_mutex);
1154
1155         rdtgroup_default.closid = 0;
1156         list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
1157
1158         ret = rdtgroup_add_files(rdt_root->kn, rdtgroup_base_files,
1159                                  ARRAY_SIZE(rdtgroup_base_files));
1160         if (ret) {
1161                 kernfs_destroy_root(rdt_root);
1162                 goto out;
1163         }
1164
1165         rdtgroup_default.kn = rdt_root->kn;
1166         kernfs_activate(rdtgroup_default.kn);
1167
1168 out:
1169         mutex_unlock(&rdtgroup_mutex);
1170
1171         return ret;
1172 }
1173
1174 /*
1175  * rdtgroup_init - rdtgroup initialization
1176  *
1177  * Setup resctrl file system including set up root, create mount point,
1178  * register rdtgroup filesystem, and initialize files under root directory.
1179  *
1180  * Return: 0 on success or -errno
1181  */
1182 int __init rdtgroup_init(void)
1183 {
1184         int ret = 0;
1185
1186         ret = rdtgroup_setup_root();
1187         if (ret)
1188                 return ret;
1189
1190         ret = sysfs_create_mount_point(fs_kobj, "resctrl");
1191         if (ret)
1192                 goto cleanup_root;
1193
1194         ret = register_filesystem(&rdt_fs_type);
1195         if (ret)
1196                 goto cleanup_mountpoint;
1197
1198         return 0;
1199
1200 cleanup_mountpoint:
1201         sysfs_remove_mount_point(fs_kobj, "resctrl");
1202 cleanup_root:
1203         kernfs_destroy_root(rdt_root);
1204
1205         return ret;
1206 }