Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Thomas Gleixner. |
| 4 | * Copyright (C) 2016-2017 Christoph Hellwig. |
| 5 | */ |
Christoph Hellwig | 5e385a6 | 2016-07-04 17:39:27 +0900 | [diff] [blame] | 6 | #include <linux/interrupt.h> |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/cpu.h> |
| 10 | |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 11 | static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk, |
| 12 | int cpus_per_vec) |
| 13 | { |
| 14 | const struct cpumask *siblmsk; |
| 15 | int cpu, sibl; |
| 16 | |
| 17 | for ( ; cpus_per_vec > 0; ) { |
| 18 | cpu = cpumask_first(nmsk); |
| 19 | |
| 20 | /* Should not happen, but I'm too lazy to think about it */ |
| 21 | if (cpu >= nr_cpu_ids) |
| 22 | return; |
| 23 | |
| 24 | cpumask_clear_cpu(cpu, nmsk); |
| 25 | cpumask_set_cpu(cpu, irqmsk); |
| 26 | cpus_per_vec--; |
| 27 | |
| 28 | /* If the cpu has siblings, use them first */ |
| 29 | siblmsk = topology_sibling_cpumask(cpu); |
| 30 | for (sibl = -1; cpus_per_vec > 0; ) { |
| 31 | sibl = cpumask_next(sibl, siblmsk); |
| 32 | if (sibl >= nr_cpu_ids) |
| 33 | break; |
| 34 | if (!cpumask_test_and_clear_cpu(sibl, nmsk)) |
| 35 | continue; |
| 36 | cpumask_set_cpu(sibl, irqmsk); |
| 37 | cpus_per_vec--; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
Ming Lei | 47778f33 | 2018-03-08 18:53:55 +0800 | [diff] [blame] | 42 | static cpumask_var_t *alloc_node_to_cpumask(void) |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 43 | { |
| 44 | cpumask_var_t *masks; |
| 45 | int node; |
| 46 | |
| 47 | masks = kcalloc(nr_node_ids, sizeof(cpumask_var_t), GFP_KERNEL); |
| 48 | if (!masks) |
| 49 | return NULL; |
| 50 | |
| 51 | for (node = 0; node < nr_node_ids; node++) { |
| 52 | if (!zalloc_cpumask_var(&masks[node], GFP_KERNEL)) |
| 53 | goto out_unwind; |
| 54 | } |
| 55 | |
| 56 | return masks; |
| 57 | |
| 58 | out_unwind: |
| 59 | while (--node >= 0) |
| 60 | free_cpumask_var(masks[node]); |
| 61 | kfree(masks); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Ming Lei | 47778f33 | 2018-03-08 18:53:55 +0800 | [diff] [blame] | 65 | static void free_node_to_cpumask(cpumask_var_t *masks) |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 66 | { |
| 67 | int node; |
| 68 | |
| 69 | for (node = 0; node < nr_node_ids; node++) |
| 70 | free_cpumask_var(masks[node]); |
| 71 | kfree(masks); |
| 72 | } |
| 73 | |
Ming Lei | 47778f33 | 2018-03-08 18:53:55 +0800 | [diff] [blame] | 74 | static void build_node_to_cpumask(cpumask_var_t *masks) |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 75 | { |
| 76 | int cpu; |
| 77 | |
Christoph Hellwig | 84676c1 | 2018-01-12 10:53:05 +0800 | [diff] [blame] | 78 | for_each_possible_cpu(cpu) |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 79 | cpumask_set_cpu(cpu, masks[cpu_to_node(cpu)]); |
| 80 | } |
| 81 | |
Ming Lei | 47778f33 | 2018-03-08 18:53:55 +0800 | [diff] [blame] | 82 | static int get_nodes_in_cpumask(cpumask_var_t *node_to_cpumask, |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 83 | const struct cpumask *mask, nodemask_t *nodemsk) |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 84 | { |
Guilherme G. Piccoli | c0af524 | 2016-12-14 16:01:12 -0200 | [diff] [blame] | 85 | int n, nodes = 0; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 86 | |
| 87 | /* Calculate the number of nodes in the supplied affinity mask */ |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 88 | for_each_node(n) { |
Ming Lei | 47778f33 | 2018-03-08 18:53:55 +0800 | [diff] [blame] | 89 | if (cpumask_intersects(mask, node_to_cpumask[n])) { |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 90 | node_set(n, *nodemsk); |
| 91 | nodes++; |
| 92 | } |
| 93 | } |
| 94 | return nodes; |
| 95 | } |
| 96 | |
Ming Lei | 1a2d091 | 2018-03-08 18:53:57 +0800 | [diff] [blame^] | 97 | static int irq_build_affinity_masks(const struct irq_affinity *affd, |
| 98 | int startvec, int numvecs, |
Ming Lei | b3e6aaa | 2018-03-08 18:53:56 +0800 | [diff] [blame] | 99 | cpumask_var_t *node_to_cpumask, |
| 100 | const struct cpumask *cpu_mask, |
| 101 | struct cpumask *nmsk, |
| 102 | struct cpumask *masks) |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 103 | { |
Ming Lei | 1a2d091 | 2018-03-08 18:53:57 +0800 | [diff] [blame^] | 104 | int n, nodes, cpus_per_vec, extra_vecs, done = 0; |
| 105 | int last_affv = affd->pre_vectors + numvecs; |
| 106 | int curvec = startvec; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 107 | nodemask_t nodemsk = NODE_MASK_NONE; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 108 | |
Ming Lei | b3e6aaa | 2018-03-08 18:53:56 +0800 | [diff] [blame] | 109 | nodes = get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk); |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 110 | |
| 111 | /* |
Guilherme G. Piccoli | c0af524 | 2016-12-14 16:01:12 -0200 | [diff] [blame] | 112 | * If the number of nodes in the mask is greater than or equal the |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 113 | * number of vectors we just spread the vectors across the nodes. |
| 114 | */ |
Ming Lei | 1a2d091 | 2018-03-08 18:53:57 +0800 | [diff] [blame^] | 115 | if (numvecs <= nodes) { |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 116 | for_each_node_mask(n, nodemsk) { |
Ming Lei | 1a2d091 | 2018-03-08 18:53:57 +0800 | [diff] [blame^] | 117 | cpumask_copy(masks + curvec, node_to_cpumask[n]); |
| 118 | if (++done == numvecs) |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 119 | break; |
Ming Lei | 1a2d091 | 2018-03-08 18:53:57 +0800 | [diff] [blame^] | 120 | if (++curvec == last_affv) |
| 121 | curvec = affd->pre_vectors; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 122 | } |
Ming Lei | b3e6aaa | 2018-03-08 18:53:56 +0800 | [diff] [blame] | 123 | goto out; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 126 | for_each_node_mask(n, nodemsk) { |
Keith Busch | 7bf8222 | 2017-04-03 15:25:53 -0400 | [diff] [blame] | 127 | int ncpus, v, vecs_to_assign, vecs_per_node; |
| 128 | |
| 129 | /* Spread the vectors per node */ |
Ming Lei | 1a2d091 | 2018-03-08 18:53:57 +0800 | [diff] [blame^] | 130 | vecs_per_node = (numvecs - (curvec - affd->pre_vectors)) / nodes; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 131 | |
| 132 | /* Get the cpus on this node which are in the mask */ |
Ming Lei | b3e6aaa | 2018-03-08 18:53:56 +0800 | [diff] [blame] | 133 | cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]); |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 134 | |
| 135 | /* Calculate the number of cpus per vector */ |
| 136 | ncpus = cpumask_weight(nmsk); |
Keith Busch | 7bf8222 | 2017-04-03 15:25:53 -0400 | [diff] [blame] | 137 | vecs_to_assign = min(vecs_per_node, ncpus); |
| 138 | |
| 139 | /* Account for rounding errors */ |
Keith Busch | 3412386 | 2017-04-13 13:28:12 -0400 | [diff] [blame] | 140 | extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign); |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 141 | |
Christoph Hellwig | bfe1307 | 2016-11-15 10:12:58 +0100 | [diff] [blame] | 142 | for (v = 0; curvec < last_affv && v < vecs_to_assign; |
| 143 | curvec++, v++) { |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 144 | cpus_per_vec = ncpus / vecs_to_assign; |
| 145 | |
| 146 | /* Account for extra vectors to compensate rounding errors */ |
| 147 | if (extra_vecs) { |
| 148 | cpus_per_vec++; |
Keith Busch | 7bf8222 | 2017-04-03 15:25:53 -0400 | [diff] [blame] | 149 | --extra_vecs; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 150 | } |
| 151 | irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec); |
| 152 | } |
| 153 | |
Ming Lei | 1a2d091 | 2018-03-08 18:53:57 +0800 | [diff] [blame^] | 154 | done += v; |
| 155 | if (done >= numvecs) |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 156 | break; |
Ming Lei | 1a2d091 | 2018-03-08 18:53:57 +0800 | [diff] [blame^] | 157 | if (curvec >= last_affv) |
| 158 | curvec = affd->pre_vectors; |
Keith Busch | 7bf8222 | 2017-04-03 15:25:53 -0400 | [diff] [blame] | 159 | --nodes; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Ming Lei | b3e6aaa | 2018-03-08 18:53:56 +0800 | [diff] [blame] | 162 | out: |
Ming Lei | 1a2d091 | 2018-03-08 18:53:57 +0800 | [diff] [blame^] | 163 | return done; |
Ming Lei | b3e6aaa | 2018-03-08 18:53:56 +0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * irq_create_affinity_masks - Create affinity masks for multiqueue spreading |
| 168 | * @nvecs: The total number of vectors |
| 169 | * @affd: Description of the affinity requirements |
| 170 | * |
| 171 | * Returns the masks pointer or NULL if allocation failed. |
| 172 | */ |
| 173 | struct cpumask * |
| 174 | irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd) |
| 175 | { |
Ming Lei | 1a2d091 | 2018-03-08 18:53:57 +0800 | [diff] [blame^] | 176 | int curvec, affvecs = nvecs - affd->pre_vectors - affd->post_vectors; |
Ming Lei | b3e6aaa | 2018-03-08 18:53:56 +0800 | [diff] [blame] | 177 | cpumask_var_t nmsk, *node_to_cpumask; |
| 178 | struct cpumask *masks = NULL; |
Ming Lei | b3e6aaa | 2018-03-08 18:53:56 +0800 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * If there aren't any vectors left after applying the pre/post |
| 182 | * vectors don't bother with assigning affinity. |
| 183 | */ |
| 184 | if (nvecs == affd->pre_vectors + affd->post_vectors) |
| 185 | return NULL; |
| 186 | |
| 187 | if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL)) |
| 188 | return NULL; |
| 189 | |
| 190 | node_to_cpumask = alloc_node_to_cpumask(); |
| 191 | if (!node_to_cpumask) |
| 192 | goto outcpumsk; |
| 193 | |
| 194 | masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL); |
| 195 | if (!masks) |
| 196 | goto outnodemsk; |
| 197 | |
| 198 | /* Fill out vectors at the beginning that don't need affinity */ |
| 199 | for (curvec = 0; curvec < affd->pre_vectors; curvec++) |
| 200 | cpumask_copy(masks + curvec, irq_default_affinity); |
| 201 | |
| 202 | /* Stabilize the cpumasks */ |
| 203 | get_online_cpus(); |
| 204 | build_node_to_cpumask(node_to_cpumask); |
Ming Lei | 1a2d091 | 2018-03-08 18:53:57 +0800 | [diff] [blame^] | 205 | curvec += irq_build_affinity_masks(affd, curvec, affvecs, |
| 206 | node_to_cpumask, cpu_possible_mask, |
| 207 | nmsk, masks); |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 208 | put_online_cpus(); |
Christoph Hellwig | 67c93c2 | 2016-11-08 17:15:03 -0800 | [diff] [blame] | 209 | |
| 210 | /* Fill out vectors at the end that don't need affinity */ |
| 211 | for (; curvec < nvecs; curvec++) |
Thomas Gleixner | b6e5d5b | 2016-11-16 18:36:44 +0100 | [diff] [blame] | 212 | cpumask_copy(masks + curvec, irq_default_affinity); |
Thomas Gleixner | 0211e12 | 2018-04-04 12:40:07 +0200 | [diff] [blame] | 213 | outnodemsk: |
Ming Lei | 47778f33 | 2018-03-08 18:53:55 +0800 | [diff] [blame] | 214 | free_node_to_cpumask(node_to_cpumask); |
Thomas Gleixner | 0211e12 | 2018-04-04 12:40:07 +0200 | [diff] [blame] | 215 | outcpumsk: |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 216 | free_cpumask_var(nmsk); |
| 217 | return masks; |
| 218 | } |
| 219 | |
| 220 | /** |
Christoph Hellwig | 212bd84 | 2016-11-08 17:15:02 -0800 | [diff] [blame] | 221 | * irq_calc_affinity_vectors - Calculate the optimal number of vectors |
Michael Hernandez | 6f9a22b | 2017-05-18 10:47:47 -0700 | [diff] [blame] | 222 | * @minvec: The minimum number of vectors available |
Christoph Hellwig | 212bd84 | 2016-11-08 17:15:02 -0800 | [diff] [blame] | 223 | * @maxvec: The maximum number of vectors available |
| 224 | * @affd: Description of the affinity requirements |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 225 | */ |
Michael Hernandez | 6f9a22b | 2017-05-18 10:47:47 -0700 | [diff] [blame] | 226 | int irq_calc_affinity_vectors(int minvec, int maxvec, const struct irq_affinity *affd) |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 227 | { |
Christoph Hellwig | 212bd84 | 2016-11-08 17:15:02 -0800 | [diff] [blame] | 228 | int resv = affd->pre_vectors + affd->post_vectors; |
| 229 | int vecs = maxvec - resv; |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 230 | int ret; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 231 | |
Michael Hernandez | 6f9a22b | 2017-05-18 10:47:47 -0700 | [diff] [blame] | 232 | if (resv > minvec) |
| 233 | return 0; |
| 234 | |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 235 | get_online_cpus(); |
Christoph Hellwig | 84676c1 | 2018-01-12 10:53:05 +0800 | [diff] [blame] | 236 | ret = min_t(int, cpumask_weight(cpu_possible_mask), vecs) + resv; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 237 | put_online_cpus(); |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 238 | return ret; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 239 | } |