| 0/0 |
2026/11/23 10:50 |
flow |
bisect-cause |
15m
Results: map[BisectReasoning:### Bisection Result
**Bug-introducing commit:** `86f1152b117a404229fd6f08ec3faca779f37b92` (`"dm: allow active and inactive tables to share dm_devs"`)
---
### Root Cause Verification
In `drivers/md/dm-thin.c:pool_ctr()`:
1. `pool_ctr()` only checks `if (!strcmp(argv[0], argv[1]))` to reject identical metadata and data devices. When a user passes two distinct strings resolving to the same underlying `dev_t` (e.g., `"/dev/ram1"` and `"1:1"`) along with the `read_only` feature:
2. `pool_ctr()` first calls `dm_get_device(ti, argv[0], BLK_OPEN_READ, &metadata_dev)`.
In `drivers/md/dm-table.c:dm_get_device()`, this allocates a `struct dm_dev_internal *dd`, calls `dm_get_table_device()` which allocates a `struct table_device *td` (`count = 1`), sets `dd->dm_dev = &td->dm_dev`, and returns `*result = dd->dm_dev` into `metadata_dev`.
3. Next, `pool_ctr()` calls `dm_get_device(ti, argv[1], BLK_OPEN_READ | BLK_OPEN_WRITE, &data_dev)`.
Because both paths resolve to the same `dev_t`, `find_device(&t->devices, dev)` finds the same `dd`. Seeing that `dd->dm_dev->mode != (mode | dd->dm_dev->mode)`, `dm_get_device()` invokes `upgrade_mode(dd, mode, t->md)`.
4. In `drivers/md/dm-table.c:upgrade_mode()`:
```c
old_dev = dd->dm_dev;
r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
dd->dm_dev->mode | new_mode, &new_dev);
if (r)
return r;
dd->dm_dev = new_dev;
dm_put_table_device(md, old_dev);
```
`dm_get_table_device()` opens a *new* `struct table_device` (`new_dev`) because `find_table_device()` matches on both `dev` and `mode`. Then `dm_put_table_device(md, old_dev)` drops `td->count` to `0`, calls `close_table_device()` (which sets `td->dm_dev.bdev = NULL`), and frees `td` via `kfree(td)`.
5. `metadata_dev` in `pool_ctr()` still points to `old_dev` (`&td->dm_dev`), which is now freed memory with `bdev == NULL`.
6. `pool_ctr()` passes `metadata_dev->bdev` to `__pool_find()` -> `pool_create()` -> `dm_pool_metadata_open()` -> `dm_block_manager_create()` -> `dm_bufio_client_create()`, where `MAJOR(bdev->bd_dev)` reads `bdev->bd_dev` at offset `0x24` from `NULL`, causing the observed kernel NULL pointer dereference.
---
### Git History Traced and Commits Examined
1. **`570b9d968bf9` (`"dm table: fix upgrade mode race"`, Apr 2009)**
- Before `86f1152b117a`, `struct dm_dev_internal` embedded `struct dm_dev dm_dev` directly (`&dd->dm_dev`).
- `upgrade_mode()` opened a local `dd_new`, updated `dd->dm_dev.mode |= new_mode` in place, and called `close_dev(&dd_old, md)` on a stack copy `dd_old`.
- Crucially, `dd` remained allocated at the same address and `dd->dm_dev.bdev` remained non-NULL and valid, so any `struct dm_dev *` previously returned by `dm_get_device()` stayed valid across `upgrade_mode()`.
2. **`5d0db96d13a4` (`"dm thin: open dev read only when possible"`, May 2013)**
- Changed `pool_ctr()` to open `metadata_dev` with `FMODE_READ` instead of `FMODE_READ | FMODE_WRITE` when `pf.mode == PM_READ_ONLY`, while `data_dev` continued to be opened with `FMODE_READ | FMODE_WRITE`.
- Ruled out as the introducing commit: at `5d0db96d13a4` (prior to `86f1152b117a`), calling `dm_get_device()` on the same `dev_t` first RO and then RW triggered `upgrade_mode()` in-place on `dd`, keeping `&dd->dm_dev` alive and `metadata_dev->bdev` valid—no UAF or NULL pointer dereference occurred.
3. **`86f1152b117a` (`"dm: allow active and inactive tables to share dm_devs"`, Aug 2014)** — **Introducing Commit**
- Changed `struct dm_dev_internal::dm_dev` from an embedded struct to a pointer `struct dm_dev *dm_dev` pointing into `struct table_device` (`md->table_devices`), and changed `upgrade_mode()` to allocate a new `table_device` (`new_dev`), overwrite `dd->dm_dev = new_dev`, and call `dm_put_table_device(md, old_dev)` which sets `old_dev->bdev = NULL` and frees `td`.
- This broke the invariant relied upon by `dm_get_device()` callers (including `pool_ctr()`, `thin_ctr()`, and `snapshot_ctr()`): any `struct dm_dev *` previously returned by `dm_get_device()` for the same `dev_t` becomes a dangling pointer to freed memory (with `bdev == NULL`) as soon as `upgrade_mode()` runs on a subsequent `dm_get_device()` call.
4. **`4df2bf466a9c` (`"dm snapshot: disallow the COW and origin devices from being identical"`, Feb 2016)** & **`26110d5afe81` (`"dm-snap: simplify the origin_dev == cow_dev check in snapshot_ctr"`, May 2023)**
- Observed the same `upgrade_mode()` dangling-pointer bug in `dm-snap.c:snapshot_ctr()`. `4df2bf466a9c` checked `dm_get_dev_t(cow_path) == origin_dev` before the second `dm_get_device()`, and `26110d5afe81` later moved the check after `dm_get_device()` (re-exposing `snapshot_ctr()`).
5. **`70de2cbda8a5` (`"dm thin: add sanity checks to thin-pool and external snapshot creation"`, Jan 2019)**
- Explicitly described the `upgrade_mode()` UAF (`"upgrade_mode() will alloc a new 'dm_dev' and free the old one, which may be referenced by a previous caller"`), and attempted to fix it in `pool_ctr()` and `thin_ctr()` by adding `if (!strcmp(argv[0], argv[1]))` and `if (!strcmp(argv[0], argv[2]))`.
- Ruled out as the introducing commit: `70de2cbda8a5` was an incomplete fix attempt that only blocked identical strings and left different strings resolving to the same `dev_t` vulnerable.
---
### Conclusion
Commit **`86f1152b117a404229fd6f08ec3faca779f37b92`** (`"dm: allow active and inactive tables to share dm_devs"`) introduced the bug by making `upgrade_mode()` free `old_dev` (`struct table_device`) and clear `old_dev->bdev = NULL` while earlier `dm_get_device()` callers in the same table (such as `metadata_dev` in `pool_ctr()`) still hold pointers to `old_dev`. Commits:[map[AuthorEmail:bmarzins@redhat.com AuthorName:Benjamin Marzinski Date:2014-08-13T13:53:43-05:00 Hash:86f1152b117a404229fd6f08ec3faca779f37b92 Recipients:[map[Email:bmarzins@redhat.com Name: To:true] map[Email:snitzer@redhat.com Name: To:true]] Title:dm: allow active and inactive tables to share dm_devs]] IsRelease:false KernelCommit:abf8c33470f78e2226ee3dcdc69004660020ab47]
|
| 1/1 |
2026/11/23 10:50 |
action |
use-crash-commit |
0m
Results: map[KernelCommit:abf8c33470f78e2226ee3dcdc69004660020ab47]
|
| 2/1 |
2026/11/23 10:50 |
action |
syz-repro-to-c-repro |
0m
Results: map[SimplifiedCRepro:// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <endian.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#ifndef __NR_ioctl
#define __NR_ioctl 54
#endif
#ifndef __NR_mmap
#define __NR_mmap 192
#endif
#ifndef __NR_openat
#define __NR_openat 322
#endif
#undef __NR_mmap
#define __NR_mmap __NR_mmap2
uint64_t r[2] = {0xffffffffffffffff, 0x0};
int main(void)
{
syscall(__NR_mmap, /*addr=*/0x1ffff000, /*len=*/0x1000, /*prot=*/0, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
syscall(__NR_mmap, /*addr=*/0x20000000, /*len=*/0x1000000, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/7, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
syscall(__NR_mmap, /*addr=*/0x21000000, /*len=*/0x1000, /*prot=*/0, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
const char* reason;
(void)reason;
intptr_t res = 0;
if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {}
// openat$dm_control arguments: [
// fd: const = 0xffffffffffffff9c (4 bytes)
// file: ptr[in, buffer] {
// buffer: {2f 64 65 76 2f 6d 61 70 70 65 72 2f 63 6f 6e 74 72 6f 6c 00} (length 0x14)
// }
// flags: open_flags = 0x2 (4 bytes)
// mode: const = 0x0 (2 bytes)
// ]
// returns fd_dm_ctl
memcpy((void*)0x20000180, "/dev/mapper/control\000", 20);
res = syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000180, /*flags=O_RDWR*/2, /*mode=*/0);
if (res != -1)
r[0] = res;
// ioctl$DM_DEV_CREATE arguments: [
// fd: fd_dm_ctl (resource)
// cmd: const = 0xc138fd03 (4 bytes)
// arg: ptr[inout, dm_ioctl_t[dm_id_create, void, const[0, int32], const[0, int32]]] {
// dm_ioctl_t[dm_id_create, void, const[0, int32], const[0, int32]] {
// version_major: const = 0x4 (4 bytes)
// version_minor: const = 0x0 (4 bytes)
// version_patch: const = 0x0 (4 bytes)
// data_size: bytesize = 0x138 (4 bytes)
// data_start: offsetof = 0x138 (4 bytes)
// target_count: const = 0x0 (4 bytes)
// open_count: const = 0x0 (4 bytes)
// flags: dm_ioctl_flags = 0x8800 (4 bytes)
// event_nr: const = 0x0 (4 bytes)
// padding: const = 0x0 (4 bytes)
// id: dm_id_create {
// dev: dm_dev_number (resource)
// name: dm_dev_name {
// prefix: buffer: {73 79 7a} (length 0x3)
// proc: proc = 0x0 (1 bytes)
// slot: buffer: {33 00} (length 0x2)
// pad = 0x0 (122 bytes)
// }
// uuid: union dm_dev_uuid_opt {
// uuid: dm_dev_uuid {
// prefix: buffer: {43 52 50 2d 73 79 7a} (length 0x7)
// proc: proc = 0x0 (1 bytes)
// slot: buffer: {30 00} (length 0x2)
// pad = 0x0 (126 bytes)
// }
// }
// }
// data: buffer: {} (length 0x0)
// }
// }
// ]
*(uint32_t*)0x200001c0 = 4;
*(uint32_t*)0x200001c4 = 0;
*(uint32_t*)0x200001c8 = 0;
*(uint32_t*)0x200001cc = 0x138;
*(uint32_t*)0x200001d0 = 0x138;
*(uint32_t*)0x200001d4 = 0;
*(uint32_t*)0x200001d8 = 0;
*(uint32_t*)0x200001dc = 0x8800;
*(uint32_t*)0x200001e0 = 0;
*(uint32_t*)0x200001e4 = 0;
memcpy((void*)0x200001f0, "syz", 3);
*(uint8_t*)0x200001f3 = 0x61;
memcpy((void*)0x200001f4, "3\000", 2);
memcpy((void*)0x20000270, "CRP-syz", 7);
*(uint8_t*)0x20000277 = 0x61;
memcpy((void*)0x20000278, "0\000", 2);
res = syscall(__NR_ioctl, /*fd=*/(intptr_t)r[0], /*cmd=*/0xc138fd03, /*arg=*/0x200001c0);
if (res != -1)
r[1] = *(uint64_t*)0x200001e8;
// ioctl$DM_TABLE_LOAD arguments: [
// fd: fd_dm_ctl (resource)
// cmd: const = 0xc138fd09 (4 bytes)
// arg: ptr[inout, dm_ioctl_t[dm_id_lookup, array[dm_target, 1], len[data, int32], const[0, int32]]] {
// dm_ioctl_t[dm_id_lookup, array[dm_target, 1], len[data, int32], const[0, int32]] {
// version_major: const = 0x4 (4 bytes)
// version_minor: const = 0x0 (4 bytes)
// version_patch: const = 0x0 (4 bytes)
// data_size: bytesize = 0x1a8 (4 bytes)
// data_start: offsetof = 0x138 (4 bytes)
// target_count: len = 0x1 (4 bytes)
// open_count: const = 0x0 (4 bytes)
// flags: dm_ioctl_flags = 0x20000 (4 bytes)
// event_nr: const = 0x0 (4 bytes)
// padding: const = 0x0 (4 bytes)
// id: union dm_id_lookup {
// by_dev: dm_id_by_dev {
// dev: dm_dev_number (resource)
// pad = 0x0 (264 bytes)
// }
// }
// data: array[dm_target] {
// union dm_target {
// thin_pool: dm_target_spec_t[dm_target_thin_pool, dm_thin_pool_params] {
// sector_start: const = 0x0 (8 bytes)
// length: dm_lengths = 0x800 (8 bytes)
// status: const = 0x0 (4 bytes)
// next: bytesize = 0x70 (4 bytes)
// target_type: buffer: {74 68 69 6e 2d 70 6f 6f 6c 00 00 00 00 00 00 00} (length 0x10)
// params: dm_thin_pool_params {
// meta: union dm_dev {
// ram: dm_dev_proc_t["/dev/ram", 2] {
// prefix: buffer: {2f 64 65 76 2f 72 61 6d} (length 0x8)
// id: proc = 0x1 (1 bytes)
// }
// }
// sp0: const = 0x20 (1 bytes)
// data: union dm_dev {
// ram_num: dm_ram_num {
// major: buffer: {31 3a} (length 0x2)
// minor: proc = 0x1 (20 bytes)
// }
// }
// sp1: const = 0x20 (1 bytes)
// bsize: buffer: {32 30 34 38} (length 0x4)
// sp2: const = 0x20 (1 bytes)
// lwm: int8 = 0x14 (20 bytes)
// feat: buffer: {20 31 20 72 65 61 64 5f 6f 6e 6c 79} (length 0xc)
// }
// z: const = 0x0 (1 bytes)
// }
// }
// }
// }
// }
// ]
*(uint32_t*)0x200006c0 = 4;
*(uint32_t*)0x200006c4 = 0;
*(uint32_t*)0x200006c8 = 0;
*(uint32_t*)0x200006cc = 0x1a8;
*(uint32_t*)0x200006d0 = 0x138;
*(uint32_t*)0x200006d4 = 1;
*(uint32_t*)0x200006d8 = 0;
*(uint32_t*)0x200006dc = 0x20000;
*(uint32_t*)0x200006e0 = 0;
*(uint32_t*)0x200006e4 = 0;
*(uint64_t*)0x200006e8 = r[1];
*(uint64_t*)0x200007f8 = 0;
*(uint64_t*)0x20000800 = 0x800;
*(uint32_t*)0x20000808 = 0;
*(uint32_t*)0x2000080c = 0x70;
memcpy((void*)0x20000810, "thin-pool\000\000\000\000\000\000\000", 16);
memcpy((void*)0x20000820, "/dev/ram", 8);
*(uint8_t*)0x20000828 = 0x31;
*(uint8_t*)0x20000829 = 0x20;
memcpy((void*)0x2000082a, "1:", 2);
sprintf((char*)0x2000082c, "%020llu", (long long)1);
*(uint8_t*)0x20000840 = 0x20;
memcpy((void*)0x20000841, "2048", 4);
*(uint8_t*)0x20000845 = 0x20;
sprintf((char*)0x20000846, "%020llu", (long long)0x14);
memcpy((void*)0x2000085a, " 1 read_only", 12);
*(uint8_t*)0x20000866 = 0;
syscall(__NR_ioctl, /*fd=*/(intptr_t)r[0], /*cmd=*/0xc138fd09, /*arg=*/0x200006c0);
return 0;
}
]
|
| 3/1 |
2026/11/23 10:50 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/app/workdir/cache/src/a9773939f99243a98267f39f9b75442796703660/data]
|
| 4/1 |
2026/11/23 10:51 |
action |
kernel-builder |
5m
Results: map[KernelObj:/app/workdir/cache/build/d309c2585cc0745044ca1276b24a03dd0c39528b/data]
|
| 5/1 |
2026/11/23 10:57 |
action |
codesearch-prepare |
5m
Results: map[Index:codesearch-index]
|
| 6/1 |
2026/11/23 11:02 |
agent |
cause-finder |
4m
Model:
Results: map[CandidateCommits:[86f1152b117a404229fd6f08ec3faca779f37b92]]
Instruction:
You are an experienced Linux kernel developer tasked with identifying the commit
that introduced a kernel bug (cause bisection).
Your investigation strategy:
1. Establish the root cause first.
A root cause analysis of the bug is provided; verify its key claims against the actual
source code before relying on it.
Identify the specific functions, structs, invariants, or lines of code where the buggy
logic resides.
2. Use git tools (git-blame, git-log, git-show) to trace the history
of the affected functions and lines.
3. Trace past pure refactorings, code movements, formatting changes, or variable renames
to find the commit that actually introduced the buggy logic or broke an existing invariant.
4. Do NOT blame commits that merely added debugging or sanitizer checks (WARN_ON, BUG_ON,
KASAN/KMSAN/LOCKDEP annotations) that exposed an older bug, unless the check itself is
incorrect and is the bug.
5. Do NOT return a merge commit unless the bug was introduced by the merge conflict
resolution itself; otherwise identify the underlying non-merge commit.
6. Verify your hypothesis by inspecting the full commit diff with git-show and
confirming how the change introduced the exact condition described in the root cause.
Output rules:
- Returning a SINGLE commit in CandidateCommits (a 1-element list) is the expected norm
and is strongly preferred. A single commit gives maintainers a clear, directly actionable
result, whereas returning extra commits dilutes the report and notifies additional commit
authors unnecessarily.
- Once you identify the primary commit that introduced (or fixed) the bug, return ONLY that
commit in CandidateCommits. Do NOT include secondary or "contributing" commits such as
earlier background/foundation commits, preparatory or follow-up commits in the same
series, later refactorings, incomplete fix attempts, or commits that merely exposed or
enabled the code path (e.g. new callers, default config changes, or sanitizer/debug checks).
Mention those related commits in your text reply for context instead.
- Returning more than one commit (at most 3) is the rare exception, not the norm. Only return
multiple commits when:
1. There is genuine, unresolvable ambiguity between 2-3 equally plausible candidates after
thorough investigation, OR
2. The bug was strictly and inseparably introduced (or fixed) by 2-3 commits together, with
no single primary commit.
In those rare cases, order CandidateCommits by relevance with the most important/likely
commit FIRST.
- If you cannot determine the commit with confidence, return an empty CandidateCommits list
rather than guessing.
- Your text reply must provide a clear bisection log explaining the root cause of the bug,
the git history you traced, the commits you examined (with their hashes and titles), why
you ruled out other commits, and how you reached your conclusion.
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 crash is:
8<--- cut here ---
Unable to handle kernel NULL pointer dereference at virtual address 00000024 when read
[00000024] *pgd=86ce2003, *pmd=df568003
Internal error: Oops: 205 [#1] SMP ARM
Modules linked in:
CPU: 0 UID: 0 PID: 4130 Comm: syz.2.17 Not tainted syzkaller #0 PREEMPT
Hardware name: ARM-Versatile Express
PC is at dm_bufio_client_create+0x298/0x5fc drivers/md/dm-bufio.c:2585
LR is at __raw_spin_unlock_irqrestore include/linux/spinlock_api_smp.h:211 [inline]
LR is at _raw_spin_unlock_irqrestore+0x30/0x54 kernel/locking/spinlock.c:221
pc : [<812f4044>] lr : [<81c19680>] psr: 60000013
sp : df9f1b30 ip : df9f1aa8 fp : df9f1bd4
r10: 00000000 r9 : df9f1b48 r8 : 837359c4
r7 : 83735984 r6 : 8633b638 r5 : 00000000 r4 : 8633b600
r3 : 00000000 r2 : 00000000 r1 : ffc00000 r0 : 00000000
Flags: nZCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user
Control: 30c5387d Table: 86a96cc0 DAC: fffffffd
Register r0 information: NULL pointer
Register r1 information: non-paged memory
Register r2 information: NULL pointer
Register r3 information: NULL pointer
Register r4 information: slab kmalloc-512 start 8633b600 pointer offset 0 size 512
Register r5 information: NULL pointer
Register r6 information: slab kmalloc-512 start 8633b600 pointer offset 56 size 512
Register r7 information: non-slab/vmalloc memory
Register r8 information: non-slab/vmalloc memory
Register r9 information: 2-page vmalloc region starting at 0xdf9f0000 allocated at kernel_clone+0xc4/0x440 kernel/fork.c:2784
Register r10 information: NULL pointer
Register r11 information: 2-page vmalloc region starting at 0xdf9f0000 allocated at kernel_clone+0xc4/0x440 kernel/fork.c:2784
Register r12 information: 2-page vmalloc region starting at 0xdf9f0000 allocated at kernel_clone+0xc4/0x440 kernel/fork.c:2784
Process syz.2.17 (pid: 4130, stack limit = 0xdf9f0000)
Stack: (0xdf9f1b30 to 0xdf9f2000)
1b20: 00000001 00000000 00001fff 00000006
1b40: 00000000 00000008 00000008 00000000 00000000 00000000 00000000 00000000
1b60: 00000000 625f6d64 6f696675 6675625f 2d726566 00312d38 00000000 00000000
1b80: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
1ba0: 00000000 5d3b91d7 80579764 863f8c80 00000000 00001000 00000006 00000000
1bc0: 00000000 00000800 df9f1c0c df9f1bd8 8130d108 812f3db8 8130d020 8130d03c
1be0: 00000000 868f1040 00000000 868f0c00 00000000 00000000 00000000 00000000
1c00: df9f1c34 df9f1c10 81329660 8130d0a4 868f1040 8276076c 826a9678 00000000
1c20: 83735a70 00000000 df9f1cb4 df9f1c38 81322274 813295d4 00000000 00000000
1c40: 835c30a0 863f87c0 8417c000 86852000 83735a60 00000003 863f894c 00000800
1c60: 863f87cc 00000000 00000014 00000000 812e5f44 00000000 00000007 00000000
1c80: 863f8858 5d3b91d7 00000000 863c7180 868f1040 00000000 00000000 00000800
1ca0: 00000000 00000000 df9f1d0c df9f1cb8 812e623c 81321df0 00000008 df9f1d2c
1cc0: 868f1040 00000000 85b2db60 85b2db50 df9f1d0c 00000006 863f8840 5d3b91d7
1ce0: 86852000 0000006f 85b2db38 85b2db60 85b2da00 00000000 85b2dba8 86852000
1d00: df9f1d64 df9f1d10 812ec884 812e60b0 00000800 00000000 85b2db60 85b2da00
1d20: 812eabcc 863c7180 00000000 863c7180 00000000 5d3b91d7 df9f1d54 812ec740
1d40: 85b2da00 200006c0 863f0e80 00000009 000001a8 000001a8 df9f1f04 df9f1d68
1d60: 812eaa04 812ec74c df9f1e74 81ee19d4 8259d20c 8088542c 81ee1718 00000077
1d80: 85b2db31 b5403587 00000000 00000000 00000001 00000000 00000004 00000032
1da0: 00000000 000001a8 00000138 00000001 00000000 00020000 00000000 00000000
1dc0: 0000f800 00000000 00000000 00000000 00000000 00000000 00000000 00000000
1de0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
1e00: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
1e20: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
1e40: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
1e60: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
1e80: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
1ea0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
1ec0: 00000000 00000000 00000000 00000000 00000003 5d3b91d7 df9f1ef4 00000000
1ee0: 863f0e80 c138fd09 200006c0 863f0e80 00000003 84d3be80 df9f1f14 df9f1f08
1f00: 812eaf64 812ea79c df9f1fa4 df9f1f18 805f2e84 812eaf60 df9f1f4c df9f1f28
1f20: 8035d380 8036088c ffffffff 81c0c4c0 df9f1f54 df9f1f40 8025cbac 00000001
1f40: df9f1fa4 df9f1f50 8035dc54 8035d1f8 00000000 00000001 ffffffff 8341a10c
1f60: 00000000 00000000 00000000 00000000 00000000 5d3b91d7 df9f1fac 00000000
1f80: 00000000 00356310 00000036 8020029c 84d3be80 00000036 00000000 df9f1fa8
1fa0: 80200060 805f2c4c 00000000 00000000 00000003 c138fd09 200006c0 00000000
1fc0: 00000000 00000000 00356310 00000036 003562d8 00000000 00000008 00000000
1fe0: 7ecaa7a0 7ecaa790 0001903c 00130cf0 60000010 00000003 00000000 00000000
Call trace:
[<812f3dac>] (dm_bufio_client_create) from [<8130d108>] (dm_block_manager_create+0x70/0xb0 drivers/md/persistent-data/dm-block-manager.c:397)
r10:00000800 r9:00000000 r8:00000000 r7:00000006 r6:00001000 r5:00000000
r4:863f8c80
[<8130d098>] (dm_block_manager_create) from [<81329660>] (__create_persistent_data_objects drivers/md/dm-thin-metadata.c:791 [inline])
[<8130d098>] (dm_block_manager_create) from [<81329660>] (dm_pool_metadata_open+0x98/0x15c drivers/md/dm-thin-metadata.c:980)
r8:00000000 r7:00000000 r6:00000000 r5:00000000 r4:868f0c00
[<813295c8>] (dm_pool_metadata_open) from [<81322274>] (pool_create drivers/md/dm-thin.c:2946 [inline])
[<813295c8>] (dm_pool_metadata_open) from [<81322274>] (__pool_find drivers/md/dm-thin.c:3113 [inline])
[<813295c8>] (dm_pool_metadata_open) from [<81322274>] (pool_ctr+0x490/0xa64 drivers/md/dm-thin.c:3363)
r9:00000000 r8:83735a70 r7:00000000 r6:826a9678 r5:8276076c r4:868f1040
[<81321de4>] (pool_ctr) from [<812e623c>] (dm_table_add_target+0x198/0x3bc drivers/md/dm-table.c:749)
r10:00000000 r9:00000000 r8:00000800 r7:00000000 r6:00000000 r5:868f1040
r4:863c7180
[<812e60a4>] (dm_table_add_target) from [<812ec884>] (populate_table drivers/md/dm-ioctl.c:1602 [inline])
[<812e60a4>] (dm_table_add_target) from [<812ec884>] (table_load+0x144/0x3d4 drivers/md/dm-ioctl.c:1645)
r10:86852000 r9:85b2dba8 r8:00000000 r7:85b2da00 r6:85b2db60 r5:85b2db38
r4:0000006f
[<812ec740>] (table_load) from [<812eaa04>] (ctl_ioctl+0x274/0x7c4 drivers/md/dm-ioctl.c:2221)
r10:000001a8 r9:000001a8 r8:00000009 r7:863f0e80 r6:200006c0 r5:85b2da00
r4:812ec740
[<812ea790>] (ctl_ioctl) from [<812eaf64>] (dm_ctl_ioctl+0x10/0x14 drivers/md/dm-ioctl.c:2243)
r10:84d3be80 r9:00000003 r8:863f0e80 r7:200006c0 r6:c138fd09 r5:863f0e80
r4:00000000
[<812eaf54>] (dm_ctl_ioctl) from [<805f2e84>] (vfs_ioctl fs/ioctl.c:51 [inline])
[<812eaf54>] (dm_ctl_ioctl) from [<805f2e84>] (do_vfs_ioctl fs/ioctl.c:551 [inline])
[<812eaf54>] (dm_ctl_ioctl) from [<805f2e84>] (__do_sys_ioctl fs/ioctl.c:595 [inline])
[<812eaf54>] (dm_ctl_ioctl) from [<805f2e84>] (sys_ioctl+0x244/0xb5c fs/ioctl.c:583)
[<805f2c40>] (sys_ioctl) from [<80200060>] (ret_fast_syscall+0x0/0x1c arch/arm/mm/proc-v7.S:67)
Exception stack(0xdf9f1fa8 to 0xdf9f1ff0)
1fa0: 00000000 00000000 00000003 c138fd09 200006c0 00000000
1fc0: 00000000 00000000 00356310 00000036 003562d8 00000000 00000008 00000000
1fe0: 7ecaa7a0 7ecaa790 0001903c 00130cf0
r10:00000036 r9:84d3be80 r8:8020029c r7:00000036 r6:00356310 r5:00000000
r4:00000000
Code: e3a05000 e3a01000 e34f1fc0 e1a00005 (e5932024)
---[ end trace 0000000000000000 ]---
----------------
Code disassembly (best guess):
0: e3a05000 mov r5, #0
4: e3a01000 mov r1, #0
8: e34f1fc0 movt r1, #65472 @ 0xffc0
c: e1a00005 mov r0, r5
* 10: e5932024 ldr r2, [r3, #36] @ 0x24 <-- trapping instruction
The following C code is a draft of the syscall sequence that triggers the kernel crash. Keep in mind
that it may lack the precise threading, sandboxing, and some arguments of a working reproducer:
// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <endian.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#ifndef __NR_ioctl
#define __NR_ioctl 54
#endif
#ifndef __NR_mmap
#define __NR_mmap 192
#endif
#ifndef __NR_openat
#define __NR_openat 322
#endif
#undef __NR_mmap
#define __NR_mmap __NR_mmap2
uint64_t r[2] = {0xffffffffffffffff, 0x0};
int main(void)
{
syscall(__NR_mmap, /*addr=*/0x1ffff000, /*len=*/0x1000, /*prot=*/0, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
syscall(__NR_mmap, /*addr=*/0x20000000, /*len=*/0x1000000, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/7, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
syscall(__NR_mmap, /*addr=*/0x21000000, /*len=*/0x1000, /*prot=*/0, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32, /*fd=*/(intptr_t)-1, /*offset=*/0);
const char* reason;
(void)reason;
intptr_t res = 0;
if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {}
// openat$dm_control arguments: [
// fd: const = 0xffffffffffffff9c (4 bytes)
// file: ptr[in, buffer] {
// buffer: {2f 64 65 76 2f 6d 61 70 70 65 72 2f 63 6f 6e 74 72 6f 6c 00} (length 0x14)
// }
// flags: open_flags = 0x2 (4 bytes)
// mode: const = 0x0 (2 bytes)
// ]
// returns fd_dm_ctl
memcpy((void*)0x20000180, "/dev/mapper/control\000", 20);
res = syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000180, /*flags=O_RDWR*/2, /*mode=*/0);
if (res != -1)
r[0] = res;
// ioctl$DM_DEV_CREATE arguments: [
// fd: fd_dm_ctl (resource)
// cmd: const = 0xc138fd03 (4 bytes)
// arg: ptr[inout, dm_ioctl_t[dm_id_create, void, const[0, int32], const[0, int32]]] {
// dm_ioctl_t[dm_id_create, void, const[0, int32], const[0, int32]] {
// version_major: const = 0x4 (4 bytes)
// version_minor: const = 0x0 (4 bytes)
// version_patch: const = 0x0 (4 bytes)
// data_size: bytesize = 0x138 (4 bytes)
// data_start: offsetof = 0x138 (4 bytes)
// target_count: const = 0x0 (4 bytes)
// open_count: const = 0x0 (4 bytes)
// flags: dm_ioctl_flags = 0x8800 (4 bytes)
// event_nr: const = 0x0 (4 bytes)
// padding: const = 0x0 (4 bytes)
// id: dm_id_create {
// dev: dm_dev_number (resource)
// name: dm_dev_name {
// prefix: buffer: {73 79 7a} (length 0x3)
// proc: proc = 0x0 (1 bytes)
// slot: buffer: {33 00} (length 0x2)
// pad = 0x0 (122 bytes)
// }
// uuid: union dm_dev_uuid_opt {
// uuid: dm_dev_uuid {
// prefix: buffer: {43 52 50 2d 73 79 7a} (length 0x7)
// proc: proc = 0x0 (1 bytes)
// slot: buffer: {30 00} (length 0x2)
// pad = 0x0 (126 bytes)
// }
// }
// }
// data: buffer: {} (length 0x0)
// }
// }
// ]
*(uint32_t*)0x200001c0 = 4;
*(uint32_t*)0x200001c4 = 0;
*(uint32_t*)0x200001c8 = 0;
*(uint32_t*)0x200001cc = 0x138;
*(uint32_t*)0x200001d0 = 0x138;
*(uint32_t*)0x200001d4 = 0;
*(uint32_t*)0x200001d8 = 0;
*(uint32_t*)0x200001dc = 0x8800;
*(uint32_t*)0x200001e0 = 0;
*(uint32_t*)0x200001e4 = 0;
memcpy((void*)0x200001f0, "syz", 3);
*(uint8_t*)0x200001f3 = 0x61;
memcpy((void*)0x200001f4, "3\000", 2);
memcpy((void*)0x20000270, "CRP-syz", 7);
*(uint8_t*)0x20000277 = 0x61;
memcpy((void*)0x20000278, "0\000", 2);
res = syscall(__NR_ioctl, /*fd=*/(intptr_t)r[0], /*cmd=*/0xc138fd03, /*arg=*/0x200001c0);
if (res != -1)
r[1] = *(uint64_t*)0x200001e8;
// ioctl$DM_TABLE_LOAD arguments: [
// fd: fd_dm_ctl (resource)
// cmd: const = 0xc138fd09 (4 bytes)
// arg: ptr[inout, dm_ioctl_t[dm_id_lookup, array[dm_target, 1], len[data, int32], const[0, int32]]] {
// dm_ioctl_t[dm_id_lookup, array[dm_target, 1], len[data, int32], const[0, int32]] {
// version_major: const = 0x4 (4 bytes)
// version_minor: const = 0x0 (4 bytes)
// version_patch: const = 0x0 (4 bytes)
// data_size: bytesize = 0x1a8 (4 bytes)
// data_start: offsetof = 0x138 (4 bytes)
// target_count: len = 0x1 (4 bytes)
// open_count: const = 0x0 (4 bytes)
// flags: dm_ioctl_flags = 0x20000 (4 bytes)
// event_nr: const = 0x0 (4 bytes)
// padding: const = 0x0 (4 bytes)
// id: union dm_id_lookup {
// by_dev: dm_id_by_dev {
// dev: dm_dev_number (resource)
// pad = 0x0 (264 bytes)
// }
// }
// data: array[dm_target] {
// union dm_target {
// thin_pool: dm_target_spec_t[dm_target_thin_pool, dm_thin_pool_params] {
// sector_start: const = 0x0 (8 bytes)
// length: dm_lengths = 0x800 (8 bytes)
// status: const = 0x0 (4 bytes)
// next: bytesize = 0x70 (4 bytes)
// target_type: buffer: {74 68 69 6e 2d 70 6f 6f 6c 00 00 00 00 00 00 00} (length 0x10)
// params: dm_thin_pool_params {
// meta: union dm_dev {
// ram: dm_dev_proc_t["/dev/ram", 2] {
// prefix: buffer: {2f 64 65 76 2f 72 61 6d} (length 0x8)
// id: proc = 0x1 (1 bytes)
// }
// }
// sp0: const = 0x20 (1 bytes)
// data: union dm_dev {
// ram_num: dm_ram_num {
// major: buffer: {31 3a} (length 0x2)
// minor: proc = 0x1 (20 bytes)
// }
// }
// sp1: const = 0x20 (1 bytes)
// bsize: buffer: {32 30 34 38} (length 0x4)
// sp2: const = 0x20 (1 bytes)
// lwm: int8 = 0x14 (20 bytes)
// feat: buffer: {20 31 20 72 65 61 64 5f 6f 6e 6c 79} (length 0xc)
// }
// z: const = 0x0 (1 bytes)
// }
// }
// }
// }
// }
// ]
*(uint32_t*)0x200006c0 = 4;
*(uint32_t*)0x200006c4 = 0;
*(uint32_t*)0x200006c8 = 0;
*(uint32_t*)0x200006cc = 0x1a8;
*(uint32_t*)0x200006d0 = 0x138;
*(uint32_t*)0x200006d4 = 1;
*(uint32_t*)0x200006d8 = 0;
*(uint32_t*)0x200006dc = 0x20000;
*(uint32_t*)0x200006e0 = 0;
*(uint32_t*)0x200006e4 = 0;
*(uint64_t*)0x200006e8 = r[1];
*(uint64_t*)0x200007f8 = 0;
*(uint64_t*)0x20000800 = 0x800;
*(uint32_t*)0x20000808 = 0;
*(uint32_t*)0x2000080c = 0x70;
memcpy((void*)0x20000810, "thin-pool\000\000\000\000\000\000\000", 16);
memcpy((void*)0x20000820, "/dev/ram", 8);
*(uint8_t*)0x20000828 = 0x31;
*(uint8_t*)0x20000829 = 0x20;
memcpy((void*)0x2000082a, "1:", 2);
sprintf((char*)0x2000082c, "%020llu", (long long)1);
*(uint8_t*)0x20000840 = 0x20;
memcpy((void*)0x20000841, "2048", 4);
*(uint8_t*)0x20000845 = 0x20;
sprintf((char*)0x20000846, "%020llu", (long long)0x14);
memcpy((void*)0x2000085a, " 1 read_only", 12);
*(uint8_t*)0x20000866 = 0;
syscall(__NR_ioctl, /*fd=*/(intptr_t)r[0], /*cmd=*/0xc138fd09, /*arg=*/0x200006c0);
return 0;
}
A previous root cause analysis of this bug (at commit abf8c33470f78e2226ee3dcdc69004660020ab47) concluded:
### Root Cause Analysis
#### 1. Functions, Structs, and Files Involved
- **`drivers/md/dm-thin.c`**: `pool_ctr()`
- **`drivers/md/dm-table.c`**: `dm_get_device()`, `upgrade_mode()`
- **`drivers/md/dm.c`**: `dm_put_table_device()`, `close_table_device()`
- **`drivers/md/dm-bufio.c`**: `dm_bufio_client_create()`
- **Structs & Fields**:
- `struct dm_dev` (`bdev`, `mode`)
- `struct table_device` (`count`, `dm_dev`, `list`)
- `struct dm_dev_internal` (`count`, `dm_dev`)
- `struct block_device` (`bd_dev` at offset `0x24` / 36 bytes on 32-bit ARM)
---
#### 2. Triggering Mechanism and Invariant Violation
1. **Flawed Disjoint-Device Validation in `pool_ctr`**:
In `drivers/md/dm-thin.c:pool_ctr()`, thin pools require that the metadata device (`argv[0]`) and data device (`argv[1]`) be separate, distinct block devices. The constructor checks:
```c
/* make sure metadata and data are different devices */
if (!strcmp(argv[0], argv[1])) {
ti->error = "Error setting metadata or data device";
r = -EINVAL;
goto out_unlock;
}
```
This string check (`!strcmp`) is insufficient. If a user supplies two syntactically different path strings (e.g. `/dev/sda` and `8:0`, or symlinks / alternative device node paths) that resolve to the same underlying `dev_t` / block device, the check passes.
2. **Mode Asymmetry and `upgrade_mode()`**:
When the `read_only` pool feature is requested (`pf.mode == PM_READ_ONLY`), the metadata device is opened with `metadata_mode = BLK_OPEN_READ`:
```c
r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
```
This allocates and returns a `struct table_device td` (holding `td->dm_dev`) with `td->count == 1`. At this moment, `metadata_dev->bdev` is valid, allowing `warn_if_metadata_device_too_big(metadata_dev->bdev)` to succeed.
Next, `pool_ctr()` opens the data device with read-write access:
```c
r = dm_get_device(ti, argv[1], BLK_OPEN_READ | BLK_OPEN_WRITE, &data_dev);
```
Because both paths resolve to the same `dev_t`, `dm_get_device()` locates the existing `struct dm_dev_internal dd` in `ti->table->devices`. Finding that the existing mode is `BLK_OPEN_READ` while the newly requested mode is `BLK_OPEN_READ | BLK_OPEN_WRITE`, it invokes `upgrade_mode(dd, mode, t->md)`.
3. **Use-After-Free via `upgrade_mode()`**:
In `drivers/md/dm-table.c:upgrade_mode()`:
```c
old_dev = dd->dm_dev;
r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
dd->dm_dev->mode | new_mode, &new_dev);
if (r)
return r;
dd->dm_dev = new_dev;
dm_put_table_device(md, old_dev);
```
`dm_get_table_device()` looks for an entry in `md->table_devices` matching `(dev, mode)`. Because the mode differs, it opens a *new* `struct table_device` (`new_dev`). Then, `dm_put_table_device(md, old_dev)` decrements the refcount of `old_dev`. Since `td->count` drops to zero, `close_table_device()` is called, unlinking and freeing `td` via `kfree(td)`.
However, `metadata_dev` in `pool_ctr()` still points directly to `old_dev` (`&td->dm_dev`). It is now a dangling pointer to freed slab memory.
4. **Propagation to `dm_bufio_client_create` and Crash**:
Later in `pool_ctr()`:
```c
pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev, data_dev->bdev,
block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
```
Reading `metadata_dev->bdev` performs a read from freed memory. Because the slab entry was cleared / zeroed upon free, `metadata_dev->bdev` evaluates to `NULL` (`0x00000000`).
This `NULL` pointer is passed down the call chain:
`pool_create()` $\to$ `dm_pool_metadata_open()` $\to$ `dm_block_manager_create()` $\to$ `dm_bufio_client_create(bdev, ...)`.
In `drivers/md/dm-bufio.c:dm_bufio_client_create()`:
```c
c->shrinker = shrinker_alloc(0, "dm-bufio:(%u:%u)",
MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
```
Evaluating `bdev->bd_dev` reads offset `36` (`0x24`) from `NULL` (`r3: 00000000`), generating the instruction `ldr r2, [r3, #36]` and triggering the observed kernel NULL pointer dereference crash.
---
#### 3. Runtime Consequences in a Production Kernel
- **MemoryCorruption: true**
The fundamental underlying defect is a Use-After-Free (UAF). While in this specific execution the freed memory was zeroed (leading to a NULL pointer read), in general production conditions the memory of the freed `struct table_device` can be reallocated to another object or retain stale/corrupted pointer data. Reading `metadata_dev->bdev` would then dereference a wild or controlled pointer. Furthermore, destructor cleanup paths such as `pool_dtr()` invoke `dm_put_device(ti, pt->metadata_dev)` on the freed device pointer.
- **RawDenialOfService: true**
The NULL pointer dereference (or wild pointer dereference) in `dm_bufio_client_create()` causes an immediate kernel oops/panic during table configuration (`DM_TABLE_LOAD` ioctl).
- **InfoLeak: none**
The bug does not copy freed or uninitialized data to userspace or expose kernel memory over the network.
- **IsNetCandidate: false** / **IsKVMCandidate: false**
The defect is situated entirely within the block / Device Mapper (`drivers/md`) subsystem and is not related to networking or KVM/virtualization.
Search for the commit that introduced this bug.
Reply: ### Bisection Result
**Bug-introducing commit:** `86f1152b117a404229fd6f08ec3faca779f37b92` (`"dm: allow active and inactive tables to share dm_devs"`)
---
### Root Cause Verification
In `drivers/md/dm-thin.c:pool_ctr()`:
1. `pool_ctr()` only checks `if (!strcmp(argv[0], argv[1]))` to reject identical metadata and data devices. When a user passes two distinct strings resolving to the same underlying `dev_t` (e.g., `"/dev/ram1"` and `"1:1"`) along with the `read_only` feature:
2. `pool_ctr()` first calls `dm_get_device(ti, argv[0], BLK_OPEN_READ, &metadata_dev)`.
In `drivers/md/dm-table.c:dm_get_device()`, this allocates a `struct dm_dev_internal *dd`, calls `dm_get_table_device()` which allocates a `struct table_device *td` (`count = 1`), sets `dd->dm_dev = &td->dm_dev`, and returns `*result = dd->dm_dev` into `metadata_dev`.
3. Next, `pool_ctr()` calls `dm_get_device(ti, argv[1], BLK_OPEN_READ | BLK_OPEN_WRITE, &data_dev)`.
Because both paths resolve to the same `dev_t`, `find_device(&t->devices, dev)` finds the same `dd`. Seeing that `dd->dm_dev->mode != (mode | dd->dm_dev->mode)`, `dm_get_device()` invokes `upgrade_mode(dd, mode, t->md)`.
4. In `drivers/md/dm-table.c:upgrade_mode()`:
```c
old_dev = dd->dm_dev;
r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
dd->dm_dev->mode | new_mode, &new_dev);
if (r)
return r;
dd->dm_dev = new_dev;
dm_put_table_device(md, old_dev);
```
`dm_get_table_device()` opens a *new* `struct table_device` (`new_dev`) because `find_table_device()` matches on both `dev` and `mode`. Then `dm_put_table_device(md, old_dev)` drops `td->count` to `0`, calls `close_table_device()` (which sets `td->dm_dev.bdev = NULL`), and frees `td` via `kfree(td)`.
5. `metadata_dev` in `pool_ctr()` still points to `old_dev` (`&td->dm_dev`), which is now freed memory with `bdev == NULL`.
6. `pool_ctr()` passes `metadata_dev->bdev` to `__pool_find()` -> `pool_create()` -> `dm_pool_metadata_open()` -> `dm_block_manager_create()` -> `dm_bufio_client_create()`, where `MAJOR(bdev->bd_dev)` reads `bdev->bd_dev` at offset `0x24` from `NULL`, causing the observed kernel NULL pointer dereference.
---
### Git History Traced and Commits Examined
1. **`570b9d968bf9` (`"dm table: fix upgrade mode race"`, Apr 2009)**
- Before `86f1152b117a`, `struct dm_dev_internal` embedded `struct dm_dev dm_dev` directly (`&dd->dm_dev`).
- `upgrade_mode()` opened a local `dd_new`, updated `dd->dm_dev.mode |= new_mode` in place, and called `close_dev(&dd_old, md)` on a stack copy `dd_old`.
- Crucially, `dd` remained allocated at the same address and `dd->dm_dev.bdev` remained non-NULL and valid, so any `struct dm_dev *` previously returned by `dm_get_device()` stayed valid across `upgrade_mode()`.
2. **`5d0db96d13a4` (`"dm thin: open dev read only when possible"`, May 2013)**
- Changed `pool_ctr()` to open `metadata_dev` with `FMODE_READ` instead of `FMODE_READ | FMODE_WRITE` when `pf.mode == PM_READ_ONLY`, while `data_dev` continued to be opened with `FMODE_READ | FMODE_WRITE`.
- Ruled out as the introducing commit: at `5d0db96d13a4` (prior to `86f1152b117a`), calling `dm_get_device()` on the same `dev_t` first RO and then RW triggered `upgrade_mode()` in-place on `dd`, keeping `&dd->dm_dev` alive and `metadata_dev->bdev` valid—no UAF or NULL pointer dereference occurred.
3. **`86f1152b117a` (`"dm: allow active and inactive tables to share dm_devs"`, Aug 2014)** — **Introducing Commit**
- Changed `struct dm_dev_internal::dm_dev` from an embedded struct to a pointer `struct dm_dev *dm_dev` pointing into `struct table_device` (`md->table_devices`), and changed `upgrade_mode()` to allocate a new `table_device` (`new_dev`), overwrite `dd->dm_dev = new_dev`, and call `dm_put_table_device(md, old_dev)` which sets `old_dev->bdev = NULL` and frees `td`.
- This broke the invariant relied upon by `dm_get_device()` callers (including `pool_ctr()`, `thin_ctr()`, and `snapshot_ctr()`): any `struct dm_dev *` previously returned by `dm_get_device()` for the same `dev_t` becomes a dangling pointer to freed memory (with `bdev == NULL`) as soon as `upgrade_mode()` runs on a subsequent `dm_get_device()` call.
4. **`4df2bf466a9c` (`"dm snapshot: disallow the COW and origin devices from being identical"`, Feb 2016)** & **`26110d5afe81` (`"dm-snap: simplify the origin_dev == cow_dev check in snapshot_ctr"`, May 2023)**
- Observed the same `upgrade_mode()` dangling-pointer bug in `dm-snap.c:snapshot_ctr()`. `4df2bf466a9c` checked `dm_get_dev_t(cow_path) == origin_dev` before the second `dm_get_device()`, and `26110d5afe81` later moved the check after `dm_get_device()` (re-exposing `snapshot_ctr()`).
5. **`70de2cbda8a5` (`"dm thin: add sanity checks to thin-pool and external snapshot creation"`, Jan 2019)**
- Explicitly described the `upgrade_mode()` UAF (`"upgrade_mode() will alloc a new 'dm_dev' and free the old one, which may be referenced by a previous caller"`), and attempted to fix it in `pool_ctr()` and `thin_ctr()` by adding `if (!strcmp(argv[0], argv[1]))` and `if (!strcmp(argv[0], argv[2]))`.
- Ruled out as the introducing commit: `70de2cbda8a5` was an incomplete fix attempt that only blocked identical strings and left different strings resolving to the same `dev_t` vulnerable.
---
### Conclusion
Commit **`86f1152b117a404229fd6f08ec3faca779f37b92`** (`"dm: allow active and inactive tables to share dm_devs"`) introduced the bug by making `upgrade_mode()` free `old_dev` (`struct table_device`) and clear `old_dev->bdev = NULL` while earlier `dm_get_device()` callers in the same table (such as `metadata_dev` in `pool_ctr()`) still hold pointers to `old_dev`.
|
| 7/2 |
2026/11/23 11:02 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 15391
output: 161
thoughts: 92
Thoughts: **My Journey to Uncover the Bug's Origin**
My immediate instinct, presented with this intriguing bug, is to dive deep into the codebase and its version control history. I need to pinpoint the exact commit that sowed the seeds of this issue. My initial hypothesis points towards the interactions between the thin provisioning layer and device mapping, so I'll be focusing my attention on `drivers/md/dm-thin.c`. Specifically, I'm interested in the `pool_ctr` function, as pool creation is a critical juncture where resource allocation and configuration take place, potentially leading to such anomalies.
Simultaneously, I'll be scrutinizing `drivers/md/dm-table.c`, particularly the `dm_get_device` and `upgrade_mode` functions. These are central to how devices are accessed and their modes are adjusted within the device mapper framework, and any misstep here could easily manifest as the bug we're observing.
To navigate this, I'll leverage powerful tools. `codesearch-definition-source` will be invaluable for quickly locating the relevant code sections and understanding their immediate context. Once I have a handle on the code, `git-log` and `git-blame` will be my trusty companions. I'll be meticulously tracing the commit history, using `git-blame` to see exactly which lines of code changed and in which commit, aiming to isolate the specific modification that introduced this problematic behavior. It's a systematic process of code archaeology, and I'm confident that by carefully following these steps, I'll unearth the root cause.
|
| 8/2 |
2026/11/23 11:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/md/dm-thin.c Name:pool_ctr]
Results: map[SourceCode:3270: /*
3271: * thin-pool <metadata dev> <data dev>
3272: * <data block size (sectors)>
3273: * <low water mark (blocks)>
3274: * [<#feature args> [<arg>]*]
3275: *
3276: * Optional feature arguments are:
3277: * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
3278: * ignore_discard: disable discard
3279: * no_discard_passdown: don't pass discards down to the data device
3280: * read_only: Don't allow any changes to be made to the pool metadata.
3281: * error_if_no_space: error IOs, instead of queueing, if no space.
3282: */
3283: static int pool_ctr(struct dm_target *ti, unsigned int argc, char **argv)
3284: {
3285: int r, pool_created = 0;
3286: struct pool_c *pt;
3287: struct pool *pool;
3288: struct pool_features pf;
3289: struct dm_arg_set as;
3290: struct dm_dev *data_dev;
3291: unsigned long block_size;
3292: dm_block_t low_water_blocks;
3293: struct dm_dev *metadata_dev;
3294: blk_mode_t metadata_mode;
3295:
3296: /*
3297: * FIXME Remove validation from scope of lock.
3298: */
3299: mutex_lock(&dm_thin_pool_table.mutex);
3300:
3301: if (argc < 4) {
3302: ti->error = "Invalid argument count";
3303: r = -EINVAL;
3304: goto out_unlock;
3305: }
3306:
3307: as.argc = argc;
3308: as.argv = argv;
3309:
3310: /* make sure metadata and data are different devices */
3311: if (!strcmp(argv[0], argv[1])) {
3312: ti->error = "Error setting metadata or data device";
3313: r = -EINVAL;
3314: goto out_unlock;
3315: }
3316:
3317: /*
3318: * Set default pool features.
3319: */
3320: pool_features_init(&pf);
3321:
3322: dm_consume_args(&as, 4);
3323: r = parse_pool_features(&as, &pf, ti);
3324: if (r)
3325: goto out_unlock;
3326:
3327: metadata_mode = BLK_OPEN_READ |
3328: ((pf.mode == PM_READ_ONLY) ? 0 : BLK_OPEN_WRITE);
3329: r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
3330: if (r) {
3331: ti->error = "Error opening metadata block device";
3332: goto out_unlock;
3333: }
3334: warn_if_metadata_device_too_big(metadata_dev->bdev);
3335:
3336: r = dm_get_device(ti, argv[1], BLK_OPEN_READ | BLK_OPEN_WRITE, &data_dev);
3337: if (r) {
3338: ti->error = "Error getting data device";
3339: goto out_metadata;
3340: }
3341:
3342: if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
3343: block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
3344: block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
3345: block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
3346: ti->error = "Invalid block size";
3347: r = -EINVAL;
3348: goto out;
3349: }
3350:
3351: if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
3352: ti->error = "Invalid low water mark";
3353: r = -EINVAL;
3354: goto out;
3355: }
3356:
3357: pt = kzalloc_obj(*pt);
3358: if (!pt) {
3359: r = -ENOMEM;
3360: goto out;
3361: }
3362:
3363: pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev, data_dev->bdev,
3364: block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
3365: if (IS_ERR(pool)) {
3366: r = PTR_ERR(pool);
3367: goto out_free_pt;
3368: }
3369:
3370: /*
3371: * 'pool_created' reflects whether this is the first table load.
3372: * Top level discard support is not allowed to be changed after
3373: * initial load. This would require a pool reload to trigger thin
3374: * device changes.
3375: */
3376: if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
3377: ti->error = "Discard support cannot be disabled once enabled";
3378: r = -EINVAL;
3379: goto out_flags_changed;
3380: }
3381:
3382: pt->pool = pool;
3383: pt->ti = ti;
3384: pt->metadata_dev = metadata_dev;
3385: pt->data_dev = data_dev;
3386: pt->low_water_blocks = low_water_blocks;
3387: pt->adjusted_pf = pt->requested_pf = pf;
3388: ti->num_flush_bios = 1;
3389: ti->limit_swap_bios = true;
3390:
3391: /*
3392: * Only need to enable discards if the pool should pass
3393: * them down to the data device. The thin device's discard
3394: * processing will cause mappings to be removed from the btree.
3395: */
3396: if (pf.discard_enabled && pf.discard_passdown) {
3397: ti->num_discard_bios = 1;
3398: /*
3399: * Setting 'discards_supported' circumvents the normal
3400: * stacking of discard limits (this keeps the pool and
3401: * thin devices' discard limits consistent).
3402: */
3403: ti->discards_supported = true;
3404: ti->max_discard_granularity = true;
3405: }
3406: ti->private = pt;
3407:
3408: r = dm_pool_register_metadata_threshold(pt->pool->pmd,
3409: calc_metadata_threshold(pt),
3410: metadata_low_callback,
3411: pool);
3412: if (r) {
3413: ti->error = "Error registering metadata threshold";
3414: goto out_flags_changed;
3415: }
3416:
3417: dm_pool_register_pre_commit_callback(pool->pmd,
3418: metadata_pre_commit_callback, pool);
3419:
3420: mutex_unlock(&dm_thin_pool_table.mutex);
3421:
3422: return 0;
3423:
3424: out_flags_changed:
3425: __pool_dec(pool);
3426: out_free_pt:
3427: kfree(pt);
3428: out:
3429: dm_put_device(ti, data_dev);
3430: out_metadata:
3431: dm_put_device(ti, metadata_dev);
3432: out_unlock:
3433: mutex_unlock(&dm_thin_pool_table.mutex);
3434:
3435: return r;
3436: }
SourceFile:drivers/md/dm-thin.c]
|
| 9/2 |
2026/11/23 11:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/md/dm-table.c Name:dm_get_device]
Results: map[SourceCode: 361: /*
362: * Add a device to the list, or just increment the usage count if
363: * it's already present.
364: */
365: int dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode,
366: struct dm_dev **result)
367: {
368: int r;
369: dev_t dev;
370: struct dm_dev_internal *dd;
371: struct dm_table *t = ti->table;
372:
373: BUG_ON(!t);
374:
375: r = dm_devt_from_path(path, &dev);
376: if (r)
377: return r;
378:
379: if (dev == disk_devt(t->md->disk))
380: return -EINVAL;
381:
382: dd = find_device(&t->devices, dev);
383: if (!dd) {
384: dd = kmalloc_obj(*dd);
385: if (!dd)
386: return -ENOMEM;
387:
388: r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev);
389: if (r) {
390: kfree(dd);
391: return r;
392: }
393:
394: refcount_set(&dd->count, 1);
395: list_add(&dd->list, &t->devices);
396: goto out;
397:
398: } else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
399: r = upgrade_mode(dd, mode, t->md);
400: if (r)
401: return r;
402: }
403: refcount_inc(&dd->count);
404: out:
405: *result = dd->dm_dev;
406: return 0;
407: }
SourceFile:drivers/md/dm-table.c]
|
| 10/2 |
2026/11/23 11:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/md/dm-table.c Name:upgrade_mode]
Results: map[SourceCode: 306: /*
307: * This upgrades the mode on an already open dm_dev, being
308: * careful to leave things as they were if we fail to reopen the
309: * device and not to touch the existing bdev field in case
310: * it is accessed concurrently.
311: */
312: static int upgrade_mode(struct dm_dev_internal *dd, blk_mode_t new_mode,
313: struct mapped_device *md)
314: {
315: int r;
316: struct dm_dev *old_dev, *new_dev;
317:
318: old_dev = dd->dm_dev;
319:
320: r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
321: dd->dm_dev->mode | new_mode, &new_dev);
322: if (r)
323: return r;
324:
325: dd->dm_dev = new_dev;
326: dm_put_table_device(md, old_dev);
327:
328: return 0;
329: }
SourceFile:drivers/md/dm-table.c]
|
| 11/2 |
2026/11/23 11:02 |
tool |
git-log |
0m
Args: map[Count:30 SourcePath:drivers/md/dm-thin.c SymbolName:pool_ctr]
Results: map[Output:bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
05bdb9965305 block: replace fmode_t with a block-specific type for block open flags
e2dd8aca2d76 dm bio prison v1: improve concurrent IO performance
9bbf5feecc7e dm thin: fix deadlock when swapping to thin device
86a3238c7b9b dm: change "unsigned" to "unsigned int"
3534e5a5ed29 dm thin: fix use-after-free crash in dm_sm_register_threshold_callback
21cf86614504 writeback: remove bdi->congested_fn
f06c03d1ded2 dm thin: change data device's flush_bio to be member of struct pool
873937e75f9a dm thin: don't allow changing data device during thin-pool reload
a4a8d286586d dm thin: fix use-after-free in metadata_pre_commit_callback
694cfe7f31db dm thin: Flush data device before committing metadata
70de2cbda8a5 dm thin: add sanity checks to thin-pool and external snapshot creation
48920ff2a5a9 block: remove the discard_zeroes_data flag
ba30670f4d52 dm thin: fix missing pool reference count decrement in pool_ctr error path
7d48935eff40 dm thin: allow metadata space larger than supported to go unused
b60ab990ccdf dm thin: do not expose non-zero discard limits if discards disabled
ac8c3f3df65e dm thin: generate event when metadata threshold passed
5d0db96d13a4 dm thin: open dev read only when possible
b17446df2ebc dm thin: refactor data dev resize
55a62eef8d1b dm: rename request variables to bios
28eed34e7662 dm thin: support discard with non power of two block size
0424caa14508 dm thin: fix discard support for data devices
9bc142dd755d dm thin: tidy discard support
307615a26e95 dm thin: do not set discard_zeroes_data
e49e582965b3 dm thin: add read only and fail io modes
0ac55489d9e3 dm: use bool bitfields in struct dm_target
55f2b8bdb0c7 dm thin: support for non power of 2 pool blocksize
f402693d06f3 dm thin: fix table output when pool target disables discard passdown internally
67e2e2b28181 dm thin: add pool target flags to control discard
]
|
| 12/2 |
2026/11/23 11:02 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 19933
output: 142
thoughts: 125
Thoughts: **Delving into the Thin Provisioning Landscape**
Right, so I'm here, looking at these commits related to thin provisioning in `pool_ctr`. My immediate thought is to get a handle on the recent developments, specifically focusing on the changes around `thin-pool` and external snapshot creation. The commit `70de2cbda8a5 dm thin: add sanity checks to thin-pool and external snapshot creation` immediately catches my eye. Sanity checks – that's fundamental for robustness. I need to understand what specific checks have been added and in what scenarios they're being enforced. This tells me there's been a push to harden the creation process, likely to prevent data corruption or unexpected behavior.
Then I see `5d0db96d13a4 dm thin: open dev read only when possible`. This one is interesting from a performance and safety perspective. The idea of opening a device read-only *when possible* suggests an optimization or a security enhancement. I'm thinking about the implications here: when *is* it possible? What are the conditions that allow for read-only access, and what are the benefits – reduced risk of accidental writes, perhaps faster access in certain read-heavy workloads? It implies a more nuanced approach to device handling.
Following that, `e49e582965b3 dm thin: add read only and fail io modes`. This builds on the previous point. The explicit addition of `read only` and `fail io` modes signals a desire for more granular control over how devices behave. I'm considering the use cases for `fail io` – is this for error injection testing, or perhaps a way to gracefully handle specific failure scenarios in production? The combination of these modes suggests a more sophisticated management layer for thin provisioned devices, offering greater flexibility in how they are operated and how their I/O is managed.
Beyond these direct thin provisioning commits, I'm also making a mental note to check the `git-log` on `upgrade_mode` and `dm_get_device` within `drivers/md/dm-table.c`. This isn't directly within `pool_ctr`, but it's crucial context. `upgrade_mode` sounds like it relates to how existing device mapper configurations are handled or potentially modified, and `dm_get_device` is a core function for interacting with devices. I need to see if there have been any changes or if these areas are being *affected* by the thin provisioning work. Are these functions being updated to better support the new thin provisioning modes, or are they interacting with them in a specific way? This cross-referencing is vital for understanding the broader impact and ensuring consistency across the device mapper subsystem. My overall thought process is to understand not just the individual changes but how they fit into the larger picture of device management and thin provisioning capabilities.
|
| 13/2 |
2026/11/23 11:03 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 5d0db96d13a4e2cd22b52494fb19ce5a9c8b8d90
Author: Joe Thornber <ejt@redhat.com>
Date: Fri May 10 14:37:19 2013 +0100
dm thin: open dev read only when possible
If a thin pool is created in read-only-metadata mode then only open the
metadata device read-only.
Previously it was always opened with FMODE_READ | FMODE_WRITE.
(Note that dm_get_device() still allows read-only dm devices to be used
read-write at the moment: If I create a read-only linear device for the
metadata, via dmsetup load --readonly, then I can still create a rw pool
out of it.)
Signed-off-by: Joe Thornber <ejt@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c
index 111c148fb1d0..ef021b0c8106 100644
--- a/drivers/md/dm-thin.c
+++ b/drivers/md/dm-thin.c
@@ -1945,6 +1945,7 @@ static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
unsigned long block_size;
dm_block_t low_water_blocks;
struct dm_dev *metadata_dev;
+ fmode_t metadata_mode;
/*
* FIXME Remove validation from scope of lock.
@@ -1956,10 +1957,22 @@ static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
r = -EINVAL;
goto out_unlock;
}
+
as.argc = argc;
as.argv = argv;
- r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &metadata_dev);
+ /*
+ * Set default pool features.
+ */
+ pool_features_init(&pf);
+
+ dm_consume_args(&as, 4);
+ r = parse_pool_features(&as, &pf, ti);
+ if (r)
+ goto out_unlock;
+
+ metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
+ r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
if (r) {
ti->error = "Error opening metadata block device";
goto out_unlock;
@@ -1992,16 +2005,6 @@ static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
goto out;
}
- /*
- * Set default pool features.
- */
- pool_features_init(&pf);
-
- dm_consume_args(&as, 4);
- r = parse_pool_features(&as, &pf, ti);
- if (r)
- goto out;
-
pt = kzalloc(sizeof(*pt), GFP_KERNEL);
if (!pt) {
r = -ENOMEM;
]
|
| 14/2 |
2026/11/23 11:03 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 70de2cbda8a5d788284469e755f8b097d339c240
Author: Jason Cai (Xiang Feng) <jason.cai.kern@gmail.com>
Date: Sun Jan 20 22:39:13 2019 +0800
dm thin: add sanity checks to thin-pool and external snapshot creation
Invoking dm_get_device() twice on the same device path with different
modes is dangerous. Because in that case, upgrade_mode() will alloc a
new 'dm_dev' and free the old one, which may be referenced by a previous
caller. Dereferencing the dangling pointer will trigger kernel NULL
pointer dereference.
The following two cases can reproduce this issue. Actually, they are
invalid setups that must be disallowed, e.g.:
1. Creating a thin-pool with read_only mode, and the same device as
both metadata and data.
dmsetup create thinp --table \
"0 41943040 thin-pool /dev/vdb /dev/vdb 128 0 1 read_only"
BUG: unable to handle kernel NULL pointer dereference at 0000000000000080
...
Call Trace:
new_read+0xfb/0x110 [dm_bufio]
dm_bm_read_lock+0x43/0x190 [dm_persistent_data]
? kmem_cache_alloc_trace+0x15c/0x1e0
__create_persistent_data_objects+0x65/0x3e0 [dm_thin_pool]
dm_pool_metadata_open+0x8c/0xf0 [dm_thin_pool]
pool_ctr.cold.79+0x213/0x913 [dm_thin_pool]
? realloc_argv+0x50/0x70 [dm_mod]
dm_table_add_target+0x14e/0x330 [dm_mod]
table_load+0x122/0x2e0 [dm_mod]
? dev_status+0x40/0x40 [dm_mod]
ctl_ioctl+0x1aa/0x3e0 [dm_mod]
dm_ctl_ioctl+0xa/0x10 [dm_mod]
do_vfs_ioctl+0xa2/0x600
? handle_mm_fault+0xda/0x200
? __do_page_fault+0x26c/0x4f0
ksys_ioctl+0x60/0x90
__x64_sys_ioctl+0x16/0x20
do_syscall_64+0x55/0x150
entry_SYSCALL_64_after_hwframe+0x44/0xa9
2. Creating a external snapshot using the same thin-pool device.
dmsetup create thinp --table \
"0 41943040 thin-pool /dev/vdc /dev/vdb 128 0 2 ignore_discard"
dmsetup message /dev/mapper/thinp 0 "create_thin 0"
dmsetup create snap --table \
"0 204800 thin /dev/mapper/thinp 0 /dev/mapper/thinp"
BUG: unable to handle kernel NULL pointer dereference at 0000000000000000
...
Call Trace:
? __alloc_pages_nodemask+0x13c/0x2e0
retrieve_status+0xa5/0x1f0 [dm_mod]
? dm_get_live_or_inactive_table.isra.7+0x20/0x20 [dm_mod]
table_status+0x61/0xa0 [dm_mod]
ctl_ioctl+0x1aa/0x3e0 [dm_mod]
dm_ctl_ioctl+0xa/0x10 [dm_mod]
do_vfs_ioctl+0xa2/0x600
ksys_ioctl+0x60/0x90
? ksys_write+0x4f/0xb0
__x64_sys_ioctl+0x16/0x20
do_syscall_64+0x55/0x150
entry_SYSCALL_64_after_hwframe+0x44/0xa9
Signed-off-by: Jason Cai (Xiang Feng) <jason.cai@linux.alibaba.com>
Signed-off-by: Mike Snitzer <snitzer@redhat.com>
diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c
index 0d9ded0f5e50..fcd887703f95 100644
--- a/drivers/md/dm-thin.c
+++ b/drivers/md/dm-thin.c
@@ -3283,6 +3283,13 @@ static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
as.argc = argc;
as.argv = argv;
+ /* make sure metadata and data are different devices */
+ if (!strcmp(argv[0], argv[1])) {
+ ti->error = "Error setting metadata or data device";
+ r = -EINVAL;
+ goto out_unlock;
+ }
+
/*
* Set default pool features.
*/
@@ -4167,6 +4174,12 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
tc->sort_bio_list = RB_ROOT;
if (argc == 3) {
+ if (!strcmp(argv[0], argv[2])) {
+ ti->error = "Error setting origin device";
+ r = -EINVAL;
+ goto bad_origin_dev;
+ }
+
r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
if (r) {
ti->error = "Error opening origin device";
]
|
| 15/2 |
2026/11/23 11:03 |
tool |
git-log |
0m
Args: map[Count:20 SourcePath:drivers/md/dm-table.c SymbolName:upgrade_mode]
Results: map[Output:a21f9edb13b0 dm: factor out helper function from dm_get_device
05bdb9965305 block: replace fmode_t with a block-specific type for block open flags
7a126d5bf975 dm: only call early_lookup_bdev from early boot context
d4a28d7defe7 dm: remove dm_get_dev_t
cf056a431215 init: improve the name_to_dev_t interface
4e7b5671c6a8 block: remove i_bdev
3c1201691006 dm table: replace while loops with for loops
4df2bf466a9c dm snapshot: disallow the COW and origin devices from being identical
86f1152b117a dm: allow active and inactive tables to share dm_devs
570b9d968bf9 dm table: fix upgrade mode race
aeb5d727062a [PATCH] introduce fmode_t, do annotations
82b1519b345d dm: export struct dm_dev
f165921df46a [PATCH] dm/md dependency tree in sysfs: dm to use bd_claim_by_disk
1da177e4c3f4 Linux-2.6.12-rc2
]
|
| 16/2 |
2026/11/23 11:03 |
tool |
git-log |
0m
Args: map[Count:20 SourcePath:drivers/md/dm-table.c SymbolName:dm_get_device]
Results: map[Output:bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
4550a71b179b Revert "dm: fix a race condition in retrieve_deps"
a21f9edb13b0 dm: factor out helper function from dm_get_device
f6007dce0cd3 dm: fix a race condition in retrieve_deps
d715fa23577c dm: avoid assignment in if conditions
d1c0e1587e68 dm table: check that a dm device doesn't reference itself
afc567a4977b dm table: fix regression from improper dm_dev_internal.count refcount_t conversion
2a0b4682e09d dm: convert dm_dev_internal.count from atomic_t to refcount_t
86f1152b117a dm: allow active and inactive tables to share dm_devs
02acc3a4fa0a dm table: ensure targets are aligned to logical_block_size
82b1519b345d dm: export struct dm_dev
f165921df46a [PATCH] dm/md dependency tree in sysfs: dm to use bd_claim_by_disk
1da177e4c3f4 Linux-2.6.12-rc2
]
|
| 17/2 |
2026/11/23 11:03 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 23439
output: 117
thoughts: 121
Thoughts: **Deep Dive into DM Core Commit Analysis**
Wow, these two commits, `86f1152b117a dm: allow active and inactive tables to share dm_devs` and `570b9d968bf9 dm table: fix upgrade mode race`, immediately catch my eye. They seem to address some critical aspects of the Device Mapper (DM) core, specifically around how device mapper devices (`dm_devs`) are managed and the stability of table upgrades. My immediate instinct as an expert in this area is to dissect them thoroughly.
To get a granular understanding, I'm going to use `git show` on `86f1152b117a` and `570b9d968bf9`. But I also have a hunch that `e49e582965b3` and `4df2bf466a9c` are relevant context or perhaps precursors to these changes. It's not uncommon for important architectural shifts or bug fixes to involve a series of commits, and examining related ones often reveals the full scope and intent of a particular change. By looking at all four, I expect to see a more complete picture of the problem space and the evolution of the solution. This approach allows me to understand not just *what* changed, but *why* it changed, and how it fits into the broader DM subsystem's design.
|
| 18/2 |
2026/11/23 11:03 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 86f1152b117a404229fd6f08ec3faca779f37b92
Author: Benjamin Marzinski <bmarzins@redhat.com>
Date: Wed Aug 13 13:53:43 2014 -0500
dm: allow active and inactive tables to share dm_devs
Until this change, when loading a new DM table, DM core would re-open
all of the devices in the DM table. Now, DM core will avoid redundant
device opens (and closes when destroying the old table) if the old
table already has a device open using the same mode. This is achieved
by managing reference counts on the table_devices that DM core now
stores in the mapped_device structure (rather than in the dm_table
structure). So a mapped_device's active and inactive dm_tables' dm_dev
lists now just point to the dm_devs stored in the mapped_device's
table_devices list.
This improvement in DM core's device reference counting has the
side-effect of fixing a long-standing limitation of the multipath
target: a DM multipath table couldn't include any paths that were unusable
(failed). For example: if all paths have failed and you add a new,
working, path to the table; you can't use it since the table load would
fail due to it still containing failed paths. Now a re-load of a
multipath table can include failed devices and when those devices become
active again they can be used instantly.
The device list code in dm.c isn't a straight copy/paste from the code in
dm-table.c, but it's very close (aside from some variable renames). One
subtle difference is that find_table_device for the tables_devices list
will only match devices with the same name and mode. This is because we
don't want to upgrade a device's mode in the active table when an
inactive table is loaded.
Access to the mapped_device structure's tables_devices list requires a
mutex (tables_devices_lock), so that tables cannot be created and
destroyed concurrently.
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
Signed-off-by: Mike Snitzer <snitzer@redhat.com>
diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c
index 51521429fb59..0be9381365d7 100644
--- a/drivers/md/dm-ioctl.c
+++ b/drivers/md/dm-ioctl.c
@@ -1418,7 +1418,7 @@ static void retrieve_deps(struct dm_table *table,
deps->count = count;
count = 0;
list_for_each_entry (dd, dm_table_get_devices(table), list)
- deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev);
+ deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev);
param->data_size = param->data_start + needed;
}
diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c
index f9c6cb8dbcf8..b2bd1ebf4562 100644
--- a/drivers/md/dm-table.c
+++ b/drivers/md/dm-table.c
@@ -210,15 +210,16 @@ int dm_table_create(struct dm_table **result, fmode_t mode,
return 0;
}
-static void free_devices(struct list_head *devices)
+static void free_devices(struct list_head *devices, struct mapped_device *md)
{
struct list_head *tmp, *next;
list_for_each_safe(tmp, next, devices) {
struct dm_dev_internal *dd =
list_entry(tmp, struct dm_dev_internal, list);
- DMWARN("dm_table_destroy: dm_put_device call missing for %s",
- dd->dm_dev.name);
+ DMWARN("%s: dm_table_destroy: dm_put_device call missing for %s",
+ dm_device_name(md), dd->dm_dev->name);
+ dm_put_table_device(md, dd->dm_dev);
kfree(dd);
}
}
@@ -247,7 +248,7 @@ void dm_table_destroy(struct dm_table *t)
vfree(t->highs);
/* free the device list */
- free_devices(&t->devices);
+ free_devices(&t->devices, t->md);
dm_free_md_mempools(t->mempools);
@@ -262,52 +263,12 @@ static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
struct dm_dev_internal *dd;
list_for_each_entry (dd, l, list)
- if (dd->dm_dev.bdev->bd_dev == dev)
+ if (dd->dm_dev->bdev->bd_dev == dev)
return dd;
return NULL;
}
-/*
- * Open a device so we can use it as a map destination.
- */
-static int open_dev(struct dm_dev_internal *d, dev_t dev,
- struct mapped_device *md)
-{
- static char *_claim_ptr = "I belong to device-mapper";
- struct block_device *bdev;
-
- int r;
-
- BUG_ON(d->dm_dev.bdev);
-
- bdev = blkdev_get_by_dev(dev, d->dm_dev.mode | FMODE_EXCL, _claim_ptr);
- if (IS_ERR(bdev))
- return PTR_ERR(bdev);
-
- r = bd_link_disk_holder(bdev, dm_disk(md));
- if (r) {
- blkdev_put(bdev, d->dm_dev.mode | FMODE_EXCL);
- return r;
- }
-
- d->dm_dev.bdev = bdev;
- return 0;
-}
-
-/*
- * Close a device that we've been using.
- */
-static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
-{
- if (!d->dm_dev.bdev)
- return;
-
- bd_unlink_disk_holder(d->dm_dev.bdev, dm_disk(md));
- blkdev_put(d->dm_dev.bdev, d->dm_dev.mode | FMODE_EXCL);
- d->dm_dev.bdev = NULL;
-}
-
/*
* If possible, this checks an area of a destination device is invalid.
*/
@@ -386,19 +347,17 @@ static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
struct mapped_device *md)
{
int r;
- struct dm_dev_internal dd_new, dd_old;
+ struct dm_dev *old_dev, *new_dev;
- dd_new = dd_old = *dd;
+ old_dev = dd->dm_dev;
- dd_new.dm_dev.mode |= new_mode;
- dd_new.dm_dev.bdev = NULL;
-
- r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md);
+ r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
+ dd->dm_dev->mode | new_mode, &new_dev);
if (r)
return r;
- dd->dm_dev.mode |= new_mode;
- close_dev(&dd_old, md);
+ dd->dm_dev = new_dev;
+ dm_put_table_device(md, old_dev);
return 0;
}
@@ -440,27 +399,22 @@ int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
if (!dd)
return -ENOMEM;
- dd->dm_dev.mode = mode;
- dd->dm_dev.bdev = NULL;
-
- if ((r = open_dev(dd, dev, t->md))) {
+ if ((r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev))) {
kfree(dd);
return r;
}
- format_dev_t(dd->dm_dev.name, dev);
-
atomic_set(&dd->count, 0);
list_add(&dd->list, &t->devices);
- } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
+ } else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
r = upgrade_mode(dd, mode, t->md);
if (r)
return r;
}
atomic_inc(&dd->count);
- *result = &dd->dm_dev;
+ *result = dd->dm_dev;
return 0;
}
EXPORT_SYMBOL(dm_get_device);
@@ -505,11 +459,23 @@ static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
*/
void dm_put_device(struct dm_target *ti, struct dm_dev *d)
{
- struct dm_dev_internal *dd = container_of(d, struct dm_dev_internal,
- dm_dev);
+ int found = 0;
+ struct list_head *devices = &ti->table->devices;
+ struct dm_dev_internal *dd;
+ list_for_each_entry(dd, devices, list) {
+ if (dd->dm_dev == d) {
+ found = 1;
+ break;
+ }
+ }
+ if (!found) {
+ DMWARN("%s: device %s not in table devices list",
+ dm_device_name(ti->table->md), d->name);
+ return;
+ }
if (atomic_dec_and_test(&dd->count)) {
- close_dev(dd, ti->table->md);
+ dm_put_table_device(ti->table->md, d);
list_del(&dd->list);
kfree(dd);
}
@@ -906,7 +872,7 @@ static int dm_table_set_type(struct dm_table *t)
/* Non-request-stackable devices can't be used for request-based dm */
devices = dm_table_get_devices(t);
list_for_each_entry(dd, devices, list) {
- if (!blk_queue_stackable(bdev_get_queue(dd->dm_dev.bdev))) {
+ if (!blk_queue_stackable(bdev_get_queue(dd->dm_dev->bdev))) {
DMWARN("table load rejected: including"
" non-request-stackable devices");
return -EINVAL;
@@ -1043,7 +1009,7 @@ static struct gendisk * dm_table_get_integrity_disk(struct dm_table *t,
struct gendisk *prev_disk = NULL, *template_disk = NULL;
list_for_each_entry(dd, devices, list) {
- template_disk = dd->dm_dev.bdev->bd_disk;
+ template_disk = dd->dm_dev->bdev->bd_disk;
if (!blk_get_integrity(template_disk))
goto no_integrity;
if (!match_all && !blk_integrity_is_initialized(template_disk))
@@ -1629,7 +1595,7 @@ int dm_table_any_congested(struct dm_table *t, int bdi_bits)
int r = 0;
list_for_each_entry(dd, devices, list) {
- struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
+ struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
char b[BDEVNAME_SIZE];
if (likely(q))
@@ -1637,7 +1603,7 @@ int dm_table_any_congested(struct dm_table *t, int bdi_bits)
else
DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
dm_device_name(t->md),
- bdevname(dd->dm_dev.bdev, b));
+ bdevname(dd->dm_dev->bdev, b));
}
list_for_each_entry(cb, &t->target_callbacks, list)
diff --git a/drivers/md/dm.c b/drivers/md/dm.c
index 56a2c74c9a3f..58f3927fd7cc 100644
--- a/drivers/md/dm.c
+++ b/drivers/md/dm.c
@@ -142,6 +142,9 @@ struct mapped_device {
*/
struct dm_table *map;
+ struct list_head table_devices;
+ struct mutex table_devices_lock;
+
unsigned long flags;
struct request_queue *queue;
@@ -212,6 +215,12 @@ struct dm_md_mempools {
struct bio_set *bs;
};
+struct table_device {
+ struct list_head list;
+ atomic_t count;
+ struct dm_dev dm_dev;
+};
+
#define RESERVED_BIO_BASED_IOS 16
#define RESERVED_REQUEST_BASED_IOS 256
#define RESERVED_MAX_IOS 1024
@@ -669,6 +678,120 @@ static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
rcu_read_unlock();
}
+/*
+ * Open a table device so we can use it as a map destination.
+ */
+static int open_table_device(struct table_device *td, dev_t dev,
+ struct mapped_device *md)
+{
+ static char *_claim_ptr = "I belong to device-mapper";
+ struct block_device *bdev;
+
+ int r;
+
+ BUG_ON(td->dm_dev.bdev);
+
+ bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
+ if (IS_ERR(bdev))
+ return PTR_ERR(bdev);
+
+ r = bd_link_disk_holder(bdev, dm_disk(md));
+ if (r) {
+ blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
+ return r;
+ }
+
+ td->dm_dev.bdev = bdev;
+ return 0;
+}
+
+/*
+ * Close a table device that we've been using.
+ */
+static void close_table_device(struct table_device *td, struct mapped_device *md)
+{
+ if (!td->dm_dev.bdev)
+ return;
+
+ bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
+ blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
+ td->dm_dev.bdev = NULL;
+}
+
+static struct table_device *find_table_device(struct list_head *l, dev_t dev,
+ fmode_t mode) {
+ struct table_device *td;
+
+ list_for_each_entry(td, l, list)
+ if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
+ return td;
+
+ return NULL;
+}
+
+int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
+ struct dm_dev **result) {
+ int r;
+ struct table_device *td;
+
+ mutex_lock(&md->table_devices_lock);
+ td = find_table_device(&md->table_devices, dev, mode);
+ if (!td) {
+ td = kmalloc(sizeof(*td), GFP_KERNEL);
+ if (!td) {
+ mutex_unlock(&md->table_devices_lock);
+ return -ENOMEM;
+ }
+
+ td->dm_dev.mode = mode;
+ td->dm_dev.bdev = NULL;
+
+ if ((r = open_table_device(td, dev, md))) {
+ mutex_unlock(&md->table_devices_lock);
+ kfree(td);
+ return r;
+ }
+
+ format_dev_t(td->dm_dev.name, dev);
+
+ atomic_set(&td->count, 0);
+ list_add(&td->list, &md->table_devices);
+ }
+ atomic_inc(&td->count);
+ mutex_unlock(&md->table_devices_lock);
+
+ *result = &td->dm_dev;
+ return 0;
+}
+EXPORT_SYMBOL_GPL(dm_get_table_device);
+
+void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
+{
+ struct table_device *td = container_of(d, struct table_device, dm_dev);
+
+ mutex_lock(&md->table_devices_lock);
+ if (atomic_dec_and_test(&td->count)) {
+ close_table_device(td, md);
+ list_del(&td->list);
+ kfree(td);
+ }
+ mutex_unlock(&md->table_devices_lock);
+}
+EXPORT_SYMBOL(dm_put_table_device);
+
+static void free_table_devices(struct list_head *devices)
+{
+ struct list_head *tmp, *next;
+
+ list_for_each_safe(tmp, next, devices) {
+ struct table_device *td = list_entry(tmp, struct table_device, list);
+
+ DMWARN("dm_destroy: %s still exists with %d references",
+ td->dm_dev.name, atomic_read(&td->count));
+ kfree(td);
+ }
+}
+
/*
* Get the geometry associated with a dm device
*/
@@ -1944,12 +2067,14 @@ static struct mapped_device *alloc_dev(int minor)
md->type = DM_TYPE_NONE;
mutex_init(&md->suspend_lock);
mutex_init(&md->type_lock);
+ mutex_init(&md->table_devices_lock);
spin_lock_init(&md->deferred_lock);
atomic_set(&md->holders, 1);
atomic_set(&md->open_count, 0);
atomic_set(&md->event_nr, 0);
atomic_set(&md->uevent_seq, 0);
INIT_LIST_HEAD(&md->uevent_list);
+ INIT_LIST_HEAD(&md->table_devices);
spin_lock_init(&md->uevent_lock);
md->queue = blk_alloc_queue(GFP_KERNEL);
@@ -2035,6 +2160,7 @@ static void free_dev(struct mapped_device *md)
blk_integrity_unregister(md->disk);
del_gendisk(md->disk);
cleanup_srcu_struct(&md->io_barrier);
+ free_table_devices(&md->table_devices);
free_minor(minor);
spin_lock(&_minor_lock);
diff --git a/drivers/md/dm.h b/drivers/md/dm.h
index e81d2152fa68..988c7fb7b145 100644
--- a/drivers/md/dm.h
+++ b/drivers/md/dm.h
@@ -44,7 +44,7 @@
struct dm_dev_internal {
struct list_head list;
atomic_t count;
- struct dm_dev dm_dev;
+ struct dm_dev *dm_dev;
};
struct dm_table;
@@ -188,6 +188,9 @@ int dm_cancel_deferred_remove(struct mapped_device *md);
int dm_request_based(struct mapped_device *md);
sector_t dm_get_size(struct mapped_device *md);
struct request_queue *dm_get_md_queue(struct mapped_device *md);
+int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
+ struct dm_dev **result);
+void dm_put_table_device(struct mapped_device *md, struct dm_dev *d);
struct dm_stats *dm_get_stats(struct mapped_device *md);
int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
diff --git a/include/uapi/linux/dm-ioctl.h b/include/uapi/linux/dm-ioctl.h
index c8a4302093a3..3315ab21f728 100644
--- a/include/uapi/linux/dm-ioctl.h
+++ b/include/uapi/linux/dm-ioctl.h
@@ -267,9 +267,9 @@ enum {
#define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, DM_DEV_SET_GEOMETRY_CMD, struct dm_ioctl)
#define DM_VERSION_MAJOR 4
-#define DM_VERSION_MINOR 27
+#define DM_VERSION_MINOR 28
#define DM_VERSION_PATCHLEVEL 0
-#define DM_VERSION_EXTRA "-ioctl (2013-10-30)"
+#define DM_VERSION_EXTRA "-ioctl (2014-09-17)"
/* Status bits */
#define DM_READONLY_FLAG (1 << 0) /* In/Out */
]
|
| 19/2 |
2026/11/23 11:03 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 570b9d968bf9b16974252ef7cbce73fa6dac34f3
Author: Alasdair G Kergon <agk@redhat.com>
Date: Thu Apr 2 19:55:28 2009 +0100
dm table: fix upgrade mode race
upgrade_mode() sets bdev to NULL temporarily, and does not have any
locking to exclude anything from seeing that NULL.
In dm_table_any_congested() bdev_get_queue() can dereference that NULL and
cause a reported oops.
Fix this by not changing that field during the mode upgrade.
Cc: stable@kernel.org
Cc: Neil Brown <neilb@suse.de>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c
index 2fd66c30f7f8..e8361b191b9b 100644
--- a/drivers/md/dm-table.c
+++ b/drivers/md/dm-table.c
@@ -399,28 +399,30 @@ static int check_device_area(struct dm_dev_internal *dd, sector_t start,
}
/*
- * This upgrades the mode on an already open dm_dev. Being
+ * This upgrades the mode on an already open dm_dev, being
* careful to leave things as they were if we fail to reopen the
- * device.
+ * device and not to touch the existing bdev field in case
+ * it is accessed concurrently inside dm_table_any_congested().
*/
static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
struct mapped_device *md)
{
int r;
- struct dm_dev_internal dd_copy;
- dev_t dev = dd->dm_dev.bdev->bd_dev;
+ struct dm_dev_internal dd_new, dd_old;
- dd_copy = *dd;
+ dd_new = dd_old = *dd;
+
+ dd_new.dm_dev.mode |= new_mode;
+ dd_new.dm_dev.bdev = NULL;
+
+ r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md);
+ if (r)
+ return r;
dd->dm_dev.mode |= new_mode;
- dd->dm_dev.bdev = NULL;
- r = open_dev(dd, dev, md);
- if (!r)
- close_dev(&dd_copy, md);
- else
- *dd = dd_copy;
+ close_dev(&dd_old, md);
- return r;
+ return 0;
}
/*
]
|
| 20/2 |
2026/11/23 11:03 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 4df2bf466a9c9c92f40d27c4aa9120f4e8227bfc
Author: DingXiang <dingxiang@huawei.com>
Date: Tue Feb 2 12:29:18 2016 +0800
dm snapshot: disallow the COW and origin devices from being identical
Otherwise loading a "snapshot" table using the same device for the
origin and COW devices, e.g.:
echo "0 20971520 snapshot 253:3 253:3 P 8" | dmsetup create snap
will trigger:
BUG: unable to handle kernel NULL pointer dereference at 0000000000000098
[ 1958.979934] IP: [<ffffffffa040efba>] dm_exception_store_set_chunk_size+0x7a/0x110 [dm_snapshot]
[ 1958.989655] PGD 0
[ 1958.991903] Oops: 0000 [#1] SMP
...
[ 1959.059647] CPU: 9 PID: 3556 Comm: dmsetup Tainted: G IO 4.5.0-rc5.snitm+ #150
...
[ 1959.083517] task: ffff8800b9660c80 ti: ffff88032a954000 task.ti: ffff88032a954000
[ 1959.091865] RIP: 0010:[<ffffffffa040efba>] [<ffffffffa040efba>] dm_exception_store_set_chunk_size+0x7a/0x110 [dm_snapshot]
[ 1959.104295] RSP: 0018:ffff88032a957b30 EFLAGS: 00010246
[ 1959.110219] RAX: 0000000000000000 RBX: 0000000000000008 RCX: 0000000000000001
[ 1959.118180] RDX: 0000000000000000 RSI: 0000000000000008 RDI: ffff880329334a00
[ 1959.126141] RBP: ffff88032a957b50 R08: 0000000000000000 R09: 0000000000000001
[ 1959.134102] R10: 000000000000000a R11: f000000000000000 R12: ffff880330884d80
[ 1959.142061] R13: 0000000000000008 R14: ffffc90001c13088 R15: ffff880330884d80
[ 1959.150021] FS: 00007f8926ba3840(0000) GS:ffff880333440000(0000) knlGS:0000000000000000
[ 1959.159047] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 1959.165456] CR2: 0000000000000098 CR3: 000000032f48b000 CR4: 00000000000006e0
[ 1959.173415] Stack:
[ 1959.175656] ffffc90001c13040 ffff880329334a00 ffff880330884ed0 ffff88032a957bdc
[ 1959.183946] ffff88032a957bb8 ffffffffa040f225 ffff880329334a30 ffff880300000000
[ 1959.192233] ffffffffa04133e0 ffff880329334b30 0000000830884d58 00000000569c58cf
[ 1959.200521] Call Trace:
[ 1959.203248] [<ffffffffa040f225>] dm_exception_store_create+0x1d5/0x240 [dm_snapshot]
[ 1959.211986] [<ffffffffa040d310>] snapshot_ctr+0x140/0x630 [dm_snapshot]
[ 1959.219469] [<ffffffffa0005c44>] ? dm_split_args+0x64/0x150 [dm_mod]
[ 1959.226656] [<ffffffffa0005ea7>] dm_table_add_target+0x177/0x440 [dm_mod]
[ 1959.234328] [<ffffffffa0009203>] table_load+0x143/0x370 [dm_mod]
[ 1959.241129] [<ffffffffa00090c0>] ? retrieve_status+0x1b0/0x1b0 [dm_mod]
[ 1959.248607] [<ffffffffa0009e35>] ctl_ioctl+0x255/0x4d0 [dm_mod]
[ 1959.255307] [<ffffffff813304e2>] ? memzero_explicit+0x12/0x20
[ 1959.261816] [<ffffffffa000a0c3>] dm_ctl_ioctl+0x13/0x20 [dm_mod]
[ 1959.268615] [<ffffffff81215eb6>] do_vfs_ioctl+0xa6/0x5c0
[ 1959.274637] [<ffffffff81120d2f>] ? __audit_syscall_entry+0xaf/0x100
[ 1959.281726] [<ffffffff81003176>] ? do_audit_syscall_entry+0x66/0x70
[ 1959.288814] [<ffffffff81216449>] SyS_ioctl+0x79/0x90
[ 1959.294450] [<ffffffff8167e4ae>] entry_SYSCALL_64_fastpath+0x12/0x71
...
[ 1959.323277] RIP [<ffffffffa040efba>] dm_exception_store_set_chunk_size+0x7a/0x110 [dm_snapshot]
[ 1959.333090] RSP <ffff88032a957b30>
[ 1959.336978] CR2: 0000000000000098
[ 1959.344121] ---[ end trace b049991ccad1169e ]---
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1195899
Cc: stable@vger.kernel.org
Signed-off-by: Ding Xiang <dingxiang@huawei.com>
Signed-off-by: Mike Snitzer <snitzer@redhat.com>
diff --git a/drivers/md/dm-snap.c b/drivers/md/dm-snap.c
index 62479ac4baf7..70bb0e8b62ce 100644
--- a/drivers/md/dm-snap.c
+++ b/drivers/md/dm-snap.c
@@ -1105,6 +1105,7 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
int i;
int r = -EINVAL;
char *origin_path, *cow_path;
+ dev_t origin_dev, cow_dev;
unsigned args_used, num_flush_bios = 1;
fmode_t origin_mode = FMODE_READ;
@@ -1135,11 +1136,19 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
ti->error = "Cannot get origin device";
goto bad_origin;
}
+ origin_dev = s->origin->bdev->bd_dev;
cow_path = argv[0];
argv++;
argc--;
+ cow_dev = dm_get_dev_t(cow_path);
+ if (cow_dev && cow_dev == origin_dev) {
+ ti->error = "COW device cannot be the same as origin device";
+ r = -EINVAL;
+ goto bad_cow;
+ }
+
r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
if (r) {
ti->error = "Cannot get COW device";
diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c
index 7210e5392cc4..f9e8f0bef332 100644
--- a/drivers/md/dm-table.c
+++ b/drivers/md/dm-table.c
@@ -364,6 +364,26 @@ static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
return 0;
}
+/*
+ * Convert the path to a device
+ */
+dev_t dm_get_dev_t(const char *path)
+{
+ dev_t uninitialized_var(dev);
+ struct block_device *bdev;
+
+ bdev = lookup_bdev(path);
+ if (IS_ERR(bdev))
+ dev = name_to_dev_t(path);
+ else {
+ dev = bdev->bd_dev;
+ bdput(bdev);
+ }
+
+ return dev;
+}
+EXPORT_SYMBOL_GPL(dm_get_dev_t);
+
/*
* Add a device to the list, or just increment the usage count if
* it's already present.
@@ -372,23 +392,15 @@ int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
struct dm_dev **result)
{
int r;
- dev_t uninitialized_var(dev);
+ dev_t dev;
struct dm_dev_internal *dd;
struct dm_table *t = ti->table;
- struct block_device *bdev;
BUG_ON(!t);
- /* convert the path to a device */
- bdev = lookup_bdev(path);
- if (IS_ERR(bdev)) {
- dev = name_to_dev_t(path);
- if (!dev)
- return -ENODEV;
- } else {
- dev = bdev->bd_dev;
- bdput(bdev);
- }
+ dev = dm_get_dev_t(path);
+ if (!dev)
+ return -ENODEV;
dd = find_device(&t->devices, dev);
if (!dd) {
diff --git a/include/linux/device-mapper.h b/include/linux/device-mapper.h
index 82ae3b5e2c45..0830c9e86f0d 100644
--- a/include/linux/device-mapper.h
+++ b/include/linux/device-mapper.h
@@ -124,6 +124,8 @@ struct dm_dev {
char name[16];
};
+dev_t dm_get_dev_t(const char *path);
+
/*
* Constructors should call these functions to ensure destination devices
* are opened/closed correctly.
]
|
| 21/2 |
2026/11/23 11:03 |
tool |
git-show |
0m
Args:
Results: map[Output:commit e49e582965b3694f07a106adc83ddb44aa4f0890
Author: Joe Thornber <ejt@redhat.com>
Date: Fri Jul 27 15:08:16 2012 +0100
dm thin: add read only and fail io modes
Add read-only and fail-io modes to thin provisioning.
If a transaction commit fails the pool's metadata device will transition
to "read-only" mode. If a commit fails once already in read-only mode
the transition to "fail-io" mode occurs.
Once in fail-io mode the pool and all associated thin devices will
report a status of "Fail".
Signed-off-by: Joe Thornber <ejt@redhat.com>
Signed-off-by: Mike Snitzer <snitzer@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
diff --git a/Documentation/device-mapper/thin-provisioning.txt b/Documentation/device-mapper/thin-provisioning.txt
index f5cfc62b7ad3..30b8b83bd333 100644
--- a/Documentation/device-mapper/thin-provisioning.txt
+++ b/Documentation/device-mapper/thin-provisioning.txt
@@ -231,6 +231,9 @@ i) Constructor
no_discard_passdown: Don't pass discards down to the underlying
data device, but just remove the mapping.
+ read_only: Don't allow any changes to be made to the pool
+ metadata.
+
Data block size must be between 64KB (128 sectors) and 1GB
(2097152 sectors) inclusive.
@@ -239,7 +242,7 @@ ii) Status
<transaction id> <used metadata blocks>/<total metadata blocks>
<used data blocks>/<total data blocks> <held metadata root>
-
+ [no_]discard_passdown ro|rw
transaction id:
A 64-bit number used by userspace to help synchronise with metadata
@@ -257,6 +260,21 @@ ii) Status
held root. This feature is not yet implemented so '-' is
always returned.
+ discard_passdown|no_discard_passdown
+ Whether or not discards are actually being passed down to the
+ underlying device. When this is enabled when loading the table,
+ it can get disabled if the underlying device doesn't support it.
+
+ ro|rw
+ If the pool encounters certain types of device failures it will
+ drop into a read-only metadata mode in which no changes to
+ the pool metadata (like allocating new blocks) are permitted.
+
+ In serious cases where even a read-only mode is deemed unsafe
+ no further I/O will be permitted and the status will just
+ contain the string 'Fail'. The userspace recovery tools
+ should then be used.
+
iii) Messages
create_thin <dev id>
@@ -329,3 +347,7 @@ regain some space then send the 'trim' message to the pool.
ii) Status
<nr mapped sectors> <highest mapped sector>
+
+ If the pool has encountered device errors and failed, the status
+ will just contain the string 'Fail'. The userspace recovery
+ tools should then be used.
diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c
index 98c50f9626d8..087e9b34d290 100644
--- a/drivers/md/dm-thin.c
+++ b/drivers/md/dm-thin.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2011 Red Hat UK.
+ * Copyright (C) 2011-2012 Red Hat UK.
*
* This file is released under the GPL.
*/
@@ -496,12 +496,27 @@ static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
*/
struct dm_thin_new_mapping;
+/*
+ * The pool runs in 3 modes. Ordered in degraded order for comparisons.
+ */
+enum pool_mode {
+ PM_WRITE, /* metadata may be changed */
+ PM_READ_ONLY, /* metadata may not be changed */
+ PM_FAIL, /* all I/O fails */
+};
+
struct pool_features {
+ enum pool_mode mode;
+
unsigned zero_new_blocks:1;
unsigned discard_enabled:1;
unsigned discard_passdown:1;
};
+struct thin_c;
+typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
+typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
+
struct pool {
struct list_head list;
struct dm_target *ti; /* Only set if a pool target is bound */
@@ -542,8 +557,17 @@ struct pool {
struct dm_thin_new_mapping *next_mapping;
mempool_t *mapping_pool;
mempool_t *endio_hook_pool;
+
+ process_bio_fn process_bio;
+ process_bio_fn process_discard;
+
+ process_mapping_fn process_prepared_mapping;
+ process_mapping_fn process_prepared_discard;
};
+static enum pool_mode get_pool_mode(struct pool *pool);
+static void set_pool_mode(struct pool *pool, enum pool_mode mode);
+
/*
* Target context for a pool.
*/
@@ -718,16 +742,28 @@ static void issue(struct thin_c *tc, struct bio *bio)
struct pool *pool = tc->pool;
unsigned long flags;
+ if (!bio_triggers_commit(tc, bio)) {
+ generic_make_request(bio);
+ return;
+ }
+
/*
- * Batch together any FUA/FLUSH bios we find and then issue
- * a single commit for them in process_deferred_bios().
+ * Complete bio with an error if earlier I/O caused changes to
+ * the metadata that can't be committed e.g, due to I/O errors
+ * on the metadata device.
*/
- if (bio_triggers_commit(tc, bio)) {
- spin_lock_irqsave(&pool->lock, flags);
- bio_list_add(&pool->deferred_flush_bios, bio);
- spin_unlock_irqrestore(&pool->lock, flags);
- } else
- generic_make_request(bio);
+ if (dm_thin_aborted_changes(tc->td)) {
+ bio_io_error(bio);
+ return;
+ }
+
+ /*
+ * Batch together any bios that trigger commits and then issue a
+ * single commit for them in process_deferred_bios().
+ */
+ spin_lock_irqsave(&pool->lock, flags);
+ bio_list_add(&pool->deferred_flush_bios, bio);
+ spin_unlock_irqrestore(&pool->lock, flags);
}
static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
@@ -864,6 +900,14 @@ static void cell_defer_except(struct thin_c *tc, struct dm_bio_prison_cell *cell
wake_worker(pool);
}
+static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
+{
+ if (m->bio)
+ m->bio->bi_end_io = m->saved_bi_end_io;
+ cell_error(m->cell);
+ list_del(&m->list);
+ mempool_free(m, m->tc->pool->mapping_pool);
+}
static void process_prepared_mapping(struct dm_thin_new_mapping *m)
{
struct thin_c *tc = m->tc;
@@ -908,18 +952,20 @@ static void process_prepared_mapping(struct dm_thin_new_mapping *m)
mempool_free(m, tc->pool->mapping_pool);
}
-static void process_prepared_discard(struct dm_thin_new_mapping *m)
+static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
{
- int r;
struct thin_c *tc = m->tc;
- r = dm_thin_remove_block(tc->td, m->virt_block);
- if (r)
- DMERR("dm_thin_remove_block() failed");
+ bio_io_error(m->bio);
+ cell_defer_except(tc, m->cell);
+ cell_defer_except(tc, m->cell2);
+ mempool_free(m, tc->pool->mapping_pool);
+}
+
+static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
+{
+ struct thin_c *tc = m->tc;
- /*
- * Pass the discard down to the underlying device?
- */
if (m->pass_discard)
remap_and_issue(tc, m->bio, m->data_block);
else
@@ -930,8 +976,20 @@ static void process_prepared_discard(struct dm_thin_new_mapping *m)
mempool_free(m, tc->pool->mapping_pool);
}
+static void process_prepared_discard(struct dm_thin_new_mapping *m)
+{
+ int r;
+ struct thin_c *tc = m->tc;
+
+ r = dm_thin_remove_block(tc->td, m->virt_block);
+ if (r)
+ DMERR("dm_thin_remove_block() failed");
+
+ process_prepared_discard_passdown(m);
+}
+
static void process_prepared(struct pool *pool, struct list_head *head,
- void (*fn)(struct dm_thin_new_mapping *))
+ process_mapping_fn *fn)
{
unsigned long flags;
struct list_head maps;
@@ -943,7 +1001,7 @@ static void process_prepared(struct pool *pool, struct list_head *head,
spin_unlock_irqrestore(&pool->lock, flags);
list_for_each_entry_safe(m, tmp, &maps, list)
- fn(m);
+ (*fn)(m);
}
/*
@@ -1109,6 +1167,35 @@ static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
}
}
+static int commit(struct pool *pool)
+{
+ int r;
+
+ r = dm_pool_commit_metadata(pool->pmd);
+ if (r)
+ DMERR("commit failed, error = %d", r);
+
+ return r;
+}
+
+/*
+ * A non-zero return indicates read_only or fail_io mode.
+ * Many callers don't care about the return value.
+ */
+static int commit_or_fallback(struct pool *pool)
+{
+ int r;
+
+ if (get_pool_mode(pool) != PM_WRITE)
+ return -EINVAL;
+
+ r = commit(pool);
+ if (r)
+ set_pool_mode(pool, PM_READ_ONLY);
+
+ return r;
+}
+
static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
{
int r;
@@ -1137,12 +1224,7 @@ static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
* Try to commit to see if that will free up some
* more space.
*/
- r = dm_pool_commit_metadata(pool->pmd);
- if (r) {
- DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
- __func__, r);
- return r;
- }
+ (void) commit_or_fallback(pool);
r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
if (r)
@@ -1373,6 +1455,7 @@ static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block
default:
DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
+ set_pool_mode(tc->pool, PM_READ_ONLY);
cell_error(cell);
break;
}
@@ -1430,6 +1513,49 @@ static void process_bio(struct thin_c *tc, struct bio *bio)
}
}
+static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
+{
+ int r;
+ int rw = bio_data_dir(bio);
+ dm_block_t block = get_bio_block(tc, bio);
+ struct dm_thin_lookup_result lookup_result;
+
+ r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
+ switch (r) {
+ case 0:
+ if (lookup_result.shared && (rw == WRITE) && bio->bi_size)
+ bio_io_error(bio);
+ else
+ remap_and_issue(tc, bio, lookup_result.block);
+ break;
+
+ case -ENODATA:
+ if (rw != READ) {
+ bio_io_error(bio);
+ break;
+ }
+
+ if (tc->origin_dev) {
+ remap_to_origin_and_issue(tc, bio);
+ break;
+ }
+
+ zero_fill_bio(bio);
+ bio_endio(bio, 0);
+ break;
+
+ default:
+ DMERR("dm_thin_find_block() failed, error = %d", r);
+ bio_io_error(bio);
+ break;
+ }
+}
+
+static void process_bio_fail(struct thin_c *tc, struct bio *bio)
+{
+ bio_io_error(bio);
+}
+
static int need_commit_due_to_time(struct pool *pool)
{
return jiffies < pool->last_commit_jiffies ||
@@ -1441,7 +1567,6 @@ static void process_deferred_bios(struct pool *pool)
unsigned long flags;
struct bio *bio;
struct bio_list bios;
- int r;
bio_list_init(&bios);
@@ -1468,9 +1593,9 @@ static void process_deferred_bios(struct pool *pool)
}
if (bio->bi_rw & REQ_DISCARD)
- process_discard(tc, bio);
+ pool->process_discard(tc, bio);
else
- process_bio(tc, bio);
+ pool->process_bio(tc, bio);
}
/*
@@ -1486,10 +1611,7 @@ static void process_deferred_bios(struct pool *pool)
if (bio_list_empty(&bios) && !need_commit_due_to_time(pool))
return;
- r = dm_pool_commit_metadata(pool->pmd);
- if (r) {
- DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
- __func__, r);
+ if (commit_or_fallback(pool)) {
while ((bio = bio_list_pop(&bios)))
bio_io_error(bio);
return;
@@ -1504,8 +1626,8 @@ static void do_worker(struct work_struct *ws)
{
struct pool *pool = container_of(ws, struct pool, worker);
- process_prepared(pool, &pool->prepared_mappings, process_prepared_mapping);
- process_prepared(pool, &pool->prepared_discards, process_prepared_discard);
+ process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
+ process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
process_deferred_bios(pool);
}
@@ -1522,6 +1644,52 @@ static void do_waker(struct work_struct *ws)
/*----------------------------------------------------------------*/
+static enum pool_mode get_pool_mode(struct pool *pool)
+{
+ return pool->pf.mode;
+}
+
+static void set_pool_mode(struct pool *pool, enum pool_mode mode)
+{
+ int r;
+
+ pool->pf.mode = mode;
+
+ switch (mode) {
+ case PM_FAIL:
+ DMERR("switching pool to failure mode");
+ pool->process_bio = process_bio_fail;
+ pool->process_discard = process_bio_fail;
+ pool->process_prepared_mapping = process_prepared_mapping_fail;
+ pool->process_prepared_discard = process_prepared_discard_fail;
+ break;
+
+ case PM_READ_ONLY:
+ DMERR("switching pool to read-only mode");
+ r = dm_pool_abort_metadata(pool->pmd);
+ if (r) {
+ DMERR("aborting transaction failed");
+ set_pool_mode(pool, PM_FAIL);
+ } else {
+ dm_pool_metadata_read_only(pool->pmd);
+ pool->process_bio = process_bio_read_only;
+ pool->process_discard = process_discard;
+ pool->process_prepared_mapping = process_prepared_mapping_fail;
+ pool->process_prepared_discard = process_prepared_discard_passdown;
+ }
+ break;
+
+ case PM_WRITE:
+ pool->process_bio = process_bio;
+ pool->process_discard = process_discard;
+ pool->process_prepared_mapping = process_prepared_mapping;
+ pool->process_prepared_discard = process_prepared_discard;
+ break;
+ }
+}
+
+/*----------------------------------------------------------------*/
+
/*
* Mapping functions.
*/
@@ -1567,6 +1735,12 @@ static int thin_bio_map(struct dm_target *ti, struct bio *bio,
struct dm_thin_lookup_result result;
map_context->ptr = thin_hook_bio(tc, bio);
+
+ if (get_pool_mode(tc->pool) == PM_FAIL) {
+ bio_io_error(bio);
+ return DM_MAPIO_SUBMITTED;
+ }
+
if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
thin_defer_bio(tc, bio);
return DM_MAPIO_SUBMITTED;
@@ -1603,14 +1777,35 @@ static int thin_bio_map(struct dm_target *ti, struct bio *bio,
break;
case -ENODATA:
+ if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
+ /*
+ * This block isn't provisioned, and we have no way
+ * of doing so. Just error it.
+ */
+ bio_io_error(bio);
+ r = DM_MAPIO_SUBMITTED;
+ break;
+ }
+ /* fall through */
+
+ case -EWOULDBLOCK:
/*
* In future, the failed dm_thin_find_block above could
* provide the hint to load the metadata into cache.
*/
- case -EWOULDBLOCK:
thin_defer_bio(tc, bio);
r = DM_MAPIO_SUBMITTED;
break;
+
+ default:
+ /*
+ * Must always call bio_io_error on failure.
+ * dm_thin_find_block can fail with -EINVAL if the
+ * pool is switched to fail-io mode.
+ */
+ bio_io_error(bio);
+ r = DM_MAPIO_SUBMITTED;
+ break;
}
return r;
@@ -1647,15 +1842,26 @@ static int bind_control_target(struct pool *pool, struct dm_target *ti)
{
struct pool_c *pt = ti->private;
+ /*
+ * We want to make sure that degraded pools are never upgraded.
+ */
+ enum pool_mode old_mode = pool->pf.mode;
+ enum pool_mode new_mode = pt->pf.mode;
+
+ if (old_mode > new_mode)
+ new_mode = old_mode;
+
pool->ti = ti;
pool->low_water_blocks = pt->low_water_blocks;
pool->pf = pt->pf;
+ set_pool_mode(pool, new_mode);
/*
* If discard_passdown was enabled verify that the data device
* supports discards. Disable discard_passdown if not; otherwise
* -EOPNOTSUPP will be returned.
*/
+ /* FIXME: pull this out into a sep fn. */
if (pt->pf.discard_passdown) {
struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
if (!q || !blk_queue_discard(q)) {
@@ -1681,6 +1887,7 @@ static void unbind_control_target(struct pool *pool, struct dm_target *ti)
/* Initialize pool features. */
static void pool_features_init(struct pool_features *pf)
{
+ pf->mode = PM_WRITE;
pf->zero_new_blocks = 1;
pf->discard_enabled = 1;
pf->discard_passdown = 1;
@@ -1711,14 +1918,16 @@ static struct kmem_cache *_endio_hook_cache;
static struct pool *pool_create(struct mapped_device *pool_md,
struct block_device *metadata_dev,
- unsigned long block_size, char **error)
+ unsigned long block_size,
+ int read_only, char **error)
{
int r;
void *err_p;
struct pool *pool;
struct dm_pool_metadata *pmd;
+ bool format_device = read_only ? false : true;
- pmd = dm_pool_metadata_open(metadata_dev, block_size, true);
+ pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
if (IS_ERR(pmd)) {
*error = "Error creating metadata object";
return (struct pool *)pmd;
@@ -1835,8 +2044,8 @@ static void __pool_dec(struct pool *pool)
static struct pool *__pool_find(struct mapped_device *pool_md,
struct block_device *metadata_dev,
- unsigned long block_size, char **error,
- int *created)
+ unsigned long block_size, int read_only,
+ char **error, int *created)
{
struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
@@ -1857,7 +2066,7 @@ static struct pool *__pool_find(struct mapped_device *pool_md,
__pool_inc(pool);
} else {
- pool = pool_create(pool_md, metadata_dev, block_size, error);
+ pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
*created = 1;
}
}
@@ -1908,19 +2117,23 @@ static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
arg_name = dm_shift_arg(as);
argc--;
- if (!strcasecmp(arg_name, "skip_block_zeroing")) {
+ if (!strcasecmp(arg_name, "skip_block_zeroing"))
pf->zero_new_blocks = 0;
- continue;
- } else if (!strcasecmp(arg_name, "ignore_discard")) {
+
+ else if (!strcasecmp(arg_name, "ignore_discard"))
pf->discard_enabled = 0;
- continue;
- } else if (!strcasecmp(arg_name, "no_discard_passdown")) {
+
+ else if (!strcasecmp(arg_name, "no_discard_passdown"))
pf->discard_passdown = 0;
- continue;
- }
- ti->error = "Unrecognised pool feature requested";
- r = -EINVAL;
+ else if (!strcasecmp(arg_name, "read_only"))
+ pf->mode = PM_READ_ONLY;
+
+ else {
+ ti->error = "Unrecognised pool feature requested";
+ r = -EINVAL;
+ break;
+ }
}
return r;
@@ -2013,7 +2226,7 @@ static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
}
pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
- block_size, &ti->error, &pool_created);
+ block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
if (IS_ERR(pool)) {
r = PTR_ERR(pool);
goto out_free_pt;
@@ -2146,15 +2359,12 @@ static int pool_preresume(struct dm_target *ti)
r = dm_pool_resize_data_dev(pool->pmd, data_size);
if (r) {
DMERR("failed to resize data device");
+ /* FIXME Stricter than necessary: Rollback transaction instead here */
+ set_pool_mode(pool, PM_READ_ONLY);
return r;
}
- r = dm_pool_commit_metadata(pool->pmd);
- if (r) {
- DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
- __func__, r);
- return r;
- }
+ (void) commit_or_fallback(pool);
}
return 0;
@@ -2177,19 +2387,12 @@ static void pool_resume(struct dm_target *ti)
static void pool_postsuspend(struct dm_target *ti)
{
- int r;
struct pool_c *pt = ti->private;
struct pool *pool = pt->pool;
cancel_delayed_work(&pool->waker);
flush_workqueue(pool->wq);
-
- r = dm_pool_commit_metadata(pool->pmd);
- if (r < 0) {
- DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
- __func__, r);
- /* FIXME: invalidate device? error the next FUA or FLUSH bio ?*/
- }
+ (void) commit_or_fallback(pool);
}
static int check_arg_count(unsigned argc, unsigned args_required)
@@ -2323,12 +2526,7 @@ static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct
if (r)
return r;
- r = dm_pool_commit_metadata(pool->pmd);
- if (r) {
- DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
- __func__, r);
- return r;
- }
+ (void) commit_or_fallback(pool);
r = dm_pool_reserve_metadata_snap(pool->pmd);
if (r)
@@ -2389,16 +2587,32 @@ static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
else
DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
- if (!r) {
- r = dm_pool_commit_metadata(pool->pmd);
- if (r)
- DMERR("%s message: dm_pool_commit_metadata() failed, error = %d",
- argv[0], r);
- }
+ if (!r)
+ (void) commit_or_fallback(pool);
return r;
}
+static void emit_flags(struct pool_features *pf, char *result,
+ unsigned sz, unsigned maxlen)
+{
+ unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
+ !pf->discard_passdown + (pf->mode == PM_READ_ONLY);
+ DMEMIT("%u ", count);
+
+ if (!pf->zero_new_blocks)
+ DMEMIT("skip_block_zeroing ");
+
+ if (!pf->discard_enabled)
+ DMEMIT("ignore_discard ");
+
+ if (!pf->discard_passdown)
+ DMEMIT("no_discard_passdown ");
+
+ if (pf->mode == PM_READ_ONLY)
+ DMEMIT("read_only ");
+}
+
/*
* Status line is:
* <transaction id> <used metadata sectors>/<total metadata sectors>
@@ -2407,7 +2621,7 @@ static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
static int pool_status(struct dm_target *ti, status_type_t type,
char *result, unsigned maxlen)
{
- int r, count;
+ int r;
unsigned sz = 0;
uint64_t transaction_id;
dm_block_t nr_free_blocks_data;
@@ -2422,6 +2636,11 @@ static int pool_status(struct dm_target *ti, status_type_t type,
switch (type) {
case STATUSTYPE_INFO:
+ if (get_pool_mode(pool) == PM_FAIL) {
+ DMEMIT("Fail");
+ break;
+ }
+
r = dm_pool_get_metadata_transaction_id(pool->pmd,
&transaction_id);
if (r)
@@ -2457,9 +2676,19 @@ static int pool_status(struct dm_target *ti, status_type_t type,
(unsigned long long)nr_blocks_data);
if (held_root)
- DMEMIT("%llu", held_root);
+ DMEMIT("%llu ", held_root);
+ else
+ DMEMIT("- ");
+
+ if (pool->pf.mode == PM_READ_ONLY)
+ DMEMIT("ro ");
else
- DMEMIT("-");
+ DMEMIT("rw ");
+
+ if (pool->pf.discard_enabled && pool->pf.discard_passdown)
+ DMEMIT("discard_passdown");
+ else
+ DMEMIT("no_discard_passdown");
break;
@@ -2469,20 +2698,7 @@ static int pool_status(struct dm_target *ti, status_type_t type,
format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
(unsigned long)pool->sectors_per_block,
(unsigned long long)pt->low_water_blocks);
-
- count = !pool->pf.zero_new_blocks + !pool->pf.discard_enabled +
- !pt->pf.discard_passdown;
- DMEMIT("%u ", count);
-
- if (!pool->pf.zero_new_blocks)
- DMEMIT("skip_block_zeroing ");
-
- if (!pool->pf.discard_enabled)
- DMEMIT("ignore_discard ");
-
- if (!pt->pf.discard_passdown)
- DMEMIT("no_discard_passdown ");
-
+ emit_flags(&pt->pf, result, sz, maxlen);
break;
}
@@ -2542,7 +2758,7 @@ static struct target_type pool_target = {
.name = "thin-pool",
.features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
DM_TARGET_IMMUTABLE,
- .version = {1, 2, 0},
+ .version = {1, 3, 0},
.module = THIS_MODULE,
.ctr = pool_ctr,
.dtr = pool_dtr,
@@ -2647,6 +2863,11 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
}
__pool_inc(tc->pool);
+ if (get_pool_mode(tc->pool) == PM_FAIL) {
+ ti->error = "Couldn't open thin device, Pool is in fail mode";
+ goto bad_thin_open;
+ }
+
r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
if (r) {
ti->error = "Couldn't open thin internal device";
@@ -2755,6 +2976,11 @@ static int thin_status(struct dm_target *ti, status_type_t type,
char buf[BDEVNAME_SIZE];
struct thin_c *tc = ti->private;
+ if (get_pool_mode(tc->pool) == PM_FAIL) {
+ DMEMIT("Fail");
+ return 0;
+ }
+
if (!tc->td)
DMEMIT("-");
else {
@@ -2823,7 +3049,7 @@ static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
static struct target_type thin_target = {
.name = "thin",
- .version = {1, 2, 0},
+ .version = {1, 3, 0},
.module = THIS_MODULE,
.ctr = thin_ctr,
.dtr = thin_dtr,
]
|
| 22/2 |
2026/11/23 11:03 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 42992
output: 173
thoughts: 1803
Thoughts: **My Deep Dive into the dm-thin Bug: Tracing the Roots and Unraveling the Conundrum**
I've been meticulously dissecting the history and the exact sequence of events that led to this bug. My focus has been on understanding how `upgrade_mode()` operated, particularly before and after the commit `86f1152b117a` in 2014, which aimed to allow active and inactive tables to share `dm_devs`.
Looking at the diff for `86f1152b117a` in `drivers/md/dm-table.c`, I see a fundamental shift. Before this commit, `struct dm_dev_internal` embedded `struct dm_dev dm_dev` directly. This meant that `dm_get_device()` returned a pointer to this embedded `dm_dev`. When `upgrade_mode()` was invoked then, it involved copying the entire `dm_dev_internal` structure (`dd_new = dd_old = *dd;`). Crucially, `open_dev` was called with `dd_new`, which would open a new block device. However, the `dd->dm_dev.bdev` pointer was not correctly updated to reflect the newly opened device, nor was it set to NULL. Furthermore, `close_dev(&dd_old, md)` was called on the *old* `dm_dev_internal`, which, because `dd->dm_dev` was embedded and `dd_new.dm_dev.bdev` wasn't properly handled, seems to have left the `bdev` reference in a state where it might not have been decremented as expected, or the same `struct block_device *` was retained. This meant `dd->dm_dev.bdev` often pointed to an invalid or unmanaged `bdev`, and the original `bdev` wasn't freed.
The commit `86f1152b117a` changed `dd->dm_dev` from an embedded struct to a pointer (`struct dm_dev *dm_dev`) within `struct table_device`. This was intended to enable shared `dm_devs`. However, the `upgrade_mode()` logic within this commit introduced a new problem: it allocated a *new* `table_device` and then called `dm_put_table_device(md, old_dev)`, which would free the *old* `table_device`. If an earlier `dm_get_device()` call in the same table had returned a `struct dm_dev *` pointer, this pointer now pointed to freed memory, becoming a dangling pointer.
My investigation then broadened to the other side of this interaction, specifically `dm_thin.c`. A commit in 2013, `5d0db96d13a4` ("dm thin: open dev read only when possible"), modified `pool_ctr` to open `metadata_dev` with `BLK_OPEN_READ` (and `FMODE_READ`) when the `read_only` feature was active, while `data_dev` was opened with `FMODE_READ | FMODE_WRITE`. The crucial oversight here was that *before* `86f1152b117a`, `pool_ctr` had no check to ensure that `argv[0]` (metadata) and `argv[1]` (data) were different devices.
So, here's the critical link: If the same device was specified for both metadata (read-only) and data (read-write) when creating a thin pool, the pre-`86f1152b117a` `upgrade_mode()` wouldn't crash immediately because the `dm_dev` was embedded and the `bdev` reference handling was… murky, but perhaps didn't lead to an immediate invalidation in all cases. However, *after* `86f1152b117a`, when `upgrade_mode()` started freeing the old `table_device` and managing `dm_dev` as pointers, and combined with the ability for metadata and data devices to be the same (due to the lack of checks in `pool_ctr`), opening the same device for both read-only metadata and read-write data would lead to the dangling pointer issue when `upgrade_mode` was subsequently called.
It appears that this exact vulnerability was encountered in 2019, leading to the commit `70de2cbda8a5` ("dm thin: add sanity checks to thin-pool and external snapshot creation"), which attempted to add sanity checks. My next step is to meticulously review all commits related to `70de2cbda8a5`, `upgrade_mode`, `dm_get_device`, `dm-thin.c`, `dm-snap.c`, and `dm-table.c` to ensure I haven't missed any subtle interactions or dependencies. I'm also looking at older commits like `4df2bf466a9c` and functions like `dm_devt_from_path` to build a comprehensive understanding.
|
| 23/2 |
2026/11/23 11:03 |
tool |
git-log |
0m
Args: map[Count:20 MessageRegexps:[upgrade_mode|dm_get_device|dm_get_dev_t|same device|different devices] PathPrefix:drivers/md/]
Results:
|
| 24/2 |
2026/11/23 11:03 |
tool |
git-show |
0m
Args:
Results: map[Output:commit a21f9edb13b0d8066775cbd5efa7261e41871182
Author: Benjamin Marzinski <bmarzins@redhat.com>
Date: Thu Jul 4 16:17:15 2024 +0200
dm: factor out helper function from dm_get_device
Factor out a helper function, dm_devt_from_path(), from dm_get_device()
for use in dm targets.
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c
index 33b7a1844ed4..eea41e38d87e 100644
--- a/drivers/md/dm-table.c
+++ b/drivers/md/dm-table.c
@@ -331,23 +331,15 @@ static int upgrade_mode(struct dm_dev_internal *dd, blk_mode_t new_mode,
}
/*
- * Add a device to the list, or just increment the usage count if
- * it's already present.
- *
* Note: the __ref annotation is because this function can call the __init
* marked early_lookup_bdev when called during early boot code from dm-init.c.
*/
-int __ref dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode,
- struct dm_dev **result)
+int __ref dm_devt_from_path(const char *path, dev_t *dev_p)
{
int r;
dev_t dev;
unsigned int major, minor;
char dummy;
- struct dm_dev_internal *dd;
- struct dm_table *t = ti->table;
-
- BUG_ON(!t);
if (sscanf(path, "%u:%u%c", &major, &minor, &dummy) == 2) {
/* Extract the major/minor numbers */
@@ -363,6 +355,29 @@ int __ref dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode,
if (r)
return r;
}
+ *dev_p = dev;
+ return 0;
+}
+EXPORT_SYMBOL(dm_devt_from_path);
+
+/*
+ * Add a device to the list, or just increment the usage count if
+ * it's already present.
+ */
+int dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode,
+ struct dm_dev **result)
+{
+ int r;
+ dev_t dev;
+ struct dm_dev_internal *dd;
+ struct dm_table *t = ti->table;
+
+ BUG_ON(!t);
+
+ r = dm_devt_from_path(path, &dev);
+ if (r)
+ return r;
+
if (dev == disk_devt(t->md->disk))
return -EINVAL;
diff --git a/include/linux/device-mapper.h b/include/linux/device-mapper.h
index 4ba2e73993bd..384649a61bfa 100644
--- a/include/linux/device-mapper.h
+++ b/include/linux/device-mapper.h
@@ -179,6 +179,11 @@ int dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode,
struct dm_dev **result);
void dm_put_device(struct dm_target *ti, struct dm_dev *d);
+/*
+ * Helper function for getting devices
+ */
+int dm_devt_from_path(const char *path, dev_t *dev_p);
+
/*
* Information about a target type
*/
]
|
| 25/2 |
2026/11/23 11:03 |
tool |
git-show |
0m
Args:
Results: map[Output:commit d4a28d7defe79006e59293a4b43d518ba8483fb0
Author: Christoph Hellwig <hch@lst.de>
Date: Wed May 31 14:55:30 2023 +0200
dm: remove dm_get_dev_t
Open code dm_get_dev_t in the only remaining caller, and propagate the
exact error code from lookup_bdev and early_lookup_bdev.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20230531125535.676098-20-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c
index 05aa16da43b0..1576b408768d 100644
--- a/drivers/md/dm-table.c
+++ b/drivers/md/dm-table.c
@@ -323,20 +323,6 @@ static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
return 0;
}
-/*
- * Convert the path to a device
- */
-dev_t dm_get_dev_t(const char *path)
-{
- dev_t dev;
-
- if (lookup_bdev(path, &dev) &&
- early_lookup_bdev(path, &dev))
- return 0;
- return dev;
-}
-EXPORT_SYMBOL_GPL(dm_get_dev_t);
-
/*
* Add a device to the list, or just increment the usage count if
* it's already present.
@@ -359,9 +345,11 @@ int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
if (MAJOR(dev) != major || MINOR(dev) != minor)
return -EOVERFLOW;
} else {
- dev = dm_get_dev_t(path);
- if (!dev)
- return -ENODEV;
+ r = lookup_bdev(path, &dev);
+ if (r)
+ r = early_lookup_bdev(path, &dev);
+ if (r)
+ return r;
}
if (dev == disk_devt(t->md->disk))
return -EINVAL;
diff --git a/include/linux/device-mapper.h b/include/linux/device-mapper.h
index a52d2b9a6846..c27b84002d83 100644
--- a/include/linux/device-mapper.h
+++ b/include/linux/device-mapper.h
@@ -170,8 +170,6 @@ struct dm_dev {
char name[16];
};
-dev_t dm_get_dev_t(const char *path);
-
/*
* Constructors should call these functions to ensure destination devices
* are opened/closed correctly.
]
|
| 26/2 |
2026/11/23 11:03 |
tool |
git-log |
0m
Args: map[Count:20 MessageRegexps:[70de2cbda8a5|86f1152b117a|5d0db96d13a4]]
Results:
|
| 27/2 |
2026/11/23 11:04 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 46927
output: 66
thoughts: 52
Thoughts: **My Investigative Process: Tracing `dm_devt_from_path`**
Alright, to get a handle on how `dm_devt_from_path` is being utilized, I need to dive into the code. My immediate thought is to pinpoint all the call sites within the `drivers/md/` directory. This will give me a comprehensive view of where this function is invoked. Following that, I intend to use `git log` to examine the history of changes specifically within `drivers/md/`, focusing on the period around commit `a21f9edb13b0`. This will help me understand the evolution of the code related to device identification and potentially uncover the rationale behind specific implementations or refactorings concerning `dm_devt_from_path`.
|
| 28/2 |
2026/11/23 11:04 |
tool |
codesearch-find-references |
0m
Args: map[Name:dm_devt_from_path]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:dm_get_device SourceFile:drivers/md/dm-table.c SourceLine:375] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:multipath_message SourceFile:drivers/md/dm-mpath.c SourceLine:2007] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__UNIQUE_ID_addressable_dm_devt_from_path_554 SourceFile:drivers/md/dm-table.c SourceLine:359] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_dm_devt_from_path SourceFile:drivers/md/dm-table.c SourceLine:359] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_dm_devt_from_path SourceFile:drivers/md/dm-table.c SourceLine:359]] TruncatedOutput:false]
|
| 29/2 |
2026/11/23 11:04 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:drivers/md/ Since:2 years]
Results: map[Output:801503b9a66f block: always adjust bi_offset on bio_advance_iter
334714e4bdf8 dm-verity: clear LSM integrity state on suspend
17f7f7ef5dc2 dm-bufio: clean-up cache size reading
005d2ec5723c dm-bufio: fix a race leading to an unsigned integer underflow
1190dbeaeaa0 dm-bufio: fix non-working memory limit
1041fca2ee8a keys: add KEY_SPEC_DM_VERITY_KEYRING
bbd6f974d038 dm-snapshot: make the chunk list sorted by the chunk number
829c4d21148a dm-snapshot: fix crash if dm_io_client_create fails
1acc65c30f85 dm-crypt: allow encryption sector size up to PAGE_SIZE
17383c7b81b9 dm-mpath: check values in hst_status while holding spinlock
eba776a4d9e4 dm-mpath: standardize repeat_count handling in the HST path selector
fe9d8c26ab25 dm-mpath: streamline setup work in the HST path selector
effd0cf5f526 md: remove the last use of page_buffers()
5d0f54ab925f md: use folio APIs in free_page()
ebb7205efe61 md: use folio_alloc_buffers()
d1472ca2ca5d md: don't hand out the array before md_alloc() has added mddev->kobj
4c90b76e0198 md: Fix the null-ptr-deref of 'mddev->private' while submitting IO
73b0df929494 md/raid5: protect big_stripe_tree lookup with RCU
af560e357a81 md/raid5: drain released_stripes before destroying the cache
900bf42e0ff8 md: take reconfig_mutex when enabling PPL on an inactive array
]
|
| 30/2 |
2026/11/23 11:04 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 47895
output: 83
thoughts: 99
Thoughts: **Initial Diagnosis and Investigation Strategy**
My immediate thought is that I need to pinpoint the exact source of a potential issue, and my suspicion is leaning towards either the `dm_get_device` function or something within the `pool_ctr` (constructor) itself, particularly in the context of device mapper (dm) targets. To do this thoroughly, I need to dive into the code. My plan is to meticulously examine `dm-snap.c` and other relevant dm target files. The key is to understand precisely how `dm_get_device` is being called and utilized within these components. Crucially, I intend to trace the evolution of this code by identifying and analyzing specific commits. My first concrete step will be to scrutinize `drivers/md/dm-snap.c`, focusing on the `snapshot_ctr` function, to see how the changes introduced by commit `4df2bf466a9c` were subsequently modified. This detailed, commit-by-commit analysis is essential for a precise understanding of the problem's origin.
|
| 31/2 |
2026/11/23 11:04 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/md/dm-snap.c Name:snapshot_ctr]
Results: map[SourceCode:1226: /*
1227: * Construct a snapshot mapping:
1228: * <origin_dev> <COW-dev> <p|po|n> <chunk-size> [<# feature args> [<arg>]*]
1229: */
1230: static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1231: {
1232: struct dm_snapshot *s;
1233: struct dm_arg_set as;
1234: int i;
1235: int r = -EINVAL;
1236: char *origin_path, *cow_path;
1237: unsigned int args_used, num_flush_bios = 1;
1238: blk_mode_t origin_mode = BLK_OPEN_READ;
1239:
1240: if (argc < 4) {
1241: ti->error = "requires 4 or more arguments";
1242: r = -EINVAL;
1243: goto bad;
1244: }
1245:
1246: if (dm_target_is_snapshot_merge(ti)) {
1247: num_flush_bios = 2;
1248: origin_mode = BLK_OPEN_WRITE;
1249: }
1250:
1251: s = kzalloc_obj(*s);
1252: if (!s) {
1253: ti->error = "Cannot allocate private snapshot structure";
1254: r = -ENOMEM;
1255: goto bad;
1256: }
1257:
1258: as.argc = argc;
1259: as.argv = argv;
1260: dm_consume_args(&as, 4);
1261: r = parse_snapshot_features(&as, s, ti);
1262: if (r)
1263: goto bad_features;
1264:
1265: origin_path = argv[0];
1266: argv++;
1267: argc--;
1268:
1269: r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
1270: if (r) {
1271: ti->error = "Cannot get origin device";
1272: goto bad_origin;
1273: }
1274:
1275: cow_path = argv[0];
1276: argv++;
1277: argc--;
1278:
1279: r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
1280: if (r) {
1281: ti->error = "Cannot get COW device";
1282: goto bad_cow;
1283: }
1284: if (s->cow->bdev && s->cow->bdev == s->origin->bdev) {
1285: ti->error = "COW device cannot be the same as origin device";
1286: r = -EINVAL;
1287: goto bad_store;
1288: }
1289:
1290: r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1291: if (r) {
1292: ti->error = "Couldn't create exception store";
1293: r = -EINVAL;
1294: goto bad_store;
1295: }
1296:
1297: /*
1298: * snapshot-merge requires the optional prepare_merge/commit_merge
1299: * callbacks; the transient exception store does not implement them.
1300: * The handover path checks this in __validate_exception_handover(),
1301: * but a directly constructed snapshot-merge never goes through it.
1302: */
1303: if (dm_target_is_snapshot_merge(ti) &&
1304: (!s->store->type->prepare_merge ||
1305: !s->store->type->commit_merge)) {
1306: ti->error = "Snapshot exception store does not support snapshot-merge.";
1307: r = -EINVAL;
1308: goto bad_hash_tables;
1309: }
1310:
1311: argv += args_used;
1312: argc -= args_used;
1313:
1314: s->ti = ti;
1315: s->valid = 1;
1316: s->snapshot_overflowed = 0;
1317: s->active = 0;
1318: atomic_set(&s->pending_exceptions_count, 0);
1319: spin_lock_init(&s->pe_allocation_lock);
1320: s->exception_start_sequence = 0;
1321: s->exception_complete_sequence = 0;
1322: s->out_of_order_tree = RB_ROOT;
1323: init_rwsem(&s->lock);
1324: INIT_LIST_HEAD(&s->list);
1325: spin_lock_init(&s->pe_lock);
1326: s->state_bits = 0;
1327: s->merge_failed = false;
1328: s->first_merging_chunk = 0;
1329: s->num_merging_chunks = 0;
1330: bio_list_init(&s->bios_queued_during_merge);
1331:
1332: /* Allocate hash table for COW data */
1333: if (init_hash_tables(s)) {
1334: ti->error = "Unable to allocate hash table space";
1335: r = -ENOMEM;
1336: goto bad_hash_tables;
1337: }
1338:
1339: init_waitqueue_head(&s->in_progress_wait);
1340:
1341: s->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
1342: if (IS_ERR(s->kcopyd_client)) {
1343: r = PTR_ERR(s->kcopyd_client);
1344: ti->error = "Could not create kcopyd client";
1345: goto bad_kcopyd;
1346: }
1347:
1348: r = mempool_init_slab_pool(&s->pending_pool, MIN_IOS, pending_cache);
1349: if (r) {
1350: ti->error = "Could not allocate mempool for pending exceptions";
1351: goto bad_pending_pool;
1352: }
1353:
1354: for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1355: INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1356:
1357: spin_lock_init(&s->tracked_chunk_lock);
1358:
1359: ti->private = s;
1360: ti->num_flush_bios = num_flush_bios;
1361: if (s->discard_zeroes_cow)
1362: ti->num_discard_bios = (s->discard_passdown_origin ? 2 : 1);
1363: ti->per_io_data_size = sizeof(struct dm_snap_tracked_chunk);
1364:
1365: /* Add snapshot to the list of snapshots for this origin */
1366: /* Exceptions aren't triggered till snapshot_resume() is called */
1367: r = register_snapshot(s);
1368: if (r == -ENOMEM) {
1369: ti->error = "Snapshot origin struct allocation failed";
1370: goto bad_load_and_register;
1371: } else if (r < 0) {
1372: /* invalid handover, register_snapshot has set ti->error */
1373: goto bad_load_and_register;
1374: }
1375:
1376: /*
1377: * Metadata must only be loaded into one table at once, so skip this
1378: * if metadata will be handed over during resume.
1379: * Chunk size will be set during the handover - set it to zero to
1380: * ensure it's ignored.
1381: */
1382: if (r > 0) {
1383: s->store->chunk_size = 0;
1384: return 0;
1385: }
1386:
1387: r = s->store->type->read_metadata(s->store, dm_add_exception,
1388: (void *)s);
1389: if (r < 0) {
1390: ti->error = "Failed to read snapshot metadata";
1391: goto bad_read_metadata;
1392: } else if (r > 0) {
1393: s->valid = 0;
1394: DMWARN("Snapshot is marked invalid.");
1395: }
1396:
1397: if (!s->store->chunk_size) {
1398: ti->error = "Chunk size not set";
1399: r = -EINVAL;
1400: goto bad_read_metadata;
1401: }
1402:
1403: r = dm_set_target_max_io_len(ti, s->store->chunk_size);
1404: if (r)
1405: goto bad_read_metadata;
1406:
1407: return 0;
1408:
1409: bad_read_metadata:
1410: unregister_snapshot(s);
1411: bad_load_and_register:
1412: mempool_exit(&s->pending_pool);
1413: bad_pending_pool:
1414: dm_kcopyd_client_destroy(s->kcopyd_client);
1415: bad_kcopyd:
1416: dm_exception_table_exit(&s->pending, pending_cache);
1417: dm_exception_table_exit(&s->complete, exception_cache);
1418: bad_hash_tables:
1419: dm_exception_store_destroy(s->store);
1420: bad_store:
1421: dm_put_device(ti, s->cow);
1422: bad_cow:
1423: dm_put_device(ti, s->origin);
1424: bad_origin:
1425: bad_features:
1426: kfree(s);
1427: bad:
1428: return r;
1429: }
SourceFile:drivers/md/dm-snap.c]
|
| 32/2 |
2026/11/23 11:04 |
tool |
git-log |
0m
Args: map[Count:15 SourcePath:drivers/md/dm-snap.c SymbolName:snapshot_ctr]
Results: map[Output:fcbde6227fcc dm-snap: reject the transient exception store for snapshot-merge
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
05bdb9965305 block: replace fmode_t with a block-specific type for block open flags
26110d5afe81 dm-snap: simplify the origin_dev == cow_dev check in snapshot_ctr
86a3238c7b9b dm: change "unsigned" to "unsigned int"
eba33b8ef1b9 dm-snap: use blkdev_issue_flush instead of open coding it
c699a0db2d62 dm snapshot: fix crash with transient storage and zero chunk size
fcc42338375a dm snapshot: flush merged data before committing metadata
1d1dda8ca8ca dm snapshot: use true/false for bool variable
b21555786f18 dm snapshot: rework COW throttling to fix deadlock
2e6023850e17 dm snapshot: add optional discard support features
3f1637f21038 dm snapshot: Use fine-grained locking scheme
4ad8d880b6c4 dm snapshot: Replace mutex with rw semaphore
721b1d98fb51 dm snapshot: Fix excessive memory usage and workqueue stalls
]
|
| 33/2 |
2026/11/23 11:04 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 51530
output: 31
thoughts: 39
Thoughts: **Exploring a Snapshot Optimization: Simplifying the Origin and COW Device Check**
Ah, yes, I've been perusing the commit titled `26110d5afe81 dm-snap: simplify the origin_dev == cow_dev check in snapshot_ctr`. This is precisely the kind of targeted, efficiency-focused change that grabs my attention. My immediate thought upon seeing this was, "Let's dig into this." I'm keen to understand the nuances of how the `dm-snap` module handles snapshot creation, particularly concerning the identification and differentiation of the origin device versus the copy-on-write (COW) device. The fact that this commit aims to "simplify" a specific check within the `snapshot_ctr` function suggests a potential for performance gains, reduced complexity, or perhaps even a subtle bug fix. I'm particularly interested in what this simplification entails. Is it removing redundant logic? Refactoring for clarity? Or perhaps a more fundamental alteration to how these devices are compared? Understanding the "why" behind this simplification is crucial, as it likely has downstream implications for how efficiently snapshots are managed and how robust the system is under various configurations. I'm eager to see the details of this change to fully appreciate its impact.
|
| 34/2 |
2026/11/23 11:04 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 26110d5afe8117d1b505fe735ac709bdf063f4da
Author: Christoph Hellwig <hch@lst.de>
Date: Wed May 31 14:55:28 2023 +0200
dm-snap: simplify the origin_dev == cow_dev check in snapshot_ctr
Use the block_device acquired in dm_get_device for the check instead
of doing an extra lookup.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Mike Snitzer <snitzer@kernel.org>
Link: https://lore.kernel.org/r/20230531125535.676098-18-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/md/dm-snap.c b/drivers/md/dm-snap.c
index 9c49f53760d0..7832974b73eb 100644
--- a/drivers/md/dm-snap.c
+++ b/drivers/md/dm-snap.c
@@ -1241,7 +1241,6 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
int i;
int r = -EINVAL;
char *origin_path, *cow_path;
- dev_t origin_dev, cow_dev;
unsigned int args_used, num_flush_bios = 1;
fmode_t origin_mode = FMODE_READ;
@@ -1279,24 +1278,21 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
ti->error = "Cannot get origin device";
goto bad_origin;
}
- origin_dev = s->origin->bdev->bd_dev;
cow_path = argv[0];
argv++;
argc--;
- cow_dev = dm_get_dev_t(cow_path);
- if (cow_dev && cow_dev == origin_dev) {
- ti->error = "COW device cannot be the same as origin device";
- r = -EINVAL;
- goto bad_cow;
- }
-
r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
if (r) {
ti->error = "Cannot get COW device";
goto bad_cow;
}
+ if (s->cow->bdev && s->cow->bdev == s->origin->bdev) {
+ ti->error = "COW device cannot be the same as origin device";
+ r = -EINVAL;
+ goto bad_store;
+ }
r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
if (r) {
]
|
| 35/2 |
2026/11/23 11:04 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 52406
output: 45
thoughts: 313
Thoughts: **Eureka! The Source of the Flaw Revealed!**
I've just had a moment of sheer, unadulterated clarity, a burst of almost giddy realization, as I stared at line `26110d5afe81` within `dm-snap.c`. It's actually quite amusing, in a darkly ironic way, to see how the logic unfolds. Here's the crux of it: even in the scenario where `origin_mode` is set to `BLK_OPEN_READ` and the `cow` (copy-on-write) mode is `RW` (read-write), the `dm_get_device(ti, cow_path, ...)` call proceeds to free `s->origin` via `upgrade_mode`. And it does this *before* the crucial check `if (s->cow->bdev && s->cow->bdev == s->origin->bdev)` is even evaluated! The implication here is clear – a potential dangling pointer, a subtle yet critical vulnerability.
But my immediate thought, once I'd savored that moment of discovery, was to sharpen my focus. While the current state of the code is fascinating, what I truly need is to pinpoint the exact commit that *introduced* this specific bug. This is not just about understanding the current problem; it's about dissecting its genesis.
So, I've begun meticulously examining the potential culprits, the commits that could have paved the way for this flaw. My mind is sifting through the candidates:
* Could it be `86f1152b117a ("dm: allow active and inactive tables to share dm_devs")` from 2014? This seems like a significant structural change, and changes in how devices are managed could certainly ripple through.
* Perhaps `5d0db96d13a4 ("dm thin: open dev read only when possible")` from 2013? Optimizations and changes in device opening behavior could also be a fertile ground for such an issue.
* Then there's `70de2cbda8a5 ("dm thin: add sanity checks to thin-pool and external snapshot creation")` from 2019. It's a bit later, but a change focused on sanity checks might have inadvertently introduced a new edge case.
* Or, could the bug lie even deeper, dating back to the very inception of `dm-thin.c` itself? I'm considering commits like `991d9fa02b40` or similar, the foundational pieces of this module.
To truly get to the bottom of this, my next logical step is to embark on a thorough historical investigation. I intend to trace the history of `dm-thin.c`, specifically focusing on the `pool_ctr` function, all the way back to its very first introduction. This deep dive into the past is crucial to definitively identify the precise commit that birthed this bug, allowing me to understand its context and devise the most effective solution.
|
| 36/2 |
2026/11/23 11:04 |
tool |
git-log |
0m
Args: map[Count:50 SourcePath:drivers/md/dm-thin.c SymbolName:pool_ctr]
Results: map[Output:bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
05bdb9965305 block: replace fmode_t with a block-specific type for block open flags
e2dd8aca2d76 dm bio prison v1: improve concurrent IO performance
9bbf5feecc7e dm thin: fix deadlock when swapping to thin device
86a3238c7b9b dm: change "unsigned" to "unsigned int"
3534e5a5ed29 dm thin: fix use-after-free crash in dm_sm_register_threshold_callback
21cf86614504 writeback: remove bdi->congested_fn
f06c03d1ded2 dm thin: change data device's flush_bio to be member of struct pool
873937e75f9a dm thin: don't allow changing data device during thin-pool reload
a4a8d286586d dm thin: fix use-after-free in metadata_pre_commit_callback
694cfe7f31db dm thin: Flush data device before committing metadata
70de2cbda8a5 dm thin: add sanity checks to thin-pool and external snapshot creation
48920ff2a5a9 block: remove the discard_zeroes_data flag
ba30670f4d52 dm thin: fix missing pool reference count decrement in pool_ctr error path
7d48935eff40 dm thin: allow metadata space larger than supported to go unused
b60ab990ccdf dm thin: do not expose non-zero discard limits if discards disabled
ac8c3f3df65e dm thin: generate event when metadata threshold passed
5d0db96d13a4 dm thin: open dev read only when possible
b17446df2ebc dm thin: refactor data dev resize
55a62eef8d1b dm: rename request variables to bios
28eed34e7662 dm thin: support discard with non power of two block size
0424caa14508 dm thin: fix discard support for data devices
9bc142dd755d dm thin: tidy discard support
307615a26e95 dm thin: do not set discard_zeroes_data
e49e582965b3 dm thin: add read only and fail io modes
0ac55489d9e3 dm: use bool bitfields in struct dm_target
55f2b8bdb0c7 dm thin: support for non power of 2 pool blocksize
f402693d06f3 dm thin: fix table output when pool target disables discard passdown internally
67e2e2b28181 dm thin: add pool target flags to control discard
104655fd4dce dm thin: support discards
c4a69ecdb463 dm thin: relax hard limit on the maximum size of a metadata device
991d9fa02da0 dm: add thin provisioning target
]
|
| 37/2 |
2026/11/23 11:04 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 53546
output: 126
thoughts: 1378
Thoughts: **My Thought Process on the Device Mapper Commits**
Okay, I need to really dig into these commits to understand the evolution of device handling in the Device Mapper, specifically focusing on potential issues like Use-After-Free (UAF) and how device modes are managed.
First, I looked at commit `991d9fa02da0` from 2011, which introduced thin provisioning. My initial thought was to scrutinize `pool_ctr` to make sure it wasn't allowing the metadata and data devices to be the same. Checking the code, I saw that both `metadata_dev` and `data_dev` were opened with `FMODE_READ | FMODE_WRITE`. The logic at the time meant that if both devices had the *same* mode, `dm_get_device()` wouldn't call `upgrade_mode()`. However, even if they *were* the same device, in this specific commit (`991d9fa02da0`), it seemed there wouldn't be a UAF or NULL pointer dereference in `dm_bufio_client_create`. So, while it might be an invalid configuration for a thin pool, it wasn't a crash-inducing bug at this point.
Next, I moved to commit `5d0db96d13a4` from May 2013, titled "dm thin: open dev read only when possible". This commit changed how the metadata device was opened, making it read-only if the `read_only` flag was passed. The data device, however, was still opened with `FMODE_READ | FMODE_WRITE`. Now, the critical question is: what happens if `argv[0]` (metadata) and `argv[1]` (data) point to the *same* underlying block device, and `read_only` is specified? I needed to trace `dm_get_device()` in this scenario, specifically in the state *before* the later fix in commit `86f1152b117a` (August 2014).
Let's walk through that scenario:
1. The first `dm_get_device()` call for the metadata device uses `FMODE_READ`. It allocates `dd` (our `struct dm_dev_internal`), opens the device, sets `dd->dm_dev.bdev`, and returns a pointer to `&dd->dm_dev`. The device count is incremented.
2. The second `dm_get_device()` call for the data device uses `FMODE_READ | FMODE_WRITE`. Because `argv[0]` and `argv[1]` resolve to the same device, `find_device()` successfully finds the existing `dd`. The requested mode (`FMODE_READ | FMODE_WRITE`) is wider than the current mode (`FMODE_READ`). This triggers `upgrade_mode()`.
3. Inside `upgrade_mode()`, a copy (`dd_old`) of the current `dd` is made. The `dd->dm_dev.mode` is updated in-place to `FMODE_READ | FMODE_WRITE`. Then, `open_dev()` is called with the *new* mode, which opens the block device with read-write access and increments its holder count. Crucially, `close_dev()` is called on `dd_old`.
4. Here's the key observation: `dd_old` was a *copy* of the `dd` structure. The actual `dd->dm_dev` structure itself was modified in-place. The `dd->dm_dev.bdev` pointer within the *original* `dd` structure was *not* set to NULL. Furthermore, `open_dev()` in step 3 had just incremented the block device's holder count. So, even though `close_dev()` on `dd_old` might have done some cleanup, the `dd` structure itself remained valid, and both `metadata_dev` and `data_dev` pointers were still pointing to the *same valid `&dd->dm_dev`*. Therefore, there was no UAF on `metadata_dev` itself. While this setup would be an invalid thin-pool configuration and might lead to other issues later (like `pool_create`), it didn't manifest as a UAF on the `metadata_dev` pointer returned by the first `dm_get_device()` call.
Finally, I examined commit `86f1152b117a` from August 2014, "dm: allow active and inactive tables to share dm_devs". This is where things fundamentally changed. I needed to understand how this commit altered `dm_get_device()` and `upgrade_mode()`. Before this commit, `struct dm_dev dm_dev` was an inline member of `struct dm_dev_internal`. This meant `&dd->dm_dev` was always valid as long as `dd` itself was valid. Any pointer returned by `dm_get_device()` would remain valid because `upgrade_mode()` modified the `dm_dev` in-place within the same `dd` structure. Now, I need to look at the actual code changes in `drivers/md/dm-table.c` around lines 300-460 and in `include/linux/device-mapper.h` to see precisely *how* `86f1152b117a` changed this ownership model and potentially introduced the vulnerability I'm looking for. I suspect this commit is where the problematic sharing of `dm_dev` structures across different contexts (active/inactive tables) might have been introduced, leading to issues where a `dm_dev` could be freed or invalidated while still being referenced by another part of the system.
|
| 38/2 |
2026/11/23 11:05 |
tool |
read-file |
0m
Args: map[File:drivers/md/dm-table.c FirstLine:300 LineCount:100]
Results: map[Contents: 300: return 1;
301: }
302:
303: return 0;
304: }
305:
306: /*
307: * This upgrades the mode on an already open dm_dev, being
308: * careful to leave things as they were if we fail to reopen the
309: * device and not to touch the existing bdev field in case
310: * it is accessed concurrently.
311: */
312: static int upgrade_mode(struct dm_dev_internal *dd, blk_mode_t new_mode,
313: struct mapped_device *md)
314: {
315: int r;
316: struct dm_dev *old_dev, *new_dev;
317:
318: old_dev = dd->dm_dev;
319:
320: r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
321: dd->dm_dev->mode | new_mode, &new_dev);
322: if (r)
323: return r;
324:
325: dd->dm_dev = new_dev;
326: dm_put_table_device(md, old_dev);
327:
328: return 0;
329: }
330:
331: /*
332: * Note: the __ref annotation is because this function can call the __init
333: * marked early_lookup_bdev when called during early boot code from dm-init.c.
334: */
335: int __ref dm_devt_from_path(const char *path, dev_t *dev_p)
336: {
337: int r;
338: dev_t dev;
339: unsigned int major, minor;
340: char dummy;
341:
342: if (sscanf(path, "%u:%u%c", &major, &minor, &dummy) == 2) {
343: /* Extract the major/minor numbers */
344: dev = MKDEV(major, minor);
345: if (MAJOR(dev) != major || MINOR(dev) != minor)
346: return -EOVERFLOW;
347: } else {
348: r = lookup_bdev(path, &dev);
349: #ifndef MODULE
350: if (r && system_state < SYSTEM_RUNNING)
351: r = early_lookup_bdev(path, &dev);
352: #endif
353: if (r)
354: return r;
355: }
356: *dev_p = dev;
357: return 0;
358: }
359: EXPORT_SYMBOL(dm_devt_from_path);
360:
361: /*
362: * Add a device to the list, or just increment the usage count if
363: * it's already present.
364: */
365: int dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode,
366: struct dm_dev **result)
367: {
368: int r;
369: dev_t dev;
370: struct dm_dev_internal *dd;
371: struct dm_table *t = ti->table;
372:
373: BUG_ON(!t);
374:
375: r = dm_devt_from_path(path, &dev);
376: if (r)
377: return r;
378:
379: if (dev == disk_devt(t->md->disk))
380: return -EINVAL;
381:
382: dd = find_device(&t->devices, dev);
383: if (!dd) {
384: dd = kmalloc_obj(*dd);
385: if (!dd)
386: return -ENOMEM;
387:
388: r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev);
389: if (r) {
390: kfree(dd);
391: return r;
392: }
393:
394: refcount_set(&dd->count, 1);
395: list_add(&dd->list, &t->devices);
396: goto out;
397:
398: } else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
399: r = upgrade_mode(dd, mode, t->md);
]
|
| 39/2 |
2026/11/23 11:05 |
tool |
read-file |
0m
Args: map[File:drivers/md/dm-table.c FirstLine:400 LineCount:60]
Results: map[Contents: 400: if (r)
401: return r;
402: }
403: refcount_inc(&dd->count);
404: out:
405: *result = dd->dm_dev;
406: return 0;
407: }
408: EXPORT_SYMBOL(dm_get_device);
409:
410: static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
411: sector_t start, sector_t len, void *data)
412: {
413: struct queue_limits *limits = data;
414: struct block_device *bdev = dev->bdev;
415: struct request_queue *q = bdev_get_queue(bdev);
416:
417: if (unlikely(!q)) {
418: DMWARN("%s: Cannot set limits for nonexistent device %pg",
419: dm_device_name(ti->table->md), bdev);
420: return 0;
421: }
422:
423: mutex_lock(&q->limits_lock);
424: /*
425: * BLK_FEAT_ATOMIC_WRITES is not inherited from the bottom device in
426: * blk_stack_limits(), so do it manually.
427: */
428: limits->features |= (q->limits.features & BLK_FEAT_ATOMIC_WRITES);
429:
430: if (blk_stack_limits(limits, &q->limits,
431: get_start_sect(bdev) + start) < 0)
432: DMWARN("%s: adding target device %pg caused an alignment inconsistency: "
433: "physical_block_size=%u, logical_block_size=%u, "
434: "alignment_offset=%u, start=%llu",
435: dm_device_name(ti->table->md), bdev,
436: q->limits.physical_block_size,
437: q->limits.logical_block_size,
438: q->limits.alignment_offset,
439: (unsigned long long) start << SECTOR_SHIFT);
440:
441: /*
442: * Only stack the integrity profile if the target doesn't have native
443: * integrity support.
444: */
445: if (!dm_target_has_integrity(ti->type))
446: queue_limits_stack_integrity_bdev(limits, bdev);
447: mutex_unlock(&q->limits_lock);
448: return 0;
449: }
450:
451: /*
452: * Decrement a device's use count and remove it if necessary.
453: */
454: void dm_put_device(struct dm_target *ti, struct dm_dev *d)
455: {
456: int found = 0;
457: struct list_head *devices = &ti->table->devices;
458: struct dm_dev_internal *dd;
459:
]
|
| 40/2 |
2026/11/23 11:05 |
tool |
grepper |
0m
Args: map[Expression:dm_get_device PathPrefixes:[drivers/md/ include/linux/device-mapper.h]]
Results: map[Output:
Full output is too long, showing 200 out of 273 lines.
Use more precise expression or PathPrefixes if possible.
Number of matching lines per file (31 files in total):
drivers/md/dm-cache-target.c:3
drivers/md/dm-clone-target.c:3
drivers/md/dm-crypt.c:1
drivers/md/dm-delay.c:1
drivers/md/dm-dust.c:1
drivers/md/dm-ebs-target.c:1
drivers/md/dm-era-target.c:2
drivers/md/dm-flakey.c:1
drivers/md/dm-inlinecrypt.c:1
drivers/md/dm-integrity.c:2
drivers/md/dm-linear.c:1
drivers/md/dm-log-userspace-base.c:1
drivers/md/dm-log-writes.c:2
drivers/md/dm-log.c:1
drivers/md/dm-mpath.c:1
drivers/md/dm-pcache/dm_pcache.c:2
drivers/md/dm-raid.c:3
drivers/md/dm-raid1.c:1
drivers/md/dm-snap.c:3
drivers/md/dm-stripe.c:1
drivers/md/dm-switch.c:1
drivers/md/dm-table.c:2
drivers/md/dm-target.c:1
drivers/md/dm-thin.c:4
drivers/md/dm-unstripe.c:1
drivers/md/dm-vdo/dm-vdo-target.c:1
drivers/md/dm-verity-fec.c:1
drivers/md/dm-verity-target.c:2
drivers/md/dm-writecache.c:2
drivers/md/dm-zoned-target.c:1
include/linux/device-mapper.h:1
drivers/md/dm-cache-target.c=2094=static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
--
drivers/md/dm-cache-target.c-2102-
drivers/md/dm-cache-target.c:2103: r = dm_get_device(ca->ti, dm_shift_arg(as),
drivers/md/dm-cache-target.c-2104- BLK_OPEN_READ | BLK_OPEN_WRITE, &ca->metadata_dev);
--
drivers/md/dm-cache-target.c=2118=static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
--
drivers/md/dm-cache-target.c-2125-
drivers/md/dm-cache-target.c:2126: r = dm_get_device(ca->ti, dm_shift_arg(as),
drivers/md/dm-cache-target.c-2127- BLK_OPEN_READ | BLK_OPEN_WRITE, &ca->cache_dev);
--
drivers/md/dm-cache-target.c=2137=static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
--
drivers/md/dm-cache-target.c-2144-
drivers/md/dm-cache-target.c:2145: r = dm_get_device(ca->ti, dm_shift_arg(as),
drivers/md/dm-cache-target.c-2146- BLK_OPEN_READ | BLK_OPEN_WRITE, &ca->origin_dev);
--
drivers/md/dm-clone-target.c=1677=static int parse_metadata_dev(struct clone *clone, struct dm_arg_set *as, char **error)
--
drivers/md/dm-clone-target.c-1681-
drivers/md/dm-clone-target.c:1682: r = dm_get_device(clone->ti, dm_shift_arg(as),
drivers/md/dm-clone-target.c-1683- BLK_OPEN_READ | BLK_OPEN_WRITE, &clone->metadata_dev);
--
drivers/md/dm-clone-target.c=1697=static int parse_dest_dev(struct clone *clone, struct dm_arg_set *as, char **error)
--
drivers/md/dm-clone-target.c-1700-
drivers/md/dm-clone-target.c:1701: r = dm_get_device(clone->ti, dm_shift_arg(as),
drivers/md/dm-clone-target.c-1702- BLK_OPEN_READ | BLK_OPEN_WRITE, &clone->dest_dev);
--
drivers/md/dm-clone-target.c=1711=static int parse_source_dev(struct clone *clone, struct dm_arg_set *as, char **error)
--
drivers/md/dm-clone-target.c-1714-
drivers/md/dm-clone-target.c:1715: r = dm_get_device(clone->ti, dm_shift_arg(as), BLK_OPEN_READ,
drivers/md/dm-clone-target.c-1716- &clone->source_dev);
--
drivers/md/dm-crypt.c=3204=static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
--
drivers/md/dm-crypt.c-3320-
drivers/md/dm-crypt.c:3321: ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
drivers/md/dm-crypt.c-3322- if (ret) {
--
drivers/md/dm-delay.c=182=static int delay_class_ctr(struct dm_target *ti, struct delay_class *c, char **argv)
--
drivers/md/dm-delay.c-198-
drivers/md/dm-delay.c:199: ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &c->dev);
drivers/md/dm-delay.c-200- if (ret) {
--
drivers/md/dm-dust.c=321=static int dust_ctr(struct dm_target *ti, unsigned int argc, char **argv)
--
drivers/md/dm-dust.c-368-
drivers/md/dm-dust.c:369: if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &dd->dev)) {
drivers/md/dm-dust.c-370- ti->error = "Device lookup failed";
--
drivers/md/dm-ebs-target.c=247=static int ebs_ctr(struct dm_target *ti, unsigned int argc, char **argv)
--
drivers/md/dm-ebs-target.c-291-
drivers/md/dm-ebs-target.c:292: r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ec->dev);
drivers/md/dm-ebs-target.c-293- if (r) {
--
drivers/md/dm-era-target.c=1472=static int era_ctr(struct dm_target *ti, unsigned int argc, char **argv)
--
drivers/md/dm-era-target.c-1491-
drivers/md/dm-era-target.c:1492: r = dm_get_device(ti, argv[0], BLK_OPEN_READ | BLK_OPEN_WRITE,
drivers/md/dm-era-target.c-1493- &era->metadata_dev);
--
drivers/md/dm-era-target.c-1499-
drivers/md/dm-era-target.c:1500: r = dm_get_device(ti, argv[1], BLK_OPEN_READ | BLK_OPEN_WRITE,
drivers/md/dm-era-target.c-1501- &era->origin_dev);
--
drivers/md/dm-flakey.c=258=static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
--
drivers/md/dm-flakey.c-319-
drivers/md/dm-flakey.c:320: r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
drivers/md/dm-flakey.c-321- if (r) {
--
drivers/md/dm-inlinecrypt.c=310=static int inlinecrypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
--
drivers/md/dm-inlinecrypt.c-367- /* <dev_path> */
drivers/md/dm-inlinecrypt.c:368: err = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table),
drivers/md/dm-inlinecrypt.c-369- &ctx->dev);
--
drivers/md/dm-integrity.c=4809=static int dm_integrity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
--
drivers/md/dm-integrity.c-4852-
drivers/md/dm-integrity.c:4853: r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ic->dev);
drivers/md/dm-integrity.c-4854- if (r) {
--
drivers/md/dm-integrity.c-4922- }
drivers/md/dm-integrity.c:4923: r = dm_get_device(ti, strchr(opt_string, ':') + 1,
drivers/md/dm-integrity.c-4924- dm_table_get_mode(ti->table), &ic->meta_dev);
--
drivers/md/dm-linear.c=30=static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv)
--
drivers/md/dm-linear.c-54-
drivers/md/dm-linear.c:55: ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &lc->dev);
drivers/md/dm-linear.c-56- if (ret) {
--
drivers/md/dm-log-userspace-base.c=192=static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
--
drivers/md/dm-log-userspace-base.c-294- }
drivers/md/dm-log-userspace-base.c:295: r = dm_get_device(ti, devices_rdata,
drivers/md/dm-log-userspace-base.c-296- dm_table_get_mode(ti->table), &lc->log_dev);
--
drivers/md/dm-log-writes.c=507=static int log_writes_ctr(struct dm_target *ti, unsigned int argc, char **argv)
--
drivers/md/dm-log-writes.c-535- devname = dm_shift_arg(&as);
drivers/md/dm-log-writes.c:536: ret = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &lc->dev);
drivers/md/dm-log-writes.c-537- if (ret) {
--
drivers/md/dm-log-writes.c-542- logdevname = dm_shift_arg(&as);
drivers/md/dm-log-writes.c:543: ret = dm_get_device(ti, logdevname, dm_table_get_mode(ti->table),
drivers/md/dm-log-writes.c-544- &lc->logdev);
--
drivers/md/dm-log.c=553=static int disk_ctr(struct dm_dirty_log *log, struct dm_target *ti,
--
drivers/md/dm-log.c-563-
drivers/md/dm-log.c:564: r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &dev);
drivers/md/dm-log.c-565- if (r)
--
drivers/md/dm-mpath.c=924=static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
--
drivers/md/dm-mpath.c-942-
drivers/md/dm-mpath.c:943: r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
drivers/md/dm-mpath.c-944- &p->path.dev);
--
drivers/md/dm-pcache/dm_pcache.c=104=static int parse_cache_dev(struct dm_pcache *pcache, struct dm_arg_set *as,
--
drivers/md/dm-pcache/dm_pcache.c-110- return -EINVAL;
drivers/md/dm-pcache/dm_pcache.c:111: ret = dm_get_device(pcache->ti, dm_shift_arg(as),
drivers/md/dm-pcache/dm_pcache.c-112- BLK_OPEN_READ | BLK_OPEN_WRITE,
--
drivers/md/dm-pcache/dm_pcache.c=122=static int parse_backing_dev(struct dm_pcache *pcache, struct dm_arg_set *as,
--
drivers/md/dm-pcache/dm_pcache.c-129-
drivers/md/dm-pcache/dm_pcache.c:130: ret = dm_get_device(pcache->ti, dm_shift_arg(as),
drivers/md/dm-pcache/dm_pcache.c-131- BLK_OPEN_READ | BLK_OPEN_WRITE,
--
drivers/md/dm-raid.c=826=static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
--
drivers/md/dm-raid.c-857- if (strcmp(arg, "-")) {
drivers/md/dm-raid.c:858: r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
drivers/md/dm-raid.c-859- &rs->dev[i].meta_dev);
--
drivers/md/dm-raid.c-890-
drivers/md/dm-raid.c:891: r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
drivers/md/dm-raid.c-892- &rs->dev[i].data_dev);
--
drivers/md/dm-raid.c=1130=static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
--
drivers/md/dm-raid.c-1262- }
drivers/md/dm-raid.c:1263: r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
drivers/md/dm-raid.c-1264- &rs->journal_dev.dev);
--
drivers/md/dm-raid1.c=955=static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
--
drivers/md/dm-raid1.c-967-
drivers/md/dm-raid1.c:968: ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
drivers/md/dm-raid1.c-969- &ms->mirror[mirror].dev);
--
drivers/md/dm-snap.c=1230=static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
--
drivers/md/dm-snap.c-1268-
drivers/md/dm-snap.c:1269: r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
drivers/md/dm-snap.c-1270- if (r) {
--
drivers/md/dm-snap.c-1278-
drivers/md/dm-snap.c:1279: r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
drivers/md/dm-snap.c-1280- if (r) {
--
drivers/md/dm-snap.c=2629=static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
--
drivers/md/dm-snap.c-2645-
drivers/md/dm-snap.c:2646: r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &o->dev);
drivers/md/dm-snap.c-2647- if (r) {
--
drivers/md/dm-stripe.c=64=static int get_stripe(struct dm_target *ti, struct stripe_c *sc,
--
drivers/md/dm-stripe.c-73-
drivers/md/dm-stripe.c:74: ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
drivers/md/dm-stripe.c-75- &sc->stripe[stripe].dev);
--
drivers/md/dm-switch.c=205=static int parse_path(struct dm_arg_set *as, struct dm_target *ti)
--
drivers/md/dm-switch.c-210-
drivers/md/dm-switch.c:211: r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
drivers/md/dm-switch.c-212- &sctx->path_list[sctx->nr_paths].dmdev);
--
drivers/md/dm-table.c=359=EXPORT_SYMBOL(dm_devt_from_path);
--
drivers/md/dm-table.c-364- */
drivers/md/dm-table.c:365:int dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode,
drivers/md/dm-table.c-366- struct dm_dev **result)
--
drivers/md/dm-table.c-407-}
drivers/md/dm-table.c:408:EXPORT_SYMBOL(dm_get_device);
drivers/md/dm-table.c-409-
--
drivers/md/dm-target.c=124=static int io_err_get_args(struct dm_target *tt, unsigned int argc, char **args)
--
]
|
| 41/2 |
2026/11/23 11:05 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 62312
output: 131
thoughts: 244
Thoughts: **Deconstructing Device Mapper Target Behavior Across All Devices**
My primary focus right now is to exhaustively examine how device mapper targets handle device opening and mode upgrading across the entire spectrum of devices, not just the specific `dm-thin.c` case I've been looking at. I need to cast a wider net.
Specifically, I'm going to scrutinize the behavior of `dm_get_device()` and `upgrade_mode()` in conjunction with various target implementations. My initial thought was to look at `dm-thin.c`, but that's just a starting point. I need to ensure my understanding is comprehensive.
I've just been reviewing `dm-clone-target.c`, and it presents an interesting scenario. In that file, `dest_dev` is opened with both read and write permissions (`BLK_OPEN_READ | BLK_OPEN_WRITE`), while `source_dev` is opened as read-only (`BLK_OPEN_READ`). This particular configuration, where the destination is opened RW and the source RO, means that `upgrade_mode()` wouldn't even be called. The fact that `dest_dev` is opened first with write capabilities, followed by `source_dev` with read, preempts the need for an upgrade in this specific instance.
However, this observation prompts me to consider other critical targets. I absolutely *must* investigate `dm-verity-target.c`, `dm-writecache.c`, and then circle back to `dm-thin.c` with a sharper focus. For `dm-thin.c`, I need to be sure I'm examining both the `thin_ctr` and the `pool_ctr` functions. Similarly, for `dm-snap.c`, the `snapshot_ctr` warrants a detailed look. This systematic approach is crucial to identifying any potential edge cases or unexpected interactions in how these targets manage their underlying block devices and their operational modes.
|
| 42/2 |
2026/11/23 11:05 |
tool |
read-file |
0m
Args: map[File:drivers/md/dm-thin.c FirstLine:4240 LineCount:100]
Results: map[Contents:4240:
4241: tc->pool = __pool_table_lookup(pool_md);
4242: if (!tc->pool) {
4243: ti->error = "Couldn't find pool object";
4244: r = -EINVAL;
4245: goto bad_pool_lookup;
4246: }
4247: __pool_inc(tc->pool);
4248:
4249: if (get_pool_mode(tc->pool) == PM_FAIL) {
4250: ti->error = "Couldn't open thin device, Pool is in fail mode";
4251: r = -EINVAL;
4252: goto bad_pool;
4253: }
4254:
4255: r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
4256: if (r) {
4257: ti->error = "Couldn't open thin internal device";
4258: goto bad_pool;
4259: }
4260:
4261: r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
4262: if (r)
4263: goto bad;
4264:
4265: ti->num_flush_bios = 1;
4266: ti->limit_swap_bios = true;
4267: ti->flush_supported = true;
4268: ti->accounts_remapped_io = true;
4269: ti->per_io_data_size = sizeof(struct dm_thin_endio_hook);
4270:
4271: /* In case the pool supports discards, pass them on. */
4272: if (tc->pool->pf.discard_enabled) {
4273: ti->discards_supported = true;
4274: ti->num_discard_bios = 1;
4275: ti->max_discard_granularity = true;
4276: }
4277:
4278: mutex_unlock(&dm_thin_pool_table.mutex);
4279:
4280: spin_lock_irq(&tc->pool->lock);
4281: if (tc->pool->suspended) {
4282: spin_unlock_irq(&tc->pool->lock);
4283: mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
4284: ti->error = "Unable to activate thin device while pool is suspended";
4285: r = -EINVAL;
4286: goto bad;
4287: }
4288: refcount_set(&tc->refcount, 1);
4289: init_completion(&tc->can_destroy);
4290: list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
4291: spin_unlock_irq(&tc->pool->lock);
4292: /*
4293: * This synchronize_rcu() call is needed here otherwise we risk a
4294: * wake_worker() call finding no bios to process (because the newly
4295: * added tc isn't yet visible). So this reduces latency since we
4296: * aren't then dependent on the periodic commit to wake_worker().
4297: */
4298: synchronize_rcu();
4299:
4300: dm_put(pool_md);
4301:
4302: return 0;
4303:
4304: bad:
4305: dm_pool_close_thin_device(tc->td);
4306: bad_pool:
4307: __pool_dec(tc->pool);
4308: bad_pool_lookup:
4309: dm_put(pool_md);
4310: bad_common:
4311: dm_put_device(ti, tc->pool_dev);
4312: bad_pool_dev:
4313: if (tc->origin_dev)
4314: dm_put_device(ti, tc->origin_dev);
4315: bad_origin_dev:
4316: kfree(tc);
4317: out_unlock:
4318: mutex_unlock(&dm_thin_pool_table.mutex);
4319:
4320: return r;
4321: }
4322:
4323: static int thin_map(struct dm_target *ti, struct bio *bio)
4324: {
4325: bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
4326:
4327: return thin_bio_map(ti, bio);
4328: }
4329:
4330: static int thin_endio(struct dm_target *ti, struct bio *bio,
4331: blk_status_t *err)
4332: {
4333: unsigned long flags;
4334: struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
4335: struct list_head work;
4336: struct dm_thin_new_mapping *m, *tmp;
4337: struct pool *pool = h->tc->pool;
4338:
4339: if (h->shared_read_entry) {
]
|
| 43/2 |
2026/11/23 11:05 |
tool |
read-file |
0m
Args: map[File:drivers/md/dm-verity-target.c FirstLine:1400 LineCount:100]
Results: map[Contents:1400: * <hash device>
1401: * <data block size>
1402: * <hash block size>
1403: * <the number of data blocks>
1404: * <hash start block>
1405: * <algorithm>
1406: * <digest>
1407: * <salt> Hex string or "-" if no salt.
1408: */
1409: static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1410: {
1411: struct dm_verity *v;
1412: struct dm_verity_sig_opts verify_args = {0};
1413: struct dm_arg_set as;
1414: unsigned int num;
1415: unsigned long long num_ll;
1416: int r;
1417: int i;
1418: sector_t hash_position;
1419: char dummy;
1420: char *root_hash_digest_to_validate;
1421:
1422: v = kzalloc_obj(struct dm_verity);
1423: if (!v) {
1424: ti->error = "Cannot allocate verity structure";
1425: return -ENOMEM;
1426: }
1427: ti->private = v;
1428: v->ti = ti;
1429:
1430: r = verity_fec_ctr_alloc(v);
1431: if (r)
1432: goto bad;
1433:
1434: if ((dm_table_get_mode(ti->table) & ~BLK_OPEN_READ)) {
1435: ti->error = "Device must be readonly";
1436: r = -EINVAL;
1437: goto bad;
1438: }
1439:
1440: if (argc < 10) {
1441: ti->error = "Not enough arguments";
1442: r = -EINVAL;
1443: goto bad;
1444: }
1445:
1446: /* Parse optional parameters that modify primary args */
1447: if (argc > 10) {
1448: as.argc = argc - 10;
1449: as.argv = argv + 10;
1450: r = verity_parse_opt_args(&as, v, &verify_args, true);
1451: if (r < 0)
1452: goto bad;
1453: }
1454:
1455: if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
1456: num > 1) {
1457: ti->error = "Invalid version";
1458: r = -EINVAL;
1459: goto bad;
1460: }
1461: v->version = num;
1462:
1463: r = dm_get_device(ti, argv[1], BLK_OPEN_READ, &v->data_dev);
1464: if (r) {
1465: ti->error = "Data device lookup failed";
1466: goto bad;
1467: }
1468:
1469: r = dm_get_device(ti, argv[2], BLK_OPEN_READ, &v->hash_dev);
1470: if (r) {
1471: ti->error = "Hash device lookup failed";
1472: goto bad;
1473: }
1474:
1475: if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
1476: !num || (num & (num - 1)) ||
1477: num < bdev_logical_block_size(v->data_dev->bdev) ||
1478: num > PAGE_SIZE) {
1479: ti->error = "Invalid data device block size";
1480: r = -EINVAL;
1481: goto bad;
1482: }
1483: v->data_dev_block_bits = __ffs(num);
1484:
1485: if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
1486: !num || (num & (num - 1)) ||
1487: num < bdev_logical_block_size(v->hash_dev->bdev) ||
1488: num > INT_MAX) {
1489: ti->error = "Invalid hash device block size";
1490: r = -EINVAL;
1491: goto bad;
1492: }
1493: v->hash_dev_block_bits = __ffs(num);
1494:
1495: if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
1496: (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
1497: >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
1498: ti->error = "Invalid data blocks";
1499: r = -EINVAL;
]
|
| 44/2 |
2026/11/23 11:05 |
tool |
read-file |
0m
Args: map[File:drivers/md/dm-writecache.c FirstLine:2400 LineCount:100]
Results: map[Contents:2400: ti->error = "Block size is smaller than device logical block size";
2401: goto bad;
2402: }
2403: wc->block_size_bits = __ffs(wc->block_size);
2404:
2405: wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
2406: wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
2407: wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
2408:
2409: /*
2410: * Parse optional arguments
2411: */
2412: r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2413: if (r)
2414: goto bad;
2415:
2416: while (opt_params) {
2417: string = dm_shift_arg(&as), opt_params--;
2418: if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2419: unsigned long long start_sector;
2420:
2421: string = dm_shift_arg(&as), opt_params--;
2422: if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2423: goto invalid_optional;
2424: wc->start_sector = start_sector;
2425: wc->start_sector_set = true;
2426: if (wc->start_sector != start_sector ||
2427: wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2428: goto invalid_optional;
2429: } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
2430: string = dm_shift_arg(&as), opt_params--;
2431: if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
2432: goto invalid_optional;
2433: if (high_wm_percent < 0 || high_wm_percent > 100)
2434: goto invalid_optional;
2435: wc->high_wm_percent_value = high_wm_percent;
2436: wc->high_wm_percent_set = true;
2437: } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
2438: string = dm_shift_arg(&as), opt_params--;
2439: if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2440: goto invalid_optional;
2441: if (low_wm_percent < 0 || low_wm_percent > 100)
2442: goto invalid_optional;
2443: wc->low_wm_percent_value = low_wm_percent;
2444: wc->low_wm_percent_set = true;
2445: } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2446: string = dm_shift_arg(&as), opt_params--;
2447: if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2448: goto invalid_optional;
2449: wc->max_writeback_jobs_set = true;
2450: } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2451: string = dm_shift_arg(&as), opt_params--;
2452: if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2453: goto invalid_optional;
2454: wc->autocommit_blocks_set = true;
2455: } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2456: unsigned int autocommit_msecs;
2457:
2458: string = dm_shift_arg(&as), opt_params--;
2459: if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2460: goto invalid_optional;
2461: if (autocommit_msecs > 3600000)
2462: goto invalid_optional;
2463: wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2464: wc->autocommit_time_value = autocommit_msecs;
2465: wc->autocommit_time_set = true;
2466: } else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
2467: unsigned int max_age_msecs;
2468: unsigned long max_age_jiffies;
2469:
2470: string = dm_shift_arg(&as), opt_params--;
2471: if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
2472: goto invalid_optional;
2473: max_age_jiffies = msecs_to_jiffies(max_age_msecs);
2474: if (max_age_jiffies >= min(MAX_JIFFY_OFFSET, (7 << 28))) // 7/8ths of 1 << 31
2475: goto invalid_optional;
2476: wc->max_age = max_age_jiffies;
2477: wc->max_age_set = true;
2478: wc->max_age_value = max_age_msecs;
2479: } else if (!strcasecmp(string, "cleaner")) {
2480: wc->cleaner_set = true;
2481: wc->cleaner = true;
2482: } else if (!strcasecmp(string, "fua")) {
2483: if (WC_MODE_PMEM(wc)) {
2484: wc->writeback_fua = true;
2485: wc->writeback_fua_set = true;
2486: } else
2487: goto invalid_optional;
2488: } else if (!strcasecmp(string, "nofua")) {
2489: if (WC_MODE_PMEM(wc)) {
2490: wc->writeback_fua = false;
2491: wc->writeback_fua_set = true;
2492: } else
2493: goto invalid_optional;
2494: } else if (!strcasecmp(string, "metadata_only")) {
2495: wc->metadata_only = true;
2496: } else if (!strcasecmp(string, "pause_writeback") && opt_params >= 1) {
2497: unsigned int pause_msecs;
2498:
2499: if (WC_MODE_PMEM(wc))
]
|
| 45/2 |
2026/11/23 11:05 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 67739
output: 44
thoughts: 35
Thoughts: **Examining the `dm-thin.c` Driver for Thin Provisioning Logic**
My current focus is on understanding the intricate workings of thin provisioning within the device mapper framework, specifically in the `drivers/md/dm-thin.c` file. I'm diving into the code between lines 4170 and 4240 to meticulously analyze the logic governing how thin volumes are managed and how these operations are translated into underlying block device requests. My aim is to gain a comprehensive grasp of the internal mechanisms, from metadata updates to data allocation and I/O handling, to ensure a robust and efficient implementation of thin provisioning.
|
| 46/2 |
2026/11/23 11:05 |
tool |
read-file |
0m
Args: map[File:drivers/md/dm-thin.c FirstLine:4170 LineCount:75]
Results: map[Contents:4170: *
4171: * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
4172: * dev_id: the internal device identifier
4173: * origin_dev: a device external to the pool that should act as the origin
4174: *
4175: * If the pool device has discards disabled, they get disabled for the thin
4176: * device as well.
4177: */
4178: static int thin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
4179: {
4180: int r;
4181: struct thin_c *tc;
4182: struct dm_dev *pool_dev, *origin_dev;
4183: struct mapped_device *pool_md;
4184:
4185: mutex_lock(&dm_thin_pool_table.mutex);
4186:
4187: if (argc != 2 && argc != 3) {
4188: ti->error = "Invalid argument count";
4189: r = -EINVAL;
4190: goto out_unlock;
4191: }
4192:
4193: tc = ti->private = kzalloc_obj(*tc);
4194: if (!tc) {
4195: ti->error = "Out of memory";
4196: r = -ENOMEM;
4197: goto out_unlock;
4198: }
4199: tc->thin_md = dm_table_get_md(ti->table);
4200: spin_lock_init(&tc->lock);
4201: INIT_LIST_HEAD(&tc->deferred_cells);
4202: bio_list_init(&tc->deferred_bio_list);
4203: bio_list_init(&tc->retry_on_resume_list);
4204: tc->sort_bio_list = RB_ROOT;
4205:
4206: if (argc == 3) {
4207: if (!strcmp(argv[0], argv[2])) {
4208: ti->error = "Error setting origin device";
4209: r = -EINVAL;
4210: goto bad_origin_dev;
4211: }
4212:
4213: r = dm_get_device(ti, argv[2], BLK_OPEN_READ, &origin_dev);
4214: if (r) {
4215: ti->error = "Error opening origin device";
4216: goto bad_origin_dev;
4217: }
4218: tc->origin_dev = origin_dev;
4219: }
4220:
4221: r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
4222: if (r) {
4223: ti->error = "Error opening pool device";
4224: goto bad_pool_dev;
4225: }
4226: tc->pool_dev = pool_dev;
4227:
4228: if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
4229: ti->error = "Invalid device id";
4230: r = -EINVAL;
4231: goto bad_common;
4232: }
4233:
4234: pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
4235: if (!pool_md) {
4236: ti->error = "Couldn't get pool mapped device";
4237: r = -EINVAL;
4238: goto bad_common;
4239: }
4240:
4241: tc->pool = __pool_table_lookup(pool_md);
4242: if (!tc->pool) {
4243: ti->error = "Couldn't find pool object";
4244: r = -EINVAL;
]
|
| 47/2 |
2026/11/23 11:05 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 68961
output: 119
thoughts: 4126
Thoughts: **My Process of Unraveling the `dm-thin` UAF Bug**
I've been meticulously investigating a particularly thorny use-after-free (UAF) bug within the `dm-thin` module, and I've finally pieced together the sequence of events and the precise commit that introduced this vulnerability. My approach involved stepping through the code execution flow, comparing different states of the codebase across key commits, and analyzing the precise semantics of each change.
First, I had to **clearly define the bug**. By carefully re-reading crash logs and the associated code, I pinpointed the failure point: in `pool_ctr()`, after obtaining a device for metadata (`metadata_dev`) and then another for data (`data_dev`), a specific condition led to `metadata_dev` pointing to freed memory.
The sequence of events I identified was this:
1. Within `pool_ctr()`, a check `if (!strcmp(argv[0], argv[1]))` was introduced in commit `70de2cbda8a5` (January 2019). This was an attempt to mitigate the bug, but as I discovered, it was incomplete.
2. `pool_ctr()` then calls `dm_get_device()` for the metadata device (`argv[0]`). If `read_only` is set, `metadata_mode` is `BLK_OPEN_READ` (as established by commit `5d0db96d13a4`, May 2013). This call successfully retrieves a device structure.
3. Subsequently, `pool_ctr()` calls `dm_get_device()` for the data device (`argv[1]`) with `BLK_OPEN_READ | BLK_OPEN_WRITE`.
4. Crucially, if `argv[0]` and `argv[1]` refer to the same underlying device (even if represented by different strings like `/dev/ram1` and `1:1`), `dm_get_device()` detects that the device is already present in the target's device list. It then calls `upgrade_mode()` to reconcile the differing access modes.
5. This `upgrade_mode()` function, under certain conditions, allocates a *new* `table_device` structure, updates the internal pointer (`dd->dm_dev`) to point to this new structure, and then calls `dm_put_table_device()` on the *old* device structure. The `dm_put_table_device()` call ultimately frees the `struct table_device` that `metadata_dev` was pointing to.
6. The problem arises because `metadata_dev` in `pool_ctr()` *still holds a pointer* to the now-freed memory.
7. When `pool_ctr()` attempts to access `metadata_dev->bdev` (which is now invalid memory, likely zeroed to NULL), it triggers a NULL pointer dereference. This occurs deep within the call chain: `__pool_find` -> `pool_create` -> `dm_pool_metadata_open` -> `dm_block_manager_create` -> `dm_bufio_client_create`, which tries to access `bdev->bd_dev` (at offset `0x24`) on a NULL pointer.
This brought me to a critical juncture: **how to precisely identify the commit that *introduced* this bug?** I considered several candidates:
* **Commit `70de2cbda8a5` (Jan 2019):** This commit added the `!strcmp(argv[0], argv[1])` check. I immediately ruled this out because it was clearly a *partial fix attempt*, not the introducer. Before this, even identical strings for both arguments would trigger the UAF. My instructions explicitly state to *exclude* incomplete fixes or commits that merely exposed the path.
* **Comparing `5d0db96d13a4` (May 2013) and `86f1152b117a` (Aug 2014):** This was the core of my investigation. I needed to trace the code's behavior *before* and *after* these significant commits, particularly focusing on how `dm_get_device()` and `upgrade_mode()` handled device structures.
* **Before `86f1152b117a` (specifically, around `5d0db96d13a4`):**
In this era, `dm_get_device()` returned a pointer to an embedded `struct dm_dev` within a `struct dm_dev_internal`. `upgrade_mode()` would modify this *in place*. The `struct dm_dev` itself was not freed, and its `bdev` pointer remained valid. Any subsequent `dm_get_device()` calls on the same underlying device would increment a reference count. `metadata_dev` and `data_dev` would correctly point to the same valid `struct dm_dev`, and accessing `metadata_dev->bdev` would work fine. My analysis suggested there was no UAF or NULL dereference in this period.
* **After `86f1152b117a`:**
This commit, titled "dm: allow active and inactive tables to share dm_devs," fundamentally changed the management of device structures. It transformed `dd->dm_dev` from an embedded `struct dm_dev` into a pointer (`struct dm_dev *dm_dev`) to a `struct table_device`. More critically, `upgrade_mode()` was refactored to:
1. Allocate a *new* `struct table_device` (`new_dev`).
2. Update `dd->dm_dev` to point to this `new_dev`.
3. Call `dm_put_table_device()` on the `old_dev`.
This change broke the fundamental invariant. `dm_put_table_device` (after `86f1152b117a`) now calls `kfree(td)` on the old `table_device`. Thus, any pointer (`metadata_dev` in my example) that previously held a reference to the *old* `table_device`'s internal `dm_dev` structure was now pointing to freed memory. Furthermore, `close_table_device`, called during `dm_put_table_device`, explicitly sets `td->dm_dev.bdev = NULL` *before* freeing the `table_device`, directly creating the NULL pointer dereference scenario I observed later.
This detailed comparison led me to conclude that **commit `86f1152b117a` is the introducing commit**. It refactored `dm_get_device` and `upgrade_mode` in a way that invalidated existing pointers when device modes were upgraded on the same underlying device, directly causing the UAF and subsequent NULL dereference. It also appears to have implicitly broken the correct handling of `dm_put_device` in error paths, as `dm_put_device` would fail to find the *now-replaced* `dd->dm_dev` and incorrectly print a "not in table devices list" message while leaking reference counts.
I also briefly considered other potential contributing factors:
* The fact that `dm_get_device()` can be called multiple times for the same `dev_t` within a single `dm_table` (not just in `pool_ctr`, but also `thin_ctr`, `snapshot_ctr`, and even across different targets). This *behavior* is a prerequisite for the bug to manifest, but it wasn't *introduced* by `86f1152b117a`.
* Targets that hardcode `BLK_OPEN_READ` (like `dm-snap` origin, `dm-thin` metadata in `read_only`) even when the table mode is `RW`. This interaction with a subsequent `BLK_OPEN_WRITE` call on the same device is what triggers `upgrade_mode`.
* I double-checked commits related to `dm-bufio` and error paths in `pool_dtr`, but found no other relevant changes that would explain the core UAF introduced by the device management refactoring.
Therefore, my definitive finding is that the structural changes in `86f1152b117a` are the root cause of this specific use-after-free and NULL pointer dereference bug.
|
| 48/2 |
2026/11/23 11:05 |
tool |
read-file |
0m
Args: map[File:drivers/md/dm-bufio.c FirstLine:2565 LineCount:40]
Results: map[Contents:2565: 0, SLAB_RECLAIM_ACCOUNT, NULL);
2566: if (!c->slab_buffer) {
2567: r = -ENOMEM;
2568: goto bad;
2569: }
2570:
2571: while (c->need_reserved_buffers) {
2572: struct dm_buffer *b = alloc_buffer(c, GFP_KERNEL);
2573:
2574: if (!b) {
2575: r = -ENOMEM;
2576: goto bad;
2577: }
2578: __free_buffer_wake(b);
2579: }
2580:
2581: INIT_WORK(&c->shrink_work, shrink_work);
2582: atomic_long_set(&c->need_shrink, 0);
2583:
2584: c->shrinker = shrinker_alloc(0, "dm-bufio:(%u:%u)",
2585: MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
2586: if (!c->shrinker) {
2587: r = -ENOMEM;
2588: goto bad;
2589: }
2590:
2591: c->shrinker->count_objects = dm_bufio_shrink_count;
2592: c->shrinker->scan_objects = dm_bufio_shrink_scan;
2593: c->shrinker->seeks = 1;
2594: c->shrinker->batch = 0;
2595: c->shrinker->private_data = c;
2596:
2597: shrinker_register(c->shrinker);
2598:
2599: mutex_lock(&dm_bufio_clients_lock);
2600: dm_bufio_client_count++;
2601: list_add(&c->client_list, &dm_bufio_all_clients);
2602: mutex_unlock(&dm_bufio_clients_lock);
2603:
2604: return c;
]
|
| 49/2 |
2026/11/23 11:05 |
tool |
git-log |
0m
Args: map[CodeRegexp:upgrade_mode Count:20 PathPrefix:drivers/md/]
Results: map[Output:05bdb9965305 block: replace fmode_t with a block-specific type for block open flags
aeb5d727062a [PATCH] introduce fmode_t, do annotations
82b1519b345d dm: export struct dm_dev
f165921df46a [PATCH] dm/md dependency tree in sysfs: dm to use bd_claim_by_disk
1da177e4c3f4 Linux-2.6.12-rc2
]
|
| 50/2 |
2026/11/23 11:05 |
tool |
git-log |
0m
Args: map[Count:20 MessageRegexps:[dm_get_device] PathPrefix:drivers/md/]
Results: map[Output:8d4dd2db7f4f dm era: fix error code propagation in era_ctr()
4550a71b179b Revert "dm: fix a race condition in retrieve_deps"
a48f6b82c5c4 dm mpath: don't call dm_get_device in multipath_message
a21f9edb13b0 dm: factor out helper function from dm_get_device
f6007dce0cd3 dm: fix a race condition in retrieve_deps
7a126d5bf975 dm: only call early_lookup_bdev from early boot context
26110d5afe81 dm-snap: simplify the origin_dev == cow_dev check in snapshot_ctr
d1c0e1587e68 dm table: check that a dm device doesn't reference itself
809b1e494577 dm: avoid filesystem lookup in dm_get_dev_t()
70de2cbda8a5 dm thin: add sanity checks to thin-pool and external snapshot creation
bff7e067ee51 dm flakey: return -EINVAL on interval bounds error in flakey_ctr()
e80d1c805a3b dm: do not override error code returned from dm_get_device()
644bda6f3460 dm table: fall back to getting device using name_to_dev_t()
5d0db96d13a4 dm thin: open dev read only when possible
5a25f0eb707b dm log userspace: add log device dependency
08649012545c dm table: clean dm_get_device and move exports
8215d6ec5fee dm table: remove unused dm_get_device range parameters
69a2ce72a4ef dm: table use uninitialized_var
3cb4021453a6 [PATCH] dm: extract device limit setting
]
|
| 51/2 |
2026/11/23 11:06 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 74433
output: 49
thoughts: 1287
Thoughts: **Deconstructing the `dm_dev` Evolution and its UAF Implications**
My thought process began with a deep dive into the history of the `dm_dev` structure within the device mapper subsystem, specifically focusing on the divergence between commits `86f1152b117a` and `5d0db96d13a4`. My primary goal was to pinpoint the exact commit that introduced a Use-After-Free (UAF) vulnerability and a related NULL pointer dereference, and to meticulously understand how the changes in `bdev` and `struct dm_dev` management led to this issue.
First, I examined the state *before* `86f1152b117a`, specifically at commit `5d0db96d13a4`. My analysis revealed that this commit introduced the `metadata_mode` initialization in `pool_ctr()`. Crucially, if a `thin-pool` was loaded with the `read_only` flag, both `metadata_dev` and `data_dev` would end up pointing to the *same* `&dd->dm_dev` structure. The `upgrade_mode()` function at this point operated by modifying the `dd->dm_dev` structure in-place, updating its `mode` flag. Importantly, the `dd->dm_dev` structure itself was *not* freed, and its `bdev` member remained valid. This meant that the subsequent call to `dm_bufio_client_create()` would not encounter a UAF or a NULL pointer dereference scenario.
Then, I turned my attention to commit `86f1152b117a`, which introduced `struct table_device` and fundamentally altered how `dm_dev` was handled. The key change here was transforming `dd->dm_dev` from an embedded struct to a pointer (`struct dm_dev *dm_dev`). The `upgrade_mode()` function was rewritten to:
1. Call `dm_get_table_device()` to allocate a new `table_device`.
2. Assign this new `table_device` to `dd->dm_dev`.
3. Crucially, call `dm_put_table_device()` on the *old* `table_device`. This operation would free the memory allocated for the old `table_device` and, critically, set its `bdev` member to `NULL`.
This transformation immediately created a problem. Any pre-existing pointers to the old `table_device` – such as `metadata_dev` in `pool_ctr()` (when metadata and data devices were the same), `s->origin` in `snapshot_ctr()`, `tc->origin_dev` in `thin_ctr()`, or even multiple targets within the same table – would now become dangling pointers to freed memory with a `bdev` of `NULL`. This would inevitably lead to a UAF or a NULL pointer dereference when these pointers were subsequently used.
I also considered the broader implications, realizing that even without the specific `pool_ctr()` scenario altered by `5d0db96d13a4`, commit `86f1152b117a` would have *still* broken other functionalities like `snapshot_ctr()` and `thin_ctr()`. These functions, even prior to `5d0db96d13a4`, would open origin devices with `FMODE_READ` and rely on pointer stability that `86f1152b117a` destroyed. Conversely, the change in `5d0db96d13a4` was safe in isolation; it was the subsequent pointer management change in `86f1152b117a` that made it vulnerable.
To definitively assign blame, I engaged in a mental "git bisect" process. I asked: could the vulnerability exist at `5d0db96d13a4`? My conclusion was no. The syzkaller reproducer's crash condition (UAF/NULL deref) is predicated on the `bdev` being set to `NULL` after a device is freed, which simply did not happen at `5d0db96d13a4`. However, at `86f1152b117a`, the `upgrade_mode()` function's behavior of freeing the old `table_device` and nullifying its `bdev` *directly* causes the syzkaller reproducer to hit the crash. Therefore, `86f1152b117a` is unambiguously the bug-introducing commit.
Finally, I performed a sanity check, reviewing the `open_dev`, `close_dev`, and `dm_table_destroy` functions immediately preceding `86f1152b117a` (at `86f1152b117a~1`) to ensure there were no subtle pre-existing conditions that could have somehow triggered the crash at `5d0db96d13a4`. My review confirmed that the critical pointer management issue leading to the UAF was indeed introduced by the changes in `86f1152b117a`.
|
| 52/2 |
2026/11/23 11:06 |
tool |
git-show |
0m
Args: map[Commit:86f1152b117a~1 File:drivers/md/dm-table.c]
Results:
|
| 53/2 |
2026/11/23 11:06 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 75786
output: 58
thoughts: 1370
Thoughts: **My Dive into the Device Mapper Commit `86f1152b117a`**
My investigation began by looking at the commit `86f1152b117a` (or its short form `f165921d` or `570b9d96`) to understand changes related to `open_dev` and `close_dev`. I recalled that these functions were indeed present in the diff output of `git-show 86f1152b117a` as removed lines, which piqued my interest.
Upon examining the provided `git-show 86f1152b117a` output, I saw the definitions for `open_dev` and `close_dev`.
* `open_dev` is responsible for acquiring a block device (`blkdev_get_by_dev`), linking it to a disk holder, and storing the `struct block_device *` in `d->dm_dev.bdev`.
* `close_dev` performs the inverse: it unlinks the disk holder, releases the block device (`blkdev_put`), and then nullifies `d->dm_dev.bdev`.
Then, I shifted my focus to the `upgrade_mode` function, specifically its behavior *before* the commit `86f1152b117a`. I observed how it operated:
1. A local struct `dm_dev_internal dd_old` was created, initialized as a direct copy of the current `*dd`.
2. `dd_new` was also initialized as a copy of `*dd`.
3. `dd_new.dm_dev.mode` was updated with `new_mode`, and `dd_new.dm_dev.bdev` was explicitly set to `NULL`. This is crucial because `open_dev` would then acquire a *new* `struct block_device *` for `dd_new`.
4. `open_dev(&dd_new, ...)` was called. This function, as noted, would increment the reference count for the block device associated with `dev`.
5. `dd->dm_dev.mode` was updated in place.
6. Finally, `close_dev(&dd_old, md)` was called. Here's the critical realization: because `dd_old` was a stack copy, calling `close_dev` on it only set `dd_old.dm_dev.bdev = NULL` on that stack variable. It did *not* affect the `bdev` pointer in the original `dd` structure.
My understanding at this point was that `dd->dm_dev.bdev` would remain valid and non-NULL because `open_dev` was called on `dd_new` (which would have incremented the refcount on the block device it was acquiring), and the `blkdev_put` inside `close_dev(&dd_old, ...)` would be decrementing the refcount that was originally associated with `dd_old`. Since `dd_new` and `dd_old` were copies, and `open_dev` was called on `dd_new`, the original `dd->dm_dev.bdev` should have been unaffected by the `close_dev` call on `dd_old`.
However, a new line of inquiry opened up when I considered the interaction with the metadata device. I remembered that there's a pathway: `pool_ctr` -> `__pool_find` -> `pool_create` -> `dm_pool_metadata_open` -> `dm_block_manager_create` -> `dm_bufio_client_create`. Before commit `86f1152b117a`, the `metadata_dev->bdev` was indeed non-NULL and valid.
Now, I looked at the *after* state of `upgrade_mode` following commit `86f1152b117a`. The key changes are:
1. `old_dev = dd->dm_dev;` This captures the current `dm_dev` pointer.
2. `dm_get_table_device(...)` is called to obtain `new_dev`.
3. `dd->dm_dev = new_dev;` The `dm_dev` pointer in the main `dd` structure is updated to point to the new device.
4. `dm_put_table_device(md, old_dev);` This is where the problem arises. `dm_put_table_device` decrements the table device count. When it drops to zero, it calls `close_table_device(td, md)`. `close_table_device` sets `td->dm_dev.bdev = NULL` and then `kfree(td)` frees the entire table device structure.
The issue is that `metadata_dev` was still pointing to `old_dev` (which is essentially `&td->dm_dev`). After `dm_put_table_device` frees `td`, `metadata_dev->bdev` becomes a use-after-free read. It attempts to access `bdev->bd_dev` at an offset of `0x24`, but the memory has already been deallocated. This is precisely what would cause a crash in `dm_bufio_client_create`.
Finally, I verified that commit `86f1152b117a404229fd6f08ec3faca779f37b92` is a standard commit, authored by Benjamin Marzinski and signed off by Mike Snitzer, not a merge commit.
With this detailed analysis, everything is clear. I am confident that the root cause of the crash is the use-after-free on `metadata_dev->bdev` due to the deallocation of the table device structure before it's no longer referenced by `metadata_dev`. Therefore, I'll proceed to report `86f1152b117a404229fd6f08ec3faca779f37b92` as the culprit.
|
| 54/2 |
2026/11/23 11:06 |
tool |
set-results |
0m
Args: map[CandidateCommits:[86f1152b117a404229fd6f08ec3faca779f37b92]]
Results: map[CandidateCommits:[86f1152b117a404229fd6f08ec3faca779f37b92]]
|
| 55/2 |
2026/11/23 11:06 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 77268
output: 1950
thoughts: 39
Thoughts: **My Thinking Process for Pinpointing the Root Cause**
Alright, let's break down how I arrived at this conclusion. The core of my task here is to provide a crystal-clear, accurate, and comprehensive summary. This summary needs to cover the fundamental root cause of the issue, detail the journey through the Git history that led me there, explain which specific commits I scrutinized, justify why certain other commits were ultimately deemed irrelevant to the problem, and finally, outline the logical progression of my investigation that brought me to my final determination. Essentially, I'm aiming to lay bare my thought process, ensuring you, as an expert in this domain, can follow the reasoning precisely and appreciate the depth of the analysis.
|
| 56/1 |
2026/11/23 11:06 |
action |
format-bisect |
0m
Results: map[Commits:[map[AuthorEmail:bmarzins@redhat.com AuthorName:Benjamin Marzinski Date:2014-08-13T13:53:43-05:00 Hash:86f1152b117a404229fd6f08ec3faca779f37b92 Recipients:[map[Email:bmarzins@redhat.com Name: To:true] map[Email:snitzer@redhat.com Name: To:true]] Title:dm: allow active and inactive tables to share dm_devs]] IsRelease:false]
|