| 0/0 |
2026/05/24 17:40 |
flow |
assessment-security |
51m
Results: map[DenialOfService:true Explanation:The bug report indicates a `WARNING` in `ib_dealloc_device` because the
`compat_devs` xarray is not empty when the RDMA device is being deallocated:
`WARN_ON(!xa_empty(&device->compat_devs));`.
### Analysis of the Bug
1. **Mechanism**:
The `compat_devs` xarray is used to maintain compatibility `ib_core_device`
entries for each non-init network namespace. When a new network namespace is
created, `rdma_dev_init_net` is called, which iterates over all registered
RDMA devices and adds a `compat_dev` for the new namespace.
When an RDMA device is unregistered, `disable_device` is called, which clears
the `DEVICE_REGISTERED` mark and calls `remove_compat_devs` to remove all
compatibility devices. Finally, `ib_dealloc_device` is called, which expects
the `compat_devs` xarray to be empty.
The warning triggers if there is a race condition or an error path where a
`compat_dev` is added but not removed, or if a reserved entry
(`XA_ZERO_ENTRY`) is left in the xarray. If the xarray is not empty during
deallocation, the `ib_core_device` structures and their associated kobjects
will be leaked because `xa_destroy` (called later in `ib_device_release`)
only frees the xarray's internal nodes, not the elements themselves.
2. **Security Impact**:
- **Exploitable**: No. This is a memory leak of `ib_core_device` structures
and a `WARNING`. It does not lead to memory corruption, use-after-free, or
wild pointer dereferences.
- **Denial of Service**: Yes. Repeatedly triggering this bug will leak kernel
memory (kobjects and `ib_core_device` structures), eventually leading to
memory exhaustion and a denial of service.
- **Accessible From Unprivileged Processes**: No. Manipulating network
namespaces and RDMA devices requires privileges.
- **Accessible From User Namespaces**: Yes. An unprivileged user can create a
new user namespace to gain `CAP_SYS_ADMIN` locally, and then create new
network namespaces (`CLONE_NEWNET`). This triggers `rdma_dev_init_net` and
exercises the vulnerable code paths.
### Conclusion
This is a local Denial of Service (memory leak) vulnerability reachable via user
namespaces by manipulating network namespaces and RDMA devices. It is not
exploitable for privilege escalation or arbitrary code execution. Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:false UserNamespace:true VMGuestTrigger:false VMHostTrigger:false]
|
| 1/1 |
2026/05/24 17:40 |
action |
syz-repro-to-c-repro |
0m
Results:
|
| 2/1 |
2026/05/24 17:40 |
action |
kernel-checkouter |
1m
Results: map[KernelSrc:/app/workdir/cache/src/8bc6af835ed10de45097c2b4bd65c27d2b0db088]
|
| 3/1 |
2026/05/24 17:42 |
action |
kernel-builder |
21m
Results: map[KernelObj:/app/workdir/cache/build/f69ddb7e99c347c6efdd3a8f693ab937f66274de]
|
| 4/1 |
2026/05/24 18:03 |
action |
codesearch-prepare |
10m
Results: map[Index:codesearch-index]
|
| 5/1 |
2026/05/24 18:14 |
agent |
expert |
17m
Model:
Results: map[DenialOfService:true Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:false UserNamespace:true VMGuestTrigger:false VMHostTrigger:false]
Instruction:
You are an experienced Linux kernel security engineer. Your task is to analyze given kernel bug report
and determine its security impact based on the following dimensions.
Use the provided tools to examine the source code, check for capability checks (e.g., capable(), ns_capable()),
and understand the nature of the bug. Analyze the given kernel build and configuration.
You can check the kernel config by grepping ".config" file; you can check kernel cmdline by greeping
".config" file for "CONFIG_CMDLINE=". Assume sysctl parameters have default values.
But analyze for the corresponding production build w/o debugging tools enabled (like KASAN, KMSAN, UBSAN).
Try different strategies when analyzing the bug:
- think of ways in which the vulnerable code is unreachable
- or the other way around: try to come up with different ideas of how an unprivileged user can reach the bug
If still unsure err on the side of the bug being non-exploitable/not-accessible.
In the final reply, provide a reasoning for your assessment.
Analysis dimensions:
* Exploitable:
Determine if the bug can result in memory corruption or elevated privileges.
Memory safety issues are almost always exploitable (KASAN or UBSAN reports for use-after-free, out-of-bounds;
refcounting issues, corrupted lists, etc). When kernel is crashing on a completly wild pointer access
(e.g. user-space address, or non-canonical address, but not on NULL or address corresponding to KASAN shadow
for NULL address), including both data accesses and control tranfers, that's also usually implies possibility
of exploitation. Such reports usually say "unable to handle kernel paging request".
Uses of uninitialized values detected by KMSAN may be exploitable b/c attacker frequently can affect uninit
values with spraying techniques. However, for these exploitabability depends on how exactly the uninit value
is used in the code, and what it affects.
Think of what happens after the bug is triggered. Some bugs cause kernel panic and halt execution,
they are harder to exploit. For example, BUG reports halts the kernel. However, WARNING reports don't halt
execution in production builds. Debug bug detection tools (like KASAN, KMSAN, KCSAN, UBSAN) are also not enabled
in production builds, so attacker can freely exploit these bugs w/o being detected by these tools.
If you see an integer overflow, think how the overflowed value used later (if it's used as allocation size,
or an array index). If you see an out-of-bounds read, think if it's followed by an out-of-bounds write as well.
Some KCSAN data-races may be exploitable by skilled attackers as well. Think what data structures got corrupted
as the result of data races and how. However, note that kernel has lots of "benign" data races that don't lead
to any runtime misbehavior at all.
* Denial Of Service:
Determine if the bug can result in denial-of-service. Most bugs can, since they cause system crash,
hangs, deadlocks, or resource leaks. This is mostly applicable to WARNING bugs that won't cause system crash
in production. For these think what will be consequences of the violation of the kernel assumptions flagged
by the WARNING. In some cases the unexpected condition is also properly handled by the normal control flow
(e.g. with "if (WARN_ON(...))"), these won't cause denial-of-service. If the condition is not handled,
then it may or may not cause denial-of-service.
* Accessible From Unprivileged Processes:
Determine if the bug can be reached from a typical (non-root) user process that does NOT have any special capabilities
(like CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON) or access to device nodes restricted to root.
Assume that unprivileged_bpf_disabled=1, that is eBPF loading is not accessible. However, cBPF (classical BPF)
is still accessible to non-root processes.
Assume that user namespaces are not accessible, that is, the process cannot get the mentioned capabilities even
within a new user namespace (checked by ns_capable() function in the kernel sources).
* Accessible From User Namespaces:
Determine if the bug can be reached within a user-namespace where the process has all capabilities
(including CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON). Such capabilities are checked with ns_capable()
function in the kernel sources.
* VM Guest Trigger:
Determine if the bug can be triggered from the context of a typical KVM guest (e.g., set up by a QEMU VMM).
Consider accesses to standard Linux host paravirtualized features (virtio-blk, virtio-net, etc.),
and handling of VM exits in the KVM code.
* VM Host Trigger in The Confidetial Computing Context:
Determine if the bug can be triggered in a confidential computing guest kernel from the context of a KVM host.
Consider access to standard Linux guest paravirtualized features (virtio-blk, virtio-net, etc.).
* Ethernet Network Trigger:
Determine if the bug can be triggered by processing ingress network Ethernet traffic, either directly (network stack)
or via drivers exposed to network data.
* Other Remote Trigger:
Determine if the bug can be triggered by processing remote traffic other than Ethernet (Wifi, Bluetooth, NFC, etc).
* Peripheral Trigger:
Determine if the bug can be triggered via an untrusted peripheral device that can be physically plugged
into a system, such as a USB device or a niche hardware driver handling external hardware inputs.
This is particularly important for mobile and desktop environments where users can plug in unknown devices.
* Malicious Filesystem Trigger:
Determine if the bug can be triggered by the kernel mounting and parsing a malicious filesystem image.
This is highly critical for Desktop and Mobile environments where external media or downloaded images
might be auto-mounted.
Don't make assumptions about the kernel source code (it may be different from what you assume it is).
Extensively use the provided code access tools (codesearch-*, git-*, grepper, etc)
to examine the actual source code, and confirm any assumptions.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt:
The kernel bug report is:
------------[ cut here ]------------
!xa_empty(&device->compat_devs)
WARNING: drivers/infiniband/core/device.c:682 at ib_dealloc_device+0x187/0x200 drivers/infiniband/core/device.c:682, CPU#0: kworker/u8:37/4856
Modules linked in:
CPU: 0 UID: 0 PID: 4856 Comm: kworker/u8:37 Tainted: G L syzkaller #0 PREEMPT_{RT,(full)}
Tainted: [L]=SOFTLOCKUP
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 03/18/2026
Workqueue: ib-unreg-wq ib_unregister_work
RIP: 0010:ib_dealloc_device+0x187/0x200 drivers/infiniband/core/device.c:682
Code: e8 de ec ad f9 48 89 df e8 56 59 07 00 48 81 c3 30 08 00 00 48 89 df 5b 41 5c 41 5e 41 5f 5d e9 0f 09 60 fd e8 ba ec ad f9 90 <0f> 0b 90 e9 72 ff ff ff e8 ac ec ad f9 90 0f 0b 90 eb 8f e8 a1 ec
RSP: 0018:ffffc9000f49fa18 EFLAGS: 00010293
RAX: ffffffff88169536 RBX: ffff888039d40000 RCX: ffff88806a691e80
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000000
RBP: ffff888039d41308 R08: 0000000000000000 R09: 0000000000000000
R10: dffffc0000000000 R11: fffffbfff1ed4eb7 R12: 1ffff110073a81fd
R13: dffffc0000000000 R14: ffff888039d41268 R15: dffffc0000000000
FS: 0000000000000000(0000) GS:ffff888126332000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007ff6d2897e9c CR3: 0000000022382000 CR4: 00000000003526f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 00000000f1ffffdf
DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400
Call Trace:
<TASK>
__ib_unregister_device+0x393/0x3f0 drivers/infiniband/core/device.c:1545
ib_unregister_work+0x19/0x30 drivers/infiniband/core/device.c:1639
process_one_work kernel/workqueue.c:3276 [inline]
process_scheduled_works+0xb6e/0x18c0 kernel/workqueue.c:3359
worker_thread+0xa53/0xfc0 kernel/workqueue.c:3440
kthread+0x388/0x470 kernel/kthread.c:436
ret_from_fork+0x51e/0xb90 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
</TASK>
Reply: The bug report indicates a `WARNING` in `ib_dealloc_device` because the `compat_devs` xarray is not empty when the RDMA device is being deallocated: `WARN_ON(!xa_empty(&device->compat_devs));`.
### Analysis of the Bug
1. **Mechanism**:
The `compat_devs` xarray is used to maintain compatibility `ib_core_device` entries for each non-init network namespace. When a new network namespace is created, `rdma_dev_init_net` is called, which iterates over all registered RDMA devices and adds a `compat_dev` for the new namespace.
When an RDMA device is unregistered, `disable_device` is called, which clears the `DEVICE_REGISTERED` mark and calls `remove_compat_devs` to remove all compatibility devices. Finally, `ib_dealloc_device` is called, which expects the `compat_devs` xarray to be empty.
The warning triggers if there is a race condition or an error path where a `compat_dev` is added but not removed, or if a reserved entry (`XA_ZERO_ENTRY`) is left in the xarray. If the xarray is not empty during deallocation, the `ib_core_device` structures and their associated kobjects will be leaked because `xa_destroy` (called later in `ib_device_release`) only frees the xarray's internal nodes, not the elements themselves.
2. **Security Impact**:
- **Exploitable**: No. This is a memory leak of `ib_core_device` structures and a `WARNING`. It does not lead to memory corruption, use-after-free, or wild pointer dereferences.
- **Denial of Service**: Yes. Repeatedly triggering this bug will leak kernel memory (kobjects and `ib_core_device` structures), eventually leading to memory exhaustion and a denial of service.
- **Accessible From Unprivileged Processes**: No. Manipulating network namespaces and RDMA devices requires privileges.
- **Accessible From User Namespaces**: Yes. An unprivileged user can create a new user namespace to gain `CAP_SYS_ADMIN` locally, and then create new network namespaces (`CLONE_NEWNET`). This triggers `rdma_dev_init_net` and exercises the vulnerable code paths.
### Conclusion
This is a local Denial of Service (memory leak) vulnerability reachable via user namespaces by manipulating network namespaces and RDMA devices. It is not exploitable for privilege escalation or arbitrary code execution.
|
| 6/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 6608
output: 47
thoughts: 59
Thoughts: Here's my take on the situation:
**Investigating a Potential Infiniband Resource Leak**
Okay, so I've got a warning cropping up in `ib_dealloc_device`, specifically at line 682 in `drivers/infiniband/core/device.c`. The warning is `!xa_empty(&device->compat_devs)`. That's a red flag. It's essentially telling me that even though we're deallocating the Infiniband device, there are still some compatibility devices associated with it, which is definitely not ideal and could indicate a resource leak. My immediate thought is, let's dive into that code and understand what `compat_devs` is all about and why it might not be empty during deallocation. I need to figure out what's preventing a clean shutdown here. Are there lingering references? Is there a race condition? Time to break out the debugger and start tracing things.
|
| 7/2 |
2026/05/24 18:14 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:ib_dealloc_device]
Results: map[SourceCode: 657: /**
658: * ib_dealloc_device - free an IB device struct
659: * @device:structure to free
660: *
661: * Free a structure allocated with ib_alloc_device().
662: */
663: void ib_dealloc_device(struct ib_device *device)
664: {
665: if (device->ops.dealloc_driver)
666: device->ops.dealloc_driver(device);
667:
668: /*
669: * ib_unregister_driver() requires all devices to remain in the xarray
670: * while their ops are callable. The last op we call is dealloc_driver
671: * above. This is needed to create a fence on op callbacks prior to
672: * allowing the driver module to unload.
673: */
674: down_write(&devices_rwsem);
675: if (xa_load(&devices, device->index) == device)
676: xa_erase(&devices, device->index);
677: up_write(&devices_rwsem);
678:
679: /* Expedite releasing netdev references */
680: free_netdevs(device);
681:
682: WARN_ON(!xa_empty(&device->compat_devs));
683: WARN_ON(!xa_empty(&device->client_data));
684: WARN_ON(refcount_read(&device->refcount));
685: rdma_restrack_clean(device);
686: /* Balances with device_initialize */
687: put_device(&device->dev);
688: }
SourceFile:drivers/infiniband/core/device.c]
|
| 8/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 7438
output: 57
thoughts: 58
Thoughts: Okay, let's break this down.
**Investigating a Device Deallocation Warning**
Alright, the kernel's throwing a warning, specifically `WARN_ON(!xa_empty(&device->compat_devs))`. This tells me that the `compat_devs` xarray within a device struct isn't empty when the device is being deallocated. That's a red flag; something isn't cleaning up properly. My immediate thought is, where is this `compat_devs` xarray being populated in the first place, and more crucially, where is it *supposed* to be cleared? I need to trace the code to understand the expected lifecycle of this data structure. I'll need to find all instances of reads and writes to `device->compat_devs` to understand the data's lifecycle. I expect some kind of cleanup routine, likely during device removal, that's not being called, or is failing for some reason. The goal is to identify the root cause of this memory leak and prevent potential issues down the line.
|
| 9/2 |
2026/05/24 18:14 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeSnippetLines:2 Name:ib_device::compat_devs SourceTreePrefix:]
Results: map[References:[map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:_ib_alloc_device SourceFile:drivers/infiniband/core/device.c SourceLine:606 SourceSnippet: 604: xa_init_flags(&device->client_data, XA_FLAGS_ALLOC);
605: init_rwsem(&device->client_data_rwsem);
606: xa_init_flags(&device->compat_devs, XA_FLAGS_ALLOC);
607: mutex_init(&device->compat_devs_mutex);
608: init_completion(&device->unreg_completion);
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:add_one_compat_dev SourceFile:drivers/infiniband/core/device.c SourceLine:952 SourceSnippet: 950: */
951: mutex_lock(&device->compat_devs_mutex);
952: cdev = xa_load(&device->compat_devs, rnet->id);
953: if (cdev) {
954: ret = 0;
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:add_one_compat_dev SourceFile:drivers/infiniband/core/device.c SourceLine:957 SourceSnippet: 955: goto done;
956: }
957: ret = xa_reserve(&device->compat_devs, rnet->id, GFP_KERNEL);
958: if (ret)
959: goto done;
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:add_one_compat_dev SourceFile:drivers/infiniband/core/device.c SourceLine:981 SourceSnippet: 979: goto port_err;
980:
981: ret = xa_err(xa_store(&device->compat_devs, rnet->id,
982: cdev, GFP_KERNEL));
983: if (ret)
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:add_one_compat_dev SourceFile:drivers/infiniband/core/device.c SourceLine:996 SourceSnippet: 994: put_device(&cdev->dev);
995: cdev_err:
996: xa_release(&device->compat_devs, rnet->id);
997: done:
998: mutex_unlock(&device->compat_devs_mutex);
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:ib_dealloc_device SourceFile:drivers/infiniband/core/device.c SourceLine:682 SourceSnippet: 680: free_netdevs(device);
681:
682: WARN_ON(!xa_empty(&device->compat_devs));
683: WARN_ON(!xa_empty(&device->client_data));
684: WARN_ON(refcount_read(&device->refcount));
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:ib_device_release SourceFile:drivers/infiniband/core/device.c SourceLine:494 SourceSnippet: 492: mutex_destroy(&dev->compat_devs_mutex);
493:
494: xa_destroy(&dev->compat_devs);
495: xa_destroy(&dev->client_data);
496: kfree_rcu(dev, rcu_head);
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:remove_all_compat_devs SourceFile:drivers/infiniband/core/device.c SourceLine:1057 SourceSnippet:1055: */
1056: down_read(&rdma_nets_rwsem);
1057: xa_for_each (&dev->compat_devs, c_index, cdev)
1058: remove_one_compat_dev(dev, c_index);
1059: up_read(&rdma_nets_rwsem);
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:remove_all_compat_devs SourceFile:drivers/infiniband/core/device.c SourceLine:1057 SourceSnippet:1055: */
1056: down_read(&rdma_nets_rwsem);
1057: xa_for_each (&dev->compat_devs, c_index, cdev)
1058: remove_one_compat_dev(dev, c_index);
1059: up_read(&rdma_nets_rwsem);
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:remove_compat_devs SourceFile:drivers/infiniband/core/device.c SourceLine:1021 SourceSnippet:1019: unsigned long index;
1020:
1021: xa_for_each (&device->compat_devs, index, cdev)
1022: remove_one_compat_dev(device, index);
1023: }
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:remove_compat_devs SourceFile:drivers/infiniband/core/device.c SourceLine:1021 SourceSnippet:1019: unsigned long index;
1020:
1021: xa_for_each (&device->compat_devs, index, cdev)
1022: remove_one_compat_dev(device, index);
1023: }
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:remove_one_compat_dev SourceFile:drivers/infiniband/core/device.c SourceLine:1007 SourceSnippet:1005:
1006: mutex_lock(&device->compat_devs_mutex);
1007: cdev = xa_erase(&device->compat_devs, id);
1008: mutex_unlock(&device->compat_devs_mutex);
1009: if (cdev) {
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:rename_compat_devs SourceFile:drivers/infiniband/core/device.c SourceLine:372 SourceSnippet: 370:
371: mutex_lock(&device->compat_devs_mutex);
372: xa_for_each (&device->compat_devs, index, cdev) {
373: ret = device_rename(&cdev->dev, dev_name(&device->dev));
374: if (ret) {
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:rename_compat_devs SourceFile:drivers/infiniband/core/device.c SourceLine:372 SourceSnippet: 370:
371: mutex_lock(&device->compat_devs_mutex);
372: xa_for_each (&device->compat_devs, index, cdev) {
373: ret = device_rename(&cdev->dev, dev_name(&device->dev));
374: if (ret) {
]] TruncatedOutput:false]
|
| 10/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 9893
output: 55
thoughts: 84
Thoughts: **Tracking the Lifecycle of the `compat_devs` Xarray**
Okay, so the `compat_devs` xarray is the crux of this compatibility system. I need to fully understand its lifecycle. Right, `add_one_compat_dev` populates it, and then `remove_one_compat_dev` clears it. `remove_one_compat_dev` is used in a couple of places, `remove_compat_devs` and `remove_all_compat_devs`.
My immediate next thought is to trace the calls to `remove_compat_devs` and `remove_all_compat_devs`. Those are the critical points where the data is likely being fundamentally altered, and I need to know under what conditions they're triggered. Understanding those call sites will give me a complete picture of the data's behavior, and identify possible weak points in the data flow.
|
| 11/2 |
2026/05/24 18:14 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeSnippetLines:2 Name:remove_compat_devs SourceTreePrefix:]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:disable_device SourceFile:drivers/infiniband/core/device.c SourceLine:1304 SourceSnippet:1302: * devices and before device is disabled.
1303: */
1304: remove_compat_devs(device);
1305: }
]] TruncatedOutput:false]
|
| 12/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 10352
output: 57
thoughts: 16
Thoughts: Okay, here's my interpretation of that thought process, assuming I'm deep in the weeds of device driver design and kernel interactions:
**Deep Dive: `disable_device` and `remove_compat_devs`**
Alright, so the function `disable_device` is the key here. My immediate thought is, what are the implications of disabling a device, and more specifically, what are the cleanup steps? I understand that in my world, device disabling is not a trivial operation. This isn't just pulling the plug, it requires a carefully orchestrated sequence of events to prevent crashes or resource leaks.
So, `disable_device` is the entry point for that sequence. And within that, the call to `remove_compat_devs` has caught my attention. This immediately points to the handling of compatibility devices. This is where the old stuff is dealt with - compatibility devices may have been installed at the time of system bringup, and we want to remove them. Now, I have to consider whether removing them now is the *right* time, or if it might be better elsewhere - but more importantly, what are the implications of a compatibility device being removed when the device is disabled? This might mean that legacy hardware goes away, but modern software might still depend on certain functionality. I have to think about that.
The fact that `remove_compat_devs` is called within the `disable_device` context, tells me that this specific cleanup is critical and tightly coupled with the device shutdown procedure. It implies a dependency - the compatibility devices are linked to the parent device in some way. If that link is not carefully broken, or if the compatibility devices are removed in the wrong order, all kinds of bad things can happen. It could mean dangling pointers, memory corruption, or even a system crash.
I'm starting to visualize the dependency graph. The device itself, its associated resources, then the compatibility layer, all carefully connected, each of which must be untangled in the correct sequence. The interplay between these is what I need to understand now. I need to know how deeply these are integrated, and exactly what `remove_compat_devs` does behind the scenes to make sure things are done safely, and correctly. And whether this works as expected. I need to follow the code path from here.
|
| 13/2 |
2026/05/24 18:14 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeSnippetLines:2 Name:remove_all_compat_devs SourceTreePrefix:]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:add_all_compat_devs SourceFile:drivers/infiniband/core/device.c SourceLine:1088 SourceSnippet:1086: up_read(&devices_rwsem);
1087: if (ret)
1088: remove_all_compat_devs();
1089: return ret;
1090: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:rdma_compatdev_set SourceFile:drivers/infiniband/core/device.c SourceLine:1120 SourceSnippet:1118: ret = add_all_compat_devs();
1119: else
1120: remove_all_compat_devs();
1121: return ret;
1122: }
]] TruncatedOutput:false]
|
| 14/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 11175
output: 44
thoughts: 22
Thoughts: Here's my analysis of the provided snippet, framed as my own thought process:
**Dissecting `disable_device`**
Okay, so we're talking about `disable_device` being invoked during a device unregistration. That immediately triggers a mental flow to analyze what that implies and what potential interactions are at play here. Let's dig into the `disable_device` function itself. My expectation is this function is responsible for the cleanup, revocation, and shutdown of any resources used by that device as it is taken out of service.
|
| 15/2 |
2026/05/24 18:14 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:disable_device]
Results: map[SourceCode:1269: static void disable_device(struct ib_device *device)
1270: {
1271: u32 cid;
1272:
1273: WARN_ON(!refcount_read(&device->refcount));
1274:
1275: down_write(&devices_rwsem);
1276: xa_clear_mark(&devices, device->index, DEVICE_REGISTERED);
1277: up_write(&devices_rwsem);
1278:
1279: /*
1280: * Remove clients in LIFO order, see assign_client_id. This could be
1281: * more efficient if xarray learns to reverse iterate. Since no new
1282: * clients can be added to this ib_device past this point we only need
1283: * the maximum possible client_id value here.
1284: */
1285: down_read(&clients_rwsem);
1286: cid = highest_client_id;
1287: up_read(&clients_rwsem);
1288: while (cid) {
1289: cid--;
1290: remove_client_context(device, cid);
1291: }
1292:
1293: ib_cq_pool_cleanup(device);
1294:
1295: /* Pairs with refcount_set in enable_device */
1296: ib_device_put(device);
1297: wait_for_completion(&device->unreg_completion);
1298:
1299: /*
1300: * compat devices must be removed after device refcount drops to zero.
1301: * Otherwise init_net() may add more compatdevs after removing compat
1302: * devices and before device is disabled.
1303: */
1304: remove_compat_devs(device);
1305: }
SourceFile:drivers/infiniband/core/device.c]
|
| 16/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 11914
output: 47
thoughts: 139
Thoughts: **Analyzing Device Removal and Compat Devices**
Okay, so I'm looking at this comment and it's highlighting a potential race condition involving "compat" devices and the refcount of a device. The comment states that *compat* devices need to be removed *after* the device's reference count hits zero. The reason for this, as I understand it, is to prevent `init_net()` from adding more compat devices in between the removal of the original set and the disablement of the device. That makes sense, avoiding a situation where we clean up, but then find ourselves needing to clean again.
Now, I'm trying to trace the device removal flow to see how this plays out in practice. The comment mentions `disable_device`. My immediate thought is, doesn't `disable_device` itself call `remove_compat_devs(device)`? Yes, it does. Okay, that's good.
So let's see, now I have to be sure. I'm thinking of `__ib_unregister_device`. Does it disable the device, and *then* deallocate it? Or the other way around? Let me double-check. I believe it calls `disable_device` directly. Yeah, that's what I thought. I will need to check `__ib_unregister_device` and ensure my mental model is correct. I have to confirm that `disable_device` is indeed called before final deallocation, which should align with the comment's assertion about refcounts and removing compat devices.
|
| 17/2 |
2026/05/24 18:14 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:__ib_unregister_device]
Results: map[SourceCode:1501: /* Callers must hold a get on the device. */
1502: static void __ib_unregister_device(struct ib_device *ib_dev)
1503: {
1504: struct ib_device *sub, *tmp;
1505:
1506: mutex_lock(&ib_dev->subdev_lock);
1507: list_for_each_entry_safe_reverse(sub, tmp,
1508: &ib_dev->subdev_list_head,
1509: subdev_list) {
1510: list_del(&sub->subdev_list);
1511: ib_dev->ops.del_sub_dev(sub);
1512: ib_device_put(ib_dev);
1513: }
1514: mutex_unlock(&ib_dev->subdev_lock);
1515:
1516: /*
1517: * We have a registration lock so that all the calls to unregister are
1518: * fully fenced, once any unregister returns the device is truly
1519: * unregistered even if multiple callers are unregistering it at the
1520: * same time. This also interacts with the registration flow and
1521: * provides sane semantics if register and unregister are racing.
1522: */
1523: mutex_lock(&ib_dev->unregistration_lock);
1524: if (!refcount_read(&ib_dev->refcount))
1525: goto out;
1526:
1527: disable_device(ib_dev);
1528: rdma_nl_notify_event(ib_dev, 0, RDMA_UNREGISTER_EVENT);
1529:
1530: /* Expedite removing unregistered pointers from the hash table */
1531: free_netdevs(ib_dev);
1532:
1533: ib_free_port_attrs(&ib_dev->coredev);
1534: device_del(&ib_dev->dev);
1535: ib_device_unregister_rdmacg(ib_dev);
1536: ib_cache_cleanup_one(ib_dev);
1537:
1538: /*
1539: * Drivers using the new flow may not call ib_dealloc_device except
1540: * in error unwind prior to registration success.
1541: */
1542: if (ib_dev->ops.dealloc_driver &&
1543: ib_dev->ops.dealloc_driver != prevent_dealloc_device) {
1544: WARN_ON(kref_read(&ib_dev->dev.kobj.kref) <= 1);
1545: ib_dealloc_device(ib_dev);
1546: }
1547: out:
1548: mutex_unlock(&ib_dev->unregistration_lock);
1549: }
SourceFile:drivers/infiniband/core/device.c]
|
| 18/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 13253
output: 47
thoughts: 125
Thoughts: **Investigating a Potential InfiniBand Device Deallocation Issue**
Okay, so the warning I'm seeing points directly to `ib_dealloc_device+0x187/0x200`. That's where things are getting hairy. Looking at the code for `ib_dealloc_device`, I see a critical check: `WARN_ON(!xa_empty(&device->compat_devs))`. That's a red flag. It's warning us if the `compat_devs` XArray isn't empty when we're trying to deallocate the device. That's a definite memory leak situation waiting to happen.
The call stack looks like this: `disable_device` is calling `remove_compat_devs(device)`, and then `__ib_unregister_device` eventually calls `ib_dealloc_device(ib_dev)`. The critical question is, *why* would `compat_devs` not be empty at the time of deallocation? `remove_compat_devs` is supposed to clean that up. Let me dig into `remove_compat_devs` and see what's going on there. I'm guessing there's a race condition or some logic bug in the device removal process. Gotta track this down.
|
| 19/2 |
2026/05/24 18:14 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:remove_compat_devs]
Results: map[SourceCode:1016: static void remove_compat_devs(struct ib_device *device)
1017: {
1018: struct ib_core_device *cdev;
1019: unsigned long index;
1020:
1021: xa_for_each (&device->compat_devs, index, cdev)
1022: remove_one_compat_dev(device, index);
1023: }
SourceFile:drivers/infiniband/core/device.c]
|
| 20/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 13856
output: 48
thoughts: 13
Thoughts: Okay, let's dive into this. Here's my take on that function call:
**Analyzing `remove_one_compat_dev`**
Alright, so the function call I'm looking at is `remove_one_compat_dev`. Just from the name, I'm already forming some initial hypotheses. "Remove" implies a deletion or deactivation of some sort. "One" suggests this is designed to handle a single entity at a time, probably to avoid concurrency issues or maintain some kind of internal consistency. "Compat" and "dev" are the more interesting parts. "Dev" clearly stands for "device," meaning we're dealing with hardware or a software representation of hardware within the system. "Compat," however, implies a compatibility layer or a device that's present for backward compatibility.
My first thought is around the use cases; Why would a device need removal? Is it for resource cleanup after a device is no longer needed, for removal of a problematic driver, or is it related to hotplugging and unplugging a device? The presence of "compat" suggests there is something more involved than a simple device removal. Is this function designed to clean up after an obsolete device? Is this function called as part of a driver or OS removal process?
I'm mentally formulating a few questions: What data structures does this function interact with? What are the implications of removing this "compatible" device? Does it affect other devices or drivers? Does it involve any locking mechanisms to ensure safety? I need to understand its context within the larger system. Is it part of a driver model, a device manager, or some other subsystem? I should probably start by checking the function's definition and any related documentation. A quick look at the surrounding code might give me more clues about its purpose and potential side effects. The goal is to figure out the exact consequences of this function call in the context of the device and system states.
|
| 21/2 |
2026/05/24 18:14 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:remove_one_compat_dev]
Results: map[SourceCode:1002: static void remove_one_compat_dev(struct ib_device *device, u32 id)
1003: {
1004: struct ib_core_device *cdev;
1005:
1006: mutex_lock(&device->compat_devs_mutex);
1007: cdev = xa_erase(&device->compat_devs, id);
1008: mutex_unlock(&device->compat_devs_mutex);
1009: if (cdev) {
1010: ib_free_port_attrs(cdev);
1011: device_del(&cdev->dev);
1012: put_device(&cdev->dev);
1013: }
1014: }
SourceFile:drivers/infiniband/core/device.c]
|
| 22/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 14556
output: 56
thoughts: 59
Thoughts: **Debugging a Potential Logic Flaw**
Okay, so I'm looking at this code and something doesn't quite sit right. It's bothering me that if this `remove_compat_devs` function, which I *assume* is meant to clear out the `compat_devs` list, actually removes *everything*, how can `compat_devs` still contain elements after that point? That's just fundamentally illogical, and it's a critical point to resolve here.
My first thought is, maybe something is adding to `compat_devs` after the call to `remove_compat_devs`? That would explain the apparent contradiction. Let's dig deeper and understand that process. So, I need to trace down where the `add_one_compat_dev` function is being called in order to follow the logic. Let's see...
|
| 23/2 |
2026/05/24 18:14 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeSnippetLines:2 Name:add_one_compat_dev SourceTreePrefix:]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:add_all_compat_devs SourceFile:drivers/infiniband/core/device.c SourceLine:1080 SourceSnippet:1078: down_read(&rdma_nets_rwsem);
1079: xa_for_each (&rdma_nets, net_index, rnet) {
1080: ret = add_one_compat_dev(dev, rnet);
1081: if (ret)
1082: break;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:add_compat_devs SourceFile:drivers/infiniband/core/device.c SourceLine:1035 SourceSnippet:1033: down_read(&rdma_nets_rwsem);
1034: xa_for_each (&rdma_nets, index, rnet) {
1035: ret = add_one_compat_dev(device, rnet);
1036: if (ret)
1037: break;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:rdma_dev_init_net SourceFile:drivers/infiniband/core/device.c SourceLine:1193 SourceSnippet:1191: */
1192: down_read(&rdma_nets_rwsem);
1193: ret = add_one_compat_dev(dev, rnet);
1194: up_read(&rdma_nets_rwsem);
1195: if (ret)
]] TruncatedOutput:false]
|
| 24/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 15317
output: 49
thoughts: 87
Thoughts: Here's my understanding of the relationships between these functions, distilled into my internal thought process:
**Function Call Graph - RDMA Device Initialization**
Okay, let's break this down. From what I'm seeing, `add_compat_devs` is triggered by `enable_device`, that makes sense, that's how we'd register compatible devices. And `add_all_compat_devs` is called from `rdma_compatdev_set` which is interesting, seems like it sets up the compatibility layer. Then, `rdma_dev_init_net` is the workhorse here, and is called, interestingly, from itself, which tells me it's a pernet initialization function, which means it likely handles network-specific setup for the RDMA device. I'll need to dig deeper into the actual `rdma_dev_init_net` function to get the full picture, since that's where the core initialization probably happens. Let's start there.
|
| 25/2 |
2026/05/24 18:14 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:rdma_dev_init_net]
Results: map[SourceCode:1164: static __net_init int rdma_dev_init_net(struct net *net)
1165: {
1166: struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
1167: unsigned long index;
1168: struct ib_device *dev;
1169: int ret;
1170:
1171: write_pnet(&rnet->net, net);
1172:
1173: ret = rdma_nl_net_init(rnet);
1174: if (ret)
1175: return ret;
1176:
1177: /* No need to create any compat devices in default init_net. */
1178: if (net_eq(net, &init_net))
1179: return 0;
1180:
1181: ret = xa_alloc(&rdma_nets, &rnet->id, rnet, xa_limit_32b, GFP_KERNEL);
1182: if (ret) {
1183: rdma_nl_net_exit(rnet);
1184: return ret;
1185: }
1186:
1187: down_read(&devices_rwsem);
1188: xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
1189: /* Hold nets_rwsem so that netlink command cannot change
1190: * system configuration for device sharing mode.
1191: */
1192: down_read(&rdma_nets_rwsem);
1193: ret = add_one_compat_dev(dev, rnet);
1194: up_read(&rdma_nets_rwsem);
1195: if (ret)
1196: break;
1197: }
1198: up_read(&devices_rwsem);
1199:
1200: if (ret)
1201: rdma_dev_exit_net(net);
1202:
1203: return ret;
1204: }
SourceFile:drivers/infiniband/core/device.c]
|
| 26/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 16290
output: 44
thoughts: 586
Thoughts: **Analyzing the `rdma_dev_init_net` and `disable_device` Synchronization**
Okay, let's break this down. My initial thought is about how `rdma_dev_init_net` iterates over registered RDMA devices marked with `DEVICE_REGISTERED`. The code snippet clearly shows this:
```c
down_read(&devices_rwsem);
xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
/* Hold nets_rwsem so that netlink command cannot change
* system configuration for device sharing mode.
*/
down_read(&rdma_nets_rwsem);
ret = add_one_compat_dev(dev, rnet);
up_read(&rdma_nets_rwsem);
if (ret)
break;
}
up_read(&devices_rwsem);
```
I see it's using a read-write semaphore `devices_rwsem` to protect access to the `devices` data structure. Within the loop, it acquires `rdma_nets_rwsem` to protect configuration related to device sharing. This is where the compatibility devices are added.
Now, my concern is with `disable_device`:
```c
down_write(&devices_rwsem);
xa_clear_mark(&devices, device->index, DEVICE_REGISTERED);
up_write(&devices_rwsem);
```
It takes a write lock on `devices_rwsem` and *clears* the `DEVICE_REGISTERED` mark before removing the compatible devices with `remove_compat_devs(device)`. The question is, what happens if `rdma_dev_init_net` is running concurrently?
Initially, I thought, if `DEVICE_REGISTERED` is cleared *before* `remove_compat_devs`, `rdma_dev_init_net` might miss this device, preventing it from adding the compatible device. But what about concurrency?
Okay, the crucial point is the semaphore `devices_rwsem`. `rdma_dev_init_net` holds `devices_rwsem` for read during the loop. `disable_device` needs a write lock on the same semaphore to clear the mark. So, they *should* be mutually exclusive.
If `rdma_dev_init_net` gets the read lock first, it finds the `DEVICE_REGISTERED` mark, adds the compat device. Then, `disable_device` acquires the write lock, clears the mark and calls `remove_compat_devs(device)`. Consequently, the compatible device added by `rdma_dev_init_net` will be removed.
If `disable_device` runs first, it clears the mark, preventing `rdma_dev_init_net` from seeing the device in the first place.
I also considered whether `rdma_dev_init_net` might release `devices_rwsem` within the loop, which could introduce a race. However, I can see that it holds `devices_rwsem` for the entire iteration. In the loop, it drops and reacquires `rdma_nets_rwsem`, but this is protected by a separate semaphore. That means `disable_device` *cannot* run concurrently with the loop.
So the locking protects against race conditions!
|
| 27/2 |
2026/05/24 18:14 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:disable_device]
Results: map[SourceCode:1269: static void disable_device(struct ib_device *device)
1270: {
1271: u32 cid;
1272:
1273: WARN_ON(!refcount_read(&device->refcount));
1274:
1275: down_write(&devices_rwsem);
1276: xa_clear_mark(&devices, device->index, DEVICE_REGISTERED);
1277: up_write(&devices_rwsem);
1278:
1279: /*
1280: * Remove clients in LIFO order, see assign_client_id. This could be
1281: * more efficient if xarray learns to reverse iterate. Since no new
1282: * clients can be added to this ib_device past this point we only need
1283: * the maximum possible client_id value here.
1284: */
1285: down_read(&clients_rwsem);
1286: cid = highest_client_id;
1287: up_read(&clients_rwsem);
1288: while (cid) {
1289: cid--;
1290: remove_client_context(device, cid);
1291: }
1292:
1293: ib_cq_pool_cleanup(device);
1294:
1295: /* Pairs with refcount_set in enable_device */
1296: ib_device_put(device);
1297: wait_for_completion(&device->unreg_completion);
1298:
1299: /*
1300: * compat devices must be removed after device refcount drops to zero.
1301: * Otherwise init_net() may add more compatdevs after removing compat
1302: * devices and before device is disabled.
1303: */
1304: remove_compat_devs(device);
1305: }
SourceFile:drivers/infiniband/core/device.c]
|
| 28/2 |
2026/05/24 18:14 |
llm |
expert |
0m
Model:
Tokens:
input: 18208
output: 17
thoughts: 96
Thoughts: **Understanding the Implications of Compat Device Removal**
Okay, so I'm reading this comment in the code, and something immediately piques my interest. It's saying that "compat devices must be removed after device refcount drops to zero." The implication is clear, but I need to dig deeper into the "why." Specifically, I'm wondering what `init_net()` is doing that this careful ordering is so crucial. What's the potential race condition here? I need to understand the function of `init_net()` in this context to see how it can interact with the removal of `compat` devices. It's obviously tied to some sort of network initialization or setup, but the specifics are important. I'm going to search through the Infiniband drivers, specifically in `drivers/infiniband/core/device.c`, to figure out what `init_net()` is up to and uncover the relationship between compat devices and network initialization. Hopefully, the surrounding code will shed some light on the vulnerability described by the comment.
|
| 29/2 |
2026/05/24 18:14 |
tool |
grepper |
0m
Args:
Results: map[Output:
Full output is too long, showing 500 out of 5852 lines.
Use more precise expression if possible.
[Documentation/admin-guide/sysctl/net.rst=378=Default : 128 KB
Documentation/admin-guide/sysctl/net.rst-379-
Documentation/admin-guide/sysctl/net.rst:380:fb_tunnels_only_for_init_net
Documentation/admin-guide/sysctl/net.rst-381-----------------------------
--
Documentation/admin-guide/sysctl/net.rst=399=Default : 0 (for compatibility reasons)
Documentation/admin-guide/sysctl/net.rst-400-
Documentation/admin-guide/sysctl/net.rst:401:devconf_inherit_init_net
Documentation/admin-guide/sysctl/net.rst-402-------------------------
--
Documentation/admin-guide/sysctl/net.rst=406=default, we keep the current behavior: for IPv4 we inherit all current
Documentation/admin-guide/sysctl/net.rst:407:settings from init_net and for IPv6 we reset all settings to default.
Documentation/admin-guide/sysctl/net.rst-408-
Documentation/admin-guide/sysctl/net.rst=409=If set to 1, both IPv4 and IPv6 settings are forced to inherit from
Documentation/admin-guide/sysctl/net.rst:410:current ones in init_net. If set to 2, both IPv4 and IPv6 settings are
Documentation/admin-guide/sysctl/net.rst-411-forced to reset to their default values. If set to 3, both IPv4 and IPv6
--
Documentation/admin-guide/sysctl/net.rst=569=Values:
--
Documentation/admin-guide/sysctl/net.rst-578-
Documentation/admin-guide/sysctl/net.rst:579:The init_net mode is always ``global``.
Documentation/admin-guide/sysctl/net.rst-580-
--
Documentation/networking/devlink/devlink-reload.rst=70=namespaces during devlink reload operation.
Documentation/networking/devlink/devlink-reload.rst:71:By default all devlink instances are created in init_net and stay there.
Documentation/networking/devlink/devlink-reload.rst-72-
--
Documentation/networking/netfilter-sysctl.rst=10=nf_log_all_netns - BOOLEAN
--
Documentation/networking/netfilter-sysctl.rst-13-
Documentation/networking/netfilter-sysctl.rst:14: By default, only init_net namespace can log packets into kernel log
Documentation/networking/netfilter-sysctl.rst-15- with LOG target; this aims to prevent containers from flooding host
Documentation/networking/netfilter-sysctl.rst-16- kernel log. If enabled, this target also works in other network
Documentation/networking/netfilter-sysctl.rst:17: namespaces. This variable is only accessible from init_net.
--
arch/m68k/coldfire/nettel.c=123=static void __init nettel_smc91x_init(void)
--
arch/m68k/coldfire/nettel.c-144-
arch/m68k/coldfire/nettel.c:145:static int __init init_nettel(void)
arch/m68k/coldfire/nettel.c-146-{
--
arch/m68k/coldfire/nettel.c-151-
arch/m68k/coldfire/nettel.c:152:arch_initcall(init_nettel);
arch/m68k/coldfire/nettel.c-153-
--
arch/s390/appldata/appldata_net_sum.c=59=static void appldata_get_net_sum_data(void *data)
--
arch/s390/appldata/appldata_net_sum.c-81- rcu_read_lock();
arch/s390/appldata/appldata_net_sum.c:82: for_each_netdev_rcu(&init_net, dev) {
arch/s390/appldata/appldata_net_sum.c-83- const struct rtnl_link_stats64 *stats;
--
drivers/android/binder.c=2999=static void binder_netlink_report(struct binder_proc *proc,
--
drivers/android/binder.c-3007-
drivers/android/binder.c:3008: if (!genl_has_listeners(&binder_nl_family, &init_net,
drivers/android/binder.c-3009- BINDER_NLGRP_REPORT))
--
drivers/atm/idt77252.c=3260=static int init_card(struct atm_dev *dev)
--
drivers/atm/idt77252.c-3512- sprintf(tname, "eth%d", card->index);
drivers/atm/idt77252.c:3513: tmp = dev_get_by_name(&init_net, tname); /* jhs: was "tmp = dev_get(tname);" */
drivers/atm/idt77252.c-3514- if (tmp) {
--
drivers/block/aoe/aoecmd.c=415=aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
--
drivers/block/aoe/aoecmd.c-422- rcu_read_lock();
drivers/block/aoe/aoecmd.c:423: for_each_netdev_rcu(&init_net, ifp) {
drivers/block/aoe/aoecmd.c-424- dev_hold(ifp);
--
drivers/block/aoe/aoenet.c=131=aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev)
--
drivers/block/aoe/aoenet.c-137-
drivers/block/aoe/aoenet.c:138: if (dev_net(ifp) != &init_net)
drivers/block/aoe/aoenet.c-139- goto exit;
--
drivers/block/drbd/drbd_receiver.c=400=static struct socket *drbd_try_connect(struct drbd_connection *connection)
--
drivers/block/drbd/drbd_receiver.c-433- what = "sock_create_kern";
drivers/block/drbd/drbd_receiver.c:434: err = sock_create_kern(&init_net, ((struct sockaddr *)&src_in6)->sa_family,
drivers/block/drbd/drbd_receiver.c-435- SOCK_STREAM, IPPROTO_TCP, &sock);
--
drivers/block/drbd/drbd_receiver.c=507=static int prepare_listen_socket(struct drbd_connection *connection, struct accept_wait_data *ad)
--
drivers/block/drbd/drbd_receiver.c-528- what = "sock_create_kern";
drivers/block/drbd/drbd_receiver.c:529: err = sock_create_kern(&init_net, ((struct sockaddr *)&my_addr)->sa_family,
drivers/block/drbd/drbd_receiver.c-530- SOCK_STREAM, IPPROTO_TCP, &s_listen);
--
drivers/connector/connector.c=274=static int cn_init(void)
--
drivers/connector/connector.c-284-
drivers/connector/connector.c:285: dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR, &cfg);
drivers/connector/connector.c-286- if (!dev->nls)
--
drivers/connector/connector.c-296-
drivers/connector/connector.c:297: proc_create_single("connector", S_IRUGO, init_net.proc_net, cn_proc_show);
drivers/connector/connector.c-298-
--
drivers/connector/connector.c=302=static void cn_fini(void)
--
drivers/connector/connector.c-307-
drivers/connector/connector.c:308: remove_proc_entry("connector", init_net.proc_net);
drivers/connector/connector.c-309-
--
drivers/infiniband/core/addr.c=133=static int ib_nl_ip_send_msg(struct rdma_dev_addr *dev_addr,
--
drivers/infiniband/core/addr.c-173- nlmsg_end(skb, nlh);
drivers/infiniband/core/addr.c:174: rdma_nl_multicast(&init_net, skb, RDMA_NL_GROUP_LS, GFP_KERNEL);
drivers/infiniband/core/addr.c-175-
--
drivers/infiniband/core/addr.c=523=static void rdma_addr_set_net_defaults(struct rdma_dev_addr *addr)
drivers/infiniband/core/addr.c-524-{
drivers/infiniband/core/addr.c:525: addr->net = &init_net;
drivers/infiniband/core/addr.c-526- addr->bound_dev_if = 0;
--
drivers/infiniband/core/addr.c=697=int roce_resolve_route_from_path(struct sa_path_rec *rec,
--
drivers/infiniband/core/addr.c-721-
drivers/infiniband/core/addr.c:722: dev_addr.net = &init_net;
drivers/infiniband/core/addr.c-723- dev_addr.sgid_attr = attr;
--
drivers/infiniband/core/addr.c=789=int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
--
drivers/infiniband/core/addr.c-805- memset(&dev_addr, 0, sizeof(dev_addr));
drivers/infiniband/core/addr.c:806: dev_addr.net = &init_net;
drivers/infiniband/core/addr.c-807- dev_addr.sgid_attr = sgid_attr;
--
drivers/infiniband/core/cma.c=1844=cma_ib_id_from_event(struct ib_cm_id *cm_id,
--
drivers/infiniband/core/cma.c-1902-
drivers/infiniband/core/cma.c:1903: bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
drivers/infiniband/core/cma.c-1904- rdma_ps_from_service_id(req->service_id),
--
drivers/infiniband/core/cma.c=5441=static void cma_remove_one(struct ib_device *device, void *client_data)
--
drivers/infiniband/core/cma.c-5456-
drivers/infiniband/core/cma.c:5457:static int cma_init_net(struct net *net)
drivers/infiniband/core/cma.c-5458-{
--
drivers/infiniband/core/cma.c=5479=static struct pernet_operations cma_pernet_operations = {
drivers/infiniband/core/cma.c:5480: .init = cma_init_net,
drivers/infiniband/core/cma.c-5481- .exit = cma_exit_net,
--
drivers/infiniband/core/device.c=115=unsigned int rdma_dev_net_id;
--
drivers/infiniband/core/device.c-119- * because we can't get the locking right using the existing net ns list. We
drivers/infiniband/core/device.c:120: * would require a init_net callback after the list is updated.
drivers/infiniband/core/device.c-121- */
--
drivers/infiniband/core/device.c=159=bool rdma_dev_has_raw_cap(const struct ib_device *dev)
--
drivers/infiniband/core/device.c-167- if (ib_devices_shared_netns)
drivers/infiniband/core/device.c:168: net = &init_net;
drivers/infiniband/core/device.c-169- else
--
drivers/infiniband/core/device.c=569=struct ib_device *_ib_alloc_device(size_t size, struct net *net)
--
drivers/infiniband/core/device.c-586- /* ib_devices_shared_netns can't change while we have active namespaces
drivers/infiniband/core/device.c:587: * in the system which means either init_net is passed or the user has
drivers/infiniband/core/device.c-588- * no idea what they are doing.
--
drivers/infiniband/core/device.c-590- * To avoid breaking backward compatibility, when in shared mode,
drivers/infiniband/core/device.c:591: * force to init the device in the init_net.
drivers/infiniband/core/device.c-592- */
drivers/infiniband/core/device.c:593: net = ib_devices_shared_netns ? &init_net : net;
drivers/infiniband/core/device.c-594- rdma_init_coredev(&device->coredev, device, net);
--
drivers/infiniband/core/device.c=928=static int add_one_compat_dev(struct ib_device *device,
--
drivers/infiniband/core/device.c-946- /*
drivers/infiniband/core/device.c:947: * The first of init_net() or ib_register_device() to take the
drivers/infiniband/core/device.c-948- * compat_devs_mutex wins and gets to add the device. Others will wait
--
drivers/infiniband/core/device.c=1092=int rdma_compatdev_set(u8 enable)
--
drivers/infiniband/core/device.c-1104- /* enable/disable of compat devices is not supported
drivers/infiniband/core/device.c:1105: * when more than default init_net exists.
drivers/infiniband/core/device.c-1106- */
--
drivers/infiniband/core/device.c=1124=static void rdma_dev_exit_net(struct net *net)
--
drivers/infiniband/core/device.c-1152- */
drivers/infiniband/core/device.c:1153: rdma_dev_change_netns(dev, net, &init_net);
drivers/infiniband/core/device.c-1154-
--
drivers/infiniband/core/device.c-1163-
drivers/infiniband/core/device.c:1164:static __net_init int rdma_dev_init_net(struct net *net)
drivers/infiniband/core/device.c-1165-{
--
drivers/infiniband/core/device.c-1176-
drivers/infiniband/core/device.c:1177: /* No need to create any compat devices in default init_net. */
drivers/infiniband/core/device.c:1178: if (net_eq(net, &init_net))
drivers/infiniband/core/device.c-1179- return 0;
--
drivers/infiniband/core/device.c=1269=static void disable_device(struct ib_device *device)
--
drivers/infiniband/core/device.c-1300- * compat devices must be removed after device refcount drops to zero.
drivers/infiniband/core/device.c:1301: * Otherwise init_net() may add more compatdevs after removing compat
drivers/infiniband/core/device.c-1302- * devices and before device is disabled.
--
drivers/infiniband/core/device.c=1775=static struct pernet_operations rdma_dev_net_ops = {
drivers/infiniband/core/device.c:1776: .init = rdma_dev_init_net,
drivers/infiniband/core/device.c-1777- .exit = rdma_dev_exit_net,
--
drivers/infiniband/core/iwcm.c=1166=static int __init iw_cm_init(void)
--
drivers/infiniband/core/iwcm.c-1177-
drivers/infiniband/core/iwcm.c:1178: iwcm_ctl_table_hdr = register_net_sysctl(&init_net, "net/iw_cm",
drivers/infiniband/core/iwcm.c-1179- iwcm_ctl_table);
--
drivers/infiniband/core/iwpm_msg.c=63=int iwpm_register_pid(struct iwpm_dev_data *pm_msg, u8 nl_client)
--
drivers/infiniband/core/iwpm_msg.c-110-
drivers/infiniband/core/iwpm_msg.c:111: ret = rdma_nl_multicast(&init_net, skb, RDMA_NL_GROUP_IWPM, GFP_KERNEL);
drivers/infiniband/core/iwpm_msg.c-112- if (ret) {
--
drivers/infiniband/core/iwpm_msg.c=143=int iwpm_add_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
--
drivers/infiniband/core/iwpm_msg.c-195-
drivers/infiniband/core/iwpm_msg.c:196: ret = rdma_nl_unicast_wait(&init_net, skb, iwpm_user_pid);
drivers/infiniband/core/iwpm_msg.c-197- if (ret) {
--
drivers/infiniband/core/iwpm_msg.c=226=int iwpm_add_and_query_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
--
drivers/infiniband/core/iwpm_msg.c-285-
drivers/infiniband/core/iwpm_msg.c:286: ret = rdma_nl_unicast_wait(&init_net, skb, iwpm_user_pid);
drivers/infiniband/core/iwpm_msg.c-287- if (ret) {
--
drivers/infiniband/core/iwpm_msg.c=314=int iwpm_remove_mapping(struct sockaddr_storage *local_addr, u8 nl_client)
--
drivers/infiniband/core/iwpm_msg.c-347-
drivers/infiniband/core/iwpm_msg.c:348: ret = rdma_nl_unicast_wait(&init_net, skb, iwpm_user_pid);
drivers/infiniband/core/iwpm_msg.c-349- if (ret) {
--
drivers/infiniband/core/iwpm_util.c=578=static int send_mapinfo_num(u32 mapping_num, u8 nl_client, int iwpm_pid)
--
drivers/infiniband/core/iwpm_util.c-603-
drivers/infiniband/core/iwpm_util.c:604: ret = rdma_nl_unicast(&init_net, skb, iwpm_pid);
drivers/infiniband/core/iwpm_util.c-605- if (ret) {
--
drivers/infiniband/core/iwpm_util.c=618=static int send_nlmsg_done(struct sk_buff *skb, u8 nl_client, int iwpm_pid)
--
drivers/infiniband/core/iwpm_util.c-631- nlh->nlmsg_type = NLMSG_DONE;
drivers/infiniband/core/iwpm_util.c:632: ret = rdma_nl_unicast(&init_net, skb, iwpm_pid);
drivers/infiniband/core/iwpm_util.c-633- if (ret)
--
drivers/infiniband/core/iwpm_util.c=761=int iwpm_send_hello(u8 nl_client, int iwpm_pid, u16 abi_version)
--
drivers/infiniband/core/iwpm_util.c-780-
drivers/infiniband/core/iwpm_util.c:781: ret = rdma_nl_unicast(&init_net, skb, iwpm_pid);
drivers/infiniband/core/iwpm_util.c-782- if (ret) {
--
drivers/infiniband/core/netlink.c=53=bool rdma_nl_chk_listeners(unsigned int group)
drivers/infiniband/core/netlink.c-54-{
drivers/infiniband/core/netlink.c:55: struct rdma_dev_net *rnet = rdma_net_to_dev_net(&init_net);
drivers/infiniband/core/netlink.c-56-
--
drivers/infiniband/core/netlink.c=82=get_cb_table(const struct sk_buff *skb, unsigned int type, unsigned int op)
--
drivers/infiniband/core/netlink.c-87- * Currently only NLDEV client is supporting netlink commands in
drivers/infiniband/core/netlink.c:88: * non init_net net namespace.
drivers/infiniband/core/netlink.c-89- */
drivers/infiniband/core/netlink.c:90: if (sock_net(skb->sk) != &init_net && type != RDMA_NL_NLDEV)
drivers/infiniband/core/netlink.c-91- return NULL;
--
drivers/infiniband/core/sa_query.c=844=static int ib_nl_make_request(struct ib_sa_query *query, gfp_t gfp_mask)
--
drivers/infiniband/core/sa_query.c-885- spin_lock_irqsave(&ib_nl_request_lock, flags);
drivers/infiniband/core/sa_query.c:886: ret = rdma_nl_multicast(&init_net, skb, RDMA_NL_GROUP_LS, gfp_flag);
drivers/infiniband/core/sa_query.c-887-
--
drivers/infiniband/core/ucma.c=1963=static int __init ucma_init(void)
--
drivers/infiniband/core/ucma.c-1976-
drivers/infiniband/core/ucma.c:1977: ucma_ctl_table_hdr = register_net_sysctl(&init_net, "net/rdma_ucm", ucma_ctl_table);
drivers/infiniband/core/ucma.c-1978- if (!ucma_ctl_table_hdr) {
--
drivers/infiniband/core/verbs.c=3133=EXPORT_SYMBOL(rdma_alloc_netdev);
drivers/infiniband/core/verbs.c-3134-
drivers/infiniband/core/verbs.c:3135:int rdma_init_netdev(struct ib_device *device, u32 port_num,
drivers/infiniband/core/verbs.c-3136- enum rdma_netdev_t type, const char *name,
--
drivers/infiniband/core/verbs.c-3154-}
drivers/infiniband/core/verbs.c:3155:EXPORT_SYMBOL(rdma_init_netdev);
drivers/infiniband/core/verbs.c-3156-
--
drivers/infiniband/hw/cxgb4/cm.c=2073=static int import_ep(struct c4iw_ep *ep, int iptype, __u8 *peer_ip,
--
drivers/infiniband/hw/cxgb4/cm.c-2088- if (iptype == 4)
drivers/infiniband/hw/cxgb4/cm.c:2089: pdev = __ip_dev_find(&init_net, *(__be32 *)peer_ip, false);
drivers/infiniband/hw/cxgb4/cm.c-2090- else if (IS_ENABLED(CONFIG_IPV6))
drivers/infiniband/hw/cxgb4/cm.c:2091: for_each_netdev(&init_net, pdev) {
drivers/infiniband/hw/cxgb4/cm.c:2092: if (ipv6_chk_addr(&init_net,
drivers/infiniband/hw/cxgb4/cm.c-2093- (struct in6_addr *)peer_ip,
--
drivers/infiniband/hw/cxgb4/cm.c=3928=static void build_cpl_pass_accept_req(struct sk_buff *skb, int stid , u8 tos)
--
drivers/infiniband/hw/cxgb4/cm.c-3956- tcp_clear_options(&tmp_opt);
drivers/infiniband/hw/cxgb4/cm.c:3957: tcp_parse_options(&init_net, skb, &tmp_opt, 0, NULL);
drivers/infiniband/hw/cxgb4/cm.c-3958-
--
drivers/infiniband/hw/cxgb4/cm.c=4060=static int rx_pkt(struct c4iw_dev *dev, struct sk_buff *skb)
--
drivers/infiniband/hw/cxgb4/cm.c-4155- if (neigh->dev->flags & IFF_LOOPBACK) {
drivers/infiniband/hw/cxgb4/cm.c:4156: pdev = ip_dev_find(&init_net, iph->daddr);
drivers/infiniband/hw/cxgb4/cm.c-4157- if (!pdev) {
--
drivers/infiniband/hw/erdma/erdma_main.c=48=static int erdma_enum_and_get_netdev(struct erdma_dev *dev)
--
drivers/infiniband/hw/erdma/erdma_main.c-57- rtnl_lock();
drivers/infiniband/hw/erdma/erdma_main.c:58: for_each_netdev(&init_net, netdev) {
drivers/infiniband/hw/erdma/erdma_main.c-59- /*
--
drivers/infiniband/hw/irdma/cm.c=1559=static u8 irdma_iw_get_vlan_prio(u32 *loc_addr, u8 prio, bool ipv4)
--
drivers/infiniband/hw/irdma/cm.c-1564- if (ipv4) {
drivers/infiniband/hw/irdma/cm.c:1565: ndev = ip_dev_find(&init_net, htonl(loc_addr[0]));
drivers/infiniband/hw/irdma/cm.c-1566- } else if (IS_ENABLED(CONFIG_IPV6)) {
--
drivers/infiniband/hw/irdma/cm.c-1571-
drivers/infiniband/hw/irdma/cm.c:1572: for_each_netdev_rcu (&init_net, ip_dev) {
drivers/infiniband/hw/irdma/cm.c:1573: if (ipv6_chk_addr(&init_net, &laddr6, ip_dev, 1)) {
drivers/infiniband/hw/irdma/cm.c-1574- ndev = ip_dev;
--
drivers/infiniband/hw/irdma/cm.c=1602=void irdma_get_vlan_mac_ipv6(u32 *addr, u16 *vlan_id, u8 *mac)
--
drivers/infiniband/hw/irdma/cm.c-1616- rcu_read_lock();
drivers/infiniband/hw/irdma/cm.c:1617: for_each_netdev_rcu (&init_net, ip_dev) {
drivers/infiniband/hw/irdma/cm.c:1618: if (ipv6_chk_addr(&init_net, &laddr6, ip_dev, 1)) {
drivers/infiniband/hw/irdma/cm.c-1619- if (vlan_id)
--
drivers/infiniband/hw/irdma/cm.c=1633=u16 irdma_get_vlan_ipv4(u32 *addr)
--
drivers/infiniband/hw/irdma/cm.c-1637-
drivers/infiniband/hw/irdma/cm.c:1638: netdev = ip_dev_find(&init_net, htonl(addr[0]));
drivers/infiniband/hw/irdma/cm.c-1639- if (netdev) {
--
drivers/infiniband/hw/irdma/cm.c=1656=static int irdma_add_mqh_6(struct irdma_device *iwdev,
--
drivers/infiniband/hw/irdma/cm.c-1667- rtnl_lock();
drivers/infiniband/hw/irdma/cm.c:1668: for_each_netdev(&init_net, ip_dev) {
drivers/infiniband/hw/irdma/cm.c-1669- if (!(ip_dev->flags & IFF_UP))
--
drivers/infiniband/hw/irdma/cm.c=1744=static int irdma_add_mqh_4(struct irdma_device *iwdev,
--
drivers/infiniband/hw/irdma/cm.c-1755- rtnl_lock();
drivers/infiniband/hw/irdma/cm.c:1756: for_each_netdev(&init_net, ip_dev) {
drivers/infiniband/hw/irdma/cm.c-1757- if (!(ip_dev->flags & IFF_UP))
--
drivers/infiniband/hw/irdma/cm.c=1980=static int irdma_addr_resolve_neigh(struct irdma_device *iwdev, u32 src_ip,
--
drivers/infiniband/hw/irdma/cm.c-1988-
drivers/infiniband/hw/irdma/cm.c:1989: rt = ip_route_output(&init_net, dst_ipaddr, src_ipaddr, 0, 0,
drivers/infiniband/hw/irdma/cm.c-1990- RT_SCOPE_UNIVERSE);
--
drivers/infiniband/hw/irdma/cm.c=2017=static struct dst_entry *irdma_get_dst_ipv6(struct sockaddr_in6 *src_addr,
--
drivers/infiniband/hw/irdma/cm.c-2029-
drivers/infiniband/hw/irdma/cm.c:2030: dst = ip6_route_output(&init_net, NULL, &fl6);
drivers/infiniband/hw/irdma/cm.c-2031- }
--
drivers/infiniband/hw/irdma/utils.c=336=static void irdma_add_ipv6_addr(struct irdma_device *iwdev)
--
drivers/infiniband/hw/irdma/utils.c-343- rcu_read_lock();
drivers/infiniband/hw/irdma/utils.c:344: for_each_netdev_rcu (&init_net, ip_dev) {
drivers/infiniband/hw/irdma/utils.c-345- if (((rdma_vlan_dev_vlan_id(ip_dev) < 0xFFFF &&
--
drivers/infiniband/hw/irdma/utils.c=378=static void irdma_add_ipv4_addr(struct irdma_device *iwdev)
--
drivers/infiniband/hw/irdma/utils.c-384- rcu_read_lock();
drivers/infiniband/hw/irdma/utils.c:385: for_each_netdev_rcu (&init_net, dev) {
drivers/infiniband/hw/irdma/utils.c-386- if (((rdma_vlan_dev_vlan_id(dev) < 0xFFFF &&
--
drivers/infiniband/hw/mlx4/main.c=126=static struct net_device *mlx4_ib_get_netdev(struct ib_device *device,
--
drivers/infiniband/hw/mlx4/main.c-132- rcu_read_lock();
drivers/infiniband/hw/mlx4/main.c:133: for_each_netdev_rcu(&init_net, dev) {
drivers/infiniband/hw/mlx4/main.c-134- if (dev->dev.parent != ibdev->ib_dev.dev.parent ||
--
drivers/infiniband/hw/mlx4/main.c=2348=static void mlx4_ib_port_event(struct ib_device *ibdev, struct net_device *ndev,
--
drivers/infiniband/hw/mlx4/main.c-2354-
drivers/infiniband/hw/mlx4/main.c:2355: if (!net_eq(dev_net(ndev), &init_net))
drivers/infiniband/hw/mlx4/main.c-2356- return;
--
drivers/infiniband/hw/mlx4/main.c=2376=static int mlx4_ib_netdev_event(struct notifier_block *this,
--
drivers/infiniband/hw/mlx4/main.c-2381-
drivers/infiniband/hw/mlx4/main.c:2382: if (!net_eq(dev_net(dev), &init_net))
drivers/infiniband/hw/mlx4/main.c-2383- return NOTIFY_DONE;
--
drivers/infiniband/hw/qedr/qedr_iw_cm.c=395=static u16 qedr_iw_get_vlan_ipv4(struct qedr_dev *dev, u32 *addr)
--
drivers/infiniband/hw/qedr/qedr_iw_cm.c-399-
drivers/infiniband/hw/qedr/qedr_iw_cm.c:400: ndev = ip_dev_find(&init_net, htonl(addr[0]));
drivers/infiniband/hw/qedr/qedr_iw_cm.c-401-
--
drivers/infiniband/hw/qedr/qedr_iw_cm.c=411=static u16 qedr_iw_get_vlan_ipv6(u32 *addr)
--
drivers/infiniband/hw/qedr/qedr_iw_cm.c-424- rcu_read_lock();
drivers/infiniband/hw/qedr/qedr_iw_cm.c:425: for_each_netdev_rcu(&init_net, ndev) {
drivers/infiniband/hw/qedr/qedr_iw_cm.c:426: if (ipv6_chk_addr(&init_net, &laddr6, ndev, 1)) {
drivers/infiniband/hw/qedr/qedr_iw_cm.c-427- vlan_id = rdma_vlan_dev_vlan_id(ndev);
--
drivers/infiniband/hw/qedr/qedr_iw_cm.c=440=qedr_addr4_resolve(struct qedr_dev *dev,
--
drivers/infiniband/hw/qedr/qedr_iw_cm.c-449-
drivers/infiniband/hw/qedr/qedr_iw_cm.c:450: rt = ip_route_output(&init_net, dst_ip, src_ip, 0, 0,
drivers/infiniband/hw/qedr/qedr_iw_cm.c-451- RT_SCOPE_UNIVERSE);
--
drivers/infiniband/hw/qedr/qedr_iw_cm.c=477=qedr_addr6_resolve(struct qedr_dev *dev,
--
drivers/infiniband/hw/qedr/qedr_iw_cm.c-489-
drivers/infiniband/hw/qedr/qedr_iw_cm.c:490: dst = ip6_route_output(&init_net, NULL, &fl6);
drivers/infiniband/hw/qedr/qedr_iw_cm.c-491-
--
drivers/infiniband/sw/rdmavt/vt.c=48=struct rvt_dev_info *rvt_alloc_device(size_t size, int nports)
--
drivers/infiniband/sw/rdmavt/vt.c-51-
drivers/infiniband/sw/rdmavt/vt.c:52: rdi = container_of(_ib_alloc_device(size, &init_net), struct rvt_dev_info, ibdev);
drivers/infiniband/sw/rdmavt/vt.c-53- if (!rdi)
--
drivers/infiniband/sw/rxe/rxe_net.c=103=static struct dst_entry *rxe_find_route4(struct rxe_qp *qp,
--
drivers/infiniband/sw/rxe/rxe_net.c-116-
drivers/infiniband/sw/rxe/rxe_net.c:117: rt = ip_route_output_key(&init_net, &fl);
drivers/infiniband/sw/rxe/rxe_net.c-118- if (IS_ERR(rt)) {
--
drivers/infiniband/sw/rxe/rxe_net.c=712=static int rxe_net_ipv4_init(void)
drivers/infiniband/sw/rxe/rxe_net.c-713-{
drivers/infiniband/sw/rxe/rxe_net.c:714: recv_sockets.sk4 = rxe_setup_udp_tunnel(&init_net,
drivers/infiniband/sw/rxe/rxe_net.c-715- htons(ROCE_V2_UDP_DPORT), false);
--
drivers/infiniband/sw/rxe/rxe_net.c=725=static int rxe_net_ipv6_init(void)
--
drivers/infiniband/sw/rxe/rxe_net.c-728-
drivers/infiniband/sw/rxe/rxe_net.c:729: recv_sockets.sk6 = rxe_setup_udp_tunnel(&init_net,
drivers/infiniband/sw/rxe/rxe_net.c-730- htons(ROCE_V2_UDP_DPORT), true);
--
drivers/infiniband/sw/rxe/rxe_qp.c=283=static int rxe_qp_init_req(struct rxe_dev *rxe, struct rxe_qp *qp,
--
drivers/infiniband/sw/rxe/rxe_qp.c-291-
drivers/infiniband/sw/rxe/rxe_qp.c:292: err = sock_create_kern(&init_net, AF_INET, SOCK_DGRAM, 0, &qp->sk);
drivers/infiniband/sw/rxe/rxe_qp.c-293- if (err < 0)
--
drivers/infiniband/ulp/ipoib/ipoib_main.c=2272=int ipoib_intf_init(struct ib_device *hca, u32 port, const char *name,
--
drivers/infiniband/ulp/ipoib/ipoib_main.c-2285-
drivers/infiniband/ulp/ipoib/ipoib_main.c:2286: rc = rdma_init_netdev(hca, port, RDMA_NETDEV_IPOIB, name,
drivers/infiniband/ulp/ipoib/ipoib_main.c-2287- NET_NAME_UNKNOWN, ipoib_setup_common, dev);
--
drivers/infiniband/ulp/iser/iser_verbs.c=750=int iser_connect(struct iser_conn *iser_conn, struct sockaddr *src_addr,
--
drivers/infiniband/ulp/iser/iser_verbs.c-766-
drivers/infiniband/ulp/iser/iser_verbs.c:767: ib_conn->cma_id = rdma_create_id(&init_net, iser_cma_handler,
drivers/infiniband/ulp/iser/iser_verbs.c-768- iser_conn, RDMA_PS_TCP, IB_QPT_RC);
--
drivers/infiniband/ulp/isert/ib_isert.c=2214=isert_setup_id(struct isert_np *isert_np)
--
drivers/infiniband/ulp/isert/ib_isert.c-2223-
drivers/infiniband/ulp/isert/ib_isert.c:2224: id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
drivers/infiniband/ulp/isert/ib_isert.c-2225- RDMA_PS_TCP, IB_QPT_RC);
--
drivers/infiniband/ulp/rtrs/rtrs-clt.c=2078=static int create_cm(struct rtrs_clt_con *con)
--
drivers/infiniband/ulp/rtrs/rtrs-clt.c-2084-
drivers/infiniband/ulp/rtrs/rtrs-clt.c:2085: cm_id = rdma_create_id(&init_net, rtrs_clt_rdma_cm_handler, con,
drivers/infiniband/ulp/rtrs/rtrs-clt.c-2086- clt_path->s.dst_addr.ss_family == AF_IB ?
--
drivers/infiniband/ulp/rtrs/rtrs-srv.c=2076=static struct rdma_cm_id *rtrs_srv_cm_init(struct rtrs_srv_ctx *ctx,
--
drivers/infiniband/ulp/rtrs/rtrs-srv.c-2082-
drivers/infiniband/ulp/rtrs/rtrs-srv.c:2083: cm_id = rdma_create_id(&init_net, rtrs_srv_rdma_cm_handler,
drivers/infiniband/ulp/rtrs/rtrs-srv.c-2084- ctx, ps, IB_QPT_RC);
--
drivers/infiniband/ulp/rtrs/rtrs.c=455=static int rtrs_str_to_sockaddr(const char *addr, size_t len,
--
drivers/infiniband/ulp/rtrs/rtrs.c-466- cpy = kstrndup(addr + 3, len - 3, GFP_KERNEL);
drivers/infiniband/ulp/rtrs/rtrs.c:467: err = cpy ? inet_pton_with_scope(&init_net, AF_UNSPEC,
drivers/infiniband/ulp/rtrs/rtrs.c-468- cpy, port_str, dst) : -ENOMEM;
--
drivers/infiniband/ulp/srpt/ib_srpt.c=3493=static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
--
drivers/infiniband/ulp/srpt/ib_srpt.c-3503- if (ret < 0)
drivers/infiniband/ulp/srpt/ib_srpt.c:3504: ret = inet_pton_with_scope(&init_net, AF_UNSPEC, name, NULL,
drivers/infiniband/ulp/srpt/ib_srpt.c-3505- &sa);
--
drivers/infiniband/ulp/srpt/ib_srpt.c=3680=static struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr)
--
drivers/infiniband/ulp/srpt/ib_srpt.c-3684-
drivers/infiniband/ulp/srpt/ib_srpt.c:3685: rdma_cm_id = rdma_create_id(&init_net, srpt_rdma_cm_handler,
drivers/infiniband/ulp/srpt/ib_srpt.c-3686- NULL, RDMA_PS_TCP, IB_QPT_RC);
--
drivers/leds/trigger/ledtrig-netdev.c=259=static int set_device_name(struct led_netdev_data *trigger_data,
--
drivers/leds/trigger/ledtrig-netdev.c-285- trigger_data->net_dev =
drivers/leds/trigger/ledtrig-netdev.c:286: dev_get_by_name(&init_net, trigger_data->device_name);
drivers/leds/trigger/ledtrig-netdev.c-287-
--
drivers/mtd/maps/netsc520.c=78=static struct mtd_info *mymtd;
drivers/mtd/maps/netsc520.c-79-
]
]
|
| 30/2 |
2026/05/24 18:15 |
llm |
expert |
0m
Model:
Tokens:
input: 30738
output: 49
thoughts: 117
Thoughts: **Analyzing the Context of Device Removal and Initialization**
Okay, so I'm looking at this comment in the code, and it's making a specific claim about compat devices. It states that they *must* be removed after the device refcount hits zero. Otherwise, some `init_net()` process could potentially add more compat devices, causing issues down the line. But hold on a second... `init_net()` isn't quite right. That's a bit imprecise.
I immediately think, based on my understanding of this part of the system, that they're likely referring to `rdma_dev_init_net()`. It feels like there may be a misunderstanding of the exact function name or possibly a typo in the comment. I need to make sure I am correctly contextualizing the comments with the right function.
I should probably refresh my understanding of how `rdma_dev_init_net()` works, just to verify that. I'll need to go back and double-check how that function handles device initialization and how it interacts with compat devices, particularly in the context of device refcounts and the removal process.
|
| 31/2 |
2026/05/24 18:15 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:rdma_dev_init_net]
Results: map[SourceCode:1164: static __net_init int rdma_dev_init_net(struct net *net)
1165: {
1166: struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
1167: unsigned long index;
1168: struct ib_device *dev;
1169: int ret;
1170:
1171: write_pnet(&rnet->net, net);
1172:
1173: ret = rdma_nl_net_init(rnet);
1174: if (ret)
1175: return ret;
1176:
1177: /* No need to create any compat devices in default init_net. */
1178: if (net_eq(net, &init_net))
1179: return 0;
1180:
1181: ret = xa_alloc(&rdma_nets, &rnet->id, rnet, xa_limit_32b, GFP_KERNEL);
1182: if (ret) {
1183: rdma_nl_net_exit(rnet);
1184: return ret;
1185: }
1186:
1187: down_read(&devices_rwsem);
1188: xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
1189: /* Hold nets_rwsem so that netlink command cannot change
1190: * system configuration for device sharing mode.
1191: */
1192: down_read(&rdma_nets_rwsem);
1193: ret = add_one_compat_dev(dev, rnet);
1194: up_read(&rdma_nets_rwsem);
1195: if (ret)
1196: break;
1197: }
1198: up_read(&devices_rwsem);
1199:
1200: if (ret)
1201: rdma_dev_exit_net(net);
1202:
1203: return ret;
1204: }
SourceFile:drivers/infiniband/core/device.c]
|
| 32/2 |
2026/05/24 18:15 |
llm |
expert |
0m
Model:
Tokens:
input: 31766
output: 48
thoughts: 286
Thoughts: **Analyzing RDMA Device Unregistration**
Okay, so I'm looking at this `disable_device` function and immediately a potential race condition pops out. My intuition is screaming about the interaction between the device unregistration and the network initialization process. Let's break it down.
First, I see `rdma_dev_init_net` iterates through registered devices marked with `DEVICE_REGISTERED`. The `disable_device` function, logically, clears this `DEVICE_REGISTERED` mark. Simple enough, it looks like it's designed to prevent further device registration. Then there's this `wait_for_completion` on `device->unreg_completion`. This seems to be part of the teardown.
The crucial point is that after this, it seems that `remove_compat_devs` is called, it seems like we *must* remove compat devs after device refcount drops to zero and not before. It's designed this way to keep everything tidy, but it's what triggers the race condition.
Now, here's where my alarm bells start ringing. If `DEVICE_REGISTERED` is cleared *before* `rdma_dev_init_net` has processed the device, no problem. It won't see it. However, what if `rdma_dev_init_net` has *already* seen this device? And what if, potentially, `rdma_dev_init_net` is blocked, say, on `compat_devs_mutex` within the `add_one_compat_dev` function? That's the critical question.
I need to dig into `add_one_compat_dev` to understand the lock contention and how it relates to the device removal process. This needs some further investigation. This needs to be understood.
|
| 33/2 |
2026/05/24 18:15 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:add_one_compat_dev]
Results: map[SourceCode: 928: static int add_one_compat_dev(struct ib_device *device,
929: struct rdma_dev_net *rnet)
930: {
931: struct ib_core_device *cdev;
932: int ret;
933:
934: lockdep_assert_held(&rdma_nets_rwsem);
935: if (!ib_devices_shared_netns)
936: return 0;
937:
938: /*
939: * Create and add compat device in all namespaces other than where it
940: * is currently bound to.
941: */
942: if (net_eq(read_pnet(&rnet->net),
943: read_pnet(&device->coredev.rdma_net)))
944: return 0;
945:
946: /*
947: * The first of init_net() or ib_register_device() to take the
948: * compat_devs_mutex wins and gets to add the device. Others will wait
949: * for completion here.
950: */
951: mutex_lock(&device->compat_devs_mutex);
952: cdev = xa_load(&device->compat_devs, rnet->id);
953: if (cdev) {
954: ret = 0;
955: goto done;
956: }
957: ret = xa_reserve(&device->compat_devs, rnet->id, GFP_KERNEL);
958: if (ret)
959: goto done;
960:
961: cdev = kzalloc_obj(*cdev);
962: if (!cdev) {
963: ret = -ENOMEM;
964: goto cdev_err;
965: }
966:
967: cdev->dev.parent = device->dev.parent;
968: rdma_init_coredev(cdev, device, read_pnet(&rnet->net));
969: cdev->dev.release = compatdev_release;
970: ret = dev_set_name(&cdev->dev, "%s", dev_name(&device->dev));
971: if (ret)
972: goto add_err;
973:
974: ret = device_add(&cdev->dev);
975: if (ret)
976: goto add_err;
977: ret = ib_setup_port_attrs(cdev);
978: if (ret)
979: goto port_err;
980:
981: ret = xa_err(xa_store(&device->compat_devs, rnet->id,
982: cdev, GFP_KERNEL));
983: if (ret)
984: goto insert_err;
985:
986: mutex_unlock(&device->compat_devs_mutex);
987: return 0;
988:
989: insert_err:
990: ib_free_port_attrs(cdev);
991: port_err:
992: device_del(&cdev->dev);
993: add_err:
994: put_device(&cdev->dev);
995: cdev_err:
996: xa_release(&device->compat_devs, rnet->id);
997: done:
998: mutex_unlock(&device->compat_devs_mutex);
999: return ret;
1000: }
SourceFile:drivers/infiniband/core/device.c]
|
| 34/2 |
2026/05/24 18:15 |
llm |
expert |
0m
Model:
Tokens:
input: 33568
output: 49
thoughts: 669
Thoughts: **Analyzing the Race Condition in RDMA Device Compatibility Handling**
Okay, let's break this down systematically. My initial concern was a potential race condition between `rdma_dev_init_net` and the device disabling process, specifically around the addition and removal of compatibility devices.
My thought process began with the observation that `rdma_dev_init_net` conditionally adds a compatibility device if it encounters `DEVICE_REGISTERED`. This addition occurs via `add_one_compat_dev`, which *does* take `device->compat_devs_mutex`, as does its counterpart for removal, `remove_one_compat_dev`, called from `remove_compat_devs`. So, the mutual exclusion on `device->compat_devs_mutex` would seem to serialize this part, in itself.
My initial thought was: what if `rdma_dev_init_net` adds the compat dev, and then `remove_compat_devs` runs and removes it? Or, the other way around: `remove_compat_devs` *first*, and then `rdma_dev_init_net` tries to add one later? The potential for `rdma_dev_init_net` to run after the `remove_compat_devs` was a concern if `rdma_dev_init_net` sees `DEVICE_REGISTERED` and then is delayed prior to taking the mutex.
However, I quickly realized that `rdma_dev_init_net` holds the `devices_rwsem` for read. Critically, `disable_device`, which eventually leads to the removal of compatibility devices, holds this same semaphore for *write* to clear `DEVICE_REGISTERED`. This means the two are mutually exclusive. Either `rdma_dev_init_net` is reading and the disabling is blocked, or the disabling is writing and the other isn't reading.
This implies that if `rdma_dev_init_net` holds `devices_rwsem`, `disable_device` cannot clear `DEVICE_REGISTERED`. Conversely, if `disable_device` clears `DEVICE_REGISTERED`, `rdma_dev_init_net` cannot see the device and therefore cannot add the compatibility device.
Then I checked that `rdma_dev_init_net` releases `devices_rwsem` *after* it has completed its iteration over the list of devices. This is key. The `rdma_dev_init_net` completes its potentially adding compat devs, releases the semaphore. Then `disable_device` acquires the write lock on `devices_rwsem`, which I was certain happened *before* any removal calls. So the removal and addition are serialized. This case is perfectly safe.
I considered a reversal of that logic, where the disabling clears `DEVICE_REGISTERED` and releases the semaphore, and then `rdma_dev_init_net` acquires the semaphore. However, in this case, `rdma_dev_init_net` will NOT see the device because `DEVICE_REGISTERED` is cleared; it will therefore *not* add any compat dev. Therefore, `disable_device` will then call `remove_compat_devs`. This scenario is also safe, as the removal only happens after the add.
But I'm not done. I then need to make sure this is all correct, and there aren't *other* potential addition sources. I then noted that `add_compat_devs` is called from `enable_device`, which itself is called from `ib_register_device`. And `ib_register_device` and its counterpart `ib_unregister_device` are serialized for the same device, so that's also safe. I was also careful to not forget about `rdma_compatdev_set`, which also has `add_all_compat_devs` or `remove_all_compat_devs` calls.
**In summary, considering all the synchronization primitives, device states, and code paths, I am now confident that the addition and removal of compatibility devices in the RDMA stack is safe from race conditions, and this particular code path is correctly serialized.**
|
| 35/2 |
2026/05/24 18:15 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:add_all_compat_devs]
Results: map[SourceCode:1064: static int add_all_compat_devs(void)
1065: {
1066: struct rdma_dev_net *rnet;
1067: struct ib_device *dev;
1068: unsigned long index;
1069: int ret = 0;
1070:
1071: down_read(&devices_rwsem);
1072: xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
1073: unsigned long net_index = 0;
1074:
1075: /* Hold nets_rwsem so that any other thread modifying this
1076: * system param can sync with this thread.
1077: */
1078: down_read(&rdma_nets_rwsem);
1079: xa_for_each (&rdma_nets, net_index, rnet) {
1080: ret = add_one_compat_dev(dev, rnet);
1081: if (ret)
1082: break;
1083: }
1084: up_read(&rdma_nets_rwsem);
1085: }
1086: up_read(&devices_rwsem);
1087: if (ret)
1088: remove_all_compat_devs();
1089: return ret;
1090: }
SourceFile:drivers/infiniband/core/device.c]
|
| 36/2 |
2026/05/24 18:15 |
llm |
expert |
0m
Model:
Tokens:
input: 35589
output: 50
thoughts: 68
Thoughts: Here's my summary, as the thought process unfolds:
**Concurrency Concerns in Device Registration**
Okay, so `add_all_compat_devs` has a read lock on `devices_rwsem`. It's also iterating over devices that have the `DEVICE_REGISTERED` flag set. That immediately raises a red flag in my mind. This means `add_all_compat_devs` is mutually exclusive with `disable_device`, which is *also* messing with `DEVICE_REGISTERED` by clearing it. That makes sense, but let's see, that's fine.
Now, the interesting question is: What happens if `rdma_dev_change_netns` gets invoked at the same time? Does that mean the flag can be changed underneath the read lock of `add_all_compat_devs`? I need to look into that function more closely to see whether it affects `DEVICE_REGISTERED`. Let me make a mental note to track down the locking in that routine.
|
| 37/2 |
2026/05/24 18:15 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:rdma_dev_change_netns]
Results: map[SourceCode:1664: /*
1665: * The caller must pass in a device that has the kref held and the refcount
1666: * released. If the device is in cur_net and still registered then it is moved
1667: * into net.
1668: */
1669: static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net,
1670: struct net *net)
1671: {
1672: int ret2 = -EINVAL;
1673: int ret;
1674:
1675: mutex_lock(&device->unregistration_lock);
1676:
1677: /*
1678: * If a device not under ib_device_get() or if the unregistration_lock
1679: * is not held, the namespace can be changed, or it can be unregistered.
1680: * Check again under the lock.
1681: */
1682: if (refcount_read(&device->refcount) == 0 ||
1683: !net_eq(cur_net, read_pnet(&device->coredev.rdma_net))) {
1684: ret = -ENODEV;
1685: goto out;
1686: }
1687:
1688: kobject_uevent(&device->dev.kobj, KOBJ_REMOVE);
1689: disable_device(device);
1690:
1691: /*
1692: * At this point no one can be using the device, so it is safe to
1693: * change the namespace.
1694: */
1695: write_pnet(&device->coredev.rdma_net, net);
1696:
1697: down_read(&devices_rwsem);
1698: /*
1699: * Currently rdma devices are system wide unique. So the device name
1700: * is guaranteed free in the new namespace. Publish the new namespace
1701: * at the sysfs level.
1702: */
1703: ret = device_rename(&device->dev, dev_name(&device->dev));
1704: up_read(&devices_rwsem);
1705: if (ret) {
1706: dev_warn(&device->dev,
1707: "%s: Couldn't rename device after namespace change\n",
1708: __func__);
1709: /* Try and put things back and re-enable the device */
1710: write_pnet(&device->coredev.rdma_net, cur_net);
1711: }
1712:
1713: ret2 = enable_device_and_get(device);
1714: if (ret2) {
1715: /*
1716: * This shouldn't really happen, but if it does, let the user
1717: * retry at later point. So don't disable the device.
1718: */
1719: dev_warn(&device->dev,
1720: "%s: Couldn't re-enable device after namespace change\n",
1721: __func__);
1722: }
1723: kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1724:
1725: ib_device_put(device);
1726: out:
1727: mutex_unlock(&device->unregistration_lock);
1728: if (ret)
1729: return ret;
1730: return ret2;
1731: }
SourceFile:drivers/infiniband/core/device.c]
|
| 38/2 |
2026/05/24 18:15 |
llm |
expert |
0m
Model:
Tokens:
input: 36958
output: 46
thoughts: 790
Thoughts: **Analyzing RDMA Device Namespace Change and Unregistration**
Okay, let's break this down. `rdma_dev_change_netns` first disables and then re-enables a device. Specifically, it calls `disable_device` and then `enable_device_and_get`. `disable_device` clears `DEVICE_REGISTERED` and removes compat devices. Then, `enable_device_and_get` sets `DEVICE_REGISTERED` and adds them back.
My initial thought was, could there be a concurrency issue if `disable_device` within `rdma_dev_change_netns` is called at the same time as `__ib_unregister_device`? No, because `rdma_dev_change_netns` holds the `device->unregistration_lock`. `__ib_unregister_device` also takes that same lock, making them mutually exclusive. Good, that's safe.
But what happens if `enable_device_and_get` fails within `rdma_dev_change_netns`? The code handles this with a warning, and importantly, it doesn't disable the device again. But it also looks like there's an `ib_device_put(device)`. The device is left disabled.
Now, `__ib_unregister_device` will be called later, and the code first attempts to lock the same `unregistration_lock`. Crucially, it checks the device's reference count. If `enable_device_and_get` failed, and `refcount` is 0 then `__ib_unregister_device` will see `refcount_read(&ib_dev->refcount) == 0`. It immediately `goto out`, skipping the call to `disable_device`.
So, it skips the `disable_device` call. Does that mean it also skips `ib_dealloc_device`? No, looking at the code, it's not simply skipped. The code checks for `ib_dev->ops.dealloc_driver`. If that's set and *not* a specific prevent function, then it calls `ib_dealloc_device`. It includes a `WARN_ON(kref_read(&ib_dev->dev.kobj.kref) <= 1)`, which is a good sanity check.
Hold on... the `goto out;` skips *both* `disable_device` and `ib_dealloc_device`. So, if `refcount` is zero, we're not properly cleaning up. It only skips `ib_dealloc_device` if `refcount` is 0. But we know `ib_dealloc_device` is also called elsewhere, like `ib_device_release`, if the `kref` drops to zero. But, wait, no, actually `ib_device_release` does *not* call `ib_dealloc_device`. Okay, got it. If `enable_device_and_get` fails and the `refcount` is 0, the device is left in a partially disabled state, the `ib_dealloc_device` call is skipped entirely. This requires more investigation.
|
| 39/2 |
2026/05/24 18:15 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:ib_device_release]
Results: map[SourceCode: 473: static void ib_device_release(struct device *device)
474: {
475: struct ib_device *dev = container_of(device, struct ib_device, dev);
476:
477: free_netdevs(dev);
478: WARN_ON(refcount_read(&dev->refcount));
479: if (dev->hw_stats_data)
480: ib_device_release_hw_stats(dev->hw_stats_data);
481: if (dev->port_data) {
482: ib_cache_release_one(dev);
483: ib_security_release_port_pkey_list(dev);
484: rdma_counter_release(dev);
485: kfree_rcu(container_of(dev->port_data, struct ib_port_data_rcu,
486: pdata[0]),
487: rcu_head);
488: }
489:
490: mutex_destroy(&dev->subdev_lock);
491: mutex_destroy(&dev->unregistration_lock);
492: mutex_destroy(&dev->compat_devs_mutex);
493:
494: xa_destroy(&dev->compat_devs);
495: xa_destroy(&dev->client_data);
496: kfree_rcu(dev, rcu_head);
497: }
SourceFile:drivers/infiniband/core/device.c]
|
| 40/2 |
2026/05/24 18:15 |
llm |
expert |
0m
Model:
Tokens:
input: 38924
output: 48
thoughts: 192
Thoughts: **Debugging the IB Device Deallocation**
Okay, the warning message is *definitely* originating within `ib_dealloc_device`. That means, without a doubt, that function *is* being called. If `ib_dealloc_device` is being called, then the `compat_devs` list must be non-empty at that point, triggering the warning. Now, that's the puzzling part: how in the world could `compat_devs` still have devices registered when we're supposedly deallocating?
Alright, let's step back and re-examine how `compat_devs` gets populated. Time to trace the calls to `add_one_compat_dev`. Looks like it's called from several places: `rdma_dev_init_net`, `add_compat_devs`, and `add_all_compat_devs`. And where do *those* functions fit into the picture? `add_compat_devs` is called by `enable_device`. `enable_device` itself is called by `ib_register_device` and also by `enable_device_and_get`. `add_all_compat_devs` is being called from `rdma_compatdev_set`.
Right. Let's dig deeper into `rdma_compatdev_set`. That seems like the logical next point to investigate.
|
| 41/2 |
2026/05/24 18:15 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:rdma_compatdev_set]
Results: map[SourceCode:1092: int rdma_compatdev_set(u8 enable)
1093: {
1094: struct rdma_dev_net *rnet;
1095: unsigned long index;
1096: int ret = 0;
1097:
1098: down_write(&rdma_nets_rwsem);
1099: if (ib_devices_shared_netns == enable) {
1100: up_write(&rdma_nets_rwsem);
1101: return 0;
1102: }
1103:
1104: /* enable/disable of compat devices is not supported
1105: * when more than default init_net exists.
1106: */
1107: xa_for_each (&rdma_nets, index, rnet) {
1108: ret++;
1109: break;
1110: }
1111: if (!ret)
1112: ib_devices_shared_netns = enable;
1113: up_write(&rdma_nets_rwsem);
1114: if (ret)
1115: return -EBUSY;
1116:
1117: if (enable)
1118: ret = add_all_compat_devs();
1119: else
1120: remove_all_compat_devs();
1121: return ret;
1122: }
SourceFile:drivers/infiniband/core/device.c]
|
| 42/2 |
2026/05/24 18:15 |
llm |
expert |
0m
Model:
Tokens:
input: 39899
output: 48
thoughts: 1152
Thoughts: **Analyzing RDMA Device Compatibility and Namespace Handling**
Okay, let's break this down. My initial thought process revolves around `rdma_compatdev_set`. It checks `rdma_nets` for existing network namespaces. If there are any, it returns `-EBUSY`. Now, the important part: `rdma_nets` contains *all* network namespaces except `init_net`. So, if `rdma_nets` is empty (meaning only `init_net` exists), it sets `ib_devices_shared_netns = enable`. Then, it proceeds to call either `add_all_compat_devs` or `remove_all_compat_devs`.
Initially, I see that if `rdma_nets` is empty, then `add_all_compat_devs` will iterate over an empty list, doing nothing, which seems correct. However, this raises a question about whether any compat devices actually get added. I have to confirm that! After looking at the code for `rdma_dev_init_net`, it confirms my hypothesis. `rdma_nets` contains all *non-init_net* namespaces. Now I know for certain.
Delving deeper, I see `add_one_compat_dev` checks `ib_devices_shared_netns`. If it's false, nothing happens. If it's true, it iterates through all nets in `rdma_nets` *except* the device's own net, and creates a compat device for each.
Now, a critical scenario crosses my mind: What if `remove_compat_devs` is called, but a compat device gets added *after* `remove_compat_devs`? My primary concern centers on race conditions and the proper handling of device registration and unregistration. I see `remove_compat_devs` is called in `disable_device`. If `DEVICE_REGISTERED` is cleared, `rdma_dev_init_net` and `add_all_compat_devs` won't "see" the device. What about `add_compat_devs`? This is called from `enable_device`, which is called from `ib_register_device` and `enable_device_and_get`.
Could `enable_device` be called *after* `disable_device`? Aha! Looking at `rdma_dev_change_netns`, it calls `disable_device` and then `enable_device_and_get`. Yes, this scenario is a possibility! If `enable_device_and_get` is called, it sets `DEVICE_REGISTERED` and calls `add_compat_devs`. However, `rdma_dev_change_netns` holds `device->unregistration_lock`. `__ib_unregister_device` also holds that same lock. Since they use the same lock, this should prevent concurrent execution. Therefore, `rdma_dev_change_netns` re-enables the device if it finishes successfully. Then `__ib_unregister_device` eventually calls `disable_device` and `ib_dealloc_device`. So `disable_device` will clear `DEVICE_REGISTERED` and call `remove_compat_devs`. Then `ib_dealloc_device` should check `xa_empty(&device->compat_devs)`. At that point, it should be empty.
The final question: What if `enable_device_and_get` fails in `rdma_dev_change_netns`? Looking at the code for that case, it indicates that if `enable_device_and_get` fails, there is a warning, and the device is *not* disabled. So the state reverts back to the old state, and `remove_compat_devs` is not called!
Looks like it is safe.
|
| 43/2 |
2026/05/24 18:16 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:enable_device_and_get]
Results: map[SourceCode:1307: /*
1308: * An enabled device is visible to all clients and to all the public facing
1309: * APIs that return a device pointer. This always returns with a new get, even
1310: * if it fails.
1311: */
1312: static int enable_device_and_get(struct ib_device *device)
1313: {
1314: struct ib_client *client;
1315: unsigned long index;
1316: int ret = 0;
1317:
1318: /*
1319: * One ref belongs to the xa and the other belongs to this
1320: * thread. This is needed to guard against parallel unregistration.
1321: */
1322: refcount_set(&device->refcount, 2);
1323: down_write(&devices_rwsem);
1324: xa_set_mark(&devices, device->index, DEVICE_REGISTERED);
1325:
1326: /*
1327: * By using downgrade_write() we ensure that no other thread can clear
1328: * DEVICE_REGISTERED while we are completing the client setup.
1329: */
1330: downgrade_write(&devices_rwsem);
1331:
1332: if (device->ops.enable_driver) {
1333: ret = device->ops.enable_driver(device);
1334: if (ret)
1335: goto out;
1336: }
1337:
1338: down_read(&clients_rwsem);
1339: xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) {
1340: ret = add_client_context(device, client);
1341: if (ret)
1342: break;
1343: }
1344: up_read(&clients_rwsem);
1345: if (!ret)
1346: ret = add_compat_devs(device);
1347: out:
1348: up_read(&devices_rwsem);
1349: return ret;
1350: }
SourceFile:drivers/infiniband/core/device.c]
|
| 44/2 |
2026/05/24 18:16 |
llm |
expert |
0m
Model:
Tokens:
input: 42568
output: 47
thoughts: 252
Thoughts: **My Assessment of the Device Registration and Unregistration Flow**
Okay, let me break this down. My initial understanding is that `add_client_context` and `device->ops.enable_driver` acting as gates to `add_compat_devs` makes logical sense. If those fail, we don't proceed with the compatible device setup. Good.
But hold on, there's a problem here. It seems that if `add_compat_devs` *itself* fails, it returns an error, but the `DEVICE_REGISTERED` flag is still set. That's not ideal. Further digging shows that if `enable_device_and_get` fails then we also have the same issue with `DEVICE_REGISTERED` and a `refcount` of 2.
So, the device is registered in the system (the `devices` xarray) and has the `DEVICE_REGISTERED` mark, even in error cases. Then `rdma_dev_change_netns` does an `ib_device_put`, decrementing the `refcount` to 1. Which leaves the device registered and visible, even with a pending error. This is also not ideal.
Later, `__ib_unregister_device` is invoked. It will call `disable_device`. That is good. The `disable_device` function *should* clear `DEVICE_REGISTERED`, wait for clients to go away, and call `remove_compat_devs`. Assuming all goes as expected, this looks like the correct shutdown procedure.
But the lingering question is what happens in the case that `add_compat_devs` fails, because it could leave the driver in an inconsistent state. Now I need to go and really understand the error paths within `add_compat_devs` and see if they properly clean up the device state.
|
| 45/2 |
2026/05/24 18:16 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:add_compat_devs]
Results: map[SourceCode:1025: static int add_compat_devs(struct ib_device *device)
1026: {
1027: struct rdma_dev_net *rnet;
1028: unsigned long index;
1029: int ret = 0;
1030:
1031: lockdep_assert_held(&devices_rwsem);
1032:
1033: down_read(&rdma_nets_rwsem);
1034: xa_for_each (&rdma_nets, index, rnet) {
1035: ret = add_one_compat_dev(device, rnet);
1036: if (ret)
1037: break;
1038: }
1039: up_read(&rdma_nets_rwsem);
1040: return ret;
1041: }
SourceFile:drivers/infiniband/core/device.c]
|
| 46/2 |
2026/05/24 18:16 |
llm |
expert |
0m
Model:
Tokens:
input: 43532
output: 53
thoughts: 795
Thoughts: **Analyzing the `compat_devs` Management in RDMA Device Lifecycle**
Okay, let me break this down. My initial thought was that if `add_one_compat_dev` fails, it errors out, leaving the already added compat devs in `device->compat_devs`. And that's fine because `remove_compat_devs` will eventually clear them.
But now I'm starting to think about concurrency. Are there any other ways `compat_devs` is populated besides the function mentioned? I'm specifically wondering about potential race conditions. Could `rdma_dev_init_net` and `disable_device` run at the same time and cause an issue? Thankfully, `rdma_dev_init_net` holds `devices_rwsem` for read, and `disable_device` holds it for write. So they are mutually exclusive. That's good.
Alright, what about `rdma_dev_init_net` running concurrently with `ib_dealloc_device`? `ib_dealloc_device` has a write lock on `devices_rwsem` and removes the device from the `devices` XArray. But `rdma_dev_init_net` iterates over the `devices` with a `DEVICE_REGISTERED` mark. `disable_device` clears this mark. So, since `disable_device` runs before `ib_dealloc_device`, `rdma_dev_init_net` won't see the device after it's disabled. Makes sense.
Now, here's a trickier scenario: `rdma_dev_init_net` running, seeing the device, dropping `rdma_nets_rwsem`, and then calling `add_one_compat_dev`. `add_one_compat_dev` takes `compat_devs_mutex`. But, crucially, `rdma_dev_init_net` *still* holds the `devices_rwsem` for read while it's in progress! This means `disable_device` is blocked from acquiring `devices_rwsem` for write to clear `DEVICE_REGISTERED`. This prevents concurrent issues. So, `disable_device` will block until `rdma_dev_init_net` is done. Then, `disable_device` clears `DEVICE_REGISTERED` and calls `remove_compat_devs`. So, `remove_compat_devs` will remove the compat dev added by `rdma_dev_init_net`. Good.
Just to be thorough, I have to ask: is there any *other* place `add_one_compat_dev` is called? Yes! `add_all_compat_devs` is called from `rdma_compatdev_set`. It, too, holds `devices_rwsem` for read. So, that's also mutually exclusive with `disable_device`. Excellent.
Now, this comment in the code catches my eye:
```c
/*
* compat devices must be removed after device refcount drops to zero.
* Otherwise init_net() may add more compatdevs after removing compat
* devices and before device is disabled.
*/
```
Wait a second, "after device refcount drops to zero". `disable_device` does:
```c
ib_device_put(device);
wait_for_completion(&device->unreg_completion);
/*
* compat devices must be removed after device refcount drops to zero.
* Otherwise init_net() may add more compatdevs after removing compat
* devices and before device is disabled.
*/
remove_compat_devs(device);
```
So `ib_device_put` drops the reference count. When the refcount hits zero, `device->unreg_completion` is signaled. Where is `unreg_completion` completed? That's the key. I need to track down where that happens to fully understand the ordering here. This all hinges on the reference count and the `unreg_completion`.
|
| 47/2 |
2026/05/24 18:16 |
tool |
codesearch-find-references |
0m
Error:
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeSnippetLines:2 Name:unreg_completion SourceTreePrefix:]
Results: map[References:<nil> TruncatedOutput:false]
|
| 48/2 |
2026/05/24 18:16 |
llm |
expert |
0m
Model:
Tokens:
input: 45265
output: 18
thoughts: 26
Thoughts: Here's my thought process, as if I were considering this search:
**Diving into the IB Completion Queue Hunt**
Okay, so I need to track down how `unreg_completion` is handled, and I know it's related to InfiniBand drivers. Given that context, my initial thought is that the best place to start is the core device-level code. So, let's target `drivers/infiniband/core/device.c`. That file likely contains the overarching device management logic, and the completion queue mechanism is fundamental to the IB protocol. I'm assuming that this is where the core registration/unregistration of completion queues would reside. Let's see what that file can tell us. I anticipate that this will either be the place where the function itself is implemented, or where it is called and handled.
|
| 49/2 |
2026/05/24 18:16 |
tool |
grepper |
0m
Args: map[Expression:unreg_completion]
Results: map[Output:drivers/infiniband/core/device.c=346=void ib_device_put(struct ib_device *device)
--
drivers/infiniband/core/device.c-348- if (refcount_dec_and_test(&device->refcount))
drivers/infiniband/core/device.c:349: complete(&device->unreg_completion);
drivers/infiniband/core/device.c-350-}
--
drivers/infiniband/core/device.c=569=struct ib_device *_ib_alloc_device(size_t size, struct net *net)
--
drivers/infiniband/core/device.c-607- mutex_init(&device->compat_devs_mutex);
drivers/infiniband/core/device.c:608: init_completion(&device->unreg_completion);
drivers/infiniband/core/device.c-609- INIT_WORK(&device->unregistration_work, ib_unregister_work);
--
drivers/infiniband/core/device.c=1269=static void disable_device(struct ib_device *device)
--
drivers/infiniband/core/device.c-1296- ib_device_put(device);
drivers/infiniband/core/device.c:1297: wait_for_completion(&device->unreg_completion);
drivers/infiniband/core/device.c-1298-
--
include/rdma/ib_verbs.h=2812=struct ib_device {
--
include/rdma/ib_verbs.h-2884- refcount_t refcount;
include/rdma/ib_verbs.h:2885: struct completion unreg_completion;
include/rdma/ib_verbs.h-2886- struct work_struct unregistration_work;
]
|
| 50/2 |
2026/05/24 18:16 |
llm |
expert |
0m
Model:
Tokens:
input: 45974
output: 33
thoughts: 625
Thoughts: **Dissecting the Device Disabling Flow**
Okay, so I'm tracing this `disable_device` function and trying to understand its interactions, particularly around reference counting and compatibility devices. My current understanding is that `ib_device_put` completes `unreg_completion` when the reference count (`refcount`) hits zero. Got it. But if the `refcount` *doesn't* hit zero right away, then `disable_device` will be blocked, waiting for `unreg_completion`.
The question is, who else is holding references? I know clients do; `add_client_context` increments the refcount and `remove_client_context` decrements it. `disable_device` iterates through all the clients and calls `remove_client_context` on each. So that clears those references. But what if there are *other* users, like those obtained through `ib_device_get_by_netdev` or `ib_device_get_by_name`? They also take references. If those users are present, `disable_device` will block, waiting for those other users to release their references, right?
Here's the tricky part, though. While `disable_device` is blocked, waiting, the code *already* clears `DEVICE_REGISTERED`! That happens pretty early on with the `xa_clear_mark` call. That means `rdma_dev_init_net` *won't* see the device after that call. Now, that is the expected behavior, but the comment in the code about `rdma_dev_init_net` has me thinking...
The comment says that compat devices must be removed after the device refcount drops to zero. Otherwise, `init_net()` may add compat devices before the device is disabled, I have to think that this `init_net` function is referring to `rdma_dev_init_net()`, as the code I am reading is very specific to this library. I am assuming I am reading the code correctly.
So, *why* does the comment say this? If `rdma_dev_init_net` iterates over registered devices, and `DEVICE_REGISTERED` is cleared, it will not see the device. Did `disable_device` clear `DEVICE_REGISTERED` *after* `remove_compat_devs` in the past? Or maybe, `rdma_dev_init_net` didn't check `DEVICE_REGISTERED` at some point? I need to go back and check the commit history of `disable_device` to see if there's been any change to the ordering or logic. I'm going to dig into the history.
|
| 51/2 |
2026/05/24 18:16 |
tool |
git-log |
10m
Error:
Args: map[CodeRegexp:compat devices must be removed after device refcount drops to zero Count:5]
Results:
|
| 52/2 |
2026/05/24 18:26 |
llm |
expert |
0m
Model:
Tokens:
input: 47242
output: 51
thoughts: 21
Thoughts: Here's a summary of my mental process:
**Diving into `drivers/infiniband/core/device.c` History**
Okay, time to dig into the history of `drivers/infiniband/core/device.c`. That file is a core component within the InfiniBand driver infrastructure, so any changes there are likely significant. I need to understand the evolution of this file, the motivations behind the modifications, and how it relates to the broader IB ecosystem. I'll be looking for key commits, particularly those involving API changes, performance enhancements, bug fixes, and new device support. I'll mentally categorize the changes: are we talking about core device management, resource allocation, error handling, or perhaps even subtle shifts in how we handle specific hardware characteristics? Identifying the specific commit messages containing keywords like "performance", "error", or device names, will be key to rapidly understanding what the important developments are within that file. I want to build a mental map of this code's evolution and any potential implications for my current work. I suspect this will be interesting.
|
| 53/2 |
2026/05/24 18:26 |
tool |
git-log |
0m
Args: map[CodeRegexp:compat devices must be removed after device refcount drops to zero Count:5 PathPrefix:drivers/infiniband/core/device.c]
Results: map[Output:4e0f7b907072 RDMA/core: Implement compat device/sysfs tree in net namespace
]
|
| 54/2 |
2026/05/24 18:26 |
llm |
expert |
0m
Model:
Tokens:
input: 47582
output: 27
thoughts: 19
Thoughts: Okay, let's dive into commit `4e0f7b907072`. I need to understand what changed here. This is likely a key point in the project's evolution, so I need to analyze it carefully. I need to understand the 'why' behind the changes, not just the 'what'. Was this a bug fix, a performance optimization, a refactor, or a new feature implementation? And what are the implications of this commit on the overall architecture and maintainability of the system? I'll need to look at the diff, probably starting with the most significant files and lines. I'll be looking for potential regressions, unintended consequences, or any subtle shifts in logic. I'll pay close attention to the comments and commit message; hopefully they provide some context and rationale.
|
| 55/2 |
2026/05/24 18:26 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 4e0f7b9070726a34bbd87a74e407d4cced6d49ab
Author: Parav Pandit <parav@mellanox.com>
Date: Tue Feb 26 13:56:13 2019 +0200
RDMA/core: Implement compat device/sysfs tree in net namespace
Implement compatibility layer sysfs entries of ib_core so that non
init_net net namespaces can also discover rdma devices.
Each non init_net net namespace has ib_core_device created in it.
Such ib_core_device sysfs tree resembles rdma devices found in
init_net namespace.
This allows discovering rdma devices in multiple non init_net net
namespaces via sysfs entries and helpful to rdma-core userspace.
Signed-off-by: Parav Pandit <parav@mellanox.com>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c
index 078566d0d7c2..167e2d46e4cb 100644
--- a/drivers/infiniband/core/device.c
+++ b/drivers/infiniband/core/device.c
@@ -38,6 +38,8 @@
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/netdevice.h>
+#include <net/net_namespace.h>
+#include <net/netns/generic.h>
#include <linux/security.h>
#include <linux/notifier.h>
#include <linux/hashtable.h>
@@ -101,6 +103,30 @@ static DECLARE_RWSEM(clients_rwsem);
* be registered.
*/
#define CLIENT_DATA_REGISTERED XA_MARK_1
+
+/**
+ * struct rdma_dev_net - rdma net namespace metadata for a net
+ * @net: Pointer to owner net namespace
+ * @id: xarray id to identify the net namespace.
+ */
+struct rdma_dev_net {
+ possible_net_t net;
+ u32 id;
+};
+
+static unsigned int rdma_dev_net_id;
+
+/*
+ * A list of net namespaces is maintained in an xarray. This is necessary
+ * because we can't get the locking right using the existing net ns list. We
+ * would require a init_net callback after the list is updated.
+ */
+static DEFINE_XARRAY_FLAGS(rdma_nets, XA_FLAGS_ALLOC);
+/*
+ * rwsem to protect accessing the rdma_nets xarray entries.
+ */
+static DECLARE_RWSEM(rdma_nets_rwsem);
+
/*
* xarray has this behavior where it won't iterate over NULL values stored in
* allocated arrays. So we need our own iterator to see all values stored in
@@ -268,6 +294,26 @@ struct ib_device *ib_device_get_by_name(const char *name,
}
EXPORT_SYMBOL(ib_device_get_by_name);
+static int rename_compat_devs(struct ib_device *device)
+{
+ struct ib_core_device *cdev;
+ unsigned long index;
+ int ret = 0;
+
+ mutex_lock(&device->compat_devs_mutex);
+ xa_for_each (&device->compat_devs, index, cdev) {
+ ret = device_rename(&cdev->dev, dev_name(&device->dev));
+ if (ret) {
+ dev_warn(&cdev->dev,
+ "Fail to rename compatdev to new name %s\n",
+ dev_name(&device->dev));
+ break;
+ }
+ }
+ mutex_unlock(&device->compat_devs_mutex);
+ return ret;
+}
+
int ib_device_rename(struct ib_device *ibdev, const char *name)
{
int ret;
@@ -287,6 +333,7 @@ int ib_device_rename(struct ib_device *ibdev, const char *name)
if (ret)
goto out;
strlcpy(ibdev->name, name, IB_DEVICE_NAME_MAX);
+ ret = rename_compat_devs(ibdev);
out:
up_write(&devices_rwsem);
return ret;
@@ -336,6 +383,7 @@ static void ib_device_release(struct device *device)
WARN_ON(refcount_read(&dev->refcount));
ib_cache_release_one(dev);
ib_security_release_port_pkey_list(dev);
+ xa_destroy(&dev->compat_devs);
xa_destroy(&dev->client_data);
if (dev->port_data)
kfree_rcu(container_of(dev->port_data, struct ib_port_data_rcu,
@@ -359,7 +407,10 @@ static int ib_device_uevent(struct device *device,
static const void *net_namespace(struct device *d)
{
- return &init_net;
+ struct ib_core_device *coredev =
+ container_of(d, struct ib_core_device, dev);
+
+ return read_pnet(&coredev->rdma_net);
}
static struct class ib_class = {
@@ -371,7 +422,7 @@ static struct class ib_class = {
};
static void rdma_init_coredev(struct ib_core_device *coredev,
- struct ib_device *dev)
+ struct ib_device *dev, struct net *net)
{
/* This BUILD_BUG_ON is intended to catch layout change
* of union of ib_core_device and device.
@@ -387,6 +438,7 @@ static void rdma_init_coredev(struct ib_core_device *coredev,
device_initialize(&coredev->dev);
coredev->owner = dev;
INIT_LIST_HEAD(&coredev->port_list);
+ write_pnet(&coredev->rdma_net, net);
}
/**
@@ -416,7 +468,7 @@ struct ib_device *_ib_alloc_device(size_t size)
}
device->groups[0] = &ib_dev_attr_group;
- rdma_init_coredev(&device->coredev, device);
+ rdma_init_coredev(&device->coredev, device, &init_net);
INIT_LIST_HEAD(&device->event_handler_list);
spin_lock_init(&device->event_handler_lock);
@@ -427,6 +479,8 @@ struct ib_device *_ib_alloc_device(size_t size)
*/
xa_init_flags(&device->client_data, XA_FLAGS_ALLOC);
init_rwsem(&device->client_data_rwsem);
+ xa_init_flags(&device->compat_devs, XA_FLAGS_ALLOC);
+ mutex_init(&device->compat_devs_mutex);
init_completion(&device->unreg_completion);
INIT_WORK(&device->unregistration_work, ib_unregister_work);
@@ -459,6 +513,7 @@ void ib_dealloc_device(struct ib_device *device)
/* Expedite releasing netdev references */
free_netdevs(device);
+ WARN_ON(!xa_empty(&device->compat_devs));
WARN_ON(!xa_empty(&device->client_data));
WARN_ON(refcount_read(&device->refcount));
rdma_restrack_clean(device);
@@ -667,6 +722,180 @@ static int ib_security_change(struct notifier_block *nb, unsigned long event,
return NOTIFY_OK;
}
+static void compatdev_release(struct device *dev)
+{
+ struct ib_core_device *cdev =
+ container_of(dev, struct ib_core_device, dev);
+
+ kfree(cdev);
+}
+
+static int add_one_compat_dev(struct ib_device *device,
+ struct rdma_dev_net *rnet)
+{
+ struct ib_core_device *cdev;
+ int ret;
+
+ /*
+ * Create and add compat device in all namespaces other than where it
+ * is currently bound to.
+ */
+ if (net_eq(read_pnet(&rnet->net),
+ read_pnet(&device->coredev.rdma_net)))
+ return 0;
+
+ /*
+ * The first of init_net() or ib_register_device() to take the
+ * compat_devs_mutex wins and gets to add the device. Others will wait
+ * for completion here.
+ */
+ mutex_lock(&device->compat_devs_mutex);
+ cdev = xa_load(&device->compat_devs, rnet->id);
+ if (cdev) {
+ ret = 0;
+ goto done;
+ }
+ ret = xa_reserve(&device->compat_devs, rnet->id, GFP_KERNEL);
+ if (ret)
+ goto done;
+
+ cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
+ if (!cdev) {
+ ret = -ENOMEM;
+ goto cdev_err;
+ }
+
+ cdev->dev.parent = device->dev.parent;
+ rdma_init_coredev(cdev, device, read_pnet(&rnet->net));
+ cdev->dev.release = compatdev_release;
+ dev_set_name(&cdev->dev, "%s", dev_name(&device->dev));
+
+ ret = device_add(&cdev->dev);
+ if (ret)
+ goto add_err;
+
+ ret = xa_err(xa_store(&device->compat_devs, rnet->id,
+ cdev, GFP_KERNEL));
+ if (ret)
+ goto insert_err;
+
+ mutex_unlock(&device->compat_devs_mutex);
+ return 0;
+
+insert_err:
+ device_del(&cdev->dev);
+add_err:
+ put_device(&cdev->dev);
+cdev_err:
+ xa_release(&device->compat_devs, rnet->id);
+done:
+ mutex_unlock(&device->compat_devs_mutex);
+ return ret;
+}
+
+static void remove_one_compat_dev(struct ib_device *device, u32 id)
+{
+ struct ib_core_device *cdev;
+
+ mutex_lock(&device->compat_devs_mutex);
+ cdev = xa_erase(&device->compat_devs, id);
+ mutex_unlock(&device->compat_devs_mutex);
+ if (cdev) {
+ device_del(&cdev->dev);
+ put_device(&cdev->dev);
+ }
+}
+
+static void remove_compat_devs(struct ib_device *device)
+{
+ struct ib_core_device *cdev;
+ unsigned long index;
+
+ xa_for_each (&device->compat_devs, index, cdev)
+ remove_one_compat_dev(device, index);
+}
+
+static int add_compat_devs(struct ib_device *device)
+{
+ struct rdma_dev_net *rnet;
+ unsigned long index;
+ int ret = 0;
+
+ down_read(&rdma_nets_rwsem);
+ xa_for_each (&rdma_nets, index, rnet) {
+ ret = add_one_compat_dev(device, rnet);
+ if (ret)
+ break;
+ }
+ up_read(&rdma_nets_rwsem);
+ return ret;
+}
+
+static void rdma_dev_exit_net(struct net *net)
+{
+ struct rdma_dev_net *rnet = net_generic(net, rdma_dev_net_id);
+ struct ib_device *dev;
+ unsigned long index;
+ int ret;
+
+ down_write(&rdma_nets_rwsem);
+ /*
+ * Prevent the ID from being re-used and hide the id from xa_for_each.
+ */
+ ret = xa_err(xa_store(&rdma_nets, rnet->id, NULL, GFP_KERNEL));
+ WARN_ON(ret);
+ up_write(&rdma_nets_rwsem);
+
+ down_read(&devices_rwsem);
+ xa_for_each (&devices, index, dev) {
+ get_device(&dev->dev);
+ /*
+ * Release the devices_rwsem so that pontentially blocking
+ * device_del, doesn't hold the devices_rwsem for too long.
+ */
+ up_read(&devices_rwsem);
+
+ remove_one_compat_dev(dev, rnet->id);
+
+ put_device(&dev->dev);
+ down_read(&devices_rwsem);
+ }
+ up_read(&devices_rwsem);
+
+ xa_erase(&rdma_nets, rnet->id);
+}
+
+static __net_init int rdma_dev_init_net(struct net *net)
+{
+ struct rdma_dev_net *rnet = net_generic(net, rdma_dev_net_id);
+ unsigned long index;
+ struct ib_device *dev;
+ int ret;
+
+ /* No need to create any compat devices in default init_net. */
+ if (net_eq(net, &init_net))
+ return 0;
+
+ write_pnet(&rnet->net, net);
+
+ ret = xa_alloc(&rdma_nets, &rnet->id, rnet, xa_limit_32b, GFP_KERNEL);
+ if (ret)
+ return ret;
+
+ down_read(&devices_rwsem);
+ xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
+ ret = add_one_compat_dev(dev, rnet);
+ if (ret)
+ break;
+ }
+ up_read(&devices_rwsem);
+
+ if (ret)
+ rdma_dev_exit_net(net);
+
+ return ret;
+}
+
/*
* Assign the unique string device name and the unique device index. This is
* undone by ib_dealloc_device.
@@ -788,6 +1017,13 @@ static void disable_device(struct ib_device *device)
ib_device_put(device);
wait_for_completion(&device->unreg_completion);
+ /*
+ * compat devices must be removed after device refcount drops to zero.
+ * Otherwise init_net() may add more compatdevs after removing compat
+ * devices and before device is disabled.
+ */
+ remove_compat_devs(device);
+
/* Expedite removing unregistered pointers from the hash table */
free_netdevs(device);
}
@@ -830,7 +1066,8 @@ static int enable_device_and_get(struct ib_device *device)
break;
}
up_read(&clients_rwsem);
-
+ if (!ret)
+ ret = add_compat_devs(device);
out:
up_read(&devices_rwsem);
return ret;
@@ -1061,6 +1298,13 @@ void ib_unregister_device_queued(struct ib_device *ib_dev)
}
EXPORT_SYMBOL(ib_unregister_device_queued);
+static struct pernet_operations rdma_dev_net_ops = {
+ .init = rdma_dev_init_net,
+ .exit = rdma_dev_exit_net,
+ .id = &rdma_dev_net_id,
+ .size = sizeof(struct rdma_dev_net),
+};
+
static int assign_client_id(struct ib_client *client)
{
int ret;
@@ -1926,12 +2170,20 @@ static int __init ib_core_init(void)
goto err_sa;
}
+ ret = register_pernet_device(&rdma_dev_net_ops);
+ if (ret) {
+ pr_warn("Couldn't init compat dev. ret %d\n", ret);
+ goto err_compat;
+ }
+
nldev_init();
rdma_nl_register(RDMA_NL_LS, ibnl_ls_cb_table);
roce_gid_mgmt_init();
return 0;
+err_compat:
+ unregister_lsm_notifier(&ibdev_lsm_nb);
err_sa:
ib_sa_cleanup();
err_mad:
@@ -1956,6 +2208,7 @@ static void __exit ib_core_cleanup(void)
roce_gid_mgmt_cleanup();
nldev_exit();
rdma_nl_unregister(RDMA_NL_LS);
+ unregister_pernet_device(&rdma_dev_net_ops);
unregister_lsm_notifier(&ibdev_lsm_nb);
ib_sa_cleanup();
ib_mad_cleanup();
diff --git a/include/rdma/ib_verbs.h b/include/rdma/ib_verbs.h
index 5f9f4fcdc4cc..d42267e72c4b 100644
--- a/include/rdma/ib_verbs.h
+++ b/include/rdma/ib_verbs.h
@@ -2559,6 +2559,7 @@ struct ib_core_device {
* union of ib_core_device and device exists in ib_device.
*/
struct device dev;
+ possible_net_t rdma_net;
struct kobject *ports_kobj;
struct list_head port_list;
struct ib_device *owner; /* reach back to owner ib_device */
@@ -2636,6 +2637,11 @@ struct ib_device {
struct work_struct unregistration_work;
const struct rdma_link_ops *link_ops;
+
+ /* Protects compat_devs xarray modifications */
+ struct mutex compat_devs_mutex;
+ /* Maintains compat devices for each net namespace */
+ struct xarray compat_devs;
};
struct ib_client {
]
|
| 56/2 |
2026/05/24 18:26 |
llm |
expert |
0m
Model:
Tokens:
input: 52719
output: 49
thoughts: 2617
Thoughts: **My Analysis of RDMA Device Registration and Namespace Transitions**
Okay, let's break this down. My initial thought was about the potential race condition involving `rdma_dev_init_net` and `rdma_nets_rwsem`, but that's been addressed, as the current code structure correctly handles the locking around `add_one_compat_dev`. The crux of the matter lies in how devices are registered, unregistered, and moved between network namespaces, and especially how the `compat_devs` list is managed within this context.
Initially, I was concerned that `disable_device` might clear `DEVICE_REGISTERED` prematurely, causing `rdma_dev_init_net` to miss a device. However, the use of `devices_rwsem` provides synchronization, ensuring that `rdma_dev_init_net` either sees the device registered or doesn't, maintaining mutual exclusion with `disable_device`. Furthermore, `rdma_dev_init_net` doesn't drop `devices_rwsem` prematurely. This seems safe.
My next concern was related to the potential for a non-empty `compat_devs` list after unregistration. `remove_compat_devs` iterates over `compat_devs` using `xa_for_each` and calls `remove_one_compat_dev`. The use of `xa_erase` within the `xa_for_each` loop triggered concern about concurrent modification. However, `xa_for_each` in the xarray library is designed to handle concurrent removals safely, since it uses RCU, and advances to the next element.
Another consideration was concurrent calls to `add_one_compat_dev` and `remove_compat_devs`. I've checked, the locking mechanism prevents this issue.
Now, let's examine namespace changes. `rdma_dev_change_netns` calls `disable_device`, which clears `DEVICE_REGISTERED` and calls `remove_compat_devs`, then calls `enable_device_and_get`, which sets `DEVICE_REGISTERED`. The critical part here is what happens if `enable_device_and_get` fails. If it fails *after* adding the compat device, the `DEVICE_REGISTERED` flag can remain set, but it should still be cleared eventually. If it fails, `__ib_unregister_device` will be called, which will call `disable_device` again, ensuring that the device's `compat_devs` is cleared, so no stale entries are left in the list. The refcount is handled correctly in the event of failure to prevent use after free.
Concurrent calls between `rdma_dev_change_netns` and `rdma_dev_init_net` are safe because the use of the `DEVICE_REGISTERED` flag, and the exclusive lock `unregistration_lock` and the reader-writer lock `devices_rwsem`, ensure that registration/unregistration either occurs sequentially, or in a way that is handled in the mutual exclusion design.
Finally, I checked for other calls to `add_one_compat_dev`, and ensured that failures within `add_one_compat_dev`, like memory allocation issues, are handled appropriately. Also, `remove_one_compat_dev` and `add_one_compat_dev` serialize via `compat_devs_mutex`.
In conclusion, I'm confident that the current design correctly handles concurrent access and ensures data consistency during device registration, unregistration, and namespace transitions. The use of appropriate locks, the xarray's concurrent handling capabilities, and the careful refcount management are key to this robust behavior.
|
| 57/2 |
2026/05/24 18:27 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:rdma_dev_exit_net]
Results: map[SourceCode:1124: static void rdma_dev_exit_net(struct net *net)
1125: {
1126: struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
1127: struct ib_device *dev;
1128: unsigned long index;
1129: int ret;
1130:
1131: down_write(&rdma_nets_rwsem);
1132: /*
1133: * Prevent the ID from being re-used and hide the id from xa_for_each.
1134: */
1135: ret = xa_err(xa_store(&rdma_nets, rnet->id, NULL, GFP_KERNEL));
1136: WARN_ON(ret);
1137: up_write(&rdma_nets_rwsem);
1138:
1139: down_read(&devices_rwsem);
1140: xa_for_each (&devices, index, dev) {
1141: get_device(&dev->dev);
1142: /*
1143: * Release the devices_rwsem so that pontentially blocking
1144: * device_del, doesn't hold the devices_rwsem for too long.
1145: */
1146: up_read(&devices_rwsem);
1147:
1148: remove_one_compat_dev(dev, rnet->id);
1149:
1150: /*
1151: * If the real device is in the NS then move it back to init.
1152: */
1153: rdma_dev_change_netns(dev, net, &init_net);
1154:
1155: put_device(&dev->dev);
1156: down_read(&devices_rwsem);
1157: }
1158: up_read(&devices_rwsem);
1159:
1160: rdma_nl_net_exit(rnet);
1161: xa_erase(&rdma_nets, rnet->id);
1162: }
SourceFile:drivers/infiniband/core/device.c]
|
| 58/2 |
2026/05/24 18:27 |
llm |
expert |
0m
Model:
Tokens:
input: 56788
output: 40
thoughts: 2304
Thoughts: **Concurrent RDMA Device Management: A Deep Dive**
Okay, so I'm digging into this RDMA device management code, and I'm starting to see how these different functions interact, particularly in the face of concurrency. `rdma_dev_exit_net` seems to be my entry point here. It's iterating through devices and calling `remove_one_compat_dev`, then moving them to the init net. But, that `rdma_dev_change_netns` call... it leads to `disable_device`, which is a key player here. If the device is moved, that call calls `add_compat_devs`. Wait a minute, I already know that `rnet->id` is set to NULL after the devices are added, so `add_compat_devs` will be a no-op which is fine.
Now, the concurrency concerns. `rdma_dev_exit_net` has a read lock on `devices_rwsem` to iterate. Meanwhile, `__ib_unregister_device` also interacts with this. And that's where things get interesting. The kobject refcount is incremented, not `dev->refcount`. And then `rdma_dev_change_netns` checks `device->refcount`. If `__ib_unregister_device` runs first and sets `refcount` to zero, `rdma_dev_change_netns` will correctly return. Likewise, if unregister runs after, that's fine too.
Now, the potential for weirdness is if `__ib_unregister_device` has already called `disable_device` and `ib_dealloc_device`, but then `rdma_dev_exit_net` comes along and calls `remove_one_compat_dev`. `ib_dealloc_device` checks `compat_devs`. No problem, because `disable_device` already calls `remove_compat_devs` to remove all the compat devs.
Okay, let's flip to the init net scenario. `rdma_dev_init_net` is adding compat devs. This function also takes a read lock on `devices_rwsem`. If `__ib_unregister_device` calls `disable_device`, it clears the `DEVICE_REGISTERED` flag. But `rdma_dev_init_net` has the read lock! Thus, `disable_device` must wait for the loop to finish, which means the compat dev gets added before the flag is cleared.
Alright, so all these functions that add compat devs hold that `devices_rwsem` for read, which is good. They're all mutually exclusive with `disable_device`.
But the main concern is: how could `compat_devs` ever be non-empty when `ib_dealloc_device` is called? The check is always done after `remove_compat_devs`. I'm starting to remember that code, so I remember `remove_one_compat_dev` uses `xa_erase`. Thus, the xarray should be empty after this function runs.
So, how can `xa_for_each` miss an element? If something's added after that's done? No, because `DEVICE_REGISTERED` is always cleared. The iteration covers every possible entry and is impossible to miss items. Okay, wait! Is there a race condition where a "reserved" entry is not correctly handled? Remember the xarray reserve, store operation? `xa_reserve` stores a `ZERO` entry in the xarray. Now, `xa_for_each` is filtering with `XA_PRESENT`. It's going to skip `ZERO` entries. That's the problem.
Alright, I think I see it. It's the `xa_reserve` combined with the concurrency.
|
| 59/2 |
2026/05/24 18:27 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:include/linux/xarray.h IncludeLines:true Name:xa_reserve]
Results: map[SourceCode:1067: /**
1068: * xa_reserve() - Reserve this index in the XArray.
1069: * @xa: XArray.
1070: * @index: Index into array.
1071: * @gfp: Memory allocation flags.
1072: *
1073: * Ensures there is somewhere to store an entry at @index in the array.
1074: * If there is already something stored at @index, this function does
1075: * nothing. If there was nothing there, the entry is marked as reserved.
1076: * Loading from a reserved entry returns a %NULL pointer.
1077: *
1078: * If you do not use the entry that you have reserved, call xa_release()
1079: * or xa_erase() to free any unnecessary memory.
1080: *
1081: * Context: Any context. Takes and releases the xa_lock.
1082: * May sleep if the @gfp flags permit.
1083: * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
1084: */
1085: static inline __must_check
1086: int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
1087: {
1088: return xa_err(xa_cmpxchg(xa, index, NULL, XA_ZERO_ENTRY, gfp));
1089: }
SourceFile:include/linux/xarray.h]
|
| 60/2 |
2026/05/24 18:27 |
llm |
expert |
0m
Model:
Tokens:
input: 60372
output: 42
thoughts: 31
Thoughts: Okay, here's my interpretation of those thoughts, presented as my own mental process, given my expertise:
**Checking XArray Iteration with Zero Entries**
Alright, so `xa_reserve` is clearly initialized with `XA_ZERO_ENTRY`. That's standard practice, I get that. Now, the key question, the thing that immediately pops into my head, is whether the `xa_for_each` macro, which is the standard way to iterate through an XArray, is *designed* to handle those zero entries appropriately. Does it skip them? Does it expose them? Does it require special handling? I need to be sure the iteration logic isn't broken by this seemingly innocuous initialization and reservation. The behavior here is critical for data integrity and expected performance when dealing with sparse arrays. I'm mentally running through the implications of how those entries are treated and the downstream effects on algorithms relying on the iteration. The edge cases... always the edge cases. Gotta make sure those zero entries don't throw a wrench into the works.
|
| 61/2 |
2026/05/24 18:27 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:include/linux/xarray.h IncludeLines:true Name:xa_for_each]
Results: map[SourceCode: SourceFile:]
|
| 62/2 |
2026/05/24 18:27 |
llm |
expert |
0m
Model:
Tokens:
input: 60683
output: 21
thoughts: 14
Thoughts: Okay, here's my attempt at summarizing that thought process, tailored for an expert audience:
**Investigating `xa_for_each`**
Alright, time to get digging. I need to understand this `xa_for_each` macro better. I'm going to start with a targeted search. I need to get my hands dirty and dive right into the code to grasp its exact behavior and how it's being used. I'll need to remember to keep an eye out for any specific nuances or optimizations implemented within the macro itself, as well as the context of its usage within the overall system. Time to see what I can find. Let's start with a focused search for that exact macro name.
|
| 63/2 |
2026/05/24 18:27 |
tool |
grepper |
0m
Args: map[Expression:#define xa_for_each]
Results: map[Output:drivers/dpll/dpll_netlink.c-19-
drivers/dpll/dpll_netlink.c:20:#define xa_for_each_marked_start(xa, index, entry, filter, start) \
drivers/dpll/dpll_netlink.c-21- for (index = start, entry = xa_find(xa, &index, ULONG_MAX, filter); \
--
include/linux/xarray.h=422=static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
--
include/linux/xarray.h-450- */
include/linux/xarray.h:451:#define xa_for_each_range(xa, index, entry, start, last) \
include/linux/xarray.h-452- for (index = start, \
--
include/linux/xarray.h-479- */
include/linux/xarray.h:480:#define xa_for_each_start(xa, index, entry, start) \
include/linux/xarray.h-481- xa_for_each_range(xa, index, entry, start, ULONG_MAX)
--
include/linux/xarray.h-503- */
include/linux/xarray.h:504:#define xa_for_each(xa, index, entry) \
include/linux/xarray.h-505- xa_for_each_start(xa, index, entry, 0)
--
include/linux/xarray.h-530- */
include/linux/xarray.h:531:#define xa_for_each_marked(xa, index, entry, filter) \
include/linux/xarray.h-532- for (index = 0, entry = xa_find(xa, &index, ULONG_MAX, filter); \
]
|
| 64/2 |
2026/05/24 18:27 |
llm |
expert |
0m
Model:
Tokens:
input: 61349
output: 44
thoughts: 13
Thoughts: Okay, let's dive into this. Here's my take on those thoughts, as if they're my own:
**Analyzing `xa_for_each_range`**
Alright, let's pull up `xa_for_each_range`. I need to understand its functionality. I'm assuming it's related to iterating through ranges within an XArray, which is a fundamental data structure in the kernel for managing large, sparse arrays. So, the core concept here is efficient traversal, probably focusing on non-contiguous regions.
I expect this is a carefully optimized routine. I'll need to figure out the key optimizations employed. Is it using pre-calculated metadata to avoid looking at every single XArray entry? Does it leverage any knowledge of the underlying data structure to minimize traversal steps? Are there special cases handled, like empty ranges or completely filled ranges?
I'll be looking for how it handles different `XA_STATE` values. I suspect this function would need to take into account metadata, specifically how that metadata is used for identifying contiguous chunks of data and the extent to which it is valid. Are there any assumptions being made? What are the key internal variables it's using? How does it avoid double-counting or getting stuck in infinite loops? And what's the function's return code? I bet it's something that indicates both the success or failure of the iteration, as well as perhaps, something to signal whether the whole list was exhausted or if an action has been taken.
I need to quickly assess how it handles concurrency. Is it safe to use under heavy contention? Does it use any locking primitives or atomics? Knowing the implications of its usage in a concurrent environment is critical for me. I'll look for read-write locks or atomic operations to ensure integrity.
Finally, I'll pay close attention to the code's documentation and any comments. Hopefully, there are decent explanations of its intended usage and any subtle gotchas. That's always my best bet for a rapid understanding of its behavior. Ok, let's get into the details...
|
| 65/2 |
2026/05/24 18:27 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:include/linux/xarray.h IncludeLines:true Name:xa_for_each_range]
Results: map[SourceCode: SourceFile:]
|
| 66/2 |
2026/05/24 18:27 |
llm |
expert |
0m
Model:
Tokens:
input: 61864
output: 23
thoughts: 14
Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience:
**Grasping the xa_for_each_range Macro**
Okay, so I'm thinking about `xa_for_each_range`. Immediately, I'm grepping for it, because that's the fastest way to get a solid grasp of its purpose and usage in the code. I need to see it in context, understand how it's being *used* rather than just its declaration. Knowing the XArray internals, I'm anticipating that this likely relates to iterating over a contiguous set of elements within an XArray. It's almost certainly a macro – efficiency is paramount when dealing with array lookups, especially those likely within hot code paths. I expect it probably abstracts some lower-level calculations to manage the index and handle edge cases gracefully, like jumping across page boundaries or handling the specific data structure nuances of the array. Let's see what the grep search turns up. This is going to give me the quick insight I need to proceed confidently with whatever I'm troubleshooting or analyzing.
|
| 67/2 |
2026/05/24 18:27 |
tool |
grepper |
0m
Args: map[Expression:#define xa_for_each_range]
Results: map[Output:include/linux/xarray.h=422=static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
--
include/linux/xarray.h-450- */
include/linux/xarray.h:451:#define xa_for_each_range(xa, index, entry, start, last) \
include/linux/xarray.h-452- for (index = start, \
]
|
| 68/2 |
2026/05/24 18:27 |
llm |
expert |
0m
Model:
Tokens:
input: 62251
output: 23
thoughts: 24
Thoughts: Okay, let's dive into this. Here's my take on those lines from `include/linux/xarray.h`:
**Analyzing xarray.h: Lines 450-460**
Alright, I'm thinking we need to zero in on lines 450-460 of `include/linux/xarray.h`. Given my background with XArray, these lines are likely related to either allocation, deallocation, lookup, or manipulation of the data structure. It's usually in this region that the core access methods for an XArray are defined. My gut tells me this will be some critical inner loop, potentially dealing with either iterating through the array, or, perhaps, managing the tree-like structure that lies beneath. Let's start with the basics; I'm expecting something to do with the leaf nodes or indexing within an internal node. I have a hunch it might be touching on how the array handles sparse data, given that's a key feature of this data structure. Let's see if the code is using bitmasks, shifts, or any clever tricks to efficiently navigate the different levels of the tree. I am particularly curious to know if it's dealing with the RADIX tree, or a simple array of pointers. I am sure I'll recognize the code pretty quickly based on my knowledge of the system.
|
| 69/2 |
2026/05/24 18:27 |
tool |
grepper |
0m
Args: map[Expression:#define xa_for_each_range]
Results: map[Output:include/linux/xarray.h=422=static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
--
include/linux/xarray.h-450- */
include/linux/xarray.h:451:#define xa_for_each_range(xa, index, entry, start, last) \
include/linux/xarray.h-452- for (index = start, \
]
|
| 70/2 |
2026/05/24 18:28 |
llm |
expert |
0m
Model:
Tokens:
input: 62707
output: 23
thoughts: 20
Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience:
**XArray Header Inclusion Strategy**
Alright, so I'm thinking, for grabbing the contents of `include/linux/xarray.h`, the most straightforward and likely the most performant approach is going to be `read-file`. No need to overcomplicate things here, no fancy parsing or interpretation needed, we just want the raw content. Direct, simple, and effective. We want to avoid any unnecessary overhead.
|
| 71/2 |
2026/05/24 18:28 |
tool |
read-file |
0m
Args: map[File:include/linux/xarray.h]
Results: map[Contents:/* SPDX-License-Identifier: GPL-2.0+ */
#ifndef _LINUX_XARRAY_H
#define _LINUX_XARRAY_H
/*
* eXtensible Arrays
* Copyright (c) 2017 Microsoft Corporation
* Author: Matthew Wilcox <willy@infradead.org>
*
* See Documentation/core-api/xarray.rst for how to use the XArray.
*/
#include <linux/bitmap.h>
#include <linux/bug.h>
#include <linux/compiler.h>
#include <linux/err.h>
#include <linux/gfp.h>
#include <linux/kconfig.h>
#include <linux/limits.h>
#include <linux/lockdep.h>
#include <linux/rcupdate.h>
#include <linux/sched/mm.h>
#include <linux/spinlock.h>
#include <linux/types.h>
struct list_lru;
/*
* The bottom two bits of the entry determine how the XArray interprets
* the contents:
*
* 00: Pointer entry
* 10: Internal entry
* x1: Value entry or tagged pointer
*
* Attempting to store internal entries in the XArray is a bug.
*
* Most internal entries are pointers to the next node in the tree.
* The following internal entries have a special meaning:
*
* 0-62: Sibling entries
* 256: Retry entry
* 257: Zero entry
*
* Errors are also represented as internal entries, but use the negative
* space (-4094 to -2). They're never stored in the slots array; only
* returned by the normal API.
*/
#define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
/**
* xa_mk_value() - Create an XArray entry from an integer.
* @v: Value to store in XArray.
*
* Context: Any context.
* Return: An entry suitable for storing in the XArray.
*/
static inline void *xa_mk_value(unsigned long v)
{
WARN_ON((long)v < 0);
return (void *)((v << 1) | 1);
}
/**
* xa_to_value() - Get value stored in an XArray entry.
* @entry: XArray entry.
*
* Context: Any context.
* Return: The value stored in the XArray entry.
*/
static inline unsigned long xa_to_value(const void *entry)
{
return (unsigned long)entry >> 1;
}
/**
* xa_is_value() - Determine if an entry is a value.
* @entry: XArray entry.
*
* Context: Any context.
* Return: True if the entry is a value, false if it is a pointer.
*/
static inline bool xa_is_value(const void *entry)
{
return (unsigned long)entry & 1;
}
/**
* xa_tag_pointer() - Create an XArray entry for a tagged pointer.
* @p: Plain pointer.
* @tag: Tag value (0, 1 or 3).
*
* If the user of the XArray prefers, they can tag their pointers instead
* of storing value entries. Three tags are available (0, 1 and 3).
* These are distinct from the xa_mark_t as they are not replicated up
* through the array and cannot be searched for.
*
* Context: Any context.
* Return: An XArray entry.
*/
static inline void *xa_tag_pointer(void *p, unsigned long tag)
{
return (void *)((unsigned long)p | tag);
}
/**
* xa_untag_pointer() - Turn an XArray entry into a plain pointer.
* @entry: XArray entry.
*
* If you have stored a tagged pointer in the XArray, call this function
* to get the untagged version of the pointer.
*
* Context: Any context.
* Return: A pointer.
*/
static inline void *xa_untag_pointer(void *entry)
{
return (void *)((unsigned long)entry & ~3UL);
}
/**
* xa_pointer_tag() - Get the tag stored in an XArray entry.
* @entry: XArray entry.
*
* If you have stored a tagged pointer in the XArray, call this function
* to get the tag of that pointer.
*
* Context: Any context.
* Return: A tag.
*/
static inline unsigned int xa_pointer_tag(void *entry)
{
return (unsigned long)entry & 3UL;
}
/*
* xa_mk_internal() - Create an internal entry.
* @v: Value to turn into an internal entry.
*
* Internal entries are used for a number of purposes. Entries 0-255 are
* used for sibling entries (only 0-62 are used by the current code). 256
* is used for the retry entry. 257 is used for the reserved / zero entry.
* Negative internal entries are used to represent errnos. Node pointers
* are also tagged as internal entries in some situations.
*
* Context: Any context.
* Return: An XArray internal entry corresponding to this value.
*/
static inline void *xa_mk_internal(unsigned long v)
{
return (void *)((v << 2) | 2);
}
/*
* xa_to_internal() - Extract the value from an internal entry.
* @entry: XArray entry.
*
* Context: Any context.
* Return: The value which was stored in the internal entry.
*/
static inline unsigned long xa_to_internal(const void *entry)
{
return (unsigned long)entry >> 2;
}
/*
* xa_is_internal() - Is the entry an internal entry?
* @entry: XArray entry.
*
* Context: Any context.
* Return: %true if the entry is an internal entry.
*/
static inline bool xa_is_internal(const void *entry)
{
return ((unsigned long)entry & 3) == 2;
}
#define XA_ZERO_ENTRY xa_mk_internal(257)
/**
* xa_is_zero() - Is the entry a zero entry?
* @entry: Entry retrieved from the XArray
*
* The normal API will return NULL as the contents of a slot containing
* a zero entry. You can only see zero entries by using the advanced API.
*
* Return: %true if the entry is a zero entry.
*/
static inline bool xa_is_zero(const void *entry)
{
return unlikely(entry == XA_ZERO_ENTRY);
}
/**
* xa_is_err() - Report whether an XArray operation returned an error
* @entry: Result from calling an XArray function
*
* If an XArray operation cannot complete an operation, it will return
* a special value indicating an error. This function tells you
* whether an error occurred; xa_err() tells you which error occurred.
*
* Context: Any context.
* Return: %true if the entry indicates an error.
*/
static inline bool xa_is_err(const void *entry)
{
return unlikely(xa_is_internal(entry) &&
entry >= xa_mk_internal(-MAX_ERRNO));
}
/**
* xa_err() - Turn an XArray result into an errno.
* @entry: Result from calling an XArray function.
*
* If an XArray operation cannot complete an operation, it will return
* a special pointer value which encodes an errno. This function extracts
* the errno from the pointer value, or returns 0 if the pointer does not
* represent an errno.
*
* Context: Any context.
* Return: A negative errno or 0.
*/
static inline int xa_err(void *entry)
{
/* xa_to_internal() would not do sign extension. */
if (xa_is_err(entry))
return (long)entry >> 2;
return 0;
}
/**
* struct xa_limit - Represents a range of IDs.
* @min: The lowest ID to allocate (inclusive).
* @max: The maximum ID to allocate (inclusive).
*
* This structure is used either directly or via the XA_LIMIT() macro
* to communicate the range of IDs that are valid for allocation.
* Three common ranges are predefined for you:
* * xa_limit_32b - [0 - UINT_MAX]
* * xa_limit_31b - [0 - INT_MAX]
* * xa_limit_16b - [0 - USHRT_MAX]
*/
struct xa_limit {
u32 max;
u32 min;
};
#define XA_LIMIT(_min, _max) (struct xa_limit) { .min = _min, .max = _max }
#define xa_limit_32b XA_LIMIT(0, UINT_MAX)
#define xa_limit_31b XA_LIMIT(0, INT_MAX)
#define xa_limit_16b XA_LIMIT(0, USHRT_MAX)
typedef unsigned __bitwise xa_mark_t;
#define XA_MARK_0 ((__force xa_mark_t)0U)
#define XA_MARK_1 ((__force xa_mark_t)1U)
#define XA_MARK_2 ((__force xa_mark_t)2U)
#define XA_PRESENT ((__force xa_mark_t)8U)
#define XA_MARK_MAX XA_MARK_2
#define XA_FREE_MARK XA_MARK_0
enum xa_lock_type {
XA_LOCK_IRQ = 1,
XA_LOCK_BH = 2,
};
/*
* Values for xa_flags. The radix tree stores its GFP flags in the xa_flags,
* and we remain compatible with that.
*/
#define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ)
#define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH)
#define XA_FLAGS_TRACK_FREE ((__force gfp_t)4U)
#define XA_FLAGS_ZERO_BUSY ((__force gfp_t)8U)
#define XA_FLAGS_ALLOC_WRAPPED ((__force gfp_t)16U)
#define XA_FLAGS_ACCOUNT ((__force gfp_t)32U)
#define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
(__force unsigned)(mark)))
/* ALLOC is for a normal 0-based alloc. ALLOC1 is for an 1-based alloc */
#define XA_FLAGS_ALLOC (XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
#define XA_FLAGS_ALLOC1 (XA_FLAGS_TRACK_FREE | XA_FLAGS_ZERO_BUSY)
/**
* struct xarray - The anchor of the XArray.
* @xa_lock: Lock that protects the contents of the XArray.
*
* To use the xarray, define it statically or embed it in your data structure.
* It is a very small data structure, so it does not usually make sense to
* allocate it separately and keep a pointer to it in your data structure.
*
* You may use the xa_lock to protect your own data structures as well.
*/
/*
* If all of the entries in the array are NULL, @xa_head is a NULL pointer.
* If the only non-NULL entry in the array is at index 0, @xa_head is that
* entry. If any other entry in the array is non-NULL, @xa_head points
* to an @xa_node.
*/
struct xarray {
spinlock_t xa_lock;
/* private: The rest of the data structure is not to be used directly. */
gfp_t xa_flags;
void __rcu * xa_head;
};
#define XARRAY_INIT(name, flags) { \
.xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \
.xa_flags = flags, \
.xa_head = NULL, \
}
/**
* DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
* @name: A string that names your XArray.
* @flags: XA_FLAG values.
*
* This is intended for file scope definitions of XArrays. It declares
* and initialises an empty XArray with the chosen name and flags. It is
* equivalent to calling xa_init_flags() on the array, but it does the
* initialisation at compiletime instead of runtime.
*/
#define DEFINE_XARRAY_FLAGS(name, flags) \
struct xarray name = XARRAY_INIT(name, flags)
/**
* DEFINE_XARRAY() - Define an XArray.
* @name: A string that names your XArray.
*
* This is intended for file scope definitions of XArrays. It declares
* and initialises an empty XArray with the chosen name. It is equivalent
* to calling xa_init() on the array, but it does the initialisation at
* compiletime instead of runtime.
*/
#define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
/**
* DEFINE_XARRAY_ALLOC() - Define an XArray which allocates IDs starting at 0.
* @name: A string that names your XArray.
*
* This is intended for file scope definitions of allocating XArrays.
* See also DEFINE_XARRAY().
*/
#define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
/**
* DEFINE_XARRAY_ALLOC1() - Define an XArray which allocates IDs starting at 1.
* @name: A string that names your XArray.
*
* This is intended for file scope definitions of allocating XArrays.
* See also DEFINE_XARRAY().
*/
#define DEFINE_XARRAY_ALLOC1(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC1)
void *xa_load(struct xarray *, unsigned long index);
void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
void *xa_erase(struct xarray *, unsigned long index);
void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
void *entry, gfp_t);
bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
void *xa_find(struct xarray *xa, unsigned long *index,
unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
void *xa_find_after(struct xarray *xa, unsigned long *index,
unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
unsigned long max, unsigned int n, xa_mark_t);
void xa_destroy(struct xarray *);
/**
* xa_init_flags() - Initialise an empty XArray with flags.
* @xa: XArray.
* @flags: XA_FLAG values.
*
* If you need to initialise an XArray with special flags (eg you need
* to take the lock from interrupt context), use this function instead
* of xa_init().
*
* Context: Any context.
*/
static inline void xa_init_flags(struct xarray *xa, gfp_t flags)
{
spin_lock_init(&xa->xa_lock);
xa->xa_flags = flags;
xa->xa_head = NULL;
}
/**
* xa_init() - Initialise an empty XArray.
* @xa: XArray.
*
* An empty XArray is full of NULL entries.
*
* Context: Any context.
*/
static inline void xa_init(struct xarray *xa)
{
xa_init_flags(xa, 0);
}
/**
* xa_empty() - Determine if an array has any present entries.
* @xa: XArray.
*
* Context: Any context.
* Return: %true if the array contains only NULL pointers.
*/
static inline bool xa_empty(const struct xarray *xa)
{
return xa->xa_head == NULL;
}
/**
* xa_marked() - Inquire whether any entry in this array has a mark set
* @xa: Array
* @mark: Mark value
*
* Context: Any context.
* Return: %true if any entry has this mark set.
*/
static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
{
return xa->xa_flags & XA_FLAGS_MARK(mark);
}
/**
* xa_for_each_range() - Iterate over a portion of an XArray.
* @xa: XArray.
* @index: Index of @entry.
* @entry: Entry retrieved from array.
* @start: First index to retrieve from array.
* @last: Last index to retrieve from array.
*
* During the iteration, @entry will have the value of the entry stored
* in @xa at @index. You may modify @index during the iteration if you
* want to skip or reprocess indices. It is safe to modify the array
* during the iteration. At the end of the iteration, @entry will be set
* to NULL and @index will have a value less than or equal to max.
*
* xa_for_each_range() is O(n.log(n)) while xas_for_each() is O(n). You have
* to handle your own locking with xas_for_each(), and if you have to unlock
* after each iteration, it will also end up being O(n.log(n)).
* xa_for_each_range() will spin if it hits a retry entry; if you intend to
* see retry entries, you should use the xas_for_each() iterator instead.
* The xas_for_each() iterator will expand into more inline code than
* xa_for_each_range().
*
* Context: Any context. Takes and releases the RCU lock.
*/
#define xa_for_each_range(xa, index, entry, start, last) \
for (index = start, \
entry = xa_find(xa, &index, last, XA_PRESENT); \
entry; \
entry = xa_find_after(xa, &index, last, XA_PRESENT))
/**
* xa_for_each_start() - Iterate over a portion of an XArray.
* @xa: XArray.
* @index: Index of @entry.
* @entry: Entry retrieved from array.
* @start: First index to retrieve from array.
*
* During the iteration, @entry will have the value of the entry stored
* in @xa at @index. You may modify @index during the iteration if you
* want to skip or reprocess indices. It is safe to modify the array
* during the iteration. At the end of the iteration, @entry will be set
* to NULL and @index will have a value less than or equal to max.
*
* xa_for_each_start() is O(n.log(n)) while xas_for_each() is O(n). You have
* to handle your own locking with xas_for_each(), and if you have to unlock
* after each iteration, it will also end up being O(n.log(n)).
* xa_for_each_start() will spin if it hits a retry entry; if you intend to
* see retry entries, you should use the xas_for_each() iterator instead.
* The xas_for_each() iterator will expand into more inline code than
* xa_for_each_start().
*
* Context: Any context. Takes and releases the RCU lock.
*/
#define xa_for_each_start(xa, index, entry, start) \
xa_for_each_range(xa, index, entry, start, ULONG_MAX)
/**
* xa_for_each() - Iterate over present entries in an XArray.
* @xa: XArray.
* @index: Index of @entry.
* @entry: Entry retrieved from array.
*
* During the iteration, @entry will have the value of the entry stored
* in @xa at @index. You may modify @index during the iteration if you want
* to skip or reprocess indices. It is safe to modify the array during the
* iteration. At the end of the iteration, @entry will be set to NULL and
* @index will have a value less than or equal to max.
*
* xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have
* to handle your own locking with xas_for_each(), and if you have to unlock
* after each iteration, it will also end up being O(n.log(n)). xa_for_each()
* will spin if it hits a retry entry; if you intend to see retry entries,
* you should use the xas_for_each() iterator instead. The xas_for_each()
* iterator will expand into more inline code than xa_for_each().
*
* Context: Any context. Takes and releases the RCU lock.
*/
#define xa_for_each(xa, index, entry) \
xa_for_each_start(xa, index, entry, 0)
/**
* xa_for_each_marked() - Iterate over marked entries in an XArray.
* @xa: XArray.
* @index: Index of @entry.
* @entry: Entry retrieved from array.
* @filter: Selection criterion.
*
* During the iteration, @entry will have the value of the entry stored
* in @xa at @index. The iteration will skip all entries in the array
* which do not match @filter. You may modify @index during the iteration
* if you want to skip or reprocess indices. It is safe to modify the array
* during the iteration. At the end of the iteration, @entry will be set to
* NULL and @index will have a value less than or equal to max.
*
* xa_for_each_marked() is O(n.log(n)) while xas_for_each_marked() is O(n).
* You have to handle your own locking with xas_for_each(), and if you have
* to unlock after each iteration, it will also end up being O(n.log(n)).
* xa_for_each_marked() will spin if it hits a retry entry; if you intend to
* see retry entries, you should use the xas_for_each_marked() iterator
* instead. The xas_for_each_marked() iterator will expand into more inline
* code than xa_for_each_marked().
*
* Context: Any context. Takes and releases the RCU lock.
*/
#define xa_for_each_marked(xa, index, entry, filter) \
for (index = 0, entry = xa_find(xa, &index, ULONG_MAX, filter); \
entry; entry = xa_find_after(xa, &index, ULONG_MAX, filter))
#define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
#define xa_lock(xa) spin_lock(&(xa)->xa_lock)
#define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
#define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
#define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
#define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
#define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
#define xa_lock_irqsave(xa, flags) \
spin_lock_irqsave(&(xa)->xa_lock, flags)
#define xa_unlock_irqrestore(xa, flags) \
spin_unlock_irqrestore(&(xa)->xa_lock, flags)
#define xa_lock_nested(xa, subclass) \
spin_lock_nested(&(xa)->xa_lock, subclass)
#define xa_lock_bh_nested(xa, subclass) \
spin_lock_bh_nested(&(xa)->xa_lock, subclass)
#define xa_lock_irq_nested(xa, subclass) \
spin_lock_irq_nested(&(xa)->xa_lock, subclass)
#define xa_lock_irqsave_nested(xa, flags, subclass) \
spin_lock_irqsave_nested(&(xa)->xa_lock, flags, subclass)
/*
* Versions of the normal API which require the caller to hold the
* xa_lock. If the GFP flags allow it, they will drop the lock to
* allocate memory, then reacquire it afterwards. These functions
* may also re-enable interrupts if the XArray flags indicate the
* locking should be interrupt safe.
*/
void *__xa_erase(struct xarray *, unsigned long index);
void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
void *entry, gfp_t);
int __must_check __xa_insert(struct xarray *, unsigned long index,
void *entry, gfp_t);
int __must_check __xa_alloc(struct xarray *, u32 *id, void *entry,
struct xa_limit, gfp_t);
int __must_check __xa_alloc_cyclic(struct xarray *, u32 *id, void *entry,
struct xa_limit, u32 *next, gfp_t);
void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
/**
* xa_store_bh() - Store this entry in the XArray.
* @xa: XArray.
* @index: Index into array.
* @entry: New entry.
* @gfp: Memory allocation flags.
*
* This function is like calling xa_store() except it disables softirqs
* while holding the array lock.
*
* Context: Any context. Takes and releases the xa_lock while
* disabling softirqs.
* Return: The old entry at this index or xa_err() if an error happened.
*/
static inline void *xa_store_bh(struct xarray *xa, unsigned long index,
void *entry, gfp_t gfp)
{
void *curr;
might_alloc(gfp);
xa_lock_bh(xa);
curr = __xa_store(xa, index, entry, gfp);
xa_unlock_bh(xa);
return curr;
}
/**
* xa_store_irq() - Store this entry in the XArray.
* @xa: XArray.
* @index: Index into array.
* @entry: New entry.
* @gfp: Memory allocation flags.
*
* This function is like calling xa_store() except it disables interrupts
* while holding the array lock.
*
* Context: Process context. Takes and releases the xa_lock while
* disabling interrupts.
* Return: The old entry at this index or xa_err() if an error happened.
*/
static inline void *xa_store_irq(struct xarray *xa, unsigned long index,
void *entry, gfp_t gfp)
{
void *curr;
might_alloc(gfp);
xa_lock_irq(xa);
curr = __xa_store(xa, index, entry, gfp);
xa_unlock_irq(xa);
return curr;
}
/**
* xa_erase_bh() - Erase this entry from the XArray.
* @xa: XArray.
* @index: Index of entry.
*
* After this function returns, loading from @index will return %NULL.
* If the index is part of a multi-index entry, all indices will be erased
* and none of the entries will be part of a multi-index entry.
*
* Context: Any context. Takes and releases the xa_lock while
* disabling softirqs.
* Return: The entry which used to be at this index.
*/
static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
{
void *entry;
xa_lock_bh(xa);
entry = __xa_erase(xa, index);
xa_unlock_bh(xa);
return entry;
}
/**
* xa_erase_irq() - Erase this entry from the XArray.
* @xa: XArray.
* @index: Index of entry.
*
* After this function returns, loading from @index will return %NULL.
* If the index is part of a multi-index entry, all indices will be erased
* and none of the entries will be part of a multi-index entry.
*
* Context: Process context. Takes and releases the xa_lock while
* disabling interrupts.
* Return: The entry which used to be at this index.
*/
static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
{
void *entry;
xa_lock_irq(xa);
entry = __xa_erase(xa, index);
xa_unlock_irq(xa);
return entry;
}
/**
* xa_cmpxchg() - Conditionally replace an entry in the XArray.
* @xa: XArray.
* @index: Index into array.
* @old: Old value to test against.
* @entry: New value to place in array.
* @gfp: Memory allocation flags.
*
* If the entry at @index is the same as @old, replace it with @entry.
* If the return value is equal to @old, then the exchange was successful.
*
* Context: Any context. Takes and releases the xa_lock. May sleep
* if the @gfp flags permit.
* Return: The old value at this index or xa_err() if an error happened.
*/
static inline void *xa_cmpxchg(struct xarray *xa, unsigned long index,
void *old, void *entry, gfp_t gfp)
{
void *curr;
might_alloc(gfp);
xa_lock(xa);
curr = __xa_cmpxchg(xa, index, old, entry, gfp);
xa_unlock(xa);
return curr;
}
/**
* xa_cmpxchg_bh() - Conditionally replace an entry in the XArray.
* @xa: XArray.
* @index: Index into array.
* @old: Old value to test against.
* @entry: New value to place in array.
* @gfp: Memory allocation flags.
*
* This function is like calling xa_cmpxchg() except it disables softirqs
* while holding the array lock.
*
* Context: Any context. Takes and releases the xa_lock while
* disabling softirqs. May sleep if the @gfp flags permit.
* Return: The old value at this index or xa_err() if an error happened.
*/
static inline void *xa_cmpxchg_bh(struct xarray *xa, unsigned long index,
void *old, void *entry, gfp_t gfp)
{
void *curr;
might_alloc(gfp);
xa_lock_bh(xa);
curr = __xa_cmpxchg(xa, index, old, entry, gfp);
xa_unlock_bh(xa);
return curr;
}
/**
* xa_cmpxchg_irq() - Conditionally replace an entry in the XArray.
* @xa: XArray.
* @index: Index into array.
* @old: Old value to test against.
* @entry: New value to place in array.
* @gfp: Memory allocation flags.
*
* This function is like calling xa_cmpxchg() except it disables interrupts
* while holding the array lock.
*
* Context: Process context. Takes and releases the xa_lock while
* disabling interrupts. May sleep if the @gfp flags permit.
* Return: The old value at this index or xa_err() if an error happened.
*/
static inline void *xa_cmpxchg_irq(struct xarray *xa, unsigned long index,
void *old, void *entry, gfp_t gfp)
{
void *curr;
might_alloc(gfp);
xa_lock_irq(xa);
curr = __xa_cmpxchg(xa, index, old, entry, gfp);
xa_unlock_irq(xa);
return curr;
}
/**
* xa_insert() - Store this entry in the XArray unless another entry is
* already present.
* @xa: XArray.
* @index: Index into array.
* @entry: New entry.
* @gfp: Memory allocation flags.
*
* Inserting a NULL entry will store a reserved entry (like xa_reserve())
* if no entry is present. Inserting will fail if a reserved entry is
* present, even though loading from this index will return NULL.
*
* Context: Any context. Takes and releases the xa_lock. May sleep if
* the @gfp flags permit.
* Return: 0 if the store succeeded. -EBUSY if another entry was present.
* -ENOMEM if memory could not be allocated.
*/
static inline int __must_check xa_insert(struct xarray *xa,
unsigned long index, void *entry, gfp_t gfp)
{
int err;
might_alloc(gfp);
xa_lock(xa);
err = __xa_insert(xa, index, entry, gfp);
xa_unlock(xa);
return err;
}
/**
* xa_insert_bh() - Store this entry in the XArray unless another entry is
* already present.
* @xa: XArray.
* @index: Index into array.
* @entry: New entry.
* @gfp: Memory allocation flags.
*
* Inserting a NULL entry will store a reserved entry (like xa_reserve())
* if no entry is present. Inserting will fail if a reserved entry is
* present, even though loading from this index will return NULL.
*
* Context: Any context. Takes and releases the xa_lock while
* disabling softirqs. May sleep if the @gfp flags permit.
* Return: 0 if the store succeeded. -EBUSY if another entry was present.
* -ENOMEM if memory could not be allocated.
*/
static inline int __must_check xa_insert_bh(struct xarray *xa,
unsigned long index, void *entry, gfp_t gfp)
{
int err;
might_alloc(gfp);
xa_lock_bh(xa);
err = __xa_insert(xa, index, entry, gfp);
xa_unlock_bh(xa);
return err;
}
/**
* xa_insert_irq() - Store this entry in the XArray unless another entry is
* already present.
* @xa: XArray.
* @index: Index into array.
* @entry: New entry.
* @gfp: Memory allocation flags.
*
* Inserting a NULL entry will store a reserved entry (like xa_reserve())
* if no entry is present. Inserting will fail if a reserved entry is
* present, even though loading from this index will return NULL.
*
* Context: Process context. Takes and releases the xa_lock while
* disabling interrupts. May sleep if the @gfp flags permit.
* Return: 0 if the store succeeded. -EBUSY if another entry was present.
* -ENOMEM if memory could not be allocated.
*/
static inline int __must_check xa_insert_irq(struct xarray *xa,
unsigned long index, void *entry, gfp_t gfp)
{
int err;
might_alloc(gfp);
xa_lock_irq(xa);
err = __xa_insert(xa, index, entry, gfp);
xa_unlock_irq(xa);
return err;
}
/**
* xa_alloc() - Find somewhere to store this entry in the XArray.
* @xa: XArray.
* @id: Pointer to ID.
* @entry: New entry.
* @limit: Range of ID to allocate.
* @gfp: Memory allocation flags.
*
* Finds an empty entry in @xa between @limit.min and @limit.max,
* stores the index into the @id pointer, then stores the entry at
* that index. A concurrent lookup will not see an uninitialised @id.
*
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
* in xa_init_flags().
*
* Context: Any context. Takes and releases the xa_lock. May sleep if
* the @gfp flags permit.
* Return: 0 on success, -ENOMEM if memory could not be allocated or
* -EBUSY if there are no free entries in @limit.
*/
static inline __must_check int xa_alloc(struct xarray *xa, u32 *id,
void *entry, struct xa_limit limit, gfp_t gfp)
{
int err;
might_alloc(gfp);
xa_lock(xa);
err = __xa_alloc(xa, id, entry, limit, gfp);
xa_unlock(xa);
return err;
}
/**
* xa_alloc_bh() - Find somewhere to store this entry in the XArray.
* @xa: XArray.
* @id: Pointer to ID.
* @entry: New entry.
* @limit: Range of ID to allocate.
* @gfp: Memory allocation flags.
*
* Finds an empty entry in @xa between @limit.min and @limit.max,
* stores the index into the @id pointer, then stores the entry at
* that index. A concurrent lookup will not see an uninitialised @id.
*
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
* in xa_init_flags().
*
* Context: Any context. Takes and releases the xa_lock while
* disabling softirqs. May sleep if the @gfp flags permit.
* Return: 0 on success, -ENOMEM if memory could not be allocated or
* -EBUSY if there are no free entries in @limit.
*/
static inline int __must_check xa_alloc_bh(struct xarray *xa, u32 *id,
void *entry, struct xa_limit limit, gfp_t gfp)
{
int err;
might_alloc(gfp);
xa_lock_bh(xa);
err = __xa_alloc(xa, id, entry, limit, gfp);
xa_unlock_bh(xa);
return err;
}
/**
* xa_alloc_irq() - Find somewhere to store this entry in the XArray.
* @xa: XArray.
* @id: Pointer to ID.
* @entry: New entry.
* @limit: Range of ID to allocate.
* @gfp: Memory allocation flags.
*
* Finds an empty entry in @xa between @limit.min and @limit.max,
* stores the index into the @id pointer, then stores the entry at
* that index. A concurrent lookup will not see an uninitialised @id.
*
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
* in xa_init_flags().
*
* Context: Process context. Takes and releases the xa_lock while
* disabling interrupts. May sleep if the @gfp flags permit.
* Return: 0 on success, -ENOMEM if memory could not be allocated or
* -EBUSY if there are no free entries in @limit.
*/
static inline int __must_check xa_alloc_irq(struct xarray *xa, u32 *id,
void *entry, struct xa_limit limit, gfp_t gfp)
{
int err;
might_alloc(gfp);
xa_lock_irq(xa);
err = __xa_alloc(xa, id, entry, limit, gfp);
xa_unlock_irq(xa);
return err;
}
/**
* xa_alloc_cyclic() - Find somewhere to store this entry in the XArray.
* @xa: XArray.
* @id: Pointer to ID.
* @entry: New entry.
* @limit: Range of allocated ID.
* @next: Pointer to next ID to allocate.
* @gfp: Memory allocation flags.
*
* Finds an empty entry in @xa between @limit.min and @limit.max,
* stores the index into the @id pointer, then stores the entry at
* that index. A concurrent lookup will not see an uninitialised @id.
* The search for an empty entry will start at @next and will wrap
* around if necessary.
*
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
* in xa_init_flags().
*
* Note that callers interested in whether wrapping has occurred should
* use __xa_alloc_cyclic() instead.
*
* Context: Any context. Takes and releases the xa_lock. May sleep if
* the @gfp flags permit.
* Return: 0 if the allocation succeeded, -ENOMEM if memory could not be
* allocated or -EBUSY if there are no free entries in @limit.
*/
static inline int xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
struct xa_limit limit, u32 *next, gfp_t gfp)
{
int err;
might_alloc(gfp);
xa_lock(xa);
err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
xa_unlock(xa);
return err < 0 ? err : 0;
}
/**
* xa_alloc_cyclic_bh() - Find somewhere to store this entry in the XArray.
* @xa: XArray.
* @id: Pointer to ID.
* @entry: New entry.
* @limit: Range of allocated ID.
* @next: Pointer to next ID to allocate.
* @gfp: Memory allocation flags.
*
* Finds an empty entry in @xa between @limit.min and @limit.max,
* stores the index into the @id pointer, then stores the entry at
* that index. A concurrent lookup will not see an uninitialised @id.
* The search for an empty entry will start at @next and will wrap
* around if necessary.
*
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
* in xa_init_flags().
*
* Note that callers interested in whether wrapping has occurred should
* use __xa_alloc_cyclic() instead.
*
* Context: Any context. Takes and releases the xa_lock while
* disabling softirqs. May sleep if the @gfp flags permit.
* Return: 0 if the allocation succeeded, -ENOMEM if memory could not be
* allocated or -EBUSY if there are no free entries in @limit.
*/
static inline int xa_alloc_cyclic_bh(struct xarray *xa, u32 *id, void *entry,
struct xa_limit limit, u32 *next, gfp_t gfp)
{
int err;
might_alloc(gfp);
xa_lock_bh(xa);
err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
xa_unlock_bh(xa);
return err < 0 ? err : 0;
}
/**
* xa_alloc_cyclic_irq() - Find somewhere to store this entry in the XArray.
* @xa: XArray.
* @id: Pointer to ID.
* @entry: New entry.
* @limit: Range of allocated ID.
* @next: Pointer to next ID to allocate.
* @gfp: Memory allocation flags.
*
* Finds an empty entry in @xa between @limit.min and @limit.max,
* stores the index into the @id pointer, then stores the entry at
* that index. A concurrent lookup will not see an uninitialised @id.
* The search for an empty entry will start at @next and will wrap
* around if necessary.
*
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
* in xa_init_flags().
*
* Note that callers interested in whether wrapping has occurred should
* use __xa_alloc_cyclic() instead.
*
* Context: Process context. Takes and releases the xa_lock while
* disabling interrupts. May sleep if the @gfp flags permit.
* Return: 0 if the allocation succeeded, -ENOMEM if memory could not be
* allocated or -EBUSY if there are no free entries in @limit.
*/
static inline int xa_alloc_cyclic_irq(struct xarray *xa, u32 *id, void *entry,
struct xa_limit limit, u32 *next, gfp_t gfp)
{
int err;
might_alloc(gfp);
xa_lock_irq(xa);
err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
xa_unlock_irq(xa);
return err < 0 ? err : 0;
}
/**
* xa_reserve() - Reserve this index in the XArray.
* @xa: XArray.
* @index: Index into array.
* @gfp: Memory allocation flags.
*
* Ensures there is somewhere to store an entry at @index in the array.
* If there is already something stored at @index, this function does
* nothing. If there was nothing there, the entry is marked as reserved.
* Loading from a reserved entry returns a %NULL pointer.
*
* If you do not use the entry that you have reserved, call xa_release()
* or xa_erase() to free any unnecessary memory.
*
* Context: Any context. Takes and releases the xa_lock.
* May sleep if the @gfp flags permit.
* Return: 0 if the reservation succeeded or -ENOMEM if it failed.
*/
static inline __must_check
int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
{
return xa_err(xa_cmpxchg(xa, index, NULL, XA_ZERO_ENTRY, gfp));
}
/**
* xa_reserve_bh() - Reserve this index in the XArray.
* @xa: XArray.
* @index: Index into array.
* @gfp: Memory allocation flags.
*
* A softirq-disabling version of xa_reserve().
*
* Context: Any context. Takes and releases the xa_lock while
* disabling softirqs.
* Return: 0 if the reservation succeeded or -ENOMEM if it failed.
*/
static inline __must_check
int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp)
{
return xa_err(xa_cmpxchg_bh(xa, index, NULL, XA_ZERO_ENTRY, gfp));
}
/**
* xa_reserve_irq() - Reserve this index in the XArray.
* @xa: XArray.
* @index: Index into array.
* @gfp: Memory allocation flags.
*
* An interrupt-disabling version of xa_reserve().
*
* Context: Process context. Takes and releases the xa_lock while
* disabling interrupts.
* Return: 0 if the reservation succeeded or -ENOMEM if it failed.
*/
static inline __must_check
int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp)
{
return xa_err(xa_cmpxchg_irq(xa, index, NULL, XA_ZERO_ENTRY, gfp));
}
/**
* xa_release() - Release a reserved entry.
* @xa: XArray.
* @index: Index of entry.
*
* After calling xa_reserve(), you can call this function to release the
* reservation. If the entry at @index has been stored to, this function
* will do nothing.
*/
static inline void xa_release(struct xarray *xa, unsigned long index)
{
xa_cmpxchg(xa, index, XA_ZERO_ENTRY, NULL, 0);
}
/* Everything below here is the Advanced API. Proceed with caution. */
/*
* The xarray is constructed out of a set of 'chunks' of pointers. Choosing
* the best chunk size requires some tradeoffs. A power of two recommends
* itself so that we can walk the tree based purely on shifts and masks.
* Generally, the larger the better; as the number of slots per level of the
* tree increases, the less tall the tree needs to be. But that needs to be
* balanced against the memory consumption of each node. On a 64-bit system,
* xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
* doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
*/
#ifndef XA_CHUNK_SHIFT
#define XA_CHUNK_SHIFT (IS_ENABLED(CONFIG_BASE_SMALL) ? 4 : 6)
#endif
#define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
#define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
#define XA_MAX_MARKS 3
#define XA_MARK_LONGS BITS_TO_LONGS(XA_CHUNK_SIZE)
/*
* @count is the count of every non-NULL element in the ->slots array
* whether that is a value entry, a retry entry, a user pointer,
* a sibling entry or a pointer to the next level of the tree.
* @nr_values is the count of every element in ->slots which is
* either a value entry or a sibling of a value entry.
*/
struct xa_node {
unsigned char shift; /* Bits remaining in each slot */
unsigned char offset; /* Slot offset in parent */
unsigned char count; /* Total entry count */
unsigned char nr_values; /* Value entry count */
struct xa_node __rcu *parent; /* NULL at top of tree */
struct xarray *array; /* The array we belong to */
union {
struct list_head private_list; /* For tree user */
struct rcu_head rcu_head; /* Used when freeing node */
};
void __rcu *slots[XA_CHUNK_SIZE];
union {
unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
};
};
void xa_dump(const struct xarray *);
void xa_dump_node(const struct xa_node *);
#ifdef XA_DEBUG
#define XA_BUG_ON(xa, x) do { \
if (x) { \
xa_dump(xa); \
BUG(); \
} \
} while (0)
#define XA_NODE_BUG_ON(node, x) do { \
if (x) { \
if (node) xa_dump_node(node); \
BUG(); \
} \
} while (0)
#else
#define XA_BUG_ON(xa, x) do { } while (0)
#define XA_NODE_BUG_ON(node, x) do { } while (0)
#endif
/* Private */
static inline void *xa_head(const struct xarray *xa)
{
return rcu_dereference_check(xa->xa_head,
lockdep_is_held(&xa->xa_lock));
}
/* Private */
static inline void *xa_head_locked(const struct xarray *xa)
{
return rcu_dereference_protected(xa->xa_head,
lockdep_is_held(&xa->xa_lock));
}
/* Private */
static inline void *xa_entry(const struct xarray *xa,
const struct xa_node *node, unsigned int offset)
{
XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
return rcu_dereference_check(node->slots[offset],
lockdep_is_held(&xa->xa_lock));
}
/* Private */
static inline void *xa_entry_locked(const struct xarray *xa,
const struct xa_node *node, unsigned int offset)
{
XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
return rcu_dereference_protected(node->slots[offset],
lockdep_is_held(&xa->xa_lock));
}
/* Private */
static inline struct xa_node *xa_parent(const struct xarray *xa,
const struct xa_node *node)
{
return rcu_dereference_check(node->parent,
lockdep_is_held(&xa->xa_lock));
}
/* Private */
static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
const struct xa_node *node)
{
return rcu_dereference_protected(node->parent,
lockdep_is_held(&xa->xa_lock));
}
/* Private */
static inline void *xa_mk_node(const struct xa_node *node)
{
return (void *)((unsigned long)node | 2);
}
/* Private */
static inline struct xa_node *xa_to_node(const void *entry)
{
return (struct xa_node *)((unsigned long)entry - 2);
}
/* Private */
static inline bool xa_is_node(const void *entry)
{
return xa_is_internal(entry) && (unsigned long)entry > 4096;
}
/* Private */
static inline void *xa_mk_sibling(unsigned int offset)
{
return xa_mk_internal(offset);
}
/* Private */
static inline unsigned long xa_to_sibling(const void *entry)
{
return xa_to_internal(entry);
}
/**
* xa_is_sibling() - Is the entry a sibling entry?
* @entry: Entry retrieved from the XArray
*
* Return: %true if the entry is a sibling entry.
*/
static inline bool xa_is_sibling(const void *entry)
{
return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
(entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
}
#define XA_RETRY_ENTRY xa_mk_internal(256)
/**
* xa_is_retry() - Is the entry a retry entry?
* @entry: Entry retrieved from the XArray
*
* Return: %true if the entry is a retry entry.
*/
static inline bool xa_is_retry(const void *entry)
{
return unlikely(entry == XA_RETRY_ENTRY);
}
/**
* xa_is_advanced() - Is the entry only permitted for the advanced API?
* @entry: Entry to be stored in the XArray.
*
* Return: %true if the entry cannot be stored by the normal API.
*/
static inline bool xa_is_advanced(const void *entry)
{
return xa_is_internal(entry) && (entry <= XA_RETRY_ENTRY);
}
/**
* typedef xa_update_node_t - A callback function from the XArray.
* @node: The node which is being processed
*
* This function is called every time the XArray updates the count of
* present and value entries in a node. It allows advanced users to
* maintain the private_list in the node.
*
* Context: The xa_lock is held and interrupts may be disabled.
* Implementations should not drop the xa_lock, nor re-enable
* interrupts.
*/
typedef void (*xa_update_node_t)(struct xa_node *node);
void xa_delete_node(struct xa_node *, xa_update_node_t);
/*
* The xa_state is opaque to its users. It contains various different pieces
* of state involved in the current operation on the XArray. It should be
* declared on the stack and passed between the various internal routines.
* The various elements in it should not be accessed directly, but only
* through the provided accessor functions. The below documentation is for
* the benefit of those working on the code, not for users of the XArray.
*
* @xa_node usually points to the xa_node containing the slot we're operating
* on (and @xa_offset is the offset in the slots array). If there is a
* single entry in the array at index 0, there are no allocated xa_nodes to
* point to, and so we store %NULL in @xa_node. @xa_node is set to
* the value %XAS_RESTART if the xa_state is not walked to the correct
* position in the tree of nodes for this operation. If an error occurs
* during an operation, it is set to an %XAS_ERROR value. If we run off the
* end of the allocated nodes, it is set to %XAS_BOUNDS.
*/
struct xa_state {
struct xarray *xa;
unsigned long xa_index;
unsigned char xa_shift;
unsigned char xa_sibs;
unsigned char xa_offset;
unsigned char xa_pad; /* Helps gcc generate better code */
struct xa_node *xa_node;
struct xa_node *xa_alloc;
xa_update_node_t xa_update;
struct list_lru *xa_lru;
};
/*
* We encode errnos in the xas->xa_node. If an error has happened, we need to
* drop the lock to fix it, and once we've done so the xa_state is invalid.
*/
#define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
#define XAS_BOUNDS ((struct xa_node *)1UL)
#define XAS_RESTART ((struct xa_node *)3UL)
#define __XA_STATE(array, index, shift, sibs) { \
.xa = array, \
.xa_index = index, \
.xa_shift = shift, \
.xa_sibs = sibs, \
.xa_offset = 0, \
.xa_pad = 0, \
.xa_node = XAS_RESTART, \
.xa_alloc = NULL, \
.xa_update = NULL, \
.xa_lru = NULL, \
}
/**
* XA_STATE() - Declare an XArray operation state.
* @name: Name of this operation state (usually xas).
* @array: Array to operate on.
* @index: Initial index of interest.
*
* Declare and initialise an xa_state on the stack.
*/
#define XA_STATE(name, array, index) \
struct xa_state name = __XA_STATE(array, index, 0, 0)
/**
* XA_STATE_ORDER() - Declare an XArray operation state.
* @name: Name of this operation state (usually xas).
* @array: Array to operate on.
* @index: Initial index of interest.
* @order: Order of entry.
*
* Declare and initialise an xa_state on the stack. This variant of
* XA_STATE() allows you to specify the 'order' of the element you
* want to operate on.`
*/
#define XA_STATE_ORDER(name, array, index, order) \
struct xa_state name = __XA_STATE(array, \
(index >> order) << order, \
order - (order % XA_CHUNK_SHIFT), \
(1U << (order % XA_CHUNK_SHIFT)) - 1)
#define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
#define xas_trylock(xas) xa_trylock((xas)->xa)
#define xas_lock(xas) xa_lock((xas)->xa)
#define xas_unlock(xas) xa_unlock((xas)->xa)
#define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
#define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
#define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
#define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
#define xas_lock_irqsave(xas, flags) \
xa_lock_irqsave((xas)->xa, flags)
#define xas_unlock_irqrestore(xas, flags) \
xa_unlock_irqrestore((xas)->xa, flags)
/**
* xas_error() - Return an errno stored in the xa_state.
* @xas: XArray operation state.
*
* Return: 0 if no error has been noted. A negative errno if one has.
*/
static inline int xas_error(const struct xa_state *xas)
{
return xa_err(xas->xa_node);
}
/**
* xas_set_err() - Note an error in the xa_state.
* @xas: XArray operation state.
* @err: Negative error number.
*
* Only call this function with a negative @err; zero or positive errors
* will probably not behave the way you think they should. If you want
* to clear the error from an xa_state, use xas_reset().
*/
static inline void xas_set_err(struct xa_state *xas, long err)
{
xas->xa_node = XA_ERROR(err);
}
/**
* xas_invalid() - Is the xas in a retry or error state?
* @xas: XArray operation state.
*
* Return: %true if the xas cannot be used for operations.
*/
static inline bool xas_invalid(const struct xa_state *xas)
{
return (unsigned long)xas->xa_node & 3;
}
/**
* xas_valid() - Is the xas a valid cursor into the array?
* @xas: XArray operation state.
*
* Return: %true if the xas can be used for operations.
*/
static inline bool xas_valid(const struct xa_state *xas)
{
return !xas_invalid(xas);
}
/**
* xas_is_node() - Does the xas point to a node?
* @xas: XArray operation state.
*
* Return: %true if the xas currently references a node.
*/
static inline bool xas_is_node(const struct xa_state *xas)
{
return xas_valid(xas) && xas->xa_node;
}
/* True if the pointer is something other than a node */
static inline bool xas_not_node(struct xa_node *node)
{
return ((unsigned long)node & 3) || !node;
}
/* True if the node represents RESTART or an error */
static inline bool xas_frozen(struct xa_node *node)
{
return (unsigned long)node & 2;
}
/* True if the node represents head-of-tree, RESTART or BOUNDS */
static inline bool xas_top(struct xa_node *node)
{
return node <= XAS_RESTART;
}
/**
* xas_reset() - Reset an XArray operation state.
* @xas: XArray operation state.
*
* Resets the error or walk state of the @xas so future walks of the
* array will start from the root. Use this if you have dropped the
* xarray lock and want to reuse the xa_state.
*
* Context: Any context.
*/
static inline void xas_reset(struct xa_state *xas)
{
xas->xa_node = XAS_RESTART;
}
/**
* xas_retry() - Retry the operation if appropriate.
* @xas: XArray operation state.
* @entry: Entry from xarray.
*
* The advanced functions may sometimes return an internal entry, such as
* a retry entry or a zero entry. This function sets up the @xas to restart
* the walk from the head of the array if needed.
*
* Context: Any context.
* Return: true if the operation needs to be retried.
*/
static inline bool xas_retry(struct xa_state *xas, const void *entry)
{
if (xa_is_zero(entry))
return true;
if (!xa_is_retry(entry))
return false;
xas_reset(xas);
return true;
}
void *xas_load(struct xa_state *);
void *xas_store(struct xa_state *, void *entry);
void *xas_find(struct xa_state *, unsigned long max);
void *xas_find_conflict(struct xa_state *);
bool xas_get_mark(const struct xa_state *, xa_mark_t);
void xas_set_mark(const struct xa_state *, xa_mark_t);
void xas_clear_mark(const struct xa_state *, xa_mark_t);
void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
void xas_init_marks(const struct xa_state *);
bool xas_nomem(struct xa_state *, gfp_t);
void xas_destroy(struct xa_state *);
void xas_pause(struct xa_state *);
void xas_create_range(struct xa_state *);
#ifdef CONFIG_XARRAY_MULTI
int xa_get_order(struct xarray *, unsigned long index);
int xas_get_order(struct xa_state *xas);
void xas_split(struct xa_state *, void *entry, unsigned int order);
void xas_split_alloc(struct xa_state *, void *entry, unsigned int order, gfp_t);
void xas_try_split(struct xa_state *xas, void *entry, unsigned int order);
unsigned int xas_try_split_min_order(unsigned int order);
#else
static inline int xa_get_order(struct xarray *xa, unsigned long index)
{
return 0;
}
static inline int xas_get_order(struct xa_state *xas)
{
return 0;
}
static inline void xas_split(struct xa_state *xas, void *entry,
unsigned int order)
{
xas_store(xas, entry);
}
static inline void xas_split_alloc(struct xa_state *xas, void *entry,
unsigned int order, gfp_t gfp)
{
}
static inline void xas_try_split(struct xa_state *xas, void *entry,
unsigned int order)
{
}
static inline unsigned int xas_try_split_min_order(unsigned int order)
{
return 0;
}
#endif
/**
* xas_reload() - Refetch an entry from the xarray.
* @xas: XArray operation state.
*
* Use this function to check that a previously loaded entry still has
* the same value. This is useful for the lockless pagecache lookup where
* we walk the array with only the RCU lock to protect us, lock the page,
* then check that the page hasn't moved since we looked it up.
*
* The caller guarantees that @xas is still valid. If it may be in an
* error or restart state, call xas_load() instead.
*
* Return: The entry at this location in the xarray.
*/
static inline void *xas_reload(struct xa_state *xas)
{
struct xa_node *node = xas->xa_node;
void *entry;
char offset;
if (!node)
return xa_head(xas->xa);
if (IS_ENABLED(CONFIG_XARRAY_MULTI)) {
offset = (xas->xa_index >> node->shift) & XA_CHUNK_MASK;
entry = xa_entry(xas->xa, node, offset);
if (!xa_is_sibling(entry))
return entry;
offset = xa_to_sibling(entry);
} else {
offset = xas->xa_offset;
}
return xa_entry(xas->xa, node, offset);
}
/**
* xas_set() - Set up XArray operation state for a different index.
* @xas: XArray operation state.
* @index: New index into the XArray.
*
* Move the operation state to refer to a different index. This will
* have the effect of starting a walk from the top; see xas_next()
* to move to an adjacent index.
*/
static inline void xas_set(struct xa_state *xas, unsigned long index)
{
xas->xa_index = index;
xas->xa_node = XAS_RESTART;
}
/**
* xas_advance() - Skip over sibling entries.
* @xas: XArray operation state.
* @index: Index of last sibling entry.
*
* Move the operation state to refer to the last sibling entry.
* This is useful for loops that normally want to see sibling
* entries but sometimes want to skip them. Use xas_set() if you
* want to move to an index which is not part of this entry.
*/
static inline void xas_advance(struct xa_state *xas, unsigned long index)
{
unsigned char shift = xas_is_node(xas) ? xas->xa_node->shift : 0;
xas->xa_index = index;
xas->xa_offset = (index >> shift) & XA_CHUNK_MASK;
}
/**
* xas_set_order() - Set up XArray operation state for a multislot entry.
* @xas: XArray operation state.
* @index: Target of the operation.
* @order: Entry occupies 2^@order indices.
*/
static inline void xas_set_order(struct xa_state *xas, unsigned long index,
unsigned int order)
{
#ifdef CONFIG_XARRAY_MULTI
xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
xas->xa_node = XAS_RESTART;
#else
BUG_ON(order > 0);
xas_set(xas, index);
#endif
}
/**
* xas_set_update() - Set up XArray operation state for a callback.
* @xas: XArray operation state.
* @update: Function to call when updating a node.
*
* The XArray can notify a caller after it has updated an xa_node.
* This is advanced functionality and is only needed by the page
* cache and swap cache.
*/
static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
{
xas->xa_update = update;
}
static inline void xas_set_lru(struct xa_state *xas, struct list_lru *lru)
{
xas->xa_lru = lru;
}
/**
* xas_next_entry() - Advance iterator to next present entry.
* @xas: XArray operation state.
* @max: Highest index to return.
*
* xas_next_entry() is an inline function to optimise xarray traversal for
* speed. It is equivalent to calling xas_find(), and will call xas_find()
* for all the hard cases.
*
* Return: The next present entry after the one currently referred to by @xas.
*/
static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
{
struct xa_node *node = xas->xa_node;
void *entry;
if (unlikely(xas_not_node(node) || node->shift ||
xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
return xas_find(xas, max);
do {
if (unlikely(xas->xa_index >= max))
return xas_find(xas, max);
if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
return xas_find(xas, max);
entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
if (unlikely(xa_is_internal(entry)))
return xas_find(xas, max);
xas->xa_offset++;
xas->xa_index++;
} while (!entry);
return entry;
}
/* Private */
static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
xa_mark_t mark)
{
unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
unsigned int offset = xas->xa_offset;
if (advance)
offset++;
if (XA_CHUNK_SIZE == BITS_PER_LONG) {
if (offset < XA_CHUNK_SIZE) {
unsigned long data = *addr & (~0UL << offset);
if (data)
return __ffs(data);
}
return XA_CHUNK_SIZE;
}
return find_next_bit(addr, XA_CHUNK_SIZE, offset);
}
/**
* xas_next_marked() - Advance iterator to next marked entry.
* @xas: XArray operation state.
* @max: Highest index to return.
* @mark: Mark to search for.
*
* xas_next_marked() is an inline function to optimise xarray traversal for
* speed. It is equivalent to calling xas_find_marked(), and will call
* xas_find_marked() for all the hard cases.
*
* Return: The next marked entry after the one currently referred to by @xas.
*/
static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
xa_mark_t mark)
{
struct xa_node *node = xas->xa_node;
void *entry;
unsigned int offset;
if (unlikely(xas_not_node(node) || node->shift))
return xas_find_marked(xas, max, mark);
offset = xas_find_chunk(xas, true, mark);
xas->xa_offset = offset;
xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
if (xas->xa_index > max)
return NULL;
if (offset == XA_CHUNK_SIZE)
return xas_find_marked(xas, max, mark);
entry = xa_entry(xas->xa, node, offset);
if (!entry)
return xas_find_marked(xas, max, mark);
return entry;
}
/*
* If iterating while holding a lock, drop the lock and reschedule
* every %XA_CHECK_SCHED loops.
*/
enum {
XA_CHECK_SCHED = 4096,
};
/**
* xas_for_each() - Iterate over a range of an XArray.
* @xas: XArray operation state.
* @entry: Entry retrieved from the array.
* @max: Maximum index to retrieve from array.
*
* The loop body will be executed for each entry present in the xarray
* between the current xas position and @max. @entry will be set to
* the entry retrieved from the xarray. It is safe to delete entries
* from the array in the loop body. You should hold either the RCU lock
* or the xa_lock while iterating. If you need to drop the lock, call
* xas_pause() first.
*/
#define xas_for_each(xas, entry, max) \
for (entry = xas_find(xas, max); entry; \
entry = xas_next_entry(xas, max))
/**
* xas_for_each_marked() - Iterate over a range of an XArray.
* @xas: XArray operation state.
* @entry: Entry retrieved from the array.
* @max: Maximum index to retrieve from array.
* @mark: Mark to search for.
*
* The loop body will be executed for each marked entry in the xarray
* between the current xas position and @max. @entry will be set to
* the entry retrieved from the xarray. It is safe to delete entries
* from the array in the loop body. You should hold either the RCU lock
* or the xa_lock while iterating. If you need to drop the lock, call
* xas_pause() first.
*/
#define xas_for_each_marked(xas, entry, max, mark) \
for (entry = xas_find_marked(xas, max, mark); entry; \
entry = xas_next_marked(xas, max, mark))
/**
* xas_for_each_conflict() - Iterate over a range of an XArray.
* @xas: XArray operation state.
* @entry: Entry retrieved from the array.
*
* The loop body will be executed for each entry in the XArray that
* lies within the range specified by @xas. If the loop terminates
* normally, @entry will be %NULL. The user may break out of the loop,
* which will leave @entry set to the conflicting entry. The caller
* may also call xa_set_err() to exit the loop while setting an error
* to record the reason.
*/
#define xas_for_each_conflict(xas, entry) \
while ((entry = xas_find_conflict(xas)))
void *__xas_next(struct xa_state *);
void *__xas_prev(struct xa_state *);
/**
* xas_prev() - Move iterator to previous index.
* @xas: XArray operation state.
*
* If the @xas was in an error state, it will remain in an error state
* and this function will return %NULL. If the @xas has never been walked,
* it will have the effect of calling xas_load(). Otherwise one will be
* subtracted from the index and the state will be walked to the correct
* location in the array for the next operation.
*
* If the iterator was referencing index 0, this function wraps
* around to %ULONG_MAX.
*
* Return: The entry at the new index. This may be %NULL or an internal
* entry.
*/
static inline void *xas_prev(struct xa_state *xas)
{
struct xa_node *node = xas->xa_node;
if (unlikely(xas_not_node(node) || node->shift ||
xas->xa_offset == 0))
return __xas_prev(xas);
xas->xa_index--;
xas->xa_offset--;
return xa_entry(xas->xa, node, xas->xa_offset);
}
/**
* xas_next() - Move state to next index.
* @xas: XArray operation state.
*
* If the @xas was in an error state, it will remain in an error state
* and this function will return %NULL. If the @xas has never been walked,
* it will have the effect of calling xas_load(). Otherwise one will be
* added to the index and the state will be walked to the correct
* location in the array for the next operation.
*
* If the iterator was referencing index %ULONG_MAX, this function wraps
* around to 0.
*
* Return: The entry at the new index. This may be %NULL or an internal
* entry.
*/
static inline void *xas_next(struct xa_state *xas)
{
struct xa_node *node = xas->xa_node;
if (unlikely(xas_not_node(node) || node->shift ||
xas->xa_offset == XA_CHUNK_MASK))
return __xas_next(xas);
xas->xa_index++;
xas->xa_offset++;
return xa_entry(xas->xa, node, xas->xa_offset);
}
#endif /* _LINUX_XARRAY_H */
]
|
| 72/2 |
2026/05/24 18:28 |
llm |
expert |
0m
Model:
Tokens:
input: 82359
output: 46
thoughts: 5923
Thoughts: **Analyzing the Compat Dev Lifecycle**
Okay, so I'm diving deep into this `xarray` and compat device issue. It seems we're getting a warning from `WARN_ON(!xa_empty(&device->compat_devs))`, which means this `compat_devs` array isn't empty when we expect it to be. The code uses `xa_for_each` and friends to manage these entries, and I need to figure out how entries could persist unexpectedly.
First, I examined `xa_for_each` and confirmed it's supposed to iterate through all entries. It doesn't seem to be skipping anything directly. Then I focused on the entry removal process within `remove_compat_devs` function, specifically how the loop handles removing entries while iterating. The critical check involved making sure that `xa_erase` actually removes the entry, otherwise it may lead to inconsistencies where an entry is found, yet not removed.
My initial thought was that a race condition between adding and removing entries might be the culprit. I looked for concurrent access, but the `compat_devs_mutex` and `devices_rwsem` seem to serialize things correctly, so I don't think that's the issue here.
I then followed the execution path, trying to identify situations where entries could be added, but not removed. I considered several scenarios: `add_one_compat_dev` failures, concurrent execution with `remove_compat_devs`, and issues during device registration/unregistration. I checked to make sure that the `xa_reserve` operation and subsequent memory allocation are handled correctly, with proper cleanup in case of errors.
I zeroed in on the `rdma_dev_change_netns` function, which calls `enable_device_and_get`. I realized that it potentially creates a scenario where the device might be re-enabled after it was disabled. The crucial part here is making sure that `DEVICE_REGISTERED` and any compat dev additions are handled consistently, especially during namespace changes. I also paid close attention to how `__ib_unregister_device` might interact with namespace changes. This required examining the use of `device->unregistration_lock` to ensure proper synchronization.
The key observation is that `disable_device` clears the `DEVICE_REGISTERED` flag *before* calling `remove_compat_devs`. However, `rdma_dev_change_netns` can re-set the flag and potentially re-add the compat devices. This re-addition only works if the `enable_device_and_get` does not fail, otherwise it would prevent re-addition of compat devs and the problem disappears. I made sure to cover the case of potential failures during this operation. After the device is moved to the new namespace and unregistered, the compat devs are properly removed.
So, the warning seems likely to occur if `enable_device_and_get` fails after a namespace change and BEFORE `__ib_unregister_device` runs, then after `ib_device_put` and `disable_device` are called, there will be a non empty `compat_devs`.
|
| 73/2 |
2026/05/24 18:28 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeLines:true Name:ib_register_device]
Results: map[SourceCode:1387: /**
1388: * ib_register_device - Register an IB device with IB core
1389: * @device: Device to register
1390: * @name: unique string device name. This may include a '%' which will
1391: * cause a unique index to be added to the passed device name.
1392: * @dma_device: pointer to a DMA-capable device. If %NULL, then the IB
1393: * device will be used. In this case the caller should fully
1394: * setup the ibdev for DMA. This usually means using dma_virt_ops.
1395: *
1396: * Low-level drivers use ib_register_device() to register their
1397: * devices with the IB core. All registered clients will receive a
1398: * callback for each device that is added. @device must be allocated
1399: * with ib_alloc_device().
1400: *
1401: * If the driver uses ops.dealloc_driver and calls any ib_unregister_device()
1402: * asynchronously then the device pointer may become freed as soon as this
1403: * function returns.
1404: */
1405: int ib_register_device(struct ib_device *device, const char *name,
1406: struct device *dma_device)
1407: {
1408: int ret;
1409:
1410: ret = assign_name(device, name);
1411: if (ret)
1412: return ret;
1413:
1414: /*
1415: * If the caller does not provide a DMA capable device then the IB core
1416: * will set up ib_sge and scatterlist structures that stash the kernel
1417: * virtual address into the address field.
1418: */
1419: WARN_ON(dma_device && !dma_device->dma_parms);
1420: device->dma_device = dma_device;
1421:
1422: ret = setup_device(device);
1423: if (ret)
1424: return ret;
1425:
1426: ret = ib_cache_setup_one(device);
1427: if (ret) {
1428: dev_warn(&device->dev,
1429: "Couldn't set up InfiniBand P_Key/GID cache\n");
1430: return ret;
1431: }
1432:
1433: device->groups[0] = &ib_dev_attr_group;
1434: device->groups[1] = device->ops.device_group;
1435: ret = ib_setup_device_attrs(device);
1436: if (ret)
1437: goto cache_cleanup;
1438:
1439: ib_device_register_rdmacg(device);
1440:
1441: rdma_counter_init(device);
1442:
1443: /*
1444: * Ensure that ADD uevent is not fired because it
1445: * is too early amd device is not initialized yet.
1446: */
1447: dev_set_uevent_suppress(&device->dev, true);
1448: ret = device_add(&device->dev);
1449: if (ret)
1450: goto cg_cleanup;
1451:
1452: ret = ib_setup_port_attrs(&device->coredev);
1453: if (ret) {
1454: dev_warn(&device->dev,
1455: "Couldn't register device with driver model\n");
1456: goto dev_cleanup;
1457: }
1458:
1459: ret = enable_device_and_get(device);
1460: if (ret) {
1461: void (*dealloc_fn)(struct ib_device *);
1462:
1463: /*
1464: * If we hit this error flow then we don't want to
1465: * automatically dealloc the device since the caller is
1466: * expected to call ib_dealloc_device() after
1467: * ib_register_device() fails. This is tricky due to the
1468: * possibility for a parallel unregistration along with this
1469: * error flow. Since we have a refcount here we know any
1470: * parallel flow is stopped in disable_device and will see the
1471: * special dealloc_driver pointer, causing the responsibility to
1472: * ib_dealloc_device() to revert back to this thread.
1473: */
1474: dealloc_fn = device->ops.dealloc_driver;
1475: device->ops.dealloc_driver = prevent_dealloc_device;
1476: ib_device_put(device);
1477: __ib_unregister_device(device);
1478: device->ops.dealloc_driver = dealloc_fn;
1479: dev_set_uevent_suppress(&device->dev, false);
1480: return ret;
1481: }
1482: dev_set_uevent_suppress(&device->dev, false);
1483:
1484: ib_device_notify_register(device);
1485:
1486: ib_device_put(device);
1487:
1488: return 0;
1489:
1490: dev_cleanup:
1491: device_del(&device->dev);
1492: cg_cleanup:
1493: dev_set_uevent_suppress(&device->dev, false);
1494: ib_device_unregister_rdmacg(device);
1495: cache_cleanup:
1496: ib_cache_cleanup_one(device);
1497: return ret;
1498: }
SourceFile:drivers/infiniband/core/device.c]
|
| 74/2 |
2026/05/24 18:28 |
llm |
expert |
0m
Model:
Tokens:
input: 90708
output: 55
thoughts: 5793
Thoughts: **Deep Dive into `compat_devs` and Device Lifecycle**
Okay, let's break this down systematically. My main concern is that `compat_devs` ends up in an inconsistent state, specifically non-empty, when it *should* be empty at deallocation. The core issue revolves around the interaction of device registration, unregistration, and the handling of compatible devices in the RDMA stack.
First, I need to understand the normal flow. If `ib_register_device` succeeds, then, eventually `__ib_unregister_device` is called, which calls `disable_device`. `disable_device` calls `remove_compat_devs` and then *that* should clean out the `compat_devs` xarray. The final step is `ib_dealloc_device`, which asserts that `compat_devs` is empty. The `remove_compat_devs` function seems safe due to the RCU locking inside `xa_for_each`, and also uses `xa_erase` which removes elements properly. The mutex `device->compat_devs_mutex` is protecting these operations too.
Next, I need to consider scenarios that are *not* normal, especially failure paths. If `ib_register_device` fails *before* `enable_device_and_get`, the xarray should never get populated. If `ib_register_device` fails *after* some compatible devices have been added, the code flow is carefully designed to still call `remove_compat_devs` through the failure path that calls `__ib_unregister_device`.
I then investigate a potential problem: if `ib_dealloc_device` is called without calling `__ib_unregister_device`. This path does not seem possible, as there is no way for the `compat_devs` xarray to be filled at all if `ib_register_device` is never called. So, the xarray should be empty.
The other thing that needs to be considered is concurrency. Could `add_one_compat_dev` be running concurrently with `remove_compat_devs`? No, since `DEVICE_REGISTERED` is cleared, so `add_one_compat_dev` can't run concurrently. The `devices_rwsem` also acts as a barrier, further guaranteeing safety.
Now, I scrutinized the code in the case of error handling within `add_one_compat_dev`, specifically, the xarray operations. All of the error handling paths within that function correctly call `xa_release` to clean up the xarray. If `xa_reserve` fails, nothing is reserved, so no problems. If `kzalloc`, `device_add`, or `xa_store` fail, `xa_release` is called, putting `NULL` back into the xarray. The most crucial part is `xa_release`: I confirmed it sets the reserved entry to `NULL`.
A potential issue is that `xa_store` can fail. But it's designed in a way that the *old* entry is returned, so in the reserved slot there is `XA_ZERO_ENTRY`, which is not an error, so there is no problem here.
I then went back to `remove_compat_devs` and confirmed that it is safe to use in a loop with the XArray.
Finally, I investigate all calls to the XArray. I determined that all modifications go through either `add_one_compat_dev` and `remove_one_compat_dev`, which are safe.
After careful review, I believe the only way `compat_devs` could be non-empty is if `disable_device` is not called before `ib_dealloc_device`. This should not happen because if `refcount_read` is not 0, `disable_device` is ALWAYS called, and if `refcount_read` is 0, `ib_dealloc_device` is NEVER called, thus it is impossible for the XArray to be non-empty. So all cases have the `compat_devs` array empty when the `ib_dealloc_device` function is called.
|
| 75/2 |
2026/05/24 18:29 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:drivers/infiniband/core/device.c IncludeSnippetLines:2 Name:ib_dealloc_device SourceTreePrefix:]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__ib_unregister_device SourceFile:drivers/infiniband/core/device.c SourceLine:1545 SourceSnippet:1543: ib_dev->ops.dealloc_driver != prevent_dealloc_device) {
1544: WARN_ON(kref_read(&ib_dev->dev.kobj.kref) <= 1);
1545: ib_dealloc_device(ib_dev);
1546: }
1547: out:
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mlx4_ib_probe SourceFile:drivers/infiniband/hw/mlx4/main.c SourceLine:2904 SourceSnippet:2902:
2903: err_dealloc:
2904: ib_dealloc_device(&ibdev->ib_dev);
2905:
2906: return err;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mlx4_ib_remove SourceFile:drivers/infiniband/hw/mlx4/main.c SourceLine:3014 SourceSnippet:3012: mlx4_uar_free(dev, &ibdev->priv_uar);
3013: mlx4_pd_free(dev, ibdev->priv_pdn);
3014: ib_dealloc_device(&ibdev->ib_dev);
3015: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:rxe_net_add SourceFile:drivers/infiniband/sw/rxe/rxe_net.c SourceLine:620 SourceSnippet: 618: err = rxe_add(rxe, ndev->mtu, ibdev_name, ndev);
619: if (err) {
620: ib_dealloc_device(&rxe->ib_dev);
621: return err;
622: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:siw_device_create SourceFile:drivers/infiniband/sw/siw/siw_main.c SourceLine:360 SourceSnippet: 358: return sdev;
359: error:
360: ib_dealloc_device(base_dev);
361:
362: return NULL;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:siw_newlink SourceFile:drivers/infiniband/sw/siw/siw_main.c SourceLine:432 SourceSnippet: 430: rv = siw_device_register(sdev, basedev_name);
431: if (rv)
432: ib_dealloc_device(&sdev->base_dev);
433: }
434: return rv;
]] TruncatedOutput:false]
|
| 76/2 |
2026/05/24 18:29 |
llm |
expert |
0m
Model:
Tokens:
input: 98291
output: 50
thoughts: 81
Thoughts: Okay, let me break this down.
**Investigating Infiniband Device Deallocation**
Right, so `ib_dealloc_device` is being called from within the driver layer. Interesting. I see a specific example: `mlx4_ib_remove`. Okay, that's a good starting point. Now, let's step back for a moment. Hold on... I'm seeing that `mlx4_ib_remove` *first* calls `ib_unregister_device`, and *then* it proceeds to call `ib_dealloc_device`. That order feels significant. It probably has to do with the device lifecycle. I need to understand that sequence better, so I'm going to dig into the `mlx4_ib_remove` function itself. Understanding the order of unregistration and deallocation is key. I need to confirm my hypothesis.
|
| 77/2 |
2026/05/24 18:29 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/infiniband/hw/mlx4/main.c IncludeLines:true Name:mlx4_ib_remove]
Results: map[SourceCode:2973: static void mlx4_ib_remove(struct auxiliary_device *adev)
2974: {
2975: struct mlx4_adev *madev = container_of(adev, struct mlx4_adev, adev);
2976: struct mlx4_dev *dev = madev->mdev;
2977: struct mlx4_ib_dev *ibdev = auxiliary_get_drvdata(adev);
2978: int p;
2979: int i;
2980:
2981: mlx4_unregister_event_notifier(dev, &ibdev->mlx_nb);
2982:
2983: mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
2984: devlink_port_type_clear(mlx4_get_devlink_port(dev, i));
2985: ibdev->ib_active = false;
2986: flush_workqueue(wq);
2987:
2988: if (ibdev->iboe.nb.notifier_call) {
2989: if (unregister_netdevice_notifier(&ibdev->iboe.nb))
2990: pr_warn("failure unregistering notifier\n");
2991: ibdev->iboe.nb.notifier_call = NULL;
2992: }
2993:
2994: mlx4_ib_close_sriov(ibdev);
2995: mlx4_ib_mad_cleanup(ibdev);
2996: ib_unregister_device(&ibdev->ib_dev);
2997: mlx4_ib_diag_cleanup(ibdev);
2998:
2999: mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
3000: ibdev->steer_qpn_count);
3001: bitmap_free(ibdev->ib_uc_qpns_bitmap);
3002:
3003: iounmap(ibdev->uar_map);
3004: for (p = 0; p < ibdev->num_ports; ++p)
3005: mlx4_ib_delete_counters_table(ibdev, &ibdev->counters_table[p]);
3006:
3007: mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
3008: mlx4_CLOSE_PORT(dev, p);
3009:
3010: mlx4_ib_free_eqs(dev, ibdev);
3011:
3012: mlx4_uar_free(dev, &ibdev->priv_uar);
3013: mlx4_pd_free(dev, ibdev->priv_pdn);
3014: ib_dealloc_device(&ibdev->ib_dev);
3015: }
SourceFile:drivers/infiniband/hw/mlx4/main.c]
|
| 78/2 |
2026/05/24 18:29 |
llm |
expert |
0m
Model:
Tokens:
input: 99418
output: 56
thoughts: 154
Thoughts: **My Analysis of the mlx4_ib Device Removal**
Okay, so `mlx4_ib_remove` first calls `ib_unregister_device`, and then *separately* calls `ib_dealloc_device`. That's a key first observation. Now, I need to understand what happens inside `ib_unregister_device`. Aha, it calls `__ib_unregister_device`. Alright, digging deeper, `__ib_unregister_device` triggers the `disable_device` function.
`disable_device` is where things get interesting. This function looks like it's the core of the unregistration process, clearing the `DEVICE_REGISTERED` flag and, importantly, calling `remove_compat_devs`. I'll need to remember that as potentially useful when tracking other related resources.
Following the execution, `__ib_unregister_device` proceeds to call `ib_dealloc_device`, but only *if* the `dealloc_driver` flag is set. The potential for a double deallocation here just jumped out, and I'm a bit concerned. But, I see that the original function, `mlx4_ib_remove`, calls `ib_dealloc_device` explicitly! This means we could be double deallocating here, which has the potential to crash. The key is in how `dealloc_driver` is being set. It looks like I need to determine whether the `mlx4` driver explicitly sets this flag. If `dealloc_driver` is set, `ib_dealloc_device` is called twice: once during the flow of `__ib_unregister_device` and once explicitly by `mlx4_ib_remove`. This looks like a definite point for further investigation.
|
| 79/2 |
2026/05/24 18:29 |
tool |
codesearch-find-references |
0m
Error:
Args: map[ContextFile:drivers/infiniband/hw/mlx4/main.c IncludeSnippetLines:2 Name:dealloc_driver SourceTreePrefix:]
Results: map[References:<nil> TruncatedOutput:false]
|
| 80/2 |
2026/05/24 18:29 |
llm |
expert |
0m
Model:
Tokens:
input: 100019
output: 18
thoughts: 29
Thoughts: Okay, here's my interpretation of that thought process, as if I were having it:
**Digging into the Infiniband Driver**
Alright, time to get to work. I need to find the definition or usage of `dealloc_driver`. I seem to recall that function being relevant to resource management within the Mellanox Infiniband driver. So, the first logical place to start looking is within the relevant driver code. Since this is an IB device driver, I'm going to jump into the `drivers/infiniband/hw` directory, and the Mellanox implementation is `mlx4`. I will start by focusing on `main.c`, assuming it's a good place to begin searching for core initialization and cleanup functions. Let's see what we find!
|
| 81/2 |
2026/05/24 18:29 |
tool |
grepper |
0m
Args: map[Expression:dealloc_driver]
Results: map[Output:drivers/infiniband/core/device.c=663=void ib_dealloc_device(struct ib_device *device)
drivers/infiniband/core/device.c-664-{
drivers/infiniband/core/device.c:665: if (device->ops.dealloc_driver)
drivers/infiniband/core/device.c:666: device->ops.dealloc_driver(device);
drivers/infiniband/core/device.c-667-
--
drivers/infiniband/core/device.c-669- * ib_unregister_driver() requires all devices to remain in the xarray
drivers/infiniband/core/device.c:670: * while their ops are callable. The last op we call is dealloc_driver
drivers/infiniband/core/device.c-671- * above. This is needed to create a fence on op callbacks prior to
--
drivers/infiniband/core/device.c=1356=static void ib_device_notify_register(struct ib_device *device)
--
drivers/infiniband/core/device.c-1400- *
drivers/infiniband/core/device.c:1401: * If the driver uses ops.dealloc_driver and calls any ib_unregister_device()
drivers/infiniband/core/device.c-1402- * asynchronously then the device pointer may become freed as soon as this
--
drivers/infiniband/core/device.c=1405=int ib_register_device(struct ib_device *device, const char *name,
--
drivers/infiniband/core/device.c-1470- * parallel flow is stopped in disable_device and will see the
drivers/infiniband/core/device.c:1471: * special dealloc_driver pointer, causing the responsibility to
drivers/infiniband/core/device.c-1472- * ib_dealloc_device() to revert back to this thread.
drivers/infiniband/core/device.c-1473- */
drivers/infiniband/core/device.c:1474: dealloc_fn = device->ops.dealloc_driver;
drivers/infiniband/core/device.c:1475: device->ops.dealloc_driver = prevent_dealloc_device;
drivers/infiniband/core/device.c-1476- ib_device_put(device);
drivers/infiniband/core/device.c-1477- __ib_unregister_device(device);
drivers/infiniband/core/device.c:1478: device->ops.dealloc_driver = dealloc_fn;
drivers/infiniband/core/device.c-1479- dev_set_uevent_suppress(&device->dev, false);
--
drivers/infiniband/core/device.c=1502=static void __ib_unregister_device(struct ib_device *ib_dev)
--
drivers/infiniband/core/device.c-1541- */
drivers/infiniband/core/device.c:1542: if (ib_dev->ops.dealloc_driver &&
drivers/infiniband/core/device.c:1543: ib_dev->ops.dealloc_driver != prevent_dealloc_device) {
drivers/infiniband/core/device.c-1544- WARN_ON(kref_read(&ib_dev->dev.kobj.kref) <= 1);
--
drivers/infiniband/core/device.c-1561- *
drivers/infiniband/core/device.c:1562: * If ops.dealloc_driver is used then ib_dev will be freed upon return from
drivers/infiniband/core/device.c-1563- * this function.
--
drivers/infiniband/core/device.c=1587=void ib_unregister_device_and_put(struct ib_device *ib_dev)
drivers/infiniband/core/device.c-1588-{
drivers/infiniband/core/device.c:1589: WARN_ON(!ib_dev->ops.dealloc_driver);
drivers/infiniband/core/device.c-1590- get_device(&ib_dev->dev);
--
drivers/infiniband/core/device.c=1611=void ib_unregister_driver(enum rdma_driver_id driver_id)
--
drivers/infiniband/core/device.c-1623-
drivers/infiniband/core/device.c:1624: WARN_ON(!ib_dev->ops.dealloc_driver);
drivers/infiniband/core/device.c-1625- __ib_unregister_device(ib_dev);
--
drivers/infiniband/core/device.c=1654=void ib_unregister_device_queued(struct ib_device *ib_dev)
--
drivers/infiniband/core/device.c-1656- WARN_ON(!refcount_read(&ib_dev->refcount));
drivers/infiniband/core/device.c:1657: WARN_ON(!ib_dev->ops.dealloc_driver);
drivers/infiniband/core/device.c-1658- get_device(&ib_dev->dev);
--
drivers/infiniband/core/device.c=2683=void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops)
--
drivers/infiniband/core/device.c-2743- SET_DEVICE_OP(dev_ops, dealloc_dmah);
drivers/infiniband/core/device.c:2744: SET_DEVICE_OP(dev_ops, dealloc_driver);
drivers/infiniband/core/device.c-2745- SET_DEVICE_OP(dev_ops, dealloc_mw);
--
drivers/infiniband/hw/irdma/verbs.c=5309=static const struct ib_device_ops irdma_gen1_dev_ops = {
drivers/infiniband/hw/irdma/verbs.c:5310: .dealloc_driver = irdma_ib_dealloc_device,
drivers/infiniband/hw/irdma/verbs.c-5311-};
--
drivers/infiniband/hw/irdma/verbs.c=5350=static const struct ib_device_ops irdma_dev_ops = {
--
drivers/infiniband/hw/irdma/verbs.c-5360- .create_qp = irdma_create_qp,
drivers/infiniband/hw/irdma/verbs.c:5361: .dealloc_driver = irdma_ib_dealloc_device,
drivers/infiniband/hw/irdma/verbs.c-5362- .dealloc_mw = irdma_dealloc_mw,
--
drivers/infiniband/hw/irdma/verbs.c=5477=int irdma_ib_register_device(struct irdma_device *iwdev)
--
drivers/infiniband/hw/irdma/verbs.c-5506- *
drivers/infiniband/hw/irdma/verbs.c:5507: * callback from ibdev dealloc_driver to deallocate resources
drivers/infiniband/hw/irdma/verbs.c-5508- * unber irdma device
--
drivers/infiniband/sw/rxe/rxe_verbs.c=1468=static const struct ib_device_ops rxe_dev_ops = {
--
drivers/infiniband/sw/rxe/rxe_verbs.c-1483- .create_user_ah = rxe_create_ah,
drivers/infiniband/sw/rxe/rxe_verbs.c:1484: .dealloc_driver = rxe_dealloc,
drivers/infiniband/sw/rxe/rxe_verbs.c-1485- .dealloc_mw = rxe_dealloc_mw,
--
drivers/infiniband/sw/siw/siw_main.c=226=static const struct ib_device_ops siw_device_ops = {
--
drivers/infiniband/sw/siw/siw_main.c-236- .create_srq = siw_create_srq,
drivers/infiniband/sw/siw/siw_main.c:237: .dealloc_driver = siw_device_cleanup,
drivers/infiniband/sw/siw/siw_main.c-238- .dealloc_pd = siw_dealloc_pd,
--
include/rdma/ib_verbs.h=2385=struct ib_device_ops {
--
include/rdma/ib_verbs.h-2698- */
include/rdma/ib_verbs.h:2699: void (*dealloc_driver)(struct ib_device *dev);
include/rdma/ib_verbs.h-2700-
]
|
| 82/2 |
2026/05/24 18:29 |
llm |
expert |
0m
Model:
Tokens:
input: 102602
output: 38
thoughts: 2968
Thoughts: **Analyzing the `mlx4` Device Unregistration Flow**
Okay, so I'm digging into how `mlx4` handles device unregistration. My initial observation is that `mlx4` doesn't set `dealloc_driver`, meaning the usual `ib_dealloc_device` call isn't triggered directly from `__ib_unregister_device`. Instead, `mlx4_ib_remove` calls `ib_unregister_device` and then *explicitly* calls `ib_dealloc_device`. This is the "old flow," as the comment notes, and this manual call is where the problem is. The core of this issue hinges on the state of `device->compat_devs` during `ib_dealloc_device`. This structure *should* be empty, but it isn't.
Now, let's trace this step by step. `ib_unregister_device` triggers `__ib_unregister_device`, which eventually calls `disable_device`. Inside `disable_device`, `DEVICE_REGISTERED` is cleared and `remove_compat_devs` is called. So far, so good. Then, `__ib_unregister_device` returns, and then `mlx4_ib_remove` calls `ib_dealloc_device`, where `xa_empty(&device->compat_devs)` is checked.
My initial thought was maybe there's a concurrency issue. What if `rdma_dev_init_net` is called between the calls, maybe adding a compat dev after the device has been unregistered. But, there is a read/write lock for that, so there will be no concurrency.
Then I checked that `add_one_compat_dev` always runs with the `devices_rwsem` lock held, so it would never be able to run at the same time as `disable_device`, which takes `devices_rwsem` for write, preventing the compat dev from being added after unregistration.
If `add_one_compat_dev` were to return an error, then it would release the compat dev, so it should be fine.
I then went through `remove_compat_devs`. I wondered if `xa_for_each` was missing an element, but that shouldn't happen. It iterates through everything that is there. The code iterates through the entries in `compat_devs` and calls `remove_one_compat_dev` for each. But `remove_one_compat_dev` *should* remove the entry completely using `xa_erase`.
Then I double-checked `add_one_compat_dev` to ensure it always cleans up if it errors, but it does. It will release the entry and delete it.
I also considered `rdma_dev_exit_net` since it also calls `remove_one_compat_dev`.
Okay, so `__ib_unregister_device` runs in a workqueue through `ib_unregister_work`. This is just how it is supposed to work.
Is there any concurrency or an issue with the use of `xa_for_each` and how it works? Does the filter matter? Does `XA_PRESENT` miss anything? It uses `xa_find`, and it only finds entries if it matches the filter. The filter is `XA_PRESENT`.
And then, it hits me! I've missed something crucial in the implementation of the `xa_for_each` macro! It is based on `xa_find` which uses the `XA_PRESENT` filter. I need to go deeper into the implementation of the XArray to understand the interaction.
|
| 83/2 |
2026/05/24 18:30 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/xarray.c IncludeLines:true Name:xa_find]
Results: map[SourceCode:2172: /**
2173: * xa_find() - Search the XArray for an entry.
2174: * @xa: XArray.
2175: * @indexp: Pointer to an index.
2176: * @max: Maximum index to search to.
2177: * @filter: Selection criterion.
2178: *
2179: * Finds the entry in @xa which matches the @filter, and has the lowest
2180: * index that is at least @indexp and no more than @max.
2181: * If an entry is found, @indexp is updated to be the index of the entry.
2182: * This function is protected by the RCU read lock, so it may not find
2183: * entries which are being simultaneously added. It will not return an
2184: * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
2185: *
2186: * Context: Any context. Takes and releases the RCU lock.
2187: * Return: The entry, if found, otherwise %NULL.
2188: */
2189: void *xa_find(struct xarray *xa, unsigned long *indexp,
2190: unsigned long max, xa_mark_t filter)
2191: {
2192: XA_STATE(xas, xa, *indexp);
2193: void *entry;
2194:
2195: rcu_read_lock();
2196: do {
2197: if ((__force unsigned int)filter < XA_MAX_MARKS)
2198: entry = xas_find_marked(&xas, max, filter);
2199: else
2200: entry = xas_find(&xas, max);
2201: } while (xas_retry(&xas, entry));
2202: rcu_read_unlock();
2203:
2204: if (entry)
2205: *indexp = xas.xa_index;
2206: return entry;
2207: }
SourceFile:lib/xarray.c]
|
| 84/2 |
2026/05/24 18:30 |
llm |
expert |
0m
Model:
Tokens:
input: 106977
output: 38
thoughts: 57
Thoughts: Okay, here's my take on those thoughts, framed as my own mental processing:
**Analyzing the `xa_find` and `xas_find` Relationship**
Right, so `xa_find` is calling `xas_find` under certain conditions. Aha! It's happening when the `filter` argument is set to `XA_PRESENT`. And that makes sense, given that `XA_PRESENT` has a value of 8, which is greater than or equal to `XA_MAX_MARKS`, which I recall being defined as 3. This means that if `xa_find` receives `XA_PRESENT` as the filter it then defers to `xas_find`. Makes perfect sense. Now, let's just make sure I have this exactly right - time to check the code for `xas_find`.
|
| 85/2 |
2026/05/24 18:30 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/xarray.c IncludeLines:true Name:xas_find]
Results: map[SourceCode:1377: /**
1378: * xas_find() - Find the next present entry in the XArray.
1379: * @xas: XArray operation state.
1380: * @max: Highest index to return.
1381: *
1382: * If the @xas has not yet been walked to an entry, return the entry
1383: * which has an index >= xas.xa_index. If it has been walked, the entry
1384: * currently being pointed at has been processed, and so we move to the
1385: * next entry.
1386: *
1387: * If no entry is found and the array is smaller than @max, the iterator
1388: * is set to the smallest index not yet in the array. This allows @xas
1389: * to be immediately passed to xas_store().
1390: *
1391: * Return: The entry, if found, otherwise %NULL.
1392: */
1393: void *xas_find(struct xa_state *xas, unsigned long max)
1394: {
1395: void *entry;
1396:
1397: if (xas_error(xas) || xas->xa_node == XAS_BOUNDS)
1398: return NULL;
1399: if (xas->xa_index > max)
1400: return set_bounds(xas);
1401:
1402: if (!xas->xa_node) {
1403: xas->xa_index = 1;
1404: return set_bounds(xas);
1405: } else if (xas->xa_node == XAS_RESTART) {
1406: entry = xas_load(xas);
1407: if (entry || xas_not_node(xas->xa_node))
1408: return entry;
1409: } else if (!xas->xa_node->shift &&
1410: xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) {
1411: xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
1412: }
1413:
1414: xas_next_offset(xas);
1415:
1416: while (xas->xa_node && (xas->xa_index <= max)) {
1417: if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1418: xas->xa_offset = xas->xa_node->offset + 1;
1419: xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1420: continue;
1421: }
1422:
1423: entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1424: if (xa_is_node(entry)) {
1425: xas->xa_node = xa_to_node(entry);
1426: xas->xa_offset = 0;
1427: continue;
1428: }
1429: if (entry && !xa_is_sibling(entry))
1430: return entry;
1431:
1432: xas_next_offset(xas);
1433: }
1434:
1435: if (!xas->xa_node)
1436: xas->xa_node = XAS_BOUNDS;
1437: return NULL;
1438: }
SourceFile:lib/xarray.c]
|
| 86/2 |
2026/05/24 18:30 |
llm |
expert |
0m
Model:
Tokens:
input: 108252
output: 39
thoughts: 955
Thoughts: **Analyzing the XArray and `XA_ZERO_ENTRY` Behavior**
Okay, so `xas_find` will return `entry` if `entry` exists and it's not a sibling entry. I need to make sure I understand this correctly. And here I was thinking that `XA_ZERO_ENTRY` was a sibling, but no, because `XA_ZERO_ENTRY` is `xa_mk_internal(257)` and `xa_mk_sibling(63)` is `xa_mk_internal(63)`. So `XA_ZERO_ENTRY` definitely isn't a sibling. So, yes `xas_find` should return `XA_ZERO_ENTRY`.
However, `xa_find` has this loop, which introduces an interesting wrinkle. I need to follow the logic. `xas_retry` gets called. If `xas_retry` returns true, the loop continues. `xas_retry` returns `true` if `xa_is_zero(entry)`. So, if `xas_find` *does* return `XA_ZERO_ENTRY`, `xas_retry` returns `true`. The loop continues!
But wait, does this mean the iterator is reset? No, `xas_retry` does *not* reset the iterator when it encounters `XA_ZERO_ENTRY`. It just returns `true`. So, if `xas_find` returns `XA_ZERO_ENTRY`, the next time it's called, it won't be on the same place! It will find the next entry because the iterator has advanced. So, `xa_find` *skips* `XA_ZERO_ENTRY`! Aha! That makes sense.
Therefore, `xa_for_each` *should* skip `XA_ZERO_ENTRY`! Excellent.
Now, if a reserved entry exists, `remove_compat_devs` *won't* call `remove_one_compat_dev` for that reserved entry. `ib_dealloc_device` then checks `xa_empty(&device->compat_devs)`. `xa_empty` looks for `xa->xa_head == NULL`. If a reserved entry exists, `xa_head` is *not* NULL. So `xa_empty` returns `false`, and the `WARN_ON` triggers!
The big question now is, how could there *be* a reserved entry?
`add_one_compat_dev` is the likely culprit, but I *thought* I had a lock on that. We checked that on a failure it does `xa_release`! And it cannot run concurrently with `remove_compat_devs`! So that doesn't seem likely.
Is there somewhere *else* that might be storing `XA_ZERO_ENTRY`? Could `xa_erase` somehow leave a `XA_ZERO_ENTRY` behind? No, `xa_erase` *removes* the entry, and if the node becomes empty, it frees the node.
Wait. Does this behavior change in allocating XArrays? `compat_devs` are initialized with the `XA_FLAGS_ALLOC`. Let me re-examine the behavior of `xa_erase` with an allocating xarray.
|
| 87/2 |
2026/05/24 18:30 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/xarray.c IncludeLines:true Name:__xa_erase]
Results: map[SourceCode:1634: /**
1635: * __xa_erase() - Erase this entry from the XArray while locked.
1636: * @xa: XArray.
1637: * @index: Index into array.
1638: *
1639: * After this function returns, loading from @index will return %NULL.
1640: * If the index is part of a multi-index entry, all indices will be erased
1641: * and none of the entries will be part of a multi-index entry.
1642: *
1643: * Context: Any context. Expects xa_lock to be held on entry.
1644: * Return: The entry which used to be at this index.
1645: */
1646: void *__xa_erase(struct xarray *xa, unsigned long index)
1647: {
1648: XA_STATE(xas, xa, index);
1649: return xas_result(&xas, xa_zero_to_null(xas_store(&xas, NULL)));
1650: }
SourceFile:lib/xarray.c]
|
| 88/2 |
2026/05/24 18:30 |
llm |
expert |
1m
Model:
Tokens:
input: 110254
output: 66
thoughts: 9949
Thoughts: **Analysis of Infiniband Device Unregistration Bug**
Okay, so I've been wrestling with this syzkaller-reported bug in Infiniband device deallocation. The core issue seems to be a situation where the `compat_devs` xarray within an `ib_device` is not empty when `ib_dealloc_device` is called, triggering a warning and, critically, a memory leak. Here's a breakdown of my thought process:
First, I examined the `xa_erase` and `xa_release` functions to understand how entries are handled in the `compat_devs` xarray. The expectation is that `xa_erase` should clear entries and `xa_release` should ensure that any "dangling" entries get cleaned up. Initially I was worried about `XA_ZERO_ENTRY`s, however, `xa_release` handles that case.
Then, I looked at how `rdma_dev_init_net` and `rdma_dev_exit_net` interact and how they affect the `compat_devs` list. I traced the addition and removal of "compat devs" (core devices) associated with network namespaces, and checked how multiple invocations of these functions would influence the internal state. It seemed that properly managing these devices was paramount.
My main focus was on the function `rdma_dev_change_netns` and how device registration/unregistration are handled. I reviewed the interplay of `enable_device_and_get`, `disable_device`, and `__ib_unregister_device`, especially in error cases or concurrent scenarios. I had to follow the refcounts and locks carefully to understand the ordering of operations. It seemed that `disable_device` was the function clearing the `DEVICE_REGISTERED` mark.
I then analyzed the flow when `enable_device_and_get` fails and that the mark must be set to ensure devices are removed as expected. It also seemed `remove_compat_devs` removes the compat devs. I paid close attention to where `DEVICE_REGISTERED` is set and cleared, as this seems to be the key to add/remove devices.
I then checked all the error cases, and what happened when `enable_device_and_get` failed, particularly *after* `add_compat_devs` and what that would look like. I investigated every possible place were compat devs could be added, or removed, and how each call site interacts with the `DEVICE_REGISTERED` flag. I also had to make sure, how the `compat_devs_mutex`, `devices_rwsem` and `unregistration_lock` protected the functions being called.
I went through many scenarios of concurrent executions to find out if there was a data race or a double-free. I also investigated cases of errors during device addition or deletion. It seemed that the core functions are serialized using a mutex.
I then went through the xarray logic (`xa_reserve`, `xa_store`, `xa_release`, `xa_erase`) to make sure an entry could be leaked, or double freed. It seemed the design of these functions were sound, but they were not implemented correctly. I then went through all the error cases, to make sure nothing was being left over.
However, I kept coming back to the case where `ib_dealloc_device` is called, but `compat_devs` is not empty. It turned out that no new device can be added once `DEVICE_REGISTERED` has been cleared. I tried to figure out how `xa_for_each` could miss an element. Then I tracked the code that frees the device. The conclusion is that if the `compat_devs` xarray is not empty the kobject will be leaked.
This scenario leads to a memory leak because the `cdev` (compatible device) struct, which *should* have been freed during unregistration is leaked. The xarray itself is cleaned up, but the entries are not removed.
Finally, I assessed the impact: the memory leak is a denial-of-service (DoS) vulnerability. Exploitation requires user namespaces to be enabled. An unprivileged user with the appropriate permissions (CAP_SYS_ADMIN) within a user namespace can trigger the leak by creating net namespaces. This requires network manipulation and RDMA device registration/unregistration.
|
| 89/2 |
2026/05/24 18:31 |
tool |
set-results |
0m
Args: map[DenialOfService:true Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:false UserNamespace:true VMGuestTrigger:false VMHostTrigger:false]
Results: map[DenialOfService:true Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:false UserNamespace:true VMGuestTrigger:false VMHostTrigger:false]
|
| 90/2 |
2026/05/24 18:31 |
llm |
expert |
0m
Model:
Tokens:
input: 121252
output: 554
thoughts: 13
Thoughts: Okay, here's the summary, rendered in the first person, assuming the user is an expert:
**Finalizing the Response**
Alright, the deep dive is done. The analysis is comprehensive, and I'm confident in the findings. Now it's time to synthesize everything and craft the definitive response. I've considered all the nuances, weighed the evidence, and formulated a clear and concise framework. The next step is to present this final work in a manner which is suitable for the end user. Everything is ready for me to get started. Time to articulate the conclusions in a way that's both accurate and impactful.
|
| 91/1 |
2026/05/24 18:31 |
action |
format-explanation |
0m
Results: map[Explanation:The bug report indicates a `WARNING` in `ib_dealloc_device` because the
`compat_devs` xarray is not empty when the RDMA device is being deallocated:
`WARN_ON(!xa_empty(&device->compat_devs));`.
### Analysis of the Bug
1. **Mechanism**:
The `compat_devs` xarray is used to maintain compatibility `ib_core_device`
entries for each non-init network namespace. When a new network namespace is
created, `rdma_dev_init_net` is called, which iterates over all registered
RDMA devices and adds a `compat_dev` for the new namespace.
When an RDMA device is unregistered, `disable_device` is called, which clears
the `DEVICE_REGISTERED` mark and calls `remove_compat_devs` to remove all
compatibility devices. Finally, `ib_dealloc_device` is called, which expects
the `compat_devs` xarray to be empty.
The warning triggers if there is a race condition or an error path where a
`compat_dev` is added but not removed, or if a reserved entry
(`XA_ZERO_ENTRY`) is left in the xarray. If the xarray is not empty during
deallocation, the `ib_core_device` structures and their associated kobjects
will be leaked because `xa_destroy` (called later in `ib_device_release`)
only frees the xarray's internal nodes, not the elements themselves.
2. **Security Impact**:
- **Exploitable**: No. This is a memory leak of `ib_core_device` structures
and a `WARNING`. It does not lead to memory corruption, use-after-free, or
wild pointer dereferences.
- **Denial of Service**: Yes. Repeatedly triggering this bug will leak kernel
memory (kobjects and `ib_core_device` structures), eventually leading to
memory exhaustion and a denial of service.
- **Accessible From Unprivileged Processes**: No. Manipulating network
namespaces and RDMA devices requires privileges.
- **Accessible From User Namespaces**: Yes. An unprivileged user can create a
new user namespace to gain `CAP_SYS_ADMIN` locally, and then create new
network namespaces (`CLONE_NEWNET`). This triggers `rdma_dev_init_net` and
exercises the vulnerable code paths.
### Conclusion
This is a local Denial of Service (memory leak) vulnerability reachable via user
namespaces by manipulating network namespaces and RDMA devices. It is not
exploitable for privilege escalation or arbitrary code execution.]
|