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 | |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 42 | static cpumask_var_t *alloc_node_to_present_cpumask(void) |
| 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 | |
| 65 | static void free_node_to_present_cpumask(cpumask_var_t *masks) |
| 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 | |
| 74 | static void build_node_to_present_cpumask(cpumask_var_t *masks) |
| 75 | { |
| 76 | int cpu; |
| 77 | |
| 78 | for_each_present_cpu(cpu) |
| 79 | cpumask_set_cpu(cpu, masks[cpu_to_node(cpu)]); |
| 80 | } |
| 81 | |
| 82 | static int get_nodes_in_cpumask(cpumask_var_t *node_to_present_cpumask, |
| 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) { |
| 89 | if (cpumask_intersects(mask, node_to_present_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 | |
| 97 | /** |
| 98 | * irq_create_affinity_masks - Create affinity masks for multiqueue spreading |
Christoph Hellwig | 67c93c2 | 2016-11-08 17:15:03 -0800 | [diff] [blame] | 99 | * @nvecs: The total number of vectors |
| 100 | * @affd: Description of the affinity requirements |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 101 | * |
| 102 | * Returns the masks pointer or NULL if allocation failed. |
| 103 | */ |
Christoph Hellwig | 67c93c2 | 2016-11-08 17:15:03 -0800 | [diff] [blame] | 104 | struct cpumask * |
| 105 | irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd) |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 106 | { |
Keith Busch | 7bf8222 | 2017-04-03 15:25:53 -0400 | [diff] [blame] | 107 | int n, nodes, cpus_per_vec, extra_vecs, curvec; |
Christoph Hellwig | 67c93c2 | 2016-11-08 17:15:03 -0800 | [diff] [blame] | 108 | int affv = nvecs - affd->pre_vectors - affd->post_vectors; |
Christoph Hellwig | bfe1307 | 2016-11-15 10:12:58 +0100 | [diff] [blame] | 109 | int last_affv = affv + affd->pre_vectors; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 110 | nodemask_t nodemsk = NODE_MASK_NONE; |
| 111 | struct cpumask *masks; |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 112 | cpumask_var_t nmsk, *node_to_present_cpumask; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 113 | |
Michael Hernandez | 6f9a22b | 2017-05-18 10:47:47 -0700 | [diff] [blame] | 114 | /* |
| 115 | * If there aren't any vectors left after applying the pre/post |
| 116 | * vectors don't bother with assigning affinity. |
| 117 | */ |
| 118 | if (!affv) |
| 119 | return NULL; |
| 120 | |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 121 | if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL)) |
| 122 | return NULL; |
| 123 | |
Christoph Hellwig | 67c93c2 | 2016-11-08 17:15:03 -0800 | [diff] [blame] | 124 | masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL); |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 125 | if (!masks) |
| 126 | goto out; |
| 127 | |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 128 | node_to_present_cpumask = alloc_node_to_present_cpumask(); |
| 129 | if (!node_to_present_cpumask) |
| 130 | goto out; |
| 131 | |
Christoph Hellwig | 67c93c2 | 2016-11-08 17:15:03 -0800 | [diff] [blame] | 132 | /* Fill out vectors at the beginning that don't need affinity */ |
| 133 | for (curvec = 0; curvec < affd->pre_vectors; curvec++) |
Thomas Gleixner | b6e5d5b | 2016-11-16 18:36:44 +0100 | [diff] [blame] | 134 | cpumask_copy(masks + curvec, irq_default_affinity); |
Christoph Hellwig | 67c93c2 | 2016-11-08 17:15:03 -0800 | [diff] [blame] | 135 | |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 136 | /* Stabilize the cpumasks */ |
| 137 | get_online_cpus(); |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 138 | build_node_to_present_cpumask(node_to_present_cpumask); |
| 139 | nodes = get_nodes_in_cpumask(node_to_present_cpumask, cpu_present_mask, |
| 140 | &nodemsk); |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 141 | |
| 142 | /* |
Guilherme G. Piccoli | c0af524 | 2016-12-14 16:01:12 -0200 | [diff] [blame] | 143 | * 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] | 144 | * number of vectors we just spread the vectors across the nodes. |
| 145 | */ |
Christoph Hellwig | 67c93c2 | 2016-11-08 17:15:03 -0800 | [diff] [blame] | 146 | if (affv <= nodes) { |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 147 | for_each_node_mask(n, nodemsk) { |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 148 | cpumask_copy(masks + curvec, |
| 149 | node_to_present_cpumask[n]); |
Christoph Hellwig | bfe1307 | 2016-11-15 10:12:58 +0100 | [diff] [blame] | 150 | if (++curvec == last_affv) |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 151 | break; |
| 152 | } |
Christoph Hellwig | 67c93c2 | 2016-11-08 17:15:03 -0800 | [diff] [blame] | 153 | goto done; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 156 | for_each_node_mask(n, nodemsk) { |
Keith Busch | 7bf8222 | 2017-04-03 15:25:53 -0400 | [diff] [blame] | 157 | int ncpus, v, vecs_to_assign, vecs_per_node; |
| 158 | |
| 159 | /* Spread the vectors per node */ |
Keith Busch | b72f805 | 2017-04-19 19:51:10 -0400 | [diff] [blame] | 160 | vecs_per_node = (affv - (curvec - affd->pre_vectors)) / nodes; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 161 | |
| 162 | /* Get the cpus on this node which are in the mask */ |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 163 | cpumask_and(nmsk, cpu_present_mask, node_to_present_cpumask[n]); |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 164 | |
| 165 | /* Calculate the number of cpus per vector */ |
| 166 | ncpus = cpumask_weight(nmsk); |
Keith Busch | 7bf8222 | 2017-04-03 15:25:53 -0400 | [diff] [blame] | 167 | vecs_to_assign = min(vecs_per_node, ncpus); |
| 168 | |
| 169 | /* Account for rounding errors */ |
Keith Busch | 3412386 | 2017-04-13 13:28:12 -0400 | [diff] [blame] | 170 | extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign); |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 171 | |
Christoph Hellwig | bfe1307 | 2016-11-15 10:12:58 +0100 | [diff] [blame] | 172 | for (v = 0; curvec < last_affv && v < vecs_to_assign; |
| 173 | curvec++, v++) { |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 174 | cpus_per_vec = ncpus / vecs_to_assign; |
| 175 | |
| 176 | /* Account for extra vectors to compensate rounding errors */ |
| 177 | if (extra_vecs) { |
| 178 | cpus_per_vec++; |
Keith Busch | 7bf8222 | 2017-04-03 15:25:53 -0400 | [diff] [blame] | 179 | --extra_vecs; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 180 | } |
| 181 | irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec); |
| 182 | } |
| 183 | |
Christoph Hellwig | bfe1307 | 2016-11-15 10:12:58 +0100 | [diff] [blame] | 184 | if (curvec >= last_affv) |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 185 | break; |
Keith Busch | 7bf8222 | 2017-04-03 15:25:53 -0400 | [diff] [blame] | 186 | --nodes; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Christoph Hellwig | 67c93c2 | 2016-11-08 17:15:03 -0800 | [diff] [blame] | 189 | done: |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 190 | put_online_cpus(); |
Christoph Hellwig | 67c93c2 | 2016-11-08 17:15:03 -0800 | [diff] [blame] | 191 | |
| 192 | /* Fill out vectors at the end that don't need affinity */ |
| 193 | for (; curvec < nvecs; curvec++) |
Thomas Gleixner | b6e5d5b | 2016-11-16 18:36:44 +0100 | [diff] [blame] | 194 | cpumask_copy(masks + curvec, irq_default_affinity); |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 195 | free_node_to_present_cpumask(node_to_present_cpumask); |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 196 | out: |
| 197 | free_cpumask_var(nmsk); |
| 198 | return masks; |
| 199 | } |
| 200 | |
| 201 | /** |
Christoph Hellwig | 212bd84 | 2016-11-08 17:15:02 -0800 | [diff] [blame] | 202 | * irq_calc_affinity_vectors - Calculate the optimal number of vectors |
Michael Hernandez | 6f9a22b | 2017-05-18 10:47:47 -0700 | [diff] [blame] | 203 | * @minvec: The minimum number of vectors available |
Christoph Hellwig | 212bd84 | 2016-11-08 17:15:02 -0800 | [diff] [blame] | 204 | * @maxvec: The maximum number of vectors available |
| 205 | * @affd: Description of the affinity requirements |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 206 | */ |
Michael Hernandez | 6f9a22b | 2017-05-18 10:47:47 -0700 | [diff] [blame] | 207 | 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] | 208 | { |
Christoph Hellwig | 212bd84 | 2016-11-08 17:15:02 -0800 | [diff] [blame] | 209 | int resv = affd->pre_vectors + affd->post_vectors; |
| 210 | int vecs = maxvec - resv; |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 211 | int ret; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 212 | |
Michael Hernandez | 6f9a22b | 2017-05-18 10:47:47 -0700 | [diff] [blame] | 213 | if (resv > minvec) |
| 214 | return 0; |
| 215 | |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 216 | get_online_cpus(); |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 217 | ret = min_t(int, cpumask_weight(cpu_present_mask), vecs) + resv; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 218 | put_online_cpus(); |
Christoph Hellwig | 9a0ef98 | 2017-06-20 01:37:55 +0200 | [diff] [blame] | 219 | return ret; |
Thomas Gleixner | 34c3d98 | 2016-09-14 16:18:48 +0200 | [diff] [blame] | 220 | } |