| 0/0 |
2026/11/27 07:00 |
flow |
bisect-cause |
36m
Results: map[BisectReasoning:The bug was introduced by commit **`475d0fe795516a9b9f286a851c3972fd8831c643`** (`"nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME"`).
### Root Cause
In `drivers/nvme/target/fcloop.c`, `fcloop_create_target_port()` creates a target port via `fcloop_alloc_nport()` followed by `nvmet_fc_register_targetport()`, and only sets `nport->tport = tport` after `nvmet_fc_register_targetport()` returns:
1. `fcloop_alloc_nport()` takes `fcloop_lock`, looks up an existing `fcloop_nport` for `(wwnn, wwpn)`, and checks `(!remoteport && nport->tport)` to reject duplicate target ports for the same WWNs.
2. If no `nport` exists (or `nport->tport` is `NULL`), it adds the new `nport` to `fcloop_nports` with `nport->tport == NULL`, releases `fcloop_lock`, and returns `nport`.
3. `fcloop_create_target_port()` then calls `nvmet_fc_register_targetport()` without holding any lock and while `nport->tport` is still `NULL`. Only after registration succeeds (line 1573) does it assign `nport->tport = tport`.
Because of this TOCTOU window, two threads concurrently writing the same `(wwnn, wwpn)` to `/sys/class/fcloop/ctl/add_target_port` can both pass the `!nport->tport` check in `fcloop_alloc_nport()`, obtain the same `nport`, and both invoke `nvmet_fc_register_targetport()` with identical `(node_name, port_name)`.
When an `nvmet_port` was previously configured for those WWNs and the earlier target port was deleted (`del_target_port`), an unbound `struct nvmet_fc_port_entry` (`pe` with `pe->tgtport == NULL`) remains on `nvmet_fc_portentry_list`. During the concurrent re-creation:
- Thread 1's `nvmet_fc_register_targetport()` -> `nvmet_fc_portentry_rebind_tgt()` binds `pe->tgtport = tgtport1`.
- Thread 2's `nvmet_fc_register_targetport()` -> `nvmet_fc_portentry_rebind_tgt()` finds the same `pe` with `pe->tgtport` already non-NULL and triggers `WARN_ON(pe->tgtport)` at `drivers/nvme/target/fc.c:1327` (and subsequently overwrites both `pe->tgtport` and `nport->tport`, leaking the first target port).
### Bisection Log & Related Commits Examined
- **`475d0fe795516a9b9f286a851c3972fd8831c643`** (`"nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME"`): **Primary introducing commit.** Added `fcloop_alloc_nport()` and `fcloop_create_target_port()` with the TOCTOU race where `fcloop_lock` is dropped with `nport->tport == NULL` before `nvmet_fc_register_targetport()` and `nport->tport = tport`.
- **`ea96d6496ff59b2b26dc9e13dc8f57d77731eb37`** (`"nvmet_fc: support target port removal with nvmet layer"`): Added `nvmet_fc_portentry_rebind_tgt()` and `WARN_ON(pe->tgtport)` at `drivers/nvme/target/fc.c:1327`. Ruled out as the root cause because the `WARN_ON` asserts a valid FC invariant (an LLDD must not register duplicate active target ports for the same WWNs) and merely exposed the pre-existing race in `fcloop.c`.
- **`d54a9d7f6d74ee385d496f0efd282ce9d4fbfcbc`** (`"nvmet-fcloop: refactor fcloop_nport_alloc and track lport"`): Refactored `fcloop_alloc_nport()` into `__fcloop_nport_lookup()` / `fcloop_alloc_nport()`, preserving the same TOCTOU behavior from `475d0fe795516`.
- **`596cba55adb473f26bfd3f6dd78f169810069531`** (`"nvmet-fc: take tgtport refs for portentry"`): Added `nvmet_fc_tgtport_get()`/`put()` around `pe->tgtport` bindings; did not alter the concurrent `fcloop_create_target_port()` race.
- **`2b559a3eb56b6e1a51ca6f4a17778a1b4e14a591`** (`"nvmet-fcloop: prevent double port deletion"`): Only added `PORT_DELETED` handling for port deletion and initialized `tport->flags = 0`. Commits:[map[AuthorEmail:jsmart2021@gmail.com AuthorName:James Smart Date:2016-12-02T00:28:44-08:00 Hash:475d0fe795516a9b9f286a851c3972fd8831c643 Recipients:[map[Email:hch@lst.de Name: To:true] map[Email:james.smart@broadcom.com Name: To:true] map[Email:james_p_freyensee@linux.intel.com Name: To:true] map[Email:jsmart2021@gmail.com Name: To:true] map[Email:jthumshirn@suse.de Name: To:true]] Title:nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME]] IsRelease:false KernelCommit:4056b3c9d3c92ea45955641dbafeb9b960e146ef]
|
| 1/1 |
2026/11/27 07:00 |
action |
use-crash-commit |
0m
Results: map[KernelCommit:4056b3c9d3c92ea45955641dbafeb9b960e146ef]
|
| 2/1 |
2026/11/27 07:00 |
action |
syz-repro-to-c-repro |
0m
Results: map[SimplifiedCRepro:#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <pthread.h>
#include <sched.h>
#include <time.h>
#include <errno.h>
// Copyright 2026 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// IMPORTANT: Do not copy the macros or definitions below directly into your reproducer.
// Instead, add the following line to your reproducer:
// #include "race_toolkit.h"
// --- Race Condition Toolkit ---
// Macros and snippets for CPU pinning, memory barriers, and userfaultfd.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/futex.h>
#include <linux/userfaultfd.h>
#include <poll.h>
#include <pthread.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
// Unbuffered I/O: Ensure logs are written immediately.
#define SETUP_UNBUFFERED_IO() setvbuf(stdout, NULL, _IONBF, 0)
// CPU Pinning: Pin the current thread to a specific CPU core.
#define PIN_TO_CPU(cpu) \
do { \
cpu_set_t mask; \
CPU_ZERO(&mask); \
CPU_SET(cpu, &mask); \
if (sched_setaffinity(0, sizeof(mask), &mask) == -1) { \
perror("sched_setaffinity"); \
} \
} while (0)
// Memory Barrier: Ensure memory ordering.
#define MB() __atomic_thread_fence(__ATOMIC_SEQ_CST)
// Spin-wait Barrier: Wait until a memory location has a specific value.
// Best for tight race windows (low latency, no context switches).
#define WAIT_ON(addr, val) \
do { \
while (__atomic_load_n(addr, __ATOMIC_ACQUIRE) != (val)) \
; \
} while (0)
// Signal: Set a memory location to a specific value to release a WAIT_ON.
#define SIGNAL(addr, val) __atomic_store_n(addr, val, __ATOMIC_RELEASE)
// --- Timing Primitives ---
// Robust timing loops in VM environments (using CLOCK_MONOTONIC to avoid time(NULL) jumps).
static inline double timer_elapsed_sec(struct timespec* start)
{
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) {
perror("clock_gettime(CLOCK_MONOTONIC) elapsed");
exit(1);
}
return (double)(now.tv_sec - start->tv_sec) + (double)(now.tv_nsec - start->tv_nsec) / 1e9;
}
// Initialize a monotonic timer variable.
#define TIMER_START(t) \
struct timespec t; \
if (clock_gettime(CLOCK_MONOTONIC, &t) == -1) { \
perror("clock_gettime(CLOCK_MONOTONIC) start"); \
exit(1); \
}
// Check if the elapsed time since 't' is less than 'sec' seconds.
#define TIMER_NOT_EXPIRED(t, sec) (timer_elapsed_sec(&(t)) < (double)(sec))
// Futex-based Event: Shared with syzkaller executor.
// Best for general synchronization or longer waits to save CPU.
typedef struct {
int state;
} event_t;
static void event_init(event_t* ev)
{
ev->state = 0;
}
static void event_reset(event_t* ev)
{
ev->state = 0;
}
static void event_set(event_t* ev)
{
if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) {
fprintf(stderr, "event already set\n");
exit(1);
}
__atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
}
static void event_wait(event_t* ev)
{
while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
}
// userfaultfd setup: Register a memory range for page fault handling.
static int setup_uffd(void* addr, size_t len)
{
int uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (uffd == -1)
return -1;
struct uffdio_api api = {.api = UFFD_API, .features = 0};
if (ioctl(uffd, UFFDIO_API, &api) == -1) {
close(uffd);
return -1;
}
struct uffdio_register reg = {
.range = {.start = (uintptr_t)addr, .len = len},
.mode = UFFDIO_REGISTER_MODE_MISSING};
if (ioctl(uffd, UFFDIO_REGISTER, ®) == -1) {
close(uffd);
return -1;
}
return uffd;
}
// --- Guidance on Usage ---
// 1. Use WAIT_ON/SIGNAL for tight race conditions to avoid scheduling overhead.
// 2. Use event_t (futexes) for general coordination or when waiting for longer periods.
// 3. Always use PIN_TO_CPU to increase race probability on multi-core systems.
// 4. Use setup_uffd to register a memory range for page fault handling. This allows you to
// pause a thread accessing that memory until you handle the fault, creating a reliable
// and controllable race window.
// 5. Call SETUP_UNBUFFERED_IO() at the start of main() to ensure that logs are printed
// immediately. This is essential for understanding the exact interleaving of events
// when debugging race conditions.
// 6. For timing-based loops (e.g., running a race for 10 seconds), do NOT use time(NULL)
// or loops relying on real-time clocks, as VM clocks are highly unreliable and can fail or drift.
// Instead, use the robust monotonic timing primitives TIMER_START and TIMER_NOT_EXPIRED:
// TIMER_START(start);
// while (TIMER_NOT_EXPIRED(start, 10.0)) {
// // Your race logic here
// }
void write_file(const char *path, const char *data) {
int fd = open(path, O_WRONLY);
if (fd < 0) {
printf("[-] Failed to open %s: %s\n", path, strerror(errno));
exit(1);
}
if (write(fd, data, strlen(data)) < 0) {
printf("[-] Failed to write to %s: %s\n", path, strerror(errno));
close(fd);
exit(1);
}
close(fd);
}
void write_file_ignore_err(const char *path, const char *data) {
int fd = open(path, O_WRONLY);
if (fd >= 0) {
write(fd, data, strlen(data));
close(fd);
}
}
char current_wwnn_str[128];
int start_flag = 0;
void *thread_func(void *arg) {
long cpu = (long)arg;
PIN_TO_CPU(cpu);
int fd = open("/sys/class/fcloop/ctl/add_target_port", O_WRONLY);
if (fd < 0) {
printf("[-] Thread failed to open add_target_port: %s\n", strerror(errno));
return NULL;
}
WAIT_ON(&start_flag, 1);
write(fd, current_wwnn_str, strlen(current_wwnn_str));
close(fd);
return NULL;
}
int main() {
SETUP_UNBUFFERED_IO();
if (mount("configfs", "/sys/kernel/config", "configfs", 0, NULL) != 0) {
if (errno != EBUSY) {
printf("[-] Failed to mount configfs: %s\n", strerror(errno));
exit(1);
}
}
printf("[+] configfs mounted.\n");
unsigned long long i = 1;
TIMER_START(start);
while (TIMER_NOT_EXPIRED(start, 10.0)) {
sprintf(current_wwnn_str, "wwnn=0x%016llx,wwpn=0x%016llx", i, i);
char traddr_str[128];
sprintf(traddr_str, "nn-0x%016llx:pn-0x%016llx", i, i);
// 1. Create the initial target port
write_file("/sys/class/fcloop/ctl/add_target_port", current_wwnn_str);
// 2. Create an NVMe-oF port and configure it for FC
char port_path[128];
sprintf(port_path, "/sys/kernel/config/nvmet/ports/%llu", i);
if (mkdir(port_path, 0777) < 0) {
printf("[-] Failed to mkdir %s: %s\n", port_path, strerror(errno));
exit(1);
}
char path_buf[256];
sprintf(path_buf, "%s/addr_trtype", port_path);
write_file(path_buf, "fc");
sprintf(path_buf, "%s/addr_traddr", port_path);
write_file(path_buf, traddr_str);
sprintf(path_buf, "%s/addr_adrfam", port_path);
write_file(path_buf, "fc");
// 3. Create a subsystem and link it to the port to enable it
char sub_path[128];
sprintf(sub_path, "/sys/kernel/config/nvmet/subsystems/%llu", i);
if (mkdir(sub_path, 0777) < 0) {
printf("[-] Failed to mkdir %s: %s\n", sub_path, strerror(errno));
exit(1);
}
char symlink_path[256];
sprintf(symlink_path, "%s/subsystems/%llu", port_path, i);
if (symlink(sub_path, symlink_path) < 0) {
printf("[-] Failed to symlink %s to %s: %s\n", sub_path, symlink_path, strerror(errno));
exit(1);
}
// 4. Delete the target port
write_file("/sys/class/fcloop/ctl/del_target_port", current_wwnn_str);
// 5. Concurrently create the target port to trigger the race condition
start_flag = 0;
pthread_t t1, t2;
if (pthread_create(&t1, NULL, thread_func, (void*)0) != 0) {
printf("[-] Failed to create thread 1\n");
exit(1);
}
if (pthread_create(&t2, NULL, thread_func, (void*)1) != 0) {
printf("[-] Failed to create thread 2\n");
exit(1);
}
usleep(5000);
SIGNAL(&start_flag, 1);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
// Cleanup for next iteration
unlink(symlink_path);
rmdir(sub_path);
rmdir(port_path);
write_file_ignore_err("/sys/class/fcloop/ctl/del_target_port", current_wwnn_str);
i++;
}
printf("[+] Reproducer finished.\n");
return 0;
}
]
|
| 3/1 |
2026/11/27 07:00 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/app/workdir/cache/src/8e2574b003f81387c02f44fb1bd815288357d882/data]
|
| 4/1 |
2026/11/27 07:00 |
action |
kernel-builder |
17m
Results: map[KernelObj:/app/workdir/cache/build/c0027a5f90ae325d785053c160a35e4e51d74b30/data]
|
| 5/1 |
2026/11/27 07:18 |
action |
codesearch-prepare |
12m
Results: map[Index:codesearch-index]
|
| 6/1 |
2026/11/27 07:31 |
agent |
cause-finder |
5m
Model:
Results: map[CandidateCommits:[475d0fe795516a9b9f286a851c3972fd8831c643]]
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.
Analyze the crash report and the code it points to (stack traces, allocation/free stacks,
the involved functions and structs) until you understand the exact invariant that is
violated and the exact execution sequence that leads to the crash.
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.
During execution, you may receive periodic guidance in <supervisor_advice> tags;
use it to refine your investigation or wrap up once you have sufficient evidence.
Prompt:
The crash is:
------------[ cut here ]------------
pe->tgtport
WARNING: drivers/nvme/target/fc.c:1327 at nvmet_fc_portentry_rebind_tgt+0x1d7/0x1e0 drivers/nvme/target/fc.c:1327, CPU#1: syz-executor148/5937
Modules linked in:
CPU: 1 UID: 0 PID: 5937 Comm: syz-executor148 Not tainted syzkaller #0 PREEMPT_{RT,(full)}
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/07/2026
RIP: 0010:nvmet_fc_portentry_rebind_tgt+0x1d7/0x1e0 drivers/nvme/target/fc.c:1327
Code: 6d 3d fc 48 8b 04 24 49 89 06 48 c7 c7 20 0f c8 8f 48 83 c4 18 5b 41 5c 41 5d 41 5e 41 5f 5d e9 3f ab 66 05 e8 ba a7 d0 fb 90 <0f> 0b 90 eb a6 0f 1f 40 00 90 90 90 90 90 90 90 90 90 90 90 90 90
RSP: 0018:ffffc9000155fa78 EFLAGS: 00010293
RAX: ffffffff85f63196 RBX: dffffc0000000000 RCX: ffff888032880000
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000000
RBP: 0000000000000001 R08: 0000000000000000 R09: 0000000000000000
R10: dffffc0000000000 R11: fffff520002abf40 R12: 1ffff1100741b8d0
R13: 0000000000000001 R14: ffff88803a0dc680 R15: ffff888012526800
FS: 00007f3c0db506c0(0000) GS:ffff888124abb000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f3c0db4fff8 CR3: 0000000042756000 CR4: 00000000003526f0
Call Trace:
<TASK>
nvmet_fc_register_targetport+0x75a/0x950 drivers/nvme/target/fc.c:1417
fcloop_create_target_port+0x135/0x4d0 drivers/nvme/target/fcloop.c:1558
kernfs_fop_write_iter+0x3a5/0x540 fs/kernfs/file.c:345
new_sync_write fs/read_write.c:595 [inline]
vfs_write+0x61e/0xbb0 fs/read_write.c:687
ksys_write+0x156/0x270 fs/read_write.c:739
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x166/0x520 arch/x86/entry/syscall_64.c:84
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f3c0e3ae35e
Code: 08 0f 85 f5 e2 ff ff 49 89 fb 48 89 f0 48 89 d7 48 89 ce 4c 89 c2 4d 89 ca 4c 8b 44 24 08 4c 8b 4c 24 10 4c 89 5c 24 08 0f 05 <c3> 90 41 57 41 56 4d 89 c6 41 55 4d 89 cd 41 54 55 53 48 83 ec 08
RSP: 002b:00007f3c0db50118 EFLAGS: 00000246 ORIG_RAX: 0000000000000001
RAX: ffffffffffffffda RBX: 00007f3c0db506c0 RCX: 00007f3c0e3ae35e
RDX: 000000000000002f RSI: 00007f3c0e427b40 RDI: 0000000000000004
RBP: 00007f3c0e427b40 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: ffffffffffffffd0
R13: 0000000000000010 R14: 00007ffdb343b7d0 R15: 00007ffdb343b8b8
</TASK>
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:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <pthread.h>
#include <sched.h>
#include <time.h>
#include <errno.h>
// Copyright 2026 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// IMPORTANT: Do not copy the macros or definitions below directly into your reproducer.
// Instead, add the following line to your reproducer:
// #include "race_toolkit.h"
// --- Race Condition Toolkit ---
// Macros and snippets for CPU pinning, memory barriers, and userfaultfd.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/futex.h>
#include <linux/userfaultfd.h>
#include <poll.h>
#include <pthread.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
// Unbuffered I/O: Ensure logs are written immediately.
#define SETUP_UNBUFFERED_IO() setvbuf(stdout, NULL, _IONBF, 0)
// CPU Pinning: Pin the current thread to a specific CPU core.
#define PIN_TO_CPU(cpu) \
do { \
cpu_set_t mask; \
CPU_ZERO(&mask); \
CPU_SET(cpu, &mask); \
if (sched_setaffinity(0, sizeof(mask), &mask) == -1) { \
perror("sched_setaffinity"); \
} \
} while (0)
// Memory Barrier: Ensure memory ordering.
#define MB() __atomic_thread_fence(__ATOMIC_SEQ_CST)
// Spin-wait Barrier: Wait until a memory location has a specific value.
// Best for tight race windows (low latency, no context switches).
#define WAIT_ON(addr, val) \
do { \
while (__atomic_load_n(addr, __ATOMIC_ACQUIRE) != (val)) \
; \
} while (0)
// Signal: Set a memory location to a specific value to release a WAIT_ON.
#define SIGNAL(addr, val) __atomic_store_n(addr, val, __ATOMIC_RELEASE)
// --- Timing Primitives ---
// Robust timing loops in VM environments (using CLOCK_MONOTONIC to avoid time(NULL) jumps).
static inline double timer_elapsed_sec(struct timespec* start)
{
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) {
perror("clock_gettime(CLOCK_MONOTONIC) elapsed");
exit(1);
}
return (double)(now.tv_sec - start->tv_sec) + (double)(now.tv_nsec - start->tv_nsec) / 1e9;
}
// Initialize a monotonic timer variable.
#define TIMER_START(t) \
struct timespec t; \
if (clock_gettime(CLOCK_MONOTONIC, &t) == -1) { \
perror("clock_gettime(CLOCK_MONOTONIC) start"); \
exit(1); \
}
// Check if the elapsed time since 't' is less than 'sec' seconds.
#define TIMER_NOT_EXPIRED(t, sec) (timer_elapsed_sec(&(t)) < (double)(sec))
// Futex-based Event: Shared with syzkaller executor.
// Best for general synchronization or longer waits to save CPU.
typedef struct {
int state;
} event_t;
static void event_init(event_t* ev)
{
ev->state = 0;
}
static void event_reset(event_t* ev)
{
ev->state = 0;
}
static void event_set(event_t* ev)
{
if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) {
fprintf(stderr, "event already set\n");
exit(1);
}
__atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
}
static void event_wait(event_t* ev)
{
while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
}
// userfaultfd setup: Register a memory range for page fault handling.
static int setup_uffd(void* addr, size_t len)
{
int uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (uffd == -1)
return -1;
struct uffdio_api api = {.api = UFFD_API, .features = 0};
if (ioctl(uffd, UFFDIO_API, &api) == -1) {
close(uffd);
return -1;
}
struct uffdio_register reg = {
.range = {.start = (uintptr_t)addr, .len = len},
.mode = UFFDIO_REGISTER_MODE_MISSING};
if (ioctl(uffd, UFFDIO_REGISTER, ®) == -1) {
close(uffd);
return -1;
}
return uffd;
}
// --- Guidance on Usage ---
// 1. Use WAIT_ON/SIGNAL for tight race conditions to avoid scheduling overhead.
// 2. Use event_t (futexes) for general coordination or when waiting for longer periods.
// 3. Always use PIN_TO_CPU to increase race probability on multi-core systems.
// 4. Use setup_uffd to register a memory range for page fault handling. This allows you to
// pause a thread accessing that memory until you handle the fault, creating a reliable
// and controllable race window.
// 5. Call SETUP_UNBUFFERED_IO() at the start of main() to ensure that logs are printed
// immediately. This is essential for understanding the exact interleaving of events
// when debugging race conditions.
// 6. For timing-based loops (e.g., running a race for 10 seconds), do NOT use time(NULL)
// or loops relying on real-time clocks, as VM clocks are highly unreliable and can fail or drift.
// Instead, use the robust monotonic timing primitives TIMER_START and TIMER_NOT_EXPIRED:
// TIMER_START(start);
// while (TIMER_NOT_EXPIRED(start, 10.0)) {
// // Your race logic here
// }
void write_file(const char *path, const char *data) {
int fd = open(path, O_WRONLY);
if (fd < 0) {
printf("[-] Failed to open %s: %s\n", path, strerror(errno));
exit(1);
}
if (write(fd, data, strlen(data)) < 0) {
printf("[-] Failed to write to %s: %s\n", path, strerror(errno));
close(fd);
exit(1);
}
close(fd);
}
void write_file_ignore_err(const char *path, const char *data) {
int fd = open(path, O_WRONLY);
if (fd >= 0) {
write(fd, data, strlen(data));
close(fd);
}
}
char current_wwnn_str[128];
int start_flag = 0;
void *thread_func(void *arg) {
long cpu = (long)arg;
PIN_TO_CPU(cpu);
int fd = open("/sys/class/fcloop/ctl/add_target_port", O_WRONLY);
if (fd < 0) {
printf("[-] Thread failed to open add_target_port: %s\n", strerror(errno));
return NULL;
}
WAIT_ON(&start_flag, 1);
write(fd, current_wwnn_str, strlen(current_wwnn_str));
close(fd);
return NULL;
}
int main() {
SETUP_UNBUFFERED_IO();
if (mount("configfs", "/sys/kernel/config", "configfs", 0, NULL) != 0) {
if (errno != EBUSY) {
printf("[-] Failed to mount configfs: %s\n", strerror(errno));
exit(1);
}
}
printf("[+] configfs mounted.\n");
unsigned long long i = 1;
TIMER_START(start);
while (TIMER_NOT_EXPIRED(start, 10.0)) {
sprintf(current_wwnn_str, "wwnn=0x%016llx,wwpn=0x%016llx", i, i);
char traddr_str[128];
sprintf(traddr_str, "nn-0x%016llx:pn-0x%016llx", i, i);
// 1. Create the initial target port
write_file("/sys/class/fcloop/ctl/add_target_port", current_wwnn_str);
// 2. Create an NVMe-oF port and configure it for FC
char port_path[128];
sprintf(port_path, "/sys/kernel/config/nvmet/ports/%llu", i);
if (mkdir(port_path, 0777) < 0) {
printf("[-] Failed to mkdir %s: %s\n", port_path, strerror(errno));
exit(1);
}
char path_buf[256];
sprintf(path_buf, "%s/addr_trtype", port_path);
write_file(path_buf, "fc");
sprintf(path_buf, "%s/addr_traddr", port_path);
write_file(path_buf, traddr_str);
sprintf(path_buf, "%s/addr_adrfam", port_path);
write_file(path_buf, "fc");
// 3. Create a subsystem and link it to the port to enable it
char sub_path[128];
sprintf(sub_path, "/sys/kernel/config/nvmet/subsystems/%llu", i);
if (mkdir(sub_path, 0777) < 0) {
printf("[-] Failed to mkdir %s: %s\n", sub_path, strerror(errno));
exit(1);
}
char symlink_path[256];
sprintf(symlink_path, "%s/subsystems/%llu", port_path, i);
if (symlink(sub_path, symlink_path) < 0) {
printf("[-] Failed to symlink %s to %s: %s\n", sub_path, symlink_path, strerror(errno));
exit(1);
}
// 4. Delete the target port
write_file("/sys/class/fcloop/ctl/del_target_port", current_wwnn_str);
// 5. Concurrently create the target port to trigger the race condition
start_flag = 0;
pthread_t t1, t2;
if (pthread_create(&t1, NULL, thread_func, (void*)0) != 0) {
printf("[-] Failed to create thread 1\n");
exit(1);
}
if (pthread_create(&t2, NULL, thread_func, (void*)1) != 0) {
printf("[-] Failed to create thread 2\n");
exit(1);
}
usleep(5000);
SIGNAL(&start_flag, 1);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
// Cleanup for next iteration
unlink(symlink_path);
rmdir(sub_path);
rmdir(port_path);
write_file_ignore_err("/sys/class/fcloop/ctl/del_target_port", current_wwnn_str);
i++;
}
printf("[+] Reproducer finished.\n");
return 0;
}
Search for the commit that introduced this bug.
Reply: The bug was introduced by commit **`475d0fe795516a9b9f286a851c3972fd8831c643`** (`"nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME"`).
### Root Cause
In `drivers/nvme/target/fcloop.c`, `fcloop_create_target_port()` creates a target port via `fcloop_alloc_nport()` followed by `nvmet_fc_register_targetport()`, and only sets `nport->tport = tport` after `nvmet_fc_register_targetport()` returns:
1. `fcloop_alloc_nport()` takes `fcloop_lock`, looks up an existing `fcloop_nport` for `(wwnn, wwpn)`, and checks `(!remoteport && nport->tport)` to reject duplicate target ports for the same WWNs.
2. If no `nport` exists (or `nport->tport` is `NULL`), it adds the new `nport` to `fcloop_nports` with `nport->tport == NULL`, releases `fcloop_lock`, and returns `nport`.
3. `fcloop_create_target_port()` then calls `nvmet_fc_register_targetport()` without holding any lock and while `nport->tport` is still `NULL`. Only after registration succeeds (line 1573) does it assign `nport->tport = tport`.
Because of this TOCTOU window, two threads concurrently writing the same `(wwnn, wwpn)` to `/sys/class/fcloop/ctl/add_target_port` can both pass the `!nport->tport` check in `fcloop_alloc_nport()`, obtain the same `nport`, and both invoke `nvmet_fc_register_targetport()` with identical `(node_name, port_name)`.
When an `nvmet_port` was previously configured for those WWNs and the earlier target port was deleted (`del_target_port`), an unbound `struct nvmet_fc_port_entry` (`pe` with `pe->tgtport == NULL`) remains on `nvmet_fc_portentry_list`. During the concurrent re-creation:
- Thread 1's `nvmet_fc_register_targetport()` -> `nvmet_fc_portentry_rebind_tgt()` binds `pe->tgtport = tgtport1`.
- Thread 2's `nvmet_fc_register_targetport()` -> `nvmet_fc_portentry_rebind_tgt()` finds the same `pe` with `pe->tgtport` already non-NULL and triggers `WARN_ON(pe->tgtport)` at `drivers/nvme/target/fc.c:1327` (and subsequently overwrites both `pe->tgtport` and `nport->tport`, leaking the first target port).
### Bisection Log & Related Commits Examined
- **`475d0fe795516a9b9f286a851c3972fd8831c643`** (`"nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME"`): **Primary introducing commit.** Added `fcloop_alloc_nport()` and `fcloop_create_target_port()` with the TOCTOU race where `fcloop_lock` is dropped with `nport->tport == NULL` before `nvmet_fc_register_targetport()` and `nport->tport = tport`.
- **`ea96d6496ff59b2b26dc9e13dc8f57d77731eb37`** (`"nvmet_fc: support target port removal with nvmet layer"`): Added `nvmet_fc_portentry_rebind_tgt()` and `WARN_ON(pe->tgtport)` at `drivers/nvme/target/fc.c:1327`. Ruled out as the root cause because the `WARN_ON` asserts a valid FC invariant (an LLDD must not register duplicate active target ports for the same WWNs) and merely exposed the pre-existing race in `fcloop.c`.
- **`d54a9d7f6d74ee385d496f0efd282ce9d4fbfcbc`** (`"nvmet-fcloop: refactor fcloop_nport_alloc and track lport"`): Refactored `fcloop_alloc_nport()` into `__fcloop_nport_lookup()` / `fcloop_alloc_nport()`, preserving the same TOCTOU behavior from `475d0fe795516`.
- **`596cba55adb473f26bfd3f6dd78f169810069531`** (`"nvmet-fc: take tgtport refs for portentry"`): Added `nvmet_fc_tgtport_get()`/`put()` around `pe->tgtport` bindings; did not alter the concurrent `fcloop_create_target_port()` race.
- **`2b559a3eb56b6e1a51ca6f4a17778a1b4e14a591`** (`"nvmet-fcloop: prevent double port deletion"`): Only added `PORT_DELETED` handling for port deletion and initialized `tport->flags = 0`.
|
| 7/2 |
2026/11/27 07:31 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 9487
output: 186
thoughts: 184
Thoughts: **My Diagnostic Journey into the NVMe-FC Target Port Rebind Crash**
Here I am, staring at this crash report and the immediate urge is to dive deep into the code. The specific crash is a `WARNING` originating from `drivers/nvme/target/fc.c` at line 1327, precisely within the `nvmet_fc_portentry_rebind_tgt` function, and it points to `pe->tgtport` being the problematic condition.
My next step is to meticulously examine the code surrounding line 1327 in `drivers/nvme/target/fc.c`. Simultaneously, I need to bring in `drivers/nvme/target/fcloop.c` and investigate what's happening around its line 1558. To get a comprehensive view of the relationships and contexts, I'll be leveraging `codesearch-definition-source`. I'm particularly interested in tracing the definitions and usages of key functions like `nvmet_fc_portentry_rebind_tgt` (the site of the crash), `nvmet_fc_register_targetport` (likely involved in the setup phase), and `fcloop_create_target_port` (which seems fundamental to the Fibre Channel loop's target port creation). My goal is to understand the lifecycle of `pe->tgtport` and how it reaches this invalid state during a rebind operation, aiming to pinpoint the exact sequence of events or missing checks that lead to this warning.
|
| 8/2 |
2026/11/27 07:31 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/nvme/target/fc.c Name:nvmet_fc_portentry_rebind_tgt]
Results: map[SourceCode:1306: /*
1307: * called when a new targetport is registered. Looks in the
1308: * existing nvmet port_entries to see if the nvmet layer is
1309: * configured for the targetport's wwn's. (the targetport existed,
1310: * nvmet configured, the lldd unregistered the tgtport, and is now
1311: * reregistering the same targetport). If so, set the nvmet port
1312: * port entry on the targetport.
1313: */
1314: static void
1315: nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
1316: {
1317: struct nvmet_fc_port_entry *pe;
1318: unsigned long flags;
1319:
1320: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1321: list_for_each_entry(pe, &nvmet_fc_portentry_list, pe_list) {
1322: if (tgtport->fc_target_port.node_name == pe->node_name &&
1323: tgtport->fc_target_port.port_name == pe->port_name) {
1324: if (!nvmet_fc_tgtport_get(tgtport))
1325: continue;
1326:
1327: WARN_ON(pe->tgtport);
1328: tgtport->pe = pe;
1329: pe->tgtport = tgtport;
1330: break;
1331: }
1332: }
1333: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1334: }
SourceFile:drivers/nvme/target/fc.c]
|
| 9/2 |
2026/11/27 07:31 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/nvme/target/fc.c Name:nvmet_fc_register_targetport]
Results: map[SourceCode:1336: /**
1337: * nvmet_fc_register_targetport - transport entry point called by an
1338: * LLDD to register the existence of a local
1339: * NVME subsystem FC port.
1340: * @pinfo: pointer to information about the port to be registered
1341: * @template: LLDD entrypoints and operational parameters for the port
1342: * @dev: physical hardware device node port corresponds to. Will be
1343: * used for DMA mappings
1344: * @portptr: pointer to a local port pointer. Upon success, the routine
1345: * will allocate a nvme_fc_local_port structure and place its
1346: * address in the local port pointer. Upon failure, local port
1347: * pointer will be set to NULL.
1348: *
1349: * Returns:
1350: * a completion status. Must be 0 upon success; a negative errno
1351: * (ex: -ENXIO) upon failure.
1352: */
1353: int
1354: nvmet_fc_register_targetport(struct nvmet_fc_port_info *pinfo,
1355: struct nvmet_fc_target_template *template,
1356: struct device *dev,
1357: struct nvmet_fc_target_port **portptr)
1358: {
1359: struct nvmet_fc_tgtport *newrec;
1360: unsigned long flags;
1361: int ret, idx;
1362:
1363: if (!template->xmt_ls_rsp || !template->fcp_op ||
1364: !template->fcp_abort ||
1365: !template->fcp_req_release || !template->targetport_delete ||
1366: !template->max_hw_queues || !template->max_sgl_segments ||
1367: !template->max_dif_sgl_segments || !template->dma_boundary) {
1368: ret = -EINVAL;
1369: goto out_regtgt_failed;
1370: }
1371:
1372: newrec = kzalloc((sizeof(*newrec) + template->target_priv_sz),
1373: GFP_KERNEL);
1374: if (!newrec) {
1375: ret = -ENOMEM;
1376: goto out_regtgt_failed;
1377: }
1378:
1379: idx = ida_alloc(&nvmet_fc_tgtport_cnt, GFP_KERNEL);
1380: if (idx < 0) {
1381: ret = -ENOSPC;
1382: goto out_fail_kfree;
1383: }
1384:
1385: if (!get_device(dev) && dev) {
1386: ret = -ENODEV;
1387: goto out_ida_put;
1388: }
1389:
1390: newrec->fc_target_port.node_name = pinfo->node_name;
1391: newrec->fc_target_port.port_name = pinfo->port_name;
1392: if (template->target_priv_sz)
1393: newrec->fc_target_port.private = &newrec[1];
1394: else
1395: newrec->fc_target_port.private = NULL;
1396: newrec->fc_target_port.port_id = pinfo->port_id;
1397: newrec->fc_target_port.port_num = idx;
1398: INIT_LIST_HEAD(&newrec->tgt_list);
1399: newrec->dev = dev;
1400: newrec->ops = template;
1401: spin_lock_init(&newrec->lock);
1402: INIT_LIST_HEAD(&newrec->ls_rcv_list);
1403: INIT_LIST_HEAD(&newrec->ls_req_list);
1404: INIT_LIST_HEAD(&newrec->ls_busylist);
1405: INIT_LIST_HEAD(&newrec->assoc_list);
1406: INIT_LIST_HEAD(&newrec->host_list);
1407: kref_init(&newrec->ref);
1408: ida_init(&newrec->assoc_cnt);
1409: newrec->max_sg_cnt = template->max_sgl_segments;
1410:
1411: ret = nvmet_fc_alloc_ls_iodlist(newrec);
1412: if (ret) {
1413: ret = -ENOMEM;
1414: goto out_free_newrec;
1415: }
1416:
1417: nvmet_fc_portentry_rebind_tgt(newrec);
1418:
1419: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1420: list_add_tail(&newrec->tgt_list, &nvmet_fc_target_list);
1421: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1422:
1423: *portptr = &newrec->fc_target_port;
1424: return 0;
1425:
1426: out_free_newrec:
1427: put_device(dev);
1428: out_ida_put:
1429: ida_free(&nvmet_fc_tgtport_cnt, idx);
1430: out_fail_kfree:
1431: kfree(newrec);
1432: out_regtgt_failed:
1433: *portptr = NULL;
1434: return ret;
1435: }
SourceFile:drivers/nvme/target/fc.c]
|
| 10/2 |
2026/11/27 07:31 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/nvme/target/fcloop.c Name:fcloop_create_target_port]
Results: map[SourceCode:1540: static ssize_t
1541: fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
1542: const char *buf, size_t count)
1543: {
1544: struct nvmet_fc_target_port *targetport;
1545: struct fcloop_nport *nport;
1546: struct fcloop_tport *tport;
1547: struct nvmet_fc_port_info tinfo;
1548: int ret;
1549:
1550: nport = fcloop_alloc_nport(buf, count, false);
1551: if (!nport)
1552: return -EIO;
1553:
1554: tinfo.node_name = nport->node_name;
1555: tinfo.port_name = nport->port_name;
1556: tinfo.port_id = nport->port_id;
1557:
1558: ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
1559: &targetport);
1560: if (ret) {
1561: fcloop_nport_put(nport);
1562: return ret;
1563: }
1564:
1565: /* success */
1566: tport = targetport->private;
1567: tport->targetport = targetport;
1568: tport->remoteport = (nport->rport) ? nport->rport->remoteport : NULL;
1569: if (nport->rport)
1570: nport->rport->targetport = targetport;
1571: tport->nport = nport;
1572: tport->lport = nport->lport;
1573: nport->tport = tport;
1574: tport->flags = 0;
1575: spin_lock_init(&tport->lock);
1576: INIT_WORK(&tport->ls_work, fcloop_tport_lsrqst_work);
1577: INIT_LIST_HEAD(&tport->ls_list);
1578:
1579: return count;
1580: }
SourceFile:drivers/nvme/target/fcloop.c]
|
| 11/2 |
2026/11/27 07:31 |
tool |
read-file |
0m
Args: map[File:drivers/nvme/target/fc.c FirstLine:1300 LineCount:100]
Results: map[Contents:1300: pe->tgtport = NULL;
1301: }
1302: tgtport->pe = NULL;
1303: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1304: }
1305:
1306: /*
1307: * called when a new targetport is registered. Looks in the
1308: * existing nvmet port_entries to see if the nvmet layer is
1309: * configured for the targetport's wwn's. (the targetport existed,
1310: * nvmet configured, the lldd unregistered the tgtport, and is now
1311: * reregistering the same targetport). If so, set the nvmet port
1312: * port entry on the targetport.
1313: */
1314: static void
1315: nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
1316: {
1317: struct nvmet_fc_port_entry *pe;
1318: unsigned long flags;
1319:
1320: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1321: list_for_each_entry(pe, &nvmet_fc_portentry_list, pe_list) {
1322: if (tgtport->fc_target_port.node_name == pe->node_name &&
1323: tgtport->fc_target_port.port_name == pe->port_name) {
1324: if (!nvmet_fc_tgtport_get(tgtport))
1325: continue;
1326:
1327: WARN_ON(pe->tgtport);
1328: tgtport->pe = pe;
1329: pe->tgtport = tgtport;
1330: break;
1331: }
1332: }
1333: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1334: }
1335:
1336: /**
1337: * nvmet_fc_register_targetport - transport entry point called by an
1338: * LLDD to register the existence of a local
1339: * NVME subsystem FC port.
1340: * @pinfo: pointer to information about the port to be registered
1341: * @template: LLDD entrypoints and operational parameters for the port
1342: * @dev: physical hardware device node port corresponds to. Will be
1343: * used for DMA mappings
1344: * @portptr: pointer to a local port pointer. Upon success, the routine
1345: * will allocate a nvme_fc_local_port structure and place its
1346: * address in the local port pointer. Upon failure, local port
1347: * pointer will be set to NULL.
1348: *
1349: * Returns:
1350: * a completion status. Must be 0 upon success; a negative errno
1351: * (ex: -ENXIO) upon failure.
1352: */
1353: int
1354: nvmet_fc_register_targetport(struct nvmet_fc_port_info *pinfo,
1355: struct nvmet_fc_target_template *template,
1356: struct device *dev,
1357: struct nvmet_fc_target_port **portptr)
1358: {
1359: struct nvmet_fc_tgtport *newrec;
1360: unsigned long flags;
1361: int ret, idx;
1362:
1363: if (!template->xmt_ls_rsp || !template->fcp_op ||
1364: !template->fcp_abort ||
1365: !template->fcp_req_release || !template->targetport_delete ||
1366: !template->max_hw_queues || !template->max_sgl_segments ||
1367: !template->max_dif_sgl_segments || !template->dma_boundary) {
1368: ret = -EINVAL;
1369: goto out_regtgt_failed;
1370: }
1371:
1372: newrec = kzalloc((sizeof(*newrec) + template->target_priv_sz),
1373: GFP_KERNEL);
1374: if (!newrec) {
1375: ret = -ENOMEM;
1376: goto out_regtgt_failed;
1377: }
1378:
1379: idx = ida_alloc(&nvmet_fc_tgtport_cnt, GFP_KERNEL);
1380: if (idx < 0) {
1381: ret = -ENOSPC;
1382: goto out_fail_kfree;
1383: }
1384:
1385: if (!get_device(dev) && dev) {
1386: ret = -ENODEV;
1387: goto out_ida_put;
1388: }
1389:
1390: newrec->fc_target_port.node_name = pinfo->node_name;
1391: newrec->fc_target_port.port_name = pinfo->port_name;
1392: if (template->target_priv_sz)
1393: newrec->fc_target_port.private = &newrec[1];
1394: else
1395: newrec->fc_target_port.private = NULL;
1396: newrec->fc_target_port.port_id = pinfo->port_id;
1397: newrec->fc_target_port.port_num = idx;
1398: INIT_LIST_HEAD(&newrec->tgt_list);
1399: newrec->dev = dev;
]
|
| 12/2 |
2026/11/27 07:31 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 14372
output: 187
thoughts: 174
Thoughts: **My Dive into NVMe/FC Target Port Management**
Alright, so I'm currently exploring the intricate dance of how the NVMe target functionality handles Fibre Channel (FC) ports. My immediate focus is on the core functions involved in establishing and managing these target ports. I've zeroed in on `fcloop_create_target_port` and `nvmet_fc_register_targetport` as crucial entry points for setting up a new target port on the FC fabric. Simultaneously, I'm also paying close attention to `nvmet_fc_portentry_rebind_tgt`, as this suggests a mechanism for re-associating or updating an existing target port entry, which is critical for dynamic fabric management and error recovery.
To get a comprehensive understanding of the lifecycle and interactions, I'm also looking into the allocation and deallocation routines. `fcloop_alloc_nport` clearly plays a role in securing the necessary resources for a new NVMe-over-FC namespace port, while `fcloop_delete_target_port` will be the counterpart for cleanup. Complementing these FC-loop specific functions, I need to examine the broader NVMe/FC target core functions. `nvmet_fc_portentry_bind` is undoubtedly key for associating the NVMe target with a specific FC fabric endpoint, and its counterpart, `nvmet_fc_unregister_targetport`, will handle the removal.
My investigative path is leading me directly to the source code. Specifically, I'll be diving deep into `drivers/nvme/target/fcloop.c`, focusing on the areas surrounding `fcloop_alloc_nport` and `fcloop_create_target_port`. This will give me the nitty-gritty details of how the FC-loop specific aspects of target port creation and management are handled. Concurrently, I'll be scrutinizing `drivers/nvme/target/fc.c`, paying particular attention to the implementations of `nvmet_fc_portentry_bind`, `nvmet_fc_register_targetport`, and `nvmet_fc_unregister_targetport`. This dual approach will allow me to see both the FC-specific layer and the overarching NVMe/FC target framework's interactions, providing me with a robust understanding of the entire process.
|
| 13/2 |
2026/11/27 07:31 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/nvme/target/fcloop.c Name:fcloop_alloc_nport]
Results: map[SourceCode:1345: static struct fcloop_nport *
1346: fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
1347: {
1348: struct fcloop_nport *newnport, *nport;
1349: struct fcloop_lport *lport;
1350: struct fcloop_ctrl_options *opts;
1351: unsigned long flags;
1352: u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS;
1353: int ret;
1354:
1355: opts = kzalloc_obj(*opts);
1356: if (!opts)
1357: return NULL;
1358:
1359: ret = fcloop_parse_options(opts, buf);
1360: if (ret)
1361: goto out_free_opts;
1362:
1363: /* everything there ? */
1364: if ((opts->mask & opts_mask) != opts_mask)
1365: goto out_free_opts;
1366:
1367: newnport = kzalloc_obj(*newnport);
1368: if (!newnport)
1369: goto out_free_opts;
1370:
1371: INIT_LIST_HEAD(&newnport->nport_list);
1372: newnport->node_name = opts->wwnn;
1373: newnport->port_name = opts->wwpn;
1374: if (opts->mask & NVMF_OPT_ROLES)
1375: newnport->port_role = opts->roles;
1376: if (opts->mask & NVMF_OPT_FCADDR)
1377: newnport->port_id = opts->fcaddr;
1378: refcount_set(&newnport->ref, 1);
1379:
1380: spin_lock_irqsave(&fcloop_lock, flags);
1381: lport = __fcloop_lport_lookup(opts->wwnn, opts->wwpn);
1382: if (lport) {
1383: /* invalid configuration */
1384: fcloop_lport_put(lport);
1385: goto out_free_newnport;
1386: }
1387:
1388: if (remoteport) {
1389: lport = __fcloop_lport_lookup(opts->lpwwnn, opts->lpwwpn);
1390: if (!lport) {
1391: /* invalid configuration */
1392: goto out_free_newnport;
1393: }
1394: }
1395:
1396: nport = __fcloop_nport_lookup(opts->wwnn, opts->wwpn);
1397: if (nport) {
1398: if ((remoteport && nport->rport) ||
1399: (!remoteport && nport->tport)) {
1400: /* invalid configuration */
1401: goto out_put_nport;
1402: }
1403:
1404: /* found existing nport, discard the new nport */
1405: kfree(newnport);
1406: } else {
1407: list_add_tail(&newnport->nport_list, &fcloop_nports);
1408: nport = newnport;
1409: }
1410:
1411: if (opts->mask & NVMF_OPT_ROLES)
1412: nport->port_role = opts->roles;
1413: if (opts->mask & NVMF_OPT_FCADDR)
1414: nport->port_id = opts->fcaddr;
1415: if (lport) {
1416: if (!nport->lport)
1417: nport->lport = lport;
1418: else
1419: fcloop_lport_put(lport);
1420: }
1421: spin_unlock_irqrestore(&fcloop_lock, flags);
1422:
1423: kfree(opts);
1424: return nport;
1425:
1426: out_put_nport:
1427: if (lport)
1428: fcloop_lport_put(lport);
1429: fcloop_nport_put(nport);
1430: out_free_newnport:
1431: spin_unlock_irqrestore(&fcloop_lock, flags);
1432: kfree(newnport);
1433: out_free_opts:
1434: kfree(opts);
1435: return NULL;
1436: }
SourceFile:drivers/nvme/target/fcloop.c]
|
| 14/2 |
2026/11/27 07:31 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/nvme/target/fcloop.c Name:fcloop_delete_target_port]
Results: map[SourceCode:1603: static ssize_t
1604: fcloop_delete_target_port(struct device *dev, struct device_attribute *attr,
1605: const char *buf, size_t count)
1606: {
1607: struct fcloop_nport *nport;
1608: struct fcloop_tport *tport;
1609: u64 nodename, portname;
1610: unsigned long flags;
1611: int ret;
1612:
1613: ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
1614: if (ret)
1615: return ret;
1616:
1617: nport = fcloop_nport_lookup(nodename, portname);
1618: if (!nport)
1619: return -ENOENT;
1620:
1621: spin_lock_irqsave(&fcloop_lock, flags);
1622: tport = __unlink_target_port(nport);
1623: spin_unlock_irqrestore(&fcloop_lock, flags);
1624:
1625: if (!tport) {
1626: ret = -ENOENT;
1627: goto out_nport_put;
1628: }
1629:
1630: ret = __targetport_unreg(nport, tport);
1631:
1632: out_nport_put:
1633: fcloop_nport_put(nport);
1634:
1635: return ret ? ret : count;
1636: }
SourceFile:drivers/nvme/target/fcloop.c]
|
| 15/2 |
2026/11/27 07:31 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/nvme/target/fc.c Name:nvmet_fc_portentry_bind]
Results: map[SourceCode:1250: static void
1251: nvmet_fc_portentry_bind(struct nvmet_fc_tgtport *tgtport,
1252: struct nvmet_fc_port_entry *pe,
1253: struct nvmet_port *port)
1254: {
1255: lockdep_assert_held(&nvmet_fc_tgtlock);
1256:
1257: nvmet_fc_tgtport_get(tgtport);
1258: pe->tgtport = tgtport;
1259: tgtport->pe = pe;
1260:
1261: pe->port = port;
1262: port->priv = pe;
1263:
1264: pe->node_name = tgtport->fc_target_port.node_name;
1265: pe->port_name = tgtport->fc_target_port.port_name;
1266: INIT_LIST_HEAD(&pe->pe_list);
1267:
1268: list_add_tail(&pe->pe_list, &nvmet_fc_portentry_list);
1269: }
SourceFile:drivers/nvme/target/fc.c]
|
| 16/2 |
2026/11/27 07:31 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/nvme/target/fc.c Name:nvmet_fc_unregister_targetport]
Results: map[SourceCode:1623: /**
1624: * nvmet_fc_unregister_targetport - transport entry point called by an
1625: * LLDD to deregister/remove a previously
1626: * registered a local NVME subsystem FC port.
1627: * @target_port: pointer to the (registered) target port that is to be
1628: * deregistered.
1629: *
1630: * Returns:
1631: * a completion status. Must be 0 upon success; a negative errno
1632: * (ex: -ENXIO) upon failure.
1633: */
1634: int
1635: nvmet_fc_unregister_targetport(struct nvmet_fc_target_port *target_port)
1636: {
1637: struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
1638: unsigned long flags;
1639:
1640: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1641: list_del(&tgtport->tgt_list);
1642: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1643:
1644: nvmet_fc_portentry_unbind_tgt(tgtport);
1645:
1646: /* terminate any outstanding associations */
1647: __nvmet_fc_free_assocs(tgtport);
1648:
1649: flush_workqueue(nvmet_wq);
1650:
1651: nvmet_fc_free_pending_reqs(tgtport);
1652: nvmet_fc_tgtport_put(tgtport);
1653:
1654: return 0;
1655: }
SourceFile:drivers/nvme/target/fc.c]
|
| 17/2 |
2026/11/27 07:31 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 17544
output: 166
thoughts: 1026
Thoughts: **My Thoughts on the FC Target Port Creation and Deletion Race Condition**
Okay, I need to really dig into what's happening in this reproducer, step-by-step. It's a race condition, and I'm trying to trace the exact sequence of events and where it breaks.
First, let's look at the "normal" flow of creating a target port.
1. **Initial Target Port Creation (`add_target_port`):** I add a target port with a specific WWNN/WWPN (`X`).
* `fcloop_alloc_nport` is called. This allocates an `nport` structure, adds it to `fcloop_nports`, but importantly, `nport->tport` is initially `NULL`.
* Then, `nvmet_fc_register_targetport` is invoked. This registers the target port object (let's call it `tgtport1`) and adds it to the `nvmet_fc_target_list`.
* Finally, `fcloop_create_target_port` is called. This is where `nport->tport` is actually set to the `tport` (which is `tgtport1` in this case).
2. **NVMe-oF Port Creation and Linking:** Next, I create an NVMe-oF port, configure it for FC, and link it to a subsystem.
* `nvmet_fc_add_port` is called. This creates a `struct nvmet_fc_port_entry` (let's call it `pe`) for the same WWNN/WWPN (`X`). It binds this `pe` to `tgtport1` and adds the `pe` to the `nvmet_fc_portentry_list`.
Now, the problem arises during deletion and a concurrent creation.
3. **Target Port Deletion (`del_target_port`):** I attempt to delete the target port with WWNN/WWPN `X`.
* `fcloop_delete_target_port` is called. This unlinks the `tport` from the `nport` by setting `nport->tport = NULL`.
* It then calls `__targetport_unreg`, which in turn calls `nvmet_fc_unregister_targetport`.
* `nvmet_fc_portentry_unbind_tgt(tgtport1)` is executed. This is crucial: it sets `pe->tgtport = NULL` and `tgtport1->pe = NULL`. **However, the `pe` itself *remains* on the `nvmet_fc_portentry_list`!**
* The `nport` structure is freed only when its reference count drops to zero. Since there's no longer an `rport` or a `tport` referencing it, it will eventually be freed.
4. **Concurrent Target Port Creation (The Race!):** This is where the real mess happens. Imagine two threads, `t1` and `t2`, both calling `add_target_port` *simultaneously* with the *same* WWNN/WWPN (`X`). This is the core of the bug.
Let's trace `fcloop_create_target_port` under this race:
* **Thread 1** enters `fcloop_alloc_nport`.
* It acquires the `fcloop_lock`.
* `__fcloop_nport_lookup` is called. At this point, the `nport` for `X` either doesn't exist yet, or it exists but its `tport` is `NULL` (because we're in the deletion path or it's a new creation). Let's assume it finds an `nport` where `nport->tport` is `NULL`.
* Thread 1 sees `!nport->tport` and releases the `fcloop_lock`.
* **Critical Observation:** The line `nport->tport = tport;` happens *after* `nvmet_fc_register_targetport` returns in `fcloop_create_target_port`. So, when Thread 1 is in `fcloop_alloc_nport`, `nport->tport` is *still* `NULL`.
* **Thread 2** enters `fcloop_alloc_nport` concurrently.
* It also acquires the `fcloop_lock`.
* `__fcloop_nport_lookup` is called. This time, it *finds* the `nport` that Thread 1 just allocated (or was in the process of allocating).
* It checks `(!remoteport && nport->tport)`. Again, `nport->tport` is *still NULL* from Thread 1's perspective because the assignment hasn't happened yet.
* Therefore, Thread 2 *also* succeeds in `fcloop_alloc_nport` and gets the *same* `nport` structure.
* Now, both Thread 1 and Thread 2 proceed to call `nvmet_fc_register_targetport` with the same node and port names. This is problematic because `nvmet_fc_register_targetport` expects a unique registration.
* **Thread 1** calls `nvmet_fc_portentry_rebind_tgt(newrec1)`.
* It finds the `pe` in `nvmet_fc_portentry_list`. At this point, `pe->tgtport` is `NULL` (from the earlier deletion).
* It successfully sets `pe->tgtport = newrec1;`.
* **Thread 2** calls `nvmet_fc_portentry_rebind_tgt(newrec2)`.
* It *also* finds the same `pe` in `nvmet_fc_portentry_list`.
* However, `pe->tgtport` is *already* set to `newrec1` by Thread 1!
* This triggers the `WARN_ON(pe->tgtport)`, indicating that we're trying to rebind an entry that's already bound.
This `WARN_ON` is a symptom, but it makes me wonder if that's the *only* issue. Is there something more insidious happening within `nvmet_fc_register_targetport` itself, or in the interaction between `fcloop_create_target_port` and `fcloop_alloc_nport` where the `nport->tport` assignment is so critically timed?
To really understand the full scope, I need to dive into the commit history. I'll be looking at `git-blame` for the key functions involved: `fcloop_create_target_port`, `fcloop_alloc_nport`, and `nvmet_fc_portentry_rebind_tgt`. I also need to review `git-log` for changes in `drivers/nvme/target/fcloop.c` and `drivers/nvme/target/fc.c` to see how this logic has evolved and if past fixes introduced new races or if this is an entirely new problem. I'm trying to pinpoint the exact code paths that allow both threads to believe they have a valid `nport` to work with when the `tport` linkage isn't yet complete.
|
| 18/2 |
2026/11/27 07:31 |
tool |
git-blame |
0m
Args: map[End:1580 File:drivers/nvme/target/fcloop.c Start:1540]
Results: map[Output:475d0fe795516 1540) static ssize_t
475d0fe795516 1541) fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
475d0fe795516 1542) const char *buf, size_t count)
475d0fe795516 1543) {
475d0fe795516 1544) struct nvmet_fc_target_port *targetport;
475d0fe795516 1545) struct fcloop_nport *nport;
475d0fe795516 1546) struct fcloop_tport *tport;
475d0fe795516 1547) struct nvmet_fc_port_info tinfo;
475d0fe795516 1548) int ret;
475d0fe795516 1549)
475d0fe795516 1550) nport = fcloop_alloc_nport(buf, count, false);
475d0fe795516 1551) if (!nport)
475d0fe795516 1552) return -EIO;
475d0fe795516 1553)
475d0fe795516 1554) tinfo.node_name = nport->node_name;
475d0fe795516 1555) tinfo.port_name = nport->port_name;
475d0fe795516 1556) tinfo.port_id = nport->port_id;
475d0fe795516 1557)
475d0fe795516 1558) ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
475d0fe795516 1559) &targetport);
475d0fe795516 1560) if (ret) {
475d0fe795516 1561) fcloop_nport_put(nport);
475d0fe795516 1562) return ret;
475d0fe795516 1563) }
475d0fe795516 1564)
475d0fe795516 1565) /* success */
475d0fe795516 1566) tport = targetport->private;
475d0fe795516 1567) tport->targetport = targetport;
475d0fe795516 1568) tport->remoteport = (nport->rport) ? nport->rport->remoteport : NULL;
475d0fe795516 1569) if (nport->rport)
475d0fe795516 1570) nport->rport->targetport = targetport;
475d0fe795516 1571) tport->nport = nport;
475d0fe795516 1572) tport->lport = nport->lport;
475d0fe795516 1573) nport->tport = tport;
2b559a3eb56b6 1574) tport->flags = 0;
437c0b824dbd0 1575) spin_lock_init(&tport->lock);
437c0b824dbd0 1576) INIT_WORK(&tport->ls_work, fcloop_tport_lsrqst_work);
437c0b824dbd0 1577) INIT_LIST_HEAD(&tport->ls_list);
475d0fe795516 1578)
7c3a23b85cac5 1579) return count;
475d0fe795516 1580) }
]
|
| 19/2 |
2026/11/27 07:31 |
tool |
git-blame |
0m
Args: map[End:1436 File:drivers/nvme/target/fcloop.c Start:1345]
Results: map[Output:475d0fe795516 1345) static struct fcloop_nport *
475d0fe795516 1346) fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
475d0fe795516 1347) {
d54a9d7f6d74e 1348) struct fcloop_nport *newnport, *nport;
d54a9d7f6d74e 1349) struct fcloop_lport *lport;
475d0fe795516 1350) struct fcloop_ctrl_options *opts;
475d0fe795516 1351) unsigned long flags;
475d0fe795516 1352) u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS;
475d0fe795516 1353) int ret;
475d0fe795516 1354)
bf4afc53b77ae 1355) opts = kzalloc_obj(*opts);
475d0fe795516 1356) if (!opts)
475d0fe795516 1357) return NULL;
475d0fe795516 1358)
475d0fe795516 1359) ret = fcloop_parse_options(opts, buf);
475d0fe795516 1360) if (ret)
475d0fe795516 1361) goto out_free_opts;
475d0fe795516 1362)
475d0fe795516 1363) /* everything there ? */
d54a9d7f6d74e 1364) if ((opts->mask & opts_mask) != opts_mask)
475d0fe795516 1365) goto out_free_opts;
475d0fe795516 1366)
bf4afc53b77ae 1367) newnport = kzalloc_obj(*newnport);
475d0fe795516 1368) if (!newnport)
475d0fe795516 1369) goto out_free_opts;
475d0fe795516 1370)
475d0fe795516 1371) INIT_LIST_HEAD(&newnport->nport_list);
475d0fe795516 1372) newnport->node_name = opts->wwnn;
475d0fe795516 1373) newnport->port_name = opts->wwpn;
475d0fe795516 1374) if (opts->mask & NVMF_OPT_ROLES)
475d0fe795516 1375) newnport->port_role = opts->roles;
475d0fe795516 1376) if (opts->mask & NVMF_OPT_FCADDR)
475d0fe795516 1377) newnport->port_id = opts->fcaddr;
f22c458f9495f 1378) refcount_set(&newnport->ref, 1);
475d0fe795516 1379)
475d0fe795516 1380) spin_lock_irqsave(&fcloop_lock, flags);
d54a9d7f6d74e 1381) lport = __fcloop_lport_lookup(opts->wwnn, opts->wwpn);
d54a9d7f6d74e 1382) if (lport) {
d54a9d7f6d74e 1383) /* invalid configuration */
d54a9d7f6d74e 1384) fcloop_lport_put(lport);
d54a9d7f6d74e 1385) goto out_free_newnport;
475d0fe795516 1386) }
475d0fe795516 1387)
475d0fe795516 1388) if (remoteport) {
d54a9d7f6d74e 1389) lport = __fcloop_lport_lookup(opts->lpwwnn, opts->lpwwpn);
d54a9d7f6d74e 1390) if (!lport) {
d54a9d7f6d74e 1391) /* invalid configuration */
475d0fe795516 1392) goto out_free_newnport;
475d0fe795516 1393) }
475d0fe795516 1394) }
475d0fe795516 1395)
d54a9d7f6d74e 1396) nport = __fcloop_nport_lookup(opts->wwnn, opts->wwpn);
d54a9d7f6d74e 1397) if (nport) {
d54a9d7f6d74e 1398) if ((remoteport && nport->rport) ||
d54a9d7f6d74e 1399) (!remoteport && nport->tport)) {
d54a9d7f6d74e 1400) /* invalid configuration */
d54a9d7f6d74e 1401) goto out_put_nport;
d54a9d7f6d74e 1402) }
d54a9d7f6d74e 1403)
d54a9d7f6d74e 1404) /* found existing nport, discard the new nport */
d54a9d7f6d74e 1405) kfree(newnport);
d54a9d7f6d74e 1406) } else {
d54a9d7f6d74e 1407) list_add_tail(&newnport->nport_list, &fcloop_nports);
d54a9d7f6d74e 1408) nport = newnport;
d54a9d7f6d74e 1409) }
475d0fe795516 1410)
d54a9d7f6d74e 1411) if (opts->mask & NVMF_OPT_ROLES)
d54a9d7f6d74e 1412) nport->port_role = opts->roles;
d54a9d7f6d74e 1413) if (opts->mask & NVMF_OPT_FCADDR)
d54a9d7f6d74e 1414) nport->port_id = opts->fcaddr;
d54a9d7f6d74e 1415) if (lport) {
d54a9d7f6d74e 1416) if (!nport->lport)
d54a9d7f6d74e 1417) nport->lport = lport;
d54a9d7f6d74e 1418) else
d54a9d7f6d74e 1419) fcloop_lport_put(lport);
d54a9d7f6d74e 1420) }
475d0fe795516 1421) spin_unlock_irqrestore(&fcloop_lock, flags);
475d0fe795516 1422)
475d0fe795516 1423) kfree(opts);
d54a9d7f6d74e 1424) return nport;
475d0fe795516 1425)
d54a9d7f6d74e 1426) out_put_nport:
d54a9d7f6d74e 1427) if (lport)
d54a9d7f6d74e 1428) fcloop_lport_put(lport);
d54a9d7f6d74e 1429) fcloop_nport_put(nport);
475d0fe795516 1430) out_free_newnport:
d54a9d7f6d74e 1431) spin_unlock_irqrestore(&fcloop_lock, flags);
475d0fe795516 1432) kfree(newnport);
475d0fe795516 1433) out_free_opts:
475d0fe795516 1434) kfree(opts);
d54a9d7f6d74e 1435) return NULL;
475d0fe795516 1436) }
]
|
| 20/2 |
2026/11/27 07:31 |
tool |
git-blame |
0m
Args: map[End:1335 File:drivers/nvme/target/fc.c Start:1306]
Results: map[Output:ea96d6496ff59 1306) /*
ea96d6496ff59 1307) * called when a new targetport is registered. Looks in the
ea96d6496ff59 1308) * existing nvmet port_entries to see if the nvmet layer is
ea96d6496ff59 1309) * configured for the targetport's wwn's. (the targetport existed,
ea96d6496ff59 1310) * nvmet configured, the lldd unregistered the tgtport, and is now
ea96d6496ff59 1311) * reregistering the same targetport). If so, set the nvmet port
ea96d6496ff59 1312) * port entry on the targetport.
ea96d6496ff59 1313) */
ea96d6496ff59 1314) static void
ea96d6496ff59 1315) nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
ea96d6496ff59 1316) {
ea96d6496ff59 1317) struct nvmet_fc_port_entry *pe;
ea96d6496ff59 1318) unsigned long flags;
ea96d6496ff59 1319)
ea96d6496ff59 1320) spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
ea96d6496ff59 1321) list_for_each_entry(pe, &nvmet_fc_portentry_list, pe_list) {
ea96d6496ff59 1322) if (tgtport->fc_target_port.node_name == pe->node_name &&
ea96d6496ff59 1323) tgtport->fc_target_port.port_name == pe->port_name) {
596cba55adb47 1324) if (!nvmet_fc_tgtport_get(tgtport))
596cba55adb47 1325) continue;
596cba55adb47 1326)
ea96d6496ff59 1327) WARN_ON(pe->tgtport);
ea96d6496ff59 1328) tgtport->pe = pe;
ea96d6496ff59 1329) pe->tgtport = tgtport;
ea96d6496ff59 1330) break;
ea96d6496ff59 1331) }
ea96d6496ff59 1332) }
ea96d6496ff59 1333) spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
ea96d6496ff59 1334) }
c53432030d864 1335)
]
|
| 21/2 |
2026/11/27 07:31 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:drivers/nvme/target/fcloop.c]
Results: map[Output:dd677d059838 nvmet-fcloop: Check remoteport port_state before calling done callback
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
86ef6f7fc741 nvmet-fcloop: remove unused lsdir member.
f9929c518de8 nvmet-fcloop: check all request and response have been processed
10c165af35d2 nvmet-fcloop: call done callback even when remote port is gone
3466b7a6b713 nvmet-fcloop: don't wait for lport cleanup
d7f7c6eb809a nvmet-fcloop: add missing fcloop_callback_host_done
84eedced1c5b nvmet-fcloop: drop response if targetport is gone
772042dd38ee nvmet-fcloop: allocate/free fcloop_lsreq directly
2b559a3eb56b nvmet-fcloop: prevent double port deletion
47a827cd7929 nvmet-fcloop: access fcpreq only when holding reqlock
88ea8f814d8d nvmet-fcloop: update refs on tfcp_req
fbaed6a810a3 nvmet-fcloop: refactor fcloop_delete_local_port
d54a9d7f6d74 nvmet-fcloop: refactor fcloop_nport_alloc and track lport
b999efc8cf41 nvmet-fcloop: remove nport from list on last user
d6c40d87e7fe nvmet-fcloop: track ref counts for nports
72511b1dc414 nvmet-fcloop: add ref counting to lport
f22c458f9495 nvmet-fcloop: replace kref with refcount
2b5f0c5bc819 nvmet-fcloop: swap list_add_tail arguments
]
|
| 22/2 |
2026/11/27 07:31 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 24068
output: 136
thoughts: 74
Thoughts: **My Investigative Process for Understanding Target Port Registration in NVMe/FC**
My initial step in unraveling this aspect of NVMe/FC was to dive directly into the commit history. I'm focusing on four specific commits: `475d0fe795516`, `d54a9d7f6d74e`, and `ea96d6496ff59`. I'm deliberately selecting these to get a granular view of the changes being introduced or modified. Beyond just the commit messages themselves, I anticipate needing to examine the diffs to understand the precise code alterations.
Simultaneously, I'm employing `git-blame` on the `nvmet_fc_register_targetport` function. This is crucial because `git-blame` will tell me, line by line, *who* last modified *which* part of this core function and in which commit. This granular attribution is essential for tracking the evolution of this specific functionality and identifying the authors responsible for particular pieces of logic.
Complementing `git-blame`, I'm also performing a `git-show` on `nvmet_fc_register_targetport`. This command will provide a more comprehensive view of the commit that last modified that function. It will include the commit message, author, date, and the full diff, offering context that `git-blame` might not fully capture on its own. My goal with these combined approaches is to build a detailed understanding of the development history of the `nvmet_fc_register_targetport` mechanism, looking for patterns, design decisions, and potential areas of complexity or improvement within its implementation.
|
| 23/2 |
2026/11/27 07:31 |
tool |
git-show |
0m
Args: map[Commit:d54a9d7f6d74e]
Results: map[Output:commit d54a9d7f6d74ee385d496f0efd282ce9d4fbfcbc
Author: Daniel Wagner <wagi@kernel.org>
Date: Wed May 7 14:22:59 2025 +0200
nvmet-fcloop: refactor fcloop_nport_alloc and track lport
The checks for a valid input values are mixed with the logic to insert a
newly allocated nport. Refactor the function so that first the checks
are done.
This allows to untangle the setup steps into a more linear form which
reduces the complexity of the functions.
Also start tracking lport when a lport is assigned to a nport. This
ensures, that the lport is not going away as long it is still referenced
by a nport.
Signed-off-by: Daniel Wagner <wagi@kernel.org>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Christoph Hellwig <hch@lst.de>
diff --git a/drivers/nvme/target/fcloop.c b/drivers/nvme/target/fcloop.c
index 7763b8bf098d..a576ad9e1dea 100644
--- a/drivers/nvme/target/fcloop.c
+++ b/drivers/nvme/target/fcloop.c
@@ -1028,6 +1028,9 @@ fcloop_nport_put(struct fcloop_nport *nport)
list_del(&nport->nport_list);
spin_unlock_irqrestore(&fcloop_lock, flags);
+ if (nport->lport)
+ fcloop_lport_put(nport->lport);
+
kfree(nport);
}
@@ -1234,6 +1237,25 @@ fcloop_nport_lookup(u64 node_name, u64 port_name)
return nport;
}
+static struct fcloop_lport *
+__fcloop_lport_lookup(u64 node_name, u64 port_name)
+{
+ struct fcloop_lport *lport;
+
+ list_for_each_entry(lport, &fcloop_lports, lport_list) {
+ if (lport->localport->node_name != node_name ||
+ lport->localport->port_name != port_name)
+ continue;
+
+ if (fcloop_lport_get(lport))
+ return lport;
+
+ break;
+ }
+
+ return NULL;
+}
+
static ssize_t
fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
@@ -1272,8 +1294,8 @@ fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
static struct fcloop_nport *
fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
{
- struct fcloop_nport *newnport, *nport = NULL;
- struct fcloop_lport *tmplport, *lport = NULL;
+ struct fcloop_nport *newnport, *nport;
+ struct fcloop_lport *lport;
struct fcloop_ctrl_options *opts;
unsigned long flags;
u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS;
@@ -1288,10 +1310,8 @@ fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
goto out_free_opts;
/* everything there ? */
- if ((opts->mask & opts_mask) != opts_mask) {
- ret = -EINVAL;
+ if ((opts->mask & opts_mask) != opts_mask)
goto out_free_opts;
- }
newnport = kzalloc(sizeof(*newnport), GFP_KERNEL);
if (!newnport)
@@ -1307,60 +1327,61 @@ fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
refcount_set(&newnport->ref, 1);
spin_lock_irqsave(&fcloop_lock, flags);
-
- list_for_each_entry(tmplport, &fcloop_lports, lport_list) {
- if (tmplport->localport->node_name == opts->wwnn &&
- tmplport->localport->port_name == opts->wwpn)
- goto out_invalid_opts;
-
- if (tmplport->localport->node_name == opts->lpwwnn &&
- tmplport->localport->port_name == opts->lpwwpn)
- lport = tmplport;
+ lport = __fcloop_lport_lookup(opts->wwnn, opts->wwpn);
+ if (lport) {
+ /* invalid configuration */
+ fcloop_lport_put(lport);
+ goto out_free_newnport;
}
if (remoteport) {
- if (!lport)
- goto out_invalid_opts;
- newnport->lport = lport;
- }
-
- list_for_each_entry(nport, &fcloop_nports, nport_list) {
- if (nport->node_name == opts->wwnn &&
- nport->port_name == opts->wwpn) {
- if ((remoteport && nport->rport) ||
- (!remoteport && nport->tport)) {
- nport = NULL;
- goto out_invalid_opts;
- }
-
- fcloop_nport_get(nport);
-
- spin_unlock_irqrestore(&fcloop_lock, flags);
-
- if (remoteport)
- nport->lport = lport;
- if (opts->mask & NVMF_OPT_ROLES)
- nport->port_role = opts->roles;
- if (opts->mask & NVMF_OPT_FCADDR)
- nport->port_id = opts->fcaddr;
+ lport = __fcloop_lport_lookup(opts->lpwwnn, opts->lpwwpn);
+ if (!lport) {
+ /* invalid configuration */
goto out_free_newnport;
}
}
- list_add_tail(&newnport->nport_list, &fcloop_nports);
+ nport = __fcloop_nport_lookup(opts->wwnn, opts->wwpn);
+ if (nport) {
+ if ((remoteport && nport->rport) ||
+ (!remoteport && nport->tport)) {
+ /* invalid configuration */
+ goto out_put_nport;
+ }
+ /* found existing nport, discard the new nport */
+ kfree(newnport);
+ } else {
+ list_add_tail(&newnport->nport_list, &fcloop_nports);
+ nport = newnport;
+ }
+
+ if (opts->mask & NVMF_OPT_ROLES)
+ nport->port_role = opts->roles;
+ if (opts->mask & NVMF_OPT_FCADDR)
+ nport->port_id = opts->fcaddr;
+ if (lport) {
+ if (!nport->lport)
+ nport->lport = lport;
+ else
+ fcloop_lport_put(lport);
+ }
spin_unlock_irqrestore(&fcloop_lock, flags);
kfree(opts);
- return newnport;
+ return nport;
-out_invalid_opts:
- spin_unlock_irqrestore(&fcloop_lock, flags);
+out_put_nport:
+ if (lport)
+ fcloop_lport_put(lport);
+ fcloop_nport_put(nport);
out_free_newnport:
+ spin_unlock_irqrestore(&fcloop_lock, flags);
kfree(newnport);
out_free_opts:
kfree(opts);
- return nport;
+ return NULL;
}
static ssize_t
]
|
| 24/2 |
2026/11/27 07:31 |
tool |
git-show |
0m
Args: map[Commit:ea96d6496ff59]
Results: map[Output:commit ea96d6496ff59b2b26dc9e13dc8f57d77731eb37
Author: James Smart <jsmart2021@gmail.com>
Date: Thu Aug 9 16:48:14 2018 -0700
nvmet_fc: support target port removal with nvmet layer
Currently, if a targetport has been connected to via the nvmet config
(in other words, the add_port() transport routine called, and the nvmet
port pointer stored for using in upcalls on new io), and if the
targetport is then removed (say the lldd driver decides to unload or
fully reset its hardware) and then re-added (the lldd driver reloads or
reinits its hardware), the port pointer has been lost so there's no way
to continue to post commands up to nvmet via the transport port.
Correct by allocating a small "port context" structure that will be
linked to by the targetport. The context will save the targetport WWN's
and the nvmet port pointer to use for it. Initial allocation will occur
when the targetport is bound to via add_port. The context will be
deallocated when remove_port() is called. If a targetport is removed
while nvmet has the active port context, the targetport will be unlinked
from the port context before removal. If a new targetport is registered,
the port contexts without a binding are looked through and if the WWN's
match (so it's the same as nvmet's port context) the port context is
linked to the new target port. Thus new io can be received on the new
targetport and operation resumes with nvmet.
Additionally, this also resolves nvmet configuration changing out from
underneath of the nvme-fc target port (for example: a nvmetcli clear).
Signed-off-by: James Smart <james.smart@broadcom.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
diff --git a/drivers/nvme/target/fc.c b/drivers/nvme/target/fc.c
index a3905673e17f..ef286b72d958 100644
--- a/drivers/nvme/target/fc.c
+++ b/drivers/nvme/target/fc.c
@@ -110,11 +110,19 @@ struct nvmet_fc_tgtport {
struct list_head ls_busylist;
struct list_head assoc_list;
struct ida assoc_cnt;
- struct nvmet_port *port;
+ struct nvmet_fc_port_entry *pe;
struct kref ref;
u32 max_sg_cnt;
};
+struct nvmet_fc_port_entry {
+ struct nvmet_fc_tgtport *tgtport;
+ struct nvmet_port *port;
+ u64 node_name;
+ u64 port_name;
+ struct list_head pe_list;
+};
+
struct nvmet_fc_defer_fcp_req {
struct list_head req_list;
struct nvmefc_tgt_fcp_req *fcp_req;
@@ -132,7 +140,6 @@ struct nvmet_fc_tgt_queue {
atomic_t zrspcnt;
atomic_t rsn;
spinlock_t qlock;
- struct nvmet_port *port;
struct nvmet_cq nvme_cq;
struct nvmet_sq nvme_sq;
struct nvmet_fc_tgt_assoc *assoc;
@@ -221,6 +228,7 @@ static DEFINE_SPINLOCK(nvmet_fc_tgtlock);
static LIST_HEAD(nvmet_fc_target_list);
static DEFINE_IDA(nvmet_fc_tgtport_cnt);
+static LIST_HEAD(nvmet_fc_portentry_list);
static void nvmet_fc_handle_ls_rqst_work(struct work_struct *work);
@@ -645,7 +653,6 @@ nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
queue->qid = qid;
queue->sqsize = sqsize;
queue->assoc = assoc;
- queue->port = assoc->tgtport->port;
queue->cpu = nvmet_fc_queue_to_cpu(assoc->tgtport, qid);
INIT_LIST_HEAD(&queue->fod_list);
INIT_LIST_HEAD(&queue->avail_defer_list);
@@ -957,6 +964,83 @@ nvmet_fc_find_target_assoc(struct nvmet_fc_tgtport *tgtport,
return ret;
}
+static void
+nvmet_fc_portentry_bind(struct nvmet_fc_tgtport *tgtport,
+ struct nvmet_fc_port_entry *pe,
+ struct nvmet_port *port)
+{
+ lockdep_assert_held(&nvmet_fc_tgtlock);
+
+ pe->tgtport = tgtport;
+ tgtport->pe = pe;
+
+ pe->port = port;
+ port->priv = pe;
+
+ pe->node_name = tgtport->fc_target_port.node_name;
+ pe->port_name = tgtport->fc_target_port.port_name;
+ INIT_LIST_HEAD(&pe->pe_list);
+
+ list_add_tail(&pe->pe_list, &nvmet_fc_portentry_list);
+}
+
+static void
+nvmet_fc_portentry_unbind(struct nvmet_fc_port_entry *pe)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
+ if (pe->tgtport)
+ pe->tgtport->pe = NULL;
+ list_del(&pe->pe_list);
+ spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
+}
+
+/*
+ * called when a targetport deregisters. Breaks the relationship
+ * with the nvmet port, but leaves the port_entry in place so that
+ * re-registration can resume operation.
+ */
+static void
+nvmet_fc_portentry_unbind_tgt(struct nvmet_fc_tgtport *tgtport)
+{
+ struct nvmet_fc_port_entry *pe;
+ unsigned long flags;
+
+ spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
+ pe = tgtport->pe;
+ if (pe)
+ pe->tgtport = NULL;
+ tgtport->pe = NULL;
+ spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
+}
+
+/*
+ * called when a new targetport is registered. Looks in the
+ * existing nvmet port_entries to see if the nvmet layer is
+ * configured for the targetport's wwn's. (the targetport existed,
+ * nvmet configured, the lldd unregistered the tgtport, and is now
+ * reregistering the same targetport). If so, set the nvmet port
+ * port entry on the targetport.
+ */
+static void
+nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
+{
+ struct nvmet_fc_port_entry *pe;
+ unsigned long flags;
+
+ spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
+ list_for_each_entry(pe, &nvmet_fc_portentry_list, pe_list) {
+ if (tgtport->fc_target_port.node_name == pe->node_name &&
+ tgtport->fc_target_port.port_name == pe->port_name) {
+ WARN_ON(pe->tgtport);
+ tgtport->pe = pe;
+ pe->tgtport = tgtport;
+ break;
+ }
+ }
+ spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
+}
/**
* nvme_fc_register_targetport - transport entry point called by an
@@ -1034,6 +1118,8 @@ nvmet_fc_register_targetport(struct nvmet_fc_port_info *pinfo,
goto out_free_newrec;
}
+ nvmet_fc_portentry_rebind_tgt(newrec);
+
spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
list_add_tail(&newrec->tgt_list, &nvmet_fc_target_list);
spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
@@ -1171,6 +1257,8 @@ nvmet_fc_unregister_targetport(struct nvmet_fc_target_port *target_port)
{
struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
+ nvmet_fc_portentry_unbind_tgt(tgtport);
+
/* terminate any outstanding associations */
__nvmet_fc_free_assocs(tgtport);
@@ -2147,7 +2235,7 @@ nvmet_fc_fcp_nvme_cmd_done(struct nvmet_req *nvme_req)
/*
- * Actual processing routine for received FC-NVME LS Requests from the LLD
+ * Actual processing routine for received FC-NVME I/O Requests from the LLD
*/
static void
nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
@@ -2157,6 +2245,13 @@ nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
u32 xfrlen = be32_to_cpu(cmdiu->data_len);
int ret;
+ /*
+ * if there is no nvmet mapping to the targetport there
+ * shouldn't be requests. just terminate them.
+ */
+ if (!tgtport->pe)
+ goto transport_error;
+
/*
* Fused commands are currently not supported in the linux
* implementation.
@@ -2184,7 +2279,7 @@ nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
fod->req.cmd = &fod->cmdiubuf.sqe;
fod->req.rsp = &fod->rspiubuf.cqe;
- fod->req.port = fod->queue->port;
+ fod->req.port = tgtport->pe->port;
/* clear any response payload */
memset(&fod->rspiubuf, 0, sizeof(fod->rspiubuf));
@@ -2508,6 +2603,7 @@ static int
nvmet_fc_add_port(struct nvmet_port *port)
{
struct nvmet_fc_tgtport *tgtport;
+ struct nvmet_fc_port_entry *pe;
struct nvmet_fc_traddr traddr = { 0L, 0L };
unsigned long flags;
int ret;
@@ -2524,24 +2620,40 @@ nvmet_fc_add_port(struct nvmet_port *port)
if (ret)
return ret;
+ pe = kzalloc(sizeof(*pe), GFP_KERNEL);
+ if (!pe)
+ return -ENOMEM;
+
ret = -ENXIO;
spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
list_for_each_entry(tgtport, &nvmet_fc_target_list, tgt_list) {
if ((tgtport->fc_target_port.node_name == traddr.nn) &&
(tgtport->fc_target_port.port_name == traddr.pn)) {
- tgtport->port = port;
- ret = 0;
+ /* a FC port can only be 1 nvmet port id */
+ if (!tgtport->pe) {
+ nvmet_fc_portentry_bind(tgtport, pe, port);
+ ret = 0;
+ } else
+ ret = -EALREADY;
break;
}
}
spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
+
+ if (ret)
+ kfree(pe);
+
return ret;
}
static void
nvmet_fc_remove_port(struct nvmet_port *port)
{
- /* nothing to do */
+ struct nvmet_fc_port_entry *pe = port->priv;
+
+ nvmet_fc_portentry_unbind(pe);
+
+ kfree(pe);
}
static const struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = {
]
|
| 25/2 |
2026/11/27 07:31 |
tool |
git-show |
0m
Args: map[Commit:475d0fe795516 Stat:true]
Results: map[Output:commit 475d0fe795516a9b9f286a851c3972fd8831c643
Author: James Smart <jsmart2021@gmail.com>
Date: Fri Dec 2 00:28:44 2016 -0800
nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME
Add FC LLDD loopback driver to test FC host and target transport within
nvme-fabrics
To aid in the development and testing of the lower-level api of the FC
transport, this loopback driver has been created to act as if it were a
FC hba driver supporting both the host interfaces as well as the target
interfaces with the nvme FC transport.
Signed-off-by: James Smart <james.smart@broadcom.com>
Reviewed-by: Jay Freyensee <james_p_freyensee@linux.intel.com>
Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: Christoph Hellwig <hch@lst.de>
MAINTAINERS | 1 +
drivers/nvme/target/Kconfig | 13 +
drivers/nvme/target/Makefile | 2 +
drivers/nvme/target/fcloop.c | 1148 ++++++++++++++++++++++++++++++++++++++++++
4 files changed, 1164 insertions(+)
]
|
| 26/2 |
2026/11/27 07:31 |
tool |
git-blame |
0m
Args: map[End:1435 File:drivers/nvme/target/fc.c Start:1353]
Results: map[Output:c53432030d864 1353) int
c53432030d864 1354) nvmet_fc_register_targetport(struct nvmet_fc_port_info *pinfo,
c53432030d864 1355) struct nvmet_fc_target_template *template,
c53432030d864 1356) struct device *dev,
c53432030d864 1357) struct nvmet_fc_target_port **portptr)
c53432030d864 1358) {
c53432030d864 1359) struct nvmet_fc_tgtport *newrec;
c53432030d864 1360) unsigned long flags;
c53432030d864 1361) int ret, idx;
c53432030d864 1362)
c53432030d864 1363) if (!template->xmt_ls_rsp || !template->fcp_op ||
a97ec51b37efa 1364) !template->fcp_abort ||
19b58d9473e8e 1365) !template->fcp_req_release || !template->targetport_delete ||
c53432030d864 1366) !template->max_hw_queues || !template->max_sgl_segments ||
c53432030d864 1367) !template->max_dif_sgl_segments || !template->dma_boundary) {
c53432030d864 1368) ret = -EINVAL;
c53432030d864 1369) goto out_regtgt_failed;
c53432030d864 1370) }
c53432030d864 1371)
c53432030d864 1372) newrec = kzalloc((sizeof(*newrec) + template->target_priv_sz),
c53432030d864 1373) GFP_KERNEL);
c53432030d864 1374) if (!newrec) {
c53432030d864 1375) ret = -ENOMEM;
c53432030d864 1376) goto out_regtgt_failed;
c53432030d864 1377) }
c53432030d864 1378)
6dd0f465d57c4 1379) idx = ida_alloc(&nvmet_fc_tgtport_cnt, GFP_KERNEL);
c53432030d864 1380) if (idx < 0) {
c53432030d864 1381) ret = -ENOSPC;
c53432030d864 1382) goto out_fail_kfree;
c53432030d864 1383) }
c53432030d864 1384)
c53432030d864 1385) if (!get_device(dev) && dev) {
c53432030d864 1386) ret = -ENODEV;
c53432030d864 1387) goto out_ida_put;
c53432030d864 1388) }
c53432030d864 1389)
c53432030d864 1390) newrec->fc_target_port.node_name = pinfo->node_name;
c53432030d864 1391) newrec->fc_target_port.port_name = pinfo->port_name;
f56bf76f79f3d 1392) if (template->target_priv_sz)
f56bf76f79f3d 1393) newrec->fc_target_port.private = &newrec[1];
f56bf76f79f3d 1394) else
f56bf76f79f3d 1395) newrec->fc_target_port.private = NULL;
c53432030d864 1396) newrec->fc_target_port.port_id = pinfo->port_id;
c53432030d864 1397) newrec->fc_target_port.port_num = idx;
c53432030d864 1398) INIT_LIST_HEAD(&newrec->tgt_list);
c53432030d864 1399) newrec->dev = dev;
c53432030d864 1400) newrec->ops = template;
c53432030d864 1401) spin_lock_init(&newrec->lock);
349c694ee71ce 1402) INIT_LIST_HEAD(&newrec->ls_rcv_list);
47bf324106449 1403) INIT_LIST_HEAD(&newrec->ls_req_list);
c53432030d864 1404) INIT_LIST_HEAD(&newrec->ls_busylist);
c53432030d864 1405) INIT_LIST_HEAD(&newrec->assoc_list);
58ab8ff9dca21 1406) INIT_LIST_HEAD(&newrec->host_list);
c53432030d864 1407) kref_init(&newrec->ref);
c53432030d864 1408) ida_init(&newrec->assoc_cnt);
d082dc1562a2f 1409) newrec->max_sg_cnt = template->max_sgl_segments;
c53432030d864 1410)
c53432030d864 1411) ret = nvmet_fc_alloc_ls_iodlist(newrec);
c53432030d864 1412) if (ret) {
c53432030d864 1413) ret = -ENOMEM;
c53432030d864 1414) goto out_free_newrec;
c53432030d864 1415) }
c53432030d864 1416)
ea96d6496ff59 1417) nvmet_fc_portentry_rebind_tgt(newrec);
ea96d6496ff59 1418)
c53432030d864 1419) spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
c53432030d864 1420) list_add_tail(&newrec->tgt_list, &nvmet_fc_target_list);
c53432030d864 1421) spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
c53432030d864 1422)
c53432030d864 1423) *portptr = &newrec->fc_target_port;
c53432030d864 1424) return 0;
c53432030d864 1425)
c53432030d864 1426) out_free_newrec:
c53432030d864 1427) put_device(dev);
c53432030d864 1428) out_ida_put:
6dd0f465d57c4 1429) ida_free(&nvmet_fc_tgtport_cnt, idx);
c53432030d864 1430) out_fail_kfree:
c53432030d864 1431) kfree(newrec);
c53432030d864 1432) out_regtgt_failed:
c53432030d864 1433) *portptr = NULL;
c53432030d864 1434) return ret;
c53432030d864 1435) }
]
|
| 27/2 |
2026/11/27 07:31 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 32789
output: 166
thoughts: 50
Thoughts: **My Internal Monologue on NVMe-oF Target Port Registration**
Alright, I need to conduct a thorough examination of the NVMe-oF target port registration mechanisms within the kernel. Specifically, I'm going to dive into the functionalities provided by `fcloop_create_remote_port` and `fcloop_create_local_port`. These functions are clearly central to how Fibre Channel targets are managed, and understanding their intricacies is crucial.
But that's not enough. To get a complete picture and ensure no corner is left unexamined, I must also meticulously audit *all* the call sites for `nvmet_fc_register_targetport` across the entire kernel codebase. This broad sweep is essential to identify any and all paths that lead to the registration of an NVMe-oF target port. By doing so, I can be confident that I'm capturing every interaction, every use case, and potentially uncover any unexpected dependencies or edge cases. This comprehensive approach will allow me to fully grasp the lifecycle and integration of NVMe-oF target ports within the kernel's Fibre Channel subsystem.
|
| 28/2 |
2026/11/27 07:31 |
tool |
codesearch-find-references |
0m
Args: map[IncludeSnippetLines:5 Name:nvmet_fc_register_targetport]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:fcloop_create_target_port SourceFile:drivers/nvme/target/fcloop.c SourceLine:1558 SourceSnippet:1553:
1554: tinfo.node_name = nport->node_name;
1555: tinfo.port_name = nport->port_name;
1556: tinfo.port_id = nport->port_id;
1557:
1558: ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
1559: &targetport);
1560: if (ret) {
1561: fcloop_nport_put(nport);
1562: return ret;
1563: }
] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__UNIQUE_ID_addressable_nvmet_fc_register_targetport_920 SourceFile:drivers/nvme/target/fc.c SourceLine:1436 SourceSnippet:1436: EXPORT_SYMBOL_GPL(nvmet_fc_register_targetport);
] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_nvmet_fc_register_targetport SourceFile:drivers/nvme/target/fc.c SourceLine:1436 SourceSnippet:1436: EXPORT_SYMBOL_GPL(nvmet_fc_register_targetport);
] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_nvmet_fc_register_targetport SourceFile:drivers/nvme/target/fc.c SourceLine:1436 SourceSnippet:1436: EXPORT_SYMBOL_GPL(nvmet_fc_register_targetport);
]] TruncatedOutput:false]
|
| 29/2 |
2026/11/27 07:31 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/nvme/target/fcloop.c Name:fcloop_create_remote_port]
Results: map[SourceCode:1438: static ssize_t
1439: fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
1440: const char *buf, size_t count)
1441: {
1442: struct nvme_fc_remote_port *remoteport;
1443: struct fcloop_nport *nport;
1444: struct fcloop_rport *rport;
1445: struct nvme_fc_port_info pinfo;
1446: int ret;
1447:
1448: nport = fcloop_alloc_nport(buf, count, true);
1449: if (!nport)
1450: return -EIO;
1451:
1452: memset(&pinfo, 0, sizeof(pinfo));
1453: pinfo.node_name = nport->node_name;
1454: pinfo.port_name = nport->port_name;
1455: pinfo.port_role = nport->port_role;
1456: pinfo.port_id = nport->port_id;
1457:
1458: ret = nvme_fc_register_remoteport(nport->lport->localport,
1459: &pinfo, &remoteport);
1460: if (ret || !remoteport) {
1461: fcloop_nport_put(nport);
1462: return ret;
1463: }
1464:
1465: /* success */
1466: rport = remoteport->private;
1467: rport->remoteport = remoteport;
1468: rport->targetport = (nport->tport) ? nport->tport->targetport : NULL;
1469: if (nport->tport) {
1470: nport->tport->remoteport = remoteport;
1471: nport->tport->lport = nport->lport;
1472: }
1473: rport->nport = nport;
1474: rport->lport = nport->lport;
1475: nport->rport = rport;
1476: rport->flags = 0;
1477: spin_lock_init(&rport->lock);
1478: INIT_WORK(&rport->ls_work, fcloop_rport_lsrqst_work);
1479: INIT_LIST_HEAD(&rport->ls_list);
1480:
1481: return count;
1482: }
SourceFile:drivers/nvme/target/fcloop.c]
|
| 30/2 |
2026/11/27 07:31 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/nvme/target/fcloop.c Name:fcloop_create_local_port]
Results: map[SourceCode:1192: static ssize_t
1193: fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
1194: const char *buf, size_t count)
1195: {
1196: struct nvme_fc_port_info pinfo;
1197: struct fcloop_ctrl_options *opts;
1198: struct nvme_fc_local_port *localport;
1199: struct fcloop_lport *lport;
1200: struct fcloop_lport_priv *lport_priv;
1201: unsigned long flags;
1202: int ret = -ENOMEM;
1203:
1204: lport = kzalloc_obj(*lport);
1205: if (!lport)
1206: return -ENOMEM;
1207:
1208: opts = kzalloc_obj(*opts);
1209: if (!opts)
1210: goto out_free_lport;
1211:
1212: ret = fcloop_parse_options(opts, buf);
1213: if (ret)
1214: goto out_free_opts;
1215:
1216: /* everything there ? */
1217: if ((opts->mask & LPORT_OPTS) != LPORT_OPTS) {
1218: ret = -EINVAL;
1219: goto out_free_opts;
1220: }
1221:
1222: memset(&pinfo, 0, sizeof(pinfo));
1223: pinfo.node_name = opts->wwnn;
1224: pinfo.port_name = opts->wwpn;
1225: pinfo.port_role = opts->roles;
1226: pinfo.port_id = opts->fcaddr;
1227:
1228: ret = nvme_fc_register_localport(&pinfo, &fctemplate, NULL, &localport);
1229: if (!ret) {
1230: /* success */
1231: lport_priv = localport->private;
1232: lport_priv->lport = lport;
1233:
1234: lport->localport = localport;
1235: INIT_LIST_HEAD(&lport->lport_list);
1236: refcount_set(&lport->ref, 1);
1237:
1238: spin_lock_irqsave(&fcloop_lock, flags);
1239: list_add_tail(&lport->lport_list, &fcloop_lports);
1240: spin_unlock_irqrestore(&fcloop_lock, flags);
1241: }
1242:
1243: out_free_opts:
1244: kfree(opts);
1245: out_free_lport:
1246: /* free only if we're going to fail */
1247: if (ret)
1248: kfree(lport);
1249:
1250: return ret ? ret : count;
1251: }
SourceFile:drivers/nvme/target/fcloop.c]
|
| 31/2 |
2026/11/27 07:32 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:drivers/nvme/target/fc.c]
Results: map[Output:ba98d6796d12 nvmet-fc: fix invalid free in LS IOD error path
12f5fb5ee124 nvmet-fc: add WQ_PERCPU to alloc_workqueue users
32a92f8c8932 Convert more 'alloc_obj' cases to default GFP_KERNEL arguments
323bbfcf1ef8 Convert 'alloc_flex' family to use the new default GFP_KERNEL argument
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
57413f0899fa nvmet-fc: use pr_* print macros instead of dev_*
f2537be4f842 nvmet-fc: avoid scheduling association deletion twice
db5a5406fb7e nvmet-fc: move lsop put work to nvmet_fc_ls_req_op
367c240b0a99 nvme: fix various comment typos
44e479d72020 nvme: spelling fixes
596cba55adb4 nvmet-fc: take tgtport refs for portentry
bbccbf791e6f nvmet-fc: free pending reqs on tgtport unregister
87b4d5ec0dca nvmet: simplify the nvmet_req_init() interface
94ee8708c91f nvmet: support completion queue sharing
bb78836b3a7c nvmet: fabrics: add CQ init and destroy
70289ae5cac4 nvmet-fc: put ref when assoc->del_work is already scheduled
b0b26ad0e194 nvmet-fc: take tgtport reference only once
1a909565733e nvmet-fc: update tgtport ref per assoc
88517565b592 nvmet-fc: inline nvmet_fc_free_hostport
]
|
| 32/2 |
2026/11/27 07:32 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 35755
output: 111
thoughts: 72
Thoughts: **Examining Recent NVMe-FC and fcloop Developments**
My current line of inquiry is focused on understanding the recent modifications pertaining to `fcloop` and `nvmet-fc`. To achieve this, I'm meticulously reviewing the changes introduced in commits `596cba55adb4`, `2b559a3eb56b`, `b999efc8cf41`, and `d6c40d87e7fe`. My aim is to gain a comprehensive grasp of how these components have evolved, likely in anticipation of or as a result of specific performance enhancements, bug fixes, or feature additions within the NVMe over Fibre Channel subsystem. I expect to find adjustments that address interoperability, efficiency, or perhaps the integration of new Fibre Channel capabilities.
|
| 33/2 |
2026/11/27 07:32 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 596cba55adb473f26bfd3f6dd78f169810069531
Author: Daniel Wagner <wagi@kernel.org>
Date: Wed May 7 14:23:09 2025 +0200
nvmet-fc: take tgtport refs for portentry
Ensure that the tgtport is not going away as long portentry has a
pointer on it.
Signed-off-by: Daniel Wagner <wagi@kernel.org>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Christoph Hellwig <hch@lst.de>
diff --git a/drivers/nvme/target/fc.c b/drivers/nvme/target/fc.c
index b8985559d1f1..254537b93e63 100644
--- a/drivers/nvme/target/fc.c
+++ b/drivers/nvme/target/fc.c
@@ -1257,6 +1257,7 @@ nvmet_fc_portentry_bind(struct nvmet_fc_tgtport *tgtport,
{
lockdep_assert_held(&nvmet_fc_tgtlock);
+ nvmet_fc_tgtport_get(tgtport);
pe->tgtport = tgtport;
tgtport->pe = pe;
@@ -1276,8 +1277,10 @@ nvmet_fc_portentry_unbind(struct nvmet_fc_port_entry *pe)
unsigned long flags;
spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
- if (pe->tgtport)
+ if (pe->tgtport) {
+ nvmet_fc_tgtport_put(pe->tgtport);
pe->tgtport->pe = NULL;
+ }
list_del(&pe->pe_list);
spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
}
@@ -1295,8 +1298,10 @@ nvmet_fc_portentry_unbind_tgt(struct nvmet_fc_tgtport *tgtport)
spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
pe = tgtport->pe;
- if (pe)
+ if (pe) {
+ nvmet_fc_tgtport_put(pe->tgtport);
pe->tgtport = NULL;
+ }
tgtport->pe = NULL;
spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
}
@@ -1319,6 +1324,9 @@ nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
list_for_each_entry(pe, &nvmet_fc_portentry_list, pe_list) {
if (tgtport->fc_target_port.node_name == pe->node_name &&
tgtport->fc_target_port.port_name == pe->port_name) {
+ if (!nvmet_fc_tgtport_get(tgtport))
+ continue;
+
WARN_ON(pe->tgtport);
tgtport->pe = pe;
pe->tgtport = tgtport;
@@ -2888,12 +2896,17 @@ nvmet_fc_add_port(struct nvmet_port *port)
list_for_each_entry(tgtport, &nvmet_fc_target_list, tgt_list) {
if ((tgtport->fc_target_port.node_name == traddr.nn) &&
(tgtport->fc_target_port.port_name == traddr.pn)) {
+ if (!nvmet_fc_tgtport_get(tgtport))
+ continue;
+
/* a FC port can only be 1 nvmet port id */
if (!tgtport->pe) {
nvmet_fc_portentry_bind(tgtport, pe, port);
ret = 0;
} else
ret = -EALREADY;
+
+ nvmet_fc_tgtport_put(tgtport);
break;
}
}
@@ -2909,11 +2922,21 @@ static void
nvmet_fc_remove_port(struct nvmet_port *port)
{
struct nvmet_fc_port_entry *pe = port->priv;
+ struct nvmet_fc_tgtport *tgtport = NULL;
+ unsigned long flags;
+
+ spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
+ if (pe->tgtport && nvmet_fc_tgtport_get(pe->tgtport))
+ tgtport = pe->tgtport;
+ spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
nvmet_fc_portentry_unbind(pe);
- /* terminate any outstanding associations */
- __nvmet_fc_free_assocs(pe->tgtport);
+ if (tgtport) {
+ /* terminate any outstanding associations */
+ __nvmet_fc_free_assocs(tgtport);
+ nvmet_fc_tgtport_put(tgtport);
+ }
kfree(pe);
}
@@ -2922,10 +2945,21 @@ static void
nvmet_fc_discovery_chg(struct nvmet_port *port)
{
struct nvmet_fc_port_entry *pe = port->priv;
- struct nvmet_fc_tgtport *tgtport = pe->tgtport;
+ struct nvmet_fc_tgtport *tgtport = NULL;
+ unsigned long flags;
+
+ spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
+ if (pe->tgtport && nvmet_fc_tgtport_get(pe->tgtport))
+ tgtport = pe->tgtport;
+ spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
+
+ if (!tgtport)
+ return;
if (tgtport && tgtport->ops->discovery_event)
tgtport->ops->discovery_event(&tgtport->fc_target_port);
+
+ nvmet_fc_tgtport_put(tgtport);
}
static ssize_t
]
|
| 34/2 |
2026/11/27 07:32 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 2b559a3eb56b6e1a51ca6f4a17778a1b4e14a591
Author: Daniel Wagner <wagi@kernel.org>
Date: Wed May 7 14:23:04 2025 +0200
nvmet-fcloop: prevent double port deletion
The delete callback can be called either via the unregister function or
from the transport directly. Thus it is necessary ensure resources are
not freed multiple times.
Signed-off-by: Daniel Wagner <wagi@kernel.org>
Signed-off-by: Christoph Hellwig <hch@lst.de>
diff --git a/drivers/nvme/target/fcloop.c b/drivers/nvme/target/fcloop.c
index f0c0d95b8dde..52f0061920a8 100644
--- a/drivers/nvme/target/fcloop.c
+++ b/drivers/nvme/target/fcloop.c
@@ -215,6 +215,9 @@ struct fcloop_lport_priv {
struct fcloop_lport *lport;
};
+/* The port is already being removed, avoid double free */
+#define PORT_DELETED 0
+
struct fcloop_rport {
struct nvme_fc_remote_port *remoteport;
struct nvmet_fc_target_port *targetport;
@@ -223,6 +226,7 @@ struct fcloop_rport {
spinlock_t lock;
struct list_head ls_list;
struct work_struct ls_work;
+ unsigned long flags;
};
struct fcloop_tport {
@@ -233,6 +237,7 @@ struct fcloop_tport {
spinlock_t lock;
struct list_head ls_list;
struct work_struct ls_work;
+ unsigned long flags;
};
struct fcloop_nport {
@@ -1061,30 +1066,38 @@ static void
fcloop_remoteport_delete(struct nvme_fc_remote_port *remoteport)
{
struct fcloop_rport *rport = remoteport->private;
+ bool put_port = false;
unsigned long flags;
flush_work(&rport->ls_work);
spin_lock_irqsave(&fcloop_lock, flags);
+ if (!test_and_set_bit(PORT_DELETED, &rport->flags))
+ put_port = true;
rport->nport->rport = NULL;
spin_unlock_irqrestore(&fcloop_lock, flags);
- fcloop_nport_put(rport->nport);
+ if (put_port)
+ fcloop_nport_put(rport->nport);
}
static void
fcloop_targetport_delete(struct nvmet_fc_target_port *targetport)
{
struct fcloop_tport *tport = targetport->private;
+ bool put_port = false;
unsigned long flags;
flush_work(&tport->ls_work);
spin_lock_irqsave(&fcloop_lock, flags);
+ if (!test_and_set_bit(PORT_DELETED, &tport->flags))
+ put_port = true;
tport->nport->tport = NULL;
spin_unlock_irqrestore(&fcloop_lock, flags);
- fcloop_nport_put(tport->nport);
+ if (put_port)
+ fcloop_nport_put(tport->nport);
}
#define FCLOOP_HW_QUEUES 4
@@ -1427,6 +1440,7 @@ fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
rport->nport = nport;
rport->lport = nport->lport;
nport->rport = rport;
+ rport->flags = 0;
spin_lock_init(&rport->lock);
INIT_WORK(&rport->ls_work, fcloop_rport_lsrqst_work);
INIT_LIST_HEAD(&rport->ls_list);
@@ -1524,6 +1538,7 @@ fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
tport->nport = nport;
tport->lport = nport->lport;
nport->tport = tport;
+ tport->flags = 0;
spin_lock_init(&tport->lock);
INIT_WORK(&tport->ls_work, fcloop_tport_lsrqst_work);
INIT_LIST_HEAD(&tport->ls_list);
]
|
| 35/2 |
2026/11/27 07:32 |
tool |
git-show |
0m
Args:
Results: map[Output:commit b999efc8cf41420f9d9f8b08c111ad80e49b274e
Author: Daniel Wagner <wagi@kernel.org>
Date: Wed May 7 14:22:58 2025 +0200
nvmet-fcloop: remove nport from list on last user
The nport object has an association with the rport and lport object,
that means we can only remove an nport object from the global nport_list
after the last user of an rport or lport is gone.
Signed-off-by: Daniel Wagner <wagi@kernel.org>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Christoph Hellwig <hch@lst.de>
diff --git a/drivers/nvme/target/fcloop.c b/drivers/nvme/target/fcloop.c
index a0828626db07..7763b8bf098d 100644
--- a/drivers/nvme/target/fcloop.c
+++ b/drivers/nvme/target/fcloop.c
@@ -1019,9 +1019,15 @@ fcloop_lport_get(struct fcloop_lport *lport)
static void
fcloop_nport_put(struct fcloop_nport *nport)
{
+ unsigned long flags;
+
if (!refcount_dec_and_test(&nport->ref))
return;
+ spin_lock_irqsave(&fcloop_lock, flags);
+ list_del(&nport->nport_list);
+ spin_unlock_irqrestore(&fcloop_lock, flags);
+
kfree(nport);
}
@@ -1414,8 +1420,6 @@ __unlink_remote_port(struct fcloop_nport *nport)
nport->tport->remoteport = NULL;
nport->rport = NULL;
- list_del(&nport->nport_list);
-
return rport;
}
]
|
| 36/2 |
2026/11/27 07:32 |
tool |
git-show |
0m
Args:
Results: map[Output:commit d6c40d87e7fed4b10f2fe93c90487145e2622ebf
Author: Daniel Wagner <wagi@kernel.org>
Date: Wed May 7 14:22:57 2025 +0200
nvmet-fcloop: track ref counts for nports
A nport object is always used in association with targerport,
remoteport, tport and rport objects. Add explicit references for any of
the associated object. This ensures that nport is not removed too early
on shutdown sequences.
Signed-off-by: Daniel Wagner <wagi@kernel.org>
Signed-off-by: Christoph Hellwig <hch@lst.de>
diff --git a/drivers/nvme/target/fcloop.c b/drivers/nvme/target/fcloop.c
index 641201e62c1b..a0828626db07 100644
--- a/drivers/nvme/target/fcloop.c
+++ b/drivers/nvme/target/fcloop.c
@@ -1047,8 +1047,14 @@ static void
fcloop_remoteport_delete(struct nvme_fc_remote_port *remoteport)
{
struct fcloop_rport *rport = remoteport->private;
+ unsigned long flags;
flush_work(&rport->ls_work);
+
+ spin_lock_irqsave(&fcloop_lock, flags);
+ rport->nport->rport = NULL;
+ spin_unlock_irqrestore(&fcloop_lock, flags);
+
fcloop_nport_put(rport->nport);
}
@@ -1056,8 +1062,14 @@ static void
fcloop_targetport_delete(struct nvmet_fc_target_port *targetport)
{
struct fcloop_tport *tport = targetport->private;
+ unsigned long flags;
flush_work(&tport->ls_work);
+
+ spin_lock_irqsave(&fcloop_lock, flags);
+ tport->nport->tport = NULL;
+ spin_unlock_irqrestore(&fcloop_lock, flags);
+
fcloop_nport_put(tport->nport);
}
@@ -1184,6 +1196,37 @@ __wait_localport_unreg(struct fcloop_lport *lport)
return ret;
}
+static struct fcloop_nport *
+__fcloop_nport_lookup(u64 node_name, u64 port_name)
+{
+ struct fcloop_nport *nport;
+
+ list_for_each_entry(nport, &fcloop_nports, nport_list) {
+ if (nport->node_name != node_name ||
+ nport->port_name != port_name)
+ continue;
+
+ if (fcloop_nport_get(nport))
+ return nport;
+
+ break;
+ }
+
+ return NULL;
+}
+
+static struct fcloop_nport *
+fcloop_nport_lookup(u64 node_name, u64 port_name)
+{
+ struct fcloop_nport *nport;
+ unsigned long flags;
+
+ spin_lock_irqsave(&fcloop_lock, flags);
+ nport = __fcloop_nport_lookup(node_name, port_name);
+ spin_unlock_irqrestore(&fcloop_lock, flags);
+
+ return nport;
+}
static ssize_t
fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
@@ -1365,6 +1408,8 @@ __unlink_remote_port(struct fcloop_nport *nport)
{
struct fcloop_rport *rport = nport->rport;
+ lockdep_assert_held(&fcloop_lock);
+
if (rport && nport->tport)
nport->tport->remoteport = NULL;
nport->rport = NULL;
@@ -1377,9 +1422,6 @@ __unlink_remote_port(struct fcloop_nport *nport)
static int
__remoteport_unreg(struct fcloop_nport *nport, struct fcloop_rport *rport)
{
- if (!rport)
- return -EALREADY;
-
return nvme_fc_unregister_remoteport(rport->remoteport);
}
@@ -1387,8 +1429,8 @@ static ssize_t
fcloop_delete_remote_port(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
- struct fcloop_nport *nport = NULL, *tmpport;
- static struct fcloop_rport *rport;
+ struct fcloop_nport *nport;
+ struct fcloop_rport *rport;
u64 nodename, portname;
unsigned long flags;
int ret;
@@ -1397,24 +1439,24 @@ fcloop_delete_remote_port(struct device *dev, struct device_attribute *attr,
if (ret)
return ret;
- spin_lock_irqsave(&fcloop_lock, flags);
-
- list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
- if (tmpport->node_name == nodename &&
- tmpport->port_name == portname && tmpport->rport) {
- nport = tmpport;
- rport = __unlink_remote_port(nport);
- break;
- }
- }
+ nport = fcloop_nport_lookup(nodename, portname);
+ if (!nport)
+ return -ENOENT;
+ spin_lock_irqsave(&fcloop_lock, flags);
+ rport = __unlink_remote_port(nport);
spin_unlock_irqrestore(&fcloop_lock, flags);
- if (!nport)
- return -ENOENT;
+ if (!rport) {
+ ret = -ENOENT;
+ goto out_nport_put;
+ }
ret = __remoteport_unreg(nport, rport);
+out_nport_put:
+ fcloop_nport_put(nport);
+
return ret ? ret : count;
}
@@ -1465,6 +1507,8 @@ __unlink_target_port(struct fcloop_nport *nport)
{
struct fcloop_tport *tport = nport->tport;
+ lockdep_assert_held(&fcloop_lock);
+
if (tport && nport->rport)
nport->rport->targetport = NULL;
nport->tport = NULL;
@@ -1475,9 +1519,6 @@ __unlink_target_port(struct fcloop_nport *nport)
static int
__targetport_unreg(struct fcloop_nport *nport, struct fcloop_tport *tport)
{
- if (!tport)
- return -EALREADY;
-
return nvmet_fc_unregister_targetport(tport->targetport);
}
@@ -1485,8 +1526,8 @@ static ssize_t
fcloop_delete_target_port(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
- struct fcloop_nport *nport = NULL, *tmpport;
- struct fcloop_tport *tport = NULL;
+ struct fcloop_nport *nport;
+ struct fcloop_tport *tport;
u64 nodename, portname;
unsigned long flags;
int ret;
@@ -1495,24 +1536,24 @@ fcloop_delete_target_port(struct device *dev, struct device_attribute *attr,
if (ret)
return ret;
- spin_lock_irqsave(&fcloop_lock, flags);
-
- list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
- if (tmpport->node_name == nodename &&
- tmpport->port_name == portname && tmpport->tport) {
- nport = tmpport;
- tport = __unlink_target_port(nport);
- break;
- }
- }
+ nport = fcloop_nport_lookup(nodename, portname);
+ if (!nport)
+ return -ENOENT;
+ spin_lock_irqsave(&fcloop_lock, flags);
+ tport = __unlink_target_port(nport);
spin_unlock_irqrestore(&fcloop_lock, flags);
- if (!nport)
- return -ENOENT;
+ if (!tport) {
+ ret = -ENOENT;
+ goto out_nport_put;
+ }
ret = __targetport_unreg(nport, tport);
+out_nport_put:
+ fcloop_nport_put(nport);
+
return ret ? ret : count;
}
@@ -1609,8 +1650,8 @@ static int __init fcloop_init(void)
static void __exit fcloop_exit(void)
{
- struct fcloop_lport *lport = NULL;
- struct fcloop_nport *nport = NULL;
+ struct fcloop_lport *lport;
+ struct fcloop_nport *nport;
struct fcloop_tport *tport;
struct fcloop_rport *rport;
unsigned long flags;
@@ -1621,7 +1662,7 @@ static void __exit fcloop_exit(void)
for (;;) {
nport = list_first_entry_or_null(&fcloop_nports,
typeof(*nport), nport_list);
- if (!nport)
+ if (!nport || !fcloop_nport_get(nport))
break;
tport = __unlink_target_port(nport);
@@ -1629,13 +1670,21 @@ static void __exit fcloop_exit(void)
spin_unlock_irqrestore(&fcloop_lock, flags);
- ret = __targetport_unreg(nport, tport);
- if (ret)
- pr_warn("%s: Failed deleting target port\n", __func__);
+ if (tport) {
+ ret = __targetport_unreg(nport, tport);
+ if (ret)
+ pr_warn("%s: Failed deleting target port\n",
+ __func__);
+ }
- ret = __remoteport_unreg(nport, rport);
- if (ret)
- pr_warn("%s: Failed deleting remote port\n", __func__);
+ if (rport) {
+ ret = __remoteport_unreg(nport, rport);
+ if (ret)
+ pr_warn("%s: Failed deleting remote port\n",
+ __func__);
+ }
+
+ fcloop_nport_put(nport);
spin_lock_irqsave(&fcloop_lock, flags);
}
]
|
| 37/2 |
2026/11/27 07:32 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 42553
output: 182
thoughts: 234
Thoughts: **Eureka! I've spotted a critical divergence in the historical behavior of `fcloop_create_target_port` and `nvmet_fc_register_targetport`.**
My initial instinct was to scrutinize the timeline, and that's where the anomaly jumped out. I'm specifically looking at how `fcloop_create_target_port` and `nvmet_fc_register_targetport` have *always* functioned versus what has recently changed.
To get to the bottom of this, I immediately dove into the `fcloop.c` file, focusing on a specific commit from 2016, `475d0fe795516`. Within that commit's context, I'm examining the interplay between `fcloop_alloc_nport` and `fcloop_create_target_port`.
To thoroughly understand the evolution, I'm performing targeted `git show` operations on `475d0fe795516` specifically for `drivers/nvme/target/fcloop.c`, paying close attention to the sections surrounding `fcloop_alloc_nport` and `fcloop_create_target_port`. Furthermore, to trace the precise lineage of these functions, I'm leveraging `git log -L` with the corresponding function names to meticulously review their change history within `drivers/nvme/target/fcloop.c`. This deep dive is essential to pinpointing the exact moment and nature of the deviation in their behavior.
|
| 38/2 |
2026/11/27 07:32 |
tool |
git-log |
0m
Args: map[SourcePath:drivers/nvme/target/fcloop.c SymbolName:fcloop_create_target_port]
Results: map[Output:2b559a3eb56b nvmet-fcloop: prevent double port deletion
437c0b824dbd nvme-fcloop: add target to host LS request support
7c3a23b85cac nvmet/fcloop: remove some logically dead code performing redundant ret checks
475d0fe79551 nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME
]
|
| 39/2 |
2026/11/27 07:32 |
tool |
git-log |
0m
Args: map[SourcePath:drivers/nvme/target/fcloop.c SymbolName:fcloop_alloc_nport]
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
d54a9d7f6d74 nvmet-fcloop: refactor fcloop_nport_alloc and track lport
f22c458f9495 nvmet-fcloop: replace kref with refcount
475d0fe79551 nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME
]
|
| 40/2 |
2026/11/27 07:33 |
tool |
git-log |
0m
Args: map[SourcePath:drivers/nvme/target/fc.c SymbolName:nvmet_fc_portentry_rebind_tgt]
Results: map[Output:44e479d72020 nvme: spelling fixes
596cba55adb4 nvmet-fc: take tgtport refs for portentry
b2fb99e42598 nvmet-fc: fix kernel-doc warning for nvmet_fc_register_targetport
ea96d6496ff5 nvmet_fc: support target port removal with nvmet layer
c53432030d86 nvme-fabrics: Add target support for FC transport
]
|
| 41/2 |
2026/11/27 07:33 |
tool |
git-log |
0m
Args: map[SourcePath:drivers/nvme/target/fc.c SymbolName:nvmet_fc_register_targetport]
Results: map[Output:db5a5406fb7e nvmet-fc: move lsop put work to nvmet_fc_ls_req_op
710c69dbaccd nvmet-fc: avoid deadlock on delete association path
6dd0f465d57c nvmet-fc: replace ida_simple[get|remove] with the simler ida_[alloc|free]
47bf32410644 nvmet-fc: Add Disconnect Association Xmt support
349c694ee71c nvmet-fc: rename ls_list to ls_rcv_list
58ab8ff9dca2 nvmet-fc: track hostport handle for associations
f56bf76f79f3 nvme-fc: Ensure private pointers are NULL if no data
ea96d6496ff5 nvmet_fc: support target port removal with nvmet layer
d082dc1562a2 nvmet-fc: fix target sgl list on large transfers
48fa362b6c3f nvmet-fc: simplify sg list handling
]
|
| 42/2 |
2026/11/27 07:33 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 43660
output: 172
thoughts: 1551
Thoughts: **My Examination of the FCLoop Race Condition**
I'm meticulously scrutinizing the reproducer to pinpoint the exact race condition and its potential origins or resolutions across various code revisions. My immediate concern is the sequence of operations within `fcloop_create_target_port`, specifically how it interacts with `fcloop_alloc_nport` and `nvmet_fc_register_targetport`.
I've observed that in commit `475d0fe79551` (from 2016), `fcloop_create_target_port` would call `fcloop_alloc_nport`, then *release* the `fcloop_lock`, and *only then* call `nvmet_fc_register_targetport`, with the assignment `nport->tport = tport` happening *after* the registration. This sequence immediately raised a red flag for concurrency issues.
I'm particularly interested in the state *before* commit `ea96d6496ff5` (from 2018). If two threads were to concurrently call `fcloop_create_target_port` back then, what would have happened? It appears that before `ea96d6496ff5`, there was no `nvmet_fc_portentry_list`, no `nvmet_fc_portentry_rebind_tgt`, and crucially, no `WARN_ON(pe->tgtport)`. In that older context, `nvmet_fc_register_targetport` would simply allocate a new `tgtport` and add it to `nvmet_fc_target_list`. My concern is that in `fcloop_create_target_port`, the second thread would then overwrite `nport->tport = tport`, leading to a leak of the first `tport` and `tgtport`. Furthermore, both threads would end up with entries in `nvmet_fc_target_list` associated with the same WWNN/WWPN.
I'm also digging into `nvmet_fc_register_targetport` in `fc.c`. Does it inherently check for duplicate WWNN/WWPNs in `nvmet_fc_target_list`? Or is the responsibility solely on the LLDD (like `fcloop`) to prevent registering duplicate target ports? Or did commit `ea96d6496ff5` introduce `nvmet_fc_portentry_rebind_tgt` with the assumption that such simultaneous registrations of duplicate WWNN/WWPNs wouldn't occur? I need to clarify this crucial dependency.
Let's break down the possibilities I'm considering:
**Possibility A: Race within `fcloop.c`'s `fcloop_create_target_port` / `fcloop_alloc_nport` across revisions `475d0fe79551`, `d54a9d7f6d74`, or other relevant changes.**
I'm comparing the behavior between `475d0fe79551` and `d54a9d7f6d74` very closely.
In `475d0fe79551`:
* A thread calls `fcloop_alloc_nport`. This function adds `newnport` to `fcloop_nports`. Crucially, `newnport->tport` is `NULL` at this point. The `fcloop_lock` is then unlocked, and the function returns `newnport`.
Now, let's consider a concurrent scenario:
* **Thread 1** executes the steps above, adding its `newnport` to the list with `tport` as `NULL` and releasing the lock.
* **Thread 2** then calls `fcloop_alloc_nport`. It iterates through `fcloop_nports` and finds the `nport` created by Thread 1. It checks the condition `!remoteport && nport->tport`. Since `nport->tport` is `NULL` (from Thread 1's `newnport`), this condition evaluates to false. Thread 2 then calls `fcloop_nport_get(nport)`, unlocks `fcloop_lock`, and returns this `nport`.
* The problem is that **both Thread 1 and Thread 2** now proceed to call `nvmet_fc_register_targetport` with what they believe is a unique `nport`.
Now, let's look at `d54a9d7f6d74`, specifically lines 1396-1409:
nport = __fcloop_nport_lookup(opts->wwnn, opts->wwpn);
if (nport) {
if ((remoteport && nport->rport) ||
(!remoteport && nport->tport)) {
/* invalid configuration */
goto out_put_nport;
}
/* found existing nport, discard the new nport */
kfree(newnport);
} else {
list_add_tail(&newnport->nport_list, &fcloop_nports);
nport = newnport;
}
This code *appears* to have the same fundamental race condition as `475d0fe79551`. In `475d0fe79551`, `fcloop_nport_get(nport)` was called on an existing `nport` if found. In `d54a9d7f6d74`, `__fcloop_nport_lookup` itself calls `fcloop_nport_get(nport)`. So, based on this specific comparison, commit `d54a9d7f6d74` does *not* seem to have introduced this race within `fcloop_alloc_nport`.
I'm continuing my investigation by examining other relevant code sections in `fc.c` and `fcloop.c`, specifically functions like `nvmet_fc_portentry_*`, `nvmet_fc_register_targetport`, `nvmet_fc_unregister_targetport`, `nvmet_fc_add_port`, `nvmet_fc_remove_port`, `nvmet_fc_tgtport_get`, `nvmet_fc_tgtport_put` in `fc.c`, and `fcloop_alloc_nport`, `fcloop_create_target_port`, `fcloop_delete_target_port`, `fcloop_nport_put`, `fcloop_targetport_delete` in `fcloop.c`. I need to understand the complete picture of how target ports are managed and protected from concurrent access.
|
| 43/2 |
2026/11/27 07:33 |
tool |
read-file |
0m
Args: map[File:drivers/nvme/target/fc.c FirstLine:1245 LineCount:95]
Results: map[Contents:1245: rcu_read_unlock();
1246:
1247: return ret;
1248: }
1249:
1250: static void
1251: nvmet_fc_portentry_bind(struct nvmet_fc_tgtport *tgtport,
1252: struct nvmet_fc_port_entry *pe,
1253: struct nvmet_port *port)
1254: {
1255: lockdep_assert_held(&nvmet_fc_tgtlock);
1256:
1257: nvmet_fc_tgtport_get(tgtport);
1258: pe->tgtport = tgtport;
1259: tgtport->pe = pe;
1260:
1261: pe->port = port;
1262: port->priv = pe;
1263:
1264: pe->node_name = tgtport->fc_target_port.node_name;
1265: pe->port_name = tgtport->fc_target_port.port_name;
1266: INIT_LIST_HEAD(&pe->pe_list);
1267:
1268: list_add_tail(&pe->pe_list, &nvmet_fc_portentry_list);
1269: }
1270:
1271: static void
1272: nvmet_fc_portentry_unbind(struct nvmet_fc_port_entry *pe)
1273: {
1274: unsigned long flags;
1275:
1276: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1277: if (pe->tgtport) {
1278: nvmet_fc_tgtport_put(pe->tgtport);
1279: pe->tgtport->pe = NULL;
1280: }
1281: list_del(&pe->pe_list);
1282: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1283: }
1284:
1285: /*
1286: * called when a targetport deregisters. Breaks the relationship
1287: * with the nvmet port, but leaves the port_entry in place so that
1288: * re-registration can resume operation.
1289: */
1290: static void
1291: nvmet_fc_portentry_unbind_tgt(struct nvmet_fc_tgtport *tgtport)
1292: {
1293: struct nvmet_fc_port_entry *pe;
1294: unsigned long flags;
1295:
1296: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1297: pe = tgtport->pe;
1298: if (pe) {
1299: nvmet_fc_tgtport_put(pe->tgtport);
1300: pe->tgtport = NULL;
1301: }
1302: tgtport->pe = NULL;
1303: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1304: }
1305:
1306: /*
1307: * called when a new targetport is registered. Looks in the
1308: * existing nvmet port_entries to see if the nvmet layer is
1309: * configured for the targetport's wwn's. (the targetport existed,
1310: * nvmet configured, the lldd unregistered the tgtport, and is now
1311: * reregistering the same targetport). If so, set the nvmet port
1312: * port entry on the targetport.
1313: */
1314: static void
1315: nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
1316: {
1317: struct nvmet_fc_port_entry *pe;
1318: unsigned long flags;
1319:
1320: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1321: list_for_each_entry(pe, &nvmet_fc_portentry_list, pe_list) {
1322: if (tgtport->fc_target_port.node_name == pe->node_name &&
1323: tgtport->fc_target_port.port_name == pe->port_name) {
1324: if (!nvmet_fc_tgtport_get(tgtport))
1325: continue;
1326:
1327: WARN_ON(pe->tgtport);
1328: tgtport->pe = pe;
1329: pe->tgtport = tgtport;
1330: break;
1331: }
1332: }
1333: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1334: }
1335:
1336: /**
1337: * nvmet_fc_register_targetport - transport entry point called by an
1338: * LLDD to register the existence of a local
1339: * NVME subsystem FC port.
]
|
| 44/2 |
2026/11/27 07:33 |
tool |
read-file |
0m
Args: map[File:drivers/nvme/target/fc.c FirstLine:1620 LineCount:65]
Results: map[Contents:1620: }
1621: }
1622:
1623: /**
1624: * nvmet_fc_unregister_targetport - transport entry point called by an
1625: * LLDD to deregister/remove a previously
1626: * registered a local NVME subsystem FC port.
1627: * @target_port: pointer to the (registered) target port that is to be
1628: * deregistered.
1629: *
1630: * Returns:
1631: * a completion status. Must be 0 upon success; a negative errno
1632: * (ex: -ENXIO) upon failure.
1633: */
1634: int
1635: nvmet_fc_unregister_targetport(struct nvmet_fc_target_port *target_port)
1636: {
1637: struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
1638: unsigned long flags;
1639:
1640: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1641: list_del(&tgtport->tgt_list);
1642: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1643:
1644: nvmet_fc_portentry_unbind_tgt(tgtport);
1645:
1646: /* terminate any outstanding associations */
1647: __nvmet_fc_free_assocs(tgtport);
1648:
1649: flush_workqueue(nvmet_wq);
1650:
1651: nvmet_fc_free_pending_reqs(tgtport);
1652: nvmet_fc_tgtport_put(tgtport);
1653:
1654: return 0;
1655: }
1656: EXPORT_SYMBOL_GPL(nvmet_fc_unregister_targetport);
1657:
1658:
1659: /* ********************** FC-NVME LS RCV Handling ************************* */
1660:
1661:
1662: static void
1663: nvmet_fc_ls_create_association(struct nvmet_fc_tgtport *tgtport,
1664: struct nvmet_fc_ls_iod *iod)
1665: {
1666: struct fcnvme_ls_cr_assoc_rqst *rqst = &iod->rqstbuf->rq_cr_assoc;
1667: struct fcnvme_ls_cr_assoc_acc *acc = &iod->rspbuf->rsp_cr_assoc;
1668: struct nvmet_fc_tgt_queue *queue;
1669: int ret = 0;
1670:
1671: memset(acc, 0, sizeof(*acc));
1672:
1673: /*
1674: * FC-NVME spec changes. There are initiators sending different
1675: * lengths as padding sizes for Create Association Cmd descriptor
1676: * was incorrect.
1677: * Accept anything of "minimum" length. Assume format per 1.15
1678: * spec (with HOSTID reduced to 16 bytes), ignore how long the
1679: * trailing pad length is.
1680: */
1681: if (iod->rqstdatalen < FCNVME_LSDESC_CRA_RQST_MINLEN)
1682: ret = VERR_CR_ASSOC_LEN;
1683: else if (be32_to_cpu(rqst->desc_list_len) <
1684: FCNVME_LSDESC_CRA_RQST_MIN_LISTLEN)
]
|
| 45/2 |
2026/11/27 07:33 |
tool |
read-file |
0m
Args: map[File:drivers/nvme/target/fc.c FirstLine:2865 LineCount:100]
Results: map[Contents:2865: {
2866: struct nvmet_fc_tgtport *tgtport;
2867: struct nvmet_fc_port_entry *pe;
2868: struct nvmet_fc_traddr traddr = { 0L, 0L };
2869: unsigned long flags;
2870: int ret;
2871:
2872: /* validate the address info */
2873: if ((port->disc_addr.trtype != NVMF_TRTYPE_FC) ||
2874: (port->disc_addr.adrfam != NVMF_ADDR_FAMILY_FC))
2875: return -EINVAL;
2876:
2877: /* map the traddr address info to a target port */
2878:
2879: ret = nvme_fc_parse_traddr(&traddr, port->disc_addr.traddr,
2880: sizeof(port->disc_addr.traddr));
2881: if (ret)
2882: return ret;
2883:
2884: pe = kzalloc_obj(*pe);
2885: if (!pe)
2886: return -ENOMEM;
2887:
2888: ret = -ENXIO;
2889: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
2890: list_for_each_entry(tgtport, &nvmet_fc_target_list, tgt_list) {
2891: if ((tgtport->fc_target_port.node_name == traddr.nn) &&
2892: (tgtport->fc_target_port.port_name == traddr.pn)) {
2893: if (!nvmet_fc_tgtport_get(tgtport))
2894: continue;
2895:
2896: /* a FC port can only be 1 nvmet port id */
2897: if (!tgtport->pe) {
2898: nvmet_fc_portentry_bind(tgtport, pe, port);
2899: ret = 0;
2900: } else
2901: ret = -EALREADY;
2902:
2903: nvmet_fc_tgtport_put(tgtport);
2904: break;
2905: }
2906: }
2907: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
2908:
2909: if (ret)
2910: kfree(pe);
2911:
2912: return ret;
2913: }
2914:
2915: static void
2916: nvmet_fc_remove_port(struct nvmet_port *port)
2917: {
2918: struct nvmet_fc_port_entry *pe = port->priv;
2919: struct nvmet_fc_tgtport *tgtport = NULL;
2920: unsigned long flags;
2921:
2922: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
2923: if (pe->tgtport && nvmet_fc_tgtport_get(pe->tgtport))
2924: tgtport = pe->tgtport;
2925: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
2926:
2927: nvmet_fc_portentry_unbind(pe);
2928:
2929: if (tgtport) {
2930: /* terminate any outstanding associations */
2931: __nvmet_fc_free_assocs(tgtport);
2932: nvmet_fc_tgtport_put(tgtport);
2933: }
2934:
2935: kfree(pe);
2936: }
2937:
2938: static void
2939: nvmet_fc_discovery_chg(struct nvmet_port *port)
2940: {
2941: struct nvmet_fc_port_entry *pe = port->priv;
2942: struct nvmet_fc_tgtport *tgtport = NULL;
2943: unsigned long flags;
2944:
2945: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
2946: if (pe->tgtport && nvmet_fc_tgtport_get(pe->tgtport))
2947: tgtport = pe->tgtport;
2948: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
2949:
2950: if (!tgtport)
2951: return;
2952:
2953: if (tgtport && tgtport->ops->discovery_event)
2954: tgtport->ops->discovery_event(&tgtport->fc_target_port);
2955:
2956: nvmet_fc_tgtport_put(tgtport);
2957: }
2958:
2959: static ssize_t
2960: nvmet_fc_host_traddr(struct nvmet_ctrl *ctrl,
2961: char *traddr, size_t traddr_size)
2962: {
2963: struct nvmet_sq *sq = ctrl->sqs[0];
2964: struct nvmet_fc_tgt_queue *queue =
]
|
| 46/2 |
2026/11/27 07:33 |
tool |
read-file |
0m
Args: map[File:drivers/nvme/target/fcloop.c FirstLine:1050 LineCount:100]
Results: map[Contents:1050:
1051: static void
1052: fcloop_lport_put(struct fcloop_lport *lport)
1053: {
1054: unsigned long flags;
1055:
1056: if (!refcount_dec_and_test(&lport->ref))
1057: return;
1058:
1059: spin_lock_irqsave(&fcloop_lock, flags);
1060: list_del(&lport->lport_list);
1061: spin_unlock_irqrestore(&fcloop_lock, flags);
1062:
1063: kfree(lport);
1064: }
1065:
1066: static int
1067: fcloop_lport_get(struct fcloop_lport *lport)
1068: {
1069: return refcount_inc_not_zero(&lport->ref);
1070: }
1071:
1072: static void
1073: fcloop_nport_put(struct fcloop_nport *nport)
1074: {
1075: unsigned long flags;
1076:
1077: if (!refcount_dec_and_test(&nport->ref))
1078: return;
1079:
1080: spin_lock_irqsave(&fcloop_lock, flags);
1081: list_del(&nport->nport_list);
1082: spin_unlock_irqrestore(&fcloop_lock, flags);
1083:
1084: if (nport->lport)
1085: fcloop_lport_put(nport->lport);
1086:
1087: kfree(nport);
1088: }
1089:
1090: static int
1091: fcloop_nport_get(struct fcloop_nport *nport)
1092: {
1093: return refcount_inc_not_zero(&nport->ref);
1094: }
1095:
1096: static void
1097: fcloop_localport_delete(struct nvme_fc_local_port *localport)
1098: {
1099: struct fcloop_lport_priv *lport_priv = localport->private;
1100: struct fcloop_lport *lport = lport_priv->lport;
1101:
1102: fcloop_lport_put(lport);
1103: }
1104:
1105: static void
1106: fcloop_remoteport_delete(struct nvme_fc_remote_port *remoteport)
1107: {
1108: struct fcloop_rport *rport = remoteport->private;
1109: bool put_port = false;
1110: unsigned long flags;
1111:
1112: flush_work(&rport->ls_work);
1113:
1114: spin_lock_irqsave(&fcloop_lock, flags);
1115: if (!test_and_set_bit(PORT_DELETED, &rport->flags))
1116: put_port = true;
1117: rport->nport->rport = NULL;
1118: spin_unlock_irqrestore(&fcloop_lock, flags);
1119:
1120: if (put_port) {
1121: WARN_ON(!list_empty(&rport->ls_list));
1122: fcloop_nport_put(rport->nport);
1123: }
1124: }
1125:
1126: static void
1127: fcloop_targetport_delete(struct nvmet_fc_target_port *targetport)
1128: {
1129: struct fcloop_tport *tport = targetport->private;
1130: bool put_port = false;
1131: unsigned long flags;
1132:
1133: flush_work(&tport->ls_work);
1134:
1135: spin_lock_irqsave(&fcloop_lock, flags);
1136: if (!test_and_set_bit(PORT_DELETED, &tport->flags))
1137: put_port = true;
1138: tport->nport->tport = NULL;
1139: spin_unlock_irqrestore(&fcloop_lock, flags);
1140:
1141: if (put_port) {
1142: WARN_ON(!list_empty(&tport->ls_list));
1143: fcloop_nport_put(tport->nport);
1144: }
1145: }
1146:
1147: #define FCLOOP_HW_QUEUES 4
1148: #define FCLOOP_SGL_SEGS 256
1149: #define FCLOOP_DMABOUND_4G 0xFFFFFFFF
]
|
| 47/2 |
2026/11/27 07:33 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 50857
output: 29
thoughts: 138
Thoughts: **NVMe-oF FC Driver Investigation**
Alright, so my primary goal here is to get a comprehensive understanding of *all* the real Fibre Channel (FC) Logical Logical Device Drivers (LLDDs) that are actually making use of the `nvmet_fc_register_targetport` or `nvme_fc_register_localport` functions. I want to be absolutely sure I'm not missing anything crucial.
My initial thought was to leverage `codesearch-find-references`. I ran it, expecting a broad sweep, but it *only* pointed me to `fcloop_create_target_port`. This immediately set off a mental alarm. Is that really *it*? It feels too narrow for such a critical part of the NVMe-oF FC stack.
This led me to question the scope of my initial search. Perhaps the relevant drivers aren't all neatly tucked away in `drivers/`. I know that established SCSI drivers, like `lpfc` and `qla2xxx`, are fundamental to FC operation, and they reside in `drivers/scsi/`. Could it be that the NVMe-oF FC integration is happening within these existing SCSI drivers, rather than in entirely new, dedicated LLDDs?
If that's the case, why didn't `codesearch-find-references` pick them up? My immediate hypotheses are:
1. **Indexing Issues:** The `codesearch` tool might not have indexed those specific files or code paths effectively.
2. **Configuration Dependencies:** The relevant code might only be compiled and thus "active" when specific kernel configurations are enabled. If my current environment or the `codesearch` indexing process didn't have those configurations set, the functions wouldn't appear as referenced in the way I expect.
Given this uncertainty, I'm going to pivot to a more direct, albeit potentially less sophisticated, approach to ensure I don't miss anything. I'm going to resort to `grepper` for `nvmet_fc_register_targetport` across the entire driver tree. This will give me a brute-force, text-based confirmation and help me identify any place this function call might exist, regardless of how it's indexed or compiled. I need to be exhaustive.
|
| 48/2 |
2026/11/27 07:33 |
tool |
grepper |
0m
Args: map[Expression:nvmet_fc_register_targetport]
Results: map[Output:drivers/nvme/target/fc.c=1315=nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
--
drivers/nvme/target/fc.c-1336-/**
drivers/nvme/target/fc.c:1337: * nvmet_fc_register_targetport - transport entry point called by an
drivers/nvme/target/fc.c-1338- * LLDD to register the existence of a local
--
drivers/nvme/target/fc.c=1353=int
drivers/nvme/target/fc.c:1354:nvmet_fc_register_targetport(struct nvmet_fc_port_info *pinfo,
drivers/nvme/target/fc.c-1355- struct nvmet_fc_target_template *template,
--
drivers/nvme/target/fc.c-1435-}
drivers/nvme/target/fc.c:1436:EXPORT_SYMBOL_GPL(nvmet_fc_register_targetport);
drivers/nvme/target/fc.c-1437-
--
drivers/nvme/target/fcloop.c=1541=fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
--
drivers/nvme/target/fcloop.c-1557-
drivers/nvme/target/fcloop.c:1558: ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
drivers/nvme/target/fcloop.c-1559- &targetport);
--
drivers/scsi/lpfc/lpfc_nvmet.c=1644=lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
--
drivers/scsi/lpfc/lpfc_nvmet.c-1671-#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
drivers/scsi/lpfc/lpfc_nvmet.c:1672: error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
drivers/scsi/lpfc/lpfc_nvmet.c-1673- &phba->pcidev->dev,
--
include/linux/nvme-fc-driver.h=963=struct nvmet_fc_target_template {
--
include/linux/nvme-fc-driver.h-995-
include/linux/nvme-fc-driver.h:996:int nvmet_fc_register_targetport(struct nvmet_fc_port_info *portinfo,
include/linux/nvme-fc-driver.h-997- struct nvmet_fc_target_template *template,
]
|
| 49/2 |
2026/11/27 07:33 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 51735
output: 131
thoughts: 133
Thoughts: **Deconstructing NVMe-oF Target Port Creation and Management**
My current focus is on understanding the intricacies of how NVMe-oF target ports are established and managed within the Linux kernel, specifically looking at the Fibre Channel (FC) transport. I'm comparing and contrasting the implementations across different drivers to gain a comprehensive understanding.
My initial hypothesis is that there are distinct approaches to this process, and I'm drilling down into the source code to verify this. Specifically, I'm examining three key functions:
* `nvmet_fc_register_targetport`: This appears to be the generic NVMe-oF target port registration function for the FC transport. I need to see how it interacts with lower-level FC fabric management and resource allocation.
* `fcloop_create_target_port`: This function, likely part of a loopback or internal FC simulation mechanism, might offer a simpler or foundational view of target port creation without the complexities of a full fabric connection. I'm curious if it shares common code paths with fabric-attached ports or if it's a separate, self-contained implementation.
* `lpfc_nvmet_create_targetport`: This is the specific implementation within the `lpfc` (Linux Fibre Channel Protocol) driver. I'm diving into `drivers/scsi/lpfc/lpfc_nvmet.c` around line 1644 to meticulously trace its execution. My goal here is to understand how `lpfc` bridges the NVMe-oF layer with its own FC fabric and HBA management.
To complement this, I'm also reviewing the NVMe-oF target FC driver's lifecycle management functions in `drivers/nvme/target/fc.c`, specifically `nvmet_fc_tgtport_get`, `nvmet_fc_tgtport_put`, and `nvmet_fc_tgtport_cleanup`. These functions are crucial for understanding how target ports are acquired, released, and ultimately cleaned up, providing insights into the resource management and state transitions involved.
My aim is to build a detailed mental model of the entire target port lifecycle, from initial registration and resource allocation through to fabric attachment, data path setup, and eventual cleanup, by comparing and contrasting these different implementation details. I'm particularly interested in identifying any shared abstractions or common interfaces that might be leveraged across these drivers, as well as understanding the unique mechanisms employed by each.
|
| 50/2 |
2026/11/27 07:33 |
tool |
read-file |
0m
Args: map[File:drivers/scsi/lpfc/lpfc_nvmet.c FirstLine:1640 LineCount:60]
Results: map[Contents:1640: return 0;
1641: }
1642:
1643: int
1644: lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
1645: {
1646: struct lpfc_vport *vport = phba->pport;
1647: struct lpfc_nvmet_tgtport *tgtp;
1648: struct nvmet_fc_port_info pinfo;
1649: int error;
1650:
1651: if (phba->targetport)
1652: return 0;
1653:
1654: error = lpfc_nvmet_setup_io_context(phba);
1655: if (error)
1656: return error;
1657:
1658: memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
1659: pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1660: pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
1661: pinfo.port_id = vport->fc_myDID;
1662:
1663: /* We need to tell the transport layer + 1 because it takes page
1664: * alignment into account. When space for the SGL is allocated we
1665: * allocate + 3, one for cmd, one for rsp and one for this alignment
1666: */
1667: lpfc_tgttemplate.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
1668: lpfc_tgttemplate.max_hw_queues = phba->cfg_hdw_queue;
1669: lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP;
1670:
1671: #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1672: error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
1673: &phba->pcidev->dev,
1674: &phba->targetport);
1675: #else
1676: error = -ENOENT;
1677: #endif
1678: if (error) {
1679: lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1680: "6025 Cannot register NVME targetport x%x: "
1681: "portnm %llx nodenm %llx segs %d qs %d\n",
1682: error,
1683: pinfo.port_name, pinfo.node_name,
1684: lpfc_tgttemplate.max_sgl_segments,
1685: lpfc_tgttemplate.max_hw_queues);
1686: phba->targetport = NULL;
1687: phba->nvmet_support = 0;
1688:
1689: lpfc_nvmet_cleanup_io_context(phba);
1690:
1691: } else {
1692: tgtp = (struct lpfc_nvmet_tgtport *)
1693: phba->targetport->private;
1694: tgtp->phba = phba;
1695:
1696: lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1697: "6026 Registered NVME "
1698: "targetport: x%px, private x%px "
1699: "portnm %llx nodenm %llx segs %d qs %d\n",
]
|
| 51/2 |
2026/11/27 07:33 |
tool |
read-file |
0m
Args: map[File:drivers/nvme/target/fc.c FirstLine:1170 LineCount:80]
Results: map[Contents:1170:
1171: for (i = NVMET_NR_QUEUES; i >= 0; i--) {
1172: if (assoc->queues[i])
1173: nvmet_fc_delete_target_queue(assoc->queues[i]);
1174: }
1175:
1176: /* Send Disconnect now that all i/o has completed */
1177: nvmet_fc_xmt_disconnect_assoc(assoc);
1178:
1179: nvmet_fc_hostport_put(assoc->hostport);
1180: spin_lock_irqsave(&tgtport->lock, flags);
1181: oldls = assoc->rcv_disconn;
1182: spin_unlock_irqrestore(&tgtport->lock, flags);
1183: /* if pending Rcv Disconnect Association LS, send rsp now */
1184: if (oldls)
1185: nvmet_fc_xmt_ls_rsp(tgtport, oldls);
1186: ida_free(&tgtport->assoc_cnt, assoc->a_id);
1187: pr_info("{%d:%d}: Association freed\n",
1188: tgtport->fc_target_port.port_num, assoc->a_id);
1189: kfree(assoc);
1190: }
1191:
1192: static void
1193: nvmet_fc_tgt_a_put(struct nvmet_fc_tgt_assoc *assoc)
1194: {
1195: kref_put(&assoc->ref, nvmet_fc_target_assoc_free);
1196: }
1197:
1198: static int
1199: nvmet_fc_tgt_a_get(struct nvmet_fc_tgt_assoc *assoc)
1200: {
1201: return kref_get_unless_zero(&assoc->ref);
1202: }
1203:
1204: static void
1205: nvmet_fc_delete_target_assoc(struct nvmet_fc_tgt_assoc *assoc)
1206: {
1207: struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
1208: unsigned long flags;
1209: int i;
1210:
1211: spin_lock_irqsave(&tgtport->lock, flags);
1212: list_del_rcu(&assoc->a_list);
1213: spin_unlock_irqrestore(&tgtport->lock, flags);
1214:
1215: synchronize_rcu();
1216:
1217: /* ensure all in-flight I/Os have been processed */
1218: for (i = NVMET_NR_QUEUES; i >= 0; i--) {
1219: if (assoc->queues[i])
1220: flush_workqueue(assoc->queues[i]->work_q);
1221: }
1222:
1223: pr_info("{%d:%d}: Association deleted\n",
1224: tgtport->fc_target_port.port_num, assoc->a_id);
1225:
1226: nvmet_fc_tgtport_put(tgtport);
1227: }
1228:
1229: static struct nvmet_fc_tgt_assoc *
1230: nvmet_fc_find_target_assoc(struct nvmet_fc_tgtport *tgtport,
1231: u64 association_id)
1232: {
1233: struct nvmet_fc_tgt_assoc *assoc;
1234: struct nvmet_fc_tgt_assoc *ret = NULL;
1235:
1236: rcu_read_lock();
1237: list_for_each_entry_rcu(assoc, &tgtport->assoc_list, a_list) {
1238: if (association_id == assoc->association_id) {
1239: ret = assoc;
1240: if (!nvmet_fc_tgt_a_get(assoc))
1241: ret = NULL;
1242: break;
1243: }
1244: }
1245: rcu_read_unlock();
1246:
1247: return ret;
1248: }
1249:
]
|
| 52/2 |
2026/11/27 07:33 |
tool |
read-file |
0m
Args: map[File:drivers/nvme/target/fc.c FirstLine:1580 LineCount:50]
Results: map[Contents:1580: nvmet_fc_schedule_delete_assoc(assoc);
1581: nvmet_fc_tgt_a_put(assoc);
1582: return;
1583: }
1584:
1585: spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
1586: }
1587: spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
1588: }
1589:
1590: static void
1591: nvmet_fc_free_pending_reqs(struct nvmet_fc_tgtport *tgtport)
1592: {
1593: struct nvmet_fc_ls_req_op *lsop;
1594: struct nvmefc_ls_req *lsreq;
1595: struct nvmet_fc_ls_iod *iod;
1596: int i;
1597:
1598: iod = tgtport->iod;
1599: for (i = 0; i < NVMET_LS_CTX_COUNT; iod++, i++)
1600: cancel_work(&iod->work);
1601:
1602: /*
1603: * After this point the connection is lost and thus any pending
1604: * request can't be processed by the normal completion path. This
1605: * is likely a request from nvmet_fc_send_ls_req_async.
1606: */
1607: while ((lsop = list_first_entry_or_null(&tgtport->ls_req_list,
1608: struct nvmet_fc_ls_req_op, lsreq_list))) {
1609: list_del(&lsop->lsreq_list);
1610:
1611: if (!lsop->req_queued)
1612: continue;
1613:
1614: lsreq = &lsop->ls_req;
1615: fc_dma_unmap_single(tgtport->dev, lsreq->rqstdma,
1616: (lsreq->rqstlen + lsreq->rsplen),
1617: DMA_BIDIRECTIONAL);
1618: nvmet_fc_tgtport_put(tgtport);
1619: kfree(lsop);
1620: }
1621: }
1622:
1623: /**
1624: * nvmet_fc_unregister_targetport - transport entry point called by an
1625: * LLDD to deregister/remove a previously
1626: * registered a local NVME subsystem FC port.
1627: * @target_port: pointer to the (registered) target port that is to be
1628: * deregistered.
1629: *
]
|
| 53/2 |
2026/11/27 07:33 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 55090
output: 76
thoughts: 1327
Thoughts: **Debugging the `WARN_ON(pe->tgtport)` in `nvmet_fc_portentry_rebind_tgt`**
I was delving into the intricacies of the Fibre Channel loop (fcloop) and NVMe target drivers, specifically focusing on `drivers/scsi/lpfc/lpfc_nvmet.c`, `drivers/nvme/target/fcloop.c`, and `drivers/nvme/target/fc.c`. My primary objective was to understand why the `WARN_ON(pe->tgtport)` assertion was being triggered within the `nvmet_fc_portentry_rebind_tgt` function. This warning indicates a critical condition where a `portentry` (`pe`) already has an associated target port (`tgtport`) when it shouldn't.
To get to the bottom of this, I embarked on a meticulous trace, examining every conceivable scenario that could lead to `pe->tgtport` being non-NULL at the point of the `nvmet_fc_portentry_rebind_tgt` call. My initial hypothesis centered around concurrent operations attempting to create target ports for the same World Wide Node Name (WWNN) and World Wide Port Name (WWPN).
**Tracing a Concurrent `fcloop_create_target_port` Scenario:**
I simulated a race condition involving two threads simultaneously attempting to create a target port with identical WWNN and WWPN values (`0x1` and `0x1` in this example) via `/sys/class/fcloop/ctl/add_target_port`.
* **Thread 1:**
* Initiates `fcloop_create_target_port`.
* Calls `fcloop_alloc_nport`, which allocates a new `nport` structure. Crucially, at this stage, `newnport->tport` is `NULL`.
* It acquires `fcloop_lock`, checks for existing `nport`s using `__fcloop_nport_lookup` (which returns `NULL` since Thread 2 hasn't acted yet), adds the `newnport` to the `fcloop_nports` list, and releases the lock.
* It then calls `nvmet_fc_register_targetport`. This function allocates a new `tgtport` (`newrec`) and subsequently calls `nvmet_fc_portentry_rebind_tgt(newrec)`.
* Inside `nvmet_fc_portentry_rebind_tgt`, it looks up the `portentry` (`pe`) that was previously established when the NVMe port was added. At this point, `pe->tgtport` is `NULL`.
* The function then successfully sets `newrec->pe = pe` and `pe->tgtport = newrec`.
* **Meanwhile, Thread 2:**
* Also initiates `fcloop_create_target_port` with the same WWNN/WWPN.
* Calls `fcloop_alloc_nport`. This time, it acquires `fcloop_lock` and `__fcloop_nport_lookup` *finds* the `nport` created by Thread 1. The reference count of this `nport` is incremented.
* Here's the critical flaw I identified: The check `if ((!remoteport && nport->tport))` within `fcloop_alloc_nport` evaluates `nport->tport`. However, Thread 1 has *not yet completed* `nvmet_fc_register_targetport`, meaning `nport->tport` is *still NULL* at this precise moment. Thread 2's `newnport` is discarded, and the lock is released.
* Thread 2 proceeds to call `nvmet_fc_register_targetport`, allocating a *second* `tgtport` (`newrec2`).
* It then calls `nvmet_fc_portentry_rebind_tgt(newrec2)`.
* Again, it finds the *same* `pe` in the `nvmet_fc_portentry_list`.
* And now, `pe->tgtport` is *no longer NULL*; it's pointing to `newrec` from Thread 1's operation.
* This leads directly to the `WARN_ON(pe->tgtport)` at `drivers/nvme/target/fc.c:1327`.
**Unraveling the Root Cause:**
The core of the problem lies in the timing and locking around the assignment of `nport->tport` (and by extension, `nport->rport` for remote ports). The `fcloop_alloc_nport` function attempts to prevent duplicate target ports by checking `nport->tport`. However, `nport->tport` is only assigned *after* the entire `nvmet_fc_register_targetport()` function completes, and importantly, *outside* the protection of the `fcloop_lock`. This leaves a window for a race condition.
The same logic, or rather, lack thereof, applies to `fcloop_create_remote_port` and its associated `nport->rport` assignment.
Furthermore, my investigation revealed that `fcloop_create_local_port` doesn't even have a check for duplicate WWNN/WWPNs within `fcloop_lports`.
I've also considered other potential contributors, such as issues within `nvmet_fc_register_targetport` itself, or dependencies on functions like `ea96d6496ff5`, `475d0fe79551`, `d54a9d7f6d74`, or `fcloop_targetport_delete`. To confirm these suspicions and understand the historical context, I've been reviewing the `git log` for `fcloop.c` and `fc.c`, specifically searching for commits related to `fcloop`, `nport`, `tport`, `portentry`, `rebind`, and `targetport` to see if similar issues have been addressed before. My analysis strongly suggests the race condition in `fcloop_alloc_nport` related to the delayed `nport->tport` assignment is the primary culprit.
|
| 54/2 |
2026/11/27 07:34 |
tool |
git-log |
0m
Args: map[Count:30 MessageRegexps:[fcloop]]
Results: map[Output:ba98d6796d12 nvmet-fc: fix invalid free in LS IOD error path
dd677d059838 nvmet-fcloop: Check remoteport port_state before calling done callback
86ef6f7fc741 nvmet-fcloop: remove unused lsdir member.
f9929c518de8 nvmet-fcloop: check all request and response have been processed
10c165af35d2 nvmet-fcloop: call done callback even when remote port is gone
0164d1350a65 nvme-fc: do not reference lsrsp after failure
3466b7a6b713 nvmet-fcloop: don't wait for lport cleanup
d7f7c6eb809a nvmet-fcloop: add missing fcloop_callback_host_done
84eedced1c5b nvmet-fcloop: drop response if targetport is gone
772042dd38ee nvmet-fcloop: allocate/free fcloop_lsreq directly
2b559a3eb56b nvmet-fcloop: prevent double port deletion
47a827cd7929 nvmet-fcloop: access fcpreq only when holding reqlock
88ea8f814d8d nvmet-fcloop: update refs on tfcp_req
fbaed6a810a3 nvmet-fcloop: refactor fcloop_delete_local_port
d54a9d7f6d74 nvmet-fcloop: refactor fcloop_nport_alloc and track lport
b999efc8cf41 nvmet-fcloop: remove nport from list on last user
d6c40d87e7fe nvmet-fcloop: track ref counts for nports
72511b1dc414 nvmet-fcloop: add ref counting to lport
f22c458f9495 nvmet-fcloop: replace kref with refcount
2b5f0c5bc819 nvmet-fcloop: swap list_add_tail arguments
bbb443e99cd3 nvme-fcloop: implement 'host_traddr'
800bb2b02fb8 nvme: fcloop: make fcloop_class constant
dcfad4ab4d67 nvmet-fcloop: swap the list_add_tail arguments
41951f83ef90 nvmet: add module description to stop warnings
f644d21baab3 nvmet-fcloop: Remove remote port from list when unlinking
d97b4111b322 nvmet-fcloop: Do not wait on completion when unregister fails
e9227d486ede nvme-fcloop: no need to return from void function
4f86a6ff6fbd nvme-fcloop: fix "inconsistent {IN-HARDIRQ-W} -> {HARDIRQ-ON-W} usage"
2b54996b7d56 nvme-fcloop: Fix sscanf type and list_first_entry_or_null warnings
03d99e5d63da nvme-fcloop: add sysfs attribute to inject command drop
]
|
| 55/2 |
2026/11/27 07:34 |
tool |
git-log |
0m
Args: map[Count:30 MessageRegexps:[portentry|rebind|targetport] PathPrefix:drivers/nvme/target/]
Results:
|
| 56/2 |
2026/11/27 07:34 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 57337
output: 137
thoughts: 76
Thoughts: **My Thought Process on Refining Git Log Searches**
Alright, I've been diving into our `git log` history, specifically trying to pinpoint commits related to port entries and targets. My initial approach was to use `MessageRegexps` with distinct patterns, like `["portentry"]` and `["targetport"]`. However, I'm starting to suspect that my regex isn't quite hitting the mark.
I recall that `MessageRegexps` leverages `git grep` under the hood, and I need to be mindful of its default behavior. There's a strong possibility that it's defaulting to basic regular expressions, which means some of the more advanced syntax I might be implicitly expecting – like alternation (`|`) for combining `portentry` and `targetport` – might not be working as intended without explicit flags.
So, my current line of thinking is to first confirm this. I'll need to test the exact behavior of `MessageRegexps` with basic vs. extended regular expressions. If that's indeed the issue, I'll need to look into how to enable extended regex support within the `MessageRegexps` context, or adjust my patterns to be compatible with basic regex. This is crucial for ensuring I'm accurately capturing all relevant commits and not missing anything due to regex interpretation nuances.
|
| 57/2 |
2026/11/27 07:34 |
tool |
git-log |
0m
Args: map[Count:20 MessageRegexps:[targetport] PathPrefix:drivers/nvme/target/]
Results: map[Output:ba98d6796d12 nvmet-fc: fix invalid free in LS IOD error path
f9929c518de8 nvmet-fcloop: check all request and response have been processed
10c165af35d2 nvmet-fcloop: call done callback even when remote port is gone
bbccbf791e6f nvmet-fc: free pending reqs on tgtport unregister
84eedced1c5b nvmet-fcloop: drop response if targetport is gone
0acb8231ebae nvmet-fc: fix kernel-doc warning for nvmet_fc_unregister_targetport
b2fb99e42598 nvmet-fc: fix kernel-doc warning for nvmet_fc_register_targetport
437c0b824dbd nvme-fcloop: add target to host LS request support
58ab8ff9dca2 nvmet-fc: track hostport handle for associations
0191e7405b68 nvmet-fc: fix issues with targetport assoc_list list walking
ea96d6496ff5 nvmet_fc: support target port removal with nvmet layer
afd299ca9969 nvme-fcloop: Fix dropped LS's to removed target port
9ce1f2e12e01 nvmet-fc: cleanup nvmet add_port/remove_port
254beb84facc nvme-fcloop: avoid possible uninitialized variable warning
fddc9923c6d4 nvme-fcloop: fix port deletes and callbacks
ce79bfc2c7b4 nvme_fcloop: split job struct from transport for req_release
]
|
| 58/2 |
2026/11/27 07:34 |
tool |
git-log |
0m
Args: map[Count:20 MessageRegexps:[portentry] PathPrefix:drivers/nvme/target/]
Results: map[Output:596cba55adb4 nvmet-fc: take tgtport refs for portentry
]
|
| 59/2 |
2026/11/27 07:34 |
tool |
git-show |
0m
Args:
Results: map[Output:commit f644d21baab34c837d639e9639aa8204dba7f3cf
Author: Daniel Wagner <dwagner@suse.de>
Date: Mon Dec 18 16:30:53 2023 +0100
nvmet-fcloop: Remove remote port from list when unlinking
The remote port is removed too late from fcloop_nports list. Remove it
when port is unregistered.
This prevents a busy loop in fcloop_exit, because it is possible the
remote port is found in the list and thus we will never progress.
The kernel log will be spammed with
nvme_fcloop: fcloop_exit: Failed deleting remote port
nvme_fcloop: fcloop_exit: Failed deleting target port
Signed-off-by: Daniel Wagner <dwagner@suse.de>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Keith Busch <kbusch@kernel.org>
diff --git a/drivers/nvme/target/fcloop.c b/drivers/nvme/target/fcloop.c
index c65a73433c05..ead349af30f1 100644
--- a/drivers/nvme/target/fcloop.c
+++ b/drivers/nvme/target/fcloop.c
@@ -995,11 +995,6 @@ fcloop_nport_free(struct kref *ref)
{
struct fcloop_nport *nport =
container_of(ref, struct fcloop_nport, ref);
- unsigned long flags;
-
- spin_lock_irqsave(&fcloop_lock, flags);
- list_del(&nport->nport_list);
- spin_unlock_irqrestore(&fcloop_lock, flags);
kfree(nport);
}
@@ -1357,6 +1352,8 @@ __unlink_remote_port(struct fcloop_nport *nport)
nport->tport->remoteport = NULL;
nport->rport = NULL;
+ list_del(&nport->nport_list);
+
return rport;
}
]
|
| 60/2 |
2026/11/27 07:34 |
tool |
git-show |
0m
Args:
Results: map[Output:commit ba98d6796d12258e837ece065d2ecb59d76ce4ff
Author: Jiang HongHui <jiang_hh2019@163.com>
Date: Wed Jul 29 19:02:06 2026 +0800
nvmet-fc: fix invalid free in LS IOD error path
nvmet_fc_alloc_ls_iodlist() advances iod while initializing the LS IOD
array. If an rqstbuf allocation or response buffer DMA mapping fails,
the unwind loop decrements iod past the start of the array. The final
kfree(iod) therefore frees an address before the allocated object.
This can be reproduced with nvme-fcloop and failslab by setting
fail-nth to 6 before creating a target port. KASAN reports:
BUG: KASAN: invalid-free in nvmet_fc_register_targetport
Free of addr ffff88816cf8ff48 by task nvmet_fail_nth/9552
Free the original allocation base stored in tgtport->iod instead. With
this fix applied, the same sysfs write with fail-nth=6 returns -ENOMEM
without any KASAN report.
Fixes: c53432030d86 ("nvme-fabrics: Add target support for FC transport")
Cc: stable@vger.kernel.org
Reviewed-by: Maurizio Lombardi <mlombard@redhat.com>
Assisted-by: Codex:gpt-5
Signed-off-by: Jiang HongHui <jiang_hh2019@163.com>
Signed-off-by: Keith Busch <kbusch@kernel.org>
diff --git a/drivers/nvme/target/fc.c b/drivers/nvme/target/fc.c
index d161707559ce..1b557775e033 100644
--- a/drivers/nvme/target/fc.c
+++ b/drivers/nvme/target/fc.c
@@ -566,7 +566,7 @@ nvmet_fc_alloc_ls_iodlist(struct nvmet_fc_tgtport *tgtport)
list_del(&iod->ls_rcv_list);
}
- kfree(iod);
+ kfree(tgtport->iod);
return -EFAULT;
}
]
|
| 61/2 |
2026/11/27 07:34 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 59268
output: 49
thoughts: 388
Thoughts: **Eureka! A Critical Insight into the NVMe-FC Bug**
Wait a minute! I've just had a significant realization after re-examining those two commits. I need to be absolutely meticulous about where this bug might be lurking. The initial inclination might be to focus on the most recent commit, `ea96d6496ff5` ("nvmet_fc: support target port removal with nvmet layer" from 2018), given its focus on target port removal. However, I'm now intensely scrutinizing the possibility that the bug might originate or be exacerbated by the earlier commit, `475d0fe79551` ("nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME" from 2016), specifically within `fcloop.c`.
I need to perform a deep dive into the specifics of both. My suspicion is that the core issue might lie within the intricate workings of `fcloop.c`, potentially introduced or exposed by that 2016 commit. To that end, I'm going to meticulously review every single commit that has touched `fcloop.c` and `fc.c` from their inception to the present HEAD. This includes a granular examination of critical functions such as `fcloop_alloc_nport`, `fcloop_create_target_port`, `fcloop_delete_target_port`, and `fcloop_targetport_delete` within `fcloop.c`, as well as their counterparts in the `nvmet_fc` layer: `nvmet_fc_register_targetport`, `nvmet_fc_unregister_targetport`, and `nvmet_fc_portentry_rebind_tgt`.
My immediate next step is to specifically investigate `git show 475d0fe79551 -- drivers/nvme/target/fcloop.c`, paying extremely close attention to the code surrounding `fcloop_alloc_nport` and `fcloop_create_target_port` within that commit. It's vital to understand precisely what changes were made there and how they might interact with or precede the functionality introduced in the 2018 commit. I will not rest until I've exhaustively explored every avenue and definitively pinpointed the root cause, be it within these two commits or in another, less obvious one.
|
| 62/2 |
2026/11/27 07:34 |
tool |
git-show |
0m
Args: map[Commit:475d0fe79551 File:drivers/nvme/target/fcloop.c]
Results: map[Output:commit 475d0fe795516a9b9f286a851c3972fd8831c643
Author: James Smart <jsmart2021@gmail.com>
Date: Fri Dec 2 00:28:44 2016 -0800
nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME
Add FC LLDD loopback driver to test FC host and target transport within
nvme-fabrics
To aid in the development and testing of the lower-level api of the FC
transport, this loopback driver has been created to act as if it were a
FC hba driver supporting both the host interfaces as well as the target
interfaces with the nvme FC transport.
Signed-off-by: James Smart <james.smart@broadcom.com>
Reviewed-by: Jay Freyensee <james_p_freyensee@linux.intel.com>
Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: Christoph Hellwig <hch@lst.de>
diff --git a/drivers/nvme/target/fcloop.c b/drivers/nvme/target/fcloop.c
new file mode 100644
index 000000000000..bcb8ebeb01c5
--- /dev/null
+++ b/drivers/nvme/target/fcloop.c
@@ -0,0 +1,1148 @@
+/*
+ * Copyright (c) 2016 Avago Technologies. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful.
+ * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
+ * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
+ * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO
+ * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
+ * See the GNU General Public License for more details, a copy of which
+ * can be found in the file COPYING included with this package
+ */
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#include <linux/module.h>
+#include <linux/parser.h>
+#include <uapi/scsi/fc/fc_fs.h>
+
+#include "../host/nvme.h"
+#include "../target/nvmet.h"
+#include <linux/nvme-fc-driver.h>
+#include <linux/nvme-fc.h>
+
+
+enum {
+ NVMF_OPT_ERR = 0,
+ NVMF_OPT_WWNN = 1 << 0,
+ NVMF_OPT_WWPN = 1 << 1,
+ NVMF_OPT_ROLES = 1 << 2,
+ NVMF_OPT_FCADDR = 1 << 3,
+ NVMF_OPT_LPWWNN = 1 << 4,
+ NVMF_OPT_LPWWPN = 1 << 5,
+};
+
+struct fcloop_ctrl_options {
+ int mask;
+ u64 wwnn;
+ u64 wwpn;
+ u32 roles;
+ u32 fcaddr;
+ u64 lpwwnn;
+ u64 lpwwpn;
+};
+
+static const match_table_t opt_tokens = {
+ { NVMF_OPT_WWNN, "wwnn=%s" },
+ { NVMF_OPT_WWPN, "wwpn=%s" },
+ { NVMF_OPT_ROLES, "roles=%d" },
+ { NVMF_OPT_FCADDR, "fcaddr=%x" },
+ { NVMF_OPT_LPWWNN, "lpwwnn=%s" },
+ { NVMF_OPT_LPWWPN, "lpwwpn=%s" },
+ { NVMF_OPT_ERR, NULL }
+};
+
+static int
+fcloop_parse_options(struct fcloop_ctrl_options *opts,
+ const char *buf)
+{
+ substring_t args[MAX_OPT_ARGS];
+ char *options, *o, *p;
+ int token, ret = 0;
+ u64 token64;
+
+ options = o = kstrdup(buf, GFP_KERNEL);
+ if (!options)
+ return -ENOMEM;
+
+ while ((p = strsep(&o, ",\n")) != NULL) {
+ if (!*p)
+ continue;
+
+ token = match_token(p, opt_tokens, args);
+ opts->mask |= token;
+ switch (token) {
+ case NVMF_OPT_WWNN:
+ if (match_u64(args, &token64)) {
+ ret = -EINVAL;
+ goto out_free_options;
+ }
+ opts->wwnn = token64;
+ break;
+ case NVMF_OPT_WWPN:
+ if (match_u64(args, &token64)) {
+ ret = -EINVAL;
+ goto out_free_options;
+ }
+ opts->wwpn = token64;
+ break;
+ case NVMF_OPT_ROLES:
+ if (match_int(args, &token)) {
+ ret = -EINVAL;
+ goto out_free_options;
+ }
+ opts->roles = token;
+ break;
+ case NVMF_OPT_FCADDR:
+ if (match_hex(args, &token)) {
+ ret = -EINVAL;
+ goto out_free_options;
+ }
+ opts->fcaddr = token;
+ break;
+ case NVMF_OPT_LPWWNN:
+ if (match_u64(args, &token64)) {
+ ret = -EINVAL;
+ goto out_free_options;
+ }
+ opts->lpwwnn = token64;
+ break;
+ case NVMF_OPT_LPWWPN:
+ if (match_u64(args, &token64)) {
+ ret = -EINVAL;
+ goto out_free_options;
+ }
+ opts->lpwwpn = token64;
+ break;
+ default:
+ pr_warn("unknown parameter or missing value '%s'\n", p);
+ ret = -EINVAL;
+ goto out_free_options;
+ }
+ }
+
+out_free_options:
+ kfree(options);
+ return ret;
+}
+
+
+static int
+fcloop_parse_nm_options(struct device *dev, u64 *nname, u64 *pname,
+ const char *buf)
+{
+ substring_t args[MAX_OPT_ARGS];
+ char *options, *o, *p;
+ int token, ret = 0;
+ u64 token64;
+
+ *nname = -1;
+ *pname = -1;
+
+ options = o = kstrdup(buf, GFP_KERNEL);
+ if (!options)
+ return -ENOMEM;
+
+ while ((p = strsep(&o, ",\n")) != NULL) {
+ if (!*p)
+ continue;
+
+ token = match_token(p, opt_tokens, args);
+ switch (token) {
+ case NVMF_OPT_WWNN:
+ if (match_u64(args, &token64)) {
+ ret = -EINVAL;
+ goto out_free_options;
+ }
+ *nname = token64;
+ break;
+ case NVMF_OPT_WWPN:
+ if (match_u64(args, &token64)) {
+ ret = -EINVAL;
+ goto out_free_options;
+ }
+ *pname = token64;
+ break;
+ default:
+ pr_warn("unknown parameter or missing value '%s'\n", p);
+ ret = -EINVAL;
+ goto out_free_options;
+ }
+ }
+
+out_free_options:
+ kfree(options);
+
+ if (!ret) {
+ if (*nname == -1)
+ return -EINVAL;
+ if (*pname == -1)
+ return -EINVAL;
+ }
+
+ return ret;
+}
+
+
+#define LPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN)
+
+#define RPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN | \
+ NVMF_OPT_LPWWNN | NVMF_OPT_LPWWPN)
+
+#define TGTPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN)
+
+#define ALL_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN | NVMF_OPT_ROLES | \
+ NVMF_OPT_FCADDR | NVMF_OPT_LPWWNN | NVMF_OPT_LPWWPN)
+
+
+static DEFINE_SPINLOCK(fcloop_lock);
+static LIST_HEAD(fcloop_lports);
+static LIST_HEAD(fcloop_nports);
+
+struct fcloop_lport {
+ struct nvme_fc_local_port *localport;
+ struct list_head lport_list;
+ struct completion unreg_done;
+};
+
+struct fcloop_rport {
+ struct nvme_fc_remote_port *remoteport;
+ struct nvmet_fc_target_port *targetport;
+ struct fcloop_nport *nport;
+ struct fcloop_lport *lport;
+};
+
+struct fcloop_tport {
+ struct nvmet_fc_target_port *targetport;
+ struct nvme_fc_remote_port *remoteport;
+ struct fcloop_nport *nport;
+ struct fcloop_lport *lport;
+};
+
+struct fcloop_nport {
+ struct fcloop_rport *rport;
+ struct fcloop_tport *tport;
+ struct fcloop_lport *lport;
+ struct list_head nport_list;
+ struct kref ref;
+ struct completion rport_unreg_done;
+ struct completion tport_unreg_done;
+ u64 node_name;
+ u64 port_name;
+ u32 port_role;
+ u32 port_id;
+};
+
+struct fcloop_lsreq {
+ struct fcloop_tport *tport;
+ struct nvmefc_ls_req *lsreq;
+ struct work_struct work;
+ struct nvmefc_tgt_ls_req tgt_ls_req;
+ int status;
+};
+
+struct fcloop_fcpreq {
+ struct fcloop_tport *tport;
+ struct nvmefc_fcp_req *fcpreq;
+ u16 status;
+ struct work_struct work;
+ struct nvmefc_tgt_fcp_req tgt_fcp_req;
+};
+
+
+static inline struct fcloop_lsreq *
+tgt_ls_req_to_lsreq(struct nvmefc_tgt_ls_req *tgt_lsreq)
+{
+ return container_of(tgt_lsreq, struct fcloop_lsreq, tgt_ls_req);
+}
+
+static inline struct fcloop_fcpreq *
+tgt_fcp_req_to_fcpreq(struct nvmefc_tgt_fcp_req *tgt_fcpreq)
+{
+ return container_of(tgt_fcpreq, struct fcloop_fcpreq, tgt_fcp_req);
+}
+
+
+static int
+fcloop_create_queue(struct nvme_fc_local_port *localport,
+ unsigned int qidx, u16 qsize,
+ void **handle)
+{
+ *handle = localport;
+ return 0;
+}
+
+static void
+fcloop_delete_queue(struct nvme_fc_local_port *localport,
+ unsigned int idx, void *handle)
+{
+}
+
+
+/*
+ * Transmit of LS RSP done (e.g. buffers all set). call back up
+ * initiator "done" flows.
+ */
+static void
+fcloop_tgt_lsrqst_done_work(struct work_struct *work)
+{
+ struct fcloop_lsreq *tls_req =
+ container_of(work, struct fcloop_lsreq, work);
+ struct fcloop_tport *tport = tls_req->tport;
+ struct nvmefc_ls_req *lsreq = tls_req->lsreq;
+
+ if (tport->remoteport)
+ lsreq->done(lsreq, tls_req->status);
+}
+
+static int
+fcloop_ls_req(struct nvme_fc_local_port *localport,
+ struct nvme_fc_remote_port *remoteport,
+ struct nvmefc_ls_req *lsreq)
+{
+ struct fcloop_lsreq *tls_req = lsreq->private;
+ struct fcloop_rport *rport = remoteport->private;
+ int ret = 0;
+
+ tls_req->lsreq = lsreq;
+ INIT_WORK(&tls_req->work, fcloop_tgt_lsrqst_done_work);
+
+ if (!rport->targetport) {
+ tls_req->status = -ECONNREFUSED;
+ schedule_work(&tls_req->work);
+ return ret;
+ }
+
+ tls_req->status = 0;
+ tls_req->tport = rport->targetport->private;
+ ret = nvmet_fc_rcv_ls_req(rport->targetport, &tls_req->tgt_ls_req,
+ lsreq->rqstaddr, lsreq->rqstlen);
+
+ return ret;
+}
+
+static int
+fcloop_xmt_ls_rsp(struct nvmet_fc_target_port *tport,
+ struct nvmefc_tgt_ls_req *tgt_lsreq)
+{
+ struct fcloop_lsreq *tls_req = tgt_ls_req_to_lsreq(tgt_lsreq);
+ struct nvmefc_ls_req *lsreq = tls_req->lsreq;
+
+ memcpy(lsreq->rspaddr, tgt_lsreq->rspbuf,
+ ((lsreq->rsplen < tgt_lsreq->rsplen) ?
+ lsreq->rsplen : tgt_lsreq->rsplen));
+ tgt_lsreq->done(tgt_lsreq);
+
+ schedule_work(&tls_req->work);
+
+ return 0;
+}
+
+/*
+ * FCP IO operation done. call back up initiator "done" flows.
+ */
+static void
+fcloop_tgt_fcprqst_done_work(struct work_struct *work)
+{
+ struct fcloop_fcpreq *tfcp_req =
+ container_of(work, struct fcloop_fcpreq, work);
+ struct fcloop_tport *tport = tfcp_req->tport;
+ struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq;
+
+ if (tport->remoteport) {
+ fcpreq->status = tfcp_req->status;
+ fcpreq->done(fcpreq);
+ }
+}
+
+
+static int
+fcloop_fcp_req(struct nvme_fc_local_port *localport,
+ struct nvme_fc_remote_port *remoteport,
+ void *hw_queue_handle,
+ struct nvmefc_fcp_req *fcpreq)
+{
+ struct fcloop_fcpreq *tfcp_req = fcpreq->private;
+ struct fcloop_rport *rport = remoteport->private;
+ int ret = 0;
+
+ INIT_WORK(&tfcp_req->work, fcloop_tgt_fcprqst_done_work);
+
+ if (!rport->targetport) {
+ tfcp_req->status = NVME_SC_FC_TRANSPORT_ERROR;
+ schedule_work(&tfcp_req->work);
+ return ret;
+ }
+
+ tfcp_req->fcpreq = fcpreq;
+ tfcp_req->tport = rport->targetport->private;
+
+ ret = nvmet_fc_rcv_fcp_req(rport->targetport, &tfcp_req->tgt_fcp_req,
+ fcpreq->cmdaddr, fcpreq->cmdlen);
+
+ return ret;
+}
+
+static void
+fcloop_fcp_copy_data(u8 op, struct scatterlist *data_sg,
+ struct scatterlist *io_sg, u32 offset, u32 length)
+{
+ void *data_p, *io_p;
+ u32 data_len, io_len, tlen;
+
+ io_p = sg_virt(io_sg);
+ io_len = io_sg->length;
+
+ for ( ; offset; ) {
+ tlen = min_t(u32, offset, io_len);
+ offset -= tlen;
+ io_len -= tlen;
+ if (!io_len) {
+ io_sg = sg_next(io_sg);
+ io_p = sg_virt(io_sg);
+ io_len = io_sg->length;
+ } else
+ io_p += tlen;
+ }
+
+ data_p = sg_virt(data_sg);
+ data_len = data_sg->length;
+
+ for ( ; length; ) {
+ tlen = min_t(u32, io_len, data_len);
+ tlen = min_t(u32, tlen, length);
+
+ if (op == NVMET_FCOP_WRITEDATA)
+ memcpy(data_p, io_p, tlen);
+ else
+ memcpy(io_p, data_p, tlen);
+
+ length -= tlen;
+
+ io_len -= tlen;
+ if ((!io_len) && (length)) {
+ io_sg = sg_next(io_sg);
+ io_p = sg_virt(io_sg);
+ io_len = io_sg->length;
+ } else
+ io_p += tlen;
+
+ data_len -= tlen;
+ if ((!data_len) && (length)) {
+ data_sg = sg_next(data_sg);
+ data_p = sg_virt(data_sg);
+ data_len = data_sg->length;
+ } else
+ data_p += tlen;
+ }
+}
+
+static int
+fcloop_fcp_op(struct nvmet_fc_target_port *tgtport,
+ struct nvmefc_tgt_fcp_req *tgt_fcpreq)
+{
+ struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
+ struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq;
+ u32 rsplen = 0, xfrlen = 0;
+ int fcp_err = 0;
+ u8 op = tgt_fcpreq->op;
+
+ switch (op) {
+ case NVMET_FCOP_WRITEDATA:
+ xfrlen = tgt_fcpreq->transfer_length;
+ fcloop_fcp_copy_data(op, tgt_fcpreq->sg, fcpreq->first_sgl,
+ tgt_fcpreq->offset, xfrlen);
+ fcpreq->transferred_length += xfrlen;
+ break;
+
+ case NVMET_FCOP_READDATA:
+ case NVMET_FCOP_READDATA_RSP:
+ xfrlen = tgt_fcpreq->transfer_length;
+ fcloop_fcp_copy_data(op, tgt_fcpreq->sg, fcpreq->first_sgl,
+ tgt_fcpreq->offset, xfrlen);
+ fcpreq->transferred_length += xfrlen;
+ if (op == NVMET_FCOP_READDATA)
+ break;
+
+ /* Fall-Thru to RSP handling */
+
+ case NVMET_FCOP_RSP:
+ rsplen = ((fcpreq->rsplen < tgt_fcpreq->rsplen) ?
+ fcpreq->rsplen : tgt_fcpreq->rsplen);
+ memcpy(fcpreq->rspaddr, tgt_fcpreq->rspaddr, rsplen);
+ if (rsplen < tgt_fcpreq->rsplen)
+ fcp_err = -E2BIG;
+ fcpreq->rcv_rsplen = rsplen;
+ fcpreq->status = 0;
+ tfcp_req->status = 0;
+ break;
+
+ case NVMET_FCOP_ABORT:
+ tfcp_req->status = NVME_SC_FC_TRANSPORT_ABORTED;
+ break;
+
+ default:
+ fcp_err = -EINVAL;
+ break;
+ }
+
+ tgt_fcpreq->transferred_length = xfrlen;
+ tgt_fcpreq->fcp_error = fcp_err;
+ tgt_fcpreq->done(tgt_fcpreq);
+
+ if ((!fcp_err) && (op == NVMET_FCOP_RSP ||
+ op == NVMET_FCOP_READDATA_RSP ||
+ op == NVMET_FCOP_ABORT))
+ schedule_work(&tfcp_req->work);
+
+ return 0;
+}
+
+static void
+fcloop_ls_abort(struct nvme_fc_local_port *localport,
+ struct nvme_fc_remote_port *remoteport,
+ struct nvmefc_ls_req *lsreq)
+{
+}
+
+static void
+fcloop_fcp_abort(struct nvme_fc_local_port *localport,
+ struct nvme_fc_remote_port *remoteport,
+ void *hw_queue_handle,
+ struct nvmefc_fcp_req *fcpreq)
+{
+}
+
+static void
+fcloop_localport_delete(struct nvme_fc_local_port *localport)
+{
+ struct fcloop_lport *lport = localport->private;
+
+ /* release any threads waiting for the unreg to complete */
+ complete(&lport->unreg_done);
+}
+
+static void
+fcloop_remoteport_delete(struct nvme_fc_remote_port *remoteport)
+{
+ struct fcloop_rport *rport = remoteport->private;
+
+ /* release any threads waiting for the unreg to complete */
+ complete(&rport->nport->rport_unreg_done);
+}
+
+static void
+fcloop_targetport_delete(struct nvmet_fc_target_port *targetport)
+{
+ struct fcloop_tport *tport = targetport->private;
+
+ /* release any threads waiting for the unreg to complete */
+ complete(&tport->nport->tport_unreg_done);
+}
+
+#define FCLOOP_HW_QUEUES 4
+#define FCLOOP_SGL_SEGS 256
+#define FCLOOP_DMABOUND_4G 0xFFFFFFFF
+
+struct nvme_fc_port_template fctemplate = {
+ .localport_delete = fcloop_localport_delete,
+ .remoteport_delete = fcloop_remoteport_delete,
+ .create_queue = fcloop_create_queue,
+ .delete_queue = fcloop_delete_queue,
+ .ls_req = fcloop_ls_req,
+ .fcp_io = fcloop_fcp_req,
+ .ls_abort = fcloop_ls_abort,
+ .fcp_abort = fcloop_fcp_abort,
+ .max_hw_queues = FCLOOP_HW_QUEUES,
+ .max_sgl_segments = FCLOOP_SGL_SEGS,
+ .max_dif_sgl_segments = FCLOOP_SGL_SEGS,
+ .dma_boundary = FCLOOP_DMABOUND_4G,
+ /* sizes of additional private data for data structures */
+ .local_priv_sz = sizeof(struct fcloop_lport),
+ .remote_priv_sz = sizeof(struct fcloop_rport),
+ .lsrqst_priv_sz = sizeof(struct fcloop_lsreq),
+ .fcprqst_priv_sz = sizeof(struct fcloop_fcpreq),
+};
+
+struct nvmet_fc_target_template tgttemplate = {
+ .targetport_delete = fcloop_targetport_delete,
+ .xmt_ls_rsp = fcloop_xmt_ls_rsp,
+ .fcp_op = fcloop_fcp_op,
+ .max_hw_queues = FCLOOP_HW_QUEUES,
+ .max_sgl_segments = FCLOOP_SGL_SEGS,
+ .max_dif_sgl_segments = FCLOOP_SGL_SEGS,
+ .dma_boundary = FCLOOP_DMABOUND_4G,
+ /* optional features */
+ .target_features = NVMET_FCTGTFEAT_READDATA_RSP |
+ NVMET_FCTGTFEAT_NEEDS_CMD_CPUSCHED,
+ /* sizes of additional private data for data structures */
+ .target_priv_sz = sizeof(struct fcloop_tport),
+};
+
+static ssize_t
+fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct nvme_fc_port_info pinfo;
+ struct fcloop_ctrl_options *opts;
+ struct nvme_fc_local_port *localport;
+ struct fcloop_lport *lport;
+ int ret;
+
+ opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+ if (!opts)
+ return -ENOMEM;
+
+ ret = fcloop_parse_options(opts, buf);
+ if (ret)
+ goto out_free_opts;
+
+ /* everything there ? */
+ if ((opts->mask & LPORT_OPTS) != LPORT_OPTS) {
+ ret = -EINVAL;
+ goto out_free_opts;
+ }
+
+ pinfo.node_name = opts->wwnn;
+ pinfo.port_name = opts->wwpn;
+ pinfo.port_role = opts->roles;
+ pinfo.port_id = opts->fcaddr;
+
+ ret = nvme_fc_register_localport(&pinfo, &fctemplate, NULL, &localport);
+ if (!ret) {
+ unsigned long flags;
+
+ /* success */
+ lport = localport->private;
+ lport->localport = localport;
+ INIT_LIST_HEAD(&lport->lport_list);
+
+ spin_lock_irqsave(&fcloop_lock, flags);
+ list_add_tail(&lport->lport_list, &fcloop_lports);
+ spin_unlock_irqrestore(&fcloop_lock, flags);
+
+ /* mark all of the input buffer consumed */
+ ret = count;
+ }
+
+out_free_opts:
+ kfree(opts);
+ return ret ? ret : count;
+}
+
+
+static void
+__unlink_local_port(struct fcloop_lport *lport)
+{
+ list_del(&lport->lport_list);
+}
+
+static int
+__wait_localport_unreg(struct fcloop_lport *lport)
+{
+ int ret;
+
+ init_completion(&lport->unreg_done);
+
+ ret = nvme_fc_unregister_localport(lport->localport);
+
+ wait_for_completion(&lport->unreg_done);
+
+ return ret;
+}
+
+
+static ssize_t
+fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct fcloop_lport *tlport, *lport = NULL;
+ u64 nodename, portname;
+ unsigned long flags;
+ int ret;
+
+ ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
+ if (ret)
+ return ret;
+
+ spin_lock_irqsave(&fcloop_lock, flags);
+
+ list_for_each_entry(tlport, &fcloop_lports, lport_list) {
+ if (tlport->localport->node_name == nodename &&
+ tlport->localport->port_name == portname) {
+ lport = tlport;
+ __unlink_local_port(lport);
+ break;
+ }
+ }
+ spin_unlock_irqrestore(&fcloop_lock, flags);
+
+ if (!lport)
+ return -ENOENT;
+
+ ret = __wait_localport_unreg(lport);
+
+ return ret ? ret : count;
+}
+
+static void
+fcloop_nport_free(struct kref *ref)
+{
+ struct fcloop_nport *nport =
+ container_of(ref, struct fcloop_nport, ref);
+ unsigned long flags;
+
+ spin_lock_irqsave(&fcloop_lock, flags);
+ list_del(&nport->nport_list);
+ spin_unlock_irqrestore(&fcloop_lock, flags);
+
+ kfree(nport);
+}
+
+static void
+fcloop_nport_put(struct fcloop_nport *nport)
+{
+ kref_put(&nport->ref, fcloop_nport_free);
+}
+
+static int
+fcloop_nport_get(struct fcloop_nport *nport)
+{
+ return kref_get_unless_zero(&nport->ref);
+}
+
+static struct fcloop_nport *
+fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
+{
+ struct fcloop_nport *newnport, *nport = NULL;
+ struct fcloop_lport *tmplport, *lport = NULL;
+ struct fcloop_ctrl_options *opts;
+ unsigned long flags;
+ u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS;
+ int ret;
+
+ opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+ if (!opts)
+ return NULL;
+
+ ret = fcloop_parse_options(opts, buf);
+ if (ret)
+ goto out_free_opts;
+
+ /* everything there ? */
+ if ((opts->mask & opts_mask) != opts_mask) {
+ ret = -EINVAL;
+ goto out_free_opts;
+ }
+
+ newnport = kzalloc(sizeof(*newnport), GFP_KERNEL);
+ if (!newnport)
+ goto out_free_opts;
+
+ INIT_LIST_HEAD(&newnport->nport_list);
+ newnport->node_name = opts->wwnn;
+ newnport->port_name = opts->wwpn;
+ if (opts->mask & NVMF_OPT_ROLES)
+ newnport->port_role = opts->roles;
+ if (opts->mask & NVMF_OPT_FCADDR)
+ newnport->port_id = opts->fcaddr;
+ kref_init(&newnport->ref);
+
+ spin_lock_irqsave(&fcloop_lock, flags);
+
+ list_for_each_entry(tmplport, &fcloop_lports, lport_list) {
+ if (tmplport->localport->node_name == opts->wwnn &&
+ tmplport->localport->port_name == opts->wwpn)
+ goto out_invalid_opts;
+
+ if (tmplport->localport->node_name == opts->lpwwnn &&
+ tmplport->localport->port_name == opts->lpwwpn)
+ lport = tmplport;
+ }
+
+ if (remoteport) {
+ if (!lport)
+ goto out_invalid_opts;
+ newnport->lport = lport;
+ }
+
+ list_for_each_entry(nport, &fcloop_nports, nport_list) {
+ if (nport->node_name == opts->wwnn &&
+ nport->port_name == opts->wwpn) {
+ if ((remoteport && nport->rport) ||
+ (!remoteport && nport->tport)) {
+ nport = NULL;
+ goto out_invalid_opts;
+ }
+
+ fcloop_nport_get(nport);
+
+ spin_unlock_irqrestore(&fcloop_lock, flags);
+
+ if (remoteport)
+ nport->lport = lport;
+ if (opts->mask & NVMF_OPT_ROLES)
+ nport->port_role = opts->roles;
+ if (opts->mask & NVMF_OPT_FCADDR)
+ nport->port_id = opts->fcaddr;
+ goto out_free_newnport;
+ }
+ }
+
+ list_add_tail(&newnport->nport_list, &fcloop_nports);
+
+ spin_unlock_irqrestore(&fcloop_lock, flags);
+
+ kfree(opts);
+ return newnport;
+
+out_invalid_opts:
+ spin_unlock_irqrestore(&fcloop_lock, flags);
+out_free_newnport:
+ kfree(newnport);
+out_free_opts:
+ kfree(opts);
+ return nport;
+}
+
+static ssize_t
+fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct nvme_fc_remote_port *remoteport;
+ struct fcloop_nport *nport;
+ struct fcloop_rport *rport;
+ struct nvme_fc_port_info pinfo;
+ int ret;
+
+ nport = fcloop_alloc_nport(buf, count, true);
+ if (!nport)
+ return -EIO;
+
+ pinfo.node_name = nport->node_name;
+ pinfo.port_name = nport->port_name;
+ pinfo.port_role = nport->port_role;
+ pinfo.port_id = nport->port_id;
+
+ ret = nvme_fc_register_remoteport(nport->lport->localport,
+ &pinfo, &remoteport);
+ if (ret || !remoteport) {
+ fcloop_nport_put(nport);
+ return ret;
+ }
+
+ /* success */
+ rport = remoteport->private;
+ rport->remoteport = remoteport;
+ rport->targetport = (nport->tport) ? nport->tport->targetport : NULL;
+ if (nport->tport) {
+ nport->tport->remoteport = remoteport;
+ nport->tport->lport = nport->lport;
+ }
+ rport->nport = nport;
+ rport->lport = nport->lport;
+ nport->rport = rport;
+
+ return ret ? ret : count;
+}
+
+
+static struct fcloop_rport *
+__unlink_remote_port(struct fcloop_nport *nport)
+{
+ struct fcloop_rport *rport = nport->rport;
+
+ if (rport && nport->tport)
+ nport->tport->remoteport = NULL;
+ nport->rport = NULL;
+
+ return rport;
+}
+
+static int
+__wait_remoteport_unreg(struct fcloop_nport *nport, struct fcloop_rport *rport)
+{
+ int ret;
+
+ if (!rport)
+ return -EALREADY;
+
+ init_completion(&nport->rport_unreg_done);
+
+ ret = nvme_fc_unregister_remoteport(rport->remoteport);
+ if (ret)
+ return ret;
+
+ wait_for_completion(&nport->rport_unreg_done);
+
+ fcloop_nport_put(nport);
+
+ return ret;
+}
+
+static ssize_t
+fcloop_delete_remote_port(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct fcloop_nport *nport = NULL, *tmpport;
+ static struct fcloop_rport *rport;
+ u64 nodename, portname;
+ unsigned long flags;
+ int ret;
+
+ ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
+ if (ret)
+ return ret;
+
+ spin_lock_irqsave(&fcloop_lock, flags);
+
+ list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
+ if (tmpport->node_name == nodename &&
+ tmpport->port_name == portname && tmpport->rport) {
+ nport = tmpport;
+ rport = __unlink_remote_port(nport);
+ break;
+ }
+ }
+
+ spin_unlock_irqrestore(&fcloop_lock, flags);
+
+ if (!nport)
+ return -ENOENT;
+
+ ret = __wait_remoteport_unreg(nport, rport);
+
+ return ret ? ret : count;
+}
+
+static ssize_t
+fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct nvmet_fc_target_port *targetport;
+ struct fcloop_nport *nport;
+ struct fcloop_tport *tport;
+ struct nvmet_fc_port_info tinfo;
+ int ret;
+
+ nport = fcloop_alloc_nport(buf, count, false);
+ if (!nport)
+ return -EIO;
+
+ tinfo.node_name = nport->node_name;
+ tinfo.port_name = nport->port_name;
+ tinfo.port_id = nport->port_id;
+
+ ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
+ &targetport);
+ if (ret) {
+ fcloop_nport_put(nport);
+ return ret;
+ }
+
+ /* success */
+ tport = targetport->private;
+ tport->targetport = targetport;
+ tport->remoteport = (nport->rport) ? nport->rport->remoteport : NULL;
+ if (nport->rport)
+ nport->rport->targetport = targetport;
+ tport->nport = nport;
+ tport->lport = nport->lport;
+ nport->tport = tport;
+
+ return ret ? ret : count;
+}
+
+
+static struct fcloop_tport *
+__unlink_target_port(struct fcloop_nport *nport)
+{
+ struct fcloop_tport *tport = nport->tport;
+
+ if (tport && nport->rport)
+ nport->rport->targetport = NULL;
+ nport->tport = NULL;
+
+ return tport;
+}
+
+static int
+__wait_targetport_unreg(struct fcloop_nport *nport, struct fcloop_tport *tport)
+{
+ int ret;
+
[Output truncated: showing 1000 of 1173 lines]]
|
| 63/2 |
2026/11/27 07:34 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 70383
output: 94
thoughts: 1388
Thoughts: **Summary of Debugging Thoughts**
Okay, I've been digging into this bug, and it's getting quite intricate, involving interactions between `475d0fe79551` and `ea96d6496ff5`, specifically within the `fcloop_create_target_port` and `nvmet_fc_register_targetport` functions. The core issue seems to stem from a race condition during concurrent target port creation after an initial port has been deleted.
The reproducer's sequence of steps is crucial. It highlights a dependency: steps 1-4 *must* occur before step 5, the concurrent creation. Let's break down what's happening:
1. We start by creating an initial target port.
2. Then, an NVMe-oF port is configured for FC.
3. A subsystem is created and linked to this port, triggering `nvmet_fc_add_port` implicitly when the port is enabled.
4. Crucially, the *initial* target port is deleted. This process calls `del_target_port`, which leads to `nvmet_fc_unregister_targetport` and then `nvmet_fc_portentry_unbind_tgt`. The `pe` (port entry) remains in the `nvmet_fc_portentry_list`, but its `tgtport` pointer is set to `NULL`.
5. Now, concurrently, new target ports are created (let's call them `t1` and `t2`). Both of these calls eventually lead to `add_target_port`.
This is where the problems arise, and I'm seeing two potential areas of concern:
**Question 1: Is `fcloop.c` (`fcloop_alloc_nport` / `fcloop_create_target_port`) at fault?**
Looking at `fcloop_create_target_port`, it calls `fcloop_alloc_nport`. This function, critically, drops the `fcloop_lock` while `nport->tport` is still `NULL`. During this window, the `nport` is left on the `fcloop_nports` list. If another thread simultaneously calls `fcloop_create_target_port` with the same WWNN/WWPN, `fcloop_alloc_nport` finds the existing `nport` (still with `tport == NULL`) and returns it.
Both threads then proceed to call `nvmet_fc_register_targetport` with identical node and port names. Even without the `WARN_ON(pe->tgtport)` check, the race continues:
* Thread 1 sets `tport1->nport = nport; nport->tport = tport1;`. The `nport` refcount is now 2 (one from the initial `newnport` and one from Thread 2's lookup).
* Thread 2 then sets `tport2->nport = nport; nport->tport = tport2;`. This overwrites `nport->tport`, effectively orphaning `tport1`. `tport1` is now leaked and unreachable from `nport`.
* The `nvmet_fc_target_list` now contains *two* `nvmet_fc_tgtport` structures for the same WWNN/WWPN.
* When `del_target_port` is eventually called, only `tport2` is unregistered. `tport1` is leaked permanently. This also leads to issues on `rmmod nvme-fcloop` as `nport` still has a refcount from `tport1`, preventing proper cleanup and potentially causing hangs or leaks.
The fundamental flaw in `fcloop_alloc_nport` seems to be that it checks `if (!remoteport && nport->tport)` *before* `nport->tport` is set and *after* releasing the lock, thus failing to prevent this double allocation.
**Question 2: Or is it a bug in `fc.c` (`nvmet_fc_register_targetport` / `nvmet_fc_portentry_rebind_tgt`)?**
I'm also looking at `nvmet_fc_register_targetport` in `drivers/nvme/target/fc.c`. This is an exported API, intended for FC LLDDs. My key observation here is that it doesn't seem to check if a target port with the same `(node_name, port_name)` is *already* registered in `nvmet_fc_target_list`.
The relevant code snippet shows:
nvmet_fc_portentry_rebind_tgt(newrec);
spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
list_add_tail(&newrec->tgt_list, &nvmet_fc_target_list);
spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
This simply adds the new record to the list.
This brings me to `ea96d6496ff5` and its inclusion of `WARN_ON(pe->tgtport)` in `nvmet_fc_portentry_rebind_tgt`. This warning is present because, in the FC world, a `(node_name, port_name)` pair is supposed to uniquely identify a port. An LLDD should unregister an old target port before registering a new one with the same WWNs. The `WARN_ON(pe->tgtport)` is there to enforce this invariant: a port entry (`pe`) shouldn't already have an active `tgtport` bound to it when a new `tgtport` with the same identity is registered.
So, is `nvmet_fc_register_targetport` *expected* to reject these duplicate `(node_name, port_name)` registrations? I need to cross-reference its documentation and how host-side FC drivers (`drivers/nvme/host/fc.c`) handle duplicate registrations via `nvme_fc_register_localport` and `nvme_fc_register_remoteport` to understand the expected behavior.
It seems like the problem might be a combination: `fcloop` allows the race to create duplicate `nport` structures with `tport` unset, and then `nvmet_fc_register_targetport` doesn't adequately guard against registering multiple `tgtport` structures for the same identity.
|
| 64/2 |
2026/11/27 07:34 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/nvme/host/fc.c Name:nvme_fc_register_localport]
Results: map[SourceCode: 329: /**
330: * nvme_fc_register_localport - transport entry point called by an
331: * LLDD to register the existence of a NVME
332: * host FC port.
333: * @pinfo: pointer to information about the port to be registered
334: * @template: LLDD entrypoints and operational parameters for the port
335: * @dev: physical hardware device node port corresponds to. Will be
336: * used for DMA mappings
337: * @portptr: pointer to a local port pointer. Upon success, the routine
338: * will allocate a nvme_fc_local_port structure and place its
339: * address in the local port pointer. Upon failure, local port
340: * pointer will be set to 0.
341: *
342: * Returns:
343: * a completion status. Must be 0 upon success; a negative errno
344: * (ex: -ENXIO) upon failure.
345: */
346: int
347: nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
348: struct nvme_fc_port_template *template,
349: struct device *dev,
350: struct nvme_fc_local_port **portptr)
351: {
352: struct nvme_fc_lport *newrec;
353: unsigned long flags;
354: int ret, idx;
355:
356: if (!template->localport_delete || !template->remoteport_delete ||
357: !template->ls_req || !template->fcp_io ||
358: !template->ls_abort || !template->fcp_abort ||
359: !template->max_hw_queues || !template->max_sgl_segments ||
360: !template->max_dif_sgl_segments || !template->dma_boundary) {
361: ret = -EINVAL;
362: goto out_reghost_failed;
363: }
364:
365: /*
366: * look to see if there is already a localport that had been
367: * deregistered and in the process of waiting for all the
368: * references to fully be removed. If the references haven't
369: * expired, we can simply re-enable the localport. Remoteports
370: * and controller reconnections should resume naturally.
371: */
372: newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
373:
374: /* found an lport, but something about its state is bad */
375: if (IS_ERR(newrec)) {
376: ret = PTR_ERR(newrec);
377: goto out_reghost_failed;
378:
379: /* found existing lport, which was resumed */
380: } else if (newrec) {
381: *portptr = &newrec->localport;
382: return 0;
383: }
384:
385: /* nothing found - allocate a new localport struct */
386:
387: newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
388: GFP_KERNEL);
389: if (!newrec) {
390: ret = -ENOMEM;
391: goto out_reghost_failed;
392: }
393:
394: idx = ida_alloc(&nvme_fc_local_port_cnt, GFP_KERNEL);
395: if (idx < 0) {
396: ret = -ENOSPC;
397: goto out_fail_kfree;
398: }
399:
400: if (!get_device(dev) && dev) {
401: ret = -ENODEV;
402: goto out_ida_put;
403: }
404:
405: INIT_LIST_HEAD(&newrec->port_list);
406: INIT_LIST_HEAD(&newrec->endp_list);
407: kref_init(&newrec->ref);
408: atomic_set(&newrec->act_rport_cnt, 0);
409: newrec->ops = template;
410: newrec->dev = dev;
411: ida_init(&newrec->endp_cnt);
412: if (template->local_priv_sz)
413: newrec->localport.private = &newrec[1];
414: else
415: newrec->localport.private = NULL;
416: newrec->localport.node_name = pinfo->node_name;
417: newrec->localport.port_name = pinfo->port_name;
418: newrec->localport.port_role = pinfo->port_role;
419: newrec->localport.port_id = pinfo->port_id;
420: newrec->localport.port_state = FC_OBJSTATE_ONLINE;
421: newrec->localport.port_num = idx;
422:
423: spin_lock_irqsave(&nvme_fc_lock, flags);
424: list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
425: spin_unlock_irqrestore(&nvme_fc_lock, flags);
426:
427: if (dev)
428: dma_set_seg_boundary(dev, template->dma_boundary);
429:
430: *portptr = &newrec->localport;
431: return 0;
432:
433: out_ida_put:
434: ida_free(&nvme_fc_local_port_cnt, idx);
435: out_fail_kfree:
436: kfree(newrec);
437: out_reghost_failed:
438: *portptr = NULL;
439:
440: return ret;
441: }
SourceFile:drivers/nvme/host/fc.c]
|
| 65/2 |
2026/11/27 07:34 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/nvme/host/fc.c Name:nvme_fc_register_remoteport]
Results: map[SourceCode: 649: /**
650: * nvme_fc_register_remoteport - transport entry point called by an
651: * LLDD to register the existence of a NVME
652: * subsystem FC port on its fabric.
653: * @localport: pointer to the (registered) local port that the remote
654: * subsystem port is connected to.
655: * @pinfo: pointer to information about the port to be registered
656: * @portptr: pointer to a remote port pointer. Upon success, the routine
657: * will allocate a nvme_fc_remote_port structure and place its
658: * address in the remote port pointer. Upon failure, remote port
659: * pointer will be set to 0.
660: *
661: * Returns:
662: * a completion status. Must be 0 upon success; a negative errno
663: * (ex: -ENXIO) upon failure.
664: */
665: int
666: nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
667: struct nvme_fc_port_info *pinfo,
668: struct nvme_fc_remote_port **portptr)
669: {
670: struct nvme_fc_lport *lport = localport_to_lport(localport);
671: struct nvme_fc_rport *newrec;
672: unsigned long flags;
673: int ret, idx;
674:
675: if (!nvme_fc_lport_get(lport)) {
676: ret = -ESHUTDOWN;
677: goto out_reghost_failed;
678: }
679:
680: /*
681: * look to see if there is already a remoteport that is waiting
682: * for a reconnect (within dev_loss_tmo) with the same WWN's.
683: * If so, transition to it and reconnect.
684: */
685: newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
686:
687: /* found an rport, but something about its state is bad */
688: if (IS_ERR(newrec)) {
689: ret = PTR_ERR(newrec);
690: goto out_lport_put;
691:
692: /* found existing rport, which was resumed */
693: } else if (newrec) {
694: nvme_fc_lport_put(lport);
695: __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
696: nvme_fc_signal_discovery_scan(lport, newrec);
697: *portptr = &newrec->remoteport;
698: return 0;
699: }
700:
701: /* nothing found - allocate a new remoteport struct */
702:
703: newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
704: GFP_KERNEL);
705: if (!newrec) {
706: ret = -ENOMEM;
707: goto out_lport_put;
708: }
709:
710: idx = ida_alloc(&lport->endp_cnt, GFP_KERNEL);
711: if (idx < 0) {
712: ret = -ENOSPC;
713: goto out_kfree_rport;
714: }
715:
716: INIT_LIST_HEAD(&newrec->endp_list);
717: INIT_LIST_HEAD(&newrec->ctrl_list);
718: INIT_LIST_HEAD(&newrec->ls_req_list);
719: INIT_LIST_HEAD(&newrec->disc_list);
720: kref_init(&newrec->ref);
721: atomic_set(&newrec->act_ctrl_cnt, 0);
722: spin_lock_init(&newrec->lock);
723: newrec->remoteport.localport = &lport->localport;
724: INIT_LIST_HEAD(&newrec->ls_rcv_list);
725: newrec->dev = lport->dev;
726: newrec->lport = lport;
727: if (lport->ops->remote_priv_sz)
728: newrec->remoteport.private = &newrec[1];
729: else
730: newrec->remoteport.private = NULL;
731: newrec->remoteport.port_role = pinfo->port_role;
732: newrec->remoteport.node_name = pinfo->node_name;
733: newrec->remoteport.port_name = pinfo->port_name;
734: newrec->remoteport.port_id = pinfo->port_id;
735: newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
736: newrec->remoteport.port_num = idx;
737: __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
738: INIT_WORK(&newrec->lsrcv_work, nvme_fc_handle_ls_rqst_work);
739:
740: spin_lock_irqsave(&nvme_fc_lock, flags);
741: list_add_tail(&newrec->endp_list, &lport->endp_list);
742: spin_unlock_irqrestore(&nvme_fc_lock, flags);
743:
744: nvme_fc_signal_discovery_scan(lport, newrec);
745:
746: *portptr = &newrec->remoteport;
747: return 0;
748:
749: out_kfree_rport:
750: kfree(newrec);
751: out_lport_put:
752: nvme_fc_lport_put(lport);
753: out_reghost_failed:
754: *portptr = NULL;
755: return ret;
756: }
SourceFile:drivers/nvme/host/fc.c]
|
| 66/2 |
2026/11/27 07:35 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 75549
output: 99
thoughts: 4743
Thoughts: **My Thought Process on the NVMe-FC Target Port Duplication Bug**
This is quite the rabbit hole, and I've been digging into the specifics of how the NVMe-FC target port management works, particularly within the `fcloop` driver. My primary focus has been to understand *why* it's possible to register duplicate target ports with the same `(node_name, port_name)` and precisely where the race condition lies.
Initially, I looked at the core registration functions: `nvmet_fc_register_targetport`, `nvme_fc_register_localport`, and `nvme_fc_register_remoteport`. My initial thought was that these functions themselves might be missing a uniqueness check at the transport layer. However, I quickly realized that the responsibility for port lifecycle management is delegated to the Lower Level Driver (LLDD). So, I pivoted to examine the LLDDs.
I also checked `nvme_fc_attach_to_unreg_lport` and `nvme_fc_attach_to_suspended_rport` to ensure they weren't inadvertently creating duplicates, but my analysis showed they only handle specific attachment states, not the core registration uniqueness.
The real key started to emerge when I dove into `fcloop.c`. I found `fcloop_alloc_nport` and noticed its check:
nport = __fcloop_nport_lookup(opts->wwnn, opts->wwpn);
if (nport) {
if ((remoteport && nport->rport) ||
(!remoteport && nport->tport)) {
/* invalid configuration */
goto out_put_nport;
}
The intent here, as evidenced by the author's comments and the check itself, seems clear: `fcloop_alloc_nport` is *designed* to prevent the creation of two `tport`s (or two `rport`s) for the same `(wwnn, wwpn)`. Specifically, if an `nport` already exists and we're trying to create a *target* port (`!remoteport`) but `nport->tport` is already set, it should reject the operation.
This led me to the critical question: why isn't this check preventing the race condition when two threads concurrently call `fcloop_create_target_port` with identical `(wwnn, wwpn)`? I've meticulously traced this, and here's how I see the race unfold:
1. **Thread 1 calls `fcloop_alloc_nport`**: It allocates a `newnport`. Crucially, it adds `newnport` to the `fcloop_nports` list with `nport->tport` still being `NULL`. It then *unlocks* `fcloop_lock`.
2. **Timing is everything**: Before Thread 1 can call `nvmet_fc_register_targetport` and subsequently set `nport->tport = tport` (which happens *after* the `nvmet_fc_register_targetport` call in `fcloop_create_target_port`), the `nport->tport` remains `NULL`.
3. **Thread 2 enters the fray**: Thread 2 also calls `fcloop_alloc_nport`. It finds the *existing* `nport` (added by Thread 1) in `fcloop_nports`. It then performs its check: `!remoteport && nport->tport`. Because `nport->tport` is *still NULL*, Thread 2 incorrectly concludes that no target port is associated with this `nport` and proceeds to return the *same* `nport` that Thread 1 is working with.
4. **The collision**: Now, both Thread 1 and Thread 2 proceed to call `nvmet_fc_register_targetport` with the *identical* `(node_name, port_name)`. This leads to both calls entering `nvmet_fc_portentry_rebind_tgt`.
* Thread 1 successfully binds its `tgtport1` by setting `pe->tgtport = tgtport1`.
* Thread 2 then encounters `pe->tgtport != NULL`. This triggers the `WARN_ON(pe->tgtport)` assertion, indicating an unexpected state.
* Worse, Thread 2 *overwrites* `pe->tgtport = tgtport2` and later `nport->tport = tport2`. This results in the first `tgtport` and `tport` being leaked because they are no longer referenced correctly and cannot be properly unregistered.
This sequence of events clearly points to a Time-of-Check to Time-of-Use (TOCTOU) race condition within `fcloop_alloc_nport` and `fcloop_create_target_port`, where the check for `nport->tport` is performed before the actual registration and setting of `nport->tport` is completed, and the lock is released prematurely.
My subsequent investigation involved trying to pinpoint *when* this bug was introduced. I examined several commits: `475d0fe79551` (the initial FC LLDD loopback driver), `d54a9d7f6d74` (a refactoring), and `ea96d6496ff5` (target port removal support).
Comparing `d54a9d7f6d74` with `475d0fe79551`, I concluded that the refactoring in `d54a9d7f6d74` did *not* introduce this bug; it preserved the existing race.
The real question became whether `475d0fe79551` or `ea96d6496ff5` was the culprit. I considered the instruction *not* to blame commits that merely added debugging checks.
When I looked at `475d0fe79551` in isolation, I saw that the race condition in `fcloop_create_target_port` (specifically the `fcloop_alloc_nport` part) would lead to duplicate target ports being registered. Before `ea96d6496ff5`, this would result in `tgtport1` being leaked and unregisterable.
Then, commit `ea96d6496ff5` introduced `nvmet_fc_portentry_rebind_tgt` with the `WARN_ON(pe->tgtport)`. This assertion *caught* the symptom of the older race condition from `475d0fe79551`. It didn't *introduce* the ability to create duplicates; it simply made the consequence of the existing race visible.
I also examined the possibility of a race between `nvmet_fc_unregister_targetport` and `nvmet_fc_register_targetport`. I found that in `fcloop_delete_target_port` (pre-`d54a9d7f6d74`), `__unlink_target_port` would set `nport->tport = NULL` under the `fcloop_lock`, release the lock, and *then* call `nvmet_fc_unregister_targetport`. This critical window allowed a concurrent `fcloop_create_target_port` to enter `fcloop_alloc_nport`, find `nport->tport == NULL`, and then proceed to call `nvmet_fc_register_targetport` before Thread 1 had fully unregistered the old target port.
Therefore, my conclusion is that the fundamental race condition exists within the `fcloop.c` implementation itself, specifically in how `fcloop_create_target_port` and `fcloop_alloc_nport` manage the `nport->tport` state relative to the `fcloop_lock` and the subsequent call to `nvmet_fc_register_targetport`. The commit `475d0fe79551` initially introduced this flawed logic. Subsequent commits, like `ea96d6496ff5`, added checks that exposed this older bug, but did not create the underlying vulnerability. The refactoring in `d54a9d7f6d74` didn't fix or break it, just rearranged some code.
The core issue is that `fcloop.c` does not properly serialize or track the `tport` registration/unregistration state under `fcloop_lock` (or a suitable mutex) across the calls to `nvmet_fc_register_targetport` and `nvmet_fc_unregister_targetport`. This allows for concurrent operations to create overlapping states where a port is considered "available" by one thread while it's in the process of being registered or unregistered by another.
|
| 67/2 |
2026/11/27 07:35 |
tool |
grepper |
0m
Args: map[Expression:lpfc_nvmet_create_targetport|lpfc_nvmet_destroy_targetport PathPrefixes:[drivers/scsi/lpfc/]]
Results: map[Output:drivers/scsi/lpfc/lpfc_crtn.h=626=void lpfc_nvme_update_localport(struct lpfc_vport *vport);
drivers/scsi/lpfc/lpfc_crtn.h:627:int lpfc_nvmet_create_targetport(struct lpfc_hba *phba);
drivers/scsi/lpfc/lpfc_crtn.h-628-int lpfc_nvmet_update_targetport(struct lpfc_hba *phba);
drivers/scsi/lpfc/lpfc_crtn.h:629:void lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba);
drivers/scsi/lpfc/lpfc_crtn.h-630-int lpfc_nvme_handle_lsreq(struct lpfc_hba *phba,
--
drivers/scsi/lpfc/lpfc_init.c=3894=lpfc_offline(struct lpfc_hba *phba)
--
drivers/scsi/lpfc/lpfc_init.c-3908- */
drivers/scsi/lpfc/lpfc_init.c:3909: lpfc_nvmet_destroy_targetport(phba);
drivers/scsi/lpfc/lpfc_init.c-3910- lpfc_nvme_destroy_localport(phba->pport);
--
drivers/scsi/lpfc/lpfc_init.c=14897=lpfc_pci_remove_one_s4(struct pci_dev *pdev)
--
drivers/scsi/lpfc/lpfc_init.c-14929- lpfc_cleanup(vport);
drivers/scsi/lpfc/lpfc_init.c:14930: lpfc_nvmet_destroy_targetport(phba);
drivers/scsi/lpfc/lpfc_init.c-14931- lpfc_nvme_destroy_localport(vport);
--
drivers/scsi/lpfc/lpfc_nvmet.c=1643=int
drivers/scsi/lpfc/lpfc_nvmet.c:1644:lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
drivers/scsi/lpfc/lpfc_nvmet.c-1645-{
--
drivers/scsi/lpfc/lpfc_nvmet.c=2103=void
drivers/scsi/lpfc/lpfc_nvmet.c:2104:lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
drivers/scsi/lpfc/lpfc_nvmet.c-2105-{
--
drivers/scsi/lpfc/lpfc_sli.c=8501=lpfc_sli4_hba_setup(struct lpfc_hba *phba)
--
drivers/scsi/lpfc/lpfc_sli.c-8957- if (phba->nvmet_support)
drivers/scsi/lpfc/lpfc_sli.c:8958: lpfc_nvmet_create_targetport(phba);
drivers/scsi/lpfc/lpfc_sli.c-8959-
]
|
| 68/2 |
2026/11/27 07:35 |
tool |
read-file |
0m
Args: map[File:drivers/scsi/lpfc/lpfc_nvmet.c FirstLine:1700 LineCount:65]
Results: map[Contents:1700: phba->targetport, tgtp,
1701: pinfo.port_name, pinfo.node_name,
1702: lpfc_tgttemplate.max_sgl_segments,
1703: lpfc_tgttemplate.max_hw_queues);
1704:
1705: atomic_set(&tgtp->rcv_ls_req_in, 0);
1706: atomic_set(&tgtp->rcv_ls_req_out, 0);
1707: atomic_set(&tgtp->rcv_ls_req_drop, 0);
1708: atomic_set(&tgtp->xmt_ls_abort, 0);
1709: atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1710: atomic_set(&tgtp->xmt_ls_rsp, 0);
1711: atomic_set(&tgtp->xmt_ls_drop, 0);
1712: atomic_set(&tgtp->xmt_ls_rsp_error, 0);
1713: atomic_set(&tgtp->xmt_ls_rsp_xb_set, 0);
1714: atomic_set(&tgtp->xmt_ls_rsp_aborted, 0);
1715: atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1716: atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1717: atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1718: atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1719: atomic_set(&tgtp->xmt_fcp_drop, 0);
1720: atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1721: atomic_set(&tgtp->xmt_fcp_read, 0);
1722: atomic_set(&tgtp->xmt_fcp_write, 0);
1723: atomic_set(&tgtp->xmt_fcp_rsp, 0);
1724: atomic_set(&tgtp->xmt_fcp_release, 0);
1725: atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
1726: atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
1727: atomic_set(&tgtp->xmt_fcp_rsp_xb_set, 0);
1728: atomic_set(&tgtp->xmt_fcp_rsp_aborted, 0);
1729: atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
1730: atomic_set(&tgtp->xmt_fcp_xri_abort_cqe, 0);
1731: atomic_set(&tgtp->xmt_fcp_abort, 0);
1732: atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
1733: atomic_set(&tgtp->xmt_abort_unsol, 0);
1734: atomic_set(&tgtp->xmt_abort_sol, 0);
1735: atomic_set(&tgtp->xmt_abort_rsp, 0);
1736: atomic_set(&tgtp->xmt_abort_rsp_error, 0);
1737: atomic_set(&tgtp->defer_ctx, 0);
1738: atomic_set(&tgtp->defer_fod, 0);
1739: atomic_set(&tgtp->defer_wqfull, 0);
1740: }
1741: return error;
1742: }
1743:
1744: int
1745: lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
1746: {
1747: struct lpfc_vport *vport = phba->pport;
1748:
1749: if (!phba->targetport)
1750: return 0;
1751:
1752: lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
1753: "6007 Update NVMET port x%px did x%x\n",
1754: phba->targetport, vport->fc_myDID);
1755:
1756: phba->targetport->port_id = vport->fc_myDID;
1757: return 0;
1758: }
1759:
1760: /**
1761: * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
1762: * @phba: pointer to lpfc hba data structure.
1763: * @axri: pointer to the nvmet xri abort wcqe structure.
1764: *
]
|
| 69/2 |
2026/11/27 07:35 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 82575
output: 81
thoughts: 1296
Thoughts: **My Thought Process on the FC LLD Loopback Driver and NVMe-FC Target Port Handling**
I've been carefully examining the behavior of the FC LLD loopback driver (`fcloop`) and its interaction with the NVMe-FC target layer, specifically focusing on how target ports are created and managed. My initial thoughts were prompted by a comparison of two commits, `475d0fe795516a9b9f286a851c3972fd8831c643` and `ea96d6496ff59b2b26dc9e13dc8f57d77731eb37`.
I first considered the general behavior of the `lpfc` driver. In `lpfc`, the `lpfc_nvmet_create_targetport` function is intended to be called only once per HBA probe during `lpfc_sli4_hba_setup`. Correspondingly, `lpfc_nvmet_destroy_targetport` is called on HBA offline or removal. This suggests a more controlled, single-instance lifecycle for target ports in the `lpfc` driver.
However, the `fcloop` driver presents a different scenario. Here, `fcloop_create_target_port` is exposed via sysfs. This means userspace can invoke it concurrently from multiple threads, and crucially, *all these concurrent calls can use the SAME (wwnn, wwpn) pair*. This immediately raised a red flag regarding potential race conditions and duplicate registrations.
Now, let's dive into the specific commits.
**Analysis of Commit `475d0fe795516a9b9f286a851c3972fd8831c643` ("nvme-fabrics: Add FC LLD loopback driver to test FC-NVME"):**
I meticulously reviewed what this commit introduces. It implements `fcloop_alloc_nport` and `fcloop_create_target_port`. The critical observation lies within `fcloop_alloc_nport`. It performs a lookup for an `nport` within `fcloop_nports` under `fcloop_lock`. If an `nport` is found and it's not a remote port (`!remoteport`) and it already has a valid target port (`nport->tport`), the function rejects the request. Otherwise, it returns an existing `nport` or a newly added one, and then unlocks `fcloop_lock`.
The problem emerges in `fcloop_create_target_port`. This function calls `nvmet_fc_register_targetport()` *without holding any locks*. Only *after* this registration succeeds does it set `nport->tport = tport`. Because `nport->tport` is not set (and no other state or mutex is held) during the execution of `nvmet_fc_register_targetport()`, two concurrent calls to `fcloop_create_target_port` with the same WWNN/WWPN can both pass the `fcloop_alloc_nport` check.
The consequence of this race, *without commit `ea96d6496ff5`*, is severe:
1. Both threads would successfully call `nvmet_fc_register_targetport`, leading to the registration of two distinct `nvmet_fc_tgtport` objects with the identical WWNN/WWPN in `nvmet_fc_target_list`.
2. Subsequently, both threads would attempt to set `tport->nport = nport` and `nport->tport = tport`. The second thread's action would *overwrite* `nport->tport`, effectively leaking the first `tport` and its associated `nvmet_fc_tgtport` object.
3. When `fcloop_delete_target_port` is eventually called, only the target port created by the second thread would be unregistered. The `nport` object would still have a reference count that prevents its proper cleanup. In the specific context of `475d0fe79551`, the `fcloop_nport_put` is called only once, meaning the `nport` is never freed, and the first `tgtport` is never unregistered.
**Analysis of Commit `ea96d6496ff59b2b26dc9e13dc8f57d77731eb37` ("nvmet_fc: support target port removal with nvmet layer"):**
I then considered how `ea96d6496ff5` affects this. My understanding is that `ea96d6496ff5` *introduced* `nvmet_fc_portentry_rebind_tgt` which contains `WARN_ON(pe->tgtport);`. This `WARN_ON` triggers if a target port is being registered with the same WWNN/WWPN as an already existing `pe` (port entry) in the `nvmet_fc_target_list`. So, when the race condition in `fcloop` occurs *after* an NVMe port has already been configured and its previous target port deleted, this `WARN_ON(pe->tgtport)` condition is met, and the warning is triggered.
My core question became: did `ea96d6496ff5` *introduce* a bug, or did it *expose* an existing race condition within `fcloop.c` by adding this check? Based on my analysis, it appears to be the latter – it highlights the problem rather than creating it.
**Considering Alternative Fixes:**
I also contemplated whether the fix could or should reside elsewhere, specifically in the FC transport layer (`fc.c`). I considered the possibility that `nvmet_fc_register_targetport` itself could be modified.
The idea is: what if `nvmet_fc_register_targetport` proactively checked the `nvmet_fc_target_list` for an existing `tgtport` with the same `node_name` and `port_name`? If a duplicate was found, the function could then return an appropriate error code like `-EEXIST` or `-EBUSY`.
This led me to a further question: If `nvmet_fc_register_targetport` is *intended* to reject duplicate (node_name, port_name) registrations, why wasn't this check implemented in the earlier commit `c53432030d86` ("nvme-fabrics: Add target support for FC transport")? I would need to examine the code introduced by `c53432030d86`, specifically looking at `nvmet_fc_register_targetport` within that commit, to understand its original design and whether it implicitly allowed or overlooked such duplicates.
In essence, I'm tracing the lifecycle of NVMe-FC target ports within `fcloop` and `nvmet_fc`, understanding the race condition introduced by concurrent sysfs access in `fcloop`, and evaluating how commit `ea96d6496ff5` surfaces this issue rather than creating it. I'm also exploring whether a more fundamental fix belongs in the core NVMe-FC target registration logic.
|
| 70/2 |
2026/11/27 07:35 |
tool |
git-show |
0m
Args: map[Commit:c53432030d86 Stat:true]
Results: map[Output:commit c53432030d86429dc9fe5adc3d68cb9d1343b0b2
Author: James Smart <jsmart2021@gmail.com>
Date: Fri Dec 2 00:28:43 2016 -0800
nvme-fabrics: Add target support for FC transport
Implements the FC-NVME T11 definition of how nvme fabric capsules are
performed on an FC fabric. Utilizes a lower-layer API to FC host adapters
to send/receive FC-4 LS operations and perform the FCP transactions
necessary to perform and FCP IO request for NVME.
The T11 definitions for FC-4 Link Services are implemented which create
NVMeOF connections. Implements the hooks with nvmet layer to pass NVME
commands to it for processing and posting of data/response base to the
host via the different connections.
Signed-off-by: James Smart <james.smart@broadcom.com>
Reviewed-by: Jay Freyensee <james_p_freyensee@linux.intel.com>
Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: Christoph Hellwig <hch@lst.de>
MAINTAINERS | 1 +
drivers/nvme/target/Kconfig | 11 +
drivers/nvme/target/Makefile | 2 +
drivers/nvme/target/fc.c | 2288 ++++++++++++++++++++++++++++++++++++++++++
4 files changed, 2302 insertions(+)
]
|
| 71/2 |
2026/11/27 07:35 |
tool |
git-show |
0m
Args: map[Commit:c53432030d86 File:drivers/nvme/target/fc.c]
Results: map[Output:commit c53432030d86429dc9fe5adc3d68cb9d1343b0b2
Author: James Smart <jsmart2021@gmail.com>
Date: Fri Dec 2 00:28:43 2016 -0800
nvme-fabrics: Add target support for FC transport
Implements the FC-NVME T11 definition of how nvme fabric capsules are
performed on an FC fabric. Utilizes a lower-layer API to FC host adapters
to send/receive FC-4 LS operations and perform the FCP transactions
necessary to perform and FCP IO request for NVME.
The T11 definitions for FC-4 Link Services are implemented which create
NVMeOF connections. Implements the hooks with nvmet layer to pass NVME
commands to it for processing and posting of data/response base to the
host via the different connections.
Signed-off-by: James Smart <james.smart@broadcom.com>
Reviewed-by: Jay Freyensee <james_p_freyensee@linux.intel.com>
Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: Christoph Hellwig <hch@lst.de>
diff --git a/drivers/nvme/target/fc.c b/drivers/nvme/target/fc.c
new file mode 100644
index 000000000000..173e842f19c9
--- /dev/null
+++ b/drivers/nvme/target/fc.c
@@ -0,0 +1,2288 @@
+/*
+ * Copyright (c) 2016 Avago Technologies. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful.
+ * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
+ * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
+ * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO
+ * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
+ * See the GNU General Public License for more details, a copy of which
+ * can be found in the file COPYING included with this package
+ *
+ */
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/blk-mq.h>
+#include <linux/parser.h>
+#include <linux/random.h>
+#include <uapi/scsi/fc/fc_fs.h>
+#include <uapi/scsi/fc/fc_els.h>
+
+#include "nvmet.h"
+#include <linux/nvme-fc-driver.h>
+#include <linux/nvme-fc.h>
+
+
+/* *************************** Data Structures/Defines ****************** */
+
+
+#define NVMET_LS_CTX_COUNT 4
+
+/* for this implementation, assume small single frame rqst/rsp */
+#define NVME_FC_MAX_LS_BUFFER_SIZE 2048
+
+struct nvmet_fc_tgtport;
+struct nvmet_fc_tgt_assoc;
+
+struct nvmet_fc_ls_iod {
+ struct nvmefc_tgt_ls_req *lsreq;
+ struct nvmefc_tgt_fcp_req *fcpreq; /* only if RS */
+
+ struct list_head ls_list; /* tgtport->ls_list */
+
+ struct nvmet_fc_tgtport *tgtport;
+ struct nvmet_fc_tgt_assoc *assoc;
+
+ u8 *rqstbuf;
+ u8 *rspbuf;
+ u16 rqstdatalen;
+ dma_addr_t rspdma;
+
+ struct scatterlist sg[2];
+
+ struct work_struct work;
+} __aligned(sizeof(unsigned long long));
+
+#define NVMET_FC_MAX_KB_PER_XFR 256
+
+enum nvmet_fcp_datadir {
+ NVMET_FCP_NODATA,
+ NVMET_FCP_WRITE,
+ NVMET_FCP_READ,
+ NVMET_FCP_ABORTED,
+};
+
+struct nvmet_fc_fcp_iod {
+ struct nvmefc_tgt_fcp_req *fcpreq;
+
+ struct nvme_fc_cmd_iu cmdiubuf;
+ struct nvme_fc_ersp_iu rspiubuf;
+ dma_addr_t rspdma;
+ struct scatterlist *data_sg;
+ struct scatterlist *next_sg;
+ int data_sg_cnt;
+ u32 next_sg_offset;
+ u32 total_length;
+ u32 offset;
+ enum nvmet_fcp_datadir io_dir;
+ bool active;
+ bool abort;
+ spinlock_t flock;
+
+ struct nvmet_req req;
+ struct work_struct work;
+
+ struct nvmet_fc_tgtport *tgtport;
+ struct nvmet_fc_tgt_queue *queue;
+
+ struct list_head fcp_list; /* tgtport->fcp_list */
+};
+
+struct nvmet_fc_tgtport {
+
+ struct nvmet_fc_target_port fc_target_port;
+
+ struct list_head tgt_list; /* nvmet_fc_target_list */
+ struct device *dev; /* dev for dma mapping */
+ struct nvmet_fc_target_template *ops;
+
+ struct nvmet_fc_ls_iod *iod;
+ spinlock_t lock;
+ struct list_head ls_list;
+ struct list_head ls_busylist;
+ struct list_head assoc_list;
+ struct ida assoc_cnt;
+ struct nvmet_port *port;
+ struct kref ref;
+};
+
+struct nvmet_fc_tgt_queue {
+ bool ninetypercent;
+ u16 qid;
+ u16 sqsize;
+ u16 ersp_ratio;
+ u16 sqhd;
+ int cpu;
+ atomic_t connected;
+ atomic_t sqtail;
+ atomic_t zrspcnt;
+ atomic_t rsn;
+ spinlock_t qlock;
+ struct nvmet_port *port;
+ struct nvmet_cq nvme_cq;
+ struct nvmet_sq nvme_sq;
+ struct nvmet_fc_tgt_assoc *assoc;
+ struct nvmet_fc_fcp_iod *fod; /* array of fcp_iods */
+ struct list_head fod_list;
+ struct workqueue_struct *work_q;
+ struct kref ref;
+} __aligned(sizeof(unsigned long long));
+
+struct nvmet_fc_tgt_assoc {
+ u64 association_id;
+ u32 a_id;
+ struct nvmet_fc_tgtport *tgtport;
+ struct list_head a_list;
+ struct nvmet_fc_tgt_queue *queues[NVMET_NR_QUEUES];
+ struct kref ref;
+};
+
+
+static inline int
+nvmet_fc_iodnum(struct nvmet_fc_ls_iod *iodptr)
+{
+ return (iodptr - iodptr->tgtport->iod);
+}
+
+static inline int
+nvmet_fc_fodnum(struct nvmet_fc_fcp_iod *fodptr)
+{
+ return (fodptr - fodptr->queue->fod);
+}
+
+
+/*
+ * Association and Connection IDs:
+ *
+ * Association ID will have random number in upper 6 bytes and zero
+ * in lower 2 bytes
+ *
+ * Connection IDs will be Association ID with QID or'd in lower 2 bytes
+ *
+ * note: Association ID = Connection ID for queue 0
+ */
+#define BYTES_FOR_QID sizeof(u16)
+#define BYTES_FOR_QID_SHIFT (BYTES_FOR_QID * 8)
+#define NVMET_FC_QUEUEID_MASK ((u64)((1 << BYTES_FOR_QID_SHIFT) - 1))
+
+static inline u64
+nvmet_fc_makeconnid(struct nvmet_fc_tgt_assoc *assoc, u16 qid)
+{
+ return (assoc->association_id | qid);
+}
+
+static inline u64
+nvmet_fc_getassociationid(u64 connectionid)
+{
+ return connectionid & ~NVMET_FC_QUEUEID_MASK;
+}
+
+static inline u16
+nvmet_fc_getqueueid(u64 connectionid)
+{
+ return (u16)(connectionid & NVMET_FC_QUEUEID_MASK);
+}
+
+static inline struct nvmet_fc_tgtport *
+targetport_to_tgtport(struct nvmet_fc_target_port *targetport)
+{
+ return container_of(targetport, struct nvmet_fc_tgtport,
+ fc_target_port);
+}
+
+static inline struct nvmet_fc_fcp_iod *
+nvmet_req_to_fod(struct nvmet_req *nvme_req)
+{
+ return container_of(nvme_req, struct nvmet_fc_fcp_iod, req);
+}
+
+
+/* *************************** Globals **************************** */
+
+
+static DEFINE_SPINLOCK(nvmet_fc_tgtlock);
+
+static LIST_HEAD(nvmet_fc_target_list);
+static DEFINE_IDA(nvmet_fc_tgtport_cnt);
+
+
+static void nvmet_fc_handle_ls_rqst_work(struct work_struct *work);
+static void nvmet_fc_handle_fcp_rqst_work(struct work_struct *work);
+static void nvmet_fc_tgt_a_put(struct nvmet_fc_tgt_assoc *assoc);
+static int nvmet_fc_tgt_a_get(struct nvmet_fc_tgt_assoc *assoc);
+static void nvmet_fc_tgt_q_put(struct nvmet_fc_tgt_queue *queue);
+static int nvmet_fc_tgt_q_get(struct nvmet_fc_tgt_queue *queue);
+static void nvmet_fc_tgtport_put(struct nvmet_fc_tgtport *tgtport);
+static int nvmet_fc_tgtport_get(struct nvmet_fc_tgtport *tgtport);
+
+
+/* *********************** FC-NVME DMA Handling **************************** */
+
+/*
+ * The fcloop device passes in a NULL device pointer. Real LLD's will
+ * pass in a valid device pointer. If NULL is passed to the dma mapping
+ * routines, depending on the platform, it may or may not succeed, and
+ * may crash.
+ *
+ * As such:
+ * Wrapper all the dma routines and check the dev pointer.
+ *
+ * If simple mappings (return just a dma address, we'll noop them,
+ * returning a dma address of 0.
+ *
+ * On more complex mappings (dma_map_sg), a pseudo routine fills
+ * in the scatter list, setting all dma addresses to 0.
+ */
+
+static inline dma_addr_t
+fc_dma_map_single(struct device *dev, void *ptr, size_t size,
+ enum dma_data_direction dir)
+{
+ return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
+}
+
+static inline int
+fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
+{
+ return dev ? dma_mapping_error(dev, dma_addr) : 0;
+}
+
+static inline void
+fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
+ enum dma_data_direction dir)
+{
+ if (dev)
+ dma_unmap_single(dev, addr, size, dir);
+}
+
+static inline void
+fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
+ enum dma_data_direction dir)
+{
+ if (dev)
+ dma_sync_single_for_cpu(dev, addr, size, dir);
+}
+
+static inline void
+fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
+ enum dma_data_direction dir)
+{
+ if (dev)
+ dma_sync_single_for_device(dev, addr, size, dir);
+}
+
+/* pseudo dma_map_sg call */
+static int
+fc_map_sg(struct scatterlist *sg, int nents)
+{
+ struct scatterlist *s;
+ int i;
+
+ WARN_ON(nents == 0 || sg[0].length == 0);
+
+ for_each_sg(sg, s, nents, i) {
+ s->dma_address = 0L;
+#ifdef CONFIG_NEED_SG_DMA_LENGTH
+ s->dma_length = s->length;
+#endif
+ }
+ return nents;
+}
+
+static inline int
+fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
+ enum dma_data_direction dir)
+{
+ return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
+}
+
+static inline void
+fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
+ enum dma_data_direction dir)
+{
+ if (dev)
+ dma_unmap_sg(dev, sg, nents, dir);
+}
+
+
+/* *********************** FC-NVME Port Management ************************ */
+
+
+static int
+nvmet_fc_alloc_ls_iodlist(struct nvmet_fc_tgtport *tgtport)
+{
+ struct nvmet_fc_ls_iod *iod;
+ int i;
+
+ iod = kcalloc(NVMET_LS_CTX_COUNT, sizeof(struct nvmet_fc_ls_iod),
+ GFP_KERNEL);
+ if (!iod)
+ return -ENOMEM;
+
+ tgtport->iod = iod;
+
+ for (i = 0; i < NVMET_LS_CTX_COUNT; iod++, i++) {
+ INIT_WORK(&iod->work, nvmet_fc_handle_ls_rqst_work);
+ iod->tgtport = tgtport;
+ list_add_tail(&iod->ls_list, &tgtport->ls_list);
+
+ iod->rqstbuf = kcalloc(2, NVME_FC_MAX_LS_BUFFER_SIZE,
+ GFP_KERNEL);
+ if (!iod->rqstbuf)
+ goto out_fail;
+
+ iod->rspbuf = iod->rqstbuf + NVME_FC_MAX_LS_BUFFER_SIZE;
+
+ iod->rspdma = fc_dma_map_single(tgtport->dev, iod->rspbuf,
+ NVME_FC_MAX_LS_BUFFER_SIZE,
+ DMA_TO_DEVICE);
+ if (fc_dma_mapping_error(tgtport->dev, iod->rspdma))
+ goto out_fail;
+ }
+
+ return 0;
+
+out_fail:
+ kfree(iod->rqstbuf);
+ list_del(&iod->ls_list);
+ for (iod--, i--; i >= 0; iod--, i--) {
+ fc_dma_unmap_single(tgtport->dev, iod->rspdma,
+ NVME_FC_MAX_LS_BUFFER_SIZE, DMA_TO_DEVICE);
+ kfree(iod->rqstbuf);
+ list_del(&iod->ls_list);
+ }
+
+ kfree(iod);
+
+ return -EFAULT;
+}
+
+static void
+nvmet_fc_free_ls_iodlist(struct nvmet_fc_tgtport *tgtport)
+{
+ struct nvmet_fc_ls_iod *iod = tgtport->iod;
+ int i;
+
+ for (i = 0; i < NVMET_LS_CTX_COUNT; iod++, i++) {
+ fc_dma_unmap_single(tgtport->dev,
+ iod->rspdma, NVME_FC_MAX_LS_BUFFER_SIZE,
+ DMA_TO_DEVICE);
+ kfree(iod->rqstbuf);
+ list_del(&iod->ls_list);
+ }
+ kfree(tgtport->iod);
+}
+
+static struct nvmet_fc_ls_iod *
+nvmet_fc_alloc_ls_iod(struct nvmet_fc_tgtport *tgtport)
+{
+ static struct nvmet_fc_ls_iod *iod;
+ unsigned long flags;
+
+ spin_lock_irqsave(&tgtport->lock, flags);
+ iod = list_first_entry_or_null(&tgtport->ls_list,
+ struct nvmet_fc_ls_iod, ls_list);
+ if (iod)
+ list_move_tail(&iod->ls_list, &tgtport->ls_busylist);
+ spin_unlock_irqrestore(&tgtport->lock, flags);
+ return iod;
+}
+
+
+static void
+nvmet_fc_free_ls_iod(struct nvmet_fc_tgtport *tgtport,
+ struct nvmet_fc_ls_iod *iod)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&tgtport->lock, flags);
+ list_move(&iod->ls_list, &tgtport->ls_list);
+ spin_unlock_irqrestore(&tgtport->lock, flags);
+}
+
+static void
+nvmet_fc_prep_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,
+ struct nvmet_fc_tgt_queue *queue)
+{
+ struct nvmet_fc_fcp_iod *fod = queue->fod;
+ int i;
+
+ for (i = 0; i < queue->sqsize; fod++, i++) {
+ INIT_WORK(&fod->work, nvmet_fc_handle_fcp_rqst_work);
+ fod->tgtport = tgtport;
+ fod->queue = queue;
+ fod->active = false;
+ list_add_tail(&fod->fcp_list, &queue->fod_list);
+ spin_lock_init(&fod->flock);
+
+ fod->rspdma = fc_dma_map_single(tgtport->dev, &fod->rspiubuf,
+ sizeof(fod->rspiubuf), DMA_TO_DEVICE);
+ if (fc_dma_mapping_error(tgtport->dev, fod->rspdma)) {
+ list_del(&fod->fcp_list);
+ for (fod--, i--; i >= 0; fod--, i--) {
+ fc_dma_unmap_single(tgtport->dev, fod->rspdma,
+ sizeof(fod->rspiubuf),
+ DMA_TO_DEVICE);
+ fod->rspdma = 0L;
+ list_del(&fod->fcp_list);
+ }
+
+ return;
+ }
+ }
+}
+
+static void
+nvmet_fc_destroy_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,
+ struct nvmet_fc_tgt_queue *queue)
+{
+ struct nvmet_fc_fcp_iod *fod = queue->fod;
+ int i;
+
+ for (i = 0; i < queue->sqsize; fod++, i++) {
+ if (fod->rspdma)
+ fc_dma_unmap_single(tgtport->dev, fod->rspdma,
+ sizeof(fod->rspiubuf), DMA_TO_DEVICE);
+ }
+}
+
+static struct nvmet_fc_fcp_iod *
+nvmet_fc_alloc_fcp_iod(struct nvmet_fc_tgt_queue *queue)
+{
+ static struct nvmet_fc_fcp_iod *fod;
+ unsigned long flags;
+
+ spin_lock_irqsave(&queue->qlock, flags);
+ fod = list_first_entry_or_null(&queue->fod_list,
+ struct nvmet_fc_fcp_iod, fcp_list);
+ if (fod) {
+ list_del(&fod->fcp_list);
+ fod->active = true;
+ fod->abort = false;
+ /*
+ * no queue reference is taken, as it was taken by the
+ * queue lookup just prior to the allocation. The iod
+ * will "inherit" that reference.
+ */
+ }
+ spin_unlock_irqrestore(&queue->qlock, flags);
+ return fod;
+}
+
+
+static void
+nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
+ struct nvmet_fc_fcp_iod *fod)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&queue->qlock, flags);
+ list_add_tail(&fod->fcp_list, &fod->queue->fod_list);
+ fod->active = false;
+ spin_unlock_irqrestore(&queue->qlock, flags);
+
+ /*
+ * release the reference taken at queue lookup and fod allocation
+ */
+ nvmet_fc_tgt_q_put(queue);
+}
+
+static int
+nvmet_fc_queue_to_cpu(struct nvmet_fc_tgtport *tgtport, int qid)
+{
+ int cpu, idx, cnt;
+
+ if (!(tgtport->ops->target_features &
+ NVMET_FCTGTFEAT_NEEDS_CMD_CPUSCHED) ||
+ tgtport->ops->max_hw_queues == 1)
+ return WORK_CPU_UNBOUND;
+
+ /* Simple cpu selection based on qid modulo active cpu count */
+ idx = !qid ? 0 : (qid - 1) % num_active_cpus();
+
+ /* find the n'th active cpu */
+ for (cpu = 0, cnt = 0; ; ) {
+ if (cpu_active(cpu)) {
+ if (cnt == idx)
+ break;
+ cnt++;
+ }
+ cpu = (cpu + 1) % num_possible_cpus();
+ }
+
+ return cpu;
+}
+
+static struct nvmet_fc_tgt_queue *
+nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
+ u16 qid, u16 sqsize)
+{
+ struct nvmet_fc_tgt_queue *queue;
+ unsigned long flags;
+ int ret;
+
+ if (qid >= NVMET_NR_QUEUES)
+ return NULL;
+
+ queue = kzalloc((sizeof(*queue) +
+ (sizeof(struct nvmet_fc_fcp_iod) * sqsize)),
+ GFP_KERNEL);
+ if (!queue)
+ return NULL;
+
+ if (!nvmet_fc_tgt_a_get(assoc))
+ goto out_free_queue;
+
+ queue->work_q = alloc_workqueue("ntfc%d.%d.%d", 0, 0,
+ assoc->tgtport->fc_target_port.port_num,
+ assoc->a_id, qid);
+ if (!queue->work_q)
+ goto out_a_put;
+
+ queue->fod = (struct nvmet_fc_fcp_iod *)&queue[1];
+ queue->qid = qid;
+ queue->sqsize = sqsize;
+ queue->assoc = assoc;
+ queue->port = assoc->tgtport->port;
+ queue->cpu = nvmet_fc_queue_to_cpu(assoc->tgtport, qid);
+ INIT_LIST_HEAD(&queue->fod_list);
+ atomic_set(&queue->connected, 0);
+ atomic_set(&queue->sqtail, 0);
+ atomic_set(&queue->rsn, 1);
+ atomic_set(&queue->zrspcnt, 0);
+ spin_lock_init(&queue->qlock);
+ kref_init(&queue->ref);
+
+ nvmet_fc_prep_fcp_iodlist(assoc->tgtport, queue);
+
+ ret = nvmet_sq_init(&queue->nvme_sq);
+ if (ret)
+ goto out_fail_iodlist;
+
+ WARN_ON(assoc->queues[qid]);
+ spin_lock_irqsave(&assoc->tgtport->lock, flags);
+ assoc->queues[qid] = queue;
+ spin_unlock_irqrestore(&assoc->tgtport->lock, flags);
+
+ return queue;
+
+out_fail_iodlist:
+ nvmet_fc_destroy_fcp_iodlist(assoc->tgtport, queue);
+ destroy_workqueue(queue->work_q);
+out_a_put:
+ nvmet_fc_tgt_a_put(assoc);
+out_free_queue:
+ kfree(queue);
+ return NULL;
+}
+
+
+static void
+nvmet_fc_tgt_queue_free(struct kref *ref)
+{
+ struct nvmet_fc_tgt_queue *queue =
+ container_of(ref, struct nvmet_fc_tgt_queue, ref);
+ unsigned long flags;
+
+ spin_lock_irqsave(&queue->assoc->tgtport->lock, flags);
+ queue->assoc->queues[queue->qid] = NULL;
+ spin_unlock_irqrestore(&queue->assoc->tgtport->lock, flags);
+
+ nvmet_fc_destroy_fcp_iodlist(queue->assoc->tgtport, queue);
+
+ nvmet_fc_tgt_a_put(queue->assoc);
+
+ destroy_workqueue(queue->work_q);
+
+ kfree(queue);
+}
+
+static void
+nvmet_fc_tgt_q_put(struct nvmet_fc_tgt_queue *queue)
+{
+ kref_put(&queue->ref, nvmet_fc_tgt_queue_free);
+}
+
+static int
+nvmet_fc_tgt_q_get(struct nvmet_fc_tgt_queue *queue)
+{
+ return kref_get_unless_zero(&queue->ref);
+}
+
+
+static void
+nvmet_fc_abort_op(struct nvmet_fc_tgtport *tgtport,
+ struct nvmefc_tgt_fcp_req *fcpreq)
+{
+ int ret;
+
+ fcpreq->op = NVMET_FCOP_ABORT;
+ fcpreq->offset = 0;
+ fcpreq->timeout = 0;
+ fcpreq->transfer_length = 0;
+ fcpreq->transferred_length = 0;
+ fcpreq->fcp_error = 0;
+ fcpreq->sg_cnt = 0;
+
+ ret = tgtport->ops->fcp_op(&tgtport->fc_target_port, fcpreq);
+ if (ret)
+ /* should never reach here !! */
+ WARN_ON(1);
+}
+
+
+static void
+nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue)
+{
+ struct nvmet_fc_fcp_iod *fod = queue->fod;
+ unsigned long flags;
+ int i;
+ bool disconnect;
+
+ disconnect = atomic_xchg(&queue->connected, 0);
+
+ spin_lock_irqsave(&queue->qlock, flags);
+ /* about outstanding io's */
+ for (i = 0; i < queue->sqsize; fod++, i++) {
+ if (fod->active) {
+ spin_lock(&fod->flock);
+ fod->abort = true;
+ spin_unlock(&fod->flock);
+ }
+ }
+ spin_unlock_irqrestore(&queue->qlock, flags);
+
+ flush_workqueue(queue->work_q);
+
+ if (disconnect)
+ nvmet_sq_destroy(&queue->nvme_sq);
+
+ nvmet_fc_tgt_q_put(queue);
+}
+
+static struct nvmet_fc_tgt_queue *
+nvmet_fc_find_target_queue(struct nvmet_fc_tgtport *tgtport,
+ u64 connection_id)
+{
+ struct nvmet_fc_tgt_assoc *assoc;
+ struct nvmet_fc_tgt_queue *queue;
+ u64 association_id = nvmet_fc_getassociationid(connection_id);
+ u16 qid = nvmet_fc_getqueueid(connection_id);
+ unsigned long flags;
+
+ spin_lock_irqsave(&tgtport->lock, flags);
+ list_for_each_entry(assoc, &tgtport->assoc_list, a_list) {
+ if (association_id == assoc->association_id) {
+ queue = assoc->queues[qid];
+ if (queue &&
+ (!atomic_read(&queue->connected) ||
+ !nvmet_fc_tgt_q_get(queue)))
+ queue = NULL;
+ spin_unlock_irqrestore(&tgtport->lock, flags);
+ return queue;
+ }
+ }
+ spin_unlock_irqrestore(&tgtport->lock, flags);
+ return NULL;
+}
+
+static struct nvmet_fc_tgt_assoc *
+nvmet_fc_alloc_target_assoc(struct nvmet_fc_tgtport *tgtport)
+{
+ struct nvmet_fc_tgt_assoc *assoc, *tmpassoc;
+ unsigned long flags;
+ u64 ran;
+ int idx;
+ bool needrandom = true;
+
+ assoc = kzalloc(sizeof(*assoc), GFP_KERNEL);
+ if (!assoc)
+ return NULL;
+
+ idx = ida_simple_get(&tgtport->assoc_cnt, 0, 0, GFP_KERNEL);
+ if (idx < 0)
+ goto out_free_assoc;
+
+ if (!nvmet_fc_tgtport_get(tgtport))
+ goto out_ida_put;
+
+ assoc->tgtport = tgtport;
+ assoc->a_id = idx;
+ INIT_LIST_HEAD(&assoc->a_list);
+ kref_init(&assoc->ref);
+
+ while (needrandom) {
+ get_random_bytes(&ran, sizeof(ran) - BYTES_FOR_QID);
+ ran = ran << BYTES_FOR_QID_SHIFT;
+
+ spin_lock_irqsave(&tgtport->lock, flags);
+ needrandom = false;
+ list_for_each_entry(tmpassoc, &tgtport->assoc_list, a_list)
+ if (ran == tmpassoc->association_id) {
+ needrandom = true;
+ break;
+ }
+ if (!needrandom) {
+ assoc->association_id = ran;
+ list_add_tail(&assoc->a_list, &tgtport->assoc_list);
+ }
+ spin_unlock_irqrestore(&tgtport->lock, flags);
+ }
+
+ return assoc;
+
+out_ida_put:
+ ida_simple_remove(&tgtport->assoc_cnt, idx);
+out_free_assoc:
+ kfree(assoc);
+ return NULL;
+}
+
+static void
+nvmet_fc_target_assoc_free(struct kref *ref)
+{
+ struct nvmet_fc_tgt_assoc *assoc =
+ container_of(ref, struct nvmet_fc_tgt_assoc, ref);
+ struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
+ unsigned long flags;
+
+ spin_lock_irqsave(&tgtport->lock, flags);
+ list_del(&assoc->a_list);
+ spin_unlock_irqrestore(&tgtport->lock, flags);
+ ida_simple_remove(&tgtport->assoc_cnt, assoc->a_id);
+ kfree(assoc);
+ nvmet_fc_tgtport_put(tgtport);
+}
+
+static void
+nvmet_fc_tgt_a_put(struct nvmet_fc_tgt_assoc *assoc)
+{
+ kref_put(&assoc->ref, nvmet_fc_target_assoc_free);
+}
+
+static int
+nvmet_fc_tgt_a_get(struct nvmet_fc_tgt_assoc *assoc)
+{
+ return kref_get_unless_zero(&assoc->ref);
+}
+
+static void
+nvmet_fc_delete_target_assoc(struct nvmet_fc_tgt_assoc *assoc)
+{
+ struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
+ struct nvmet_fc_tgt_queue *queue;
+ unsigned long flags;
+ int i;
+
+ spin_lock_irqsave(&tgtport->lock, flags);
+ for (i = NVMET_NR_QUEUES - 1; i >= 0; i--) {
+ queue = assoc->queues[i];
+ if (queue) {
+ if (!nvmet_fc_tgt_q_get(queue))
+ continue;
+ spin_unlock_irqrestore(&tgtport->lock, flags);
+ nvmet_fc_delete_target_queue(queue);
+ nvmet_fc_tgt_q_put(queue);
+ spin_lock_irqsave(&tgtport->lock, flags);
+ }
+ }
+ spin_unlock_irqrestore(&tgtport->lock, flags);
+
+ nvmet_fc_tgt_a_put(assoc);
+}
+
+static struct nvmet_fc_tgt_assoc *
+nvmet_fc_find_target_assoc(struct nvmet_fc_tgtport *tgtport,
+ u64 association_id)
+{
+ struct nvmet_fc_tgt_assoc *assoc;
+ struct nvmet_fc_tgt_assoc *ret = NULL;
+ unsigned long flags;
+
+ spin_lock_irqsave(&tgtport->lock, flags);
+ list_for_each_entry(assoc, &tgtport->assoc_list, a_list) {
+ if (association_id == assoc->association_id) {
+ ret = assoc;
+ nvmet_fc_tgt_a_get(assoc);
+ break;
+ }
+ }
+ spin_unlock_irqrestore(&tgtport->lock, flags);
+
+ return ret;
+}
+
+
+/**
+ * nvme_fc_register_targetport - transport entry point called by an
+ * LLDD to register the existence of a local
+ * NVME subystem FC port.
+ * @pinfo: pointer to information about the port to be registered
+ * @template: LLDD entrypoints and operational parameters for the port
+ * @dev: physical hardware device node port corresponds to. Will be
+ * used for DMA mappings
+ * @portptr: pointer to a local port pointer. Upon success, the routine
+ * will allocate a nvme_fc_local_port structure and place its
+ * address in the local port pointer. Upon failure, local port
+ * pointer will be set to NULL.
+ *
+ * Returns:
+ * a completion status. Must be 0 upon success; a negative errno
+ * (ex: -ENXIO) upon failure.
+ */
+int
+nvmet_fc_register_targetport(struct nvmet_fc_port_info *pinfo,
+ struct nvmet_fc_target_template *template,
+ struct device *dev,
+ struct nvmet_fc_target_port **portptr)
+{
+ struct nvmet_fc_tgtport *newrec;
+ unsigned long flags;
+ int ret, idx;
+
+ if (!template->xmt_ls_rsp || !template->fcp_op ||
+ !template->targetport_delete ||
+ !template->max_hw_queues || !template->max_sgl_segments ||
+ !template->max_dif_sgl_segments || !template->dma_boundary) {
+ ret = -EINVAL;
+ goto out_regtgt_failed;
+ }
+
+ newrec = kzalloc((sizeof(*newrec) + template->target_priv_sz),
+ GFP_KERNEL);
+ if (!newrec) {
+ ret = -ENOMEM;
+ goto out_regtgt_failed;
+ }
+
+ idx = ida_simple_get(&nvmet_fc_tgtport_cnt, 0, 0, GFP_KERNEL);
+ if (idx < 0) {
+ ret = -ENOSPC;
+ goto out_fail_kfree;
+ }
+
+ if (!get_device(dev) && dev) {
+ ret = -ENODEV;
+ goto out_ida_put;
+ }
+
+ newrec->fc_target_port.node_name = pinfo->node_name;
+ newrec->fc_target_port.port_name = pinfo->port_name;
+ newrec->fc_target_port.private = &newrec[1];
+ newrec->fc_target_port.port_id = pinfo->port_id;
+ newrec->fc_target_port.port_num = idx;
+ INIT_LIST_HEAD(&newrec->tgt_list);
+ newrec->dev = dev;
+ newrec->ops = template;
+ spin_lock_init(&newrec->lock);
+ INIT_LIST_HEAD(&newrec->ls_list);
+ INIT_LIST_HEAD(&newrec->ls_busylist);
+ INIT_LIST_HEAD(&newrec->assoc_list);
+ kref_init(&newrec->ref);
+ ida_init(&newrec->assoc_cnt);
+
+ ret = nvmet_fc_alloc_ls_iodlist(newrec);
+ if (ret) {
+ ret = -ENOMEM;
+ goto out_free_newrec;
+ }
+
+ spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
+ list_add_tail(&newrec->tgt_list, &nvmet_fc_target_list);
+ spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
+
+ *portptr = &newrec->fc_target_port;
+ return 0;
+
+out_free_newrec:
+ put_device(dev);
+out_ida_put:
+ ida_simple_remove(&nvmet_fc_tgtport_cnt, idx);
+out_fail_kfree:
+ kfree(newrec);
+out_regtgt_failed:
+ *portptr = NULL;
+ return ret;
+}
+EXPORT_SYMBOL_GPL(nvmet_fc_register_targetport);
+
+
+static void
+nvmet_fc_free_tgtport(struct kref *ref)
+{
+ struct nvmet_fc_tgtport *tgtport =
+ container_of(ref, struct nvmet_fc_tgtport, ref);
+ struct device *dev = tgtport->dev;
+ unsigned long flags;
+
+ spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
+ list_del(&tgtport->tgt_list);
+ spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
+
+ nvmet_fc_free_ls_iodlist(tgtport);
+
+ /* let the LLDD know we've finished tearing it down */
+ tgtport->ops->targetport_delete(&tgtport->fc_target_port);
+
+ ida_simple_remove(&nvmet_fc_tgtport_cnt,
+ tgtport->fc_target_port.port_num);
+
+ ida_destroy(&tgtport->assoc_cnt);
+
+ kfree(tgtport);
+
+ put_device(dev);
+}
+
+static void
+nvmet_fc_tgtport_put(struct nvmet_fc_tgtport *tgtport)
+{
+ kref_put(&tgtport->ref, nvmet_fc_free_tgtport);
+}
+
+static int
+nvmet_fc_tgtport_get(struct nvmet_fc_tgtport *tgtport)
+{
+ return kref_get_unless_zero(&tgtport->ref);
+}
+
+static void
+__nvmet_fc_free_assocs(struct nvmet_fc_tgtport *tgtport)
+{
+ struct nvmet_fc_tgt_assoc *assoc, *next;
+ unsigned long flags;
+
+ spin_lock_irqsave(&tgtport->lock, flags);
+ list_for_each_entry_safe(assoc, next,
+ &tgtport->assoc_list, a_list) {
+ if (!nvmet_fc_tgt_a_get(assoc))
+ continue;
+ spin_unlock_irqrestore(&tgtport->lock, flags);
+ nvmet_fc_delete_target_assoc(assoc);
+ nvmet_fc_tgt_a_put(assoc);
+ spin_lock_irqsave(&tgtport->lock, flags);
+ }
+ spin_unlock_irqrestore(&tgtport->lock, flags);
+}
+
+/*
[Output truncated: showing 1000 of 2315 lines]]
|
| 72/2 |
2026/11/27 07:36 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 95138
output: 58
thoughts: 3205
Thoughts: **Summary of Thought Process Regarding FC-NVMe Target Port Race Condition**
I've been examining a critical race condition within the FC-NVMe subsystem, specifically concerning the creation and management of target ports. My goal was to pinpoint the root cause and the committing that introduced this vulnerability.
Initially, I observed the state of the system after a successful picture of the bug emerged. To understand this, I decided to meticulously trace the history of relevant code changes, looking at both sides of the story: how target ports are created and how they are managed within the NVMe target framework.
My first detailed examination focused on commit `475d0fe795516a9b9f286a851c3972fd8831c643` ("nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME"). In this commit, I noted how `fcloop_create_target_port` calls `fcloop_alloc_nport`. This function acquires `fcloop_lock`, checks for an existing `nport` (specifically `nport->tport != NULL`), and if none exists, it either creates a new `nport` (with `tport == NULL`) or reuses an existing one. Crucially, it then releases `fcloop_lock`.
The problem arises because *after* releasing `fcloop_lock`, and *while `nport->tport` is still `NULL`*, `fcloop_create_target_port` proceeds to call `nvmet_fc_register_targetport()`. Only *after* this call returns does it set `nport->tport = tport`. This creates a distinct race window. If two concurrent `fcloop_create_target_port` calls are made with the same WWNN and WWPN, both will pass the `fcloop_alloc_nport` check because `nport->tport` is `NULL` for both at that moment. They will both operate on the same `nport` object. Consequently, both will call `nvmet_fc_register_targetport()` with the same identifiers. The second call will then overwrite `nport->tport`, leading to a leak of the first `tport`/`tgtport` and the registration of duplicate target ports with identical WWNs.
Next, I reviewed commit `ea96d6496ff59b2b26dc9e13dc8f57d77731eb37` ("nvmet_fc: support target port removal with nvmet layer"). This commit introduced `nvmet_fc_portentry_rebind_tgt()`, which includes a `WARN_ON(pe->tgtport)`. This function is designed to handle LLDD target port unregistration followed by re-registration when an NVMe port is already configured.
At this point, I paused to consider: is commit `ea96d6496ff59` also buggy, or is commit `475d0fe795516` the true root cause, or perhaps both? I delved deeper into `nvmet_fc_portentry_rebind_tgt()` and `nvmet_fc_register_targetport()` within `ea96d6496ff5`.
I observed that `nvmet_fc_portentry_rebind_tgt()` iterates through the `nvmet_fc_portentry_list` under `nvmet_fc_tgtlock`. It checks for a matching `tgtport` based on `node_name` and `port_name`. If a match is found, it attempts to bind the `tgtport` to the `pe`. The `WARN_ON(pe->tgtport)` assertion is there specifically because `nvmet-fc` expects that an LLDD should *not* register a duplicate target port with the same WWNs while one is already registered and bound to a `pe`.
However, I realized that even if `nvmet_fc_portentry_rebind_tgt()` were modified to explicitly check `if (pe->tgtport)`, or if `nvmet_fc_register_targetport()` were changed to reject duplicates, the *original race condition* in `fcloop_create_target_port` (introduced in `475d0fe795516`) would still persist. This race allows two target ports with the same WWNs to be registered concurrently. The subsequent overwrite of `nport->tport` would still happen, leading to reference counting issues and cleanup failures.
I then considered the possibility of proper serialization within `fcloop`. If `fcloop` had robust synchronization mechanisms (or prevented concurrent target port creation for the same WWNs by maintaining a state or holding a mutex across `fcloop_create_target_port`), the `nvmet_fc_register_targetport` function would never be called concurrently for the same WWNs by `fcloop`. In such a scenario, the `WARN_ON(pe->tgtport)` from `ea96d6496ff59` would never be triggered.
I also briefly looked at `lpfc`. In `lpfc`, it appears that concurrent calls to `nvmet_fc_register_targetport` for the same WWN are prevented. Each HBA typically has a unique WWPN, and `lpfc_nvmet_create_targetport` checks `if (phba->targetport) return 0;`, only registering once during probe.
My next step was to double-check if `fcloop_create_target_port` could also fail to wait for `fcloop_targetport_delete` or `nvmet_fc_unregister_targetport`. Examining `fcloop_delete_target_port`, I saw that it looks up the `nport`, acquires `fcloop_lock`, unlinks the `tport` using `__unlink_target_port`, releases the lock, and then calls `__targetport_unreg`.
This led me to meticulously re-read the syzkaller reproducer. The reproducer's steps are crucial:
1. **Create the initial target port.**
2. **Create an NVMe-oF port and configure it for FC.** This involves calling `nvmet_fc_add_port`, creating a `pe`, binding it to the `tgtport`, and adding the `pe` to `nvmet_fc_portentry_list`.
3. **Delete the target port.** This synchronously completes by calling `nvmet_fc_unregister_targetport`, which in turn calls `nvmet_fc_portentry_unbind_tgt`. Importantly, this unbinding *sets `pe->tgtport = NULL`* but leaves the `pe` in the `nvmet_fc_portentry_list`.
4. **Concurrently create the target port.** This is done using two threads (`t1` and `t2`) which concurrently run `fcloop_create_target_port` for the *same* WWNN. The reproducer explicitly notes: "// 5. Concurrently create the target port to trigger the race condition".
The critical observation here is that step 4 finishes *synchronously* and *before* steps 5 (`t1` and `t2`) even begin. This confirms that the race is not between deletion and concurrent creation, but rather between two *simultaneous* creation attempts of the same target port.
This detailed analysis solidified my understanding. Let's verify every single detail across the relevant commits: `475d0fe795516a9b9f286a851c3972fd8831c643`, `d54a9d7f6d74ee385d496f0efd282ce9d4fbfcbc`, and `ea96d6496ff59b2b26dc9e13dc8f57d77731eb37`.
1. **Why is `d54a9d7f6d74e` NOT the introducing commit?**
In `d54a9d7f6d74e` ("nvmet-fcloop: refactor fcloop_nport_alloc and track lport"), `fcloop_alloc_nport` was refactored. However, the fundamental race window between `fcloop_alloc_nport` (releasing `fcloop_lock` with `nport->tport == NULL`) and `fcloop_create_target_port` (calling `nvmet_fc_register_targetport` and only later setting `nport->tport = tport`) remained identical to the state in `475d0fe795516`.
2. **Why is `ea96d6496ff59` NOT the primary introducing commit (or should we think carefully about this)?**
In `ea96d6496ff59`, the `WARN_ON(pe->tgtport)` was added to `nvmet_fc_portentry_rebind_tgt`. This aligns with my rule: "Do NOT blame commits that merely added debugging or sanitizer checks... that exposed an older bug, unless the check itself is incorrect." Is `WARN_ON(pe->tgtport)` incorrect?
* `pe` represents an `nvmet_port` configured for a specific FC `(node_name, port_name)`.
* An `nvmet_fc_port_entry` can only be bound to *one* `nvmet_fc_tgtport` at a time (`pe->tgtport`).
* `nvmet_fc_portentry_rebind_tgt()` is intended to bind a *newly* registered `tgtport` to an *unbound* `pe` (`pe->tgtport == NULL`) only after the old `tgtport` has been unregistered.
* If two `tgtport`s with the same WWNs are registered simultaneously by an LLDD (like `fcloop`), then `pe->tgtport` would be non-NULL when the second one registers. This violates the invariant that a `pe` has at most one active `tgtport`. It would also overwrite `pe->tgtport`, leaking a `tgtport` reference and corrupting `tgtport->pe`.
* Meanwhile, in `fcloop.c` (introduced in `475d0fe795516`), `fcloop_alloc_nport()` *explicitly* checks `if (!remoteport && nport->tport)` under `fcloop_lock` to *prevent* creating two target ports for the same WWNs. This is because only one target port per WWNN/WWPN is allowed. However, the critical flaw is that `fcloop_alloc_nport()` *releases* `fcloop_lock` while `nport->tport` is still `NULL`. Then, `fcloop_create_target_port()` only sets `nport->tport = tport` *after* `nvmet_fc_register_targetport()` returns. Therefore, two concurrent `fcloop_create_target_port()` calls with the same WWNNs *will* pass the check in `fcloop_alloc_nport()`, both register target ports via `nvmet_fc_register_targetport()`, and then proceed to overwrite `nport->tport`.
Consequently:
* The *underlying bug* is the TOCTOU (Time-of-check to time-of-use) and missing synchronization in `fcloop_alloc_nport()` / `fcloop_create_target_port()`. This was introduced by commit `475d0fe795516a9b9f286a851c3972fd8831c643` ("nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME").
* Commit `ea96d6496ff59b2b26dc9e13dc8f57d77731eb37` ("nvmet_fc: support target port removal with nvmet layer") later added `nvmet_fc_portentry_rebind_tgt()` and its `WARN_ON(pe->tgtport)` check. This commit *exposed* the existing race by triggering a kernel warning when an NVMe port is bound to a WWNN/WWPN *before* attempting to re-create the target port with the same identifiers.
* Commit `d54a9d7f6d74ee385d496f0efd282ce9d4fbfcbc` ("nvmet-fcloop: refactor fcloop_nport_alloc and track lport") later refactored `fcloop_alloc_nport()` without rectifying this fundamental race.
I've also cross-referenced with `git-log` on `drivers/nvme/target/fcloop.c` and `drivers/nvme/target/fc.c`, specifically looking for `create_target_port`, `alloc_nport`, `mutex`, or `lock` related changes to ensure no other relevant commits were missed. My analysis remains consistent.
|
| 73/2 |
2026/11/27 07:36 |
tool |
git-log |
0m
Args: map[CodeRegexp:fcloop_create_target_port|fcloop_alloc_nport PathPrefix:drivers/nvme/target/fcloop.c]
Results: map[Output:475d0fe79551 nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME
]
|
| 74/2 |
2026/11/27 07:36 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 98448
output: 60
thoughts: 68
Thoughts: **My Thought Process: Verifying and Communicating Results**
I've reached a point where the current state of affairs is crystal clear and thoroughly verified. There's no ambiguity left; all the information I need is present and accurate.
Now, I need to communicate these confirmed findings. My immediate next step is to invoke the `set-results` function. When I do this, I will pass it the specific identifier `["475d0fe795516a9b9f286a851c3972fd8831c643"]`. Following this action, I am committed to providing an explanation that is not only comprehensive but also exceptionally clear, ensuring anyone who needs to understand these results can do so without reservation. My aim is to leave no room for misinterpretation, offering a detailed and easily digestible account of what has been verified.
|
| 75/2 |
2026/11/27 07:36 |
tool |
set-results |
0m
Args: map[CandidateCommits:[475d0fe795516a9b9f286a851c3972fd8831c643]]
Results: map[CandidateCommits:[475d0fe795516a9b9f286a851c3972fd8831c643]]
|
| 76/2 |
2026/11/27 07:36 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 98632
output: 1276
thoughts: 15
Thoughts: **My Thought Process for Generating the Final Response**
Okay, I've just finished the last step, which was to compile the complete bisection log. My immediate thought is, "Right, time to construct the final output." This means I need to synthesize everything we've discussed and developed into a cohesive and comprehensive response.
I'm thinking about the structure. It needs to be clear, logical, and address the user's (your) expertise directly. Since you're an expert, I don't need to over-explain fundamental concepts, but rather focus on the application, the nuances, and the specific results derived from our process.
So, the core of my thinking now is to take that bisection log, which is essentially the raw data of our investigation, and present it in a way that tells a story. It's not just a list of commands and outputs; it's a narrative of how we pinpointed the issue.
I'm mentally mapping out how to best frame this. I'll need to start with a clear statement of purpose, outlining what this final response represents. Then, I'll introduce the bisection log itself, explaining its significance. I'm considering how to present the log – perhaps as a table, or clearly delineated sections, so it's easy to follow the progression of the bisect.
More importantly, I'm focusing on the *interpretation* of that log. What does it *mean*? What was the critical divergence point? This is where I believe the real value lies for an expert like yourself. I need to articulate precisely *why* a particular commit was identified as the culprit, what the symptoms were before and after that point, and how the bisection process systematically led us there.
I'm also thinking about the implications. What does this identified change mean for the project? Are there immediate steps we should consider? What are the potential knock-on effects? While the bisection log itself is the technical core, the summary needs to provide that higher-level insight.
Essentially, my current thought is a process of distillation and articulation. I've done the groundwork, the debugging, the systematic isolation. Now, I need to present that entire journey, culminating in the definitive identification of the problematic change, in a way that is both informative and actionable for someone who understands the underlying complexities. The bisection log is the proof, but the summary is the explanation and the conclusion.
|
| 77/1 |
2026/11/27 07:36 |
action |
format-bisect |
0m
Results: map[Commits:[map[AuthorEmail:jsmart2021@gmail.com AuthorName:James Smart Date:2016-12-02T00:28:44-08:00 Hash:475d0fe795516a9b9f286a851c3972fd8831c643 Recipients:[map[Email:hch@lst.de Name: To:true] map[Email:james.smart@broadcom.com Name: To:true] map[Email:james_p_freyensee@linux.intel.com Name: To:true] map[Email:jsmart2021@gmail.com Name: To:true] map[Email:jthumshirn@suse.de Name: To:true]] Title:nvme-fabrics: Add FC LLDD loopback driver to test FC-NVME]] IsRelease:false]
|