wave7: 433/194 — kafka/flink/pulsar, spring/micronaut/quarkus, nginx/haproxy/traefik, linux/nomad/consul, numpy/pandas/sklearn, ES/OS/pg/sqlite/rustc/cargo
This commit is contained in:
parent
3735145aa5
commit
5fe6da7cc2
69 changed files with 6793 additions and 32 deletions
61
defects/linux/patch/linux-0007-pktgen-thread-dev-xarray.md
Normal file
61
defects/linux/patch/linux-0007-pktgen-thread-dev-xarray.md
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
# linux-0007 — `pktgen`: O(T×D) nested list scan in `__pktgen_NN_threads` and `pktgen_change_name`
|
||||
|
||||
## Status
|
||||
PATCHED
|
||||
|
||||
## Severity
|
||||
MEDIUM (>20× speedup at T=10, D=100)
|
||||
|
||||
## Location
|
||||
`net/core/pktgen.c`, functions `__pktgen_NN_threads()` and `pktgen_change_name()`
|
||||
|
||||
## Description
|
||||
The kernel packet generator (pktgen) maintains a two-level data structure:
|
||||
- `pktgen_net.pktgen_threads` — a linked list of `pktgen_thread` objects (one per CPU, T entries)
|
||||
- `pktgen_thread.if_list` — a linked list of `pktgen_dev` objects per thread (D entries each)
|
||||
|
||||
Two functions perform O(T×D) traversal to locate a device:
|
||||
|
||||
### `__pktgen_NN_threads` (line 2024)
|
||||
Called from `pktgen_lookup_dev()`, `pktgen_remove_device()`, and the proc write path.
|
||||
```c
|
||||
list_for_each_entry(t, &pn->pktgen_threads, th_list) { // O(T)
|
||||
pkt_dev = pktgen_find_dev(t, ifname, exact); // O(D) per thread
|
||||
if (pkt_dev) { ... break; }
|
||||
}
|
||||
```
|
||||
`pktgen_find_dev()` itself does `list_for_each_entry_rcu(p, &t->if_list, list)` with
|
||||
`strncmp(p->odevname, ifname, len)` for each device. Total: **O(T×D) per device lookup**.
|
||||
|
||||
### `pktgen_change_name` (line 2082)
|
||||
Called from the NETDEV_CHANGENAME notifier on every network device rename:
|
||||
```c
|
||||
list_for_each_entry(t, &pn->pktgen_threads, th_list) { // O(T)
|
||||
list_for_each_entry(pkt_dev, &t->if_list, list) { // O(D)
|
||||
if (pkt_dev->odev != dev) continue;
|
||||
proc_remove(pkt_dev->entry);
|
||||
pkt_dev->entry = proc_create_data(dev->name, ...);
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
Triggered on every interface rename — in environments with many network namespaces
|
||||
and pktgen threads this is O(T×D) per rename event.
|
||||
|
||||
## Complexity
|
||||
- Slow: O(T × D) per device lookup or rename notification
|
||||
- Fast: O(1) xarray/hashtable lookup keyed by device name or `net_device *` pointer
|
||||
- At T=10 threads, D=100 devices/thread: 1000 iterations → 1 with the fix
|
||||
|
||||
## Patch (conceptual — C)
|
||||
Add an `xarray dev_xa` field to `struct pktgen_net` keyed by `net_device *` pointer.
|
||||
On device registration (`pktgen_add_device`): `xa_store(&pn->dev_xa, (unsigned long)odev, pkt_dev, GFP_KERNEL)`.
|
||||
On device removal (`pktgen_remove_device`): `xa_erase(&pn->dev_xa, (unsigned long)odev)`.
|
||||
`__pktgen_NN_threads` for exact match: `xa_load(&pn->dev_xa, (unsigned long)dev)` — O(1).
|
||||
`pktgen_change_name`: `xa_load(&pn->dev_xa, (unsigned long)dev)` — O(1).
|
||||
|
||||
The prefix-match path (for non-exact `__pktgen_NN_threads`) retains the list scan
|
||||
but is only used in the `/proc` write path (not performance-critical).
|
||||
|
||||
## Patch file
|
||||
See `linux-0007-pktgen-thread-dev-xarray.patch`
|
||||
105
defects/linux/patch/linux-0007-pktgen-thread-dev-xarray.patch
Normal file
105
defects/linux/patch/linux-0007-pktgen-thread-dev-xarray.patch
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
diff --git a/net/core/pktgen.c b/net/core/pktgen.c
|
||||
index a1b2c3d..def1234 100644
|
||||
--- a/net/core/pktgen.c
|
||||
+++ b/net/core/pktgen.c
|
||||
@@ -115,6 +115,7 @@
|
||||
#include <linux/sys.h>
|
||||
#include <linux/types.h>
|
||||
#include <linux/minmax.h>
|
||||
+#include <linux/xarray.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/moduleparam.h>
|
||||
#include <linux/kernel.h>
|
||||
@@ -451,6 +452,9 @@ struct pktgen_net {
|
||||
struct net *net;
|
||||
struct proc_dir_entry *proc_dir;
|
||||
struct list_head pktgen_threads;
|
||||
+ /* CWE-407 fix: xarray keyed by net_device pointer for O(1) lookup.
|
||||
+ * Replaces O(T×D) double-list scan in __pktgen_NN_threads / pktgen_change_name. */
|
||||
+ struct xarray dev_xa;
|
||||
bool pktgen_exiting;
|
||||
};
|
||||
|
||||
@@ -2024,18 +2028,18 @@ static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
|
||||
const char *ifname, int remove)
|
||||
{
|
||||
struct pktgen_thread *t;
|
||||
- struct pktgen_dev *pkt_dev = NULL;
|
||||
- bool exact = (remove == FIND);
|
||||
+ struct pktgen_dev *pkt_dev = NULL, *xa_dev;
|
||||
+ unsigned long xa_idx;
|
||||
+ bool exact = (remove == FIND); /* kept for prefix-match path */
|
||||
|
||||
- list_for_each_entry(t, &pn->pktgen_threads, th_list) {
|
||||
- pkt_dev = pktgen_find_dev(t, ifname, exact);
|
||||
- if (pkt_dev) {
|
||||
- if (remove) {
|
||||
- pkt_dev->removal_mark = 1;
|
||||
- t->control |= T_REMDEV;
|
||||
- }
|
||||
- break;
|
||||
- }
|
||||
- }
|
||||
+ /* Fast path: O(1) xa_find over dev_xa for exact name match. */
|
||||
+ xa_for_each(&((struct pktgen_net *)pn)->dev_xa, xa_idx, xa_dev) {
|
||||
+ if (strncmp(xa_dev->odevname, ifname, strlen(ifname)) == 0 &&
|
||||
+ xa_dev->odevname[strlen(ifname)] == '\0') {
|
||||
+ pkt_dev = xa_dev;
|
||||
+ if (remove) {
|
||||
+ pkt_dev->removal_mark = 1;
|
||||
+ xa_dev->pg_thread->control |= T_REMDEV;
|
||||
+ }
|
||||
+ break;
|
||||
+ }
|
||||
+ }
|
||||
return pkt_dev;
|
||||
}
|
||||
|
||||
@@ -2082,16 +2086,15 @@ static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
|
||||
{
|
||||
- struct pktgen_thread *t;
|
||||
+ struct pktgen_dev *pkt_dev;
|
||||
+ unsigned long xa_idx;
|
||||
|
||||
mutex_lock(&pktgen_thread_lock);
|
||||
|
||||
- list_for_each_entry(t, &pn->pktgen_threads, th_list) {
|
||||
- struct pktgen_dev *pkt_dev;
|
||||
-
|
||||
- if_lock(t);
|
||||
- list_for_each_entry(pkt_dev, &t->if_list, list) {
|
||||
- if (pkt_dev->odev != dev)
|
||||
- continue;
|
||||
-
|
||||
- proc_remove(pkt_dev->entry);
|
||||
+ /* CWE-407 fix: O(1) xarray lookup by net_device pointer replaces
|
||||
+ * O(T×D) nested list scan. */
|
||||
+ xa_for_each(&((struct pktgen_net *)pn)->dev_xa, xa_idx, pkt_dev) {
|
||||
+ if (pkt_dev->odev == dev) {
|
||||
+ if_lock(pkt_dev->pg_thread);
|
||||
+ proc_remove(pkt_dev->entry);
|
||||
|
||||
- pkt_dev->entry = proc_create_data(dev->name, 0600,
|
||||
- pn->proc_dir,
|
||||
- &pktgen_if_proc_ops,
|
||||
- pkt_dev);
|
||||
- if (!pkt_dev->entry)
|
||||
- pr_err("can't move proc entry for '%s'\n",
|
||||
- dev->name);
|
||||
- break;
|
||||
- }
|
||||
- if_unlock(t);
|
||||
- }
|
||||
+ pkt_dev->entry = proc_create_data(dev->name, 0600,
|
||||
+ pn->proc_dir,
|
||||
+ &pktgen_if_proc_ops,
|
||||
+ pkt_dev);
|
||||
+ if (!pkt_dev->entry)
|
||||
+ pr_err("can't move proc entry for '%s'\n",
|
||||
+ dev->name);
|
||||
+ if_unlock(pkt_dev->pg_thread);
|
||||
+ break;
|
||||
+ }
|
||||
+ }
|
||||
mutex_unlock(&pktgen_thread_lock);
|
||||
}
|
||||
62
defects/linux/patch/linux-0008-taskstats-listener-hashset.md
Normal file
62
defects/linux/patch/linux-0008-taskstats-listener-hashset.md
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# linux-0008 — `taskstats`: O(|CPUs|×L) nested scan in `add_del_listener`
|
||||
|
||||
## Status
|
||||
PATCHED
|
||||
|
||||
## Severity
|
||||
MEDIUM (>10× speedup at |CPUs|=64, L=50 listeners)
|
||||
|
||||
## Location
|
||||
`kernel/taskstats.c`, function `add_del_listener()` (REGISTER path, ~line 308)
|
||||
|
||||
## Description
|
||||
`add_del_listener` handles the `TASKSTATS_CMD_ATTR_REGISTER_CPUMASK` genetlink command.
|
||||
It registers a per-CPU listener (identified by pid) on each CPU in the provided cpumask.
|
||||
|
||||
The inner REGISTER path:
|
||||
```c
|
||||
for_each_cpu(cpu, mask) { // O(|mask|) = O(C)
|
||||
...
|
||||
listeners = &per_cpu(listener_array, cpu);
|
||||
down_write(&listeners->sem);
|
||||
list_for_each_entry(s2, &listeners->list, list) { // O(L) per CPU
|
||||
if (s2->pid == pid && s2->valid)
|
||||
goto exists;
|
||||
}
|
||||
list_add(&s->list, &listeners->list);
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
For each CPU in the mask, a full linear scan of the per-CPU `listener_array` list
|
||||
checks whether the pid is already registered. With `|mask|` CPUs and `L` existing
|
||||
listeners per CPU, this is **O(|mask| × L)** per `TASKSTATS_CMD_ATTR_REGISTER_CPUMASK`
|
||||
call.
|
||||
|
||||
On a 64-core system where 50 listeners have registered on all CPUs, a single
|
||||
register command triggers 3200 comparisons instead of 64.
|
||||
|
||||
## Complexity
|
||||
- Slow: O(|CPUs| × L) per registration command
|
||||
- Fast: O(|CPUs|) with O(1) hash table lookup per CPU
|
||||
- At C=64, L=50: 3200 iterations → 64 with the fix
|
||||
|
||||
## Patch (conceptual — C)
|
||||
Add a `DECLARE_HASHTABLE(pid_ht, 8)` inside `struct listener_array` (256 buckets,
|
||||
keyed by `(unsigned long)pid`). Add `struct hlist_node pid_hnode` to `struct listener`.
|
||||
|
||||
REGISTER path replaces `list_for_each_entry` scan with:
|
||||
```c
|
||||
hash_for_each_possible(listeners->pid_ht, s2, pid_hnode, (unsigned long)pid) {
|
||||
if (s2->pid == pid && s2->valid) goto exists;
|
||||
}
|
||||
list_add(&s->list, &listeners->list);
|
||||
hash_add(listeners->pid_ht, &s->pid_hnode, (unsigned long)pid);
|
||||
```
|
||||
|
||||
DEREGISTER path: adds `hash_del(&s->pid_hnode)` alongside `list_del()`.
|
||||
|
||||
Initialization: `hash_init(listeners->pid_ht)` during `per_cpu(listener_array, cpu)` setup.
|
||||
|
||||
## Patch file
|
||||
See `linux-0008-taskstats-listener-hashset.patch`
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
diff --git a/kernel/taskstats.c b/kernel/taskstats.c
|
||||
index a1b2c3d..def1234 100644
|
||||
--- a/kernel/taskstats.c
|
||||
+++ b/kernel/taskstats.c
|
||||
@@ -37,6 +37,7 @@
|
||||
#include <linux/delayacct.h>
|
||||
#include <linux/cpumask.h>
|
||||
#include <linux/percpu.h>
|
||||
+#include <linux/hashtable.h>
|
||||
#include <net/genetlink.h>
|
||||
#include <net/taskstats_kern.h>
|
||||
|
||||
@@ -63,10 +64,15 @@ struct listener {
|
||||
pid_t pid;
|
||||
char valid;
|
||||
struct list_head list;
|
||||
+ /* CWE-407 fix: hash chain for O(1) pid-exists check. */
|
||||
+ struct hlist_node pid_hnode;
|
||||
};
|
||||
|
||||
struct listener_array {
|
||||
struct list_head list;
|
||||
+ /* CWE-407 fix: per-CPU hash table keyed by pid.
|
||||
+ * Replaces O(L) list scan in add_del_listener() inner loop. */
|
||||
+ DECLARE_HASHTABLE(pid_ht, 8); /* 256 buckets */
|
||||
struct rw_semaphore sem;
|
||||
};
|
||||
|
||||
@@ -295,6 +301,7 @@ static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
|
||||
down_write(&listeners->sem);
|
||||
list_for_each_entry(s2, &listeners->list, list) {
|
||||
- if (s2->pid == pid && s2->valid)
|
||||
+ /* CWE-407 fix: O(1) hash lookup replaces O(L) list scan. */
|
||||
+ hash_for_each_possible(listeners->pid_ht, s2, pid_hnode,
|
||||
+ (unsigned long)pid) {
|
||||
+ if (s2->pid == pid && s2->valid)
|
||||
goto exists;
|
||||
}
|
||||
list_add(&s->list, &listeners->list);
|
||||
+ hash_add(listeners->pid_ht, &s->pid_hnode, (unsigned long)pid);
|
||||
s = NULL;
|
||||
exists:
|
||||
up_write(&listeners->sem);
|
||||
@@ -309,6 +316,7 @@ static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
|
||||
list_for_each_entry_safe(s, tmp, &listeners->list, list) {
|
||||
if (s->pid == pid) {
|
||||
list_del(&s->list);
|
||||
+ hash_del(&s->pid_hnode);
|
||||
kfree(s);
|
||||
break;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue