x86/intel_rdt: Protect against resource group changes during locking
[linux-block.git] / arch / x86 / kernel / cpu / intel_rdt_ctrlmondata.c
1 /*
2  * Resource Director Technology(RDT)
3  * - Cache Allocation code.
4  *
5  * Copyright (C) 2016 Intel Corporation
6  *
7  * Authors:
8  *    Fenghua Yu <fenghua.yu@intel.com>
9  *    Tony Luck <tony.luck@intel.com>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms and conditions of the GNU General Public License,
13  * version 2, as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18  * more details.
19  *
20  * More information about RDT be found in the Intel (R) x86 Architecture
21  * Software Developer Manual June 2016, volume 3, section 17.17.
22  */
23
24 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
25
26 #include <linux/kernfs.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29 #include "intel_rdt.h"
30
31 /*
32  * Check whether MBA bandwidth percentage value is correct. The value is
33  * checked against the minimum and max bandwidth values specified by the
34  * hardware. The allocated bandwidth percentage is rounded to the next
35  * control step available on the hardware.
36  */
37 static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
38 {
39         unsigned long bw;
40         int ret;
41
42         /*
43          * Only linear delay values is supported for current Intel SKUs.
44          */
45         if (!r->membw.delay_linear) {
46                 rdt_last_cmd_puts("No support for non-linear MB domains\n");
47                 return false;
48         }
49
50         ret = kstrtoul(buf, 10, &bw);
51         if (ret) {
52                 rdt_last_cmd_printf("Non-decimal digit in MB value %s\n", buf);
53                 return false;
54         }
55
56         if ((bw < r->membw.min_bw || bw > r->default_ctrl) &&
57             !is_mba_sc(r)) {
58                 rdt_last_cmd_printf("MB value %ld out of range [%d,%d]\n", bw,
59                                     r->membw.min_bw, r->default_ctrl);
60                 return false;
61         }
62
63         *data = roundup(bw, (unsigned long)r->membw.bw_gran);
64         return true;
65 }
66
67 int parse_bw(void *_buf, struct rdt_resource *r, struct rdt_domain *d)
68 {
69         unsigned long data;
70         char *buf = _buf;
71
72         if (d->have_new_ctrl) {
73                 rdt_last_cmd_printf("duplicate domain %d\n", d->id);
74                 return -EINVAL;
75         }
76
77         if (!bw_validate(buf, &data, r))
78                 return -EINVAL;
79         d->new_ctrl = data;
80         d->have_new_ctrl = true;
81
82         return 0;
83 }
84
85 /*
86  * Check whether a cache bit mask is valid. The SDM says:
87  *      Please note that all (and only) contiguous '1' combinations
88  *      are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
89  * Additionally Haswell requires at least two bits set.
90  */
91 static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
92 {
93         unsigned long first_bit, zero_bit, val;
94         unsigned int cbm_len = r->cache.cbm_len;
95         int ret;
96
97         ret = kstrtoul(buf, 16, &val);
98         if (ret) {
99                 rdt_last_cmd_printf("non-hex character in mask %s\n", buf);
100                 return false;
101         }
102
103         if (val == 0 || val > r->default_ctrl) {
104                 rdt_last_cmd_puts("mask out of range\n");
105                 return false;
106         }
107
108         first_bit = find_first_bit(&val, cbm_len);
109         zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
110
111         if (find_next_bit(&val, cbm_len, zero_bit) < cbm_len) {
112                 rdt_last_cmd_printf("mask %lx has non-consecutive 1-bits\n", val);
113                 return false;
114         }
115
116         if ((zero_bit - first_bit) < r->cache.min_cbm_bits) {
117                 rdt_last_cmd_printf("Need at least %d bits in mask\n",
118                                     r->cache.min_cbm_bits);
119                 return false;
120         }
121
122         *data = val;
123         return true;
124 }
125
126 struct rdt_cbm_parse_data {
127         struct rdtgroup         *rdtgrp;
128         char                    *buf;
129 };
130
131 /*
132  * Read one cache bit mask (hex). Check that it is valid for the current
133  * resource type.
134  */
135 int parse_cbm(void *_data, struct rdt_resource *r, struct rdt_domain *d)
136 {
137         struct rdt_cbm_parse_data *data = _data;
138         struct rdtgroup *rdtgrp = data->rdtgrp;
139         u32 cbm_val;
140
141         if (d->have_new_ctrl) {
142                 rdt_last_cmd_printf("duplicate domain %d\n", d->id);
143                 return -EINVAL;
144         }
145
146         if (!cbm_validate(data->buf, &cbm_val, r))
147                 return -EINVAL;
148
149         /*
150          * The CBM may not overlap with the CBM of another closid if
151          * either is exclusive.
152          */
153         if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, true)) {
154                 rdt_last_cmd_printf("overlaps with exclusive group\n");
155                 return -EINVAL;
156         }
157
158         if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, false)) {
159                 if (rdtgrp->mode == RDT_MODE_EXCLUSIVE) {
160                         rdt_last_cmd_printf("overlaps with other group\n");
161                         return -EINVAL;
162                 }
163         }
164
165         d->new_ctrl = cbm_val;
166         d->have_new_ctrl = true;
167
168         return 0;
169 }
170
171 /*
172  * For each domain in this resource we expect to find a series of:
173  *      id=mask
174  * separated by ";". The "id" is in decimal, and must match one of
175  * the "id"s for this resource.
176  */
177 static int parse_line(char *line, struct rdt_resource *r,
178                       struct rdtgroup *rdtgrp)
179 {
180         struct rdt_cbm_parse_data data;
181         char *dom = NULL, *id;
182         struct rdt_domain *d;
183         unsigned long dom_id;
184
185 next:
186         if (!line || line[0] == '\0')
187                 return 0;
188         dom = strsep(&line, ";");
189         id = strsep(&dom, "=");
190         if (!dom || kstrtoul(id, 10, &dom_id)) {
191                 rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
192                 return -EINVAL;
193         }
194         dom = strim(dom);
195         list_for_each_entry(d, &r->domains, list) {
196                 if (d->id == dom_id) {
197                         data.buf = dom;
198                         data.rdtgrp = rdtgrp;
199                         if (r->parse_ctrlval(&data, r, d))
200                                 return -EINVAL;
201                         goto next;
202                 }
203         }
204         return -EINVAL;
205 }
206
207 int update_domains(struct rdt_resource *r, int closid)
208 {
209         struct msr_param msr_param;
210         cpumask_var_t cpu_mask;
211         struct rdt_domain *d;
212         bool mba_sc;
213         u32 *dc;
214         int cpu;
215
216         if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
217                 return -ENOMEM;
218
219         msr_param.low = closid;
220         msr_param.high = msr_param.low + 1;
221         msr_param.res = r;
222
223         mba_sc = is_mba_sc(r);
224         list_for_each_entry(d, &r->domains, list) {
225                 dc = !mba_sc ? d->ctrl_val : d->mbps_val;
226                 if (d->have_new_ctrl && d->new_ctrl != dc[closid]) {
227                         cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
228                         dc[closid] = d->new_ctrl;
229                 }
230         }
231
232         /*
233          * Avoid writing the control msr with control values when
234          * MBA software controller is enabled
235          */
236         if (cpumask_empty(cpu_mask) || mba_sc)
237                 goto done;
238         cpu = get_cpu();
239         /* Update CBM on this cpu if it's in cpu_mask. */
240         if (cpumask_test_cpu(cpu, cpu_mask))
241                 rdt_ctrl_update(&msr_param);
242         /* Update CBM on other cpus. */
243         smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
244         put_cpu();
245
246 done:
247         free_cpumask_var(cpu_mask);
248
249         return 0;
250 }
251
252 static int rdtgroup_parse_resource(char *resname, char *tok,
253                                    struct rdtgroup *rdtgrp)
254 {
255         struct rdt_resource *r;
256
257         for_each_alloc_enabled_rdt_resource(r) {
258                 if (!strcmp(resname, r->name) && rdtgrp->closid < r->num_closid)
259                         return parse_line(tok, r, rdtgrp);
260         }
261         rdt_last_cmd_printf("unknown/unsupported resource name '%s'\n", resname);
262         return -EINVAL;
263 }
264
265 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
266                                 char *buf, size_t nbytes, loff_t off)
267 {
268         struct rdtgroup *rdtgrp;
269         struct rdt_domain *dom;
270         struct rdt_resource *r;
271         char *tok, *resname;
272         int ret = 0;
273
274         /* Valid input requires a trailing newline */
275         if (nbytes == 0 || buf[nbytes - 1] != '\n')
276                 return -EINVAL;
277         buf[nbytes - 1] = '\0';
278
279         rdtgrp = rdtgroup_kn_lock_live(of->kn);
280         if (!rdtgrp) {
281                 rdtgroup_kn_unlock(of->kn);
282                 return -ENOENT;
283         }
284         rdt_last_cmd_clear();
285
286         /*
287          * No changes to pseudo-locked region allowed. It has to be removed
288          * and re-created instead.
289          */
290         if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
291                 ret = -EINVAL;
292                 rdt_last_cmd_puts("resource group is pseudo-locked\n");
293                 goto out;
294         }
295
296         for_each_alloc_enabled_rdt_resource(r) {
297                 list_for_each_entry(dom, &r->domains, list)
298                         dom->have_new_ctrl = false;
299         }
300
301         while ((tok = strsep(&buf, "\n")) != NULL) {
302                 resname = strim(strsep(&tok, ":"));
303                 if (!tok) {
304                         rdt_last_cmd_puts("Missing ':'\n");
305                         ret = -EINVAL;
306                         goto out;
307                 }
308                 if (tok[0] == '\0') {
309                         rdt_last_cmd_printf("Missing '%s' value\n", resname);
310                         ret = -EINVAL;
311                         goto out;
312                 }
313                 ret = rdtgroup_parse_resource(resname, tok, rdtgrp);
314                 if (ret)
315                         goto out;
316         }
317
318         for_each_alloc_enabled_rdt_resource(r) {
319                 ret = update_domains(r, rdtgrp->closid);
320                 if (ret)
321                         goto out;
322         }
323
324 out:
325         rdtgroup_kn_unlock(of->kn);
326         return ret ?: nbytes;
327 }
328
329 static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
330 {
331         struct rdt_domain *dom;
332         bool sep = false;
333         u32 ctrl_val;
334
335         seq_printf(s, "%*s:", max_name_width, r->name);
336         list_for_each_entry(dom, &r->domains, list) {
337                 if (sep)
338                         seq_puts(s, ";");
339
340                 ctrl_val = (!is_mba_sc(r) ? dom->ctrl_val[closid] :
341                             dom->mbps_val[closid]);
342                 seq_printf(s, r->format_str, dom->id, max_data_width,
343                            ctrl_val);
344                 sep = true;
345         }
346         seq_puts(s, "\n");
347 }
348
349 int rdtgroup_schemata_show(struct kernfs_open_file *of,
350                            struct seq_file *s, void *v)
351 {
352         struct rdtgroup *rdtgrp;
353         struct rdt_resource *r;
354         int ret = 0;
355         u32 closid;
356
357         rdtgrp = rdtgroup_kn_lock_live(of->kn);
358         if (rdtgrp) {
359                 closid = rdtgrp->closid;
360                 for_each_alloc_enabled_rdt_resource(r) {
361                         if (closid < r->num_closid)
362                                 show_doms(s, r, closid);
363                 }
364         } else {
365                 ret = -ENOENT;
366         }
367         rdtgroup_kn_unlock(of->kn);
368         return ret;
369 }
370
371 void mon_event_read(struct rmid_read *rr, struct rdt_domain *d,
372                     struct rdtgroup *rdtgrp, int evtid, int first)
373 {
374         /*
375          * setup the parameters to send to the IPI to read the data.
376          */
377         rr->rgrp = rdtgrp;
378         rr->evtid = evtid;
379         rr->d = d;
380         rr->val = 0;
381         rr->first = first;
382
383         smp_call_function_any(&d->cpu_mask, mon_event_count, rr, 1);
384 }
385
386 int rdtgroup_mondata_show(struct seq_file *m, void *arg)
387 {
388         struct kernfs_open_file *of = m->private;
389         u32 resid, evtid, domid;
390         struct rdtgroup *rdtgrp;
391         struct rdt_resource *r;
392         union mon_data_bits md;
393         struct rdt_domain *d;
394         struct rmid_read rr;
395         int ret = 0;
396
397         rdtgrp = rdtgroup_kn_lock_live(of->kn);
398
399         md.priv = of->kn->priv;
400         resid = md.u.rid;
401         domid = md.u.domid;
402         evtid = md.u.evtid;
403
404         r = &rdt_resources_all[resid];
405         d = rdt_find_domain(r, domid, NULL);
406         if (!d) {
407                 ret = -ENOENT;
408                 goto out;
409         }
410
411         mon_event_read(&rr, d, rdtgrp, evtid, false);
412
413         if (rr.val & RMID_VAL_ERROR)
414                 seq_puts(m, "Error\n");
415         else if (rr.val & RMID_VAL_UNAVAIL)
416                 seq_puts(m, "Unavailable\n");
417         else
418                 seq_printf(m, "%llu\n", rr.val * r->mon_scale);
419
420 out:
421         rdtgroup_kn_unlock(of->kn);
422         return ret;
423 }