Merge branch 'x86/urgent' into x86/cpu, to resolve conflict
[linux-2.6-block.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
519         return 0;
520 }
521
522 static int rdt_cbm_mask_show(struct kernfs_open_file *of,
523                              struct seq_file *seq, void *v)
524 {
525         struct rdt_resource *r = of->kn->parent->priv;
526
527         seq_printf(seq, "%x\n", r->max_cbm);
528
529         return 0;
530 }
531
532 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
533                              struct seq_file *seq, void *v)
534 {
535         struct rdt_resource *r = of->kn->parent->priv;
536
537         seq_printf(seq, "%d\n", r->min_cbm_bits);
538
539         return 0;
540 }
541
542 /* rdtgroup information files for one cache resource. */
543 static struct rftype res_info_files[] = {
544         {
545                 .name           = "num_closids",
546                 .mode           = 0444,
547                 .kf_ops         = &rdtgroup_kf_single_ops,
548                 .seq_show       = rdt_num_closids_show,
549         },
550         {
551                 .name           = "cbm_mask",
552                 .mode           = 0444,
553                 .kf_ops         = &rdtgroup_kf_single_ops,
554                 .seq_show       = rdt_cbm_mask_show,
555         },
556         {
557                 .name           = "min_cbm_bits",
558                 .mode           = 0444,
559                 .kf_ops         = &rdtgroup_kf_single_ops,
560                 .seq_show       = rdt_min_cbm_bits_show,
561         },
562 };
563
564 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
565 {
566         struct kernfs_node *kn_subdir;
567         struct rdt_resource *r;
568         int ret;
569
570         /* create the directory */
571         kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
572         if (IS_ERR(kn_info))
573                 return PTR_ERR(kn_info);
574         kernfs_get(kn_info);
575
576         for_each_enabled_rdt_resource(r) {
577                 kn_subdir = kernfs_create_dir(kn_info, r->name,
578                                               kn_info->mode, r);
579                 if (IS_ERR(kn_subdir)) {
580                         ret = PTR_ERR(kn_subdir);
581                         goto out_destroy;
582                 }
583                 kernfs_get(kn_subdir);
584                 ret = rdtgroup_kn_set_ugid(kn_subdir);
585                 if (ret)
586                         goto out_destroy;
587                 ret = rdtgroup_add_files(kn_subdir, res_info_files,
588                                          ARRAY_SIZE(res_info_files));
589                 if (ret)
590                         goto out_destroy;
591                 kernfs_activate(kn_subdir);
592         }
593
594         /*
595          * This extra ref will be put in kernfs_remove() and guarantees
596          * that @rdtgrp->kn is always accessible.
597          */
598         kernfs_get(kn_info);
599
600         ret = rdtgroup_kn_set_ugid(kn_info);
601         if (ret)
602                 goto out_destroy;
603
604         kernfs_activate(kn_info);
605
606         return 0;
607
608 out_destroy:
609         kernfs_remove(kn_info);
610         return ret;
611 }
612
613 static void l3_qos_cfg_update(void *arg)
614 {
615         bool *enable = arg;
616
617         wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
618 }
619
620 static int set_l3_qos_cfg(struct rdt_resource *r, bool enable)
621 {
622         cpumask_var_t cpu_mask;
623         struct rdt_domain *d;
624         int cpu;
625
626         if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
627                 return -ENOMEM;
628
629         list_for_each_entry(d, &r->domains, list) {
630                 /* Pick one CPU from each domain instance to update MSR */
631                 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
632         }
633         cpu = get_cpu();
634         /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
635         if (cpumask_test_cpu(cpu, cpu_mask))
636                 l3_qos_cfg_update(&enable);
637         /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
638         smp_call_function_many(cpu_mask, l3_qos_cfg_update, &enable, 1);
639         put_cpu();
640
641         free_cpumask_var(cpu_mask);
642
643         return 0;
644 }
645
646 static int cdp_enable(void)
647 {
648         struct rdt_resource *r_l3data = &rdt_resources_all[RDT_RESOURCE_L3DATA];
649         struct rdt_resource *r_l3code = &rdt_resources_all[RDT_RESOURCE_L3CODE];
650         struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
651         int ret;
652
653         if (!r_l3->capable || !r_l3data->capable || !r_l3code->capable)
654                 return -EINVAL;
655
656         ret = set_l3_qos_cfg(r_l3, true);
657         if (!ret) {
658                 r_l3->enabled = false;
659                 r_l3data->enabled = true;
660                 r_l3code->enabled = true;
661         }
662         return ret;
663 }
664
665 static void cdp_disable(void)
666 {
667         struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
668
669         r->enabled = r->capable;
670
671         if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled) {
672                 rdt_resources_all[RDT_RESOURCE_L3DATA].enabled = false;
673                 rdt_resources_all[RDT_RESOURCE_L3CODE].enabled = false;
674                 set_l3_qos_cfg(r, false);
675         }
676 }
677
678 static int parse_rdtgroupfs_options(char *data)
679 {
680         char *token, *o = data;
681         int ret = 0;
682
683         while ((token = strsep(&o, ",")) != NULL) {
684                 if (!*token)
685                         return -EINVAL;
686
687                 if (!strcmp(token, "cdp"))
688                         ret = cdp_enable();
689         }
690
691         return ret;
692 }
693
694 /*
695  * We don't allow rdtgroup directories to be created anywhere
696  * except the root directory. Thus when looking for the rdtgroup
697  * structure for a kernfs node we are either looking at a directory,
698  * in which case the rdtgroup structure is pointed at by the "priv"
699  * field, otherwise we have a file, and need only look to the parent
700  * to find the rdtgroup.
701  */
702 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
703 {
704         if (kernfs_type(kn) == KERNFS_DIR) {
705                 /*
706                  * All the resource directories use "kn->priv"
707                  * to point to the "struct rdtgroup" for the
708                  * resource. "info" and its subdirectories don't
709                  * have rdtgroup structures, so return NULL here.
710                  */
711                 if (kn == kn_info || kn->parent == kn_info)
712                         return NULL;
713                 else
714                         return kn->priv;
715         } else {
716                 return kn->parent->priv;
717         }
718 }
719
720 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
721 {
722         struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
723
724         if (!rdtgrp)
725                 return NULL;
726
727         atomic_inc(&rdtgrp->waitcount);
728         kernfs_break_active_protection(kn);
729
730         mutex_lock(&rdtgroup_mutex);
731
732         /* Was this group deleted while we waited? */
733         if (rdtgrp->flags & RDT_DELETED)
734                 return NULL;
735
736         return rdtgrp;
737 }
738
739 void rdtgroup_kn_unlock(struct kernfs_node *kn)
740 {
741         struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
742
743         if (!rdtgrp)
744                 return;
745
746         mutex_unlock(&rdtgroup_mutex);
747
748         if (atomic_dec_and_test(&rdtgrp->waitcount) &&
749             (rdtgrp->flags & RDT_DELETED)) {
750                 kernfs_unbreak_active_protection(kn);
751                 kernfs_put(rdtgrp->kn);
752                 kfree(rdtgrp);
753         } else {
754                 kernfs_unbreak_active_protection(kn);
755         }
756 }
757
758 static struct dentry *rdt_mount(struct file_system_type *fs_type,
759                                 int flags, const char *unused_dev_name,
760                                 void *data)
761 {
762         struct dentry *dentry;
763         int ret;
764
765         mutex_lock(&rdtgroup_mutex);
766         /*
767          * resctrl file system can only be mounted once.
768          */
769         if (static_branch_unlikely(&rdt_enable_key)) {
770                 dentry = ERR_PTR(-EBUSY);
771                 goto out;
772         }
773
774         ret = parse_rdtgroupfs_options(data);
775         if (ret) {
776                 dentry = ERR_PTR(ret);
777                 goto out_cdp;
778         }
779
780         closid_init();
781
782         ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
783         if (ret) {
784                 dentry = ERR_PTR(ret);
785                 goto out_cdp;
786         }
787
788         dentry = kernfs_mount(fs_type, flags, rdt_root,
789                               RDTGROUP_SUPER_MAGIC, NULL);
790         if (IS_ERR(dentry))
791                 goto out_cdp;
792
793         static_branch_enable(&rdt_enable_key);
794         goto out;
795
796 out_cdp:
797         cdp_disable();
798 out:
799         mutex_unlock(&rdtgroup_mutex);
800
801         return dentry;
802 }
803
804 static int reset_all_cbms(struct rdt_resource *r)
805 {
806         struct msr_param msr_param;
807         cpumask_var_t cpu_mask;
808         struct rdt_domain *d;
809         int i, cpu;
810
811         if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
812                 return -ENOMEM;
813
814         msr_param.res = r;
815         msr_param.low = 0;
816         msr_param.high = r->num_closid;
817
818         /*
819          * Disable resource control for this resource by setting all
820          * CBMs in all domains to the maximum mask value. Pick one CPU
821          * from each domain to update the MSRs below.
822          */
823         list_for_each_entry(d, &r->domains, list) {
824                 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
825
826                 for (i = 0; i < r->num_closid; i++)
827                         d->cbm[i] = r->max_cbm;
828         }
829         cpu = get_cpu();
830         /* Update CBM on this cpu if it's in cpu_mask. */
831         if (cpumask_test_cpu(cpu, cpu_mask))
832                 rdt_cbm_update(&msr_param);
833         /* Update CBM on all other cpus in cpu_mask. */
834         smp_call_function_many(cpu_mask, rdt_cbm_update, &msr_param, 1);
835         put_cpu();
836
837         free_cpumask_var(cpu_mask);
838
839         return 0;
840 }
841
842 /*
843  * Move tasks from one to the other group. If @from is NULL, then all tasks
844  * in the systems are moved unconditionally (used for teardown).
845  *
846  * If @mask is not NULL the cpus on which moved tasks are running are set
847  * in that mask so the update smp function call is restricted to affected
848  * cpus.
849  */
850 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
851                                  struct cpumask *mask)
852 {
853         struct task_struct *p, *t;
854
855         read_lock(&tasklist_lock);
856         for_each_process_thread(p, t) {
857                 if (!from || t->closid == from->closid) {
858                         t->closid = to->closid;
859 #ifdef CONFIG_SMP
860                         /*
861                          * This is safe on x86 w/o barriers as the ordering
862                          * of writing to task_cpu() and t->on_cpu is
863                          * reverse to the reading here. The detection is
864                          * inaccurate as tasks might move or schedule
865                          * before the smp function call takes place. In
866                          * such a case the function call is pointless, but
867                          * there is no other side effect.
868                          */
869                         if (mask && t->on_cpu)
870                                 cpumask_set_cpu(task_cpu(t), mask);
871 #endif
872                 }
873         }
874         read_unlock(&tasklist_lock);
875 }
876
877 /*
878  * Forcibly remove all of subdirectories under root.
879  */
880 static void rmdir_all_sub(void)
881 {
882         struct rdtgroup *rdtgrp, *tmp;
883
884         /* Move all tasks to the default resource group */
885         rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
886
887         list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
888                 /* Remove each rdtgroup other than root */
889                 if (rdtgrp == &rdtgroup_default)
890                         continue;
891
892                 /*
893                  * Give any CPUs back to the default group. We cannot copy
894                  * cpu_online_mask because a CPU might have executed the
895                  * offline callback already, but is still marked online.
896                  */
897                 cpumask_or(&rdtgroup_default.cpu_mask,
898                            &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
899
900                 kernfs_remove(rdtgrp->kn);
901                 list_del(&rdtgrp->rdtgroup_list);
902                 kfree(rdtgrp);
903         }
904         /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
905         get_online_cpus();
906         rdt_update_closid(cpu_online_mask, &rdtgroup_default.closid);
907         put_online_cpus();
908
909         kernfs_remove(kn_info);
910 }
911
912 static void rdt_kill_sb(struct super_block *sb)
913 {
914         struct rdt_resource *r;
915
916         mutex_lock(&rdtgroup_mutex);
917
918         /*Put everything back to default values. */
919         for_each_enabled_rdt_resource(r)
920                 reset_all_cbms(r);
921         cdp_disable();
922         rmdir_all_sub();
923         static_branch_disable(&rdt_enable_key);
924         kernfs_kill_sb(sb);
925         mutex_unlock(&rdtgroup_mutex);
926 }
927
928 static struct file_system_type rdt_fs_type = {
929         .name    = "resctrl",
930         .mount   = rdt_mount,
931         .kill_sb = rdt_kill_sb,
932 };
933
934 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
935                           umode_t mode)
936 {
937         struct rdtgroup *parent, *rdtgrp;
938         struct kernfs_node *kn;
939         int ret, closid;
940
941         /* Only allow mkdir in the root directory */
942         if (parent_kn != rdtgroup_default.kn)
943                 return -EPERM;
944
945         /* Do not accept '\n' to avoid unparsable situation. */
946         if (strchr(name, '\n'))
947                 return -EINVAL;
948
949         parent = rdtgroup_kn_lock_live(parent_kn);
950         if (!parent) {
951                 ret = -ENODEV;
952                 goto out_unlock;
953         }
954
955         ret = closid_alloc();
956         if (ret < 0)
957                 goto out_unlock;
958         closid = ret;
959
960         /* allocate the rdtgroup. */
961         rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
962         if (!rdtgrp) {
963                 ret = -ENOSPC;
964                 goto out_closid_free;
965         }
966         rdtgrp->closid = closid;
967         list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
968
969         /* kernfs creates the directory for rdtgrp */
970         kn = kernfs_create_dir(parent->kn, name, mode, rdtgrp);
971         if (IS_ERR(kn)) {
972                 ret = PTR_ERR(kn);
973                 goto out_cancel_ref;
974         }
975         rdtgrp->kn = kn;
976
977         /*
978          * kernfs_remove() will drop the reference count on "kn" which
979          * will free it. But we still need it to stick around for the
980          * rdtgroup_kn_unlock(kn} call below. Take one extra reference
981          * here, which will be dropped inside rdtgroup_kn_unlock().
982          */
983         kernfs_get(kn);
984
985         ret = rdtgroup_kn_set_ugid(kn);
986         if (ret)
987                 goto out_destroy;
988
989         ret = rdtgroup_add_files(kn, rdtgroup_base_files,
990                                  ARRAY_SIZE(rdtgroup_base_files));
991         if (ret)
992                 goto out_destroy;
993
994         kernfs_activate(kn);
995
996         ret = 0;
997         goto out_unlock;
998
999 out_destroy:
1000         kernfs_remove(rdtgrp->kn);
1001 out_cancel_ref:
1002         list_del(&rdtgrp->rdtgroup_list);
1003         kfree(rdtgrp);
1004 out_closid_free:
1005         closid_free(closid);
1006 out_unlock:
1007         rdtgroup_kn_unlock(parent_kn);
1008         return ret;
1009 }
1010
1011 static int rdtgroup_rmdir(struct kernfs_node *kn)
1012 {
1013         int ret, cpu, closid = rdtgroup_default.closid;
1014         struct rdtgroup *rdtgrp;
1015         cpumask_var_t tmpmask;
1016
1017         if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
1018                 return -ENOMEM;
1019
1020         rdtgrp = rdtgroup_kn_lock_live(kn);
1021         if (!rdtgrp) {
1022                 ret = -EPERM;
1023                 goto out;
1024         }
1025
1026         /* Give any tasks back to the default group */
1027         rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
1028
1029         /* Give any CPUs back to the default group */
1030         cpumask_or(&rdtgroup_default.cpu_mask,
1031                    &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
1032
1033         /* Update per cpu closid of the moved CPUs first */
1034         for_each_cpu(cpu, &rdtgrp->cpu_mask)
1035                 per_cpu(cpu_closid, cpu) = closid;
1036         /*
1037          * Update the MSR on moved CPUs and CPUs which have moved
1038          * task running on them.
1039          */
1040         cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
1041         rdt_update_closid(tmpmask, NULL);
1042
1043         rdtgrp->flags = RDT_DELETED;
1044         closid_free(rdtgrp->closid);
1045         list_del(&rdtgrp->rdtgroup_list);
1046
1047         /*
1048          * one extra hold on this, will drop when we kfree(rdtgrp)
1049          * in rdtgroup_kn_unlock()
1050          */
1051         kernfs_get(kn);
1052         kernfs_remove(rdtgrp->kn);
1053         ret = 0;
1054 out:
1055         rdtgroup_kn_unlock(kn);
1056         free_cpumask_var(tmpmask);
1057         return ret;
1058 }
1059
1060 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
1061 {
1062         if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled)
1063                 seq_puts(seq, ",cdp");
1064         return 0;
1065 }
1066
1067 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
1068         .mkdir          = rdtgroup_mkdir,
1069         .rmdir          = rdtgroup_rmdir,
1070         .show_options   = rdtgroup_show_options,
1071 };
1072
1073 static int __init rdtgroup_setup_root(void)
1074 {
1075         int ret;
1076
1077         rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
1078                                       KERNFS_ROOT_CREATE_DEACTIVATED,
1079                                       &rdtgroup_default);
1080         if (IS_ERR(rdt_root))
1081                 return PTR_ERR(rdt_root);
1082
1083         mutex_lock(&rdtgroup_mutex);
1084
1085         rdtgroup_default.closid = 0;
1086         list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
1087
1088         ret = rdtgroup_add_files(rdt_root->kn, rdtgroup_base_files,
1089                                  ARRAY_SIZE(rdtgroup_base_files));
1090         if (ret) {
1091                 kernfs_destroy_root(rdt_root);
1092                 goto out;
1093         }
1094
1095         rdtgroup_default.kn = rdt_root->kn;
1096         kernfs_activate(rdtgroup_default.kn);
1097
1098 out:
1099         mutex_unlock(&rdtgroup_mutex);
1100
1101         return ret;
1102 }
1103
1104 /*
1105  * rdtgroup_init - rdtgroup initialization
1106  *
1107  * Setup resctrl file system including set up root, create mount point,
1108  * register rdtgroup filesystem, and initialize files under root directory.
1109  *
1110  * Return: 0 on success or -errno
1111  */
1112 int __init rdtgroup_init(void)
1113 {
1114         int ret = 0;
1115
1116         ret = rdtgroup_setup_root();
1117         if (ret)
1118                 return ret;
1119
1120         ret = sysfs_create_mount_point(fs_kobj, "resctrl");
1121         if (ret)
1122                 goto cleanup_root;
1123
1124         ret = register_filesystem(&rdt_fs_type);
1125         if (ret)
1126                 goto cleanup_mountpoint;
1127
1128         return 0;
1129
1130 cleanup_mountpoint:
1131         sysfs_remove_mount_point(fs_kobj, "resctrl");
1132 cleanup_root:
1133         kernfs_destroy_root(rdt_root);
1134
1135         return ret;
1136 }