函数源码

Linux Kernel

v5.5.9

Brick Technologies Co., Ltd

Source File:kernel\irq\affinity.c Create Date:2022-07-27 11:16:13
首页 Copyright©Brick

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Allocate vector number for each node, so that for each node:
 *
 * 1) the allocated number is >= 1
 *
 * 2) the allocated numbver is <= active CPU number of this node
 *
 * The actual allocated total vectors may be less than @numvecs when
 * active total CPU number is less than @numvecs.
 *
 * Active CPUs means the CPUs in '@cpu_mask AND @node_to_cpumask[]'
 * for each node.
 */
static void alloc_nodes_vectors(unsigned int numvecs,
                cpumask_var_t *node_to_cpumask,
                const struct cpumask *cpu_mask,
                const nodemask_t nodemsk,
                struct cpumask *nmsk,
                struct node_vectors *node_vectors)
{
    unsigned n, remaining_ncpus = 0;
 
    for (n = 0; n < nr_node_ids; n++) {
        node_vectors[n].id = n;
        node_vectors[n].ncpus = UINT_MAX;
    }
 
    for_each_node_mask(n, nodemsk) {
        unsigned ncpus;
 
        cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
        ncpus = cpumask_weight(nmsk);
 
        if (!ncpus)
            continue;
        remaining_ncpus += ncpus;
        node_vectors[n].ncpus = ncpus;
    }
 
    numvecs = min_t(unsigned, remaining_ncpus, numvecs);
 
    sort(node_vectors, nr_node_ids, sizeof(node_vectors[0]),
         ncpus_cmp_func, NULL);
 
    /*
     * Allocate vectors for each node according to the ratio of this
     * node's nr_cpus to remaining un-assigned ncpus. 'numvecs' is
     * bigger than number of active numa nodes. Always start the
     * allocation from the node with minimized nr_cpus.
     *
     * This way guarantees that each active node gets allocated at
     * least one vector, and the theory is simple: over-allocation
     * is only done when this node is assigned by one vector, so
     * other nodes will be allocated >= 1 vector, since 'numvecs' is
     * bigger than number of numa nodes.
     *
     * One perfect invariant is that number of allocated vectors for
     * each node is <= CPU count of this node:
     *
     * 1) suppose there are two nodes: A and B
     *  ncpu(X) is CPU count of node X
     *  vecs(X) is the vector count allocated to node X via this
     *  algorithm
     *
     *  ncpu(A) <= ncpu(B)
     *  ncpu(A) + ncpu(B) = N
     *  vecs(A) + vecs(B) = V
     *
     *  vecs(A) = max(1, round_down(V * ncpu(A) / N))
     *  vecs(B) = V - vecs(A)
     *
     *  both N and V are integer, and 2 <= V <= N, suppose
     *  V = N - delta, and 0 <= delta <= N - 2
     *
     * 2) obviously vecs(A) <= ncpu(A) because:
     *
     *  if vecs(A) is 1, then vecs(A) <= ncpu(A) given
     *  ncpu(A) >= 1
     *
     *  otherwise,
     *      vecs(A) <= V * ncpu(A) / N <= ncpu(A), given V <= N
     *
     * 3) prove how vecs(B) <= ncpu(B):
     *
     *  if round_down(V * ncpu(A) / N) == 0, vecs(B) won't be
     *  over-allocated, so vecs(B) <= ncpu(B),
     *
     *  otherwise:
     *
     *  vecs(A) =
     *      round_down(V * ncpu(A) / N) =
     *      round_down((N - delta) * ncpu(A) / N) =
     *      round_down((N * ncpu(A) - delta * ncpu(A)) / N)  >=
     *      round_down((N * ncpu(A) - delta * N) / N)    =
     *      cpu(A) - delta
     *
     *  then:
     *
     *  vecs(A) - V >= ncpu(A) - delta - V
     *  =>
     *  V - vecs(A) <= V + delta - ncpu(A)
     *  =>
     *  vecs(B) <= N - ncpu(A)
     *  =>
     *  vecs(B) <= cpu(B)
     *
     * For nodes >= 3, it can be thought as one node and another big
     * node given that is exactly what this algorithm is implemented,
     * and we always re-calculate 'remaining_ncpus' & 'numvecs', and
     * finally for each node X: vecs(X) <= ncpu(X).
     *
     */
    for (n = 0; n < nr_node_ids; n++) {
        unsigned nvectors, ncpus;
 
        if (node_vectors[n].ncpus == UINT_MAX)
            continue;
 
        WARN_ON_ONCE(numvecs == 0);
 
        ncpus = node_vectors[n].ncpus;
        nvectors = max_t(unsigned, 1,
                 numvecs * ncpus / remaining_ncpus);
        WARN_ON_ONCE(nvectors > ncpus);
 
        node_vectors[n].nvectors = nvectors;
 
        remaining_ncpus -= ncpus;
        numvecs -= nvectors;
    }
}