| 0/0 |
2026/09/28 05:07 |
flow |
assessment-kcsan |
52m
Results: map[Benign:true Explanation:### Race Summary
`mon_stop` (via `mon_reader_del`) vs `usbmon_urb_submit` (via
`usb_hcd_submit_urb`) on `struct usb_bus::monitored`.
---
### Synchronization Context
- **Writer (`mon_stop` / `mon_reader_del`)**: Invoked when a usbmon reader file
descriptor is closed (e.g. `/dev/usbmon0` in `mon_bin_release`). It executes
with `mbus->lock` (and `mon_lock`) held, clearing `ubus->monitored = 0` when the
number of active readers (`mbus->nreaders`) drops to 0.
- **Reader (`usbmon_urb_submit` / `usb_hcd_submit_urb`)**: Invoked during URB
submission in the USB core fast path. It performs a lockless read `if
(bus->monitored)` to avoid lock overhead when monitoring is inactive.
- **Mutual Exclusion**: None exists between `usb_hcd_submit_urb()` and
`mon_reader_del()`.
---
### Final Classification
**BENIGN**
---
### Reasoning
`bus->monitored` is an aligned 32-bit integer used as a fast-path bypass
heuristic flag:
1. **Tolerance to Stale Reads (`monitored == 0`)**: If the reader sees `0` while
monitoring is being activated or deactivated, it skips invoking
`mon_ops->urb_submit()`. At worst, an URB trace event is omitted right around
the moment when readers are subscribing/unsubscribing. Because reader
registration is inherently asynchronous with respect to URB submissions, this is
standard and harmless.
2. **Tolerance to Stale Reads (`monitored == 1`)**: If the reader sees `1` right
after the last reader closed the device, it invokes `mon_submit()`, which calls
`mon_bus_submit()`. `mon_bus_submit()` acquires `mbus->lock`, the exact same
spinlock held by `mon_reader_del()`. Under `mbus->lock`, the reader list
(`mbus->r_list`) is iterated safely:
- If `mon_bus_submit()` takes the lock after `mon_reader_del()`, `r_list` is
empty, so it does nothing and returns safely.
- The lifetime of `mbus` is governed by `mbus->ref` and is not destroyed by
reader deletion (only when the USB host controller is removed in
`mon_bus_remove()`).
3. **No Tearing or Fused Access Hazards**: `monitored` is an aligned 4-byte
scalar, so single-word reads and writes are atomic on all Linux architectures.
`usbmon_urb_submit()` is invoked once per URB submission (not in a polling/wait
loop), preventing compiler hoisting or livelock issues.
---
### Recommended Annotations
To silence KCSAN and explicitly document the lockless heuristic read/write:
- Use `READ_ONCE(bus->monitored)` in `usbmon_urb_submit()`,
`usbmon_urb_submit_error()`, and `usbmon_urb_complete()` in
`include/linux/usb/hcd.h`.
- Use `WRITE_ONCE(ubus->monitored, 0)` in `mon_stop()` and `mon_dissolve()`, and
`WRITE_ONCE(ubus->monitored, 1)` in `mon_reader_add()` and `mon_bus_add()` in
`drivers/usb/mon/mon_main.c`.]
|
| 1/1 |
2026/09/28 05:07 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/app/workdir/cache/src/a51997b17f9061ace6a8c85f4f13c6d71423ef5d]
|
| 2/1 |
2026/09/28 05:07 |
action |
kernel-builder |
18m
Results: map[KernelObj:/app/workdir/cache/build/6a993b44ac8884b5f7cc045965661e5d840c509e]
|
| 3/1 |
2026/09/28 05:26 |
action |
codesearch-prepare |
13m
Results: map[Index:codesearch-index]
|
| 4/1 |
2026/09/28 05:39 |
agent |
expert |
19m
Model:
Results:
Instruction: # KCSAN Data Race Severity Analysis Protocol
You are an expert Linux kernel concurrency engineer analyzing a Kernel
Concurrency Sanitizer (KCSAN) data race report to determine if it is
**BENIGN** or **HARMFUL**.
## 1. CLASSIFICATIONS
### **BENIGN (Truly Benign)**
The logic is sound and structurally tolerant to compiler optimizations or
stale/torn reads.
- **Diagnostics/Stats:** Reads used only for `/proc`, `/sys`, counters, or
`pr_info`.
- **Heuristic Hints:** A "hint" flag where an old value only causes a
slightly delayed update or a sub-optimal but safe fast-path.
- **Single-Writer Flag Updates:** A single writer updating flags where the
concurrent read is a simple bitwise check (e.g., `flags & MASK`). These are
historically tolerated, assuming neither "Fused Accesses" nor "Ordering
Violations" are relevant in this context.
- **Marked Reloads:** A load feeding into a `cmpxchg()` loop or checked
against a later `READ_ONCE()` reload.
- **Safe Overwrites:** Writing the same value already present.
### **HARMFUL (Logic Bug or Marking Required)**
The race causes incorrect behavior due to a synchronization failure or
because missing annotations allow the compiler to break the algorithm.
**Marking Required for Correctness:**
The algorithm is logically sound but requires annotations (`READ_ONCE()`,
`WRITE_ONCE()`, `smp_load_acquire()`, `smp_store_release()`, etc.) to be safe.
- **Fused Accesses:** The compiler might merge accesses or hoist a load out
of a loop, breaking polling/wait loops (livelocks).
- **Torn Accesses:** A large access (e.g., 64-bit on 32-bit arch) might be
split into multiple non-atomic accesses. Note that `READ_ONCE()` does **not**
guarantee atomicity for 64-bit variables on 32-bit architectures.
- **Ordering Violations:** The race breaks a "happens-before" relationship
(requires primitives with implied or explicit memory barriers).
**Logic Bugs:**
A fundamental synchronization failure. Marking accesses will **not** fix it;
the logic itself must change.
- **Pointers/Lifecycle:** The racing variable is a pointer being dereferenced
or a refcount governing object lifecycle (Use-After-Free risk).
- **Control Flow:** The variable guards a critical section, memory allocation,
or hardware command.
- **Bitfields:** Concurrent writes to different bits in the same word.
Compilers often use non-atomic read-modify-write sequences, meaning a
write to `bit_A` can "clobber" a concurrent write to `bit_B`. However,
do not blindly assume all bitfield accesses are harmful; you must prove
that a concurrent write actually clobbers another in a way that breaks
logic.
- **Complex Structures:** Races on shared lists, trees, or hashmaps.
- **Lossy Updates:** Concurrent plain RMW operations (e.g., `var++`) on
non-diagnostic variables where every increment must be preserved.
- **State Machines:** Races allowing a state machine to bypass transitions
or enter an invalid state.
- **Adjacent Unsynchronized Operations:** Consider races happening at the
same time. For example, if both threads execute `struct->has_elements = true;
list_add(node, &struct->list);`, the race on `has_elements` implies an
adjacent race on `list_head`, which is HARMFUL.
## 2. RESEARCH & ANALYSIS WORKFLOW
1. **Locate the Race:** Find the exact variables and functions in the stack
traces. Do not speculate about hypothetical compiler
behaviors or theoretical dangers (e.g., dismissing something as
"fundamentally unsafe") without tracing the actual data flow to a crash.
2. **Contextualize:** Identify held locks, RCU sections, or interrupt
contexts. Explain why the race is possible (e.g., "Thread A holds `lock`,
but Thread B is a lockless reader").
3. **Data Flow:** Follow the racing variable into its subsequent uses. If
the reader sees a stale or torn value, what branch is taken? Does it lead
to a pointer dereference or `BUG_ON()`? **You must prove harm via this
data flow.**
4. **Failure Sequencing (if HARMFUL):** Construct a two-column execution trace
demonstrating the failure.
*Example format:*
```text
CPU0 CPU1
function_a()
// lockless read
ptr = global_ptr
<PREEMPT>
function_b()
lock(&my_lock)
global_ptr = NULL
unlock(&my_lock)
kfree(ptr)
if (ptr)
*ptr = 1; // -> Use-After-Free!
```
## 3. OUTPUT FORMAT
- **Race Summary:** `[Function A]` vs `[Function B]` on `[Variable/Field]`.
- **Synchronization context:** List held locks and explain the lack of mutual
exclusion.
- **Final Classification:** **BENIGN** or **HARMFUL**.
**If BENIGN:**
- **Reasoning:** Briefly explain why the race is structurally tolerant to stale
or torn reads.
- **Recommended Annotations:** Suggest appropriate annotations (e.g.,
`READ_ONCE()`, `data_race()`).
**If HARMFUL:**
- **Failure Reasoning:** Explain and provide a two-column interleaving showing
exactly how the race leads to a failure such as corruption or kernel crash.
- **Recommended Fix (ONLY if fix is trivial):** Suggest a structural fix (e.g.,
"Hold `mapping->i_pages` lock", "Convert to `atomic_t`") or required memory
ordering annotations (e.g., "Wrap in `READ_ONCE()`", "Use
`smp_load_acquire()`").
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 data race report is:
==================================================================
BUG: KCSAN: data-race in mon_reader_del / usb_hcd_submit_urb
write to 0xffff8881039c5080 of 4 bytes by task 6488 on cpu 0:
mon_stop drivers/usb/mon/mon_main.c:166 [inline]
mon_reader_del+0x1ca/0x260 drivers/usb/mon/mon_main.c:73
mon_bin_release+0x67/0x120 drivers/usb/mon/mon_bin.c:795
__fput+0x29e/0x630 fs/file_table.c:512
____fput+0x1c/0x30 fs/file_table.c:540
task_work_run+0x130/0x1a0 kernel/task_work.c:233
resume_user_mode_work include/linux/resume_user_mode.h:50 [inline]
__exit_to_user_mode_loop kernel/entry/common.c:70 [inline]
exit_to_user_mode_loop+0x1e4/0x8c0 kernel/entry/common.c:101
__exit_to_user_mode_prepare include/linux/irq-entry-common.h:207 [inline]
syscall_exit_to_user_mode_prepare include/linux/irq-entry-common.h:230 [inline]
syscall_exit_to_user_mode include/linux/entry-common.h:343 [inline]
do_syscall_64+0x212/0x360 arch/x86/entry/syscall_64.c:89
entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff8881039c5080 of 4 bytes by task 37 on cpu 1:
usbmon_urb_submit include/linux/usb/hcd.h:723 [inline]
usb_hcd_submit_urb+0x64/0x11f0 drivers/usb/core/hcd.c:1527
usb_submit_urb+0xaa3/0xbb0 drivers/usb/core/urb.c:586
usb_start_wait_urb+0x91/0x220 drivers/usb/core/message.c:62
usb_internal_control_msg drivers/usb/core/message.c:117 [inline]
usb_control_msg+0x182/0x240 drivers/usb/core/message.c:167
get_port_status drivers/usb/core/hub.c:607 [inline]
hub_ext_port_status+0xb4/0x410 drivers/usb/core/hub.c:624
usb_hub_port_status drivers/usb/core/hub.c:674 [inline]
port_event drivers/usb/core/hub.c:5774 [inline]
hub_event+0x47e/0x2aa0 drivers/usb/core/hub.c:5966
process_one_work kernel/workqueue.c:3396 [inline]
process_scheduled_works+0x56a/0xa50 kernel/workqueue.c:3479
worker_thread+0x569/0x750 kernel/workqueue.c:3560
kthread+0x223/0x270 kernel/kthread.c:436
ret_from_fork+0x146/0x330 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
value changed: 0x00000001 -> 0x00000000
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 37 Comm: kworker/1:1 Not tainted syzkaller #0 PREEMPT(lazy)
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 07/24/2026
Workqueue: usb_hub_wq hub_event
==================================================================
Reply: ### Race Summary
`mon_stop` (via `mon_reader_del`) vs `usbmon_urb_submit` (via `usb_hcd_submit_urb`) on `struct usb_bus::monitored`.
---
### Synchronization Context
- **Writer (`mon_stop` / `mon_reader_del`)**: Invoked when a usbmon reader file descriptor is closed (e.g. `/dev/usbmon0` in `mon_bin_release`). It executes with `mbus->lock` (and `mon_lock`) held, clearing `ubus->monitored = 0` when the number of active readers (`mbus->nreaders`) drops to 0.
- **Reader (`usbmon_urb_submit` / `usb_hcd_submit_urb`)**: Invoked during URB submission in the USB core fast path. It performs a lockless read `if (bus->monitored)` to avoid lock overhead when monitoring is inactive.
- **Mutual Exclusion**: None exists between `usb_hcd_submit_urb()` and `mon_reader_del()`.
---
### Final Classification
**BENIGN**
---
### Reasoning
`bus->monitored` is an aligned 32-bit integer used as a fast-path bypass heuristic flag:
1. **Tolerance to Stale Reads (`monitored == 0`)**: If the reader sees `0` while monitoring is being activated or deactivated, it skips invoking `mon_ops->urb_submit()`. At worst, an URB trace event is omitted right around the moment when readers are subscribing/unsubscribing. Because reader registration is inherently asynchronous with respect to URB submissions, this is standard and harmless.
2. **Tolerance to Stale Reads (`monitored == 1`)**: If the reader sees `1` right after the last reader closed the device, it invokes `mon_submit()`, which calls `mon_bus_submit()`. `mon_bus_submit()` acquires `mbus->lock`, the exact same spinlock held by `mon_reader_del()`. Under `mbus->lock`, the reader list (`mbus->r_list`) is iterated safely:
- If `mon_bus_submit()` takes the lock after `mon_reader_del()`, `r_list` is empty, so it does nothing and returns safely.
- The lifetime of `mbus` is governed by `mbus->ref` and is not destroyed by reader deletion (only when the USB host controller is removed in `mon_bus_remove()`).
3. **No Tearing or Fused Access Hazards**: `monitored` is an aligned 4-byte scalar, so single-word reads and writes are atomic on all Linux architectures. `usbmon_urb_submit()` is invoked once per URB submission (not in a polling/wait loop), preventing compiler hoisting or livelock issues.
---
### Recommended Annotations
To silence KCSAN and explicitly document the lockless heuristic read/write:
- Use `READ_ONCE(bus->monitored)` in `usbmon_urb_submit()`, `usbmon_urb_submit_error()`, and `usbmon_urb_complete()` in `include/linux/usb/hcd.h`.
- Use `WRITE_ONCE(ubus->monitored, 0)` in `mon_stop()` and `mon_dissolve()`, and `WRITE_ONCE(ubus->monitored, 1)` in `mon_reader_add()` and `mon_bus_add()` in `drivers/usb/mon/mon_main.c`.
|
| 5/2 |
2026/09/28 05:39 |
llm |
expert |
0m
Model:
Tokens:
input: 6460
output: 115
thoughts: 416
Thoughts: **Analyzing a KCSAN Data Race Report**
Okay, so I've got a KCSAN data race report to look at: `BUG: KCSAN: data-race in mon_reader_del / usb_hcd_submit_urb`. First things first, let's do a quick risk assessment. This looks like a standard debugging task, analyzing a kernel concurrency issue. The purpose is to classify the data race as either benign or potentially harmful, and I'll need to figure out if it needs a fix or annotation. This is definitely on the defensive side of things, related to application maintenance and debugging, and low severity. It's completely safe to proceed with the analysis.
Now, let's dive into the specifics. I need to get my hands dirty with the source code. Luckily, I've got my usual toolkit ready. I'll need to use `read-file`, `codesearch-*`, `grepper`, `git-log`, `git-show`, `git-blame`, and possibly `codeexpert` if this gets hairy. I can also use `set-results` to keep track of any findings.
My immediate focus is on the relevant files. The report highlights:
* **Write:** `drivers/usb/mon/mon_main.c:166` and `mon_main.c:73` (likely near `mon_reader_del`)
* **Read:** `include/linux/usb/hcd.h:723` (part of `usbmon_urb_submit`) and `drivers/usb/core/hcd.c:1527` (where `usb_hcd_submit_urb` resides)
My goal is to understand how these code paths interact and to determine what's actually being raced on. I'll start by reading the code at those locations and see if I can understand what's happening.
|
| 6/2 |
2026/09/28 05:40 |
tool |
read-file |
0m
Args: map[File:drivers/usb/mon/mon_main.c FirstLine:50 LineCount:50]
Results: map[Contents: 50: mbus->u_bus->monitored = 1;
51: }
52: }
53: mbus->nreaders++;
54: list_add_tail(&r->r_link, &mbus->r_list);
55: spin_unlock_irqrestore(&mbus->lock, flags);
56:
57: kref_get(&mbus->ref);
58: }
59:
60: /*
61: * Unlink reader from the bus.
62: *
63: * This is called with mon_lock taken, so we can decrement mbus->ref.
64: */
65: void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
66: {
67: unsigned long flags;
68:
69: spin_lock_irqsave(&mbus->lock, flags);
70: list_del(&r->r_link);
71: --mbus->nreaders;
72: if (mbus->nreaders == 0)
73: mon_stop(mbus);
74: spin_unlock_irqrestore(&mbus->lock, flags);
75:
76: kref_put(&mbus->ref, mon_bus_drop);
77: }
78:
79: /*
80: */
81: static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
82: {
83: unsigned long flags;
84: struct mon_reader *r;
85:
86: spin_lock_irqsave(&mbus->lock, flags);
87: mbus->cnt_events++;
88: list_for_each_entry(r, &mbus->r_list, r_link)
89: r->rnf_submit(r->r_data, urb);
90: spin_unlock_irqrestore(&mbus->lock, flags);
91: }
92:
93: static void mon_submit(struct usb_bus *ubus, struct urb *urb)
94: {
95: struct mon_bus *mbus;
96:
97: mbus = ubus->mon_bus;
98: if (mbus != NULL)
99: mon_bus_submit(mbus, urb);
]
|
| 7/2 |
2026/09/28 05:40 |
tool |
read-file |
0m
Args: map[File:drivers/usb/mon/mon_main.c FirstLine:140 LineCount:50]
Results: map[Contents: 140:
141: static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
142: {
143: struct mon_bus *mbus;
144:
145: mbus = ubus->mon_bus;
146: if (mbus != NULL)
147: mon_bus_complete(mbus, urb, status);
148: mon_bus_complete(&mon_bus0, urb, status);
149: }
150:
151: /* int (*unlink_urb) (struct urb *urb, int status); */
152:
153: /*
154: * Stop monitoring.
155: */
156: static void mon_stop(struct mon_bus *mbus)
157: {
158: struct usb_bus *ubus;
159:
160: if (mbus == &mon_bus0) {
161: list_for_each_entry(mbus, &mon_buses, bus_link) {
162: /*
163: * We do not change nreaders here, so rely on mon_lock.
164: */
165: if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
166: ubus->monitored = 0;
167: }
168: } else {
169: /*
170: * A stop can be called for a dissolved mon_bus in case of
171: * a reader staying across an rmmod foo_hcd, so test ->u_bus.
172: */
173: if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
174: ubus->monitored = 0;
175: mb();
176: }
177: }
178: }
179:
180: /*
181: * Add a USB bus (usually by a modprobe foo-hcd)
182: *
183: * This does not return an error code because the core cannot care less
184: * if monitoring is not established.
185: */
186: static void mon_bus_add(struct usb_bus *ubus)
187: {
188: mon_bus_init(ubus);
189: mutex_lock(&mon_lock);
]
|
| 8/2 |
2026/09/28 05:40 |
tool |
read-file |
0m
Args: map[File:include/linux/usb/hcd.h FirstLine:710 LineCount:35]
Results: map[Contents: 710: #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
711:
712: struct usb_mon_operations {
713: void (*urb_submit)(struct usb_bus *bus, struct urb *urb);
714: void (*urb_submit_error)(struct usb_bus *bus, struct urb *urb, int err);
715: void (*urb_complete)(struct usb_bus *bus, struct urb *urb, int status);
716: /* void (*urb_unlink)(struct usb_bus *bus, struct urb *urb); */
717: };
718:
719: extern const struct usb_mon_operations *mon_ops;
720:
721: static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
722: {
723: if (bus->monitored)
724: (*mon_ops->urb_submit)(bus, urb);
725: }
726:
727: static inline void usbmon_urb_submit_error(struct usb_bus *bus, struct urb *urb,
728: int error)
729: {
730: if (bus->monitored)
731: (*mon_ops->urb_submit_error)(bus, urb, error);
732: }
733:
734: static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb,
735: int status)
736: {
737: if (bus->monitored)
738: (*mon_ops->urb_complete)(bus, urb, status);
739: }
740:
741: int usb_mon_register(const struct usb_mon_operations *ops);
742: void usb_mon_deregister(void);
743:
744: #else
]
|
| 9/2 |
2026/09/28 05:40 |
llm |
expert |
0m
Model:
Tokens:
input: 9375
output: 37
thoughts: 140
Thoughts: Okay, let's break this down.
**Investigating a `usbmon` Issue**
Alright, so the task at hand is to understand how `usbmon` interacts with USB bus monitoring. Specifically, I need to trace the flow from a potential stopping point, `mon_stop`, and how it affects the bus's monitored state. I've noted a couple of specific lines in `mon_reader_del`: line 166 and line 174, where `ubus->monitored = 0;` appears. My hunch is these lines are crucial for disabling the monitoring functionality.
Now, I'm thinking about the inverse path: when a USB URB is submitted via `usbmon_urb_submit`. The key there, is the conditional: `if (bus->monitored)`. This indicates that the monitoring is only active if the `monitored` flag on the `usb_bus` is set.
The next logical step, from an engineering perspective, is to confirm the data structures involved, to gain a proper understanding of the design. Let's delve into the struct `usb_bus` definition to examine the `monitored` field. I need to understand what type it is, and where it lives within the structure to fully reason about how `mon_stop` affects the `usbmon` behavior. Knowing the type and its memory location is key to spotting any memory corruption that may be present. I also need to verify that `monitored` is being updated in the correct thread context, and that the mutexes are correct.
|
| 10/2 |
2026/09/28 05:40 |
tool |
codesearch-struct-layout |
0m
Args: map[ContextFile:include/linux/usb/hcd.h Name:usb_bus]
Results: map[Fields:[map[Name:controller OffsetBits:0 SizeBits:64] map[Name:sysdev OffsetBits:64 SizeBits:64] map[Name:busnum OffsetBits:128 SizeBits:32] map[Name:bus_name OffsetBits:192 SizeBits:64] map[Name:uses_pio_for_control OffsetBits:256 SizeBits:8] map[Name:otg_port OffsetBits:264 SizeBits:8] map[Name:is_b_host OffsetBits:272 SizeBits:1] map[Name:b_hnp_enable OffsetBits:273 SizeBits:1] map[Name:no_stop_on_short OffsetBits:274 SizeBits:1] map[Name:no_sg_constraint OffsetBits:275 SizeBits:1] map[Name:sg_tablesize OffsetBits:288 SizeBits:32] map[Name:devnum_next OffsetBits:320 SizeBits:32] map[Name:devnum_next_mutex OffsetBits:384 SizeBits:192] map[Name:devmap OffsetBits:576 SizeBits:128] map[Name:root_hub OffsetBits:704 SizeBits:64] map[Name:hs_companion OffsetBits:768 SizeBits:64] map[Name:bandwidth_allocated OffsetBits:832 SizeBits:32] map[Name:bandwidth_int_reqs OffsetBits:864 SizeBits:32] map[Name:bandwidth_isoc_reqs OffsetBits:896 SizeBits:32] map[Name:resuming_ports OffsetBits:928 SizeBits:32] map[Name:mon_bus OffsetBits:960 SizeBits:64] map[Name:monitored OffsetBits:1024 SizeBits:32]]]
|
| 11/2 |
2026/09/28 05:40 |
llm |
expert |
0m
Model:
Tokens:
input: 10380
output: 29
thoughts: 68
Thoughts: Okay, let's break this down.
**Decoding This "monitored" Variable**
Right, so I'm looking at this data structure, and the system is telling me that this "monitored" variable is 32 bits, `SizeBits: 32`. I need to figure out what type it is - is it an integer, is it an unsigned 8-bit? The output also shows an `OffsetBits: 1024`, which seems odd. That detail is unhelpful unless I know the data structure. Based on this information alone, it's not immediately clear. Therefore, my next step is clear. Time to dive into the header file. I need to go straight to where the `struct usb_bus` is defined to understand how "monitored" is declared. Knowing the actual declaration is the key.
|
| 12/2 |
2026/09/28 05:40 |
tool |
grepper |
0m
Error:
Args: map[Expression:monitored; PathPrefix:include/linux/usb]
Results:
|
| 13/2 |
2026/09/28 05:40 |
llm |
expert |
0m
Model:
Tokens:
input: 10672
output: 52
thoughts: 12
Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience:
**Tracing the usb_bus Structure Definition**
Alright, let's get down to brass tacks. I need to understand how `struct usb_bus` is defined. Knowing its structure is fundamental to dissecting the USB stack interactions. My goal is to quickly locate its declaration. I'll need to know where to find the source code file where this struct is defined, so I can examine the relevant members and understand its role in representing a USB bus. Itβs the starting point for my analysis, so I need to find it fast.
|
| 14/2 |
2026/09/28 05:40 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:include/linux/usb/hcd.h IncludeSnippetLines:5 Name:usb_bus SourceTreePrefix:include/]
Results: map[References:[map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:bus_to_hcd SourceFile:include/linux/usb/hcd.h SourceLine:229 SourceSnippet: 229: static inline struct usb_hcd *bus_to_hcd(struct usb_bus *bus)
230: {
231: return container_of(bus, struct usb_hcd, self);
232: }
] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:otg_set_host SourceFile:include/linux/usb/otg.h SourceLine:84 SourceSnippet: 82: /* for HCDs */
83: static inline int
84: otg_set_host(struct usb_otg *otg, struct usb_bus *host)
85: {
86: if (otg && otg->set_host)
87: return otg->set_host(otg, host);
88:
89: return -ENOTSUPP;
] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:usbmon_urb_complete SourceFile:include/linux/usb/hcd.h SourceLine:734 SourceSnippet: 734: static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb,
735: int status)
736: {
737: if (bus->monitored)
738: (*mon_ops->urb_complete)(bus, urb, status);
739: }
] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:usbmon_urb_submit SourceFile:include/linux/usb/hcd.h SourceLine:721 SourceSnippet: 721: static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
722: {
723: if (bus->monitored)
724: (*mon_ops->urb_submit)(bus, urb);
725: }
] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:usbmon_urb_submit_error SourceFile:include/linux/usb/hcd.h SourceLine:727 SourceSnippet: 727: static inline void usbmon_urb_submit_error(struct usb_bus *bus, struct urb *urb,
728: int error)
729: {
730: if (bus->monitored)
731: (*mon_ops->urb_submit_error)(bus, urb, error);
732: }
] map[ReferenceKind:uses ReferencingEntityKind:struct ReferencingEntityName:usb_mon_operations SourceFile:include/linux/usb/hcd.h SourceLine:713 SourceSnippet: 712: struct usb_mon_operations {
713: void (*urb_submit)(struct usb_bus *bus, struct urb *urb);
714: void (*urb_submit_error)(struct usb_bus *bus, struct urb *urb, int err);
715: void (*urb_complete)(struct usb_bus *bus, struct urb *urb, int status);
716: /* void (*urb_unlink)(struct usb_bus *bus, struct urb *urb); */
717: };
] map[ReferenceKind:uses ReferencingEntityKind:struct ReferencingEntityName:usb_mon_operations SourceFile:include/linux/usb/hcd.h SourceLine:714 SourceSnippet: 712: struct usb_mon_operations {
713: void (*urb_submit)(struct usb_bus *bus, struct urb *urb);
714: void (*urb_submit_error)(struct usb_bus *bus, struct urb *urb, int err);
715: void (*urb_complete)(struct usb_bus *bus, struct urb *urb, int status);
716: /* void (*urb_unlink)(struct usb_bus *bus, struct urb *urb); */
717: };
] map[ReferenceKind:uses ReferencingEntityKind:struct ReferencingEntityName:usb_mon_operations SourceFile:include/linux/usb/hcd.h SourceLine:715 SourceSnippet: 712: struct usb_mon_operations {
713: void (*urb_submit)(struct usb_bus *bus, struct urb *urb);
714: void (*urb_submit_error)(struct usb_bus *bus, struct urb *urb, int err);
715: void (*urb_complete)(struct usb_bus *bus, struct urb *urb, int status);
716: /* void (*urb_unlink)(struct usb_bus *bus, struct urb *urb); */
717: };
] map[ReferenceKind:uses ReferencingEntityKind:struct ReferencingEntityName:usb_otg SourceFile:include/linux/usb/otg.h SourceLine:28 SourceSnippet: 23: struct usb_gadget *gadget;
24:
25: enum usb_otg_state state;
26:
27: /* bind/unbind the host controller */
28: int (*set_host)(struct usb_otg *otg, struct usb_bus *host);
29:
30: /* bind/unbind the peripheral controller */
31: int (*set_peripheral)(struct usb_otg *otg,
32: struct usb_gadget *gadget);
33:
]] TruncatedOutput:false]
|
| 15/2 |
2026/09/28 05:40 |
llm |
expert |
0m
Model:
Tokens:
input: 12408
output: 29
thoughts: 12
Thoughts: Okay, here's my attempt at summarizing my thought process in first person, suitable for a technical expert:
**Investigating `struct usb_bus`**
Alright, let's get down to brass tacks. I need to understand this `struct usb_bus`. No point in guessing; let's go straight to the source. The best approach, as always, is to dig up the actual definition. I need to understand what this structure *represents* in the USB architecture. Is it a conceptual model, a hardware abstraction, a software construct? Knowing the members, and their meaning within the struct, is the key to understanding its purpose. So, where's the best place to find this definition? Likely in the Linux kernel source, given this is USB related. From there, I can analyze the fields and get a proper grip on how the USB subsystem is really implemented.
|
| 16/2 |
2026/09/28 05:40 |
tool |
grepper |
0m
Args: map[Expression:struct usb_bus \{ PathPrefix:include/linux/]
Results: map[Output:include/linux/usb.h=438=int __usb_get_extra_descriptor(char *buffer, unsigned size,
--
include/linux/usb.h-449- */
include/linux/usb.h:450:struct usb_bus {
include/linux/usb.h-451- struct device *controller; /* host side hardware */
]
|
| 17/2 |
2026/09/28 05:40 |
llm |
expert |
0m
Model:
Tokens:
input: 12733
output: 35
thoughts: 20
Thoughts: Here's a summary of my thought process, tailored for an expert like yourself:
**Diving into USB Core - Specifically, around Line 450 of `include/linux/usb.h`**
Okay, so I'm thinking about digging into the `include/linux/usb.h` header file. It's a fundamental part of the USB subsystem in the Linux kernel, obviously. I'm focusing specifically on around line 450. Knowing the code, I'm anticipating that this section likely defines a crucial data structure or perhaps some important macro definitions relevant to USB device or host controller management. It could be something like descriptors, request blocks, or maybe even core function prototypes, but I'll need to confirm what is present when reading. I anticipate that this section of the file probably deals with some of the more fundamental structures or definitions used to interact with USB devices at a low level. Let's see what interesting tidbits we can find. It's time to get hands-on and confirm my suppositions.
|
| 18/2 |
2026/09/28 05:40 |
tool |
read-file |
0m
Args: map[File:include/linux/usb.h FirstLine:450 LineCount:50]
Results: map[Contents: 450: struct usb_bus {
451: struct device *controller; /* host side hardware */
452: struct device *sysdev; /* as seen from firmware or bus */
453: int busnum; /* Bus number (in order of reg) */
454: const char *bus_name; /* stable id (PCI slot_name etc) */
455: u8 uses_pio_for_control; /*
456: * Does the host controller use PIO
457: * for control transfers?
458: */
459: u8 otg_port; /* 0, or number of OTG/HNP port */
460: unsigned is_b_host:1; /* true during some HNP roleswitches */
461: unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */
462: unsigned no_stop_on_short:1; /*
463: * Quirk: some controllers don't stop
464: * the ep queue on a short transfer
465: * with the URB_SHORT_NOT_OK flag set.
466: */
467: unsigned no_sg_constraint:1; /* no sg constraint */
468: unsigned sg_tablesize; /* 0 or largest number of sg list entries */
469:
470: int devnum_next; /* Next open device number in
471: * round-robin allocation */
472: struct mutex devnum_next_mutex; /* devnum_next mutex */
473:
474: DECLARE_BITMAP(devmap, 128); /* USB device number allocation bitmap */
475: struct usb_device *root_hub; /* Root hub */
476: struct usb_bus *hs_companion; /* Companion EHCI bus, if any */
477:
478: int bandwidth_allocated; /* on this bus: how much of the time
479: * reserved for periodic (intr/iso)
480: * requests is used, on average?
481: * Units: microseconds/frame.
482: * Limits: Full/low speed reserve 90%,
483: * while high speed reserves 80%.
484: */
485: int bandwidth_int_reqs; /* number of Interrupt requests */
486: int bandwidth_isoc_reqs; /* number of Isoc. requests */
487:
488: unsigned resuming_ports; /* bit array: resuming root-hub ports */
489:
490: #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
491: struct mon_bus *mon_bus; /* non-null when associated */
492: int monitored; /* non-zero when monitored */
493: #endif
494: };
495:
496: struct usb_dev_state;
497:
498: /* ----------------------------------------------------------------------- */
499:
]
|
| 19/2 |
2026/09/28 05:40 |
llm |
expert |
0m
Model:
Tokens:
input: 13871
output: 51
thoughts: 51
Thoughts: Okay, here's my take on those thoughts, framed as my own:
**Analyzing the 'monitored' Variable**
Right, let's nail down what's going on with this `monitored` variable. The code defines it as `int monitored; /* non-zero when monitored */`. Okay, so it's a straightforward, regular 32-bit integer. Nothing fancy, no bitfields. This is good to know, confirming my initial expectation. Now, let's make sure we examine *every* instance where `monitored` is used within the kernel. I need to understand how this variable is being set, read, and manipulated across the system to fully grasp its role and potential implications. Time to dive into the cross-references and start tracing its usage.
|
| 20/2 |
2026/09/28 05:40 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:include/linux/usb.h IncludeSnippetLines:5 Name:usb_bus::monitored SourceTreePrefix:]
Results: map[References:[map[ReferenceKind:writes ReferencingEntityKind:function ReferencingEntityName:mon_bus_add SourceFile:drivers/usb/mon/mon_main.c SourceLine:191 SourceSnippet: 186: static void mon_bus_add(struct usb_bus *ubus)
187: {
188: mon_bus_init(ubus);
189: mutex_lock(&mon_lock);
190: if (mon_bus0.nreaders != 0)
191: ubus->monitored = 1;
192: mutex_unlock(&mon_lock);
193: }
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:mon_dissolve SourceFile:drivers/usb/mon/mon_main.c SourceLine:246 SourceSnippet: 241: * Tear usb_bus and mon_bus apart.
242: */
243: static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
244: {
245:
246: if (ubus->monitored) {
247: ubus->monitored = 0;
248: mb();
249: }
250:
251: ubus->mon_bus = NULL;
] map[ReferenceKind:writes ReferencingEntityKind:function ReferencingEntityName:mon_dissolve SourceFile:drivers/usb/mon/mon_main.c SourceLine:247 SourceSnippet: 242: */
243: static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
244: {
245:
246: if (ubus->monitored) {
247: ubus->monitored = 0;
248: mb();
249: }
250:
251: ubus->mon_bus = NULL;
252: mbus->u_bus = NULL;
] map[ReferenceKind:writes ReferencingEntityKind:function ReferencingEntityName:mon_reader_add SourceFile:drivers/usb/mon/mon_main.c SourceLine:47 SourceSnippet: 42: if (mbus->nreaders == 0) {
43: if (mbus == &mon_bus0) {
44: list_for_each (p, &mon_buses) {
45: struct mon_bus *m1;
46: m1 = list_entry(p, struct mon_bus, bus_link);
47: m1->u_bus->monitored = 1;
48: }
49: } else {
50: mbus->u_bus->monitored = 1;
51: }
52: }
] map[ReferenceKind:writes ReferencingEntityKind:function ReferencingEntityName:mon_reader_add SourceFile:drivers/usb/mon/mon_main.c SourceLine:50 SourceSnippet: 45: struct mon_bus *m1;
46: m1 = list_entry(p, struct mon_bus, bus_link);
47: m1->u_bus->monitored = 1;
48: }
49: } else {
50: mbus->u_bus->monitored = 1;
51: }
52: }
53: mbus->nreaders++;
54: list_add_tail(&r->r_link, &mbus->r_list);
55: spin_unlock_irqrestore(&mbus->lock, flags);
] map[ReferenceKind:writes ReferencingEntityKind:function ReferencingEntityName:mon_stop SourceFile:drivers/usb/mon/mon_main.c SourceLine:166 SourceSnippet: 161: list_for_each_entry(mbus, &mon_buses, bus_link) {
162: /*
163: * We do not change nreaders here, so rely on mon_lock.
164: */
165: if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
166: ubus->monitored = 0;
167: }
168: } else {
169: /*
170: * A stop can be called for a dissolved mon_bus in case of
171: * a reader staying across an rmmod foo_hcd, so test ->u_bus.
] map[ReferenceKind:writes ReferencingEntityKind:function ReferencingEntityName:mon_stop SourceFile:drivers/usb/mon/mon_main.c SourceLine:174 SourceSnippet: 169: /*
170: * A stop can be called for a dissolved mon_bus in case of
171: * a reader staying across an rmmod foo_hcd, so test ->u_bus.
172: */
173: if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
174: ubus->monitored = 0;
175: mb();
176: }
177: }
178: }
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:usbmon_urb_complete SourceFile:include/linux/usb/hcd.h SourceLine:737 SourceSnippet: 734: static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb,
735: int status)
736: {
737: if (bus->monitored)
738: (*mon_ops->urb_complete)(bus, urb, status);
739: }
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:usbmon_urb_submit SourceFile:include/linux/usb/hcd.h SourceLine:723 SourceSnippet: 721: static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
722: {
723: if (bus->monitored)
724: (*mon_ops->urb_submit)(bus, urb);
725: }
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:usbmon_urb_submit_error SourceFile:include/linux/usb/hcd.h SourceLine:730 SourceSnippet: 727: static inline void usbmon_urb_submit_error(struct usb_bus *bus, struct urb *urb,
728: int error)
729: {
730: if (bus->monitored)
731: (*mon_ops->urb_submit_error)(bus, urb, error);
732: }
]] TruncatedOutput:false]
|
| 21/2 |
2026/09/28 05:40 |
llm |
expert |
0m
Model:
Tokens:
input: 16098
output: 38
thoughts: 46
Thoughts: Here's my summary, as if I were thinking these thoughts:
**Diving into the USBmon Architecture**
Okay, time to really dig into `usbmon` and specifically that `monitored` flag. I need to understand its design and the implementation details thoroughly. I should probably start by just reading through `drivers/usb/mon/mon_main.c` β maybe the whole thing, or at least substantial chunks. I want to see how the pieces fit together and how that flag is being used. Let's see what interesting tidbits and tricky logic it contains. I'm looking for the key interactions and potential areas for optimization or, well, potential bugs. Let's go.
|
| 22/2 |
2026/09/28 05:40 |
tool |
read-file |
0m
Args: map[File:drivers/usb/mon/mon_main.c FirstLine:1 LineCount:100]
Results: map[Contents: 1: // SPDX-License-Identifier: GPL-2.0
2: /*
3: * The USB Monitor, inspired by Dave Harding's USBMon.
4: *
5: * mon_main.c: Main file, module initiation and exit, registrations, etc.
6: *
7: * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com)
8: */
9:
10: #include <linux/kernel.h>
11: #include <linux/module.h>
12: #include <linux/usb.h>
13: #include <linux/usb/hcd.h>
14: #include <linux/slab.h>
15: #include <linux/notifier.h>
16: #include <linux/mutex.h>
17:
18: #include "usb_mon.h"
19:
20:
21: static void mon_stop(struct mon_bus *mbus);
22: static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
23: static void mon_bus_drop(struct kref *r);
24: static void mon_bus_init(struct usb_bus *ubus);
25:
26: DEFINE_MUTEX(mon_lock);
27:
28: struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
29: static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
30:
31: /*
32: * Link a reader into the bus.
33: *
34: * This must be called with mon_lock taken because of mbus->ref.
35: */
36: void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
37: {
38: unsigned long flags;
39: struct list_head *p;
40:
41: spin_lock_irqsave(&mbus->lock, flags);
42: if (mbus->nreaders == 0) {
43: if (mbus == &mon_bus0) {
44: list_for_each (p, &mon_buses) {
45: struct mon_bus *m1;
46: m1 = list_entry(p, struct mon_bus, bus_link);
47: m1->u_bus->monitored = 1;
48: }
49: } else {
50: mbus->u_bus->monitored = 1;
51: }
52: }
53: mbus->nreaders++;
54: list_add_tail(&r->r_link, &mbus->r_list);
55: spin_unlock_irqrestore(&mbus->lock, flags);
56:
57: kref_get(&mbus->ref);
58: }
59:
60: /*
61: * Unlink reader from the bus.
62: *
63: * This is called with mon_lock taken, so we can decrement mbus->ref.
64: */
65: void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
66: {
67: unsigned long flags;
68:
69: spin_lock_irqsave(&mbus->lock, flags);
70: list_del(&r->r_link);
71: --mbus->nreaders;
72: if (mbus->nreaders == 0)
73: mon_stop(mbus);
74: spin_unlock_irqrestore(&mbus->lock, flags);
75:
76: kref_put(&mbus->ref, mon_bus_drop);
77: }
78:
79: /*
80: */
81: static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
82: {
83: unsigned long flags;
84: struct mon_reader *r;
85:
86: spin_lock_irqsave(&mbus->lock, flags);
87: mbus->cnt_events++;
88: list_for_each_entry(r, &mbus->r_list, r_link)
89: r->rnf_submit(r->r_data, urb);
90: spin_unlock_irqrestore(&mbus->lock, flags);
91: }
92:
93: static void mon_submit(struct usb_bus *ubus, struct urb *urb)
94: {
95: struct mon_bus *mbus;
96:
97: mbus = ubus->mon_bus;
98: if (mbus != NULL)
99: mon_bus_submit(mbus, urb);
100: mon_bus_submit(&mon_bus0, urb);
]
|
| 23/2 |
2026/09/28 05:40 |
llm |
expert |
0m
Model:
Tokens:
input: 17746
output: 39
thoughts: 11
Thoughts: Okay, here we go...
**Diving Back into `mon_main.c`**
Alright, time to get back into `mon_main.c`. I need to refresh my memory on what we were looking at last time. Let's see... I remember we were digging around in the initialization routines, specifically looking for how the monitoring process is bootstrapped. There might be some subtle but critical configuration parameters defined in here, or maybe a control flow that reveals how it interacts with the underlying system libraries. I'm focusing on the overall architecture here. Any specific sections of note? Are there any obvious potential areas of concern that stand out from the code? Let me scan the function calls and variables quickly to see if any of them catch my eye. Let's get to it.
|
| 24/2 |
2026/09/28 05:40 |
tool |
read-file |
0m
Args: map[File:drivers/usb/mon/mon_main.c FirstLine:101 LineCount:80]
Results: map[Contents: 101: }
102:
103: /*
104: */
105: static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
106: {
107: unsigned long flags;
108: struct mon_reader *r;
109:
110: spin_lock_irqsave(&mbus->lock, flags);
111: mbus->cnt_events++;
112: list_for_each_entry(r, &mbus->r_list, r_link)
113: r->rnf_error(r->r_data, urb, error);
114: spin_unlock_irqrestore(&mbus->lock, flags);
115: }
116:
117: static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
118: {
119: struct mon_bus *mbus;
120:
121: mbus = ubus->mon_bus;
122: if (mbus != NULL)
123: mon_bus_submit_error(mbus, urb, error);
124: mon_bus_submit_error(&mon_bus0, urb, error);
125: }
126:
127: /*
128: */
129: static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
130: {
131: unsigned long flags;
132: struct mon_reader *r;
133:
134: spin_lock_irqsave(&mbus->lock, flags);
135: mbus->cnt_events++;
136: list_for_each_entry(r, &mbus->r_list, r_link)
137: r->rnf_complete(r->r_data, urb, status);
138: spin_unlock_irqrestore(&mbus->lock, flags);
139: }
140:
141: static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
142: {
143: struct mon_bus *mbus;
144:
145: mbus = ubus->mon_bus;
146: if (mbus != NULL)
147: mon_bus_complete(mbus, urb, status);
148: mon_bus_complete(&mon_bus0, urb, status);
149: }
150:
151: /* int (*unlink_urb) (struct urb *urb, int status); */
152:
153: /*
154: * Stop monitoring.
155: */
156: static void mon_stop(struct mon_bus *mbus)
157: {
158: struct usb_bus *ubus;
159:
160: if (mbus == &mon_bus0) {
161: list_for_each_entry(mbus, &mon_buses, bus_link) {
162: /*
163: * We do not change nreaders here, so rely on mon_lock.
164: */
165: if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
166: ubus->monitored = 0;
167: }
168: } else {
169: /*
170: * A stop can be called for a dissolved mon_bus in case of
171: * a reader staying across an rmmod foo_hcd, so test ->u_bus.
172: */
173: if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
174: ubus->monitored = 0;
175: mb();
176: }
177: }
178: }
179:
180: /*
]
|
| 25/2 |
2026/09/28 05:40 |
llm |
expert |
0m
Model:
Tokens:
input: 19144
output: 40
thoughts: 61
Thoughts: Okay, let me delve into this. Here's my thought process regarding `mon_stop`:
**Analyzing the `mon_stop` Function**
Alright, let's take a look at `mon_stop`. Immediately, I'm drawn to line 175. I see that `mb();` call, and that raises a flag. Why is memory barrier being employed there? My immediate instinct is to understand the context and the reasons behind this specific placement.
To fully grasp the intent, my next step is clear: I need the historical context. I'll fire up `git blame` or `git log` and examine the commit history for both `mon_stop` and, for good measure, `mon_dissolve`. Looking at the evolution of these functions, especially around the time this particular line was added or modified, will provide the crucial insights I need. This will expose the rationale behind the memory barrier, shedding light on any potential concurrency issues or memory model considerations that prompted its inclusion. I'll be looking for clues about the intended synchronization behavior and the data structures involved.
|
| 26/2 |
2026/09/28 05:40 |
tool |
git-log |
10m
Error:
Args: map[CodeRegexp:monitored = 0 SourcePath:drivers/usb/mon/mon_main.c]
Results:
|
| 27/2 |
2026/09/28 05:50 |
llm |
expert |
0m
Model:
Tokens:
input: 19508
output: 39
thoughts: 32
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:
**Analyzing Potential USB Monitor Driver Issue with Git-Blame**
Alright, I'm thinking we need to dig into the USB monitor driver. Specifically, I'm going to leverage `git-blame` on the `drivers/usb/mon/mon_main.c` file. My focus is on lines 150 through 180. That region likely handles some critical aspects of the monitoring functionality β could be data capture, filtering, packet processing, or something similar. Blaming those lines will give me immediate insight into who wrote that code, when, and most importantly, *why* it's structured that way. It's a quick way to understand the history and intention behind that particular implementation, and gives me context as to where any current issues may be arising. This should help to rapidly understand what's been changed recently, and by whom. Let's see what that git history tells us.
|
| 28/2 |
2026/09/28 05:50 |
tool |
git-blame |
0m
Args: map[End:180 File:drivers/usb/mon/mon_main.c Start:150]
Results: map[Output:^1da177e4c3f4 150)
^1da177e4c3f4 151) /* int (*unlink_urb) (struct urb *urb, int status); */
^1da177e4c3f4 152)
^1da177e4c3f4 153) /*
^1da177e4c3f4 154) * Stop monitoring.
^1da177e4c3f4 155) */
^1da177e4c3f4 156) static void mon_stop(struct mon_bus *mbus)
^1da177e4c3f4 157) {
c36d54ab380fb 158) struct usb_bus *ubus;
^1da177e4c3f4 159)
ecb658d387dc0 160) if (mbus == &mon_bus0) {
80920e2126926 161) list_for_each_entry(mbus, &mon_buses, bus_link) {
ecb658d387dc0 162) /*
ecb658d387dc0 163) * We do not change nreaders here, so rely on mon_lock.
ecb658d387dc0 164) */
ecb658d387dc0 165) if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
ecb658d387dc0 166) ubus->monitored = 0;
ecb658d387dc0 167) }
ecb658d387dc0 168) } else {
ecb658d387dc0 169) /*
ecb658d387dc0 170) * A stop can be called for a dissolved mon_bus in case of
ecb658d387dc0 171) * a reader staying across an rmmod foo_hcd, so test ->u_bus.
ecb658d387dc0 172) */
ecb658d387dc0 173) if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
ecb658d387dc0 174) ubus->monitored = 0;
ecb658d387dc0 175) mb();
ecb658d387dc0 176) }
^1da177e4c3f4 177) }
^1da177e4c3f4 178) }
^1da177e4c3f4 179)
^1da177e4c3f4 180) /*
]
|
| 29/2 |
2026/09/28 05:51 |
llm |
expert |
0m
Model:
Tokens:
input: 20616
output: 26
thoughts: 22
Thoughts: Okay, here's my interpretation of that simple "thought," tailored for an expert audience:
**Analyzing the Latest Commit**
Alright, let's take a look at `ecb658d387dc0`. I immediately recognize that as a potential candidate for closer inspection. I need to understand the implications of this commit, and what changes it represents in the larger context of the project. I'm going to pull up the diff and really dig in. I need to assess its impact, potential side effects, and if it aligns with the overall architectural goals of the system. Let's see what interesting tidbits await.
|
| 30/2 |
2026/09/28 05:51 |
tool |
git-show |
0m
Args: map[Commit:ecb658d387dc0]
Results: map[Output:commit ecb658d387dc09f344b3d755e8674076072032c7
Author: Pete Zaitcev <zaitcev@redhat.com>
Date: Wed Apr 11 13:47:26 2007 -0700
usbmon: bus zero
Add the "bus zero" feature to the usbmon. If a user process specifies bus
with number zero, it receives events from all buses. This is useful when
we wish to see initial enumeration when a bus is created, typically after
a modprobe. Until now, an application had to loop until a new bus could
be open, then start capturing on it. This procedure was cumbersome and
could lose initial events. Also, often it's too bothersome to find exactly
to which bus a specific device is attached.
Paolo Albeni provided the original concept implementation. I added the
handling of "bus->monitored" flag and generally fixed it up.
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
index b2bedd974ac3..0af11a66207c 100644
--- a/drivers/usb/mon/mon_bin.c
+++ b/drivers/usb/mon/mon_bin.c
@@ -356,8 +356,10 @@ static inline char mon_bin_get_setup(unsigned char *setupb,
if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
return '-';
- if (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
return mon_dmapeek(setupb, urb->setup_dma, SETUP_LEN);
+ }
if (urb->setup_packet == NULL)
return 'Z';
@@ -369,7 +371,8 @@ static char mon_bin_get_data(const struct mon_reader_bin *rp,
unsigned int offset, struct urb *urb, unsigned int length)
{
- if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) {
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
mon_dmapeek_vec(rp, offset, urb->transfer_dma, length);
return 0;
}
@@ -440,7 +443,7 @@ static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
/* We use the fact that usb_pipein() returns 0x80 */
ep->epnum = usb_pipeendpoint(urb->pipe) | usb_pipein(urb->pipe);
ep->devnum = usb_pipedevice(urb->pipe);
- ep->busnum = rp->r.m_bus->u_bus->busnum;
+ ep->busnum = urb->dev->bus->busnum;
ep->id = (unsigned long) urb;
ep->ts_sec = ts.tv_sec;
ep->ts_usec = ts.tv_usec;
@@ -500,7 +503,7 @@ static void mon_bin_error(void *data, struct urb *urb, int error)
/* We use the fact that usb_pipein() returns 0x80 */
ep->epnum = usb_pipeendpoint(urb->pipe) | usb_pipein(urb->pipe);
ep->devnum = usb_pipedevice(urb->pipe);
- ep->busnum = rp->r.m_bus->u_bus->busnum;
+ ep->busnum = urb->dev->bus->busnum;
ep->id = (unsigned long) urb;
ep->status = error;
@@ -515,7 +518,6 @@ static void mon_bin_error(void *data, struct urb *urb, int error)
static int mon_bin_open(struct inode *inode, struct file *file)
{
struct mon_bus *mbus;
- struct usb_bus *ubus;
struct mon_reader_bin *rp;
size_t size;
int rc;
@@ -525,7 +527,7 @@ static int mon_bin_open(struct inode *inode, struct file *file)
mutex_unlock(&mon_lock);
return -ENODEV;
}
- if ((ubus = mbus->u_bus) == NULL) {
+ if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
printk(KERN_ERR TAG ": consistency error on open\n");
mutex_unlock(&mon_lock);
return -ENODEV;
diff --git a/drivers/usb/mon/mon_main.c b/drivers/usb/mon/mon_main.c
index c9739e7b35e5..8a1df2c9c73e 100644
--- a/drivers/usb/mon/mon_main.c
+++ b/drivers/usb/mon/mon_main.c
@@ -16,8 +16,6 @@
#include "usb_mon.h"
#include "../core/hcd.h"
-static void mon_submit(struct usb_bus *ubus, struct urb *urb);
-static void mon_complete(struct usb_bus *ubus, struct urb *urb);
static void mon_stop(struct mon_bus *mbus);
static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
static void mon_bus_drop(struct kref *r);
@@ -25,6 +23,7 @@ static void mon_bus_init(struct usb_bus *ubus);
DEFINE_MUTEX(mon_lock);
+struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
/*
@@ -35,22 +34,19 @@ static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
{
unsigned long flags;
- struct usb_bus *ubus;
+ struct list_head *p;
spin_lock_irqsave(&mbus->lock, flags);
if (mbus->nreaders == 0) {
- ubus = mbus->u_bus;
- if (ubus->monitored) {
- /*
- * Something is really broken, refuse to go on and
- * possibly corrupt ops pointers or worse.
- */
- printk(KERN_ERR TAG ": bus %d is already monitored\n",
- ubus->busnum);
- spin_unlock_irqrestore(&mbus->lock, flags);
- return;
+ if (mbus == &mon_bus0) {
+ list_for_each (p, &mon_buses) {
+ struct mon_bus *m1;
+ m1 = list_entry(p, struct mon_bus, bus_link);
+ m1->u_bus->monitored = 1;
+ }
+ } else {
+ mbus->u_bus->monitored = 1;
}
- ubus->monitored = 1;
}
mbus->nreaders++;
list_add_tail(&r->r_link, &mbus->r_list);
@@ -80,77 +76,79 @@ void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
/*
*/
-static void mon_submit(struct usb_bus *ubus, struct urb *urb)
+static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
{
- struct mon_bus *mbus;
unsigned long flags;
struct list_head *pos;
struct mon_reader *r;
- mbus = ubus->mon_bus;
- if (mbus == NULL)
- goto out_unlocked;
-
spin_lock_irqsave(&mbus->lock, flags);
- if (mbus->nreaders == 0)
- goto out_locked;
-
mbus->cnt_events++;
list_for_each (pos, &mbus->r_list) {
r = list_entry(pos, struct mon_reader, r_link);
r->rnf_submit(r->r_data, urb);
}
-
spin_unlock_irqrestore(&mbus->lock, flags);
return;
+}
-out_locked:
- spin_unlock_irqrestore(&mbus->lock, flags);
-out_unlocked:
- return;
+static void mon_submit(struct usb_bus *ubus, struct urb *urb)
+{
+ struct mon_bus *mbus;
+
+ if ((mbus = ubus->mon_bus) != NULL)
+ mon_bus_submit(mbus, urb);
+ mon_bus_submit(&mon_bus0, urb);
}
/*
*/
-static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
+static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
{
- struct mon_bus *mbus;
unsigned long flags;
struct list_head *pos;
struct mon_reader *r;
- mbus = ubus->mon_bus;
- if (mbus == NULL)
- goto out_unlocked;
-
spin_lock_irqsave(&mbus->lock, flags);
- if (mbus->nreaders == 0)
- goto out_locked;
-
mbus->cnt_events++;
list_for_each (pos, &mbus->r_list) {
r = list_entry(pos, struct mon_reader, r_link);
r->rnf_error(r->r_data, urb, error);
}
-
spin_unlock_irqrestore(&mbus->lock, flags);
return;
+}
-out_locked:
- spin_unlock_irqrestore(&mbus->lock, flags);
-out_unlocked:
- return;
+static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
+{
+ struct mon_bus *mbus;
+
+ if ((mbus = ubus->mon_bus) != NULL)
+ mon_bus_submit_error(mbus, urb, error);
+ mon_bus_submit_error(&mon_bus0, urb, error);
}
/*
*/
-static void mon_complete(struct usb_bus *ubus, struct urb *urb)
+static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb)
{
- struct mon_bus *mbus;
unsigned long flags;
struct list_head *pos;
struct mon_reader *r;
+ spin_lock_irqsave(&mbus->lock, flags);
+ mbus->cnt_events++;
+ list_for_each (pos, &mbus->r_list) {
+ r = list_entry(pos, struct mon_reader, r_link);
+ r->rnf_complete(r->r_data, urb);
+ }
+ spin_unlock_irqrestore(&mbus->lock, flags);
+}
+
+static void mon_complete(struct usb_bus *ubus, struct urb *urb)
+{
+ struct mon_bus *mbus;
+
mbus = ubus->mon_bus;
if (mbus == NULL) {
/*
@@ -162,13 +160,8 @@ static void mon_complete(struct usb_bus *ubus, struct urb *urb)
return;
}
- spin_lock_irqsave(&mbus->lock, flags);
- mbus->cnt_events++;
- list_for_each (pos, &mbus->r_list) {
- r = list_entry(pos, struct mon_reader, r_link);
- r->rnf_complete(r->r_data, urb);
- }
- spin_unlock_irqrestore(&mbus->lock, flags);
+ mon_bus_complete(mbus, urb);
+ mon_bus_complete(&mon_bus0, urb);
}
/* int (*unlink_urb) (struct urb *urb, int status); */
@@ -179,14 +172,26 @@ static void mon_complete(struct usb_bus *ubus, struct urb *urb)
static void mon_stop(struct mon_bus *mbus)
{
struct usb_bus *ubus = mbus->u_bus;
+ struct list_head *p;
- /*
- * A stop can be called for a dissolved mon_bus in case of
- * a reader staying across an rmmod foo_hcd.
- */
- if (ubus != NULL) {
- ubus->monitored = 0;
- mb();
+ if (mbus == &mon_bus0) {
+ list_for_each (p, &mon_buses) {
+ mbus = list_entry(p, struct mon_bus, bus_link);
+ /*
+ * We do not change nreaders here, so rely on mon_lock.
+ */
+ if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
+ ubus->monitored = 0;
+ }
+ } else {
+ /*
+ * A stop can be called for a dissolved mon_bus in case of
+ * a reader staying across an rmmod foo_hcd, so test ->u_bus.
+ */
+ if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
+ ubus->monitored = 0;
+ mb();
+ }
}
}
@@ -199,6 +204,10 @@ static void mon_stop(struct mon_bus *mbus)
static void mon_bus_add(struct usb_bus *ubus)
{
mon_bus_init(ubus);
+ mutex_lock(&mon_lock);
+ if (mon_bus0.nreaders != 0)
+ ubus->monitored = 1;
+ mutex_unlock(&mon_lock);
}
/*
@@ -250,12 +259,7 @@ static struct usb_mon_operations mon_ops_0 = {
static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
{
- /*
- * Never happens, but...
- */
if (ubus->monitored) {
- printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
- ubus->busnum);
ubus->monitored = 0;
mb();
}
@@ -263,6 +267,8 @@ static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
ubus->mon_bus = NULL;
mbus->u_bus = NULL;
mb();
+
+ /* We want synchronize_irq() here, but that needs an argument. */
}
/*
@@ -295,9 +301,8 @@ static void mon_bus_init(struct usb_bus *ubus)
*/
mbus->u_bus = ubus;
ubus->mon_bus = mbus;
- mbus->uses_dma = ubus->uses_dma;
- mbus->text_inited = mon_text_add(mbus, ubus);
+ mbus->text_inited = mon_text_add(mbus, ubus->busnum);
// mon_bin_add(...)
mutex_lock(&mon_lock);
@@ -309,6 +314,18 @@ static void mon_bus_init(struct usb_bus *ubus)
return;
}
+static void mon_bus0_init(void)
+{
+ struct mon_bus *mbus = &mon_bus0;
+
+ kref_init(&mbus->ref);
+ spin_lock_init(&mbus->lock);
+ INIT_LIST_HEAD(&mbus->r_list);
+
+ mbus->text_inited = mon_text_add(mbus, 0);
+ // mbus->bin_inited = mon_bin_add(mbus, 0);
+}
+
/*
* Search a USB bus by number. Notice that USB bus numbers start from one,
* which we may later use to identify "all" with zero.
@@ -322,6 +339,9 @@ struct mon_bus *mon_bus_lookup(unsigned int num)
struct list_head *p;
struct mon_bus *mbus;
+ if (num == 0) {
+ return &mon_bus0;
+ }
list_for_each (p, &mon_buses) {
mbus = list_entry(p, struct mon_bus, bus_link);
if (mbus->u_bus->busnum == num) {
@@ -341,6 +361,8 @@ static int __init mon_init(void)
if ((rc = mon_bin_init()) != 0)
goto err_bin;
+ mon_bus0_init();
+
if (usb_mon_register(&mon_ops_0) != 0) {
printk(KERN_NOTICE TAG ": unable to register with the core\n");
rc = -ENODEV;
@@ -374,6 +396,7 @@ static void __exit mon_exit(void)
usb_mon_deregister();
mutex_lock(&mon_lock);
+
while (!list_empty(&mon_buses)) {
p = mon_buses.next;
mbus = list_entry(p, struct mon_bus, bus_link);
@@ -397,6 +420,11 @@ static void __exit mon_exit(void)
mon_dissolve(mbus, mbus->u_bus);
kref_put(&mbus->ref, mon_bus_drop);
}
+
+ mbus = &mon_bus0;
+ if (mbus->text_inited)
+ mon_text_del(mbus);
+
mutex_unlock(&mon_lock);
mon_text_exit();
diff --git a/drivers/usb/mon/mon_text.c b/drivers/usb/mon/mon_text.c
index bbf54396e5f2..ec0cc51e39ac 100644
--- a/drivers/usb/mon/mon_text.c
+++ b/drivers/usb/mon/mon_text.c
@@ -124,8 +124,10 @@ static inline char mon_text_get_setup(struct mon_event_text *ep,
if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
return '-';
- if (mbus->uses_dma && (urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
+ }
if (urb->setup_packet == NULL)
return 'Z'; /* '0' would be not as pretty. */
@@ -160,8 +162,10 @@ static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
* contain non-NULL garbage in case the upper level promised to
* set DMA for the HCD.
*/
- if (mbus->uses_dma && (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
return mon_dmapeek(ep->data, urb->transfer_dma, len);
+ }
if (urb->transfer_buffer == NULL)
return 'Z'; /* '0' would be not as pretty. */
@@ -201,7 +205,7 @@ static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
ep->type = ev_type;
ep->pipe = urb->pipe;
ep->id = (unsigned long) urb;
- ep->busnum = rp->r.m_bus->u_bus->busnum;
+ ep->busnum = urb->dev->bus->busnum;
ep->tstamp = stamp;
ep->length = (ev_type == 'S') ?
urb->transfer_buffer_length : urb->actual_length;
@@ -305,13 +309,11 @@ static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
static int mon_text_open(struct inode *inode, struct file *file)
{
struct mon_bus *mbus;
- struct usb_bus *ubus;
struct mon_reader_text *rp;
int rc;
mutex_lock(&mon_lock);
mbus = inode->i_private;
- ubus = mbus->u_bus;
rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
if (rp == NULL) {
@@ -335,8 +337,7 @@ static int mon_text_open(struct inode *inode, struct file *file)
rp->r.rnf_error = mon_text_error;
rp->r.rnf_complete = mon_text_complete;
- snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
- (long)rp);
+ snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
rp->e_slab = kmem_cache_create(rp->slab_name,
sizeof(struct mon_event_text), sizeof(long), 0,
mon_text_ctor, NULL);
@@ -654,14 +655,14 @@ static const struct file_operations mon_fops_text_u = {
.release = mon_text_release,
};
-int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
+int mon_text_add(struct mon_bus *mbus, int busnum)
{
struct dentry *d;
enum { NAMESZ = 10 };
char name[NAMESZ];
int rc;
- rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
+ rc = snprintf(name, NAMESZ, "%dt", busnum);
if (rc <= 0 || rc >= NAMESZ)
goto err_print_t;
d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_t);
@@ -669,7 +670,7 @@ int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
goto err_create_t;
mbus->dent_t = d;
- rc = snprintf(name, NAMESZ, "%du", ubus->busnum);
+ rc = snprintf(name, NAMESZ, "%du", busnum);
if (rc <= 0 || rc >= NAMESZ)
goto err_print_u;
d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
@@ -677,7 +678,7 @@ int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
goto err_create_u;
mbus->dent_u = d;
- rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
+ rc = snprintf(name, NAMESZ, "%ds", busnum);
if (rc <= 0 || rc >= NAMESZ)
goto err_print_s;
d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
diff --git a/drivers/usb/mon/usb_mon.h b/drivers/usb/mon/usb_mon.h
index a1e2b8e9d777..13d63255283e 100644
--- a/drivers/usb/mon/usb_mon.h
+++ b/drivers/usb/mon/usb_mon.h
@@ -23,7 +23,6 @@ struct mon_bus {
struct dentry *dent_s; /* Debugging file */
struct dentry *dent_t; /* Text interface file */
struct dentry *dent_u; /* Second text interface file */
- int uses_dma;
/* Ref */
int nreaders; /* Under mon_lock AND mbus->lock */
@@ -53,7 +52,7 @@ void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r);
struct mon_bus *mon_bus_lookup(unsigned int num);
-int /*bool*/ mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus);
+int /*bool*/ mon_text_add(struct mon_bus *mbus, int busnum);
void mon_text_del(struct mon_bus *mbus);
// void mon_bin_add(struct mon_bus *);
@@ -82,4 +81,6 @@ extern struct mutex mon_lock;
extern const struct file_operations mon_fops_stat;
+extern struct mon_bus mon_bus0; /* Only for redundant checks */
+
#endif /* __USB_MON_H */
]
|
| 31/2 |
2026/09/28 05:51 |
llm |
expert |
0m
Model:
Tokens:
input: 27733
output: 39
thoughts: 858
Thoughts: **Analyzing the USB Monitor Subsystem**
Okay, let's break this down. I see we have a function `mon_stop` that seems crucial, specifically the assignments `ubus->monitored = 0;`. I'm immediately interested in *where* and *why* this is happening. The conditional statements `if (mbus == &mon_bus0)` and `if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL)` pique my interest; it's almost certainly related to the monitoring state and cleanup. The second `ubus->monitored = 0` assignment with the `mb()` barrier is also worth noting; that's potentially important for memory ordering.
Next, I quickly jump to `usbmon_urb_submit`, which calls `mon_ops->urb_submit` only if the bus is marked as `monitored`. This makes perfect sense; it's the entry point for intercepting URB submissions.
Now, `mon_submit`, in turn, delegates to `mon_bus_submit`. Interesting: there's both a check for `ubus->mon_bus != NULL` and a call to `mon_bus_submit(&mon_bus0, urb);`. This looks like it's handling both per-bus and a "global" monitor. This function clearly locks and iterates over a reader list. The use of a spinlock suggests we're dealing with a potentially concurrent environment.
If `mbus->r_list` is empty, the loop does nothing. That's a key point. This implies a scenario where no readers are present, making the lock potentially unnecessary. But it might be necessary for correct synchronization with other operations on the bus.
My immediate question: What are the lifetimes of the various `struct`s? `struct usb_bus`, `struct mon_bus`, and `mon_reader`? I need to understand how the monitor is set up and torn down. How is the association between the `usb_bus` and the `mon_bus` handled? I suspect there are functions involved in initializing and dissolving this association: `mon_dissolve`, `mon_bus_drop`, and `mon_bus_init`, are likely involved in these processes and they need to be reviewed. The logic in `mon_stop` hints at potential race conditions, or at least concerns, around teardown and cleanup, especially around the reference counting or other concurrency mechanisms (e.g., `nreaders`). I'll need to follow the lifecycle of these structures to ensure correctness and prevent any double-frees or use-after-free scenarios.
|
| 32/2 |
2026/09/28 05:51 |
tool |
read-file |
0m
Args: map[File:drivers/usb/mon/mon_main.c FirstLine:230 LineCount:80]
Results: map[Contents: 230:
231: /*
232: * Ops
233: */
234: static const struct usb_mon_operations mon_ops_0 = {
235: .urb_submit = mon_submit,
236: .urb_submit_error = mon_submit_error,
237: .urb_complete = mon_complete,
238: };
239:
240: /*
241: * Tear usb_bus and mon_bus apart.
242: */
243: static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
244: {
245:
246: if (ubus->monitored) {
247: ubus->monitored = 0;
248: mb();
249: }
250:
251: ubus->mon_bus = NULL;
252: mbus->u_bus = NULL;
253: mb();
254:
255: /* We want synchronize_irq() here, but that needs an argument. */
256: }
257:
258: /*
259: */
260: static void mon_bus_drop(struct kref *r)
261: {
262: struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
263: kfree(mbus);
264: }
265:
266: /*
267: * Initialize a bus for us:
268: * - allocate mon_bus
269: * - refcount USB bus struct
270: * - link
271: */
272: static void mon_bus_init(struct usb_bus *ubus)
273: {
274: struct mon_bus *mbus;
275:
276: mbus = kzalloc_obj(struct mon_bus);
277: if (mbus == NULL)
278: goto err_alloc;
279: kref_init(&mbus->ref);
280: spin_lock_init(&mbus->lock);
281: INIT_LIST_HEAD(&mbus->r_list);
282:
283: /*
284: * We don't need to take a reference to ubus, because we receive
285: * a notification if the bus is about to be removed.
286: */
287: mbus->u_bus = ubus;
288: ubus->mon_bus = mbus;
289:
290: mbus->text_inited = mon_text_add(mbus, ubus);
291: mbus->bin_inited = mon_bin_add(mbus, ubus);
292:
293: mutex_lock(&mon_lock);
294: list_add_tail(&mbus->bus_link, &mon_buses);
295: mutex_unlock(&mon_lock);
296: return;
297:
298: err_alloc:
299: return;
300: }
301:
302: static void mon_bus0_init(void)
303: {
304: struct mon_bus *mbus = &mon_bus0;
305:
306: kref_init(&mbus->ref);
307: spin_lock_init(&mbus->lock);
308: INIT_LIST_HEAD(&mbus->r_list);
309:
]
|
| 33/2 |
2026/09/28 05:51 |
llm |
expert |
0m
Model:
Tokens:
input: 30271
output: 52
thoughts: 146
Thoughts: Okay, let's break this down.
**Analyzing the `mon_bus` Reference Count**
Alright, I need to understand the lifecycle of this `mbus` object and, more specifically, how its reference count is managed. I see `kref_put(&mbus->ref, mon_bus_drop);` being called with `mon_bus_drop`. That's the crucial call that ultimately releases the object. I need to trace the incrementing and decrementing of this reference count to ensure there aren't any leaks or double frees.
First, I see the initialization: `mon_bus_init` calls `kref_init(&mbus->ref)`, which sets the initial reference count to 1. Good, the object starts with one reference.
Now, let's track the increments: `mon_reader_add` calls `kref_get(&mbus->ref)`. Okay, each time a reader is added, the count goes up. Makes sense.
And the decrements? `mon_reader_del` calls `kref_put(&mbus->ref, mon_bus_drop)`. So, when a reader is removed, the reference count is decremented, and if the count hits zero, `mon_bus_drop` is called to clean up. That's the normal path.
I'm also seeing calls when `mon_exit` or bus removal happens. It's important to understand *when* the bus gets dissolved or removed, and how that interacts with the reader lifecycle. I need to investigate where `mon_dissolve` or the bus removal routines are called. This is the critical piece to ensure the `mbus` object isn't prematurely freed before all readers are done with it. I must examine the control flow in this area very carefully.
|
| 34/2 |
2026/09/28 05:51 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:drivers/usb/mon/mon_main.c IncludeSnippetLines:5 Name:mon_dissolve SourceTreePrefix:]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mon_bus_remove SourceFile:drivers/usb/mon/mon_main.c SourceLine:209 SourceSnippet: 204: if (mbus->text_inited)
205: mon_text_del(mbus);
206: if (mbus->bin_inited)
207: mon_bin_del(mbus);
208:
209: mon_dissolve(mbus, ubus);
210: kref_put(&mbus->ref, mon_bus_drop);
211: mutex_unlock(&mon_lock);
212: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mon_exit SourceFile:drivers/usb/mon/mon_main.c SourceLine:403 SourceSnippet: 398: ": Outstanding opens (%d) on usb%d, leaking...\n",
399: mbus->nreaders, mbus->u_bus->busnum);
400: kref_get(&mbus->ref); /* Force leak */
401: }
402:
403: mon_dissolve(mbus, mbus->u_bus);
404: kref_put(&mbus->ref, mon_bus_drop);
405: }
406:
407: mbus = &mon_bus0;
408: if (mbus->text_inited)
]] TruncatedOutput:false]
|
| 35/2 |
2026/09/28 05:51 |
llm |
expert |
0m
Model:
Tokens:
input: 31306
output: 39
thoughts: 11
Thoughts: Okay, here's my attempt at summarizing that thought process, keeping in mind the expert user:
**Assessing the `mon_bus_remove` Function**
Alright, time to dive in. My immediate thought is, "Let's check `mon_bus_remove`". It's a quick, internal check-in. What's the context? What's the current problem I'm dealing with that made me hone in on this function? Am I debugging? Optimizing? Perhaps I'm architecting a new system and evaluating existing components. It could be any number of things, but the core of the idea is, I need to understand what `mon_bus_remove` is doing, or if the function is even working as intended. Based on my past, my thought process is to quickly remind myself what the function does and what the function is intended to do, and begin to analyze the function based on current circumstance.
|
| 36/2 |
2026/09/28 05:51 |
tool |
read-file |
0m
Args: map[File:drivers/usb/mon/mon_main.c FirstLine:180 LineCount:40]
Results: map[Contents: 180: /*
181: * Add a USB bus (usually by a modprobe foo-hcd)
182: *
183: * This does not return an error code because the core cannot care less
184: * if monitoring is not established.
185: */
186: static void mon_bus_add(struct usb_bus *ubus)
187: {
188: mon_bus_init(ubus);
189: mutex_lock(&mon_lock);
190: if (mon_bus0.nreaders != 0)
191: ubus->monitored = 1;
192: mutex_unlock(&mon_lock);
193: }
194:
195: /*
196: * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
197: */
198: static void mon_bus_remove(struct usb_bus *ubus)
199: {
200: struct mon_bus *mbus = ubus->mon_bus;
201:
202: mutex_lock(&mon_lock);
203: list_del(&mbus->bus_link);
204: if (mbus->text_inited)
205: mon_text_del(mbus);
206: if (mbus->bin_inited)
207: mon_bin_del(mbus);
208:
209: mon_dissolve(mbus, ubus);
210: kref_put(&mbus->ref, mon_bus_drop);
211: mutex_unlock(&mon_lock);
212: }
213:
214: static int mon_notify(struct notifier_block *self, unsigned long action,
215: void *dev)
216: {
217: switch (action) {
218: case USB_BUS_ADD:
219: mon_bus_add(dev);
]
|
| 37/2 |
2026/09/28 05:51 |
llm |
expert |
0m
Model:
Tokens:
input: 32144
output: 35
thoughts: 3053
Thoughts: **Analyzing the USBmon Data Race**
Okay, so I'm diving into this USBmon data race, and it looks like a classic synchronization issue between a reader thread closing a monitor and a USB device transfer submission. My focus is on the `ubus->monitored` flag and the `mon_bus` structure.
First, I see the `mon_bus_init` function, which sets up the monitoring bus and initializes the `ubus->mon_bus` pointer. This bus remains allocated as long as its reference count (`mbus->ref`) is greater than zero. When `mon_bus_remove` is called, `ubus->mon_bus` is set to NULL, which is concerning. The `mon_dissolve` function is the culprit.
The race report points directly to `ubus->monitored` and shows a write (setting it to 0) in `mon_reader_del` (specifically `mon_stop`) and a read in `usb_hcd_submit_urb`, in the `usbmon_urb_submit` inline function.
The reader thread, running in `mon_reader_del`, decrements `mbus->nreaders`. When this count hits zero, `mon_stop` gets called and that sets `ubus->monitored = 0`. So, when I close `/dev/usbmon0` or the relevant bus, the monitor is supposed to stop.
On the other hand, the USB transfer submission path, `usb_submit_urb` -> `usb_hcd_submit_urb`, calls `usbmon_urb_submit` which checks `bus->monitored`. This is the potential race condition. Let's see what happens if the check reads the old value of `ubus->monitored` while `mon_stop` is running:
Case A: `bus->monitored == 0`. The monitoring is being stopped, so it's safe.
Case B: `bus->monitored == 1`. The monitoring is still on. In this scenario, `mon_bus_submit` is called. Critically, `mon_bus_submit` acquires a lock (`spin_lock_irqsave(&mbus->lock, flags)`) that is the same lock used in `mon_reader_del`.
So, it's either:
1. CPU 1 gets the lock, meaning there's still a valid `mbus->r_list` to iterate over.
2. CPU 1 waits for the lock. In the meantime, `mon_reader_del` has run, removed the reader `r` from the `mbus->r_list` set `ubus->monitored=0` and released the lock. CPU 1 then takes the lock and sees an empty list; all is good, or so I thought.
A key concern is if `mbus` itself can be freed while a transfer is in progress. The analysis clarifies that `kref_put(&mbus->ref, mon_bus_drop)` decrements the refcount acquired by `mon_reader_add` and this does not free the bus unless the initial ref from `mon_bus_init` is dropped. The initial ref is only dropped in `mon_bus_remove`. So the global struct `mon_bus0` is never freed.
The key issue is the race where the write to `ubus->monitored` (setting it to 0) in `mon_stop` (called from `mon_reader_del`) conflicts with the read in `usbmon_urb_submit`. The race exists because `ubus->monitored` can be read or written at any time, potentially giving an incorrect value. This isn't just about accessing the list; it's about the state of the monitor itself (`ubus->monitored`). This makes sense given the bug report.
|
| 38/2 |
2026/09/28 05:51 |
tool |
git-log |
0m
Args: map[Count:5 MessageRegexps:[monitored] PathPrefix:include/linux/usb]
Results:
|
| 39/2 |
2026/09/28 05:51 |
llm |
expert |
0m
Model:
Tokens:
input: 36083
output: 25
thoughts: 21
Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience and presented in the first person:
**Initial Investigation into USBmon Submissions**
Right, let's dive into this. My initial instinct is to hone in on commits that have interacted with either the `usbmon_urb_submit` function or, more broadly, anything labeled or connected with `"monitored"`. Both of these terms offer potential avenues to uncover relevant code interactions or, hopefully, the root cause of the behavior I'm seeing. This is a solid starting point β it's a focused query that should give me a manageable dataset of potentially relevant changes without being overly broad initially. This allows me to quickly get a sense of which commits are worth a more in-depth look. I anticipate that this will provide a good foundation to discover further insights by looking at related commits, too.
|
| 40/2 |
2026/09/28 05:51 |
tool |
git-log |
0m
Args: map[Count:5 PathPrefix:drivers/usb/mon]
Results: map[Output:32a92f8c8932 Convert more 'alloc_obj' cases to default GFP_KERNEL arguments
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
368ed48a5ef5 usb: mon: Increase BUFF_MAX to 64 MiB to support multi-MB URBs
fa3b4b9bc74c drivers/usb/mon: refactor min with min_t
]
|
| 41/2 |
2026/09/28 05:51 |
llm |
expert |
0m
Model:
Tokens:
input: 36465
output: 38
thoughts: 21
Thoughts: Here's a summary of my thought process:
**Diving into USB Host Controller Driver History**
Okay, time to get my hands dirty. I need to understand the evolution of the USB Host Controller Driver interface. Let's start with the historical context. I'm going to jump straight into `git log` and inspect the commit history of `include/linux/usb/hcd.h`. That header file is the lynchpin, the interface definition. By tracing its changes, I can quickly grasp how the HCD API has been shaped, what features were introduced, and what architectural decisions drove the modifications. Knowing the lineage of the interface will give me a solid foundation for evaluating its current design and identifying potential areas of improvement or compatibility concerns. Hopefully, this approach will quickly uncover any key regressions, deprecated functionality, or architectural shifts relevant to my current focus.
|
| 42/2 |
2026/09/28 05:51 |
tool |
git-blame |
0m
Args: map[End:740 File:include/linux/usb/hcd.h Start:720]
Results: map[Output:^1da177e4c3f4 drivers/usb/core/hcd.h 720)
^1da177e4c3f4 drivers/usb/core/hcd.h 721) static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
^1da177e4c3f4 drivers/usb/core/hcd.h 722) {
^1da177e4c3f4 drivers/usb/core/hcd.h 723) if (bus->monitored)
^1da177e4c3f4 drivers/usb/core/hcd.h 724) (*mon_ops->urb_submit)(bus, urb);
^1da177e4c3f4 drivers/usb/core/hcd.h 725) }
^1da177e4c3f4 drivers/usb/core/hcd.h 726)
^1da177e4c3f4 drivers/usb/core/hcd.h 727) static inline void usbmon_urb_submit_error(struct usb_bus *bus, struct urb *urb,
^1da177e4c3f4 drivers/usb/core/hcd.h 728) int error)
^1da177e4c3f4 drivers/usb/core/hcd.h 729) {
^1da177e4c3f4 drivers/usb/core/hcd.h 730) if (bus->monitored)
^1da177e4c3f4 drivers/usb/core/hcd.h 731) (*mon_ops->urb_submit_error)(bus, urb, error);
^1da177e4c3f4 drivers/usb/core/hcd.h 732) }
^1da177e4c3f4 drivers/usb/core/hcd.h 733)
9347d51c52afc drivers/usb/core/hcd.h 734) static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb,
9347d51c52afc drivers/usb/core/hcd.h 735) int status)
^1da177e4c3f4 drivers/usb/core/hcd.h 736) {
^1da177e4c3f4 drivers/usb/core/hcd.h 737) if (bus->monitored)
9347d51c52afc drivers/usb/core/hcd.h 738) (*mon_ops->urb_complete)(bus, urb, status);
^1da177e4c3f4 drivers/usb/core/hcd.h 739) }
^1da177e4c3f4 drivers/usb/core/hcd.h 740)
]
|
| 43/2 |
2026/09/28 05:52 |
llm |
expert |
0m
Model:
Tokens:
input: 37473
output: 27
thoughts: 54
Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience:
**Analyzing the `usbmon_urb_submit` Timeline**
Alright, so `usbmon_urb_submit` has been a fundamental part of the USB monitoring infrastructure *since 2.6.12* β that's quite a heritage, all the way back to the foundational commits! I'm thinking of the kernel commit `^1da177e4c3f4` for context, likely the genesis of that function. Given that history, I should be looking for any shifts in its usage and behavior.
My immediate next step is to examine commit `9347d51c52afc`. I'm guessing this commit might provide some insight into performance improvements, a bug fix, or a structural change that could inform the current performance I'm observing. It will likely reveal how `usbmon_urb_submit` has evolved. Knowing the specific changes at a certain point will certainly help to diagnose the problem at hand, or will at least contribute to my current diagnostic procedure.
|
| 44/2 |
2026/09/28 05:52 |
tool |
git-show |
0m
Args: map[Commit:9347d51c52afc]
Results: map[Output:commit 9347d51c52afcf1a77d2104f162cf8a085624c83
Author: Alan Stern <stern@rowland.harvard.edu>
Date: Fri Aug 24 15:41:41 2007 -0400
USB: reorganize urb->status use in usbmon
This patch (as978) reorganizes the way usbmon uses urb->status. It
now accepts the status value as an argument.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Acked-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 8b17babf5c55..4ac021e42cd8 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -1275,7 +1275,7 @@ void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb)
urb->status = -EREMOTEIO;
unmap_urb_for_dma(hcd, urb);
- usbmon_urb_complete(&hcd->self, urb);
+ usbmon_urb_complete(&hcd->self, urb, urb->status);
usb_unanchor_urb(urb);
/* pass ownership to the completion handler */
diff --git a/drivers/usb/core/hcd.h b/drivers/usb/core/hcd.h
index 745be2566f62..729b7a03acd8 100644
--- a/drivers/usb/core/hcd.h
+++ b/drivers/usb/core/hcd.h
@@ -413,7 +413,7 @@ static inline void usbfs_cleanup(void) { }
struct usb_mon_operations {
void (*urb_submit)(struct usb_bus *bus, struct urb *urb);
void (*urb_submit_error)(struct usb_bus *bus, struct urb *urb, int err);
- void (*urb_complete)(struct usb_bus *bus, struct urb *urb);
+ void (*urb_complete)(struct usb_bus *bus, struct urb *urb, int status);
/* void (*urb_unlink)(struct usb_bus *bus, struct urb *urb); */
};
@@ -432,10 +432,11 @@ static inline void usbmon_urb_submit_error(struct usb_bus *bus, struct urb *urb,
(*mon_ops->urb_submit_error)(bus, urb, error);
}
-static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb)
+static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb,
+ int status)
{
if (bus->monitored)
- (*mon_ops->urb_complete)(bus, urb);
+ (*mon_ops->urb_complete)(bus, urb, status);
}
int usb_mon_register(struct usb_mon_operations *ops);
@@ -446,7 +447,8 @@ void usb_mon_deregister(void);
static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb) {}
static inline void usbmon_urb_submit_error(struct usb_bus *bus, struct urb *urb,
int error) {}
-static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb) {}
+static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb,
+ int status) {}
#endif /* CONFIG_USB_MON */
diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
index 3d6f03819ffd..f06e4e2b49d3 100644
--- a/drivers/usb/mon/mon_bin.c
+++ b/drivers/usb/mon/mon_bin.c
@@ -386,7 +386,7 @@ static char mon_bin_get_data(const struct mon_reader_bin *rp,
}
static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
- char ev_type)
+ char ev_type, int status)
{
const struct usb_endpoint_descriptor *epd = &urb->ep->desc;
unsigned long flags;
@@ -452,7 +452,7 @@ static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
ep->id = (unsigned long) urb;
ep->ts_sec = ts.tv_sec;
ep->ts_usec = ts.tv_usec;
- ep->status = urb->status;
+ ep->status = status;
ep->len_urb = urb_length;
ep->len_cap = length;
@@ -475,13 +475,13 @@ static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
static void mon_bin_submit(void *data, struct urb *urb)
{
struct mon_reader_bin *rp = data;
- mon_bin_event(rp, urb, 'S');
+ mon_bin_event(rp, urb, 'S', -EINPROGRESS);
}
-static void mon_bin_complete(void *data, struct urb *urb)
+static void mon_bin_complete(void *data, struct urb *urb, int status)
{
struct mon_reader_bin *rp = data;
- mon_bin_event(rp, urb, 'C');
+ mon_bin_event(rp, urb, 'C', status);
}
static void mon_bin_error(void *data, struct urb *urb, int error)
diff --git a/drivers/usb/mon/mon_main.c b/drivers/usb/mon/mon_main.c
index 2e317bd79e92..b371ffd39d36 100644
--- a/drivers/usb/mon/mon_main.c
+++ b/drivers/usb/mon/mon_main.c
@@ -129,7 +129,8 @@ static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
/*
*/
-static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb)
+static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb,
+ int status)
{
unsigned long flags;
struct list_head *pos;
@@ -139,18 +140,18 @@ static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb)
mbus->cnt_events++;
list_for_each (pos, &mbus->r_list) {
r = list_entry(pos, struct mon_reader, r_link);
- r->rnf_complete(r->r_data, urb);
+ r->rnf_complete(r->r_data, urb, status);
}
spin_unlock_irqrestore(&mbus->lock, flags);
}
-static void mon_complete(struct usb_bus *ubus, struct urb *urb)
+static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
{
struct mon_bus *mbus;
if ((mbus = ubus->mon_bus) != NULL)
- mon_bus_complete(mbus, urb);
- mon_bus_complete(&mon_bus0, urb);
+ mon_bus_complete(mbus, urb, status);
+ mon_bus_complete(&mon_bus0, urb, status);
}
/* int (*unlink_urb) (struct urb *urb, int status); */
diff --git a/drivers/usb/mon/mon_text.c b/drivers/usb/mon/mon_text.c
index 663a702a17dd..ebb04ac4857b 100644
--- a/drivers/usb/mon/mon_text.c
+++ b/drivers/usb/mon/mon_text.c
@@ -183,7 +183,7 @@ static inline unsigned int mon_get_timestamp(void)
}
static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
- char ev_type)
+ char ev_type, int status)
{
struct mon_event_text *ep;
unsigned int stamp;
@@ -210,7 +210,7 @@ static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
ep->length = (ev_type == 'S') ?
urb->transfer_buffer_length : urb->actual_length;
/* Collecting status makes debugging sense for submits, too */
- ep->status = urb->status;
+ ep->status = status;
if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
ep->interval = urb->interval;
@@ -248,13 +248,13 @@ static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
static void mon_text_submit(void *data, struct urb *urb)
{
struct mon_reader_text *rp = data;
- mon_text_event(rp, urb, 'S');
+ mon_text_event(rp, urb, 'S', -EINPROGRESS);
}
-static void mon_text_complete(void *data, struct urb *urb)
+static void mon_text_complete(void *data, struct urb *urb, int status)
{
struct mon_reader_text *rp = data;
- mon_text_event(rp, urb, 'C');
+ mon_text_event(rp, urb, 'C', status);
}
static void mon_text_error(void *data, struct urb *urb, int error)
diff --git a/drivers/usb/mon/usb_mon.h b/drivers/usb/mon/usb_mon.h
index f68ad6d99ad7..f5d84ff8c101 100644
--- a/drivers/usb/mon/usb_mon.h
+++ b/drivers/usb/mon/usb_mon.h
@@ -46,7 +46,7 @@ struct mon_reader {
void (*rnf_submit)(void *data, struct urb *urb);
void (*rnf_error)(void *data, struct urb *urb, int error);
- void (*rnf_complete)(void *data, struct urb *urb);
+ void (*rnf_complete)(void *data, struct urb *urb, int status);
};
void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r);
]
|
| 45/2 |
2026/09/28 05:52 |
llm |
expert |
0m
Model:
Tokens:
input: 40659
output: 58
thoughts: 2852
Thoughts: **Analysis of the usbmon Race Condition**
Okay, let's break this down. My initial concern is whether `bus->monitored` is safe to use as a flag, given that it's read in the hot path. It seems like the core issue revolves around potential race conditions between the setting and reading of this flag, particularly in the context of URB submission.
First, I see that `bus->monitored` is an integer flag that acts as a guard. When non-zero, it enables the `usbmon` hooks within the URB submission/completion/error paths. If zero, it's a fast path, avoiding the overhead of calling into `usbmon`. `mon_reader_add` sets this flag, and `mon_reader_del` clears it, with `mon_stop` handling the reset if the reader count drops to zero. The kicker is that `mon_stop` modifies `bus->monitored` without holding a lock that's used in the URB submission path.
My first thought is, is this a benign heuristic flag as the protocol suggests? My instinct is to determine whether it is being used in a "Truly Benign" manner or a "HARMFUL" way.
I looked carefully at the protocol guidelines. It looks like we're hoping this is a "Heuristic Hint" where a stale value causes a safe, although potentially sub-optimal, fast path or is a Single-Writer Flag Update with a bitwise check.
The crucial point is that when `bus->monitored` is not zero, the code calls `mon_bus_submit`. Inside `mon_bus_submit`, there is the acquisition of a lock (`mbus->lock`) that protects access to the reader list (`mbus->r_list`). This is good! It means the reader list manipulation is serialized. So, at a glance, it seems that if `mon_stop` has set `bus->monitored` to 0 *before* `mon_bus_submit` acquires the lock, everything is fine. The lock provides an order. The critical sections are protected. The loop will just not be entered.
My next concern is what happens if `bus->monitored` is zero when the reader *was* just added. In that case, we simply miss the first URB submission for the newly opened monitor, which is normal and acceptable.
Now, my mind goes to the memory ordering. The `mon_stop` function caught my eye, especially the presence of the `mb()` memory barrier in `mon_stop` (but not in other places). I was worried about ordering violations. `mon_stop` writes `bus->monitored = 0` and I need to ensure this is seen by other CPUs. Then, I see `mon_dissolve` also has `mb()`. Then I started tracking why `mon_stop` had it. The comment mentions `synchronize_irq()`, which suggests a need for an explicit barrier.
Next, I think "What about torn reads/writes?" Fortunately, `bus->monitored` is a 32-bit integer, and on all Linux architectures, a naturally aligned 32-bit load/store is atomic. I can rule out the possibility of the value being split during the read or write operation.
Finally, I wonder about compiler optimizations. I check where `usbmon_urb_submit` is called. It's not called in a loop, so the compiler is unlikely to hoist or fuse the `bus->monitored` read.
So it all looks safe!
|
| 46/2 |
2026/09/28 05:52 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:drivers/usb/core/hcd.c IncludeSnippetLines:10 Name:usbmon_urb_submit SourceTreePrefix:drivers/usb/]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:usb_hcd_submit_urb SourceFile:drivers/usb/core/hcd.c SourceLine:1527 SourceSnippet:1517: int status;
1518: struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
1519:
1520: /* increment urb's reference count as part of giving it to the HCD
1521: * (which will control it). HCD guarantees that it either returns
1522: * an error or calls giveback(), but not both.
1523: */
1524: usb_get_urb(urb);
1525: atomic_inc(&urb->use_count);
1526: atomic_inc(&urb->dev->urbnum);
1527: usbmon_urb_submit(&hcd->self, urb);
1528:
1529: /* NOTE requirements on root-hub callers (usbfs and the hub
1530: * driver, for now): URBs' urb->transfer_buffer must be
1531: * valid and usb_buffer_{sync,unmap}() not be needed, since
1532: * they could clobber root hub response data. Also, control
1533: * URBs must be submitted in process context with interrupts
1534: * enabled.
1535: */
1536:
1537: if (is_root_hub(urb->dev)) {
]] TruncatedOutput:false]
|
| 47/2 |
2026/09/28 05:52 |
llm |
expert |
0m
Model:
Tokens:
input: 44772
output: 54
thoughts: 111
Thoughts: **USB Monitor Submissions and Completions: A Quick Reality Check**
Okay, so I was looking into the USB monitoring code, specifically around URB (USB Request Block) handling, and I initially thought there might be some kind of loop involved in the submission process. But now I'm seeing that `usbmon_urb_submit` is actually called directly within `usb_hcd_submit_urb`. That's important! It's not a loop at all; it's being called once for *each* URB that gets submitted. No chance of getting caught in a runaway.
Same deal with `usbmon_urb_submit_error`. It's only called when there's an error return, so it makes sense. And, thinking about completions, I see that `usbmon_urb_complete` is called within `usb_hcd_giveback_urb`. That's the expected place. No surprises there.
Wait a second, though... I need to consider `mon_ops`. I need to make sure that isn't modified dynamically. Gotta check that out to ensure that the proper monitoring functions are actually being called.
|
| 48/2 |
2026/09/28 05:52 |
tool |
codesearch-find-references |
0m
Error:
Args: map[ContextFile:include/linux/usb/hcd.h IncludeSnippetLines:5 Name:mon_ops SourceTreePrefix:drivers/usb/]
Results: map[References:<nil> TruncatedOutput:false]
|
| 49/2 |
2026/09/28 05:52 |
llm |
expert |
0m
Model:
Tokens:
input: 45202
output: 26
thoughts: 17
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:
**Initial Investigation into USB Driver Monitoring Operations**
Alright, so the initial thought process is pretty straightforward. I'm hitting a USB driver issue, probably related to monitoring some aspect of its operation. I need to get a handle on the relevant code. Given the context, I immediately think of using `grep` to quickly locate the instances where `mon_ops` is used within the USB driver directory. This assumes `mon_ops` is a defined structure or variable name used for monitor operations within the USB subsystem, a common pattern. I'm going to start there. This grep search is a fast way to narrow down the relevant source files and code blocks related to these monitor operations. No need to waste time going down a manual dive if I can use a simple grep statement to assist.
|
| 50/2 |
2026/09/28 05:52 |
tool |
grepper |
0m
Args: map[Expression:mon_ops PathPrefix:drivers/usb]
Results: map[Output:drivers/usb/core/hcd.c=3154=EXPORT_SYMBOL_GPL(usb_hcd_setup_local_mem);
--
drivers/usb/core/hcd.c-3159-
drivers/usb/core/hcd.c:3160:const struct usb_mon_operations *mon_ops;
drivers/usb/core/hcd.c-3161-
--
drivers/usb/core/hcd.c=3170=int usb_mon_register(const struct usb_mon_operations *ops)
--
drivers/usb/core/hcd.c-3172-
drivers/usb/core/hcd.c:3173: if (mon_ops)
drivers/usb/core/hcd.c-3174- return -EBUSY;
drivers/usb/core/hcd.c-3175-
drivers/usb/core/hcd.c:3176: mon_ops = ops;
drivers/usb/core/hcd.c-3177- mb();
--
drivers/usb/core/hcd.c=3182=void usb_mon_deregister (void)
--
drivers/usb/core/hcd.c-3184-
drivers/usb/core/hcd.c:3185: if (mon_ops == NULL) {
drivers/usb/core/hcd.c-3186- printk(KERN_ERR "USB: monitor was not registered\n");
--
drivers/usb/core/hcd.c-3188- }
drivers/usb/core/hcd.c:3189: mon_ops = NULL;
drivers/usb/core/hcd.c-3190- mb();
--
drivers/usb/mon/mon_main.c=227=static struct notifier_block mon_nb = {
--
drivers/usb/mon/mon_main.c-233- */
drivers/usb/mon/mon_main.c:234:static const struct usb_mon_operations mon_ops_0 = {
drivers/usb/mon/mon_main.c-235- .urb_submit = mon_submit,
--
drivers/usb/mon/mon_main.c=337=static int __init mon_init(void)
--
drivers/usb/mon/mon_main.c-348-
drivers/usb/mon/mon_main.c:349: if (usb_mon_register(&mon_ops_0) != 0) {
drivers/usb/mon/mon_main.c-350- printk(KERN_NOTICE TAG ": unable to register with the core\n");
]
|
| 51/2 |
2026/09/28 05:52 |
llm |
expert |
0m
Model:
Tokens:
input: 46110
output: 36
thoughts: 1305
Thoughts: **Data Race Analysis: `bus->monitored` Flag in `usbmon`**
Okay, let's break this down. My initial focus was on `mon_ops`, which is set during module initialization and cleared during exit. But wait, the race isn't on `mon_ops`; it's on `bus->monitored`! This is a 4-byte value that determines whether USB monitoring is active for a given bus.
The write to this flag happens in `mon_stop`, when a reader file descriptor is closed, where it's set to `0`. The read occurs within `usbmon_urb_submit`. The key is this conditional: `if (bus->monitored)`.
Now, the crucial question: is this a BENIGN or HARMFUL data race? I'm classifying this. `bus->monitored` acts as a simple flag: `0` means no monitoring, and the URB submission *skips* `usbmon`. `1` means monitoring is active, and the URB submission *calls* `mon_submit`, acquiring a spinlock. Even if there are no readers, the function safely does nothing and releases the lock.
Here's the crux: `mon_reader_del` sets `ubus->monitored = 0` concurrently. If `usb_hcd_submit_urb` reads a `1`, it proceeds into `mon_submit`, grabs the spinlock, processes the event (or does nothing), and releases the lock. If it reads `0`, it completely skips `mon_submit`. The reader removal and USB traffic are completely asynchronous. This non-determinism, a read of `1` *after* the writer set it to `0`, or a read of `0` before it was set to `1` is harmless! The race doesn't cause incorrect behavior, memory corruption, UAF, deadlock, or crash.
But... do we need `READ_ONCE()` or `WRITE_ONCE()`? Let's check the rules. The algorithm is logically sound and doesn't require these annotations. This race doesn't involve:
* Fused Accesses: We're not in a polling/wait loop.
* Torn Accesses: It's a 32-bit (4-byte) aligned access.
* Ordering Violations: The `mbus->lock` provides the necessary synchronization for internal data, and the flag is merely an optimization.
This falls directly under "Heuristic Hints" and "Single-Writer Flag Updates". I'm remembering this.
Wait! Are there *multiple* writers? Let's check.
Writers are:
* `mon_reader_add` (under `mbus->lock` and `mon_lock`),
* `mon_stop` (under `mbus->lock` and `mon_lock`),
* `mon_bus_add` (under `mon_lock`),
* `mon_dissolve` (under `mon_lock`).
All the writers are serialized under `mon_lock` or `mbus->lock`! The readers, like `usbmon_urb_submit`, are lockless readers simply testing the flag.
So, this looks like a harmless race. Let me check `codeexpert` now and search the git logs for any existing discussions on this. Just to be sure.
|
| 52/2 |
2026/09/28 05:52 |
tool |
git-log |
0m
Args: map[CodeRegexp:READ_ONCE.*monitored PathPrefix:include/linux/usb]
Results:
|
| 53/2 |
2026/09/28 05:52 |
llm |
expert |
0m
Model:
Tokens:
input: 48196
output: 34
thoughts: 22
Thoughts: Here's my thought process, as requested:
**Checking Code History for Write-Once Monitoring**
Okay, so I need to track down where this "WRITE_ONCE" pattern, specifically when it relates to monitoring, has been implemented in the codebase. Let's start with a `git log`. Seems the most efficient way to do it will be a CodeRegexp search for the phrase "WRITE_ONCE" in close proximity to the term "monitored". This narrows the search down to the relevant commits, hopefully. Should be a pretty straightforward way to find the relevant implementation or change history. This should provide the information needed for any future analysis.
|
| 54/2 |
2026/09/28 05:52 |
tool |
git-log |
0m
Args: map[CodeRegexp:WRITE_ONCE.*monitored PathPrefix:drivers/usb]
Results:
|
| 55/2 |
2026/09/28 05:53 |
llm |
expert |
0m
Model:
Tokens:
input: 48403
output: 39
thoughts: 24
Thoughts: Here's my thought process, summarized as you requested:
**Debugging Strategy for USBmon Data Corruption**
Okay, time to get to the bottom of this. My initial instinct is to go straight to the source and start digging through the `git log` to see what kind of activity has been happening around the `usbmon` module. We've got potential issues with data races or KCSAN triggering something, so I need to filter the commit history to see if anything jumps out. I'll use `MessageRegexps` with targeted keywords: "usbmon", and then terms like "data-race", "KCSAN", "race condition", or anything else that might indicate potential concurrency problems. This approach should help me quickly identify relevant commits that might offer clues about the cause of this corruption. Hopefully, I'll be able to quickly zero in on a fix from there!
|
| 56/2 |
2026/09/28 05:53 |
tool |
git-log |
0m
Args: map[Count:10 MessageRegexps:[usbmon race] PathPrefix:drivers/usb/mon]
Results: map[Output:2dd23cc4d0e6 usb: mon: Fix atomicity violation in mon_bin_vma_fault
47cb17089c05 USB: usbmon: mask seconds properly in text API
2bc0d109326e usbmon: add bus number to text API
f1c0a2a3aff5 USB: usbmon: fix read(2)
bb4e3b5ac81a usbmon: fix tiny race exposed by the fastboot patches
ae0d6cceb20e [PATCH] USB: Patch to make usbmon to print control setup packets
]
|
| 57/2 |
2026/09/28 05:53 |
llm |
expert |
0m
Model:
Tokens:
input: 48805
output: 25
thoughts: 16
Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person:
**Analyzing the Codebase - Initial Scan**
Alright, let's dive into this commit, `bb4e3b5ac81a`. My initial instinct is to treat this as a potentially significant area of change, considering that the ID is on the shorter side, suggesting it's either part of a larger change or a very targeted one. I need to understand the scope and implications without making any assumptions. I'll need to use some tooling for a quick check.
|
| 58/2 |
2026/09/28 05:53 |
tool |
git-show |
0m
Args:
Results: map[Output:commit bb4e3b5ac81a9601775e62649038934acafa78e2
Author: Arjan van de Ven <arjan@linux.intel.com>
Date: Mon Sep 22 15:00:10 2008 -0700
usbmon: fix tiny race exposed by the fastboot patches
usbmon registers the notifier chain, takes the bus lock and then goes to
scan the existing devices for hooking up.
Unfortunately, if usb_mon gets initialized while USB bus discovery is
going on, it's possible that usbmon gets a notifier on one cpu (which runs
without USB locks), and the scan is going on and also finds the new bus,
resulting in a double sysfs registration, which then produces a WARNING.
Pete Zaitcev did the bug diagnostics on this one
Signed-off-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/drivers/usb/mon/mon_main.c b/drivers/usb/mon/mon_main.c
index 442d8076b201..5e0ab4201c00 100644
--- a/drivers/usb/mon/mon_main.c
+++ b/drivers/usb/mon/mon_main.c
@@ -361,12 +361,12 @@ static int __init mon_init(void)
}
// MOD_INC_USE_COUNT(which_module?);
- usb_register_notify(&mon_nb);
mutex_lock(&usb_bus_list_lock);
list_for_each_entry (ubus, &usb_bus_list, bus_list) {
mon_bus_init(ubus);
}
+ usb_register_notify(&mon_nb);
mutex_unlock(&usb_bus_list_lock);
return 0;
]
|
| 59/2 |
2026/09/28 05:53 |
llm |
expert |
0m
Model:
Tokens:
input: 49543
output: 169
thoughts: 168
Thoughts: **Analyzing a Potential Race Condition in the USB Subsystem**
Okay, let's take a look at this. Before diving too deep, I think it's wise to consult `codeexpert`. I need to be precise, so I'll phrase the question carefully. The context is the Linux kernel's USB subsystem, specifically the interaction between the `usb_bus` struct, the `usbmon_urb_submit()` function (in `include/linux/usb/hcd.h`), and `mon_stop()` (in `drivers/usb/mon/mon_main.c`). The `usb_bus` struct contains an `int monitored` flag. `usbmon_urb_submit()` *reads* this flag, and `mon_stop()` *modifies* it, setting it to zero when a reader is removed via `mon_reader_del()`.
The core of the problem, as I see it, is a potential race condition. My question to `codeexpert` then is this: If `usbmon_urb_submit()` reads `bus->monitored` concurrently with `mon_stop()` setting `ubus->monitored` to zero, is this a benign interaction? Specifically, can a stale read β a read of `monitored == 1` when it should have been `0`, or a read of `monitored == 0` when it should have been `1` β lead to memory corruption, a use-after-free scenario, or some other kind of invalid behavior? I need to know if this subtle interaction could have serious consequences. I'm looking for a clear understanding of the potential impact on the system's stability.
|
| 60/2 |
2026/09/28 05:53 |
tool |
codeexpert |
5m
Args: map[Question:In the Linux kernel USB subsystem, struct usb_bus has an `int monitored` flag checked in `usbmon_urb_submit()` (`include/linux/usb/hcd.h`) and modified in `mon_stop()` (`drivers/usb/mon/mon_main.c`) when a reader is removed via `mon_reader_del()`. Is a concurrent read of `bus->monitored` in `usbmon_urb_submit()` while `mon_stop()` sets `ubus->monitored = 0` benign or harmful? Can a stale read of `monitored == 1` or `monitored == 0` cause any memory corruption, use-after-free, or invalid behavior?]
Results: map[Answer:In the Linux kernel USB subsystem, a concurrent read of `bus->monitored` in `usbmon_urb_submit()` while `mon_stop()` clears `ubus->monitored = 0` is **completely benign**. Neither a stale read of `monitored == 1` nor `monitored == 0` can cause memory corruption, use-after-free (UAF), or invalid behavior.
### 1. Architectural Role of `bus->monitored`
The check in `include/linux/usb/hcd.h`:
```c
static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
{
if (bus->monitored)
(*mon_ops->urb_submit)(bus, urb);
}
```
is an intentional, lockless fast-path optimization designed to avoid taking locks on the high-throughput URB submission path (`usb_hcd_submit_urb()`) when no readers are active. `monitored` is an aligned `int`, meaning reads and writes are single-word atomic operations on all supported Linux architectures (no word tearing).
---
### 2. Analysis of Stale Read: `monitored == 0`
If `usbmon_urb_submit()` observes `monitored == 0`:
- It simply skips calling `(*mon_ops->urb_submit)(bus, urb)` and returns immediately.
- The URB submission proceeds normally through `usb_hcd_submit_urb()`.
- At worst, a packet trace event is omitted right around the moment when the last reader is unsubscribing. This is standard and expected behavior during monitor shutdown.
- **Result:** No corruption, no invalid state.
---
### 3. Analysis of Stale Read: `monitored == 1`
If `usbmon_urb_submit()` observes `monitored == 1` after `mon_stop()` has already set or is setting `ubus->monitored = 0`:
- It invokes `mon_submit(ubus, urb)` (via `mon_ops->urb_submit`).
- `mon_submit()` invokes `mon_bus_submit(mbus, urb)` for the bus (and `mon_bus0`).
- **Reader list synchronization:**
In `mon_bus_submit()`:
```c
spin_lock_irqsave(&mbus->lock, flags);
mbus->cnt_events++;
list_for_each_entry(r, &mbus->r_list, r_link)
r->rnf_submit(r->r_data, urb);
spin_unlock_irqrestore(&mbus->lock, flags);
```
`mon_bus_submit()` acquires `mbus->lock`. This is the **exact same spinlock** acquired by `mon_reader_del()`:
```c
spin_lock_irqsave(&mbus->lock, flags);
list_del(&r->r_link);
--mbus->nreaders;
if (mbus->nreaders == 0)
mon_stop(mbus);
spin_unlock_irqrestore(&mbus->lock, flags);
```
- If `mon_bus_submit()` acquires `mbus->lock` **before** `mon_reader_del()`: the reader is still safely on `mbus->r_list` and receives the callback normally.
- If `mon_bus_submit()` acquires `mbus->lock` **after** `mon_reader_del()`: the reader has already been unlinked via `list_del(&r->r_link)`. The list traversal sees only valid remaining readers, or an empty list. The deleted reader callback is not invoked.
- Furthermore, the reader struct `r` is only freed later in `mon_bin_release()` / `mon_text_release()` after `mon_reader_del()` returns and under `mon_lock`.
- **Data structure lifetime (`ubus` and `mbus`):**
- `ubus` is `&hcd->self` receiving the URB and is kept valid throughout `usb_hcd_submit_urb()`.
- `mbus` (`ubus->mon_bus`) is **not** freed by `mon_reader_del()`. Its refcount was initialized to 1 at bus registration (`mon_bus_init()`), and each reader takes an additional reference (`kref_get(&mbus->ref)`). `mon_reader_del()` only drops the reader's reference via `kref_put(&mbus->ref, mon_bus_drop)`. The base reference remains intact until the USB host controller itself is unregistered (`mon_bus_remove()`).
- **Result:** No use-after-free, no memory corruption.
---
### Verification and Limitations
- **Verified:** Source implementations of `usbmon_urb_submit()`, `mon_stop()`, `mon_reader_del()`, `mon_reader_add()`, `mon_bus_submit()`, `mon_bin_release()`, and `mon_text_release()` were verified in the codebase.
- **Unverified / Nuance:** Although KCSAN (Kernel Concurrency Sanitizer) might theoretically report a plain data-race on `bus->monitored` due to the lack of explicit `READ_ONCE()` / `WRITE_ONCE()` annotations (code dates back to Linux 2.6), this is a classic benign race in the kernel where exact point-in-time synchronization is unnecessary and memory ordering is safeguarded by downstream spinlocks.]
|
| 61/3 |
2026/09/28 05:53 |
agent |
codeexpert |
5m
Model:
Results: map[Answer:In the Linux kernel USB subsystem, a concurrent read of `bus->monitored` in `usbmon_urb_submit()` while `mon_stop()` clears `ubus->monitored = 0` is **completely benign**. Neither a stale read of `monitored == 1` nor `monitored == 0` can cause memory corruption, use-after-free (UAF), or invalid behavior.
### 1. Architectural Role of `bus->monitored`
The check in `include/linux/usb/hcd.h`:
```c
static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
{
if (bus->monitored)
(*mon_ops->urb_submit)(bus, urb);
}
```
is an intentional, lockless fast-path optimization designed to avoid taking locks on the high-throughput URB submission path (`usb_hcd_submit_urb()`) when no readers are active. `monitored` is an aligned `int`, meaning reads and writes are single-word atomic operations on all supported Linux architectures (no word tearing).
---
### 2. Analysis of Stale Read: `monitored == 0`
If `usbmon_urb_submit()` observes `monitored == 0`:
- It simply skips calling `(*mon_ops->urb_submit)(bus, urb)` and returns immediately.
- The URB submission proceeds normally through `usb_hcd_submit_urb()`.
- At worst, a packet trace event is omitted right around the moment when the last reader is unsubscribing. This is standard and expected behavior during monitor shutdown.
- **Result:** No corruption, no invalid state.
---
### 3. Analysis of Stale Read: `monitored == 1`
If `usbmon_urb_submit()` observes `monitored == 1` after `mon_stop()` has already set or is setting `ubus->monitored = 0`:
- It invokes `mon_submit(ubus, urb)` (via `mon_ops->urb_submit`).
- `mon_submit()` invokes `mon_bus_submit(mbus, urb)` for the bus (and `mon_bus0`).
- **Reader list synchronization:**
In `mon_bus_submit()`:
```c
spin_lock_irqsave(&mbus->lock, flags);
mbus->cnt_events++;
list_for_each_entry(r, &mbus->r_list, r_link)
r->rnf_submit(r->r_data, urb);
spin_unlock_irqrestore(&mbus->lock, flags);
```
`mon_bus_submit()` acquires `mbus->lock`. This is the **exact same spinlock** acquired by `mon_reader_del()`:
```c
spin_lock_irqsave(&mbus->lock, flags);
list_del(&r->r_link);
--mbus->nreaders;
if (mbus->nreaders == 0)
mon_stop(mbus);
spin_unlock_irqrestore(&mbus->lock, flags);
```
- If `mon_bus_submit()` acquires `mbus->lock` **before** `mon_reader_del()`: the reader is still safely on `mbus->r_list` and receives the callback normally.
- If `mon_bus_submit()` acquires `mbus->lock` **after** `mon_reader_del()`: the reader has already been unlinked via `list_del(&r->r_link)`. The list traversal sees only valid remaining readers, or an empty list. The deleted reader callback is not invoked.
- Furthermore, the reader struct `r` is only freed later in `mon_bin_release()` / `mon_text_release()` after `mon_reader_del()` returns and under `mon_lock`.
- **Data structure lifetime (`ubus` and `mbus`):**
- `ubus` is `&hcd->self` receiving the URB and is kept valid throughout `usb_hcd_submit_urb()`.
- `mbus` (`ubus->mon_bus`) is **not** freed by `mon_reader_del()`. Its refcount was initialized to 1 at bus registration (`mon_bus_init()`), and each reader takes an additional reference (`kref_get(&mbus->ref)`). `mon_reader_del()` only drops the reader's reference via `kref_put(&mbus->ref, mon_bus_drop)`. The base reference remains intact until the USB host controller itself is unregistered (`mon_bus_remove()`).
- **Result:** No use-after-free, no memory corruption.
---
### Verification and Limitations
- **Verified:** Source implementations of `usbmon_urb_submit()`, `mon_stop()`, `mon_reader_del()`, `mon_reader_add()`, `mon_bus_submit()`, `mon_bin_release()`, and `mon_text_release()` were verified in the codebase.
- **Unverified / Nuance:** Although KCSAN (Kernel Concurrency Sanitizer) might theoretically report a plain data-race on `bus->monitored` due to the lack of explicit `READ_ONCE()` / `WRITE_ONCE()` annotations (code dates back to Linux 2.6), this is a classic benign race in the kernel where exact point-in-time synchronization is unnecessary and memory ordering is safeguarded by downstream spinlocks.]
Instruction:
You are a capable Linux kernel developer tasked with researching complex questions
about kernel source code. You will be given a concrete question, and need to provide
a concrete answer.
Research the question using targeted, efficient tool calls. Don't make assumptions,
or rely on your previous knowledge about the kernel source code, use available tools
to access the actual source code. Keep your tool calls focused and limited to the
scope of the question. Do not explore tangential code paths or perform unbounded searches.
Use all available sources of information:
- kernel source code
- documentation in the Documentation dir in the source tree
- git commits descriptions, git blame
Do not guess file names or file paths and attempt to read them without
verifying their existence first using content search or directory listing tools.
If a file, symbol, or directory is not found via content search (grepper) or
directory listing (codesearch-dir-index), treat it as completely absent.
Do not attempt to guess alternative names, extensions, or directories.
Tool Selection Guidelines:
1. For C functions, structs, and variables: Prefer codesearch-definition-source
or codesearch-struct-layout FIRST to retrieve clean, exact definitions
without line-number guessing.
2. If symbol lookup fails (e.g., preprocessor macros, macro-generated code, or
disabled #ifdef branches), fall back to read-file or grepper.
3. For file headers, #include directives, preprocessor macro definitions, and
non-C files (Kconfig, Makefiles, docs): Use read-file or grepper directly.
4. Be concise and targeted. Answer the question as directly and concisely as possible
once you have located the relevant code or definitions. If a symbol or function cannot
be found after targeted searches, conclude with the best available information rather
than continuing repetitive searches.
Do NOT use git-log to search for the presence or existence of files in the
repository. git-log is only for tracing commit history of files that are
already present in the current checkout. If a file does not exist in the
current checkout, it cannot be used for reproduction.
Avoid running broad git-log queries (such as searches on the entire repo)
without a specific 'PathPrefix' to restrict the scope, as these are very
expensive and will time out.
If a git-log tool call times out, do not retry the query with the same broad
scope. You must specify a tighter, narrower 'PathPrefix' for subsequent queries.
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: In the Linux kernel USB subsystem, struct usb_bus has an `int monitored` flag checked in `usbmon_urb_submit()` (`include/linux/usb/hcd.h`) and modified in `mon_stop()` (`drivers/usb/mon/mon_main.c`) when a reader is removed via `mon_reader_del()`. Is a concurrent read of `bus->monitored` in `usbmon_urb_submit()` while `mon_stop()` sets `ubus->monitored = 0` benign or harmful? Can a stale read of `monitored == 1` or `monitored == 0` cause any memory corruption, use-after-free, or invalid behavior?
|
| 62/4 |
2026/09/28 05:53 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 4301
output: 68
thoughts: 313
Thoughts: **Analyzing Concurrency in the Linux USB Subsystem**
Okay, so a user is asking a pretty specific question about concurrency within the Linux kernel's USB subsystem. They're focused on the `usb_bus` struct, specifically the `monitored` flag. The core of their question boils down to whether a concurrent read of `bus->monitored` in `usbmon_urb_submit()` is safe when `mon_stop()` is potentially modifying it via `mon_reader_del()`. They're concerned about potential stale reads, leading to issues like memory corruption or use-after-free scenarios.
This isn't an exploit or anything malicious; it's a solid, focused concurrency analysis question. It's a low-severity, benign request, essentially a concurrency audit. We're talking about understanding the code's behavior, not crafting an attack.
My plan is to dig into the relevant code: `usbmon_urb_submit()`, `mon_stop()`, `mon_reader_del()`, and the `usb_bus` structure. I'll need to see *how* `monitored` is checked and modified, and what synchronization mechanisms, if any, are in place. I'll use tools like `codesearch-definition-source`, `grepper`, and `read-file` to navigate the code and understand the context surrounding these functions and the `monitored` flag. The goal is a clear understanding of the data flow and concurrency safety within this specific piece of the Linux kernel's USB subsystem.
|
| 63/4 |
2026/09/28 05:54 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:include/linux/usb/hcd.h Name:usbmon_urb_submit]
Results: map[SourceCode: 721: static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
722: {
723: if (bus->monitored)
724: (*mon_ops->urb_submit)(bus, urb);
725: }
SourceFile:include/linux/usb/hcd.h]
|
| 64/4 |
2026/09/28 05:54 |
tool |
grepper |
0m
Args: map[Expression:mon_stop PathPrefix:drivers/usb/mon]
Results: map[Output:drivers/usb/mon/mon_main.c-20-
drivers/usb/mon/mon_main.c:21:static void mon_stop(struct mon_bus *mbus);
drivers/usb/mon/mon_main.c-22-static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
--
drivers/usb/mon/mon_main.c=65=void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
--
drivers/usb/mon/mon_main.c-72- if (mbus->nreaders == 0)
drivers/usb/mon/mon_main.c:73: mon_stop(mbus);
drivers/usb/mon/mon_main.c-74- spin_unlock_irqrestore(&mbus->lock, flags);
--
drivers/usb/mon/mon_main.c=141=static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
--
drivers/usb/mon/mon_main.c-155- */
drivers/usb/mon/mon_main.c:156:static void mon_stop(struct mon_bus *mbus)
drivers/usb/mon/mon_main.c-157-{
]
|
| 65/4 |
2026/09/28 05:54 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 5454
output: 39
thoughts: 68
Thoughts: Okay, let's dive into this. Here's how I'm thinking about it, focusing on `drivers/usb/mon/mon_main.c`:
**Dissecting the USB Monitor Module: A Deep Dive**
My goal here is to understand the crucial parts of this USB monitor implementation, specifically how it manages the monitoring process, particularly around the deletion of readers and the stopping of the monitoring itself. I want to build a coherent mental model of how data flows, and where potential issues might reside.
I'm starting by zeroing in on `mon_reader_del` and `mon_stop`. These functions are, without a doubt, going to be central to how readers are added, removed, and how the monitoring process is shut down gracefully. I'll need to trace the implications of these calls carefully.
Next, I'll need to examine `mon_ops->urb_submit`. This will undoubtedly illuminate how the monitor interacts with the USB bus itself. How does it intercept URBs? What's the timing? How does it buffer data, if at all? The specifics of the `urb_submit` operation are the key to the core functionality of the monitoring system.
Of course, the lifetime of key data structures is crucial here. I must examine the lives of the `struct mon_bus`, `mon_ops`, `struct usb_bus`, and of the various reader structures. How are these objects allocated, initialized, and, critically, cleaned up? I need to understand the dependencies between them and how the lifecycle of one affects the others. I need to know precisely *when* and *where* these structures are created and destroyed. The goal here is a clean, understandable lifecycle, without leaks, use-after-free conditions, or other related problems.
|
| 66/4 |
2026/09/28 05:54 |
tool |
read-file |
0m
Args: map[File:drivers/usb/mon/mon_main.c FirstLine:40 LineCount:100]
Results: map[Contents: 40:
41: spin_lock_irqsave(&mbus->lock, flags);
42: if (mbus->nreaders == 0) {
43: if (mbus == &mon_bus0) {
44: list_for_each (p, &mon_buses) {
45: struct mon_bus *m1;
46: m1 = list_entry(p, struct mon_bus, bus_link);
47: m1->u_bus->monitored = 1;
48: }
49: } else {
50: mbus->u_bus->monitored = 1;
51: }
52: }
53: mbus->nreaders++;
54: list_add_tail(&r->r_link, &mbus->r_list);
55: spin_unlock_irqrestore(&mbus->lock, flags);
56:
57: kref_get(&mbus->ref);
58: }
59:
60: /*
61: * Unlink reader from the bus.
62: *
63: * This is called with mon_lock taken, so we can decrement mbus->ref.
64: */
65: void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
66: {
67: unsigned long flags;
68:
69: spin_lock_irqsave(&mbus->lock, flags);
70: list_del(&r->r_link);
71: --mbus->nreaders;
72: if (mbus->nreaders == 0)
73: mon_stop(mbus);
74: spin_unlock_irqrestore(&mbus->lock, flags);
75:
76: kref_put(&mbus->ref, mon_bus_drop);
77: }
78:
79: /*
80: */
81: static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
82: {
83: unsigned long flags;
84: struct mon_reader *r;
85:
86: spin_lock_irqsave(&mbus->lock, flags);
87: mbus->cnt_events++;
88: list_for_each_entry(r, &mbus->r_list, r_link)
89: r->rnf_submit(r->r_data, urb);
90: spin_unlock_irqrestore(&mbus->lock, flags);
91: }
92:
93: static void mon_submit(struct usb_bus *ubus, struct urb *urb)
94: {
95: struct mon_bus *mbus;
96:
97: mbus = ubus->mon_bus;
98: if (mbus != NULL)
99: mon_bus_submit(mbus, urb);
100: mon_bus_submit(&mon_bus0, urb);
101: }
102:
103: /*
104: */
105: static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
106: {
107: unsigned long flags;
108: struct mon_reader *r;
109:
110: spin_lock_irqsave(&mbus->lock, flags);
111: mbus->cnt_events++;
112: list_for_each_entry(r, &mbus->r_list, r_link)
113: r->rnf_error(r->r_data, urb, error);
114: spin_unlock_irqrestore(&mbus->lock, flags);
115: }
116:
117: static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
118: {
119: struct mon_bus *mbus;
120:
121: mbus = ubus->mon_bus;
122: if (mbus != NULL)
123: mon_bus_submit_error(mbus, urb, error);
124: mon_bus_submit_error(&mon_bus0, urb, error);
125: }
126:
127: /*
128: */
129: static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
130: {
131: unsigned long flags;
132: struct mon_reader *r;
133:
134: spin_lock_irqsave(&mbus->lock, flags);
135: mbus->cnt_events++;
136: list_for_each_entry(r, &mbus->r_list, r_link)
137: r->rnf_complete(r->r_data, urb, status);
138: spin_unlock_irqrestore(&mbus->lock, flags);
139: }
]
|
| 67/4 |
2026/09/28 05:54 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 7387
output: 39
thoughts: 29
Thoughts: Okay, here's my interpretation of reading lines 140-220 of `drivers/usb/mon/mon_main.c`, presented as a first-person account of my thoughts:
**Analyzing the USB Monitor Core Code**
Alright, let's dive into this specific section of the USB monitor code, lines 140 to 220 within `drivers/usb/mon/mon_main.c`. My initial focus, as always, is on understanding the core functionality this segment contributes to the overall monitoring process. Given the file and the general structure of the USB stack, I'm expecting to see elements related to data capture, filtering, and potentially the initial handling of incoming USB traffic intended for monitoring.
I'm quickly scanning for key data structures β likely something related to packet buffers, filter definitions, and structures that tie into the `usbmon` device itself. The goal is to identify how incoming USB traffic gets stored, perhaps how it's classified, and which parts of the kernel are responsible for managing the data flow. I'll be keeping an eye out for any concurrency issues that might be present β lock usage, interrupt handling, and the overall thread safety of the monitor's operations. Any poorly-designed locking patterns here could severely impact the performance and stability of the kernel, so this is a key area of scrutiny.
I'm also looking for the interfaces the module exposes to user-space. Are there ioctls defined? Sysfs entries? How does user-space configure the monitor's behaviour, define filters, and retrieve captured data? Understanding the user-space interaction is crucial for a complete picture. Are there any indications of performance optimizations, such as zero-copy data transfer or efficient buffering techniques? Given the potentially high volume of USB traffic, this is essential.
I'll be mindful of any unusual patterns or any potentially buggy code that might be present here, so I'll be quick to use my extensive memory and experience with USB and kernel code to spot any oddities that may have been missed by others in code review.
|
| 68/4 |
2026/09/28 05:54 |
tool |
read-file |
0m
Args: map[File:drivers/usb/mon/mon_main.c FirstLine:140 LineCount:80]
Results: map[Contents: 140:
141: static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
142: {
143: struct mon_bus *mbus;
144:
145: mbus = ubus->mon_bus;
146: if (mbus != NULL)
147: mon_bus_complete(mbus, urb, status);
148: mon_bus_complete(&mon_bus0, urb, status);
149: }
150:
151: /* int (*unlink_urb) (struct urb *urb, int status); */
152:
153: /*
154: * Stop monitoring.
155: */
156: static void mon_stop(struct mon_bus *mbus)
157: {
158: struct usb_bus *ubus;
159:
160: if (mbus == &mon_bus0) {
161: list_for_each_entry(mbus, &mon_buses, bus_link) {
162: /*
163: * We do not change nreaders here, so rely on mon_lock.
164: */
165: if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
166: ubus->monitored = 0;
167: }
168: } else {
169: /*
170: * A stop can be called for a dissolved mon_bus in case of
171: * a reader staying across an rmmod foo_hcd, so test ->u_bus.
172: */
173: if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
174: ubus->monitored = 0;
175: mb();
176: }
177: }
178: }
179:
180: /*
181: * Add a USB bus (usually by a modprobe foo-hcd)
182: *
183: * This does not return an error code because the core cannot care less
184: * if monitoring is not established.
185: */
186: static void mon_bus_add(struct usb_bus *ubus)
187: {
188: mon_bus_init(ubus);
189: mutex_lock(&mon_lock);
190: if (mon_bus0.nreaders != 0)
191: ubus->monitored = 1;
192: mutex_unlock(&mon_lock);
193: }
194:
195: /*
196: * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
197: */
198: static void mon_bus_remove(struct usb_bus *ubus)
199: {
200: struct mon_bus *mbus = ubus->mon_bus;
201:
202: mutex_lock(&mon_lock);
203: list_del(&mbus->bus_link);
204: if (mbus->text_inited)
205: mon_text_del(mbus);
206: if (mbus->bin_inited)
207: mon_bin_del(mbus);
208:
209: mon_dissolve(mbus, ubus);
210: kref_put(&mbus->ref, mon_bus_drop);
211: mutex_unlock(&mon_lock);
212: }
213:
214: static int mon_notify(struct notifier_block *self, unsigned long action,
215: void *dev)
216: {
217: switch (action) {
218: case USB_BUS_ADD:
219: mon_bus_add(dev);
]
|
| 69/4 |
2026/09/28 05:54 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 9053
output: 40
thoughts: 73
Thoughts: Here's my analysis of what I'm thinking:
**Dissecting USB Monitor Functionality**
Okay, let's dive into this. I need to understand the inner workings of `mon_dissolve`, `mon_bus_drop`, and `mon_bus_init`, and how the `mon_ops` structure ties everything together. The goal here is to get a handle on the USB monitor's fundamental operational model. Let's see how the drivers are set up. I'll need to locate lines 220 to 350 of `drivers/usb/mon/mon_main.c` and focus on the `mon_dissolve` function β that's likely where the core of this operation resides. That section will probably provide the logic I need to understand what's happening.
|
| 70/4 |
2026/09/28 05:54 |
tool |
read-file |
0m
Args: map[File:drivers/usb/mon/mon_main.c FirstLine:220 LineCount:100]
Results: map[Contents: 220: break;
221: case USB_BUS_REMOVE:
222: mon_bus_remove(dev);
223: }
224: return NOTIFY_OK;
225: }
226:
227: static struct notifier_block mon_nb = {
228: .notifier_call = mon_notify,
229: };
230:
231: /*
232: * Ops
233: */
234: static const struct usb_mon_operations mon_ops_0 = {
235: .urb_submit = mon_submit,
236: .urb_submit_error = mon_submit_error,
237: .urb_complete = mon_complete,
238: };
239:
240: /*
241: * Tear usb_bus and mon_bus apart.
242: */
243: static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
244: {
245:
246: if (ubus->monitored) {
247: ubus->monitored = 0;
248: mb();
249: }
250:
251: ubus->mon_bus = NULL;
252: mbus->u_bus = NULL;
253: mb();
254:
255: /* We want synchronize_irq() here, but that needs an argument. */
256: }
257:
258: /*
259: */
260: static void mon_bus_drop(struct kref *r)
261: {
262: struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
263: kfree(mbus);
264: }
265:
266: /*
267: * Initialize a bus for us:
268: * - allocate mon_bus
269: * - refcount USB bus struct
270: * - link
271: */
272: static void mon_bus_init(struct usb_bus *ubus)
273: {
274: struct mon_bus *mbus;
275:
276: mbus = kzalloc_obj(struct mon_bus);
277: if (mbus == NULL)
278: goto err_alloc;
279: kref_init(&mbus->ref);
280: spin_lock_init(&mbus->lock);
281: INIT_LIST_HEAD(&mbus->r_list);
282:
283: /*
284: * We don't need to take a reference to ubus, because we receive
285: * a notification if the bus is about to be removed.
286: */
287: mbus->u_bus = ubus;
288: ubus->mon_bus = mbus;
289:
290: mbus->text_inited = mon_text_add(mbus, ubus);
291: mbus->bin_inited = mon_bin_add(mbus, ubus);
292:
293: mutex_lock(&mon_lock);
294: list_add_tail(&mbus->bus_link, &mon_buses);
295: mutex_unlock(&mon_lock);
296: return;
297:
298: err_alloc:
299: return;
300: }
301:
302: static void mon_bus0_init(void)
303: {
304: struct mon_bus *mbus = &mon_bus0;
305:
306: kref_init(&mbus->ref);
307: spin_lock_init(&mbus->lock);
308: INIT_LIST_HEAD(&mbus->r_list);
309:
310: mbus->text_inited = mon_text_add(mbus, NULL);
311: mbus->bin_inited = mon_bin_add(mbus, NULL);
312: }
313:
314: /*
315: * Search a USB bus by number. Notice that USB bus numbers start from one,
316: * which we may later use to identify "all" with zero.
317: *
318: * This function must be called with mon_lock held.
319: *
]
|
| 71/4 |
2026/09/28 05:54 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 10688
output: 37
thoughts: 37
Thoughts: Okay, let's dive into this USB monitoring code.
**Diving into the USB Monitoring Code**
Alright, my first instinct is to get a handle on the overall structure. I need to understand the big picture before I start picking at individual functions. So, let's start with `drivers/usb/mon/mon_main.c`. Seeing the main file should give me a good entry point. After that, I'll need to look at how new monitors are added, and how the "reader" functions work. I need to check out `mon_reader_add`, and also functions like `mon_open`. These are likely key to understanding how monitoring sessions are initiated and data is actually pulled from the USB bus. This should provide the foundation for a good analysis of the subsystem.
|
| 72/4 |
2026/09/28 05:54 |
tool |
read-file |
0m
Args: map[File:drivers/usb/mon/mon_main.c FirstLine:1 LineCount:45]
Results: map[Contents: 1: // SPDX-License-Identifier: GPL-2.0
2: /*
3: * The USB Monitor, inspired by Dave Harding's USBMon.
4: *
5: * mon_main.c: Main file, module initiation and exit, registrations, etc.
6: *
7: * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com)
8: */
9:
10: #include <linux/kernel.h>
11: #include <linux/module.h>
12: #include <linux/usb.h>
13: #include <linux/usb/hcd.h>
14: #include <linux/slab.h>
15: #include <linux/notifier.h>
16: #include <linux/mutex.h>
17:
18: #include "usb_mon.h"
19:
20:
21: static void mon_stop(struct mon_bus *mbus);
22: static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
23: static void mon_bus_drop(struct kref *r);
24: static void mon_bus_init(struct usb_bus *ubus);
25:
26: DEFINE_MUTEX(mon_lock);
27:
28: struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
29: static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
30:
31: /*
32: * Link a reader into the bus.
33: *
34: * This must be called with mon_lock taken because of mbus->ref.
35: */
36: void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
37: {
38: unsigned long flags;
39: struct list_head *p;
40:
41: spin_lock_irqsave(&mbus->lock, flags);
42: if (mbus->nreaders == 0) {
43: if (mbus == &mon_bus0) {
44: list_for_each (p, &mon_buses) {
45: struct mon_bus *m1;
]
|
| 73/4 |
2026/09/28 05:54 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 11591
output: 39
thoughts: 29
Thoughts: Okay, let's dive into this. I need to get a handle on the tail end of `drivers/usb/mon/mon_main.c`, specifically from line 320 to the end. I'm assuming there's some crucial functionality implemented in this section, likely related to the monitoring process itself β perhaps finalization, cleanup, or the handling of specific events.
**Analyzing `mon_main.c` from Line 320 Onward**
I'm expecting to find the nitty-gritty details here. It could involve deallocating resources associated with the monitoring, shutting down threads, perhaps handling error conditions after the monitoring has run, or maybe even finalizing statistics gathering. It could be specific to handling a shutdown request or exiting after an error occurred. I'll need to look for any calls to functions related to device unregistration or cleanup of data structures. The file naming convention gives us some clues that the focus here is likely the main "orchestrator" logic for the USB monitor, so I anticipate seeing the end game of the monitoring session. If there are kernel modules involved, I expect to see the unloading process. The last few lines should give me a solid picture of the entire function's role in the USB monitoring setup. Let's see what we've got.
|
| 74/4 |
2026/09/28 05:54 |
tool |
read-file |
0m
Args: map[File:drivers/usb/mon/mon_main.c FirstLine:320 LineCount:80]
Results: map[Contents: 320: * This is obviously inefficient and may be revised in the future.
321: */
322: struct mon_bus *mon_bus_lookup(unsigned int num)
323: {
324: struct mon_bus *mbus;
325:
326: if (num == 0) {
327: return &mon_bus0;
328: }
329: list_for_each_entry(mbus, &mon_buses, bus_link) {
330: if (mbus->u_bus->busnum == num) {
331: return mbus;
332: }
333: }
334: return NULL;
335: }
336:
337: static int __init mon_init(void)
338: {
339: struct usb_bus *ubus;
340: int rc, id;
341:
342: if ((rc = mon_text_init()) != 0)
343: goto err_text;
344: if ((rc = mon_bin_init()) != 0)
345: goto err_bin;
346:
347: mon_bus0_init();
348:
349: if (usb_mon_register(&mon_ops_0) != 0) {
350: printk(KERN_NOTICE TAG ": unable to register with the core\n");
351: rc = -ENODEV;
352: goto err_reg;
353: }
354: // MOD_INC_USE_COUNT(which_module?);
355:
356: mutex_lock(&usb_bus_idr_lock);
357: idr_for_each_entry(&usb_bus_idr, ubus, id)
358: mon_bus_init(ubus);
359: usb_register_notify(&mon_nb);
360: mutex_unlock(&usb_bus_idr_lock);
361: return 0;
362:
363: err_reg:
364: mon_bin_exit();
365: err_bin:
366: mon_text_exit();
367: err_text:
368: return rc;
369: }
370:
371: static void __exit mon_exit(void)
372: {
373: struct mon_bus *mbus;
374: struct list_head *p;
375:
376: usb_unregister_notify(&mon_nb);
377: usb_mon_deregister();
378:
379: mutex_lock(&mon_lock);
380:
381: while (!list_empty(&mon_buses)) {
382: p = mon_buses.next;
383: mbus = list_entry(p, struct mon_bus, bus_link);
384: list_del(p);
385:
386: if (mbus->text_inited)
387: mon_text_del(mbus);
388: if (mbus->bin_inited)
389: mon_bin_del(mbus);
390:
391: /*
392: * This never happens, because the open/close paths in
393: * file level maintain module use counters and so rmmod fails
394: * before reaching here. However, better be safe...
395: */
396: if (mbus->nreaders) {
397: printk(KERN_ERR TAG
398: ": Outstanding opens (%d) on usb%d, leaking...\n",
399: mbus->nreaders, mbus->u_bus->busnum);
]
|
| 75/4 |
2026/09/28 05:54 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 13055
output: 46
thoughts: 55
Thoughts: Okay, here's my assessment of the situation:
**Investigating Potential Data Corruption in the USB Monitoring System**
Right, so we've got a potential issue on our hands, and my first step is to methodically track down the source. I need to dig into the relationship between `mon_ops`, `usbmon_urb_submit`, and any possible interaction points. My immediate focus is going to be on a few key areas. I need to thoroughly examine `mon_ops` β where it's defined, how it's initialized, and every single place where it's invoked. This is crucial for understanding its role in the system.
Simultaneously, I'll be searching for `usbmon_urb_submit` and, of course, any references to `mon_ops` as well. This should give me a holistic view of how these pieces fit together. This focused approach is the most efficient way to pinpoint any potential data corruption scenarios or unexpected behavior. This should give me a clear picture of what's happening.
|
| 76/4 |
2026/09/28 05:54 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:include/linux/usb/hcd.h IncludeSnippetLines:5 Name:usbmon_urb_submit]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:usb_hcd_submit_urb SourceFile:drivers/usb/core/hcd.c SourceLine:1527 SourceSnippet:1522: * an error or calls giveback(), but not both.
1523: */
1524: usb_get_urb(urb);
1525: atomic_inc(&urb->use_count);
1526: atomic_inc(&urb->dev->urbnum);
1527: usbmon_urb_submit(&hcd->self, urb);
1528:
1529: /* NOTE requirements on root-hub callers (usbfs and the hub
1530: * driver, for now): URBs' urb->transfer_buffer must be
1531: * valid and usb_buffer_{sync,unmap}() not be needed, since
1532: * they could clobber root hub response data. Also, control
]] TruncatedOutput:false]
|
| 77/4 |
2026/09/28 05:54 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 13652
output: 38
thoughts: 28
Thoughts: Okay, let's dive into this. Here's my thought process as I approach the task:
**Deconstructing `usbmon_urb_submit` Definitions**
Alright, the objective is to understand how `usbmon_urb_submit` works, so I need to go to the source: `include/linux/usb/hcd.h`. That's where the core definitions related to USB host controller drivers are located. My first step is to methodically parse the code in that file, zeroing in on anything related to `usbmon_urb_submit`. I'll be looking for the structure definitions, function prototypes, and any associated macros or inline functions that shed light on how this function integrates with the USB stack. I'll need to understand the data structures it takes as arguments, the return values, and what kind of operations it's likely responsible for. This includes understanding any interactions with URBs (USB Request Blocks), the core data structure for USB transfers, and how this relates to the host controller driver's interaction with the USB hardware. Essentially, I'm tracing the data flow to understand the complete picture. I'll also be paying attention to any comments or documentation in the file to help decipher the design and purpose of this specific mechanism.
|
| 78/4 |
2026/09/28 05:54 |
tool |
read-file |
0m
Args: map[File:include/linux/usb/hcd.h FirstLine:715 LineCount:40]
Results: map[Contents: 715: void (*urb_complete)(struct usb_bus *bus, struct urb *urb, int status);
716: /* void (*urb_unlink)(struct usb_bus *bus, struct urb *urb); */
717: };
718:
719: extern const struct usb_mon_operations *mon_ops;
720:
721: static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
722: {
723: if (bus->monitored)
724: (*mon_ops->urb_submit)(bus, urb);
725: }
726:
727: static inline void usbmon_urb_submit_error(struct usb_bus *bus, struct urb *urb,
728: int error)
729: {
730: if (bus->monitored)
731: (*mon_ops->urb_submit_error)(bus, urb, error);
732: }
733:
734: static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb,
735: int status)
736: {
737: if (bus->monitored)
738: (*mon_ops->urb_complete)(bus, urb, status);
739: }
740:
741: int usb_mon_register(const struct usb_mon_operations *ops);
742: void usb_mon_deregister(void);
743:
744: #else
745:
746: static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb) {}
747: static inline void usbmon_urb_submit_error(struct usb_bus *bus, struct urb *urb,
748: int error) {}
749: static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb,
750: int status) {}
751:
752: #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */
753:
754: /*-------------------------------------------------------------------------*/
]
|
| 79/4 |
2026/09/28 05:54 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 14610
output: 26
thoughts: 61
Thoughts: Okay, let's break this down.
**Diving Deep into the USB Monitor Implementation**
Right, so I need to understand this `mon_ops` structure. Where is it defined, what's its purpose? And how does it relate to the `usb_mon_register` and `usb_mon_deregister` functions? Those registration and deregistration routines are key. I need to understand the lifecycle here. I'll need to trace the flow, see how this monitoring system is set up and torn down. Then, I need to get into `mon_submit`. That's where the actual data handling happens, so that's crucial. I need to understand how events are submitted, what kind of checks are involved and what this is using the monitored USB data for. Time to go hunting for `usb_mon_register` and `mon_ops`. Let's see what we can dig up.
|
| 80/4 |
2026/09/28 05:54 |
tool |
grepper |
0m
Args: map[Expression:mon_ops PathPrefix:drivers/usb]
Results: map[Output:drivers/usb/core/hcd.c=3154=EXPORT_SYMBOL_GPL(usb_hcd_setup_local_mem);
--
drivers/usb/core/hcd.c-3159-
drivers/usb/core/hcd.c:3160:const struct usb_mon_operations *mon_ops;
drivers/usb/core/hcd.c-3161-
--
drivers/usb/core/hcd.c=3170=int usb_mon_register(const struct usb_mon_operations *ops)
--
drivers/usb/core/hcd.c-3172-
drivers/usb/core/hcd.c:3173: if (mon_ops)
drivers/usb/core/hcd.c-3174- return -EBUSY;
drivers/usb/core/hcd.c-3175-
drivers/usb/core/hcd.c:3176: mon_ops = ops;
drivers/usb/core/hcd.c-3177- mb();
--
drivers/usb/core/hcd.c=3182=void usb_mon_deregister (void)
--
drivers/usb/core/hcd.c-3184-
drivers/usb/core/hcd.c:3185: if (mon_ops == NULL) {
drivers/usb/core/hcd.c-3186- printk(KERN_ERR "USB: monitor was not registered\n");
--
drivers/usb/core/hcd.c-3188- }
drivers/usb/core/hcd.c:3189: mon_ops = NULL;
drivers/usb/core/hcd.c-3190- mb();
--
drivers/usb/mon/mon_main.c=227=static struct notifier_block mon_nb = {
--
drivers/usb/mon/mon_main.c-233- */
drivers/usb/mon/mon_main.c:234:static const struct usb_mon_operations mon_ops_0 = {
drivers/usb/mon/mon_main.c-235- .urb_submit = mon_submit,
--
drivers/usb/mon/mon_main.c=337=static int __init mon_init(void)
--
drivers/usb/mon/mon_main.c-348-
drivers/usb/mon/mon_main.c:349: if (usb_mon_register(&mon_ops_0) != 0) {
drivers/usb/mon/mon_main.c-350- printk(KERN_NOTICE TAG ": unable to register with the core\n");
]
|
| 81/4 |
2026/09/28 05:54 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 15567
output: 29
thoughts: 2001
Thoughts: **Analyzing the USBmon Concurrency Issue**
Alright, let's dissect this concurrency issue in the Linux kernel's USB monitoring subsystem. The core of the question revolves around the race condition between reading the `monitored` flag in `usbmon_urb_submit` and the setting of `ubus->monitored = 0` in `mon_stop`, triggered by the removal of a reader through `mon_reader_del`.
First, I need to understand the control flow. `mon_reader_del` is the starting point. It acquires a spinlock, deletes a reader from the list, decrements the reader count, and *then* calls `mon_stop` if the reader count hits zero. `mon_stop` iterates through the list of monitored buses and sets `ubus->monitored = 0` for any bus with no readers. Importantly, `mon_stop` is *also* protected by the same spinlock acquired in `mon_reader_del`.
Meanwhile, `usbmon_urb_submit` checks the `bus->monitored` flag. If it's set, it calls `mon_ops->urb_submit`. This function goes through `mon_submit`, which then calls `mon_bus_submit`. `mon_bus_submit` *also* attempts to acquire the same spinlock that `mon_reader_del` is using! It also iterates the list of readers.
Now, let's explore the possible scenarios:
1. **Stale Read of `monitored == 0`:** If `bus->monitored` reads as zero (either a stale read, or the flag has already been cleared by `mon_stop`), `usbmon_urb_submit` immediately returns. In this case, no monitoring occurs, and there's no race. It's completely benign; a monitoring event is simply skipped for a bus which is no longer being monitored, or which is in the process of being stopped. This seems safe since a reader is in the process of being removed.
2. **Stale Read of `monitored == 1`:** If `bus->monitored` reads as one, then `mon_ops->urb_submit`, i.e., `mon_submit`, is called. This function *will* acquire the spinlock in `mon_bus_submit`.
* **Scenario 1: `mon_bus_submit` acquires the lock *before* `mon_reader_del`:** This scenario is simple and benign. `mon_bus_submit` sees the reader in the list, processes the event, releases the lock, and *then* `mon_reader_del` runs.
* **Scenario 2: `mon_reader_del` acquires the lock *before* `mon_bus_submit`:** In this case, `mon_bus_submit` waits for `mon_reader_del` to release the lock. When it does, `mon_reader_del` has already deleted the reader from the list. Therefore, `list_for_each_entry` will be an empty list, `cnt_events` will be incremented, the lock is released, and the function returns. The critical point here is that the reader is already gone, and the worst that can happen is an empty iteration in `mon_bus_submit`, which is safe and expected, and a race can't happen.
* **Scenario 3: A stale value `bus->monitored == 1` being checked *after* `mon_reader_del` has run, but before `mon_bus_submit` has run:** In this case, `mon_submit` would execute, see a stale bus->monitored flag of 1, but find the corresponding spinlock available, enter `mon_bus_submit` and acquire the lock. It would then iterate the empty list and release the lock. This is not problematic; monitoring has stopped. The missed event isn't an issue.
The more tricky question is about the lifetime of `mbus` itself, where `mbus` comes from, and whether it could be freed while in use. In `mon_reader_del`, after releasing the spinlock, there's a `kref_put(&mbus->ref, mon_bus_drop)`. It looks like that releases the ref and potentially frees `mbus`. But the spinlock acquisition in `mon_bus_submit` protects against races with the removal of `mbus` *while* monitoring events are taking place and readers are being removed. Since the spinlock *is* acquired, the reader list and `mbus` are safely accessible.
Given the existing spinlock usage, I conclude that while the stale read of `mon
|
| 82/4 |
2026/09/28 05:54 |
tool |
grepper |
0m
Args: map[Expression:mbus->ref PathPrefix:drivers/usb/mon]
Results: map[Output:drivers/usb/mon/mon_main.c=29=static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
--
drivers/usb/mon/mon_main.c-33- *
drivers/usb/mon/mon_main.c:34: * This must be called with mon_lock taken because of mbus->ref.
drivers/usb/mon/mon_main.c-35- */
drivers/usb/mon/mon_main.c=36=void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
--
drivers/usb/mon/mon_main.c-56-
drivers/usb/mon/mon_main.c:57: kref_get(&mbus->ref);
drivers/usb/mon/mon_main.c-58-}
--
drivers/usb/mon/mon_main.c-62- *
drivers/usb/mon/mon_main.c:63: * This is called with mon_lock taken, so we can decrement mbus->ref.
drivers/usb/mon/mon_main.c-64- */
drivers/usb/mon/mon_main.c=65=void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
--
drivers/usb/mon/mon_main.c-75-
drivers/usb/mon/mon_main.c:76: kref_put(&mbus->ref, mon_bus_drop);
drivers/usb/mon/mon_main.c-77-}
--
drivers/usb/mon/mon_main.c=198=static void mon_bus_remove(struct usb_bus *ubus)
--
drivers/usb/mon/mon_main.c-209- mon_dissolve(mbus, ubus);
drivers/usb/mon/mon_main.c:210: kref_put(&mbus->ref, mon_bus_drop);
drivers/usb/mon/mon_main.c-211- mutex_unlock(&mon_lock);
--
drivers/usb/mon/mon_main.c=272=static void mon_bus_init(struct usb_bus *ubus)
--
drivers/usb/mon/mon_main.c-278- goto err_alloc;
drivers/usb/mon/mon_main.c:279: kref_init(&mbus->ref);
drivers/usb/mon/mon_main.c-280- spin_lock_init(&mbus->lock);
--
drivers/usb/mon/mon_main.c=302=static void mon_bus0_init(void)
--
drivers/usb/mon/mon_main.c-305-
drivers/usb/mon/mon_main.c:306: kref_init(&mbus->ref);
drivers/usb/mon/mon_main.c-307- spin_lock_init(&mbus->lock);
--
drivers/usb/mon/mon_main.c=371=static void __exit mon_exit(void)
--
drivers/usb/mon/mon_main.c-399- mbus->nreaders, mbus->u_bus->busnum);
drivers/usb/mon/mon_main.c:400: kref_get(&mbus->ref); /* Force leak */
drivers/usb/mon/mon_main.c-401- }
--
drivers/usb/mon/mon_main.c-403- mon_dissolve(mbus, mbus->u_bus);
drivers/usb/mon/mon_main.c:404: kref_put(&mbus->ref, mon_bus_drop);
drivers/usb/mon/mon_main.c-405- }
]
|
| 83/4 |
2026/09/28 05:54 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 19570
output: 36
thoughts: 646
Thoughts: **Analyzing the USB Monitoring Subsystem Concurrency**
Okay, let's break this down. My initial observation is that the reference counting mechanism with `kref` in the USB monitor (usbmon) seems robust, particularly with respect to the `mon_bus` structure. When `mon_bus_init(ubus)` is called, the reference count (`mbus->ref`) starts at 1, essentially representing the USB bus itself and its monitoring context. Each call to `mon_reader_add` increments this count via `kref_get(&mbus->ref)`, indicating an active reader. Conversely, `mon_reader_del` decrements the count using `kref_put(&mbus->ref, mon_bus_drop)`. It's crucial to understand that the `mbus` isn't freed by a mere `mon_reader_del()` call because the base reference, originating from the USB bus's own existence, remains until `mon_bus_remove(ubus)` or `mon_exit()` is called. In fact, `mon_bus0`, as a statically allocated structure, is never dynamically freed which adds an interesting wrinkle.
The critical question here involves concurrency and the `monitored` flag in `struct usb_bus`. Specifically, it concerns the potential for race conditions between reads and writes to this flag. When does `mon_bus_remove(ubus)` even get called? It looks like it's linked to the removal of the USB bus itself β such as when a host controller driver unloads or the device is unplugged. The design appears to prevent `usb_hcd_submit_urb()` from being called when the HCD is stopping or the device is unplugged. This suggests a careful handoff of resource management.
Therefore, the core question is: is the concurrent access to the `bus->monitored` flag in `usbmon_urb_submit()` when `mon_stop()` sets `ubus->monitored = 0` benign? More broadly, can a stale read of `monitored == 1` or `monitored == 0` cause any problems like memory corruption, use-after-free conditions, or other undefined behavior?
I need to dive deeper into the code. Time to explore the git history, check relevant comments, and scrutinize the actual code paths. I will focus my search within `drivers/usb/mon`, `include/linux/usb/hcd.h`, and possibly `drivers/usb/core/hcd.c`, specifically looking for references to `monitored` to pinpoint the interactions.
|
| 84/4 |
2026/09/28 05:55 |
tool |
git-log |
0m
Args: map[CodeRegexp:monitored Count:10 PathPrefix:drivers/usb/mon]
Results: map[Output:ecb658d387dc usbmon: bus zero
1da177e4c3f4 Linux-2.6.12-rc2
]
|
| 85/4 |
2026/09/28 05:55 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 20857
output: 25
thoughts: 17
Thoughts: Okay, here's my interpretation of what's going through my mind, rendered in first person, keeping in mind the user is a subject-matter expert:
**Analyzing Commit ecb658d387dc**
Alright, let's dive into commit `ecb658d387dc`. Immediately, my brain starts running through the typical questions. What's the context? What's the area of the codebase being touched? Did it reference any issues or tickets? I'm already mentally forming a hypothesis based on the commit hash itself, hoping itβs related to one of the ongoing refactoring efforts I'm tracking.
I'm probably going to start by glancing at the diff itself, looking for any immediate red flags, like unexpected large-scale changes, particularly in core or shared components. I'll be mentally parsing the code, looking for potential performance bottlenecks, security vulnerabilities, or logic errors that might have snuck in. I'm especially keen on checking for any regression β is this change going to break something else that was working fine?
If it's a feature commit, I'll be looking for signs of careful planning, adherence to design principles, and, critically, test coverage. Iβm thinking, does it follow the project's coding standards? Is it well-documented? Iβll expect to see clear, concise code, not overly complex or convoluted.
The key is efficiency. I'm going to quickly ascertain the scope and impact of this commit, and then, depending on what I see, I'll drill down further into the details. If it looks straightforward, I'll give it a quick once-over to confirm my initial assessment and move on. If there's anything even slightly ambiguous or complex, I'll delve deep, maybe even running some local tests to ensure the behavior matches the intent.
|
| 86/4 |
2026/09/28 05:55 |
tool |
git-show |
0m
Args:
Results: map[Output:commit ecb658d387dc09f344b3d755e8674076072032c7
Author: Pete Zaitcev <zaitcev@redhat.com>
Date: Wed Apr 11 13:47:26 2007 -0700
usbmon: bus zero
Add the "bus zero" feature to the usbmon. If a user process specifies bus
with number zero, it receives events from all buses. This is useful when
we wish to see initial enumeration when a bus is created, typically after
a modprobe. Until now, an application had to loop until a new bus could
be open, then start capturing on it. This procedure was cumbersome and
could lose initial events. Also, often it's too bothersome to find exactly
to which bus a specific device is attached.
Paolo Albeni provided the original concept implementation. I added the
handling of "bus->monitored" flag and generally fixed it up.
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
index b2bedd974ac3..0af11a66207c 100644
--- a/drivers/usb/mon/mon_bin.c
+++ b/drivers/usb/mon/mon_bin.c
@@ -356,8 +356,10 @@ static inline char mon_bin_get_setup(unsigned char *setupb,
if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
return '-';
- if (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
return mon_dmapeek(setupb, urb->setup_dma, SETUP_LEN);
+ }
if (urb->setup_packet == NULL)
return 'Z';
@@ -369,7 +371,8 @@ static char mon_bin_get_data(const struct mon_reader_bin *rp,
unsigned int offset, struct urb *urb, unsigned int length)
{
- if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) {
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
mon_dmapeek_vec(rp, offset, urb->transfer_dma, length);
return 0;
}
@@ -440,7 +443,7 @@ static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
/* We use the fact that usb_pipein() returns 0x80 */
ep->epnum = usb_pipeendpoint(urb->pipe) | usb_pipein(urb->pipe);
ep->devnum = usb_pipedevice(urb->pipe);
- ep->busnum = rp->r.m_bus->u_bus->busnum;
+ ep->busnum = urb->dev->bus->busnum;
ep->id = (unsigned long) urb;
ep->ts_sec = ts.tv_sec;
ep->ts_usec = ts.tv_usec;
@@ -500,7 +503,7 @@ static void mon_bin_error(void *data, struct urb *urb, int error)
/* We use the fact that usb_pipein() returns 0x80 */
ep->epnum = usb_pipeendpoint(urb->pipe) | usb_pipein(urb->pipe);
ep->devnum = usb_pipedevice(urb->pipe);
- ep->busnum = rp->r.m_bus->u_bus->busnum;
+ ep->busnum = urb->dev->bus->busnum;
ep->id = (unsigned long) urb;
ep->status = error;
@@ -515,7 +518,6 @@ static void mon_bin_error(void *data, struct urb *urb, int error)
static int mon_bin_open(struct inode *inode, struct file *file)
{
struct mon_bus *mbus;
- struct usb_bus *ubus;
struct mon_reader_bin *rp;
size_t size;
int rc;
@@ -525,7 +527,7 @@ static int mon_bin_open(struct inode *inode, struct file *file)
mutex_unlock(&mon_lock);
return -ENODEV;
}
- if ((ubus = mbus->u_bus) == NULL) {
+ if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
printk(KERN_ERR TAG ": consistency error on open\n");
mutex_unlock(&mon_lock);
return -ENODEV;
diff --git a/drivers/usb/mon/mon_main.c b/drivers/usb/mon/mon_main.c
index c9739e7b35e5..8a1df2c9c73e 100644
--- a/drivers/usb/mon/mon_main.c
+++ b/drivers/usb/mon/mon_main.c
@@ -16,8 +16,6 @@
#include "usb_mon.h"
#include "../core/hcd.h"
-static void mon_submit(struct usb_bus *ubus, struct urb *urb);
-static void mon_complete(struct usb_bus *ubus, struct urb *urb);
static void mon_stop(struct mon_bus *mbus);
static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
static void mon_bus_drop(struct kref *r);
@@ -25,6 +23,7 @@ static void mon_bus_init(struct usb_bus *ubus);
DEFINE_MUTEX(mon_lock);
+struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
/*
@@ -35,22 +34,19 @@ static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
{
unsigned long flags;
- struct usb_bus *ubus;
+ struct list_head *p;
spin_lock_irqsave(&mbus->lock, flags);
if (mbus->nreaders == 0) {
- ubus = mbus->u_bus;
- if (ubus->monitored) {
- /*
- * Something is really broken, refuse to go on and
- * possibly corrupt ops pointers or worse.
- */
- printk(KERN_ERR TAG ": bus %d is already monitored\n",
- ubus->busnum);
- spin_unlock_irqrestore(&mbus->lock, flags);
- return;
+ if (mbus == &mon_bus0) {
+ list_for_each (p, &mon_buses) {
+ struct mon_bus *m1;
+ m1 = list_entry(p, struct mon_bus, bus_link);
+ m1->u_bus->monitored = 1;
+ }
+ } else {
+ mbus->u_bus->monitored = 1;
}
- ubus->monitored = 1;
}
mbus->nreaders++;
list_add_tail(&r->r_link, &mbus->r_list);
@@ -80,77 +76,79 @@ void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
/*
*/
-static void mon_submit(struct usb_bus *ubus, struct urb *urb)
+static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
{
- struct mon_bus *mbus;
unsigned long flags;
struct list_head *pos;
struct mon_reader *r;
- mbus = ubus->mon_bus;
- if (mbus == NULL)
- goto out_unlocked;
-
spin_lock_irqsave(&mbus->lock, flags);
- if (mbus->nreaders == 0)
- goto out_locked;
-
mbus->cnt_events++;
list_for_each (pos, &mbus->r_list) {
r = list_entry(pos, struct mon_reader, r_link);
r->rnf_submit(r->r_data, urb);
}
-
spin_unlock_irqrestore(&mbus->lock, flags);
return;
+}
-out_locked:
- spin_unlock_irqrestore(&mbus->lock, flags);
-out_unlocked:
- return;
+static void mon_submit(struct usb_bus *ubus, struct urb *urb)
+{
+ struct mon_bus *mbus;
+
+ if ((mbus = ubus->mon_bus) != NULL)
+ mon_bus_submit(mbus, urb);
+ mon_bus_submit(&mon_bus0, urb);
}
/*
*/
-static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
+static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
{
- struct mon_bus *mbus;
unsigned long flags;
struct list_head *pos;
struct mon_reader *r;
- mbus = ubus->mon_bus;
- if (mbus == NULL)
- goto out_unlocked;
-
spin_lock_irqsave(&mbus->lock, flags);
- if (mbus->nreaders == 0)
- goto out_locked;
-
mbus->cnt_events++;
list_for_each (pos, &mbus->r_list) {
r = list_entry(pos, struct mon_reader, r_link);
r->rnf_error(r->r_data, urb, error);
}
-
spin_unlock_irqrestore(&mbus->lock, flags);
return;
+}
-out_locked:
- spin_unlock_irqrestore(&mbus->lock, flags);
-out_unlocked:
- return;
+static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
+{
+ struct mon_bus *mbus;
+
+ if ((mbus = ubus->mon_bus) != NULL)
+ mon_bus_submit_error(mbus, urb, error);
+ mon_bus_submit_error(&mon_bus0, urb, error);
}
/*
*/
-static void mon_complete(struct usb_bus *ubus, struct urb *urb)
+static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb)
{
- struct mon_bus *mbus;
unsigned long flags;
struct list_head *pos;
struct mon_reader *r;
+ spin_lock_irqsave(&mbus->lock, flags);
+ mbus->cnt_events++;
+ list_for_each (pos, &mbus->r_list) {
+ r = list_entry(pos, struct mon_reader, r_link);
+ r->rnf_complete(r->r_data, urb);
+ }
+ spin_unlock_irqrestore(&mbus->lock, flags);
+}
+
+static void mon_complete(struct usb_bus *ubus, struct urb *urb)
+{
+ struct mon_bus *mbus;
+
mbus = ubus->mon_bus;
if (mbus == NULL) {
/*
@@ -162,13 +160,8 @@ static void mon_complete(struct usb_bus *ubus, struct urb *urb)
return;
}
- spin_lock_irqsave(&mbus->lock, flags);
- mbus->cnt_events++;
- list_for_each (pos, &mbus->r_list) {
- r = list_entry(pos, struct mon_reader, r_link);
- r->rnf_complete(r->r_data, urb);
- }
- spin_unlock_irqrestore(&mbus->lock, flags);
+ mon_bus_complete(mbus, urb);
+ mon_bus_complete(&mon_bus0, urb);
}
/* int (*unlink_urb) (struct urb *urb, int status); */
@@ -179,14 +172,26 @@ static void mon_complete(struct usb_bus *ubus, struct urb *urb)
static void mon_stop(struct mon_bus *mbus)
{
struct usb_bus *ubus = mbus->u_bus;
+ struct list_head *p;
- /*
- * A stop can be called for a dissolved mon_bus in case of
- * a reader staying across an rmmod foo_hcd.
- */
- if (ubus != NULL) {
- ubus->monitored = 0;
- mb();
+ if (mbus == &mon_bus0) {
+ list_for_each (p, &mon_buses) {
+ mbus = list_entry(p, struct mon_bus, bus_link);
+ /*
+ * We do not change nreaders here, so rely on mon_lock.
+ */
+ if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
+ ubus->monitored = 0;
+ }
+ } else {
+ /*
+ * A stop can be called for a dissolved mon_bus in case of
+ * a reader staying across an rmmod foo_hcd, so test ->u_bus.
+ */
+ if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
+ ubus->monitored = 0;
+ mb();
+ }
}
}
@@ -199,6 +204,10 @@ static void mon_stop(struct mon_bus *mbus)
static void mon_bus_add(struct usb_bus *ubus)
{
mon_bus_init(ubus);
+ mutex_lock(&mon_lock);
+ if (mon_bus0.nreaders != 0)
+ ubus->monitored = 1;
+ mutex_unlock(&mon_lock);
}
/*
@@ -250,12 +259,7 @@ static struct usb_mon_operations mon_ops_0 = {
static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
{
- /*
- * Never happens, but...
- */
if (ubus->monitored) {
- printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
- ubus->busnum);
ubus->monitored = 0;
mb();
}
@@ -263,6 +267,8 @@ static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
ubus->mon_bus = NULL;
mbus->u_bus = NULL;
mb();
+
+ /* We want synchronize_irq() here, but that needs an argument. */
}
/*
@@ -295,9 +301,8 @@ static void mon_bus_init(struct usb_bus *ubus)
*/
mbus->u_bus = ubus;
ubus->mon_bus = mbus;
- mbus->uses_dma = ubus->uses_dma;
- mbus->text_inited = mon_text_add(mbus, ubus);
+ mbus->text_inited = mon_text_add(mbus, ubus->busnum);
// mon_bin_add(...)
mutex_lock(&mon_lock);
@@ -309,6 +314,18 @@ static void mon_bus_init(struct usb_bus *ubus)
return;
}
+static void mon_bus0_init(void)
+{
+ struct mon_bus *mbus = &mon_bus0;
+
+ kref_init(&mbus->ref);
+ spin_lock_init(&mbus->lock);
+ INIT_LIST_HEAD(&mbus->r_list);
+
+ mbus->text_inited = mon_text_add(mbus, 0);
+ // mbus->bin_inited = mon_bin_add(mbus, 0);
+}
+
/*
* Search a USB bus by number. Notice that USB bus numbers start from one,
* which we may later use to identify "all" with zero.
@@ -322,6 +339,9 @@ struct mon_bus *mon_bus_lookup(unsigned int num)
struct list_head *p;
struct mon_bus *mbus;
+ if (num == 0) {
+ return &mon_bus0;
+ }
list_for_each (p, &mon_buses) {
mbus = list_entry(p, struct mon_bus, bus_link);
if (mbus->u_bus->busnum == num) {
@@ -341,6 +361,8 @@ static int __init mon_init(void)
if ((rc = mon_bin_init()) != 0)
goto err_bin;
+ mon_bus0_init();
+
if (usb_mon_register(&mon_ops_0) != 0) {
printk(KERN_NOTICE TAG ": unable to register with the core\n");
rc = -ENODEV;
@@ -374,6 +396,7 @@ static void __exit mon_exit(void)
usb_mon_deregister();
mutex_lock(&mon_lock);
+
while (!list_empty(&mon_buses)) {
p = mon_buses.next;
mbus = list_entry(p, struct mon_bus, bus_link);
@@ -397,6 +420,11 @@ static void __exit mon_exit(void)
mon_dissolve(mbus, mbus->u_bus);
kref_put(&mbus->ref, mon_bus_drop);
}
+
+ mbus = &mon_bus0;
+ if (mbus->text_inited)
+ mon_text_del(mbus);
+
mutex_unlock(&mon_lock);
mon_text_exit();
diff --git a/drivers/usb/mon/mon_text.c b/drivers/usb/mon/mon_text.c
index bbf54396e5f2..ec0cc51e39ac 100644
--- a/drivers/usb/mon/mon_text.c
+++ b/drivers/usb/mon/mon_text.c
@@ -124,8 +124,10 @@ static inline char mon_text_get_setup(struct mon_event_text *ep,
if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
return '-';
- if (mbus->uses_dma && (urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
+ }
if (urb->setup_packet == NULL)
return 'Z'; /* '0' would be not as pretty. */
@@ -160,8 +162,10 @@ static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
* contain non-NULL garbage in case the upper level promised to
* set DMA for the HCD.
*/
- if (mbus->uses_dma && (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
return mon_dmapeek(ep->data, urb->transfer_dma, len);
+ }
if (urb->transfer_buffer == NULL)
return 'Z'; /* '0' would be not as pretty. */
@@ -201,7 +205,7 @@ static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
ep->type = ev_type;
ep->pipe = urb->pipe;
ep->id = (unsigned long) urb;
- ep->busnum = rp->r.m_bus->u_bus->busnum;
+ ep->busnum = urb->dev->bus->busnum;
ep->tstamp = stamp;
ep->length = (ev_type == 'S') ?
urb->transfer_buffer_length : urb->actual_length;
@@ -305,13 +309,11 @@ static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
static int mon_text_open(struct inode *inode, struct file *file)
{
struct mon_bus *mbus;
- struct usb_bus *ubus;
struct mon_reader_text *rp;
int rc;
mutex_lock(&mon_lock);
mbus = inode->i_private;
- ubus = mbus->u_bus;
rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
if (rp == NULL) {
@@ -335,8 +337,7 @@ static int mon_text_open(struct inode *inode, struct file *file)
rp->r.rnf_error = mon_text_error;
rp->r.rnf_complete = mon_text_complete;
- snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
- (long)rp);
+ snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
rp->e_slab = kmem_cache_create(rp->slab_name,
sizeof(struct mon_event_text), sizeof(long), 0,
mon_text_ctor, NULL);
@@ -654,14 +655,14 @@ static const struct file_operations mon_fops_text_u = {
.release = mon_text_release,
};
-int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
+int mon_text_add(struct mon_bus *mbus, int busnum)
{
struct dentry *d;
enum { NAMESZ = 10 };
char name[NAMESZ];
int rc;
- rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
+ rc = snprintf(name, NAMESZ, "%dt", busnum);
if (rc <= 0 || rc >= NAMESZ)
goto err_print_t;
d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_t);
@@ -669,7 +670,7 @@ int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
goto err_create_t;
mbus->dent_t = d;
- rc = snprintf(name, NAMESZ, "%du", ubus->busnum);
+ rc = snprintf(name, NAMESZ, "%du", busnum);
if (rc <= 0 || rc >= NAMESZ)
goto err_print_u;
d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
@@ -677,7 +678,7 @@ int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
goto err_create_u;
mbus->dent_u = d;
- rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
+ rc = snprintf(name, NAMESZ, "%ds", busnum);
if (rc <= 0 || rc >= NAMESZ)
goto err_print_s;
d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
diff --git a/drivers/usb/mon/usb_mon.h b/drivers/usb/mon/usb_mon.h
index a1e2b8e9d777..13d63255283e 100644
--- a/drivers/usb/mon/usb_mon.h
+++ b/drivers/usb/mon/usb_mon.h
@@ -23,7 +23,6 @@ struct mon_bus {
struct dentry *dent_s; /* Debugging file */
struct dentry *dent_t; /* Text interface file */
struct dentry *dent_u; /* Second text interface file */
- int uses_dma;
/* Ref */
int nreaders; /* Under mon_lock AND mbus->lock */
@@ -53,7 +52,7 @@ void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r);
struct mon_bus *mon_bus_lookup(unsigned int num);
-int /*bool*/ mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus);
+int /*bool*/ mon_text_add(struct mon_bus *mbus, int busnum);
void mon_text_del(struct mon_bus *mbus);
// void mon_bin_add(struct mon_bus *);
@@ -82,4 +81,6 @@ extern struct mutex mon_lock;
extern const struct file_operations mon_fops_stat;
+extern struct mon_bus mon_bus0; /* Only for redundant checks */
+
#endif /* __USB_MON_H */
]
|
| 87/4 |
2026/09/28 05:55 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 28229
output: 36
thoughts: 48
Thoughts: Okay, let's dive into this. Here's how I'd approach this task, piecing together the information and thinking through the logical steps:
**Investigating the Origins and Evolution of USB Monitor Design**
Alright, I'm going to start with a deep dive into the historical context of the USB monitor implementation. My first move is to go way back to commit `1da177e4c3f4`. That's the likely genesis of this, or at least a significant early iteration. Understanding the initial design choices is crucial; it sets the foundation for everything that followed. I need to understand the intent of the original creators. What were they thinking? What trade-offs did they make? Why? I need to get into their heads, essentially.
Next, and this is where the real value comes, I'll use git to trace the evolution of the `usbmon` code. I'm going to search for instances of the word "monitored" across all the commits. I'm hoping to uncover past discussions, bug fixes, performance improvements, and any refactoring that has touched the monitoring system. The goal here is to get a picture of *why* changes were made. Knowing this context will help me understand what's good, bad, and ugly, and determine what areas are still open to improvement. I'll be looking for clues in commit messages, review comments, and associated mailing list discussions, wherever I can find them.
|
| 88/4 |
2026/09/28 05:55 |
tool |
git-log |
0m
Args: map[CodeRegexp:monitored Count:10 PathPrefix:drivers/usb/core]
Results: map[Output:27729aadd31d USB: make hcd.h public (drivers dependency)
1da177e4c3f4 Linux-2.6.12-rc2
]
|
| 89/4 |
2026/09/28 05:55 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 28679
output: 24
thoughts: 56
Thoughts: Okay, let's dive into this.
**Examining Commit History and Documentation for Lockless Fastpath Implementation**
Alright, I'm thinking I need to refresh my memory on the specifics of those commits, 1da177e4c3f4 and 27729aadd31d. Gotta see how they address this lockless fastpath situation. I'll need to understand the architectural implications, what changes were made, and why. Then, I need to check the USBmon documentation. I suspect there might be some insightful architectural notes related to the implementation, especially since it's about lockless fastpath. I'm hoping to find details that explain the design decisions, the trade-offs, and the rationale behind the chosen approach. This information could be critical to understanding any edge cases or performance considerations related to the fastpath logic.
|
| 90/4 |
2026/09/28 05:55 |
tool |
grepper |
0m
Args: map[Expression:monitored PathPrefix:Documentation]
Results: map[Output:
Full output is too long, showing 500 out of 998 lines.
Use more precise expression if possible.
Documentation/ABI/testing/debugfs-driver-qat_telemetry=140=Description: (RW) Selects up to 4 Ring Pairs (RP) to monitor, one per file,
--
Documentation/ABI/testing/debugfs-driver-qat_telemetry-145- * 0 to ``<num_rps - 1>``:
Documentation/ABI/testing/debugfs-driver-qat_telemetry:146: Ring pair to be monitored. The value of ``num_rps`` can be
Documentation/ABI/testing/debugfs-driver-qat_telemetry-147- retrieved through ``/sys/bus/pci/devices/<BDF>/qat/num_rps``.
--
Documentation/ABI/testing/sysfs-bus-event_source-devices-events=15=Description: Generic performance monitoring events
--
Documentation/ABI/testing/sysfs-bus-event_source-devices-events-17- A collection of performance monitoring events that may be
Documentation/ABI/testing/sysfs-bus-event_source-devices-events:18: supported by many/most CPUs. These events can be monitored
Documentation/ABI/testing/sysfs-bus-event_source-devices-events-19- using the 'perf(1)' tool.
--
Documentation/ABI/testing/sysfs-class-platform-profile=36=Description: Reading this file gives the current selected profile for this
--
Documentation/ABI/testing/sysfs-class-platform-profile-39-
Documentation/ABI/testing/sysfs-class-platform-profile:40: This file can be monitored for changes by polling for POLLPRI,
Documentation/ABI/testing/sysfs-class-platform-profile-41- POLLPRI will be signaled on any changes, independent of those
--
Documentation/ABI/testing/sysfs-class-powercap=31=Description:
Documentation/ABI/testing/sysfs-class-powercap-32- A power zone is a single or a collection of devices, which can
Documentation/ABI/testing/sysfs-class-powercap:33: be independently monitored and controlled. A power zone sysfs
Documentation/ABI/testing/sysfs-class-powercap-34- entry is qualified with the name of the <control type>.
--
Documentation/ABI/testing/sysfs-devices-edac=49=Description: This attribute file displays the total count of correctable
--
Documentation/ABI/testing/sysfs-devices-edac-52- indications that a DIMM is beginning to fail. This count
Documentation/ABI/testing/sysfs-devices-edac:53: field should be monitored for non-zero values and report
Documentation/ABI/testing/sysfs-devices-edac-54- such information to the system administrator.
--
Documentation/ABI/testing/sysfs-devices-edac=59=Description: This attribute file displays the number of CEs that
--
Documentation/ABI/testing/sysfs-devices-edac-63- to indicate which slot the failing memory is in. This count
Documentation/ABI/testing/sysfs-devices-edac:64: field should be also be monitored for non-zero values.
Documentation/ABI/testing/sysfs-devices-edac-65-
--
Documentation/ABI/testing/sysfs-devices-edac=145=Description: This attribute file displays the total count of correctable
--
Documentation/ABI/testing/sysfs-devices-edac-147- to examine. CEs provide early indications that a DIMM is beginning
Documentation/ABI/testing/sysfs-devices-edac:148: to fail. This count field should be monitored for non-zero values
Documentation/ABI/testing/sysfs-devices-edac-149- and report such information to the system administrator.
--
Documentation/ABI/testing/sysfs-devices-mapping=4=Description:
--
Documentation/ABI/testing/sysfs-devices-mapping-6- each dieX file (where X is die number) holds "Segment:Root Bus"
Documentation/ABI/testing/sysfs-devices-mapping:7: for PCIe root port, which can be monitored by that IIO PMON
Documentation/ABI/testing/sysfs-devices-mapping-8- block.
--
Documentation/ABI/testing/sysfs-devices-mapping=39=Description:
--
Documentation/ABI/testing/sysfs-devices-mapping-41- value that means UPI link number X on die Y is connected to UPI
Documentation/ABI/testing/sysfs-devices-mapping:42: link Z on die W and this link between sockets can be monitored
Documentation/ABI/testing/sysfs-devices-mapping-43- by UPI PMON block.
--
Documentation/ABI/testing/sysfs-platform-mellanox-pmc=19=Description:
Documentation/ABI/testing/sysfs-platform-mellanox-pmc:20: Event monitored by corresponding counter. This is used to
Documentation/ABI/testing/sysfs-platform-mellanox-pmc-21- program or read back the event that should be or is currently
Documentation/ABI/testing/sysfs-platform-mellanox-pmc:22: being monitored by counter<N>.
Documentation/ABI/testing/sysfs-platform-mellanox-pmc-23-
--
Documentation/ABI/testing/sysfs-platform-mellanox-pmc=28=Description:
Documentation/ABI/testing/sysfs-platform-mellanox-pmc:29: Counter value of the event being monitored. This is used to
Documentation/ABI/testing/sysfs-platform-mellanox-pmc-30- read the counter value of the event which was programmed using
--
Documentation/ABI/testing/sysfs-platform_profile=26=Description: Reading this file gives the current selected profile for this
--
Documentation/ABI/testing/sysfs-platform_profile-29-
Documentation/ABI/testing/sysfs-platform_profile:30: This file can be monitored for changes by polling for POLLPRI,
Documentation/ABI/testing/sysfs-platform_profile-31- POLLPRI will be signalled on any changes, independent of those
--
Documentation/accounting/psi.rst=77=To register a trigger user has to open psi interface file under
Documentation/accounting/psi.rst:78:/proc/pressure/ representing the resource to be monitored and write the
Documentation/accounting/psi.rst-79-desired threshold and time window. The open file descriptor should be
--
Documentation/accounting/psi.rst=95=with an already existing psi trigger will fail with EBUSY.
Documentation/accounting/psi.rst-96-
Documentation/accounting/psi.rst:97:Monitors activate only when system enters stall state for the monitored
Documentation/accounting/psi.rst-98-psi metric and deactivates upon exit from the stall state. While system is
Documentation/accounting/psi.rst:99:in the stall state psi signal growth is monitored at a rate of 10 times per
Documentation/accounting/psi.rst-100-tracking window.
--
Documentation/admin-guide/RAS/main.rst=480=this ``X`` memory module:
--
Documentation/admin-guide/RAS/main.rst-499- DIMM is beginning to fail. This count field should be
Documentation/admin-guide/RAS/main.rst:500: monitored for non-zero values and report such information
Documentation/admin-guide/RAS/main.rst-501- to the system administrator.
--
Documentation/admin-guide/RAS/main.rst=813=In this directory the default attributes are:
--
Documentation/admin-guide/RAS/main.rst-816- ce_count which is counter of CE events for this ``block``
Documentation/admin-guide/RAS/main.rst:817: of hardware being monitored
Documentation/admin-guide/RAS/main.rst-818- ue_count which is counter of UE events for this ``block``
Documentation/admin-guide/RAS/main.rst:819: of hardware being monitored
Documentation/admin-guide/RAS/main.rst-820- ============== ================================================
--
Documentation/admin-guide/mm/damon/start.rst=67=started process. The third command retrieves the current snapshot of the
Documentation/admin-guide/mm/damon/start.rst:68:monitored access pattern of the process from DAMON and shows the pattern in a
Documentation/admin-guide/mm/damon/start.rst-69-human readable format.
--
Documentation/admin-guide/mm/damon/start.rst=72=the regions in a single row hetmap format. Each column on the heatmap
Documentation/admin-guide/mm/damon/start.rst:73:represents regions of same size on the monitored virtual address space. The
Documentation/admin-guide/mm/damon/start.rst-74-position of the colun on the row and the number on the column represents the
--
Documentation/admin-guide/mm/userfaultfd.rst=620=Non-cooperative userfaultfd
--
Documentation/admin-guide/mm/userfaultfd.rst-622-
Documentation/admin-guide/mm/userfaultfd.rst:623:When the ``userfaultfd`` is monitored by an external manager, the manager
Documentation/admin-guide/mm/userfaultfd.rst-624-must be able to track changes in the process virtual memory
--
Documentation/admin-guide/mm/userfaultfd.rst=656=are pretty similar, they quite differ in the action expected from the
Documentation/admin-guide/mm/userfaultfd.rst-657-``userfaultfd`` manager. In the former case, the virtual memory is
Documentation/admin-guide/mm/userfaultfd.rst:658:removed, but the area is not, the area remains monitored by the
Documentation/admin-guide/mm/userfaultfd.rst-659-``userfaultfd``, and if a page fault occurs in that area it will be
--
Documentation/admin-guide/mm/userfaultfd.rst=674=processing. To aid the synchronization, the ``UFFDIO_COPY`` ioctl will
Documentation/admin-guide/mm/userfaultfd.rst:675:return ``-ENOSPC`` when the monitored process exits at the time of
Documentation/admin-guide/mm/userfaultfd.rst-676-``UFFDIO_COPY``, and ``-ENOENT``, when the non-cooperative process has changed
--
Documentation/admin-guide/perf-security.rst=10=can impose a considerable risk of leaking sensitive data accessed by
Documentation/admin-guide/perf-security.rst:11:monitored processes. The data leakage is possible both in scenarios of
Documentation/admin-guide/perf-security.rst-12-direct usage of perf_events system call API [2]_ and over data files
--
Documentation/admin-guide/perf-security.rst=17=categories:
--
Documentation/admin-guide/perf-security.rst-31- (PMC) [8]_ and machine specific registers (MSR) [9]_ that provide
Documentation/admin-guide/perf-security.rst:32: execution metrics for various monitored parts of the system (e.g.,
Documentation/admin-guide/perf-security.rst-33- memory controller (IMC), interconnect (QPI/UPI) or peripheral (PCIe)
--
Documentation/admin-guide/perf-security.rst=97=effectively enable capturing of additional data required for later
Documentation/admin-guide/perf-security.rst:98:performance analysis of monitored processes or a system. For example,
Documentation/admin-guide/perf-security.rst-99-CAP_SYSLOG capability permits reading kernel space memory addresses from
--
Documentation/admin-guide/perf-security.rst=227=is governed by perf_event_paranoid [2]_ setting:
--
Documentation/admin-guide/perf-security.rst-233- performance data. This is the least secure mode since allowed
Documentation/admin-guide/perf-security.rst:234: monitored *scope* is maximized and no perf_events specific limits
Documentation/admin-guide/perf-security.rst-235- are imposed on *resources* allocated for performance monitoring.
--
Documentation/admin-guide/perf-security.rst-240- monitoring. CPU and system events happened when executing either in
Documentation/admin-guide/perf-security.rst:241: user or in kernel space can be monitored and captured for later
Documentation/admin-guide/perf-security.rst-242- analysis. Per-user per-cpu perf_event_mlock_kb locking limit is
--
Documentation/admin-guide/perf-security.rst-249- happened when executing either in user or in kernel space can be
Documentation/admin-guide/perf-security.rst:250: monitored and captured for later analysis. Per-user per-cpu
Documentation/admin-guide/perf-security.rst-251- perf_event_mlock_kb locking limit is imposed but ignored for
--
Documentation/admin-guide/perf-security.rst-256- system events happened when executing in user space only can be
Documentation/admin-guide/perf-security.rst:257: monitored and captured for later analysis. Per-user per-cpu
Documentation/admin-guide/perf-security.rst-258- perf_event_mlock_kb locking limit is imposed but ignored for
--
Documentation/admin-guide/perf-security.rst=275=sampling session (perf record) requires an amount of open perf_event
Documentation/admin-guide/perf-security.rst:276:file descriptors that is not less than the number of monitored events
Documentation/admin-guide/perf-security.rst:277:multiplied by the number of monitored CPUs.
Documentation/admin-guide/perf-security.rst-278-
--
Documentation/admin-guide/perf-security.rst=287=RLIMIT_MEMLOCK [11]_ limit, but only for memory regions mapped
Documentation/admin-guide/perf-security.rst:288:specifically for capturing monitored performance events and related data.
Documentation/admin-guide/perf-security.rst-289-
--
Documentation/admin-guide/perf/hisi-pcie-pmu.rst=30=The "bus" sysfs file allows users to get the bus number of Root Ports
Documentation/admin-guide/perf/hisi-pcie-pmu.rst:31:monitored by PMU. Furthermore users can get the Root Ports range in
Documentation/admin-guide/perf/hisi-pcie-pmu.rst-32-[bdf_min, bdf_max] from "bdf_min" and "bdf_max" sysfs attributes
--
Documentation/admin-guide/perf/hisi-pcie-pmu.rst=61=Filter options
--
Documentation/admin-guide/perf/hisi-pcie-pmu.rst-84- lanes), bit8 is set, port=0x100; if these two Root Ports are both
Documentation/admin-guide/perf/hisi-pcie-pmu.rst:85: monitored, port=0x101.
Documentation/admin-guide/perf/hisi-pcie-pmu.rst-86-
--
Documentation/admin-guide/perf/mrvl-pem-pmu.rst=11=Also, inbound and outbound memory read requests along with their
Documentation/admin-guide/perf/mrvl-pem-pmu.rst:12:latencies can also be monitored. Address Translation Services(ATS)events
Documentation/admin-guide/perf/mrvl-pem-pmu.rst-13-such as ATS Translation, ATS Page Request, ATS Invalidation along with
--
Documentation/admin-guide/perf/nvidia-tegra410-pmu.rst=155=The PMU events can be filtered based on the traffic source and destination.
Documentation/admin-guide/perf/nvidia-tegra410-pmu.rst:156:The source filter indicates the PCIE devices that will be monitored. The
Documentation/admin-guide/perf/nvidia-tegra410-pmu.rst-157-destination filter specifies the destination memory type, e.g. local system
--
Documentation/admin-guide/perf/nvidia-tegra410-pmu.rst=163=The list of event filters:
--
Documentation/admin-guide/perf/nvidia-tegra410-pmu.rst-166-
Documentation/admin-guide/perf/nvidia-tegra410-pmu.rst:167: * src_rp_mask: bitmask of root ports that will be monitored. Each bit in this
Documentation/admin-guide/perf/nvidia-tegra410-pmu.rst-168- bitmask represents the RP index in the RC. If the bit is set, all devices under
Documentation/admin-guide/perf/nvidia-tegra410-pmu.rst:169: the associated RP will be monitored. E.g "src_rp_mask=0xF" will monitor
Documentation/admin-guide/perf/nvidia-tegra410-pmu.rst-170- devices in root port 0 to 3.
Documentation/admin-guide/perf/nvidia-tegra410-pmu.rst:171: * src_bdf: the BDF that will be monitored. This is a 16-bit value that
Documentation/admin-guide/perf/nvidia-tegra410-pmu.rst-172- follows formula: (bus << 8) + (device << 3) + (function). For example, the
--
Documentation/admin-guide/sysctl/fs.rst=329=max_user_watches
--
Documentation/admin-guide/sysctl/fs.rst-331-
Documentation/admin-guide/sysctl/fs.rst:332:Every epoll file descriptor can store a number of files to be monitored
Documentation/admin-guide/sysctl/fs.rst:333:for event readiness. Each one of these monitored files constitutes a "watch".
Documentation/admin-guide/sysctl/fs.rst-334-This configuration option sets the maximum number of "watches" that are
--
Documentation/arch/arm64/mpam.rst=10=system components such as the caches or memory controllers that allow memory
Documentation/arch/arm64/mpam.rst:11:traffic to be labelled, partitioned and monitored.
Documentation/arch/arm64/mpam.rst-12-
--
Documentation/arch/powerpc/imc.rst=99=information. The CPMCxSEL and CPMC_LOAD in the trace SCOM, specifies the event
Documentation/arch/powerpc/imc.rst:100:to be monitored and the sampling duration. On each overflow in the CPMCxSEL,
Documentation/arch/powerpc/imc.rst-101-hardware snapshots the program counter along with event counts and writes into
--
Documentation/arch/powerpc/imc.rst=150=buffer reaches the end.
Documentation/arch/powerpc/imc.rst-151-
Documentation/arch/powerpc/imc.rst:152:*Currently the event monitored for trace-mode is fixed as cycle.*
Documentation/arch/powerpc/imc.rst-153-
--
Documentation/arch/riscv/zicfilp.rst=74=the program.
--
Documentation/arch/riscv/zicfilp.rst-78-
Documentation/arch/riscv/zicfilp.rst:79:Per-task indirect branch tracking state can be monitored and
Documentation/arch/riscv/zicfilp.rst-80-controlled via the :c:macro:`PR_GET_CFI` and :c:macro:`PR_SET_CFI`
--
Documentation/arch/sparc/oradax/oracle-dax.rst=46=processor until the completion status has been updated by the
Documentation/arch/sparc/oradax/oracle-dax.rst:47:coprocessor. This is done using the monitored load and mwait
Documentation/arch/sparc/oradax/oracle-dax.rst-48-instructions, which are described in more detail later. The DAX
--
Documentation/arch/sparc/oradax/oracle-dax.rst=167=new M7/M8 processor capabilities to efficiently poll this status byte.
Documentation/arch/sparc/oradax/oracle-dax.rst:168:First, a "monitored load" is achieved via a Load from Alternate Space
Documentation/arch/sparc/oradax/oracle-dax.rst-169-(ldxa, lduba, etc.) with ASI 0x84 (ASI_MONITOR_PRIMARY). Second, a
Documentation/arch/sparc/oradax/oracle-dax.rst:170:"monitored wait" is achieved via the mwait instruction (a write to
Documentation/arch/sparc/oradax/oracle-dax.rst-171-%asr28). This instruction is like pause in that it suspends execution
--
Documentation/arch/sparc/oradax/oracle-dax.rst=173=addition will terminate early when one of several events occur. If the
Documentation/arch/sparc/oradax/oracle-dax.rst:174:block of data containing the monitored location is modified, then the
Documentation/arch/sparc/oradax/oracle-dax.rst-175-mwait terminates. This causes software to resume execution immediately
--
Documentation/arch/sparc/oradax/oracle-dax.rst=181=Application Life Cycle of a DAX Submission
--
Documentation/arch/sparc/oradax/oracle-dax.rst-187- - submit CCB via write() or pwrite()
Documentation/arch/sparc/oradax/oracle-dax.rst:188: - go into a loop executing monitored load + monitored wait and
Documentation/arch/sparc/oradax/oracle-dax.rst-189- terminate when the command status indicates the request is complete
--
Documentation/core-api/printk-index.rst=21=is not always trivial. Various changes might be backported. Various kernel
Documentation/core-api/printk-index.rst:22:versions might be used on different monitored systems.
Documentation/core-api/printk-index.rst-23-
--
Documentation/devicetree/bindings/devfreq/nvidia,tegra30-actmon.yaml=14=description: |
--
Documentation/devicetree/bindings/devfreq/nvidia,tegra30-actmon.yaml-17- which the external memory needs to be clocked in order to serve all requests
Documentation/devicetree/bindings/devfreq/nvidia,tegra30-actmon.yaml:18: from the monitored clients.
Documentation/devicetree/bindings/devfreq/nvidia,tegra30-actmon.yaml-19-
--
Documentation/devicetree/bindings/embedded-controller/microsoft,surface-rt-ec.yaml=20=properties:
--
Documentation/devicetree/bindings/embedded-controller/microsoft,surface-rt-ec.yaml-32-
Documentation/devicetree/bindings/embedded-controller/microsoft,surface-rt-ec.yaml:33: monitored-battery: true
Documentation/devicetree/bindings/embedded-controller/microsoft,surface-rt-ec.yaml-34-
--
Documentation/devicetree/bindings/embedded-controller/microsoft,surface-rt-ec.yaml=43=examples:
--
Documentation/devicetree/bindings/embedded-controller/microsoft,surface-rt-ec.yaml-59- enable-gpios = <&gpio 88 GPIO_ACTIVE_HIGH>;
Documentation/devicetree/bindings/embedded-controller/microsoft,surface-rt-ec.yaml:60: monitored-battery = <&battery>;
Documentation/devicetree/bindings/embedded-controller/microsoft,surface-rt-ec.yaml-61- };
--
Documentation/devicetree/bindings/hwmon/adi,ltc2992.yaml=32=patternProperties:
--
Documentation/devicetree/bindings/hwmon/adi,ltc2992.yaml-36- description: |
Documentation/devicetree/bindings/hwmon/adi,ltc2992.yaml:37: Represents the two supplies to be monitored.
Documentation/devicetree/bindings/hwmon/adi,ltc2992.yaml-38-
--
Documentation/devicetree/bindings/hwmon/adi,ltc4283.yaml=19=properties:
--
Documentation/devicetree/bindings/hwmon/adi,ltc4283.yaml-93- description:
Documentation/devicetree/bindings/hwmon/adi,ltc4283.yaml:94: If set, enables the attenuated MOSFET drain voltage to be monitored. This
Documentation/devicetree/bindings/hwmon/adi,ltc4283.yaml:95: effectively means that the MOSFET power is monitored. If not set, the
Documentation/devicetree/bindings/hwmon/adi,ltc4283.yaml:96: attenuated input voltage (and hence input power) is monitored.
Documentation/devicetree/bindings/hwmon/adi,ltc4283.yaml-97- type: boolean
--
Documentation/devicetree/bindings/hwmon/vexpress.txt=15=Optional node properties:
Documentation/devicetree/bindings/hwmon/vexpress.txt:16:- label : string describing the monitored value
Documentation/devicetree/bindings/hwmon/vexpress.txt-17-
--
Documentation/devicetree/bindings/iio/adc/adi,max14001.yaml=23=properties:
--
Documentation/devicetree/bindings/iio/adc/adi,max14001.yaml-57- fault: when fault reporting is enabled, the FAULT pin is asserted
Documentation/devicetree/bindings/iio/adc/adi,max14001.yaml:58: low whenever one of the monitored fault conditions occurs.
Documentation/devicetree/bindings/iio/adc/adi,max14001.yaml-59-
--
Documentation/devicetree/bindings/iio/adc/maxim,max34408.yaml=12=description: |
Documentation/devicetree/bindings/iio/adc/maxim,max34408.yaml-13- The MAX34408/MAX34409 are two- and four-channel current monitors that are
Documentation/devicetree/bindings/iio/adc/maxim,max34408.yaml:14: configured and monitored with a standard I2C/SMBus serial interface. Each
Documentation/devicetree/bindings/iio/adc/maxim,max34408.yaml-15- unidirectional current sensor offers precision high-side operation with a
--
Documentation/devicetree/bindings/iio/dac/adi,ad5592r.yaml=71=patternProperties:
--
Documentation/devicetree/bindings/iio/dac/adi,ad5592r.yaml-91- * CH_MODE_DAC (the pin is DAC output)
Documentation/devicetree/bindings/iio/dac/adi,ad5592r.yaml:92: * CH_MODE_DAC_AND_ADC (the pin is DAC output but can be monitored
Documentation/devicetree/bindings/iio/dac/adi,ad5592r.yaml-93- by an ADC, since there is no disadvantage this should be
--
Documentation/devicetree/bindings/leds/common.yaml=24=properties:
--
Documentation/devicetree/bindings/leds/common.yaml-215- related source device(s).
Documentation/devicetree/bindings/leds/common.yaml:216: Another example is a GPIO line that will be monitored and mirror the
Documentation/devicetree/bindings/leds/common.yaml-217- state of the line (with or without inversion flags) to the LED.
--
Documentation/devicetree/bindings/mfd/ene-kb930.yaml=35=examples:
--
Documentation/devicetree/bindings/mfd/ene-kb930.yaml-59-
Documentation/devicetree/bindings/mfd/ene-kb930.yaml:60: monitored-battery = <&battery>;
Documentation/devicetree/bindings/mfd/ene-kb930.yaml-61- power-supplies = <&mains>;
--
Documentation/devicetree/bindings/mfd/nxp,pf1550.yaml=19=properties:
--
Documentation/devicetree/bindings/mfd/nxp,pf1550.yaml-43-
Documentation/devicetree/bindings/mfd/nxp,pf1550.yaml:44: monitored-battery:
Documentation/devicetree/bindings/mfd/nxp,pf1550.yaml-45- description: |
Documentation/devicetree/bindings/mfd/nxp,pf1550.yaml:46: A phandle to a monitored battery node that contains a valid value
Documentation/devicetree/bindings/mfd/nxp,pf1550.yaml-47- for:
--
Documentation/devicetree/bindings/mfd/nxp,pf1550.yaml=74=examples:
--
Documentation/devicetree/bindings/mfd/nxp,pf1550.yaml-94- wakeup-source;
Documentation/devicetree/bindings/mfd/nxp,pf1550.yaml:95: monitored-battery = <&battery>;
Documentation/devicetree/bindings/mfd/nxp,pf1550.yaml-96- nxp,min-system-microvolt = <4300000>;
--
Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml=53=examples:
--
Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml-132- compatible = "richtek,rt5033-charger";
Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml:133: monitored-battery = <&battery>;
Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml-134- richtek,usb-connector = <&usb_con>;
--
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml=18=properties:
--
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml-121- properties:
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml:122: monitored-battery:
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml-123- description: |
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml:124: A phandle to a monitored battery node that contains a valid
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml-125- value for:
--
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml-151- required:
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml:152: - monitored-battery
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml-153- - rockchip,resistor-sense-micro-ohms
--
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml=200=examples:
--
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml-382- rk817_charger: charger {
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml:383: monitored-battery = <&battery>;
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml-384- rockchip,resistor-sense-micro-ohms = <10000>;
--
Documentation/devicetree/bindings/mfd/rohm,bd72720-pmic.yaml=12=description:
--
Documentation/devicetree/bindings/mfd/rohm,bd72720-pmic.yaml-46-#
Documentation/devicetree/bindings/mfd/rohm,bd72720-pmic.yaml:47:# monitored-battery = <&battery>;
Documentation/devicetree/bindings/mfd/rohm,bd72720-pmic.yaml-48-
--
Documentation/devicetree/bindings/mfd/samsung,s2mu005-pmic.yaml=61=examples:
--
Documentation/devicetree/bindings/mfd/samsung,s2mu005-pmic.yaml-75-
Documentation/devicetree/bindings/mfd/samsung,s2mu005-pmic.yaml:76: monitored-battery = <&battery>;
Documentation/devicetree/bindings/mfd/samsung,s2mu005-pmic.yaml-77-
--
Documentation/devicetree/bindings/mfd/sprd,sc2731.yaml=106=examples:
--
Documentation/devicetree/bindings/mfd/sprd,sc2731.yaml-130- phys = <&ssphy>;
Documentation/devicetree/bindings/mfd/sprd,sc2731.yaml:131: monitored-battery = <&bat>;
Documentation/devicetree/bindings/mfd/sprd,sc2731.yaml-132- };
--
Documentation/devicetree/bindings/mfd/sprd,sc2731.yaml-216- nvmem-cell-names = "fgu_calib";
Documentation/devicetree/bindings/mfd/sprd,sc2731.yaml:217: monitored-battery = <&bat>;
Documentation/devicetree/bindings/mfd/sprd,sc2731.yaml-218- sprd,calib-resistance-micro-ohms = <21500>;
--
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml=12=properties:
--
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml-30-
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml:31: monitored-battery:
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml-32- description:
--
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml=132=required:
--
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml-135- - input-current-limit-microamp
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml:136: - monitored-battery
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml-137- - power-supplies
--
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml=139=examples:
--
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml-153- interrupts = <RK_PD5 IRQ_TYPE_LEVEL_LOW>;
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml:154: monitored-battery = <&battery>;
Documentation/devicetree/bindings/mfd/ti,bq25703a.yaml-155- power-supplies = <&fusb302>;
--
Documentation/devicetree/bindings/mfd/ti,twl.yaml=457=examples:
--
Documentation/devicetree/bindings/mfd/ti,twl.yaml-475- io-channel-names = "vusb";
Documentation/devicetree/bindings/mfd/ti,twl.yaml:476: monitored-battery = <&bat>;
Documentation/devicetree/bindings/mfd/ti,twl.yaml-477- };
--
Documentation/devicetree/bindings/power/supply/adc-battery.yaml=26=properties:
--
Documentation/devicetree/bindings/power/supply/adc-battery.yaml-47-
Documentation/devicetree/bindings/power/supply/adc-battery.yaml:48: monitored-battery: true
Documentation/devicetree/bindings/power/supply/adc-battery.yaml-49-
Documentation/devicetree/bindings/power/supply/adc-battery.yaml=50=required:
--
Documentation/devicetree/bindings/power/supply/adc-battery.yaml-53- - io-channel-names
Documentation/devicetree/bindings/power/supply/adc-battery.yaml:54: - monitored-battery
Documentation/devicetree/bindings/power/supply/adc-battery.yaml-55-
--
Documentation/devicetree/bindings/power/supply/adc-battery.yaml=58=examples:
--
Documentation/devicetree/bindings/power/supply/adc-battery.yaml-68- power-supplies = <&charger>;
Documentation/devicetree/bindings/power/supply/adc-battery.yaml:69: monitored-battery = <&battery>;
Documentation/devicetree/bindings/power/supply/adc-battery.yaml-70- };
--
Documentation/devicetree/bindings/power/supply/battery.yaml=12=description: |
--
Documentation/devicetree/bindings/power/supply/battery.yaml-27- Batteries must be referenced by chargers and/or fuel-gauges using a phandle.
Documentation/devicetree/bindings/power/supply/battery.yaml:28: The phandle's property should be named "monitored-battery".
Documentation/devicetree/bindings/power/supply/battery.yaml-29-
--
Documentation/devicetree/bindings/power/supply/battery.yaml=173=examples:
--
Documentation/devicetree/bindings/power/supply/battery.yaml-207- reg = <0x11>;
Documentation/devicetree/bindings/power/supply/battery.yaml:208: monitored-battery = <&battery>;
Documentation/devicetree/bindings/power/supply/battery.yaml-209- };
--
Documentation/devicetree/bindings/power/supply/battery.yaml-212- reg = <0x22>;
Documentation/devicetree/bindings/power/supply/battery.yaml:213: monitored-battery = <&battery>;
Documentation/devicetree/bindings/power/supply/battery.yaml-214- };
--
Documentation/devicetree/bindings/power/supply/bq24190.yaml=16=properties:
--
Documentation/devicetree/bindings/power/supply/bq24190.yaml-55-
Documentation/devicetree/bindings/power/supply/bq24190.yaml:56: monitored-battery:
Documentation/devicetree/bindings/power/supply/bq24190.yaml-57- description: |
--
Documentation/devicetree/bindings/power/supply/bq24190.yaml=75=examples:
--
Documentation/devicetree/bindings/power/supply/bq24190.yaml-94- interrupts = <10 IRQ_TYPE_EDGE_FALLING>;
Documentation/devicetree/bindings/power/supply/bq24190.yaml:95: monitored-battery = <&bat>;
Documentation/devicetree/bindings/power/supply/bq24190.yaml-96- ti,system-minimum-microvolt = <3200000>;
--
Documentation/devicetree/bindings/power/supply/bq2515x.yaml=23=properties:
--
Documentation/devicetree/bindings/power/supply/bq2515x.yaml-55-
Documentation/devicetree/bindings/power/supply/bq2515x.yaml:56: monitored-battery: true
Documentation/devicetree/bindings/power/supply/bq2515x.yaml-57-
Documentation/devicetree/bindings/power/supply/bq2515x.yaml=58=required:
--
Documentation/devicetree/bindings/power/supply/bq2515x.yaml-60- - reg
Documentation/devicetree/bindings/power/supply/bq2515x.yaml:61: - monitored-battery
Documentation/devicetree/bindings/power/supply/bq2515x.yaml-62-
--
Documentation/devicetree/bindings/power/supply/bq2515x.yaml=68=examples:
--
Documentation/devicetree/bindings/power/supply/bq2515x.yaml-83- reg = <0x6b>;
Documentation/devicetree/bindings/power/supply/bq2515x.yaml:84: monitored-battery = <&bat>;
Documentation/devicetree/bindings/power/supply/bq2515x.yaml-85- input-current-limit-microamp = <100000>;
--
Documentation/devicetree/bindings/power/supply/bq256xx.yaml=27=properties:
--
Documentation/devicetree/bindings/power/supply/bq256xx.yaml-60-
Documentation/devicetree/bindings/power/supply/bq256xx.yaml:61: monitored-battery: true
Documentation/devicetree/bindings/power/supply/bq256xx.yaml-62-
--
Documentation/devicetree/bindings/power/supply/bq256xx.yaml=73=required:
--
Documentation/devicetree/bindings/power/supply/bq256xx.yaml-75- - reg
Documentation/devicetree/bindings/power/supply/bq256xx.yaml:76: - monitored-battery
Documentation/devicetree/bindings/power/supply/bq256xx.yaml-77-
--
Documentation/devicetree/bindings/power/supply/bq256xx.yaml=95=examples:
--
Documentation/devicetree/bindings/power/supply/bq256xx.yaml-115- reg = <0x6b>;
Documentation/devicetree/bindings/power/supply/bq256xx.yaml:116: monitored-battery = <&bat>;
Documentation/devicetree/bindings/power/supply/bq256xx.yaml-117-
--
Documentation/devicetree/bindings/power/supply/bq25980.yaml=22=properties:
--
Documentation/devicetree/bindings/power/supply/bq25980.yaml-75-
Documentation/devicetree/bindings/power/supply/bq25980.yaml:76: monitored-battery: true
Documentation/devicetree/bindings/power/supply/bq25980.yaml-77-
Documentation/devicetree/bindings/power/supply/bq25980.yaml=78=required:
--
Documentation/devicetree/bindings/power/supply/bq25980.yaml-80- - reg
Documentation/devicetree/bindings/power/supply/bq25980.yaml:81: - monitored-battery
Documentation/devicetree/bindings/power/supply/bq25980.yaml-82-
--
Documentation/devicetree/bindings/power/supply/bq25980.yaml=85=examples:
--
Documentation/devicetree/bindings/power/supply/bq25980.yaml-107- ti,sc-ovp-limit-microvolt = <17800000>;
Documentation/devicetree/bindings/power/supply/bq25980.yaml:108: monitored-battery = <&bat>;
Documentation/devicetree/bindings/power/supply/bq25980.yaml-109- };
--
Documentation/devicetree/bindings/power/supply/bq27xxx.yaml=19=properties:
--
Documentation/devicetree/bindings/power/supply/bq27xxx.yaml-61-
Documentation/devicetree/bindings/power/supply/bq27xxx.yaml:62: monitored-battery:
Documentation/devicetree/bindings/power/supply/bq27xxx.yaml-63- description: |
--
Documentation/devicetree/bindings/power/supply/bq27xxx.yaml=107=examples:
--
Documentation/devicetree/bindings/power/supply/bq27xxx.yaml-122- reg = <0x55>;
Documentation/devicetree/bindings/power/supply/bq27xxx.yaml:123: monitored-battery = <&bat>;
Documentation/devicetree/bindings/power/supply/bq27xxx.yaml-124- };
--
Documentation/devicetree/bindings/power/supply/cpcap-battery.yaml=22=properties:
--
Documentation/devicetree/bindings/power/supply/cpcap-battery.yaml-57-
Documentation/devicetree/bindings/power/supply/cpcap-battery.yaml:58: monitored-battery: true
Documentation/devicetree/bindings/power/supply/cpcap-battery.yaml-59- power-supplies: true
--
]
|
| 91/4 |
2026/09/28 05:55 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 40495
output: 34
thoughts: 13
Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience:
**Diving into USB Monitoring**
Okay, so I'm thinking I need to understand what's happening on the USB bus better. Where do I start? The most logical resource for in-depth USB insights within the kernel documentation is probably going to be the `Documentation` directory. Specifically, I'm going to look for information on monitoring tools, and that usually falls under the `usb` subdirectory.
Right, `usbmon.rst`. That sounds promising. This is where I'll find the kernel documentation for `usbmon`, which I'm hoping explains how to use the kernel's built-in USB monitoring capabilities. I'm expecting this will detail how to capture USB traffic, decode it, and provide the tools or methods for interpreting the data. This `usbmon` documentation should give me the low down. Hopefully, it'll cover the `debugfs` interface and any related tools like `wireshark` integration. Time to dig in!
|
| 92/4 |
2026/09/28 05:55 |
tool |
read-file |
0m
Args: map[File:Documentation/usb/usbmon.rst FirstLine:1 LineCount:80]
Results: map[Contents: 1: ======
2: usbmon
3: ======
4:
5: Introduction
6: ============
7:
8: The name "usbmon" in lowercase refers to a facility in kernel which is
9: used to collect traces of I/O on the USB bus. This function is analogous
10: to a packet socket used by network monitoring tools such as tcpdump(1)
11: or Ethereal. Similarly, it is expected that a tool such as usbdump or
12: USBMon (with uppercase letters) is used to examine raw traces produced
13: by usbmon.
14:
15: The usbmon reports requests made by peripheral-specific drivers to Host
16: Controller Drivers (HCD). So, if HCD is buggy, the traces reported by
17: usbmon may not correspond to bus transactions precisely. This is the same
18: situation as with tcpdump.
19:
20: Two APIs are currently implemented: "text" and "binary". The binary API
21: is available through a character device in /dev namespace and is an ABI.
22: The text API is deprecated since 2.6.35, but available for convenience.
23:
24: How to use usbmon to collect raw text traces
25: ============================================
26:
27: Unlike the packet socket, usbmon has an interface which provides traces
28: in a text format. This is used for two purposes. First, it serves as a
29: common trace exchange format for tools while more sophisticated formats
30: are finalized. Second, humans can read it in case tools are not available.
31:
32: To collect a raw text trace, execute following steps.
33:
34: 1. Prepare
35: ----------
36:
37: Mount debugfs (it has to be enabled in your kernel configuration), and
38: load the usbmon module (if built as module). The second step is skipped
39: if usbmon is built into the kernel::
40:
41: # mount -t debugfs none_debugs /sys/kernel/debug
42: # modprobe usbmon
43: #
44:
45: Verify that bus sockets are present::
46:
47: # ls /sys/kernel/debug/usb/usbmon
48: 0s 0u 1s 1t 1u 2s 2t 2u 3s 3t 3u 4s 4t 4u
49: #
50:
51: Now you can choose to either use the socket '0u' (to capture packets on all
52: buses), and skip to step #3, or find the bus used by your device with step #2.
53: This allows to filter away annoying devices that talk continuously.
54:
55: 2. Find which bus connects to the desired device
56: ------------------------------------------------
57:
58: Run "cat /sys/kernel/debug/usb/devices", and find the T-line which corresponds
59: to the device. Usually you do it by looking for the vendor string. If you have
60: many similar devices, unplug one and compare the two
61: /sys/kernel/debug/usb/devices outputs. The T-line will have a bus number.
62:
63: Example::
64:
65: T: Bus=03 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=12 MxCh= 0
66: D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
67: P: Vendor=0557 ProdID=2004 Rev= 1.00
68: S: Manufacturer=ATEN
69: S: Product=UC100KM V2.00
70:
71: "Bus=03" means it's bus 3. Alternatively, you can look at the output from
72: "lsusb" and get the bus number from the appropriate line. Example:
73:
74: Bus 003 Device 002: ID 0557:2004 ATEN UC100KM V2.00
75:
76: 3. Start 'cat'
77: --------------
78:
79: ::
80:
]
|
| 93/4 |
2026/09/28 05:55 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 41999
output: 36
thoughts: 18
Thoughts: Okay, let's dive into this USB monitoring documentation, `Documentation/usb/usbmon.rst`, and make sure we have a comprehensive understanding of it. I need to refresh my memory on the specifics of this tool, how it works, and its role in the debugging workflow. While I'm familiar with the general concepts of USB monitoring, a detailed review will help me identify any nuances, potential gotchas, or alternative approaches that might be worth considering, especially in the context of [*Insert user's specific field here, e.g., real-time embedded systems, low-power device drivers, etc.*]. I'm particularly interested in seeing if there's any new information or features added since my last deep dive, or maybe something I missed previously that could streamline my debugging efforts. It is always useful to know about what's available when doing an in-depth dive!
|
| 94/4 |
2026/09/28 05:55 |
tool |
read-file |
0m
Args: map[File:Documentation/usb/usbmon.rst FirstLine:81 LineCount:100]
Results: map[Contents: 81: # cat /sys/kernel/debug/usb/usbmon/3u > /tmp/1.mon.out
82:
83: to listen on a single bus, otherwise, to listen on all buses, type::
84:
85: # cat /sys/kernel/debug/usb/usbmon/0u > /tmp/1.mon.out
86:
87: This process will read until it is killed. Naturally, the output can be
88: redirected to a desirable location. This is preferred, because it is going
89: to be quite long.
90:
91: 4. Perform the desired operation on the USB bus
92: -----------------------------------------------
93:
94: This is where you do something that creates the traffic: plug in a flash key,
95: copy files, control a webcam, etc.
96:
97: 5. Kill cat
98: -----------
99:
100: Usually it's done with a keyboard interrupt (Control-C).
101:
102: At this point the output file (/tmp/1.mon.out in this example) can be saved,
103: sent by e-mail, or inspected with a text editor. In the last case make sure
104: that the file size is not excessive for your favourite editor.
105:
106: Raw text data format
107: ====================
108:
109: Two formats are supported currently: the original, or '1t' format, and
110: the '1u' format. The '1t' format is deprecated in kernel 2.6.21. The '1u'
111: format adds a few fields, such as ISO frame descriptors, interval, etc.
112: It produces slightly longer lines, but otherwise is a perfect superset
113: of '1t' format.
114:
115: If it is desired to recognize one from the other in a program, look at the
116: "address" word (see below), where '1u' format adds a bus number. If 2 colons
117: are present, it's the '1t' format, otherwise '1u'.
118:
119: Any text format data consists of a stream of events, such as URB submission,
120: URB callback, submission error. Every event is a text line, which consists
121: of whitespace separated words. The number or position of words may depend
122: on the event type, but there is a set of words, common for all types.
123:
124: Here is the list of words, from left to right:
125:
126: - URB Tag. This is used to identify URBs, and is normally an in-kernel address
127: of the URB structure in hexadecimal, but can be a sequence number or any
128: other unique string, within reason.
129:
130: - Timestamp in microseconds, a decimal number. The timestamp's resolution
131: depends on available clock, and so it can be much worse than a microsecond
132: (if the implementation uses jiffies, for example).
133:
134: - Event Type. This type refers to the format of the event, not URB type.
135: Available types are: S - submission, C - callback, E - submission error.
136:
137: - "Address" word (formerly a "pipe"). It consists of four fields, separated by
138: colons: URB type and direction, Bus number, Device address, Endpoint number.
139: Type and direction are encoded with two bytes in the following manner:
140:
141: == == =============================
142: Ci Co Control input and output
143: Zi Zo Isochronous input and output
144: Ii Io Interrupt input and output
145: Bi Bo Bulk input and output
146: == == =============================
147:
148: Bus number, Device address, and Endpoint are decimal numbers, but they may
149: have leading zeros, for the sake of human readers.
150:
151: - URB Status word. This is either a letter, or several numbers separated
152: by colons: URB status, interval, start frame, and error count. Unlike the
153: "address" word, all fields save the status are optional. Interval is printed
154: only for interrupt and isochronous URBs. Start frame is printed only for
155: isochronous URBs. Error count is printed only for isochronous callback
156: events.
157:
158: The status field is a decimal number, sometimes negative, which represents
159: a "status" field of the URB. This field makes no sense for submissions, but
160: is present anyway to help scripts with parsing. When an error occurs, the
161: field contains the error code.
162:
163: In case of a submission of a Control packet, this field contains a Setup Tag
164: instead of an group of numbers. It is easy to tell whether the Setup Tag is
165: present because it is never a number. Thus if scripts find a set of numbers
166: in this word, they proceed to read Data Length (except for isochronous URBs).
167: If they find something else, like a letter, they read the setup packet before
168: reading the Data Length or isochronous descriptors.
169:
170: - Setup packet, if present, consists of 5 words: one of each for bmRequestType,
171: bRequest, wValue, wIndex, wLength, as specified by the USB Specification 2.0.
172: These words are safe to decode if Setup Tag was 's'. Otherwise, the setup
173: packet was present, but not captured, and the fields contain filler.
174:
175: - Number of isochronous frame descriptors and descriptors themselves.
176: If an Isochronous transfer event has a set of descriptors, a total number
177: of them in an URB is printed first, then a word per descriptor, up to a
178: total of 5. The word consists of 3 colon-separated decimal numbers for
179: status, offset, and length respectively. For submissions, initial length
180: is reported. For callbacks, actual length is reported.
]
|
| 95/4 |
2026/09/28 05:55 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 44044
output: 37
thoughts: 70
Thoughts: Here's a breakdown of my thought process as I delve into the implementation details:
**Deconstructing `mon_reader_del()` and `usbmon_urb_submit()`**
Okay, so I'm diving into the nitty-gritty of the `mon_reader_del()` and `usbmon_urb_submit()` functions. I need to understand exactly how these are implemented. Where to begin? Right, let's start with the low-hanging fruit and trace the execution path. First, I need to understand what `usb_bus` is and where it's defined. And what is that `bus->monitored` flag? I need to check its context within the `struct usb_bus`.
|
| 96/4 |
2026/09/28 05:55 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:include/linux/usb/hcd.h Name:usb_bus]
Results: map[SourceCode: 447: /*
448: * Allocated per bus (tree of devices) we have:
449: */
450: struct usb_bus {
451: struct device *controller; /* host side hardware */
452: struct device *sysdev; /* as seen from firmware or bus */
453: int busnum; /* Bus number (in order of reg) */
454: const char *bus_name; /* stable id (PCI slot_name etc) */
455: u8 uses_pio_for_control; /*
456: * Does the host controller use PIO
457: * for control transfers?
458: */
459: u8 otg_port; /* 0, or number of OTG/HNP port */
460: unsigned is_b_host:1; /* true during some HNP roleswitches */
461: unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */
462: unsigned no_stop_on_short:1; /*
463: * Quirk: some controllers don't stop
464: * the ep queue on a short transfer
465: * with the URB_SHORT_NOT_OK flag set.
466: */
467: unsigned no_sg_constraint:1; /* no sg constraint */
468: unsigned sg_tablesize; /* 0 or largest number of sg list entries */
469:
470: int devnum_next; /* Next open device number in
471: * round-robin allocation */
472: struct mutex devnum_next_mutex; /* devnum_next mutex */
473:
474: DECLARE_BITMAP(devmap, 128); /* USB device number allocation bitmap */
475: struct usb_device *root_hub; /* Root hub */
476: struct usb_bus *hs_companion; /* Companion EHCI bus, if any */
477:
478: int bandwidth_allocated; /* on this bus: how much of the time
479: * reserved for periodic (intr/iso)
480: * requests is used, on average?
481: * Units: microseconds/frame.
482: * Limits: Full/low speed reserve 90%,
483: * while high speed reserves 80%.
484: */
485: int bandwidth_int_reqs; /* number of Interrupt requests */
486: int bandwidth_isoc_reqs; /* number of Isoc. requests */
487:
488: unsigned resuming_ports; /* bit array: resuming root-hub ports */
489:
490: #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
491: struct mon_bus *mon_bus; /* non-null when associated */
492: int monitored; /* non-zero when monitored */
493: #endif
494: };
SourceFile:include/linux/usb.h]
|
| 97/4 |
2026/09/28 05:55 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 45171
output: 47
thoughts: 2834
Thoughts: **USBmon Reader Deletion Concurrency Analysis**
Okay, so I've been tracing the interaction between `usbmon` and reader deletion, particularly the potential race conditions when a USB device is being monitored and a reader is being removed. My primary concern is ensuring that URB events aren't delivered to freed reader structures, and that monitoring is disabled consistently.
Here's my thought process: The core issue revolves around `usbmon_urb_submit` which checks `bus->monitored` and, if true, calls the `mon_ops->urb_submit` function. `bus->monitored` is toggled by `mon_reader_add()` and `mon_reader_del()`. The critical bit seems to be where `mon_reader_del()` decreases `mbus->nreaders` to 0, it calls `mon_stop()` which sets `ubus->monitored = 0`. So, I started with a deep dive into the code.
I walked through the code, looking at what happens during `mon_reader_del()`. I noticed that `mon_reader_del` acquires a spinlock, unlinks the reader from the `mbus->r_list`, decrements the reader count, and *then* calls `mon_stop` if the reader count becomes zero. `mon_stop` iterates all buses. It checks `mbus->nreaders` and then sets `ubus->monitored = 0`. This is the point of interest.
The real questions are: What happens if `usbmon_urb_submit` is called concurrently?
I've carefully considered two scenarios: (A) `bus->monitored` reads as false. This is safe, and the event is ignored, which is expected. (B) The more complicated scenario is when the read of `bus->monitored` is stale (1). In this case, `(*mon_ops->urb_submit)(bus, urb)` is executed. This calls `mon_submit`, which then calls `mon_bus_submit`.
Now, the important part: `mon_bus_submit` *also* grabs the same lock held by `mon_reader_del()`. This is crucial. I analyzed two sub-cases of locking:
* **Subcase B1**: `mon_bus_submit` acquires the lock *before* `mon_reader_del`. This means the reader is still on the list, the event will get delivered safely, and then the reader is unlinked and monitoring turned off. Perfect.
* **Subcase B2**: `mon_reader_del` acquires the lock first, unlinks the reader, and calls `mon_stop`. This means that if `usbmon_urb_submit` grabs the lock after the reader is unlinked, the loop in `mon_bus_submit` will simply not execute. There will be no reader on the list. `mon_bus_submit` returns immediately, after grabbing the lock, but the call will happen as `mon_bus_submit(&mon_bus0, urb)` which may call reader callbacks if registered.
My primary concern was around whether the `r` (reader struct) could be freed *before* `rnf_submit` completes. In Subcase B1, the `rnf_submit` executes *before* the reader is unlinked, ensuring the reader pointer remains valid during the callback. In subcase B2, even though `mon_reader_del` has already unlinked `r`, and thus `r` is no longer on the list, `mon_bus_submit` is holding the lock. And since `mon_reader_del` returns before `r` is freed, no memory is corrupted, since the delete is protected by the lock.
It looks like the `mbus->lock` protects the reader list from concurrent access and reader deletion. There are no race conditions.
|
| 98/4 |
2026/09/28 05:55 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:drivers/usb/mon/mon_main.c IncludeSnippetLines:10 Name:mon_reader_del]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mon_bin_release SourceFile:drivers/usb/mon/mon_bin.c SourceLine:795 SourceSnippet: 785: struct mon_reader_bin *rp = file->private_data;
786: struct mon_bus* mbus = rp->r.m_bus;
787:
788: mutex_lock(&mon_lock);
789:
790: if (mbus->nreaders <= 0) {
791: printk(KERN_ERR TAG ": consistency error on close\n");
792: mutex_unlock(&mon_lock);
793: return 0;
794: }
795: mon_reader_del(mbus, &rp->r);
796:
797: mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
798: kfree(rp->b_vec);
799: kfree(rp);
800:
801: mutex_unlock(&mon_lock);
802: return 0;
803: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mon_text_release SourceFile:drivers/usb/mon/mon_text.c SourceLine:658 SourceSnippet: 648: struct mon_event_text *ep;
649:
650: mutex_lock(&mon_lock);
651: mbus = inode->i_private;
652:
653: if (mbus->nreaders <= 0) {
654: printk(KERN_ERR TAG ": consistency error on close\n");
655: mutex_unlock(&mon_lock);
656: return 0;
657: }
658: mon_reader_del(mbus, &rp->r);
659:
660: /*
661: * In theory, e_list is protected by mbus->lock. However,
662: * after mon_reader_del has finished, the following is the case:
663: * - we are not on reader list anymore, so new events won't be added;
664: * - whole mbus may be dropped if it was orphaned.
665: * So, we better not touch mbus.
666: */
667: /* spin_lock_irqsave(&mbus->lock, flags); */
668: while (!list_empty(&rp->e_list)) {
]] TruncatedOutput:false]
|
| 99/4 |
2026/09/28 05:55 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 49635
output: 39
thoughts: 921
Thoughts: **Analyzing the Reader Deletion and Submission Logic in `usbmon`**
Okay, so I've been digging into this `usbmon` code, and I see a potential race condition in how readers are handled, specifically in the context of releasing them. In both `mon_bin_release` and `mon_text_release`, the function `mon_reader_del(mbus, &rp->r)` is called, which is protected by `mutex_lock(&mon_lock)`. Inside `mon_reader_del`, a `spin_lock_irqsave(&mbus->lock, flags)` is acquired, and the reader is unlinked from `mbus->r_list`. Crucially, this unlinking happens *before* `mon_bin_release` or `mon_text_release` cleans up and frees the reader pointer `rp`.
The problem is, `mon_bus_submit` *also* uses `spin_lock_irqsave(&mbus->lock, flags)`. So, if `mon_bus_submit` is already executing the reader callback, `mon_reader_del` will block on `mbus->lock`. However, if `mon_reader_del` acquires the lock first, the reader's entry, `r`, is unlinked from the list. The issue here, I believe, is that when `mon_bus_submit` gets the lock later, `r` is no longer in `mbus->r_list`. As a result, `rnf_submit` is never called for that specific reader, which could lead to some data loss.
Now, let's consider the specific case of `mbus == &mon_bus0`. I see `mon_stop` includes a check for this scenario:
```c
if (mbus == &mon_bus0) {
list_for_each_entry(mbus, &mon_buses, bus_link) {
/*
* We do not change nreaders here, so rely on mon_lock.
*/
if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
ubus->monitored = 0;
}
}
```
The comment here is interesting: "We do not change nreaders here, so rely on mon_lock." This reinforces that `mon_reader_del` for `mon_bus0` is called with `mon_lock` already held (from `mon_bin_release` or `mon_text_release`). For each `mbus` in the `mon_buses` list, if `mbus->nreaders == 0`, `ubus->monitored` is cleared.
If a concurrent `usbmon_urb_submit()` runs on a specific `ubus`:
* If `ubus->monitored` is read as 0, the function does nothing.
* If `ubus->monitored` is read as 1, the code enters `mon_submit()`, which in turn calls both `mon_bus_submit(mbus, urb)` and `mon_bus_submit(&mon_bus0, urb)`. Both of these functions will acquire their respective `mbus->lock`.
For the first `mbus` in the for loop, if `mbus->nreaders == 0`, `r_list` is empty, so nothing gets called. For `mon_bus0`, because `mon_reader_del` holds `mon_bus0.lock`, `mon_bus_submit(&mon_bus0, urb)` waits until `mon_reader_del` releases the lock. At that point, the reader has already been unlinked from `mon_bus0.r_list`, so, again, nothing gets called.
Wait, the memory barrier (`mb()`) in `mon_stop` has me a bit confused, too. The `mon_stop` function has this snippet:
```c
} else {
/*
* A stop can be called for a dissolved mon_bus in case of
* a reader staying across an rmmod foo_hcd, so test ->u_bus.
*/
if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
ubus->monitored = 0;
mb();
}
}
```
I need to check the git blame/commit history to really understand why this `mb()` is there. It's likely a critical piece to ensure the `ubus->monitored = 0` write is visible to any other core that might be
|
| 100/4 |
2026/09/28 05:56 |
tool |
git-blame |
0m
Args: map[End:178 File:drivers/usb/mon/mon_main.c Start:156]
Results: map[Output:^1da177e4c3f4 156) static void mon_stop(struct mon_bus *mbus)
^1da177e4c3f4 157) {
c36d54ab380fb 158) struct usb_bus *ubus;
^1da177e4c3f4 159)
ecb658d387dc0 160) if (mbus == &mon_bus0) {
80920e2126926 161) list_for_each_entry(mbus, &mon_buses, bus_link) {
ecb658d387dc0 162) /*
ecb658d387dc0 163) * We do not change nreaders here, so rely on mon_lock.
ecb658d387dc0 164) */
ecb658d387dc0 165) if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
ecb658d387dc0 166) ubus->monitored = 0;
ecb658d387dc0 167) }
ecb658d387dc0 168) } else {
ecb658d387dc0 169) /*
ecb658d387dc0 170) * A stop can be called for a dissolved mon_bus in case of
ecb658d387dc0 171) * a reader staying across an rmmod foo_hcd, so test ->u_bus.
ecb658d387dc0 172) */
ecb658d387dc0 173) if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
ecb658d387dc0 174) ubus->monitored = 0;
ecb658d387dc0 175) mb();
ecb658d387dc0 176) }
^1da177e4c3f4 177) }
^1da177e4c3f4 178) }
]
|
| 101/4 |
2026/09/28 05:56 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 52265
output: 42
thoughts: 84
Thoughts: Okay, let's dive into this. I'm looking at commit `1da177e4c3f4` concerning `mon_stop`. The key line jumps out at me: `ubus->monitored = 0; mb();`. My initial question is: why was that `mb()` β the memory barrier β *originally* included? It's crucial to understand the rationale behind it, so I'll need to go beyond just what's presented here. The memory barrier suggests some potential concurrency concern. I need to understand what else was happening around the call to `mon_stop` at that time. I'll need to meticulously inspect the surrounding code within commit `1da177e4c3f4` and see what other memory accesses or synchronizations might have been in play to determine the original intent behind that memory barrier. It's likely related to the potential hazards of concurrent access, but to be sure, I'll need the broader context.
|
| 102/4 |
2026/09/28 05:56 |
tool |
git-show |
0m
Args: map[Commit:1da177e4c3f4 File:drivers/usb/mon/mon_main.c]
Results: map[Output:commit 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2
Author: Linus Torvalds <torvalds@ppc970.osdl.org>
Date: Sat Apr 16 15:20:36 2005 -0700
Linux-2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
diff --git a/drivers/usb/mon/mon_main.c b/drivers/usb/mon/mon_main.c
new file mode 100644
index 000000000000..aa9d00808e4e
--- /dev/null
+++ b/drivers/usb/mon/mon_main.c
@@ -0,0 +1,377 @@
+/*
+ * The USB Monitor, inspired by Dave Harding's USBMon.
+ *
+ * mon_main.c: Main file, module initiation and exit, registrations, etc.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/usb.h>
+#include <linux/debugfs.h>
+#include <linux/smp_lock.h>
+
+#include "usb_mon.h"
+#include "../core/hcd.h"
+
+static void mon_submit(struct usb_bus *ubus, struct urb *urb);
+static void mon_complete(struct usb_bus *ubus, struct urb *urb);
+static void mon_stop(struct mon_bus *mbus);
+static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
+static void mon_bus_drop(struct kref *r);
+static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus);
+
+DECLARE_MUTEX(mon_lock);
+
+static struct dentry *mon_dir; /* /dbg/usbmon */
+static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
+
+/*
+ * Link a reader into the bus.
+ *
+ * This must be called with mon_lock taken because of mbus->ref.
+ */
+void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
+{
+ unsigned long flags;
+ struct usb_bus *ubus;
+
+ spin_lock_irqsave(&mbus->lock, flags);
+ if (mbus->nreaders == 0) {
+ ubus = mbus->u_bus;
+ if (ubus->monitored) {
+ /*
+ * Something is really broken, refuse to go on and
+ * possibly corrupt ops pointers or worse.
+ */
+ printk(KERN_ERR TAG ": bus %d is already monitored\n",
+ ubus->busnum);
+ spin_unlock_irqrestore(&mbus->lock, flags);
+ return;
+ }
+ ubus->monitored = 1;
+ }
+ mbus->nreaders++;
+ list_add_tail(&r->r_link, &mbus->r_list);
+ spin_unlock_irqrestore(&mbus->lock, flags);
+
+ kref_get(&mbus->ref);
+}
+
+/*
+ * Unlink reader from the bus.
+ *
+ * This is called with mon_lock taken, so we can decrement mbus->ref.
+ */
+void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&mbus->lock, flags);
+ list_del(&r->r_link);
+ --mbus->nreaders;
+ if (mbus->nreaders == 0)
+ mon_stop(mbus);
+ spin_unlock_irqrestore(&mbus->lock, flags);
+
+ kref_put(&mbus->ref, mon_bus_drop);
+}
+
+/*
+ */
+static void mon_submit(struct usb_bus *ubus, struct urb *urb)
+{
+ struct mon_bus *mbus;
+ unsigned long flags;
+ struct list_head *pos;
+ struct mon_reader *r;
+
+ mbus = ubus->mon_bus;
+ if (mbus == NULL)
+ goto out_unlocked;
+
+ spin_lock_irqsave(&mbus->lock, flags);
+ if (mbus->nreaders == 0)
+ goto out_locked;
+
+ list_for_each (pos, &mbus->r_list) {
+ r = list_entry(pos, struct mon_reader, r_link);
+ r->rnf_submit(r->r_data, urb);
+ }
+
+ spin_unlock_irqrestore(&mbus->lock, flags);
+ return;
+
+out_locked:
+ spin_unlock_irqrestore(&mbus->lock, flags);
+out_unlocked:
+ return;
+}
+
+/*
+ */
+static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int err)
+{
+ struct mon_bus *mbus;
+
+ mbus = ubus->mon_bus;
+ if (mbus == NULL)
+ goto out_unlocked;
+
+ /*
+ * XXX Capture the error code and the 'E' event.
+ */
+
+ return;
+
+out_unlocked:
+ return;
+}
+
+/*
+ */
+static void mon_complete(struct usb_bus *ubus, struct urb *urb)
+{
+ struct mon_bus *mbus;
+ unsigned long flags;
+ struct list_head *pos;
+ struct mon_reader *r;
+
+ mbus = ubus->mon_bus;
+ if (mbus == NULL) {
+ /*
+ * This should not happen.
+ * At this point we do not even know the bus number...
+ */
+ printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
+ urb->pipe);
+ return;
+ }
+
+ spin_lock_irqsave(&mbus->lock, flags);
+ list_for_each (pos, &mbus->r_list) {
+ r = list_entry(pos, struct mon_reader, r_link);
+ r->rnf_complete(r->r_data, urb);
+ }
+ spin_unlock_irqrestore(&mbus->lock, flags);
+}
+
+/* int (*unlink_urb) (struct urb *urb, int status); */
+
+/*
+ * Stop monitoring.
+ * Obviously this must be well locked, so no need to play with mb's.
+ */
+static void mon_stop(struct mon_bus *mbus)
+{
+ struct usb_bus *ubus = mbus->u_bus;
+
+ /*
+ * A stop can be called for a dissolved mon_bus in case of
+ * a reader staying across an rmmod foo_hcd.
+ */
+ if (ubus != NULL) {
+ ubus->monitored = 0;
+ mb();
+ }
+}
+
+/*
+ * Add a USB bus (usually by a modprobe foo-hcd)
+ *
+ * This does not return an error code because the core cannot care less
+ * if monitoring is not established.
+ */
+static void mon_bus_add(struct usb_bus *ubus)
+{
+ mon_bus_init(mon_dir, ubus);
+}
+
+/*
+ * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
+ */
+static void mon_bus_remove(struct usb_bus *ubus)
+{
+ struct mon_bus *mbus = ubus->mon_bus;
+
+ down(&mon_lock);
+ list_del(&mbus->bus_link);
+ debugfs_remove(mbus->dent_t);
+ debugfs_remove(mbus->dent_s);
+
+ mon_dissolve(mbus, ubus);
+ kref_put(&mbus->ref, mon_bus_drop);
+ up(&mon_lock);
+}
+
+/*
+ * Ops
+ */
+static struct usb_mon_operations mon_ops_0 = {
+ .urb_submit = mon_submit,
+ .urb_submit_error = mon_submit_error,
+ .urb_complete = mon_complete,
+ .bus_add = mon_bus_add,
+ .bus_remove = mon_bus_remove,
+};
+
+/*
+ * Tear usb_bus and mon_bus apart.
+ */
+static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
+{
+
+ /*
+ * Never happens, but...
+ */
+ if (ubus->monitored) {
+ printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
+ ubus->busnum);
+ ubus->monitored = 0;
+ mb();
+ }
+
+ ubus->mon_bus = NULL;
+ mbus->u_bus = NULL;
+ mb();
+ // usb_bus_put(ubus);
+}
+
+/*
+ */
+static void mon_bus_drop(struct kref *r)
+{
+ struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
+ kfree(mbus);
+}
+
+/*
+ * Initialize a bus for us:
+ * - allocate mon_bus
+ * - refcount USB bus struct
+ * - link
+ */
+static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
+{
+ struct dentry *d;
+ struct mon_bus *mbus;
+ enum { NAMESZ = 10 };
+ char name[NAMESZ];
+ int rc;
+
+ if ((mbus = kmalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
+ goto err_alloc;
+ memset(mbus, 0, sizeof(struct mon_bus));
+ kref_init(&mbus->ref);
+ spin_lock_init(&mbus->lock);
+ INIT_LIST_HEAD(&mbus->r_list);
+
+ /*
+ * This usb_bus_get here is superfluous, because we receive
+ * a notification if usb_bus is about to be removed.
+ */
+ // usb_bus_get(ubus);
+ mbus->u_bus = ubus;
+ ubus->mon_bus = mbus;
+
+ rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
+ if (rc <= 0 || rc >= NAMESZ)
+ goto err_print_t;
+ d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_text);
+ if (d == NULL)
+ goto err_create_t;
+ mbus->dent_t = d;
+
+ rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
+ if (rc <= 0 || rc >= NAMESZ)
+ goto err_print_s;
+ d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_stat);
+ if (d == NULL)
+ goto err_create_s;
+ mbus->dent_s = d;
+
+ down(&mon_lock);
+ list_add_tail(&mbus->bus_link, &mon_buses);
+ up(&mon_lock);
+ return;
+
+err_create_s:
+err_print_s:
+ debugfs_remove(mbus->dent_t);
+err_create_t:
+err_print_t:
+ kfree(mbus);
+err_alloc:
+ return;
+}
+
+static int __init mon_init(void)
+{
+ struct usb_bus *ubus;
+ struct dentry *mondir;
+
+ mondir = debugfs_create_dir("usbmon", NULL);
+ if (IS_ERR(mondir)) {
+ printk(KERN_NOTICE TAG ": debugs is not available\n");
+ return -ENODEV;
+ }
+ if (mondir == NULL) {
+ printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
+ return -ENODEV;
+ }
+ mon_dir = mondir;
+
+ if (usb_mon_register(&mon_ops_0) != 0) {
+ printk(KERN_NOTICE TAG ": unable to register with the core\n");
+ debugfs_remove(mondir);
+ return -ENODEV;
+ }
+ // MOD_INC_USE_COUNT(which_module?);
+
+ down(&usb_bus_list_lock);
+ list_for_each_entry (ubus, &usb_bus_list, bus_list) {
+ mon_bus_init(mondir, ubus);
+ }
+ up(&usb_bus_list_lock);
+ return 0;
+}
+
+static void __exit mon_exit(void)
+{
+ struct mon_bus *mbus;
+ struct list_head *p;
+
+ usb_mon_deregister();
+
+ down(&mon_lock);
+ while (!list_empty(&mon_buses)) {
+ p = mon_buses.next;
+ mbus = list_entry(p, struct mon_bus, bus_link);
+ list_del(p);
+
+ debugfs_remove(mbus->dent_t);
+ debugfs_remove(mbus->dent_s);
+
+ /*
+ * This never happens, because the open/close paths in
+ * file level maintain module use counters and so rmmod fails
+ * before reaching here. However, better be safe...
+ */
+ if (mbus->nreaders) {
+ printk(KERN_ERR TAG
+ ": Outstanding opens (%d) on usb%d, leaking...\n",
+ mbus->nreaders, mbus->u_bus->busnum);
+ atomic_set(&mbus->ref.refcount, 2); /* Force leak */
+ }
+
+ mon_dissolve(mbus, mbus->u_bus);
+ kref_put(&mbus->ref, mon_bus_drop);
+ }
+ up(&mon_lock);
+
+ debugfs_remove(mon_dir);
+}
+
+module_init(mon_init);
+module_exit(mon_exit);
+
+MODULE_LICENSE("GPL");
]
|
| 103/4 |
2026/09/28 05:56 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 56536
output: 30
thoughts: 2679
Thoughts: **Analysis of USB Mon concurrent read/write to `monitored` flag during reader removal and URB submission**
Okay, so the question is about potential race conditions and stale reads on the `bus->monitored` flag in the USB monitor code, specifically around `mon_reader_del()` and `usbmon_urb_submit()`. I need to figure out if these operations can lead to memory corruption, use-after-free, or invalid behavior. Here's my breakdown:
First, the direct issue is a data race. C/C++ doesn't guarantee atomicity for plain reads/writes to integers, but in the kernel, single-word reads/writes are generally atomic due to the underlying architecture (single-copy atomic). The code assumes this, which is common in the kernel for performance. While technically a race, in this case it's benign because the value, either 0 or 1, is being used as a simple flag. I'd typically add `READ_ONCE/WRITE_ONCE` to this type of code in a modern kernel for KCSAN protection.
Next, I need to analyze both scenarios: if `bus->monitored` reads 0 or 1. If it reads 0, the check `if (bus->monitored)` will be skipped, so the URB won't be monitored. This is perfectly fine. The reader is being deleted, meaning no one is interested in the bus traffic anymore. Missing an event at that boundary is expected and harmless. Nothing is corrupted, no UAF.
Now, the interesting scenario: reading a "stale" `monitored = 1` while `mon_stop()` is setting it to 0. This means `mon_bus_submit()` is called. `mon_bus_submit()` itself protects against concurrency with `mbus->lock`, acquired with IRQs disabled.
Inside `mon_bus_submit()`:
1. The code calls `mon_bus_submit(mbus, urb)` which calls `list_for_each_entry(r, &mbus->r_list, r_link)`. The critical section here is `mbus->lock`.
2. In this scenario, `mon_reader_del()` is removing the reader `r`. Both `mon_reader_del` and `mon_bus_submit` serialize on `mbus->lock`.
* If `mon_bus_submit()` acquires the lock *before* `mon_reader_del()`, then the reader `r` is still in the list and the `rnf_submit` callback is executed safely. Then `mon_reader_del` unlinks `r`.
* If `mon_bus_submit()` acquires the lock *after* `mon_reader_del()`, then `r` has already been unlinked and the `list_for_each_entry` will iterate zero times and `rnf_submit` is not called, which is correct since the reader is being deleted. Either way, safe.
3. Regarding memory safety:
* The `mbus` itself is refcounted. `mon_reader_del` drops the reference of the reader, but the reference of the bus remains. `mbus` isn't freed.
* The reader `r` *is* freed, but only after `mon_reader_del` returns (in `mon_bin_release` or `mon_text_release`). Critically, `list_del` and the call to `rnf_submit` are protected by `mbus->lock`, preventing use-after-free on `r`.
* `urb` and `bus` are valid; checked during `usb_hcd_submit_urb`.
* `mon_ops` won't change; protected during module unload.
Lastly, I confirmed the protection on reader add/del: `mon_reader_add` and `mon_reader_del` are protected by `mutex_lock(&mon_lock)` in `mon_text_open`/`release` and `mon_bin_open`/`release`, along with `spin_lock_irqsave(&mbus->lock, flags)` inside those add/del functions, so concurrency there is also safe.
|
| 104/4 |
2026/09/28 05:56 |
tool |
grepper |
0m
Args: map[Expression:mutex.*mon_lock PathPrefix:drivers/usb/mon]
Results: map[Output:drivers/usb/mon/mon_bin.c=678=static int mon_bin_open(struct inode *inode, struct file *file)
--
drivers/usb/mon/mon_bin.c-684-
drivers/usb/mon/mon_bin.c:685: mutex_lock(&mon_lock);
drivers/usb/mon/mon_bin.c-686- mbus = mon_bus_lookup(iminor(inode));
drivers/usb/mon/mon_bin.c-687- if (mbus == NULL) {
drivers/usb/mon/mon_bin.c:688: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_bin.c-689- return -ENODEV;
--
drivers/usb/mon/mon_bin.c-692- printk(KERN_ERR TAG ": consistency error on open\n");
drivers/usb/mon/mon_bin.c:693: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_bin.c-694- return -ENODEV;
--
drivers/usb/mon/mon_bin.c-724- file->private_data = rp;
drivers/usb/mon/mon_bin.c:725: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_bin.c-726- return 0;
--
drivers/usb/mon/mon_bin.c-732-err_alloc:
drivers/usb/mon/mon_bin.c:733: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_bin.c-734- return rc;
--
drivers/usb/mon/mon_bin.c=783=static int mon_bin_release(struct inode *inode, struct file *file)
--
drivers/usb/mon/mon_bin.c-787-
drivers/usb/mon/mon_bin.c:788: mutex_lock(&mon_lock);
drivers/usb/mon/mon_bin.c-789-
--
drivers/usb/mon/mon_bin.c-791- printk(KERN_ERR TAG ": consistency error on close\n");
drivers/usb/mon/mon_bin.c:792: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_bin.c-793- return 0;
--
drivers/usb/mon/mon_bin.c-800-
drivers/usb/mon/mon_bin.c:801: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_bin.c-802- return 0;
--
drivers/usb/mon/mon_main.c=186=static void mon_bus_add(struct usb_bus *ubus)
--
drivers/usb/mon/mon_main.c-188- mon_bus_init(ubus);
drivers/usb/mon/mon_main.c:189: mutex_lock(&mon_lock);
drivers/usb/mon/mon_main.c-190- if (mon_bus0.nreaders != 0)
drivers/usb/mon/mon_main.c-191- ubus->monitored = 1;
drivers/usb/mon/mon_main.c:192: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_main.c-193-}
--
drivers/usb/mon/mon_main.c=198=static void mon_bus_remove(struct usb_bus *ubus)
--
drivers/usb/mon/mon_main.c-201-
drivers/usb/mon/mon_main.c:202: mutex_lock(&mon_lock);
drivers/usb/mon/mon_main.c-203- list_del(&mbus->bus_link);
--
drivers/usb/mon/mon_main.c-210- kref_put(&mbus->ref, mon_bus_drop);
drivers/usb/mon/mon_main.c:211: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_main.c-212-}
--
drivers/usb/mon/mon_main.c=272=static void mon_bus_init(struct usb_bus *ubus)
--
drivers/usb/mon/mon_main.c-292-
drivers/usb/mon/mon_main.c:293: mutex_lock(&mon_lock);
drivers/usb/mon/mon_main.c-294- list_add_tail(&mbus->bus_link, &mon_buses);
drivers/usb/mon/mon_main.c:295: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_main.c-296- return;
--
drivers/usb/mon/mon_main.c=371=static void __exit mon_exit(void)
--
drivers/usb/mon/mon_main.c-378-
drivers/usb/mon/mon_main.c:379: mutex_lock(&mon_lock);
drivers/usb/mon/mon_main.c-380-
--
drivers/usb/mon/mon_main.c-412-
drivers/usb/mon/mon_main.c:413: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_main.c-414-
--
drivers/usb/mon/mon_text.c=324=static int mon_text_open(struct inode *inode, struct file *file)
--
drivers/usb/mon/mon_text.c-329-
drivers/usb/mon/mon_text.c:330: mutex_lock(&mon_lock);
drivers/usb/mon/mon_text.c-331- mbus = inode->i_private;
--
drivers/usb/mon/mon_text.c-366- file->private_data = rp;
drivers/usb/mon/mon_text.c:367: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_text.c-368- return 0;
--
drivers/usb/mon/mon_text.c-376-err_alloc:
drivers/usb/mon/mon_text.c:377: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_text.c-378- return rc;
--
drivers/usb/mon/mon_text.c=642=static int mon_text_release(struct inode *inode, struct file *file)
--
drivers/usb/mon/mon_text.c-649-
drivers/usb/mon/mon_text.c:650: mutex_lock(&mon_lock);
drivers/usb/mon/mon_text.c-651- mbus = inode->i_private;
--
drivers/usb/mon/mon_text.c-654- printk(KERN_ERR TAG ": consistency error on close\n");
drivers/usb/mon/mon_text.c:655: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_text.c-656- return 0;
--
drivers/usb/mon/mon_text.c-680-
drivers/usb/mon/mon_text.c:681: mutex_unlock(&mon_lock);
drivers/usb/mon/mon_text.c-682- return 0;
--
drivers/usb/mon/usb_mon.h=66=void mon_bin_exit(void);
--
drivers/usb/mon/usb_mon.h-69- */
drivers/usb/mon/usb_mon.h:70:extern struct mutex mon_lock;
drivers/usb/mon/usb_mon.h-71-
]
|
| 105/4 |
2026/09/28 05:56 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 62140
output: 40
thoughts: 25
Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience:
**Investigating Deletion Mechanisms**
Okay, so I'm diving back into this deletion logic. My initial hunch is that something isn't quite right with how we're handling data removal. Let me quickly check the implementations of `mon_bin_del` and `mon_text_del`. I want to understand their specific roles. I'm assuming that based on the naming convention, these methods are responsible for deleting binary and text data respectively. I need to refresh my understanding of how each of them interacts with the underlying storage and indices. I'm also curious if there's any overlap or potential conflict in their operations. A quick review of their code will help me identify any potential bottlenecks or areas prone to errors. I'll need to pay close attention to the transaction management within each to make sure the deletions are consistent and atomic.
|
| 106/4 |
2026/09/28 05:56 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/mon/mon_bin.c Name:mon_bin_del]
Results: map[SourceCode:1381: void mon_bin_del(struct mon_bus *mbus)
1382: {
1383: device_destroy(&mon_bin_class, mbus->classdev->devt);
1384: }
SourceFile:drivers/usb/mon/mon_bin.c]
|
| 107/4 |
2026/09/28 05:56 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 62496
output: 40
thoughts: 8
Thoughts: Okay, I'm ready. Here's a summary as if I'm processing the "mon_text_del" information:
**Analyzing "mon_text_del" β What's the Context?**
Alright, let's break this down. "mon_text_del"... that sounds like a deletion command or signal, likely related to text manipulation within a larger system. My initial instinct is to consider the context: is this a data processing pipeline, a text editor, a natural language processing model, or something else entirely? The 'mon' prefix implies some kind of monitoring or management element, which is critical. What's being *monitored* and *deleted*? Understanding the data structure and purpose is key before I can determine the function. Is it a log file, a database entry, temporary buffer or text from the screen?
From a technical perspective, the deletion could involve a simple flag, a physical removal, a masking operation, or even a more complex versioning process. The nuances of the implementation are important. Is it an atomic operation? Does it consider dependencies or cascading effects? Does it include any form of auditing or journaling? The implications for data integrity and recovery are immediate concerns. In the context of NLP, removing text, especially in a monitored system, can significantly affect the accuracy or even bias of the outputs or the behavior of an AI model.
I immediately consider edge cases: what happens if the 'mon_text_del' command fails? Are there rollback mechanisms, error handling, and alerts in place? How does it handle concurrent operations, particularly if this is a multi-threaded or distributed environment? I have to consider the scale; is this a single instance operation or a high-volume scenario?
I want to know if there's any versioning and backup that might go on as the system deletes the text; the scale of the system, and its potential impact on data integrity.
In conclusion, "mon_text_del" is just the start.
|
| 108/4 |
2026/09/28 05:57 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/mon/mon_text.c Name:mon_text_del]
Results: map[SourceCode: 725: void mon_text_del(struct mon_bus *mbus)
726: {
727: debugfs_remove(mbus->dent_u);
728: debugfs_remove(mbus->dent_t);
729: debugfs_remove(mbus->dent_s);
730: }
SourceFile:drivers/usb/mon/mon_text.c]
|
| 109/4 |
2026/09/28 05:57 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 63088
output: 29
thoughts: 67
Thoughts: Here's my summary, as the expert, of those thoughts:
**Analyzing `rnf_submit` Implementations in the USB Monitor Drivers**
Okay, so I'm diving into the `mon_bin` and `mon_text` codebases to understand the `rnf_submit` function implementations. My initial thought is to pinpoint exactly what function `rnf_submit` is pointing to in the `mon_bin.c` file. I need to see the actual function definition. Then, to get the complete picture I should check the implementation of `rnf_submit` in `mon_text.c` as well. Since I'm working with the USB monitor drivers, a quick search within the `drivers/usb/mon/` directory for `rnf_submit` should yield the various implementations and usages across the monitoring tools. That's the first step for a deep understanding.
|
| 110/4 |
2026/09/28 05:57 |
tool |
grepper |
0m
Args: map[Expression:rnf_submit PathPrefix:drivers/usb/mon]
Results: map[Output:drivers/usb/mon/mon_bin.c=678=static int mon_bin_open(struct inode *inode, struct file *file)
--
drivers/usb/mon/mon_bin.c-717- rp->r.r_data = rp;
drivers/usb/mon/mon_bin.c:718: rp->r.rnf_submit = mon_bin_submit;
drivers/usb/mon/mon_bin.c-719- rp->r.rnf_error = mon_bin_error;
--
drivers/usb/mon/mon_main.c=81=static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
--
drivers/usb/mon/mon_main.c-88- list_for_each_entry(r, &mbus->r_list, r_link)
drivers/usb/mon/mon_main.c:89: r->rnf_submit(r->r_data, urb);
drivers/usb/mon/mon_main.c-90- spin_unlock_irqrestore(&mbus->lock, flags);
--
drivers/usb/mon/mon_text.c=324=static int mon_text_open(struct inode *inode, struct file *file)
--
drivers/usb/mon/mon_text.c-350- rp->r.r_data = rp;
drivers/usb/mon/mon_text.c:351: rp->r.rnf_submit = mon_text_submit;
drivers/usb/mon/mon_text.c-352- rp->r.rnf_error = mon_text_error;
--
drivers/usb/mon/usb_mon.h=43=struct mon_reader {
--
drivers/usb/mon/usb_mon.h-47-
drivers/usb/mon/usb_mon.h:48: void (*rnf_submit)(void *data, struct urb *urb);
drivers/usb/mon/usb_mon.h-49- void (*rnf_error)(void *data, struct urb *urb, int error);
]
|
| 111/4 |
2026/09/28 05:57 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 63905
output: 40
thoughts: 19
Thoughts: Okay, here's my interpretation of what's going on, framed as a thought process:
**Diving into the Submission Components**
Right, let's take a look at these two seemingly separate submission mechanisms: `mon_bin_submit` and `mon_text_submit`. My initial thought is to figure out if there is any shared logic, or if they are wildly different. Are we talking about handling different *types* of data, like binary files versus plain text, or is there a higher-level organizational distinction? This is important because understanding the differences in structure will determine where I focus my efforts. I need to understand if they use a shared architecture with key differences in data handling, or a more completely independent design. Based on that knowledge, I can start mapping out the various code paths and dependencies within each of these functions.
|
| 112/4 |
2026/09/28 05:57 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/mon/mon_bin.c Name:mon_bin_submit]
Results: map[SourceCode: 625: static void mon_bin_submit(void *data, struct urb *urb)
626: {
627: struct mon_reader_bin *rp = data;
628: mon_bin_event(rp, urb, 'S', -EINPROGRESS);
629: }
SourceFile:drivers/usb/mon/mon_bin.c]
|
| 113/4 |
2026/09/28 05:57 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 64255
output: 40
thoughts: 8
Thoughts: Okay, I'm ready. Here's a summary, in the first person, assuming I'm an expert in the field, as requested. I'll include the "mon_text_submit" as well, understanding its potential significance.
***
**My Analytical Process: Deconstructing the "mon_text_submit" Prompt**
Alright, let's break this down. The core directive is to interpret and summarize a thought process presented as a text, and then to present it as my own. The additional element, "mon_text_submit," feels crucial. Given my experience, I'm immediately assuming this is related to monitoring, and potentially the submission of text, perhaps within a system or application. It could signify a trigger, a signal, a specific event in a workflow β a critical point I need to be aware of. The challenge is to maintain the expert framing throughout the thought process and the summary. It's about displaying the rigor expected of a professional, not just repeating the text. So, I will be adding my own expertise to the summary. I need to be able to do this.
Therefore:
I will apply my expertise to give a fulsome summary of any data provided in the style of my own thoughts, including the reference to "mon_text_submit," as a professional.
|
| 114/4 |
2026/09/28 05:57 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/mon/mon_text.c Name:mon_text_submit]
Results: map[SourceCode: 259: static void mon_text_submit(void *data, struct urb *urb)
260: {
261: struct mon_reader_text *rp = data;
262: mon_text_event(rp, urb, 'S', -EINPROGRESS);
263: }
SourceFile:drivers/usb/mon/mon_text.c]
|
| 115/4 |
2026/09/28 05:57 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 64693
output: 40
thoughts: 22
Thoughts: Okay, here's my attempt at summarizing those thoughts from an expert's perspective, using a first-person perspective:
**Diving into the Event Handlers**
Alright, let's take a closer look under the hood. My immediate thought is to drill down into the internals of `mon_bin_event` and `mon_text_event`. I need to understand precisely how these handlers are structured and how they're handling their respective binary and text-based event data. Specifically, I want to unpack the logic flow within each; what's the control structure? Is there any interesting or novel approach to data processing, error handling, or resource management? Knowing this will be key to identifying potential performance bottlenecks, security vulnerabilities, or simply opportunities for optimization. I want to see if they deviate from established best practices, or if they offer interesting implementation patterns that can inform future projects. I need to understand what's unique about these specific event handlers, especially when contrasted with other event handling systems I've encountered.
|
| 116/4 |
2026/09/28 05:57 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/mon/mon_bin.c Name:mon_bin_event]
Results: map[SourceCode: 490: static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
491: char ev_type, int status)
492: {
493: const struct usb_endpoint_descriptor *epd = &urb->ep->desc;
494: struct timespec64 ts;
495: unsigned long flags;
496: unsigned int urb_length;
497: unsigned int offset;
498: unsigned int length;
499: unsigned int delta;
500: unsigned int ndesc, lendesc;
501: unsigned char dir;
502: struct mon_bin_hdr *ep;
503: char data_tag = 0;
504:
505: ktime_get_real_ts64(&ts);
506:
507: spin_lock_irqsave(&rp->b_lock, flags);
508:
509: /*
510: * Find the maximum allowable length, then allocate space.
511: */
512: urb_length = (ev_type == 'S') ?
513: urb->transfer_buffer_length : urb->actual_length;
514: length = urb_length;
515:
516: if (usb_endpoint_xfer_isoc(epd)) {
517: if (urb->number_of_packets < 0) {
518: ndesc = 0;
519: } else if (urb->number_of_packets >= ISODESC_MAX) {
520: ndesc = ISODESC_MAX;
521: } else {
522: ndesc = urb->number_of_packets;
523: }
524: if (ev_type == 'C' && usb_urb_dir_in(urb))
525: length = mon_bin_collate_isodesc(rp, urb, ndesc);
526: } else {
527: ndesc = 0;
528: }
529: lendesc = ndesc*sizeof(struct mon_bin_isodesc);
530:
531: /* not an issue unless there's a subtle bug in a HCD somewhere */
532: if (length >= urb->transfer_buffer_length)
533: length = urb->transfer_buffer_length;
534:
535: if (length >= rp->b_size/5)
536: length = rp->b_size/5;
537:
538: if (usb_urb_dir_in(urb)) {
539: if (ev_type == 'S') {
540: length = 0;
541: data_tag = '<';
542: }
543: /* Cannot rely on endpoint number in case of control ep.0 */
544: dir = USB_DIR_IN;
545: } else {
546: if (ev_type == 'C') {
547: length = 0;
548: data_tag = '>';
549: }
550: dir = 0;
551: }
552:
553: if (rp->mmap_active) {
554: offset = mon_buff_area_alloc_contiguous(rp,
555: length + PKT_SIZE + lendesc);
556: } else {
557: offset = mon_buff_area_alloc(rp, length + PKT_SIZE + lendesc);
558: }
559: if (offset == ~0) {
560: rp->cnt_lost++;
561: spin_unlock_irqrestore(&rp->b_lock, flags);
562: return;
563: }
564:
565: ep = MON_OFF2HDR(rp, offset);
566: if ((offset += PKT_SIZE) >= rp->b_size) offset = 0;
567:
568: /*
569: * Fill the allocated area.
570: */
571: memset(ep, 0, PKT_SIZE);
572: ep->type = ev_type;
573: ep->xfer_type = xfer_to_pipe[usb_endpoint_type(epd)];
574: ep->epnum = dir | usb_endpoint_num(epd);
575: ep->devnum = urb->dev->devnum;
576: ep->busnum = urb->dev->bus->busnum;
577: ep->id = (unsigned long) urb;
578: ep->ts_sec = ts.tv_sec;
579: ep->ts_usec = ts.tv_nsec / NSEC_PER_USEC;
580: ep->status = status;
581: ep->len_urb = urb_length;
582: ep->len_cap = length + lendesc;
583: ep->xfer_flags = urb->transfer_flags;
584:
585: if (usb_endpoint_xfer_int(epd)) {
586: ep->interval = urb->interval;
587: } else if (usb_endpoint_xfer_isoc(epd)) {
588: ep->interval = urb->interval;
589: ep->start_frame = urb->start_frame;
590: ep->s.iso.error_count = urb->error_count;
591: ep->s.iso.numdesc = urb->number_of_packets;
592: }
593:
594: if (usb_endpoint_xfer_control(epd) && ev_type == 'S') {
595: ep->flag_setup = mon_bin_get_setup(ep->s.setup, urb, ev_type);
596: } else {
597: ep->flag_setup = '-';
598: }
599:
600: if (ndesc != 0) {
601: ep->ndesc = ndesc;
602: mon_bin_get_isodesc(rp, offset, urb, ev_type, ndesc);
603: if ((offset += lendesc) >= rp->b_size)
604: offset -= rp->b_size;
605: }
606:
607: if (length != 0) {
608: length = mon_bin_get_data(rp, offset, urb, length,
609: &ep->flag_data);
610: if (length > 0) {
611: delta = (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
612: ep->len_cap -= length;
613: delta -= (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
614: mon_buff_area_shrink(rp, delta);
615: }
616: } else {
617: ep->flag_data = data_tag;
618: }
619:
620: spin_unlock_irqrestore(&rp->b_lock, flags);
621:
622: wake_up(&rp->b_wait);
623: }
SourceFile:drivers/usb/mon/mon_bin.c]
|
| 117/4 |
2026/09/28 05:57 |
llm |
codeexpert-compressor |
0m
Model:
Reply: ### Overview of Investigation and Workspace State
The user is asking:
In the Linux kernel USB subsystem, `struct usb_bus` has an `int monitored` flag checked in `usbmon_urb_submit()` (`include/linux/usb/hcd.h`) and modified in `mon_stop()` (`drivers/usb/mon/mon_main.c`) when a reader is removed via `mon_reader_del()`.
- Is a concurrent read of `bus->monitored` in `usbmon_urb_submit()` while `mon_stop()` sets `ubus->monitored = 0` benign or harmful?
- Can a stale read of `monitored == 1` or `monitored == 0` cause any memory corruption, use-after-free, or invalid behavior?
---
### Relevant Code & Key Data Structures Discovered
#### 1. Checking `bus->monitored` in Fast-Path (`include/linux/usb/hcd.h`):
```c
static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
{
if (bus->monitored)
(*mon_ops->urb_submit)(bus, urb);
}
static inline void usbmon_urb_submit_error(struct usb_bus *bus, struct urb *urb,
int error)
{
if (bus->monitored)
(*mon_ops->urb_submit_error)(bus, urb, error);
}
static inline void usbmon_urb_complete(struct usb_bus *bus, struct urb *urb,
int status)
{
if (bus->monitored)
(*mon_ops->urb_complete)(bus, urb, status);
}
```
Called from `drivers/usb/core/hcd.c`: `usb_hcd_submit_urb()`:
```c
usb_get_urb(urb);
atomic_inc(&urb->use_count);
atomic_inc(&urb->dev->urbnum);
usbmon_urb_submit(&hcd->self, urb);
```
#### 2. Definition in `struct usb_bus` (`include/linux/usb.h`):
```c
#if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
struct mon_bus *mon_bus; /* non-null when associated */
int monitored; /* non-zero when monitored */
#endif
```
#### 3. Manipulation in `drivers/usb/mon/mon_main.c`:
- **Adding readers (`mon_reader_add`)**:
```c
void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
{
unsigned long flags;
struct list_head *p;
spin_lock_irqsave(&mbus->lock, flags);
if (mbus->nreaders == 0) {
if (mbus == &mon_bus0) {
list_for_each (p, &mon_buses) {
struct mon_bus *m1;
m1 = list_entry(p, struct mon_bus, bus_link);
m1->u_bus->monitored = 1;
}
} else {
mbus->u_bus->monitored = 1;
}
}
mbus->nreaders++;
list_add_tail(&r->r_link, &mbus->r_list);
spin_unlock_irqrestore(&mbus->lock, flags);
kref_get(&mbus->ref);
}
```
- **Deleting readers (`mon_reader_del`)**:
```c
void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
{
unsigned long flags;
spin_lock_irqsave(&mbus->lock, flags);
list_del(&r->r_link);
--mbus->nreaders;
if (mbus->nreaders == 0)
mon_stop(mbus);
spin_unlock_irqrestore(&mbus->lock, flags);
kref_put(&mbus->ref, mon_bus_drop);
}
```
- **Stopping monitoring (`mon_stop`)**:
```c
static void mon_stop(struct mon_bus *mbus)
{
struct usb_bus *ubus;
if (mbus == &mon_bus0) {
list_for_each_entry(mbus, &mon_buses, bus_link) {
/*
* We do not change nreaders here, so rely on mon_lock.
*/
if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
ubus->monitored = 0;
}
} else {
/*
* A stop can be called for a dissolved mon_bus in case of
* a reader staying across an rmmod foo_hcd, so test ->u_bus.
*/
if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
ubus->monitored = 0;
mb();
}
}
}
```
- **Submission handler invoked via `mon_ops->urb_submit` (`mon_submit`)**:
```c
static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
{
unsigned long flags;
struct mon_reader *r;
spin_lock_irqsave(&mbus->lock, flags);
mbus->cnt_events++;
list_for_each_entry(r, &mbus->r_list, r_link)
r->rnf_submit(r->r_data, urb);
spin_unlock_irqrestore(&mbus->lock, flags);
}
static void mon_submit(struct usb_bus *ubus, struct urb *urb)
{
struct mon_bus *mbus;
mbus = ubus->mon_bus;
if (mbus != NULL)
mon_bus_submit(mbus, urb);
mon_bus_submit(&mon_bus0, urb);
}
```
---
### Approaches and Analyses Undertaken
1. **Investigated what happens on stale read of `monitored == 0`**:
- If `bus->monitored` reads 0, `usbmon_urb_submit()` simply skips calling `(*mon_ops->urb_submit)(bus, urb)`.
- The submission continues normally via host controller driver (`hcd.c`). No lock is taken, no structures dereferenced.
- At worst, a packet trace event is omitted right around the moment when the last reader is unsubscribing or already unsubscribed. This is standard and expected when stopping trace/monitoring.
2. **Investigated what happens on stale read of `monitored == 1`**:
- If `bus->monitored` reads 1, `usbmon_urb_submit()` calls `(*mon_ops->urb_submit)(bus, urb)`.
- In `mon_submit()`, `mbus = ubus->mon_bus; if (mbus != NULL) mon_bus_submit(mbus, urb); mon_bus_submit(&mon_bus0, urb);`.
- Inside `mon_bus_submit()`:
- It acquires `spin_lock_irqsave(&mbus->lock, flags);`.
- Increments `mbus->cnt_events++`.
- Iterates over `&mbus->r_list`.
- If the reader was already removed in `mon_reader_del()` (which took `&mbus->lock` and did `list_del(&r->r_link)` and `--mbus->nreaders`), `r_list` is either empty or contains remaining readers.
- If the list is empty, nothing is dispatched.
- Releases `spin_unlock_irqrestore(&mbus->lock, flags);`.
- Also checks `&mon_bus0`, with same locking behavior on `mon_bus0.lock`.
3. **Investigated object lifecycles (`mbus`, `ubus`, `mon_ops`)**:
- **`mon_ops`**: Registered at `mon_init()` and deregistered at `mon_exit()`. Statically defined (`mon_ops_0`). `mon_ops` is never unloaded while there are active readers, and `usb_hcd_submit_urb` holds a reference to URB/bus.
- **`mbus` lifetime across `mon_reader_del`**:
- `mon_reader_del` does `kref_put(&mbus->ref, mon_bus_drop);`.
- BUT `mbus->ref` starts at 1 in `mon_bus_init()` when the USB bus is added (`mon_bus_add`).
- Each reader does `kref_get(&mbus->ref)` in `mon_reader_add()`.
- Therefore, when the last reader is deleted, `mbus->ref` drops from 2 to 1 (the initial reference owned by the USB bus itself remains).
- `mbus` is NOT destroyed when `nreaders` becomes 0. `mbus` is only dissolved/freed when the USB bus is removed (`mon_bus_remove()` -> `mon_dissolve()` -> `kref_put()`), or at `mon_exit()`.
- Thus, `ubus->mon_bus` remains valid and `mbus` is not freed by `mon_reader_del()`.
- **`ubus` lifetime**:
- The call to `usbmon_urb_submit(&hcd->self, urb)` occurs inside `usb_hcd_submit_urb()`, where `ubus` (`&hcd->self`) is the active host controller bus receiving an URB submission; it cannot be freed during this function.
- **Data race safety**:
- `monitored` is a simple `int`. On all Linux supported architectures, aligned natural word reads and writes are atomic (no partial/torn reads).
- Concurrent read without a lock while written with `mon_stop()` is a standard lockless fast-path test.
4. **Reviewed Git History & Intent**:
- Commit `ecb658d387dc` ("usbmon: bus zero") by Pete Zaitcev: introduced `bus0` and updated handling of `bus->monitored` flag.
- Memory barriers: Note `mb()` in `mon_stop()` after setting `ubus->monitored = 0`.
- The design intentionally avoids taking a spinlock on the fast path (`usb_hcd_submit_urb`) for every URB when monitoring is disabled (`bus->monitored == 0`).
---
### Current Findings & Conclusions
- **Is concurrent read benign or harmful?**
**Benign**. The `bus->monitored` flag is an intentional lockless fastpath filter to avoid locking overhead in the URB submission/completion paths when no monitoring is active.
- **Can a stale read of `monitored == 1` cause harm, corruption, or UAF?**
**No**. If `monitored == 1` is read staled, `mon_submit()` is called. `mon_submit()` safely checks `ubus->mon_bus` and calls `mon_bus_submit()`. `mon_bus_submit()` acquires `mbus->lock` (the same spinlock held by `mon_reader_del()`). If the reader was already deleted by `mon_reader_del()`, the reader list is safely empty (or reflects current readers). `mbus` is still alive because `mon_reader_del()` only drops the reader's refcount; the base refcount held by `ubus` keeps `mbus` allocated until bus removal.
- **Can a stale read of `monitored == 0` cause harm?**
**No**. If `monitored == 0` is read, `usbmon_urb_submit()` simply does not invoke the callback and returns immediately. No URB or memory corruption occurs; at most, one of the final URBs during shutdown is not logged, which is normal when monitoring stops.
---
### Potential Issues / Repetitive Tool Use To Avoid
- Do not perform further broad greps across `Documentation/` (the previous grep produced 998 results).
- The relevant files (`drivers/usb/mon/mon_main.c`, `include/linux/usb/hcd.h`, `include/linux/usb.h`, `drivers/usb/core/hcd.c`) have all been inspected in detail.
- Remember to call `set-results` before the final reply when instructed.
Tokens:
input: 43319
output: 2955
thoughts: 0
|
| 118/4 |
2026/09/28 05:57 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 22518
output: 31
thoughts: 3239
Thoughts: **Analysis of Potential Concurrency Issues in USBMon Subsystem**
Okay, let's break this down. The user's question is focused, which helps. They're specifically asking about a race condition between `usbmon_urb_submit()` and `mon_reader_del()` when `bus->monitored` is being accessed. The goal here is to determine if a stale read (either `1` or `0`) of `bus->monitored` can lead to memory corruption or undefined behavior.
First, let's confirm the code flow. `usbmon_urb_submit()` checks `bus->monitored`. If true, it calls `mon_ops->urb_submit()`, which then calls `mon_submit()`. `mon_submit()` invokes `mon_bus_submit()` to iterate through registered readers and call their submit functions. `mon_reader_del()` is where a reader is removed, and, critically, where `ubus->monitored` is set to `0` via `mon_stop()` if the reader being deleted is the last reader, as the removal logic includes a check for `mbus->nreaders == 0`.
Let's look at `mon_reader_del()`: The code shows that it acquires `mbus->lock`, removes the reader from a list, decrements the reader count, and *then* calls `mon_stop()` if needed. `mon_stop()` then sets `ubus->monitored = 0`. `mon_reader_del()` is *also* protected by a lock (either `mon_lock` or `mbus->lock`, depending on the path). This means *any* access to resources associated with `mbus` (particularly its reader list and reader data) are properly synchronized.
Now, let's consider the possible scenarios and potential problems:
1. **Stale Read of `monitored == 0`**: If `usbmon_urb_submit()` reads `bus->monitored = 0` *after* `mon_stop()` has already set it (or even concurrently), the `urb` submission is simply skipped. No harm. It just means the monitoring of that URB is not triggered (which is the *intended* behavior when a reader is being removed or has been removed). A concurrent transition from `1` to `0` or a stale read after the fact is completely benign.
2. **Stale Read of `monitored == 1`**: If `usbmon_urb_submit()` reads `bus->monitored = 1` *while* `mon_stop()` is attempting to set it to 0, it means `mon_ops->urb_submit` is called. That leads to `mon_bus_submit()`. `mon_bus_submit()` then *also* acquires `mbus->lock`. Crucially, both `mon_reader_del()` and `mon_bus_submit()` serialize access to the reader list with `mbus->lock`.
* **Case 1: `mon_bus_submit()` acquires `mbus->lock` first**: Then the read sees the data, the reader's `rnf_submit` function is called. That reader is *still* valid, because `mon_reader_del()` hasn't freed anything yet and it acquires `rp->b_lock` to perform the logging. `mon_bus_submit()` releases `mbus->lock`. Then `mon_reader_del()` gets its turn and unlinks the reader, etc. Completely safe.
* **Case 2: `mon_reader_del()` acquires `mbus->lock` first**: It unlinks the reader and decrements the reader count and calls `mon_stop()`. Then, *later*, `mon_bus_submit()` grabs `mbus->lock`. When iterating the reader list, the list will be empty (or contain whatever other readers are present, if any). It *won't* try to access a deleted reader, thus preventing any use-after-free.
3. **Object Lifetime of `mbus`**: The question specifically concerns the interaction with the `monitored` flag, but to be thorough, consider the lifetime of `mbus` itself. `mbus` is created with an initial reference count of 1, and is incremented on `mon_reader_add()` and decremented on `mon_reader_del()` *or* `mon_bus_remove()`. Crucially, `mon_bus_remove()` (the removal of the USB bus itself) *must* be carefully coordinated with URB submissions, which can be seen in the locking semantics and the call to `mon_dissolve()` to clear the `
|
| 119/4 |
2026/09/28 05:58 |
tool |
git-log |
0m
Args: map[CodeRegexp:monitored PathPrefix:drivers/usb/mon]
Results: map[Output:ecb658d387dc usbmon: bus zero
1da177e4c3f4 Linux-2.6.12-rc2
]
|
| 120/4 |
2026/09/28 05:58 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 26865
output: 25
thoughts: 17
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and rendered in the first person:
**Diving into the Commit Log**
Alright, time to dive in. My immediate thought is, "Let's zero in on commit `ecb658d387dc`." No context, no preamble, just straight to the point. This implies a need to understand the history of the codebase and the impact of the changes contained in that specific commit. It's a surgical approach β I'm not just browsing randomly; I'm honing in on something specific that requires attention. There's an underlying assumption that this commit, for whatever reason, is the key to understanding something complex or possibly problematic. I need to understand what changed, and how that changed the system.
|
| 121/4 |
2026/09/28 05:58 |
tool |
git-show |
0m
Args:
Results: map[Output:commit ecb658d387dc09f344b3d755e8674076072032c7
Author: Pete Zaitcev <zaitcev@redhat.com>
Date: Wed Apr 11 13:47:26 2007 -0700
usbmon: bus zero
Add the "bus zero" feature to the usbmon. If a user process specifies bus
with number zero, it receives events from all buses. This is useful when
we wish to see initial enumeration when a bus is created, typically after
a modprobe. Until now, an application had to loop until a new bus could
be open, then start capturing on it. This procedure was cumbersome and
could lose initial events. Also, often it's too bothersome to find exactly
to which bus a specific device is attached.
Paolo Albeni provided the original concept implementation. I added the
handling of "bus->monitored" flag and generally fixed it up.
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
index b2bedd974ac3..0af11a66207c 100644
--- a/drivers/usb/mon/mon_bin.c
+++ b/drivers/usb/mon/mon_bin.c
@@ -356,8 +356,10 @@ static inline char mon_bin_get_setup(unsigned char *setupb,
if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
return '-';
- if (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
return mon_dmapeek(setupb, urb->setup_dma, SETUP_LEN);
+ }
if (urb->setup_packet == NULL)
return 'Z';
@@ -369,7 +371,8 @@ static char mon_bin_get_data(const struct mon_reader_bin *rp,
unsigned int offset, struct urb *urb, unsigned int length)
{
- if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) {
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
mon_dmapeek_vec(rp, offset, urb->transfer_dma, length);
return 0;
}
@@ -440,7 +443,7 @@ static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
/* We use the fact that usb_pipein() returns 0x80 */
ep->epnum = usb_pipeendpoint(urb->pipe) | usb_pipein(urb->pipe);
ep->devnum = usb_pipedevice(urb->pipe);
- ep->busnum = rp->r.m_bus->u_bus->busnum;
+ ep->busnum = urb->dev->bus->busnum;
ep->id = (unsigned long) urb;
ep->ts_sec = ts.tv_sec;
ep->ts_usec = ts.tv_usec;
@@ -500,7 +503,7 @@ static void mon_bin_error(void *data, struct urb *urb, int error)
/* We use the fact that usb_pipein() returns 0x80 */
ep->epnum = usb_pipeendpoint(urb->pipe) | usb_pipein(urb->pipe);
ep->devnum = usb_pipedevice(urb->pipe);
- ep->busnum = rp->r.m_bus->u_bus->busnum;
+ ep->busnum = urb->dev->bus->busnum;
ep->id = (unsigned long) urb;
ep->status = error;
@@ -515,7 +518,6 @@ static void mon_bin_error(void *data, struct urb *urb, int error)
static int mon_bin_open(struct inode *inode, struct file *file)
{
struct mon_bus *mbus;
- struct usb_bus *ubus;
struct mon_reader_bin *rp;
size_t size;
int rc;
@@ -525,7 +527,7 @@ static int mon_bin_open(struct inode *inode, struct file *file)
mutex_unlock(&mon_lock);
return -ENODEV;
}
- if ((ubus = mbus->u_bus) == NULL) {
+ if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
printk(KERN_ERR TAG ": consistency error on open\n");
mutex_unlock(&mon_lock);
return -ENODEV;
diff --git a/drivers/usb/mon/mon_main.c b/drivers/usb/mon/mon_main.c
index c9739e7b35e5..8a1df2c9c73e 100644
--- a/drivers/usb/mon/mon_main.c
+++ b/drivers/usb/mon/mon_main.c
@@ -16,8 +16,6 @@
#include "usb_mon.h"
#include "../core/hcd.h"
-static void mon_submit(struct usb_bus *ubus, struct urb *urb);
-static void mon_complete(struct usb_bus *ubus, struct urb *urb);
static void mon_stop(struct mon_bus *mbus);
static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
static void mon_bus_drop(struct kref *r);
@@ -25,6 +23,7 @@ static void mon_bus_init(struct usb_bus *ubus);
DEFINE_MUTEX(mon_lock);
+struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
/*
@@ -35,22 +34,19 @@ static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
{
unsigned long flags;
- struct usb_bus *ubus;
+ struct list_head *p;
spin_lock_irqsave(&mbus->lock, flags);
if (mbus->nreaders == 0) {
- ubus = mbus->u_bus;
- if (ubus->monitored) {
- /*
- * Something is really broken, refuse to go on and
- * possibly corrupt ops pointers or worse.
- */
- printk(KERN_ERR TAG ": bus %d is already monitored\n",
- ubus->busnum);
- spin_unlock_irqrestore(&mbus->lock, flags);
- return;
+ if (mbus == &mon_bus0) {
+ list_for_each (p, &mon_buses) {
+ struct mon_bus *m1;
+ m1 = list_entry(p, struct mon_bus, bus_link);
+ m1->u_bus->monitored = 1;
+ }
+ } else {
+ mbus->u_bus->monitored = 1;
}
- ubus->monitored = 1;
}
mbus->nreaders++;
list_add_tail(&r->r_link, &mbus->r_list);
@@ -80,77 +76,79 @@ void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
/*
*/
-static void mon_submit(struct usb_bus *ubus, struct urb *urb)
+static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
{
- struct mon_bus *mbus;
unsigned long flags;
struct list_head *pos;
struct mon_reader *r;
- mbus = ubus->mon_bus;
- if (mbus == NULL)
- goto out_unlocked;
-
spin_lock_irqsave(&mbus->lock, flags);
- if (mbus->nreaders == 0)
- goto out_locked;
-
mbus->cnt_events++;
list_for_each (pos, &mbus->r_list) {
r = list_entry(pos, struct mon_reader, r_link);
r->rnf_submit(r->r_data, urb);
}
-
spin_unlock_irqrestore(&mbus->lock, flags);
return;
+}
-out_locked:
- spin_unlock_irqrestore(&mbus->lock, flags);
-out_unlocked:
- return;
+static void mon_submit(struct usb_bus *ubus, struct urb *urb)
+{
+ struct mon_bus *mbus;
+
+ if ((mbus = ubus->mon_bus) != NULL)
+ mon_bus_submit(mbus, urb);
+ mon_bus_submit(&mon_bus0, urb);
}
/*
*/
-static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
+static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
{
- struct mon_bus *mbus;
unsigned long flags;
struct list_head *pos;
struct mon_reader *r;
- mbus = ubus->mon_bus;
- if (mbus == NULL)
- goto out_unlocked;
-
spin_lock_irqsave(&mbus->lock, flags);
- if (mbus->nreaders == 0)
- goto out_locked;
-
mbus->cnt_events++;
list_for_each (pos, &mbus->r_list) {
r = list_entry(pos, struct mon_reader, r_link);
r->rnf_error(r->r_data, urb, error);
}
-
spin_unlock_irqrestore(&mbus->lock, flags);
return;
+}
-out_locked:
- spin_unlock_irqrestore(&mbus->lock, flags);
-out_unlocked:
- return;
+static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
+{
+ struct mon_bus *mbus;
+
+ if ((mbus = ubus->mon_bus) != NULL)
+ mon_bus_submit_error(mbus, urb, error);
+ mon_bus_submit_error(&mon_bus0, urb, error);
}
/*
*/
-static void mon_complete(struct usb_bus *ubus, struct urb *urb)
+static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb)
{
- struct mon_bus *mbus;
unsigned long flags;
struct list_head *pos;
struct mon_reader *r;
+ spin_lock_irqsave(&mbus->lock, flags);
+ mbus->cnt_events++;
+ list_for_each (pos, &mbus->r_list) {
+ r = list_entry(pos, struct mon_reader, r_link);
+ r->rnf_complete(r->r_data, urb);
+ }
+ spin_unlock_irqrestore(&mbus->lock, flags);
+}
+
+static void mon_complete(struct usb_bus *ubus, struct urb *urb)
+{
+ struct mon_bus *mbus;
+
mbus = ubus->mon_bus;
if (mbus == NULL) {
/*
@@ -162,13 +160,8 @@ static void mon_complete(struct usb_bus *ubus, struct urb *urb)
return;
}
- spin_lock_irqsave(&mbus->lock, flags);
- mbus->cnt_events++;
- list_for_each (pos, &mbus->r_list) {
- r = list_entry(pos, struct mon_reader, r_link);
- r->rnf_complete(r->r_data, urb);
- }
- spin_unlock_irqrestore(&mbus->lock, flags);
+ mon_bus_complete(mbus, urb);
+ mon_bus_complete(&mon_bus0, urb);
}
/* int (*unlink_urb) (struct urb *urb, int status); */
@@ -179,14 +172,26 @@ static void mon_complete(struct usb_bus *ubus, struct urb *urb)
static void mon_stop(struct mon_bus *mbus)
{
struct usb_bus *ubus = mbus->u_bus;
+ struct list_head *p;
- /*
- * A stop can be called for a dissolved mon_bus in case of
- * a reader staying across an rmmod foo_hcd.
- */
- if (ubus != NULL) {
- ubus->monitored = 0;
- mb();
+ if (mbus == &mon_bus0) {
+ list_for_each (p, &mon_buses) {
+ mbus = list_entry(p, struct mon_bus, bus_link);
+ /*
+ * We do not change nreaders here, so rely on mon_lock.
+ */
+ if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
+ ubus->monitored = 0;
+ }
+ } else {
+ /*
+ * A stop can be called for a dissolved mon_bus in case of
+ * a reader staying across an rmmod foo_hcd, so test ->u_bus.
+ */
+ if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
+ ubus->monitored = 0;
+ mb();
+ }
}
}
@@ -199,6 +204,10 @@ static void mon_stop(struct mon_bus *mbus)
static void mon_bus_add(struct usb_bus *ubus)
{
mon_bus_init(ubus);
+ mutex_lock(&mon_lock);
+ if (mon_bus0.nreaders != 0)
+ ubus->monitored = 1;
+ mutex_unlock(&mon_lock);
}
/*
@@ -250,12 +259,7 @@ static struct usb_mon_operations mon_ops_0 = {
static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
{
- /*
- * Never happens, but...
- */
if (ubus->monitored) {
- printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
- ubus->busnum);
ubus->monitored = 0;
mb();
}
@@ -263,6 +267,8 @@ static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
ubus->mon_bus = NULL;
mbus->u_bus = NULL;
mb();
+
+ /* We want synchronize_irq() here, but that needs an argument. */
}
/*
@@ -295,9 +301,8 @@ static void mon_bus_init(struct usb_bus *ubus)
*/
mbus->u_bus = ubus;
ubus->mon_bus = mbus;
- mbus->uses_dma = ubus->uses_dma;
- mbus->text_inited = mon_text_add(mbus, ubus);
+ mbus->text_inited = mon_text_add(mbus, ubus->busnum);
// mon_bin_add(...)
mutex_lock(&mon_lock);
@@ -309,6 +314,18 @@ static void mon_bus_init(struct usb_bus *ubus)
return;
}
+static void mon_bus0_init(void)
+{
+ struct mon_bus *mbus = &mon_bus0;
+
+ kref_init(&mbus->ref);
+ spin_lock_init(&mbus->lock);
+ INIT_LIST_HEAD(&mbus->r_list);
+
+ mbus->text_inited = mon_text_add(mbus, 0);
+ // mbus->bin_inited = mon_bin_add(mbus, 0);
+}
+
/*
* Search a USB bus by number. Notice that USB bus numbers start from one,
* which we may later use to identify "all" with zero.
@@ -322,6 +339,9 @@ struct mon_bus *mon_bus_lookup(unsigned int num)
struct list_head *p;
struct mon_bus *mbus;
+ if (num == 0) {
+ return &mon_bus0;
+ }
list_for_each (p, &mon_buses) {
mbus = list_entry(p, struct mon_bus, bus_link);
if (mbus->u_bus->busnum == num) {
@@ -341,6 +361,8 @@ static int __init mon_init(void)
if ((rc = mon_bin_init()) != 0)
goto err_bin;
+ mon_bus0_init();
+
if (usb_mon_register(&mon_ops_0) != 0) {
printk(KERN_NOTICE TAG ": unable to register with the core\n");
rc = -ENODEV;
@@ -374,6 +396,7 @@ static void __exit mon_exit(void)
usb_mon_deregister();
mutex_lock(&mon_lock);
+
while (!list_empty(&mon_buses)) {
p = mon_buses.next;
mbus = list_entry(p, struct mon_bus, bus_link);
@@ -397,6 +420,11 @@ static void __exit mon_exit(void)
mon_dissolve(mbus, mbus->u_bus);
kref_put(&mbus->ref, mon_bus_drop);
}
+
+ mbus = &mon_bus0;
+ if (mbus->text_inited)
+ mon_text_del(mbus);
+
mutex_unlock(&mon_lock);
mon_text_exit();
diff --git a/drivers/usb/mon/mon_text.c b/drivers/usb/mon/mon_text.c
index bbf54396e5f2..ec0cc51e39ac 100644
--- a/drivers/usb/mon/mon_text.c
+++ b/drivers/usb/mon/mon_text.c
@@ -124,8 +124,10 @@ static inline char mon_text_get_setup(struct mon_event_text *ep,
if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
return '-';
- if (mbus->uses_dma && (urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
+ }
if (urb->setup_packet == NULL)
return 'Z'; /* '0' would be not as pretty. */
@@ -160,8 +162,10 @@ static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
* contain non-NULL garbage in case the upper level promised to
* set DMA for the HCD.
*/
- if (mbus->uses_dma && (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
+ if (urb->dev->bus->uses_dma &&
+ (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
return mon_dmapeek(ep->data, urb->transfer_dma, len);
+ }
if (urb->transfer_buffer == NULL)
return 'Z'; /* '0' would be not as pretty. */
@@ -201,7 +205,7 @@ static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
ep->type = ev_type;
ep->pipe = urb->pipe;
ep->id = (unsigned long) urb;
- ep->busnum = rp->r.m_bus->u_bus->busnum;
+ ep->busnum = urb->dev->bus->busnum;
ep->tstamp = stamp;
ep->length = (ev_type == 'S') ?
urb->transfer_buffer_length : urb->actual_length;
@@ -305,13 +309,11 @@ static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
static int mon_text_open(struct inode *inode, struct file *file)
{
struct mon_bus *mbus;
- struct usb_bus *ubus;
struct mon_reader_text *rp;
int rc;
mutex_lock(&mon_lock);
mbus = inode->i_private;
- ubus = mbus->u_bus;
rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
if (rp == NULL) {
@@ -335,8 +337,7 @@ static int mon_text_open(struct inode *inode, struct file *file)
rp->r.rnf_error = mon_text_error;
rp->r.rnf_complete = mon_text_complete;
- snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
- (long)rp);
+ snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
rp->e_slab = kmem_cache_create(rp->slab_name,
sizeof(struct mon_event_text), sizeof(long), 0,
mon_text_ctor, NULL);
@@ -654,14 +655,14 @@ static const struct file_operations mon_fops_text_u = {
.release = mon_text_release,
};
-int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
+int mon_text_add(struct mon_bus *mbus, int busnum)
{
struct dentry *d;
enum { NAMESZ = 10 };
char name[NAMESZ];
int rc;
- rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
+ rc = snprintf(name, NAMESZ, "%dt", busnum);
if (rc <= 0 || rc >= NAMESZ)
goto err_print_t;
d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_t);
@@ -669,7 +670,7 @@ int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
goto err_create_t;
mbus->dent_t = d;
- rc = snprintf(name, NAMESZ, "%du", ubus->busnum);
+ rc = snprintf(name, NAMESZ, "%du", busnum);
if (rc <= 0 || rc >= NAMESZ)
goto err_print_u;
d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
@@ -677,7 +678,7 @@ int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
goto err_create_u;
mbus->dent_u = d;
- rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
+ rc = snprintf(name, NAMESZ, "%ds", busnum);
if (rc <= 0 || rc >= NAMESZ)
goto err_print_s;
d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
diff --git a/drivers/usb/mon/usb_mon.h b/drivers/usb/mon/usb_mon.h
index a1e2b8e9d777..13d63255283e 100644
--- a/drivers/usb/mon/usb_mon.h
+++ b/drivers/usb/mon/usb_mon.h
@@ -23,7 +23,6 @@ struct mon_bus {
struct dentry *dent_s; /* Debugging file */
struct dentry *dent_t; /* Text interface file */
struct dentry *dent_u; /* Second text interface file */
- int uses_dma;
/* Ref */
int nreaders; /* Under mon_lock AND mbus->lock */
@@ -53,7 +52,7 @@ void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r);
struct mon_bus *mon_bus_lookup(unsigned int num);
-int /*bool*/ mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus);
+int /*bool*/ mon_text_add(struct mon_bus *mbus, int busnum);
void mon_text_del(struct mon_bus *mbus);
// void mon_bin_add(struct mon_bus *);
@@ -82,4 +81,6 @@ extern struct mutex mon_lock;
extern const struct file_operations mon_fops_stat;
+extern struct mon_bus mon_bus0; /* Only for redundant checks */
+
#endif /* __USB_MON_H */
]
|
| 122/4 |
2026/09/28 05:58 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 34013
output: 40
thoughts: 25
Thoughts: Here's a summary of my thought process, as requested:
**Diving into USB Host Controller Driver (HCD) Internals**
Okay, so we need to understand something related to USB host controller drivers, particularly the `usbmon_urb_submit` function. Let's start with the standard practice and dig into the heart of the matter. My gut tells me the `include/linux/usb/hcd.h` header file is a good starting point to grasp the critical structures and definitions around this function. Specifically, I'll be looking in that area to see how URBs (USB Request Blocks) are handled and submitted to the HCD. I'm focusing on `usbmon_urb_submit` because I expect it plays a key role in monitoring or tracing USB transactions, perhaps by intercepting and logging these submissions before they're processed. This means I'll be focused on how that particular function fits into the overall HCD workflow. Let's see what we can find. I anticipate having to trace the execution path from there to understand data flows.
|
| 123/4 |
2026/09/28 05:58 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:include/linux/usb/hcd.h Name:usbmon_urb_submit]
Results: map[SourceCode: 721: static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
722: {
723: if (bus->monitored)
724: (*mon_ops->urb_submit)(bus, urb);
725: }
SourceFile:include/linux/usb/hcd.h]
|
| 124/4 |
2026/09/28 05:58 |
llm |
codeexpert |
0m
Model:
Tokens:
input: 31248
output: 1272
thoughts: 1982
Thoughts: **Analysis of Concurrent Access to `usb_bus->monitored` in Linux USB Subsystem**
Alright, let's break this down completely. The question is whether a concurrent read of `bus->monitored` in `usbmon_urb_submit()` while `mon_stop()` sets `ubus->monitored = 0` is harmful. My conclusion, based on this careful analysis, is that it is entirely benign.
Firstly, we're dealing with an `int` flag, `bus->monitored`. The read happens within the fast path of URB submission in `usbmon_urb_submit()`, explicitly lockless for performance. The write occurs in `mon_stop()`, triggered by reader removal (`mon_reader_del()`) under a spinlock.
Regarding whether it's harmful, the answer is no. This is a classic example of a benign data race, an opportunistic optimization. On modern Linux architectures, an aligned `int` read/write is atomic, preventing word tearing or undefined memory values.
Now, let's examine potential stale reads. If `monitored == 0`, `usbmon_urb_submit()` simply skips calling the monitoring function. The URB submission still proceeds as normal in `usb_hcd_submit_urb()`. The only consequence is that the URB event isn't logged by usbmon. Considering this might happen as the reader is being removed, missing an event at that time is perfectly acceptable, expected, and harmless.
If `monitored == 1`, `usbmon_urb_submit()` calls `mon_submit()`. Crucially, `mon_submit()` uses the bus pointer (`ubus`). `ubus` itself is the active USB host controller, valid during the whole `usb_hcd_submit_urb()` call. The `mbus` (the bus-specific monitoring struct) is *not* freed during `mon_reader_del()`. It's refcounted and only freed when the host controller is dismantled, which happens later (during `mon_bus_remove()`). Even if the host controller is being removed concurrently, `ubus->mon_bus` becomes NULL (with memory barriers), and `mon_submit()` checks for it. Furthermore, `mon_bus_submit()` takes the *same* spinlock as `mon_reader_del()`. Thus, there is robust protection against use-after-free and related issues. The spinlock ensures either: the reader is still active during the call, or, the reader has already been removed and the loop handles it correctly.
In essence, no memory corruption, use-after-free, or invalid behavior is possible because of these lock mechanisms and the inherent robustness of integer atomicity on the target architectures.
While this might technically be flagged as a data race in modern C standards, the kernel code predates those annotations, and the load/store operation is atomic on all targeted Linux architectures.
Therefore, the conclusion remains: This concurrent access is benign.
|
| 125/4 |
2026/09/28 05:58 |
tool |
set-results |
0m
Args: map[Answer:In the Linux kernel USB subsystem, a concurrent read of `bus->monitored` in `usbmon_urb_submit()` while `mon_stop()` clears `ubus->monitored = 0` is **completely benign**. Neither a stale read of `monitored == 1` nor `monitored == 0` can cause memory corruption, use-after-free (UAF), or invalid behavior.
### 1. Architectural Role of `bus->monitored`
The check in `include/linux/usb/hcd.h`:
```c
static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
{
if (bus->monitored)
(*mon_ops->urb_submit)(bus, urb);
}
```
is an intentional, lockless fast-path optimization designed to avoid taking locks on the high-throughput URB submission path (`usb_hcd_submit_urb()`) when no readers are active. `monitored` is an aligned `int`, meaning reads and writes are single-word atomic operations on all supported Linux architectures (no word tearing).
---
### 2. Analysis of Stale Read: `monitored == 0`
If `usbmon_urb_submit()` observes `monitored == 0`:
- It simply skips calling `(*mon_ops->urb_submit)(bus, urb)` and returns immediately.
- The URB submission proceeds normally through `usb_hcd_submit_urb()`.
- At worst, a packet trace event is omitted right around the moment when the last reader is unsubscribing. This is standard and expected behavior during monitor shutdown.
- **Result:** No corruption, no invalid state.
---
### 3. Analysis of Stale Read: `monitored == 1`
If `usbmon_urb_submit()` observes `monitored == 1` after `mon_stop()` has already set or is setting `ubus->monitored = 0`:
- It invokes `mon_submit(ubus, urb)` (via `mon_ops->urb_submit`).
- `mon_submit()` invokes `mon_bus_submit(mbus, urb)` for the bus (and `mon_bus0`).
- **Reader list synchronization:**
In `mon_bus_submit()`:
```c
spin_lock_irqsave(&mbus->lock, flags);
mbus->cnt_events++;
list_for_each_entry(r, &mbus->r_list, r_link)
r->rnf_submit(r->r_data, urb);
spin_unlock_irqrestore(&mbus->lock, flags);
```
`mon_bus_submit()` acquires `mbus->lock`. This is the **exact same spinlock** acquired by `mon_reader_del()`:
```c
spin_lock_irqsave(&mbus->lock, flags);
list_del(&r->r_link);
--mbus->nreaders;
if (mbus->nreaders == 0)
mon_stop(mbus);
spin_unlock_irqrestore(&mbus->lock, flags);
```
- If `mon_bus_submit()` acquires `mbus->lock` **before** `mon_reader_del()`: the reader is still safely on `mbus->r_list` and receives the callback normally.
- If `mon_bus_submit()` acquires `mbus->lock` **after** `mon_reader_del()`: the reader has already been unlinked via `list_del(&r->r_link)`. The list traversal sees only valid remaining readers, or an empty list. The deleted reader callback is not invoked.
- Furthermore, the reader struct `r` is only freed later in `mon_bin_release()` / `mon_text_release()` after `mon_reader_del()` returns and under `mon_lock`.
- **Data structure lifetime (`ubus` and `mbus`):**
- `ubus` is `&hcd->self` receiving the URB and is kept valid throughout `usb_hcd_submit_urb()`.
- `mbus` (`ubus->mon_bus`) is **not** freed by `mon_reader_del()`. Its refcount was initialized to 1 at bus registration (`mon_bus_init()`), and each reader takes an additional reference (`kref_get(&mbus->ref)`). `mon_reader_del()` only drops the reader's reference via `kref_put(&mbus->ref, mon_bus_drop)`. The base reference remains intact until the USB host controller itself is unregistered (`mon_bus_remove()`).
- **Result:** No use-after-free, no memory corruption.
---
### Verification and Limitations
- **Verified:** Source implementations of `usbmon_urb_submit()`, `mon_stop()`, `mon_reader_del()`, `mon_reader_add()`, `mon_bus_submit()`, `mon_bin_release()`, and `mon_text_release()` were verified in the codebase.
- **Unverified / Nuance:** Although KCSAN (Kernel Concurrency Sanitizer) might theoretically report a plain data-race on `bus->monitored` due to the lack of explicit `READ_ONCE()` / `WRITE_ONCE()` annotations (code dates back to Linux 2.6), this is a classic benign race in the kernel where exact point-in-time synchronization is unnecessary and memory ordering is safeguarded by downstream spinlocks.]
Results: map[Answer:In the Linux kernel USB subsystem, a concurrent read of `bus->monitored` in `usbmon_urb_submit()` while `mon_stop()` clears `ubus->monitored = 0` is **completely benign**. Neither a stale read of `monitored == 1` nor `monitored == 0` can cause memory corruption, use-after-free (UAF), or invalid behavior.
### 1. Architectural Role of `bus->monitored`
The check in `include/linux/usb/hcd.h`:
```c
static inline void usbmon_urb_submit(struct usb_bus *bus, struct urb *urb)
{
if (bus->monitored)
(*mon_ops->urb_submit)(bus, urb);
}
```
is an intentional, lockless fast-path optimization designed to avoid taking locks on the high-throughput URB submission path (`usb_hcd_submit_urb()`) when no readers are active. `monitored` is an aligned `int`, meaning reads and writes are single-word atomic operations on all supported Linux architectures (no word tearing).
---
### 2. Analysis of Stale Read: `monitored == 0`
If `usbmon_urb_submit()` observes `monitored == 0`:
- It simply skips calling `(*mon_ops->urb_submit)(bus, urb)` and returns immediately.
- The URB submission proceeds normally through `usb_hcd_submit_urb()`.
- At worst, a packet trace event is omitted right around the moment when the last reader is unsubscribing. This is standard and expected behavior during monitor shutdown.
- **Result:** No corruption, no invalid state.
---
### 3. Analysis of Stale Read: `monitored == 1`
If `usbmon_urb_submit()` observes `monitored == 1` after `mon_stop()` has already set or is setting `ubus->monitored = 0`:
- It invokes `mon_submit(ubus, urb)` (via `mon_ops->urb_submit`).
- `mon_submit()` invokes `mon_bus_submit(mbus, urb)` for the bus (and `mon_bus0`).
- **Reader list synchronization:**
In `mon_bus_submit()`:
```c
spin_lock_irqsave(&mbus->lock, flags);
mbus->cnt_events++;
list_for_each_entry(r, &mbus->r_list, r_link)
r->rnf_submit(r->r_data, urb);
spin_unlock_irqrestore(&mbus->lock, flags);
```
`mon_bus_submit()` acquires `mbus->lock`. This is the **exact same spinlock** acquired by `mon_reader_del()`:
```c
spin_lock_irqsave(&mbus->lock, flags);
list_del(&r->r_link);
--mbus->nreaders;
if (mbus->nreaders == 0)
mon_stop(mbus);
spin_unlock_irqrestore(&mbus->lock, flags);
```
- If `mon_bus_submit()` acquires `mbus->lock` **before** `mon_reader_del()`: the reader is still safely on `mbus->r_list` and receives the callback normally.
- If `mon_bus_submit()` acquires `mbus->lock` **after** `mon_reader_del()`: the reader has already been unlinked via `list_del(&r->r_link)`. The list traversal sees only valid remaining readers, or an empty list. The deleted reader callback is not invoked.
- Furthermore, the reader struct `r` is only freed later in `mon_bin_release()` / `mon_text_release()` after `mon_reader_del()` returns and under `mon_lock`.
- **Data structure lifetime (`ubus` and `mbus`):**
- `ubus` is `&hcd->self` receiving the URB and is kept valid throughout `usb_hcd_submit_urb()`.
- `mbus` (`ubus->mon_bus`) is **not** freed by `mon_reader_del()`. Its refcount was initialized to 1 at bus registration (`mon_bus_init()`), and each reader takes an additional reference (`kref_get(&mbus->ref)`). `mon_reader_del()` only drops the reader's reference via `kref_put(&mbus->ref, mon_bus_drop)`. The base reference remains intact until the USB host controller itself is unregistered (`mon_bus_remove()`).
- **Result:** No use-after-free, no memory corruption.
---
### Verification and Limitations
- **Verified:** Source implementations of `usbmon_urb_submit()`, `mon_stop()`, `mon_reader_del()`, `mon_reader_add()`, `mon_bus_submit()`, `mon_bin_release()`, and `mon_text_release()` were verified in the codebase.
- **Unverified / Nuance:** Although KCSAN (Kernel Concurrency Sanitizer) might theoretically report a plain data-race on `bus->monitored` due to the lack of explicit `READ_ONCE()` / `WRITE_ONCE()` annotations (code dates back to Linux 2.6), this is a classic benign race in the kernel where exact point-in-time synchronization is unnecessary and memory ordering is safeguarded by downstream spinlocks.]
|
| 126/2 |
2026/09/28 05:58 |
llm |
expert |
0m
Model:
Tokens:
input: 51504
output: 15
thoughts: 215
Thoughts: **Benign Data Race Analysis and Conclusion**
Okay, so I've dug into this data race, and the analysis is conclusive: it's completely benign. We're dealing with a classic Heuristic Hint or fast-path bypass flag situation. The specifics are key, though.
First, reading a '0' when the reader was just added: that means we miss capturing a URB submission. But that's acceptable, as the reader registration is designed to be asynchronous to URB submission anyway. It's not a critical loss.
Second, reading a '1' when the reader was just removed: this triggers `mon_ops->urb_submit(bus, urb)`. Here's where the real safety is evident. This function acquires `mbus->lock`, crucially. Then, it checks `r_list` safely, sees that itβs empty, and consequently, it just does nothing and returns safely. There's no use-after-free scenario, no dangling pointers, no crashes, and absolutely no memory corruption.
Beyond that, the `bus->monitored` variable is 32-bit aligned. Thus, its accesses are atomic. So, we're protected from word tearing.
Finally, the read in `usbmon_urb_submit` is not inside a loop. It's called once per URB submission, so we can't be tripped up by compiler loop hoisting or a livelock situation.
So, all told, this race is harmless. I'm going to call `set-results` now. We're good here.
|
| 127/2 |
2026/09/28 05:59 |
tool |
set-results |
0m
Args:
Results:
|
| 128/2 |
2026/09/28 05:59 |
llm |
expert |
0m
Model:
Tokens:
input: 52083
output: 796
thoughts: 140
Thoughts: Okay, here's my analysis. I'll summarize my thought process in the first person, assuming I'm an expert in this field, and then provide the requested output.
**My Analysis: Thinking Through the Potential Race**
Alright, let's break this down. My mind immediately goes to identifying the two functions involved, Function A and Function B, and the variable or field they're both touching. I need to understand how they are *accessing* this shared resource β is it a read-write scenario, or read-read? Are we dealing with volatile variables, atomic operations, or simple memory accesses? That's the core of the problem here: understand the access patterns.
Next, I need to visualize the *synchronization context*. Are there any locks held during either of these accesses? If so, what do they protect, and more importantly, do they *also* protect this shared resource? The presence or absence of mutual exclusion is critical. If no locks are held, or the existing locks don't cover the shared resource, that's a red flag. We're probably looking at a data race.
Now, even if a race *exists*, it doesn't automatically mean it's harmful. The *semantics* of the accesses are crucial. Does a stale or torn read corrupt data, lead to incorrect program behavior, or, worst-case scenario, cause crashes or security vulnerabilities? Or, could it be a situation where stale data is acceptable, or even expected. Is the data being read used only for display or for informational purposes?
Finally, based on my understanding of the code and the access patterns, I have to make a call: **BENIGN** or **HARMFUL**. If benign, I have to articulate *why* it's benign, explaining the inherent tolerance to stale or torn reads. And, importantly, I need to recommend specific annotations to clarify the code's intent and prevent future misunderstandings. If harmful, the need for a solution is obvious.
Now, let's formulate the response.
```
- **Race Summary:** `Function A` vs `Function B` on `Variable/Field`.
- **Synchronization context:** List held locks and explain the lack of mutual exclusion.
- **Final Classification:** **BENIGN** or **HARMFUL**.
**If BENIGN:**
- **Reasoning:** Briefly explain why the race is structurally tolerant to stale or torn reads.
- **Recommended Annotations:** Suggest appropriate annotations (e.g., `READ_ONCE()`, `data_race()`).
```
```json
{
"Race Summary": "Summarize the race between Function A and Function B on the shared Variable/Field. Consider Read/Write operations.",
"Synchronization Context": "Detail existing locks, if any. Explain how and if they protect the shared Variable/Field to avoid mutual exclusion. Highlight absence of protection",
"Final Classification": "HARMFUL",
"reasoning": "A torn write could lead to an inconsistent state of the shared variable, causing incorrect computations downstream and possible crashes",
"Recommended Annotations": "Annotate both read and write operations with `data_race()` and document their purpose"
}
```
|
| 129/1 |
2026/09/28 05:59 |
action |
format-explanation |
0m
Results: map[Explanation:### Race Summary
`mon_stop` (via `mon_reader_del`) vs `usbmon_urb_submit` (via
`usb_hcd_submit_urb`) on `struct usb_bus::monitored`.
---
### Synchronization Context
- **Writer (`mon_stop` / `mon_reader_del`)**: Invoked when a usbmon reader file
descriptor is closed (e.g. `/dev/usbmon0` in `mon_bin_release`). It executes
with `mbus->lock` (and `mon_lock`) held, clearing `ubus->monitored = 0` when the
number of active readers (`mbus->nreaders`) drops to 0.
- **Reader (`usbmon_urb_submit` / `usb_hcd_submit_urb`)**: Invoked during URB
submission in the USB core fast path. It performs a lockless read `if
(bus->monitored)` to avoid lock overhead when monitoring is inactive.
- **Mutual Exclusion**: None exists between `usb_hcd_submit_urb()` and
`mon_reader_del()`.
---
### Final Classification
**BENIGN**
---
### Reasoning
`bus->monitored` is an aligned 32-bit integer used as a fast-path bypass
heuristic flag:
1. **Tolerance to Stale Reads (`monitored == 0`)**: If the reader sees `0` while
monitoring is being activated or deactivated, it skips invoking
`mon_ops->urb_submit()`. At worst, an URB trace event is omitted right around
the moment when readers are subscribing/unsubscribing. Because reader
registration is inherently asynchronous with respect to URB submissions, this is
standard and harmless.
2. **Tolerance to Stale Reads (`monitored == 1`)**: If the reader sees `1` right
after the last reader closed the device, it invokes `mon_submit()`, which calls
`mon_bus_submit()`. `mon_bus_submit()` acquires `mbus->lock`, the exact same
spinlock held by `mon_reader_del()`. Under `mbus->lock`, the reader list
(`mbus->r_list`) is iterated safely:
- If `mon_bus_submit()` takes the lock after `mon_reader_del()`, `r_list` is
empty, so it does nothing and returns safely.
- The lifetime of `mbus` is governed by `mbus->ref` and is not destroyed by
reader deletion (only when the USB host controller is removed in
`mon_bus_remove()`).
3. **No Tearing or Fused Access Hazards**: `monitored` is an aligned 4-byte
scalar, so single-word reads and writes are atomic on all Linux architectures.
`usbmon_urb_submit()` is invoked once per URB submission (not in a polling/wait
loop), preventing compiler hoisting or livelock issues.
---
### Recommended Annotations
To silence KCSAN and explicitly document the lockless heuristic read/write:
- Use `READ_ONCE(bus->monitored)` in `usbmon_urb_submit()`,
`usbmon_urb_submit_error()`, and `usbmon_urb_complete()` in
`include/linux/usb/hcd.h`.
- Use `WRITE_ONCE(ubus->monitored, 0)` in `mon_stop()` and `mon_dissolve()`, and
`WRITE_ONCE(ubus->monitored, 1)` in `mon_reader_add()` and `mon_bus_add()` in
`drivers/usb/mon/mon_main.c`.]
|