Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
4e0473f
Documentation
LICENSES
arch
block
certs
crypto
drivers
fs
include
init
io_uring
ipc
kernel
lib
842
crypto
dim
fonts
kunit
livepatch
lz4
lzo
math
mpi
pldmfw
raid6
reed_solomon
test_fortify
vdso
xz
zlib_deflate
zlib_dfltcc
zlib_inflate
zstd
.gitignore
Kconfig
Kconfig.debug
Kconfig.kasan
Kconfig.kcsan
Kconfig.kfence
Kconfig.kgdb
Kconfig.kmsan
Kconfig.ubsan
Makefile
argv_split.c
ashldi3.c
ashrdi3.c
asn1_decoder.c
asn1_encoder.c
assoc_array.c
atomic64.c
atomic64_test.c
audit.c
base64.c
bcd.c
bch.c
bitfield_kunit.c
bitmap.c
bitrev.c
bootconfig-data.S
bootconfig.c
bsearch.c
btree.c
bucket_locks.c
bug.c
build_OID_registry
buildid.c
bust_spinlocks.c
check_signature.c
checksum.c
clz_ctz.c
clz_tab.c
cmdline.c
cmdline_kunit.c
cmpdi2.c
compat_audit.c
cpu_rmap.c
cpumask.c
cpumask_kunit.c
crc-ccitt.c
crc-itu-t.c
crc-t10dif.c
crc16.c
crc32.c
crc32defs.h
crc32test.c
crc4.c
crc64-rocksoft.c
crc64.c
crc7.c
crc8.c
ctype.c
debug_info.c
debug_locks.c
debugobjects.c
dec_and_lock.c
decompress.c
decompress_bunzip2.c
decompress_inflate.c
decompress_unlz4.c
decompress_unlzma.c
decompress_unlzo.c
decompress_unxz.c
decompress_unzstd.c
devmem_is_allowed.c
devres.c
dhry.h
dhry_1.c
dhry_2.c
dhry_run.c
digsig.c
dump_stack.c
dynamic_debug.c
dynamic_queue_limits.c
earlycpio.c
errname.c
error-inject.c
errseq.c
extable.c
fault-inject-usercopy.c
fault-inject.c
fdt.c
fdt_addresses.c
fdt_empty_tree.c
fdt_ro.c
fdt_rw.c
fdt_strerror.c
fdt_sw.c
fdt_wip.c
find_bit.c
find_bit_benchmark.c
flex_proportions.c
fortify_kunit.c
gen_crc32table.c
gen_crc64table.c
genalloc.c
generic-radix-tree.c
glob.c
globtest.c
group_cpus.c
hashtable_test.c
hexdump.c
hweight.c
idr.c
inflate.c
interval_tree.c
interval_tree_test.c
iomap.c
iomap_copy.c
iommu-helper.c
iov_iter.c
irq_poll.c
irq_regs.c
is_signed_type_kunit.c
is_single_threaded.c
kasprintf.c
kfifo.c
klist.c
kobject.c
kobject_uevent.c
kstrtox.c
kstrtox.h
libcrc32c.c
linear_ranges.c
list-test.c
list_debug.c
list_sort.c
llist.c
locking-selftest-hardirq.h
locking-selftest-mutex.h
locking-selftest-rlock-hardirq.h
locking-selftest-rlock-softirq.h
locking-selftest-rlock.h
locking-selftest-rsem.h
locking-selftest-rtmutex.h
locking-selftest-softirq.h
locking-selftest-spin-hardirq.h
locking-selftest-spin-softirq.h
locking-selftest-spin.h
locking-selftest-wlock-hardirq.h
locking-selftest-wlock-softirq.h
locking-selftest-wlock.h
locking-selftest-wsem.h
locking-selftest.c
lockref.c
logic_iomem.c
logic_pio.c
lru_cache.c
lshrdi3.c
maple_tree.c
memcat_p.c
memcpy_kunit.c
memory-notifier-error-inject.c
memregion.c
memweight.c
muldi3.c
net_utils.c
netdev-notifier-error-inject.c
nlattr.c
nmi_backtrace.c
notifier-error-inject.c
notifier-error-inject.h
objagg.c
of-reconfig-notifier-error-inject.c
oid_registry.c
once.c
overflow_kunit.c
packing.c
parman.c
parser.c
pci_iomap.c
percpu-refcount.c
percpu_counter.c
percpu_test.c
plist.c
pm-notifier-error-inject.c
polynomial.c
radix-tree.c
random32.c
ratelimit.c
rbtree.c
rbtree_test.c
ref_tracker.c
refcount.c
rhashtable.c
sbitmap.c
scatterlist.c
seq_buf.c
sg_pool.c
sg_split.c
show_mem.c
siphash.c
siphash_kunit.c
slub_kunit.c
smp_processor_id.c
sort.c
stackdepot.c
stackinit_kunit.c
stmp_device.c
string.c
string_helpers.c
strncpy_from_user.c
strnlen_user.c
strscpy_kunit.c
syscall.c
test-kstrtox.c
test-string_helpers.c
test_bitmap.c
test_bitops.c
test_bits.c
test_blackhole_dev.c
test_bpf.c
test_debug_virtual.c
test_dynamic_debug.c
test_firmware.c
test_fprobe.c
test_fpu.c
test_free_pages.c
test_hash.c
test_hexdump.c
test_hmm.c
test_hmm_uapi.h
test_ida.c
test_kmod.c
test_kprobes.c
test_linear_ranges.c
test_list_sort.c
test_lockup.c
test_maple_tree.c
test_memcat_p.c
test_meminit.c
test_min_heap.c
test_module.c
test_objagg.c
test_parman.c
test_printf.c
test_ref_tracker.c
test_rhashtable.c
test_scanf.c
test_sort.c
test_static_key_base.c
test_static_keys.c
test_string.c
test_sysctl.c
test_ubsan.c
test_user_copy.c
test_uuid.c
test_vmalloc.c
test_xarray.c
textsearch.c
timerqueue.c
trace_readwrite.c
ts_bm.c
ts_fsm.c
ts_kmp.c
ubsan.c
ubsan.h
ucmpdi2.c
ucs2_string.c
usercopy.c
uuid.c
vsprintf.c
win_minmax.c
xarray.c
xxhash.c
mm
net
rust
samples
scripts
security
sound
tools
usr
virt
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
.rustfmt.toml
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
lib
/
cpu_rmap.c
Blame
Blame
Latest commit
History
History
307 lines (267 loc) · 7.67 KB
Breadcrumbs
linux
/
lib
/
cpu_rmap.c
Top
File metadata and controls
Code
Blame
307 lines (267 loc) · 7.67 KB
Raw
// SPDX-License-Identifier: GPL-2.0-only /* * cpu_rmap.c: CPU affinity reverse-map support * Copyright 2011 Solarflare Communications Inc. */ #include <linux/cpu_rmap.h> #include <linux/interrupt.h> #include <linux/export.h> /* * These functions maintain a mapping from CPUs to some ordered set of * objects with CPU affinities. This can be seen as a reverse-map of * CPU affinity. However, we do not assume that the object affinities * cover all CPUs in the system. For those CPUs not directly covered * by object affinities, we attempt to find a nearest object based on * CPU topology. */ /** * alloc_cpu_rmap - allocate CPU affinity reverse-map * @size: Number of objects to be mapped * @flags: Allocation flags e.g. %GFP_KERNEL */ struct cpu_rmap *alloc_cpu_rmap(unsigned int size, gfp_t flags) { struct cpu_rmap *rmap; unsigned int cpu; size_t obj_offset; /* This is a silly number of objects, and we use u16 indices. */ if (size > 0xffff) return NULL; /* Offset of object pointer array from base structure */ obj_offset = ALIGN(offsetof(struct cpu_rmap, near[nr_cpu_ids]), sizeof(void *)); rmap = kzalloc(obj_offset + size * sizeof(rmap->obj[0]), flags); if (!rmap) return NULL; kref_init(&rmap->refcount); rmap->obj = (void **)((char *)rmap + obj_offset); /* Initially assign CPUs to objects on a rota, since we have * no idea where the objects are. Use infinite distance, so * any object with known distance is preferable. Include the * CPUs that are not present/online, since we definitely want * any newly-hotplugged CPUs to have some object assigned. */ for_each_possible_cpu(cpu) { rmap->near[cpu].index = cpu % size; rmap->near[cpu].dist = CPU_RMAP_DIST_INF; } rmap->size = size; return rmap; } EXPORT_SYMBOL(alloc_cpu_rmap); /** * cpu_rmap_release - internal reclaiming helper called from kref_put * @ref: kref to struct cpu_rmap */ static void cpu_rmap_release(struct kref *ref) { struct cpu_rmap *rmap = container_of(ref, struct cpu_rmap, refcount); kfree(rmap); } /** * cpu_rmap_get - internal helper to get new ref on a cpu_rmap * @rmap: reverse-map allocated with alloc_cpu_rmap() */ static inline void cpu_rmap_get(struct cpu_rmap *rmap) { kref_get(&rmap->refcount); } /** * cpu_rmap_put - release ref on a cpu_rmap * @rmap: reverse-map allocated with alloc_cpu_rmap() */ int cpu_rmap_put(struct cpu_rmap *rmap) { return kref_put(&rmap->refcount, cpu_rmap_release); } EXPORT_SYMBOL(cpu_rmap_put); /* Reevaluate nearest object for given CPU, comparing with the given * neighbours at the given distance. */ static bool cpu_rmap_copy_neigh(struct cpu_rmap *rmap, unsigned int cpu, const struct cpumask *mask, u16 dist) { int neigh; for_each_cpu(neigh, mask) { if (rmap->near[cpu].dist > dist && rmap->near[neigh].dist <= dist) { rmap->near[cpu].index = rmap->near[neigh].index; rmap->near[cpu].dist = dist; return true; } } return false; } #ifdef DEBUG static void debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix) { unsigned index; unsigned int cpu; pr_info("cpu_rmap %p, %s:\n", rmap, prefix); for_each_possible_cpu(cpu) { index = rmap->near[cpu].index; pr_info("cpu %d -> obj %u (distance %u)\n", cpu, index, rmap->near[cpu].dist); } } #else static inline void debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix) { } #endif /** * cpu_rmap_add - add object to a rmap * @rmap: CPU rmap allocated with alloc_cpu_rmap() * @obj: Object to add to rmap * * Return index of object. */ int cpu_rmap_add(struct cpu_rmap *rmap, void *obj) { u16 index; BUG_ON(rmap->used >= rmap->size); index = rmap->used++; rmap->obj[index] = obj; return index; } EXPORT_SYMBOL(cpu_rmap_add); /** * cpu_rmap_update - update CPU rmap following a change of object affinity * @rmap: CPU rmap to update * @index: Index of object whose affinity changed * @affinity: New CPU affinity of object */ int cpu_rmap_update(struct cpu_rmap *rmap, u16 index, const struct cpumask *affinity) { cpumask_var_t update_mask; unsigned int cpu; if (unlikely(!zalloc_cpumask_var(&update_mask, GFP_KERNEL))) return -ENOMEM; /* Invalidate distance for all CPUs for which this used to be * the nearest object. Mark those CPUs for update. */ for_each_online_cpu(cpu) { if (rmap->near[cpu].index == index) { rmap->near[cpu].dist = CPU_RMAP_DIST_INF; cpumask_set_cpu(cpu, update_mask); } } debug_print_rmap(rmap, "after invalidating old distances"); /* Set distance to 0 for all CPUs in the new affinity mask. * Mark all CPUs within their NUMA nodes for update. */ for_each_cpu(cpu, affinity) { rmap->near[cpu].index = index; rmap->near[cpu].dist = 0; cpumask_or(update_mask, update_mask, cpumask_of_node(cpu_to_node(cpu))); } debug_print_rmap(rmap, "after updating neighbours"); /* Update distances based on topology */ for_each_cpu(cpu, update_mask) { if (cpu_rmap_copy_neigh(rmap, cpu, topology_sibling_cpumask(cpu), 1)) continue; if (cpu_rmap_copy_neigh(rmap, cpu, topology_core_cpumask(cpu), 2)) continue; if (cpu_rmap_copy_neigh(rmap, cpu, cpumask_of_node(cpu_to_node(cpu)), 3)) continue; /* We could continue into NUMA node distances, but for now * we give up. */ } debug_print_rmap(rmap, "after copying neighbours"); free_cpumask_var(update_mask); return 0; } EXPORT_SYMBOL(cpu_rmap_update); /* Glue between IRQ affinity notifiers and CPU rmaps */ struct irq_glue { struct irq_affinity_notify notify; struct cpu_rmap *rmap; u16 index; }; /** * free_irq_cpu_rmap - free a CPU affinity reverse-map used for IRQs * @rmap: Reverse-map allocated with alloc_irq_cpu_map(), or %NULL * * Must be called in process context, before freeing the IRQs. */ void free_irq_cpu_rmap(struct cpu_rmap *rmap) { struct irq_glue *glue; u16 index; if (!rmap) return; for (index = 0; index < rmap->used; index++) { glue = rmap->obj[index]; if (glue) irq_set_affinity_notifier(glue->notify.irq, NULL); } cpu_rmap_put(rmap); } EXPORT_SYMBOL(free_irq_cpu_rmap); /** * irq_cpu_rmap_notify - callback for IRQ subsystem when IRQ affinity updated * @notify: struct irq_affinity_notify passed by irq/manage.c * @mask: cpu mask for new SMP affinity * * This is executed in workqueue context. */ static void irq_cpu_rmap_notify(struct irq_affinity_notify *notify, const cpumask_t *mask) { struct irq_glue *glue = container_of(notify, struct irq_glue, notify); int rc; rc = cpu_rmap_update(glue->rmap, glue->index, mask); if (rc) pr_warn("irq_cpu_rmap_notify: update failed: %d\n", rc); } /** * irq_cpu_rmap_release - reclaiming callback for IRQ subsystem * @ref: kref to struct irq_affinity_notify passed by irq/manage.c */ static void irq_cpu_rmap_release(struct kref *ref) { struct irq_glue *glue = container_of(ref, struct irq_glue, notify.kref); cpu_rmap_put(glue->rmap); glue->rmap->obj[glue->index] = NULL; kfree(glue); } /** * irq_cpu_rmap_add - add an IRQ to a CPU affinity reverse-map * @rmap: The reverse-map * @irq: The IRQ number * * This adds an IRQ affinity notifier that will update the reverse-map * automatically. * * Must be called in process context, after the IRQ is allocated but * before it is bound with request_irq(). */ int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq) { struct irq_glue *glue = kzalloc(sizeof(*glue), GFP_KERNEL); int rc; if (!glue) return -ENOMEM; glue->notify.notify = irq_cpu_rmap_notify; glue->notify.release = irq_cpu_rmap_release; glue->rmap = rmap; cpu_rmap_get(rmap); glue->index = cpu_rmap_add(rmap, glue); rc = irq_set_affinity_notifier(irq, &glue->notify); if (rc) { cpu_rmap_put(glue->rmap); rmap->obj[glue->index] = NULL; kfree(glue); } return rc; } EXPORT_SYMBOL(irq_cpu_rmap_add);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
You can’t perform that action at this time.