| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/09/15 14:31 | flow | patch-triage |
26mArgs:null Results: {
"EnableConfigs": [
"IO_URING",
"USERFAULTFD",
"SECCOMP"
],
"FocusSymbols": [
"fd_prepare",
"fd_stage",
"fd_slots_commit",
"__fd_slots_commit",
"exit_fd_slots",
"io_req_post_cqe",
"scm_recv_one_fd"
],
"KMSANReasoning": "The patch series refactors file descriptor reservation and installation across syscalls, io_uring, and drivers by deferring publication to syscall exit time via `fd_prepare()`, `fd_stage()`, and `fd_slots_commit()`.\n\nFrom a KMSAN perspective:\n1. `task_struct::fd_slots` is explicitly zeroed on task duplication via `memset(\u0026tsk-\u003efd_slots, 0, sizeof(tsk-\u003efd_slots))` in `dup_task_struct()`.\n2. When recording a slot in `fd_slot_record()`, both fields of `struct fd_slot` (`fd` and `file`) are unconditionally initialized before `slots-\u003enr` is incremented. All loops in `fd_slots_install()`, `fd_slots_drop()`, and `fd_slot_find()` strictly iterate over indices `0` to `slots-\u003enr - 1`, ensuring uninitialized spill buffer elements are never read.\n3. No kernel-to-user memory info-leaks are introduced. The data copied to userspace in the converted call sites (such as ioctl output arguments or `put_user` for fd numbers) consists of explicitly initialized scalar descriptors or structs that were either zeroed or populated from `copy_from_user()`.\n4. The risks introduced by this patch relate to descriptor/file reference counting (e.g. potential double free or use-after-free on error paths), locking (`files-\u003efile_lock`), and architecture-specific syscall exit register state. These hazards are addressed by KASAN, LOCKDEP, and standard kernel debugging facilities, not KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch series introduces a core VFS mechanism (fd_slots) to defer file descriptor installation until syscall exit, committing staged files on success and releasing/dropping them on error. It modifies core task lifecycle code (task_struct, fork, exec, exit), architecture syscall exit handling (SYSCALL_WORK_FD_SLOTS in generic entry common), io_uring completion handling, and various syscall/ioctl entrypoints (perf_event_open, userfaultfd, seccomp, eventfd, epoll, scm, etc.). It also introduces multiple new assertions (VFS_WARN_ON_ONCE and WARN_ON_ONCE) in reachability paths including exec, unshare, task exit, and io_uring. Fuzzing is warranted to discover potential descriptor leaks, premature commits/drops, or triggered assertions during complex error unwind or asynchronous completion paths.",
"WorthFuzzing": true
} |
| 1/1 | 2026/09/15 14:31 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit a40598dcddd1056cb33bf7ce7eca154940bc9aaf\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Tue Sep 15 14:31:17 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/arch/alpha/include/asm/thread_info.h b/arch/alpha/include/asm/thread_info.h\nindex 1552ecca85207..db8465f5fabb2 100644\n--- a/arch/alpha/include/asm/thread_info.h\n+++ b/arch/alpha/include/asm/thread_info.h\n@@ -67,6 +67,7 @@ register unsigned long *current_stack_pointer __asm__ (\"$30\");\n #define TIF_NOTIFY_SIGNAL\t5\t/* signal notifications exist */\n #define TIF_SECCOMP\t\t6\t/* seccomp syscall filtering active */\n #define\tTIF_SYSCALL_TRACEPOINT\t7\t/* syscall tracepoint instrumentation */\n+#define TIF_FD_SLOTS\t\t8\t/* syscall prepared descriptors */\n #define TIF_DIE_IF_KERNEL\t9\t/* dik recursion lock */\n #define TIF_MEMDIE\t\t13\t/* is terminating due to OOM killer */\n #define TIF_POLLING_NRFLAG\t14\t/* idle is polling for TIF_NEED_RESCHED */\n@@ -80,6 +81,7 @@ register unsigned long *current_stack_pointer __asm__ (\"$30\");\n #define _TIF_SECCOMP\t\t(1\u003c\u003cTIF_SECCOMP)\n #define _TIF_POLLING_NRFLAG\t(1\u003c\u003cTIF_POLLING_NRFLAG)\n #define _TIF_SYSCALL_TRACEPOINT\t(1\u003c\u003cTIF_SYSCALL_TRACEPOINT)\n+#define _TIF_FD_SLOTS\t\t(1\u003c\u003cTIF_FD_SLOTS)\n \n /*\n * Work to do on syscall entry (in entry.S).\ndiff --git a/arch/alpha/kernel/entry.S b/arch/alpha/kernel/entry.S\nindex 9f2608de25446..20372e9263591 100644\n--- a/arch/alpha/kernel/entry.S\n+++ b/arch/alpha/kernel/entry.S\n@@ -569,6 +569,13 @@ entSys:\n $ret_success:\n \tstq\t$0, 0($sp)\n \tstq\t$31, 72($sp)\t\t/* a3=0 =\u003e no error */\n+$syscall_exit:\n+\t/* Descriptors reserved by the syscall are committed in\n+\t syscall_trace_leave, off the return path interrupts share. */\n+\tldl\t$17, TI_FLAGS($8)\n+\tlda\t$2, _TIF_FD_SLOTS\n+\tand\t$17, $2, $2\n+\tbne\t$2, $fd_slots_exit\n \n \t.align\t4\n \t.globl\tret_from_sys_call\n@@ -612,7 +619,7 @@ $syscall_error:\n \tstq\t$0, 0($sp)\n \tmov\t$31, $26\t/* tell \"ret_from_sys_call\" we can restart */\n \tstq\t$1, 72($sp)\t/* a3 for return */\n-\tbr\tret_from_sys_call\n+\tbr\t$syscall_exit\n \n /*\n * Do all cleanup when returning from all interrupts and system calls.\n@@ -648,6 +655,21 @@ $work_notifysig:\n \tUNDO_SWITCH_STACK\n \tbr\trestore_all\n \n+\t.align\t4\n+$fd_slots_exit:\n+\t/* Keep the return address and the syscall restart state in\n+\t $26/$18/$19 across the call. */\n+\tDO_SWITCH_STACK\n+\tmov\t$26, $9\n+\tmov\t$18, $10\n+\tmov\t$19, $11\n+\tjsr\t$26, syscall_trace_leave\n+\tmov\t$9, $26\n+\tmov\t$10, $18\n+\tmov\t$11, $19\n+\tUNDO_SWITCH_STACK\n+\tbr\tret_from_sys_call\n+\n /*\n * PTRACE syscall handler\n */\ndiff --git a/arch/alpha/kernel/ptrace.c b/arch/alpha/kernel/ptrace.c\nindex fc8f6cedbb286..736218b34601e 100644\n--- a/arch/alpha/kernel/ptrace.c\n+++ b/arch/alpha/kernel/ptrace.c\n@@ -6,6 +6,7 @@\n /* more mutilation by David Mosberger (davidm@azstarnet.com) */\n \n #include \u003clinux/kernel.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/sched.h\u003e\n #include \u003clinux/sched/task_stack.h\u003e\n #include \u003clinux/mm.h\u003e\n@@ -507,6 +508,8 @@ asmlinkage unsigned long syscall_trace_enter(void)\n asmlinkage void\n syscall_trace_leave(void)\n {\n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(current_pt_regs());\n \taudit_syscall_exit(current_pt_regs());\n \tif (test_thread_flag(TIF_SYSCALL_TRACE))\n \t\tptrace_report_syscall_exit(current_pt_regs(), 0);\ndiff --git a/arch/arc/include/asm/thread_info.h b/arch/arc/include/asm/thread_info.h\nindex 255d2c7742190..4c67d4eb434b9 100644\n--- a/arch/arc/include/asm/thread_info.h\n+++ b/arch/arc/include/asm/thread_info.h\n@@ -77,6 +77,7 @@ static inline __attribute_const__ struct thread_info *current_thread_info(void)\n #define TIF_NEED_RESCHED\t3\t/* rescheduling necessary */\n #define TIF_SYSCALL_AUDIT\t4\t/* syscall auditing active */\n #define TIF_NOTIFY_SIGNAL\t5\t/* signal notifications exist */\n+#define TIF_FD_SLOTS\t\t6\t/* syscall prepared descriptors */\n #define TIF_SYSCALL_TRACE\t15\t/* syscall trace active */\n /* true if poll_idle() is polling TIF_NEED_RESCHED */\n #define TIF_MEMDIE\t\t16\n@@ -88,6 +89,7 @@ static inline __attribute_const__ struct thread_info *current_thread_info(void)\n #define _TIF_NEED_RESCHED\t(1\u003c\u003cTIF_NEED_RESCHED)\n #define _TIF_SYSCALL_AUDIT\t(1\u003c\u003cTIF_SYSCALL_AUDIT)\n #define _TIF_NOTIFY_SIGNAL\t(1\u003c\u003cTIF_NOTIFY_SIGNAL)\n+#define _TIF_FD_SLOTS\t\t(1\u003c\u003cTIF_FD_SLOTS)\n #define _TIF_MEMDIE\t\t(1\u003c\u003cTIF_MEMDIE)\n #define _TIF_SYSCALL_TRACEPOINT\t(1\u003c\u003cTIF_SYSCALL_TRACEPOINT)\n \ndiff --git a/arch/arc/kernel/entry.S b/arch/arc/kernel/entry.S\nindex 3c7e74aba6794..f436391526e16 100644\n--- a/arch/arc/kernel/entry.S\n+++ b/arch/arc/kernel/entry.S\n@@ -241,6 +241,11 @@ ENTRY(EV_Trap)\n \tjl [r9]\n \n .Lret_from_system_call:\n+\t; descriptors reserved by the syscall are committed in syscall_trace_exit()\n+\tGET_CURR_THR_INFO_FLAGS r10\n+\tand.f 0, r10, _TIF_FD_SLOTS\n+\tbnz tracesys_exit\n+\n \tst r0, [sp, PT_r0] ; sys call return value in pt_regs\n \n \t; fall through to ret_from_exception\ndiff --git a/arch/arc/kernel/ptrace.c b/arch/arc/kernel/ptrace.c\nindex fe2b6572e9199..0a0d74b9c7c43 100644\n--- a/arch/arc/kernel/ptrace.c\n+++ b/arch/arc/kernel/ptrace.c\n@@ -3,6 +3,7 @@\n * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)\n */\n \n+#include \u003clinux/file.h\u003e\n #include \u003clinux/ptrace.h\u003e\n #include \u003clinux/sched/task_stack.h\u003e\n #include \u003clinux/regset.h\u003e\n@@ -355,6 +356,9 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs)\n \n asmlinkage void syscall_trace_exit(struct pt_regs *regs)\n {\n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \tif (test_thread_flag(TIF_SYSCALL_TRACE))\n \t\tptrace_report_syscall_exit(regs, 0);\n \ndiff --git a/arch/arm/include/asm/thread_info.h b/arch/arm/include/asm/thread_info.h\nindex 943ffcf069d29..5337449e8409d 100644\n--- a/arch/arm/include/asm/thread_info.h\n+++ b/arch/arm/include/asm/thread_info.h\n@@ -144,6 +144,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp *,\n #define TIF_NOTIFY_RESUME\t2\t/* callback before returning to user */\n #define TIF_UPROBE\t\t3\t/* breakpointed or singlestepping */\n #define TIF_NOTIFY_SIGNAL\t4\t/* signal notifications exist */\n+#define TIF_FD_SLOTS\t\t5\t/* syscall prepared descriptors, entry-common.S needs it below 16 */\n \n #define TIF_USING_IWMMXT\t17\n #define TIF_MEMDIE\t\t18\t/* is terminating due to OOM killer */\n@@ -163,6 +164,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp *,\n #define _TIF_SYSCALL_TRACEPOINT\t(1 \u003c\u003c TIF_SYSCALL_TRACEPOINT)\n #define _TIF_SECCOMP\t\t(1 \u003c\u003c TIF_SECCOMP)\n #define _TIF_NOTIFY_SIGNAL\t(1 \u003c\u003c TIF_NOTIFY_SIGNAL)\n+#define _TIF_FD_SLOTS\t\t(1 \u003c\u003c TIF_FD_SLOTS)\n #define _TIF_USING_IWMMXT\t(1 \u003c\u003c TIF_USING_IWMMXT)\n \n /* Checks for any syscall work in entry-common.S */\ndiff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S\nindex 88336a1292bb7..59a70d90b0b61 100644\n--- a/arch/arm/kernel/entry-common.S\n+++ b/arch/arm/kernel/entry-common.S\n@@ -83,6 +83,8 @@ ENDPROC(ret_fast_syscall)\n \n \ttst\tr1, #_TIF_SYSCALL_WORK\n \tbne\t__sys_trace_return_nosave\n+\ttst\tr1, #(_TIF_FD_SLOTS \u003c\u003c 16)\t@ r1 holds TI_FLAGS \u003c\u003c 16\n+\tbne\t__sys_trace_return_nosave\n slow_work_pending:\n \tmov\tr0, sp\t\t\t\t@ 'regs'\n \tmov\tr2, why\t\t\t\t@ 'syscall'\ndiff --git a/arch/arm/kernel/ptrace.c b/arch/arm/kernel/ptrace.c\nindex ed7a2a87a6707..75fe26012fcbc 100644\n--- a/arch/arm/kernel/ptrace.c\n+++ b/arch/arm/kernel/ptrace.c\n@@ -11,6 +11,7 @@\n #include \u003clinux/sched/task_stack.h\u003e\n #include \u003clinux/mm.h\u003e\n #include \u003clinux/elf.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/smp.h\u003e\n #include \u003clinux/ptrace.h\u003e\n #include \u003clinux/user.h\u003e\n@@ -876,6 +877,9 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs)\n \n asmlinkage void syscall_trace_exit(struct pt_regs *regs)\n {\n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \t/*\n \t * Audit the syscall before anything else, as a debugger may\n \t * come in and change the current registers.\ndiff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/thread_info.h\nindex 5d7fe3e153c85..e48006eb98a85 100644\n--- a/arch/arm64/include/asm/thread_info.h\n+++ b/arch/arm64/include/asm/thread_info.h\n@@ -74,6 +74,7 @@ void arch_setup_new_exec(void);\n #define TIF_SECCOMP\t\t11\t/* syscall secure computing */\n #define TIF_SYSCALL_EMU\t\t12\t/* syscall emulation active */\n #define TIF_PATCH_PENDING\t13\t/* pending live patching update */\n+#define TIF_FD_SLOTS\t\t14\t/* syscall prepared descriptors */\n #define TIF_MEMDIE\t\t18\t/* is terminating due to OOM killer */\n #define TIF_FREEZE\t\t19\n #define TIF_RESTORE_SIGMASK\t20\n@@ -107,10 +108,11 @@ void arch_setup_new_exec(void);\n #define _TIF_MTE_ASYNC_FAULT\t(1 \u003c\u003c TIF_MTE_ASYNC_FAULT)\n #define _TIF_NOTIFY_SIGNAL\t(1 \u003c\u003c TIF_NOTIFY_SIGNAL)\n #define _TIF_TSC_SIGSEGV\t(1 \u003c\u003c TIF_TSC_SIGSEGV)\n+#define _TIF_FD_SLOTS\t\t(1 \u003c\u003c TIF_FD_SLOTS)\n \n #define _TIF_SYSCALL_WORK\t(_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \\\n \t\t\t\t _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \\\n-\t\t\t\t _TIF_SYSCALL_EMU)\n+\t\t\t\t _TIF_SYSCALL_EMU | _TIF_FD_SLOTS)\n \n #ifdef CONFIG_SHADOW_CALL_STACK\n #define INIT_SCS\t\t\t\t\t\t\t\\\ndiff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c\nindex f743cbec1c3ad..3bedfed977d47 100644\n--- a/arch/arm64/kernel/ptrace.c\n+++ b/arch/arm64/kernel/ptrace.c\n@@ -28,6 +28,7 @@\n #include \u003clinux/hw_breakpoint.h\u003e\n #include \u003clinux/regset.h\u003e\n #include \u003clinux/elf.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/rseq.h\u003e\n \n #include \u003casm/compat.h\u003e\n@@ -2486,6 +2487,9 @@ void syscall_trace_exit(struct pt_regs *regs)\n {\n \tunsigned long flags = read_thread_flags();\n \n+\tif (flags \u0026 _TIF_FD_SLOTS)\n+\t\tfd_slots_commit(regs);\n+\n \taudit_syscall_exit(regs);\n \n \tif (flags \u0026 _TIF_SYSCALL_TRACEPOINT)\ndiff --git a/arch/csky/include/asm/thread_info.h b/arch/csky/include/asm/thread_info.h\nindex fdd4f8ad45acf..59109ce646951 100644\n--- a/arch/csky/include/asm/thread_info.h\n+++ b/arch/csky/include/asm/thread_info.h\n@@ -61,6 +61,7 @@ static inline struct thread_info *current_thread_info(void)\n #define TIF_SYSCALL_TRACEPOINT\t5 /* syscall tracepoint instrumentation */\n #define TIF_SYSCALL_AUDIT\t6\t/* syscall auditing */\n #define TIF_NOTIFY_SIGNAL\t7\t/* signal notifications exist */\n+#define TIF_FD_SLOTS\t\t8\t/* syscall prepared descriptors */\n #define TIF_POLLING_NRFLAG\t16\t/* poll_idle() is TIF_NEED_RESCHED */\n #define TIF_MEMDIE\t\t18 /* is terminating due to OOM killer */\n #define TIF_RESTORE_SIGMASK\t20\t/* restore signal mask in do_signal() */\n@@ -73,6 +74,7 @@ static inline struct thread_info *current_thread_info(void)\n #define _TIF_SYSCALL_TRACEPOINT\t(1 \u003c\u003c TIF_SYSCALL_TRACEPOINT)\n #define _TIF_SYSCALL_AUDIT\t(1 \u003c\u003c TIF_SYSCALL_AUDIT)\n #define _TIF_NOTIFY_SIGNAL\t(1 \u003c\u003c TIF_NOTIFY_SIGNAL)\n+#define _TIF_FD_SLOTS\t\t(1 \u003c\u003c TIF_FD_SLOTS)\n #define _TIF_UPROBE\t\t(1 \u003c\u003c TIF_UPROBE)\n #define _TIF_POLLING_NRFLAG\t(1 \u003c\u003c TIF_POLLING_NRFLAG)\n #define _TIF_MEMDIE\t\t(1 \u003c\u003c TIF_MEMDIE)\ndiff --git a/arch/csky/kernel/entry.S b/arch/csky/kernel/entry.S\nindex 3261f46f22442..6ff3074b9787b 100644\n--- a/arch/csky/kernel/entry.S\n+++ b/arch/csky/kernel/entry.S\n@@ -80,6 +80,15 @@ ENTRY(csky_systemcall)\n \tjsr syscallid\n #endif\n \tstw a0, (sp, LSAVE_A0) /* Save return value */\n+\t/* descriptors reserved by the syscall are committed in syscall_trace_exit() */\n+\tmov r9, sp\n+\tbmaski r10, THREAD_SHIFT\n+\tandn r9, r10\n+\tldw r10, (r9, TINFO_FLAGS)\n+\tlrw\tr9, _TIF_FD_SLOTS\n+\tand\tr10, r9\n+\tcmpnei\tr10, 0\n+\tbt 1f\n \tjmpi ret_from_exception\n \n csky_syscall_trace:\ndiff --git a/arch/csky/kernel/ptrace.c b/arch/csky/kernel/ptrace.c\nindex ee2867a1576e9..b19fef9d396dc 100644\n--- a/arch/csky/kernel/ptrace.c\n+++ b/arch/csky/kernel/ptrace.c\n@@ -4,6 +4,7 @@\n #include \u003clinux/audit.h\u003e\n #include \u003clinux/elf.h\u003e\n #include \u003clinux/errno.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/kernel.h\u003e\n #include \u003clinux/mm.h\u003e\n #include \u003clinux/ptrace.h\u003e\n@@ -335,6 +336,9 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs)\n \n asmlinkage void syscall_trace_exit(struct pt_regs *regs)\n {\n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \taudit_syscall_exit(regs);\n \n \tif (test_thread_flag(TIF_SYSCALL_TRACE))\ndiff --git a/arch/hexagon/include/asm/thread_info.h b/arch/hexagon/include/asm/thread_info.h\nindex e90f280b9ce3e..8558d2d147f87 100644\n--- a/arch/hexagon/include/asm/thread_info.h\n+++ b/arch/hexagon/include/asm/thread_info.h\n@@ -88,6 +88,7 @@ register struct thread_info *__current_thread_info asm(QUOTED_THREADINFO_REG);\n #define TIF_SIGPENDING 2 /* signal pending */\n #define TIF_NEED_RESCHED 3 /* rescheduling necessary */\n #define TIF_SINGLESTEP 4 /* restore ss @ return to usr mode */\n+#define TIF_FD_SLOTS 5 /* syscall prepared descriptors */\n #define TIF_RESTORE_SIGMASK 6 /* restore sig mask in do_signal() */\n #define TIF_NOTIFY_SIGNAL\t7 /* signal notifications exist */\n /* true if poll_idle() is polling TIF_NEED_RESCHED */\n@@ -98,6 +99,7 @@ register struct thread_info *__current_thread_info asm(QUOTED_THREADINFO_REG);\n #define _TIF_SIGPENDING (1 \u003c\u003c TIF_SIGPENDING)\n #define _TIF_NEED_RESCHED (1 \u003c\u003c TIF_NEED_RESCHED)\n #define _TIF_SINGLESTEP (1 \u003c\u003c TIF_SINGLESTEP)\n+#define _TIF_FD_SLOTS (1 \u003c\u003c TIF_FD_SLOTS)\n #define _TIF_NOTIFY_SIGNAL\t(1 \u003c\u003c TIF_NOTIFY_SIGNAL)\n \n /* work to do on interrupt/exception return - All but TIF_SYSCALL_TRACE */\ndiff --git a/arch/hexagon/kernel/traps.c b/arch/hexagon/kernel/traps.c\nindex 6fe0c1b62c1b3..4d15c5212fd78 100644\n--- a/arch/hexagon/kernel/traps.c\n+++ b/arch/hexagon/kernel/traps.c\n@@ -6,6 +6,7 @@\n */\n \n #include \u003clinux/init.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/sched/signal.h\u003e\n #include \u003clinux/sched/debug.h\u003e\n #include \u003clinux/sched/task_stack.h\u003e\n@@ -381,6 +382,9 @@ void do_trap0(struct pt_regs *regs)\n \t\t\t\t regs-\u003er04, regs-\u003er05);\n \t\t}\n \n+\t\tif (unlikely(test_thread_flag(TIF_FD_SLOTS)))\n+\t\t\tfd_slots_commit(regs);\n+\n \t\t/* allow strace to get the syscall return state */\n \t\tif (unlikely(test_thread_flag(TIF_SYSCALL_TRACE)))\n \t\t\tptrace_report_syscall_exit(regs, 0);\ndiff --git a/arch/m68k/68000/entry.S b/arch/m68k/68000/entry.S\nindex c257cc415c478..479032af2f2f0 100644\n--- a/arch/m68k/68000/entry.S\n+++ b/arch/m68k/68000/entry.S\n@@ -89,6 +89,16 @@ ENTRY(system_call)\n \tjbsr\t%a0@\n \tmovel\t%d0,%sp@(PT_OFF_D0)\t/* save the return value*/\n \n+\t/* descriptors reserved by the syscall are committed in syscall_trace_leave */\n+\tgetthreadinfo\n+\tbtst\t#(TIF_FD_SLOTS%8),%a2@(TINFO_FLAGS+(31-TIF_FD_SLOTS)/8)\n+\tjeq\tret_from_exception\n+\tsubql\t#4,%sp\t\t\t/* dummy return address */\n+\tSAVE_SWITCH_STACK\n+\tjbsr\tsyscall_trace_leave\n+\tRESTORE_SWITCH_STACK\n+\taddql\t#4,%sp\n+\n ret_from_exception:\n \tbtst\t#5,%sp@(PT_OFF_SR)\t/* check if returning to kernel*/\n \tjeq\tLuser_return\t\t/* if so, skip resched, signals*/\ndiff --git a/arch/m68k/coldfire/entry.S b/arch/m68k/coldfire/entry.S\nindex 4ea08336e2fb0..43e47d4c00187 100644\n--- a/arch/m68k/coldfire/entry.S\n+++ b/arch/m68k/coldfire/entry.S\n@@ -78,6 +78,18 @@ ENTRY(system_call)\n \tmovel\t%d3,%a0\n \tjbsr\t%a0@\n \tmovel\t%d0,%sp@(PT_OFF_D0)\t/* save the return value */\n+\n+\t/* descriptors reserved by the syscall are committed in syscall_trace_leave */\n+\tmovel\t%sp,%d2\t\t\t/* get thread_info pointer */\n+\tandl\t#-THREAD_SIZE,%d2\t/* at start of kernel stack */\n+\tmovel\t%d2,%a0\n+\tbtst\t#(TIF_FD_SLOTS%8),%a0@(TINFO_FLAGS+(31-TIF_FD_SLOTS)/8)\n+\tjeq\tret_from_exception\n+\tsubql\t#4,%sp\t\t\t/* dummy return address */\n+\tSAVE_SWITCH_STACK\n+\tjbsr\tsyscall_trace_leave\n+\tRESTORE_SWITCH_STACK\n+\taddql\t#4,%sp\n \tjra\tret_from_exception\n 1:\n \tmovel\t#-ENOSYS,%d2\t\t/* strace needs -ENOSYS in PT_OFF_D0 */\ndiff --git a/arch/m68k/include/asm/thread_info.h b/arch/m68k/include/asm/thread_info.h\nindex 5cb3ace556222..861b6f523bda4 100644\n--- a/arch/m68k/include/asm/thread_info.h\n+++ b/arch/m68k/include/asm/thread_info.h\n@@ -62,6 +62,7 @@ static inline struct thread_info *current_thread_info(void)\n #define TIF_NOTIFY_RESUME\t5\t/* callback before returning to user */\n #define TIF_SIGPENDING\t\t6\t/* signal pending */\n #define TIF_NEED_RESCHED\t7\t/* rescheduling necessary */\n+#define TIF_FD_SLOTS\t\t12\t/* syscall prepared descriptors */\n #define TIF_SECCOMP\t\t13\t/* seccomp syscall filtering active */\n #define TIF_DELAYED_TRACE\t14\t/* single step a syscall */\n #define TIF_SYSCALL_TRACE\t15\t/* syscall trace active */\n@@ -71,6 +72,7 @@ static inline struct thread_info *current_thread_info(void)\n #define _TIF_NOTIFY_RESUME\t(1 \u003c\u003c TIF_NOTIFY_RESUME)\n #define _TIF_SIGPENDING\t\t(1 \u003c\u003c TIF_SIGPENDING)\n #define _TIF_NEED_RESCHED\t(1 \u003c\u003c TIF_NEED_RESCHED)\n+#define _TIF_FD_SLOTS\t\t(1 \u003c\u003c TIF_FD_SLOTS)\n #define _TIF_SECCOMP\t\t(1 \u003c\u003c TIF_SECCOMP)\n #define _TIF_DELAYED_TRACE\t(1 \u003c\u003c TIF_DELAYED_TRACE)\n #define _TIF_SYSCALL_TRACE\t(1 \u003c\u003c TIF_SYSCALL_TRACE)\ndiff --git a/arch/m68k/kernel/entry.S b/arch/m68k/kernel/entry.S\nindex 338b474910f74..9e4471f681652 100644\n--- a/arch/m68k/kernel/entry.S\n+++ b/arch/m68k/kernel/entry.S\n@@ -229,6 +229,8 @@ ret_from_syscall:\n syscall_exit_work:\n \tbtst\t#5,%sp@(PT_OFF_SR)\t| check if returning to kernel\n \tbnes\t1b\t\t\t| if so, skip resched, signals\n+\tbtst\t#4,%a1@(TINFO_FLAGS+2)\t| TIF_FD_SLOTS: commit descriptors\n+\tjne\tdo_trace_exit\n \tlslw\t#1,%d0\n \tjcs\tdo_trace_exit\n \tjmi\tdo_delayed_trace\ndiff --git a/arch/m68k/kernel/ptrace.c b/arch/m68k/kernel/ptrace.c\nindex 4575f6487a9aa..07103e6ddeb04 100644\n--- a/arch/m68k/kernel/ptrace.c\n+++ b/arch/m68k/kernel/ptrace.c\n@@ -11,6 +11,7 @@\n */\n \n #include \u003clinux/kernel.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/sched.h\u003e\n #include \u003clinux/sched/task_stack.h\u003e\n #include \u003clinux/mm.h\u003e\n@@ -289,6 +290,8 @@ asmlinkage int syscall_trace_enter(void)\n \n asmlinkage void syscall_trace_leave(void)\n {\n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(task_pt_regs(current));\n \tif (test_thread_flag(TIF_SYSCALL_TRACE))\n \t\tptrace_report_syscall_exit(task_pt_regs(current), 0);\n }\ndiff --git a/arch/microblaze/include/asm/thread_info.h b/arch/microblaze/include/asm/thread_info.h\nindex 0153f7c2717c9..ee2f76589696b 100644\n--- a/arch/microblaze/include/asm/thread_info.h\n+++ b/arch/microblaze/include/asm/thread_info.h\n@@ -103,6 +103,7 @@ static inline struct thread_info *current_thread_info(void)\n #define TIF_SINGLESTEP\t\t4\n #define TIF_NOTIFY_SIGNAL\t5\t/* signal notifications exist */\n #define TIF_MEMDIE\t\t6\t/* is terminating due to OOM killer */\n+#define TIF_FD_SLOTS\t\t7\t/* syscall prepared descriptors */\n #define TIF_SYSCALL_AUDIT\t9 /* syscall auditing active */\n #define TIF_SECCOMP\t\t10 /* secure computing */\n \n@@ -118,10 +119,12 @@ static inline struct thread_info *current_thread_info(void)\n #define _TIF_POLLING_NRFLAG\t(1 \u003c\u003c TIF_POLLING_NRFLAG)\n #define _TIF_SYSCALL_AUDIT\t(1 \u003c\u003c TIF_SYSCALL_AUDIT)\n #define _TIF_SECCOMP\t\t(1 \u003c\u003c TIF_SECCOMP)\n+#define _TIF_FD_SLOTS\t\t(1 \u003c\u003c TIF_FD_SLOTS)\n \n /* work to do in syscall trace */\n #define _TIF_WORK_SYSCALL_MASK (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | \\\n-\t\t\t\t _TIF_SYSCALL_AUDIT | _TIF_SECCOMP)\n+\t\t\t\t _TIF_SYSCALL_AUDIT | _TIF_SECCOMP | \\\n+\t\t\t\t _TIF_FD_SLOTS)\n \n /* work to do on interrupt/exception return */\n #define _TIF_WORK_MASK\t\t0x0000FFFE\ndiff --git a/arch/microblaze/kernel/ptrace.c b/arch/microblaze/kernel/ptrace.c\nindex 236264e932d64..e6951fea85937 100644\n--- a/arch/microblaze/kernel/ptrace.c\n+++ b/arch/microblaze/kernel/ptrace.c\n@@ -25,6 +25,7 @@\n */\n \n #include \u003clinux/kernel.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/mm.h\u003e\n #include \u003clinux/sched.h\u003e\n #include \u003clinux/sched/task_stack.h\u003e\n@@ -156,6 +157,9 @@ asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)\n {\n \tint step;\n \n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \taudit_syscall_exit(regs);\n \n \tstep = test_thread_flag(TIF_SINGLESTEP);\ndiff --git a/arch/mips/include/asm/thread_info.h b/arch/mips/include/asm/thread_info.h\nindex 2707dad260dd7..79c81696e9c19 100644\n--- a/arch/mips/include/asm/thread_info.h\n+++ b/arch/mips/include/asm/thread_info.h\n@@ -114,6 +114,7 @@ register unsigned long current_stack_pointer __asm__(\"sp\");\n #define TIF_NOTIFY_RESUME\t5\t/* callback before returning to user */\n #define TIF_UPROBE\t\t6\t/* breakpointed or singlestepping */\n #define TIF_NOTIFY_SIGNAL\t7\t/* signal notifications exist */\n+#define TIF_FD_SLOTS\t\t8\t/* syscall prepared descriptors */\n #define TIF_RESTORE_SIGMASK\t9\t/* restore signal mask in do_signal() */\n #define TIF_USEDFPU\t\t16\t/* FPU was used by this task this quantum (SMP) */\n #define TIF_MEMDIE\t\t18\t/* is terminating due to OOM killer */\n@@ -152,6 +153,7 @@ register unsigned long current_stack_pointer __asm__(\"sp\");\n #define _TIF_USEDMSA\t\t(1\u003c\u003cTIF_USEDMSA)\n #define _TIF_MSA_CTX_LIVE\t(1\u003c\u003cTIF_MSA_CTX_LIVE)\n #define _TIF_SYSCALL_TRACEPOINT\t(1\u003c\u003cTIF_SYSCALL_TRACEPOINT)\n+#define _TIF_FD_SLOTS\t\t(1\u003c\u003cTIF_FD_SLOTS)\n \n #define _TIF_WORK_SYSCALL_ENTRY\t(_TIF_NOHZ | _TIF_SYSCALL_TRACE |\t\\\n \t\t\t\t _TIF_SYSCALL_AUDIT | \\\n@@ -159,7 +161,8 @@ register unsigned long current_stack_pointer __asm__(\"sp\");\n \n /* work to do in syscall_trace_leave() */\n #define _TIF_WORK_SYSCALL_EXIT\t(_TIF_NOHZ | _TIF_SYSCALL_TRACE |\t\\\n-\t\t\t\t _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_TRACEPOINT)\n+\t\t\t\t _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_TRACEPOINT | \\\n+\t\t\t\t _TIF_FD_SLOTS)\n \n /* work to do on interrupt/exception return */\n #define _TIF_WORK_MASK\t\t\\\ndiff --git a/arch/mips/kernel/ptrace.c b/arch/mips/kernel/ptrace.c\nindex 17dc7249b8cac..d68eab59bcc21 100644\n--- a/arch/mips/kernel/ptrace.c\n+++ b/arch/mips/kernel/ptrace.c\n@@ -17,6 +17,7 @@\n #include \u003clinux/compiler.h\u003e\n #include \u003clinux/context_tracking.h\u003e\n #include \u003clinux/elf.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/kernel.h\u003e\n #include \u003clinux/sched.h\u003e\n #include \u003clinux/sched/task_stack.h\u003e\n@@ -1364,6 +1365,9 @@ asmlinkage void syscall_trace_leave(struct pt_regs *regs)\n \t */\n \tuser_exit();\n \n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \taudit_syscall_exit(regs);\n \n \tif (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))\ndiff --git a/arch/nios2/include/asm/thread_info.h b/arch/nios2/include/asm/thread_info.h\nindex 83df79286d62e..7f5816e1b3d6e 100644\n--- a/arch/nios2/include/asm/thread_info.h\n+++ b/arch/nios2/include/asm/thread_info.h\n@@ -78,6 +78,7 @@ static inline struct thread_info *current_thread_info(void)\n #define TIF_SECCOMP\t\t5\t/* secure computing */\n #define TIF_SYSCALL_AUDIT\t6\t/* syscall auditing active */\n #define TIF_NOTIFY_SIGNAL\t7\t/* signal notifications exist */\n+#define TIF_FD_SLOTS\t\t8\t/* syscall prepared descriptors */\n #define TIF_RESTORE_SIGMASK\t9\t/* restore signal mask in do_signal() */\n \n #define TIF_POLLING_NRFLAG\t16\t/* true if poll_idle() is polling\n@@ -90,6 +91,7 @@ static inline struct thread_info *current_thread_info(void)\n #define _TIF_SECCOMP\t\t(1 \u003c\u003c TIF_SECCOMP)\n #define _TIF_SYSCALL_AUDIT\t(1 \u003c\u003c TIF_SYSCALL_AUDIT)\n #define _TIF_NOTIFY_SIGNAL\t(1 \u003c\u003c TIF_NOTIFY_SIGNAL)\n+#define _TIF_FD_SLOTS\t\t(1 \u003c\u003c TIF_FD_SLOTS)\n #define _TIF_RESTORE_SIGMASK\t(1 \u003c\u003c TIF_RESTORE_SIGMASK)\n #define _TIF_POLLING_NRFLAG\t(1 \u003c\u003c TIF_POLLING_NRFLAG)\n \ndiff --git a/arch/nios2/kernel/entry.S b/arch/nios2/kernel/entry.S\nindex dd40dfd908e59..b3a99a6db379e 100644\n--- a/arch/nios2/kernel/entry.S\n+++ b/arch/nios2/kernel/entry.S\n@@ -222,6 +222,11 @@ translate_rc_and_ret:\n \tstw\tr2, PT_R2(sp)\n \tstw\tr1, PT_R7(sp)\n end_translate_rc_and_ret:\n+\t/* Descriptors reserved by the syscall are committed in\n+\t * do_syscall_trace_exit, take the traced exit path for them. */\n+\tGET_THREAD_INFO r11\n+\tldw\tr11, TI_FLAGS(r11)\n+\tBTBNZ\tr11, r11, TIF_FD_SLOTS, end_translate_rc_and_ret2\n \n ret_from_exception:\n \tldw\tr1, PT_ESTATUS(sp)\ndiff --git a/arch/nios2/kernel/ptrace.c b/arch/nios2/kernel/ptrace.c\nindex 2cd7e1ecbd74b..5cee1b717662f 100644\n--- a/arch/nios2/kernel/ptrace.c\n+++ b/arch/nios2/kernel/ptrace.c\n@@ -9,6 +9,7 @@\n \n #include \u003clinux/elf.h\u003e\n #include \u003clinux/errno.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/kernel.h\u003e\n #include \u003clinux/mm.h\u003e\n #include \u003clinux/ptrace.h\u003e\n@@ -140,6 +141,8 @@ asmlinkage int do_syscall_trace_enter(void)\n \n asmlinkage void do_syscall_trace_exit(void)\n {\n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(task_pt_regs(current));\n \tif (test_thread_flag(TIF_SYSCALL_TRACE))\n \t\tptrace_report_syscall_exit(task_pt_regs(current), 0);\n }\ndiff --git a/arch/openrisc/include/asm/thread_info.h b/arch/openrisc/include/asm/thread_info.h\nindex e338fff7efb0e..3d8a710a809d2 100644\n--- a/arch/openrisc/include/asm/thread_info.h\n+++ b/arch/openrisc/include/asm/thread_info.h\n@@ -92,6 +92,7 @@ register struct thread_info *current_thread_info_reg asm(\"r10\");\n \t\t\t\t\t * mode\n \t\t\t\t\t */\n #define TIF_NOTIFY_SIGNAL\t5\t/* signal notifications exist */\n+#define TIF_FD_SLOTS\t\t6\t/* syscall prepared descriptors */\n #define TIF_SYSCALL_TRACEPOINT 8 /* for ftrace syscall instrumentation */\n #define TIF_RESTORE_SIGMASK 9\n #define TIF_POLLING_NRFLAG\t16\t/* true if poll_idle() is polling\t\t\t\t\t\t * TIF_NEED_RESCHED\n@@ -104,12 +105,14 @@ register struct thread_info *current_thread_info_reg asm(\"r10\");\n #define _TIF_NEED_RESCHED\t(1\u003c\u003cTIF_NEED_RESCHED)\n #define _TIF_SINGLESTEP\t\t(1\u003c\u003cTIF_SINGLESTEP)\n #define _TIF_NOTIFY_SIGNAL\t(1\u003c\u003cTIF_NOTIFY_SIGNAL)\n+#define _TIF_FD_SLOTS\t\t(1\u003c\u003cTIF_FD_SLOTS)\n #define _TIF_POLLING_NRFLAG\t(1\u003c\u003cTIF_POLLING_NRFLAG)\n \n \n /* Work to do when returning from interrupt/exception */\n /* For OpenRISC, this is anything in the LSW other than syscall trace */\n-#define _TIF_WORK_MASK (0xff \u0026 ~(_TIF_SYSCALL_TRACE|_TIF_SINGLESTEP))\n+#define _TIF_WORK_MASK (0xff \u0026 ~(_TIF_SYSCALL_TRACE|_TIF_SINGLESTEP| \\\n+\t\t\t\t _TIF_FD_SLOTS))\n \n #endif /* __KERNEL__ */\n \ndiff --git a/arch/openrisc/kernel/entry.S b/arch/openrisc/kernel/entry.S\nindex 18e68680471eb..c70265c4fe555 100644\n--- a/arch/openrisc/kernel/entry.S\n+++ b/arch/openrisc/kernel/entry.S\n@@ -701,10 +701,11 @@ _syscall_show_regs:\n #endif\n \n _syscall_check_trace_leave:\n-\t/* r30 is a callee-saved register so this should still hold the\n-\t * _TIF_SYSCALL_TRACE flag from _syscall_check_trace_enter above...\n+\t/* Reload the flags, the syscall may have set TIF_FD_SLOTS.\n \t * _syscall_trace_leave expects syscall result to be in pt_regs-\u003er11.\n \t */\n+\tl.lwz\tr30,TI_FLAGS(r10)\n+\tl.andi\tr30,r30,(_TIF_SYSCALL_TRACE|_TIF_FD_SLOTS)\n \tl.sfne\tr30,r0\n \tl.bf\t_syscall_trace_leave\n \t l.nop\ndiff --git a/arch/openrisc/kernel/ptrace.c b/arch/openrisc/kernel/ptrace.c\nindex 287a9718f0c60..71614b36e5162 100644\n--- a/arch/openrisc/kernel/ptrace.c\n+++ b/arch/openrisc/kernel/ptrace.c\n@@ -13,6 +13,7 @@\n */\n \n #include \u003clinux/kernel.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/sched.h\u003e\n #include \u003clinux/sched/task_stack.h\u003e\n #include \u003clinux/string.h\u003e\n@@ -311,6 +312,9 @@ asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)\n {\n \tint step;\n \n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \taudit_syscall_exit(regs);\n \n \tstep = test_thread_flag(TIF_SINGLESTEP);\ndiff --git a/arch/parisc/include/asm/thread_info.h b/arch/parisc/include/asm/thread_info.h\nindex b283738bb6dab..14a40c53689f9 100644\n--- a/arch/parisc/include/asm/thread_info.h\n+++ b/arch/parisc/include/asm/thread_info.h\n@@ -52,6 +52,7 @@ struct thread_info {\n #define TIF_SECCOMP\t\t11\t/* secure computing */\n #define TIF_SYSCALL_TRACEPOINT\t12\t/* syscall tracepoint instrumentation */\n #define TIF_NONBLOCK_WARNING\t13\t/* warned about wrong O_NONBLOCK usage */\n+#define TIF_FD_SLOTS\t\t14\t/* syscall prepared descriptors */\n \n #define _TIF_SYSCALL_TRACE\t(1 \u003c\u003c TIF_SYSCALL_TRACE)\n #define _TIF_SIGPENDING\t\t(1 \u003c\u003c TIF_SIGPENDING)\n@@ -65,6 +66,7 @@ struct thread_info {\n #define _TIF_BLOCKSTEP\t\t(1 \u003c\u003c TIF_BLOCKSTEP)\n #define _TIF_SECCOMP\t\t(1 \u003c\u003c TIF_SECCOMP)\n #define _TIF_SYSCALL_TRACEPOINT\t(1 \u003c\u003c TIF_SYSCALL_TRACEPOINT)\n+#define _TIF_FD_SLOTS\t\t(1 \u003c\u003c TIF_FD_SLOTS)\n \n #define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | \\\n _TIF_NEED_RESCHED | _TIF_NOTIFY_SIGNAL)\ndiff --git a/arch/parisc/kernel/entry.S b/arch/parisc/kernel/entry.S\nindex e04c5d806c105..c5807cf6fbd8d 100644\n--- a/arch/parisc/kernel/entry.S\n+++ b/arch/parisc/kernel/entry.S\n@@ -1732,6 +1732,8 @@ syscall_check_resched:\n \t/* check for reschedule */\n \tmfctl\t%cr30,%r19\n \tLDREG\tTASK_TI_FLAGS(%r19),%r19\t/* long */\n+\t/* descriptors reserved by the syscall are committed in do_syscall_trace_exit */\n+\tbb,\u003c,n\t%r19, 31-TIF_FD_SLOTS, syscall_do_fd_slots /* forward */\n \tbb,\u003c,n\t%r19, 31-TIF_NEED_RESCHED, syscall_do_resched /* forward */\n \n \t.import do_signal,code\n@@ -1765,6 +1767,16 @@ syscall_do_signal:\n \n \tb,n syscall_check_sig\n \n+syscall_do_fd_slots:\n+\tmfctl\t%cr30,%r1\n+#ifdef CONFIG_64BIT\n+\tldo\t-16(%r30),%r29\t\t\t/* Reference param save area */\n+#endif\n+\tldo\tTASK_REGS(%r1),%r26\t\t/* struct pt_regs *regs */\n+\tBL\tdo_syscall_trace_exit,%r2\n+\tnop\n+\tb,n\tsyscall_check_resched\n+\n syscall_restore:\n \tmfctl\t%cr30,%r1\n \ndiff --git a/arch/parisc/kernel/ptrace.c b/arch/parisc/kernel/ptrace.c\nindex 1d9e210702e1f..f142766792afa 100644\n--- a/arch/parisc/kernel/ptrace.c\n+++ b/arch/parisc/kernel/ptrace.c\n@@ -9,6 +9,7 @@\n */\n \n #include \u003clinux/kernel.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/sched.h\u003e\n #include \u003clinux/mm.h\u003e\n #include \u003clinux/smp.h\u003e\n@@ -381,6 +382,9 @@ void do_syscall_trace_exit(struct pt_regs *regs)\n \tint stepping = test_thread_flag(TIF_SINGLESTEP) ||\n \t\ttest_thread_flag(TIF_BLOCKSTEP);\n \n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \taudit_syscall_exit(regs);\n \n #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS\ndiff --git a/arch/powerpc/platforms/cell/spufs/inode.c b/arch/powerpc/platforms/cell/spufs/inode.c\nindex 2b54afb31529f..b3e9204e70312 100644\n--- a/arch/powerpc/platforms/cell/spufs/inode.c\n+++ b/arch/powerpc/platforms/cell/spufs/inode.c\n@@ -266,10 +266,10 @@ spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,\n static int spufs_context_open(const struct path *path)\n {\n \tFD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred()));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \tfd_prepare_file(fdf)-\u003ef_op = \u0026spufs_context_fops;\n-\treturn fd_publish(fdf);\n+\treturn fd_prepare_fd(fdf);\n }\n \n static struct spu_context *\n@@ -499,10 +499,10 @@ static int spufs_gang_open(const struct path *path)\n \t * in error path of *_open().\n \t */\n \tFD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred()));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \tfd_prepare_file(fdf)-\u003ef_op = \u0026spufs_gang_fops;\n-\treturn fd_publish(fdf);\n+\treturn fd_prepare_fd(fdf);\n }\n \n static int spufs_create_gang(struct inode *inode,\ndiff --git a/arch/sh/include/asm/thread_info.h b/arch/sh/include/asm/thread_info.h\nindex 471db51730361..d976cf21f3463 100644\n--- a/arch/sh/include/asm/thread_info.h\n+++ b/arch/sh/include/asm/thread_info.h\n@@ -106,6 +106,7 @@ extern void init_thread_xstate(void);\n #define TIF_SECCOMP\t\t6\t/* secure computing */\n #define TIF_NOTIFY_RESUME\t7\t/* callback before returning to user */\n #define TIF_SYSCALL_TRACEPOINT\t8\t/* for ftrace syscall instrumentation */\n+#define TIF_FD_SLOTS\t\t9\t/* syscall prepared descriptors */\n #define TIF_POLLING_NRFLAG\t17\t/* true if poll_idle() is polling TIF_NEED_RESCHED */\n #define TIF_MEMDIE\t\t18\t/* is terminating due to OOM killer */\n \n@@ -118,22 +119,25 @@ extern void init_thread_xstate(void);\n #define _TIF_SECCOMP\t\t(1 \u003c\u003c TIF_SECCOMP)\n #define _TIF_NOTIFY_RESUME\t(1 \u003c\u003c TIF_NOTIFY_RESUME)\n #define _TIF_SYSCALL_TRACEPOINT\t(1 \u003c\u003c TIF_SYSCALL_TRACEPOINT)\n+#define _TIF_FD_SLOTS\t\t(1 \u003c\u003c TIF_FD_SLOTS)\n #define _TIF_POLLING_NRFLAG\t(1 \u003c\u003c TIF_POLLING_NRFLAG)\n \n /* work to do in syscall trace */\n #define _TIF_WORK_SYSCALL_MASK\t(_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | \\\n \t\t\t\t _TIF_SYSCALL_AUDIT | _TIF_SECCOMP | \\\n-\t\t\t\t _TIF_SYSCALL_TRACEPOINT)\n+\t\t\t\t _TIF_SYSCALL_TRACEPOINT | _TIF_FD_SLOTS)\n \n /* work to do on any return to u-space */\n #define _TIF_ALLWORK_MASK\t(_TIF_SYSCALL_TRACE | _TIF_SIGPENDING | \\\n \t\t\t\t _TIF_NEED_RESCHED | _TIF_SYSCALL_AUDIT | \\\n \t\t\t\t _TIF_SINGLESTEP | _TIF_NOTIFY_RESUME | \\\n-\t\t\t\t _TIF_SYSCALL_TRACEPOINT | _TIF_NOTIFY_SIGNAL)\n+\t\t\t\t _TIF_SYSCALL_TRACEPOINT | _TIF_NOTIFY_SIGNAL | \\\n+\t\t\t\t _TIF_FD_SLOTS)\n \n /* work to do on interrupt/exception return */\n #define _TIF_WORK_MASK\t\t(_TIF_ALLWORK_MASK \u0026 ~(_TIF_SYSCALL_TRACE | \\\n-\t\t\t\t _TIF_SYSCALL_AUDIT | _TIF_SINGLESTEP))\n+\t\t\t\t _TIF_SYSCALL_AUDIT | _TIF_SINGLESTEP | \\\n+\t\t\t\t _TIF_FD_SLOTS))\n \n /*\n * Thread-synchronous status.\ndiff --git a/arch/sh/kernel/entry-common.S b/arch/sh/kernel/entry-common.S\nindex 91ab2607a1ff9..593a482e85f52 100644\n--- a/arch/sh/kernel/entry-common.S\n+++ b/arch/sh/kernel/entry-common.S\n@@ -164,9 +164,13 @@ work_resched:\n \n \t.align\t2\n syscall_exit_work:\n-\t! r0: current_thread_info-\u003eflags\n \t! r8: current_thread_info\n-\ttst\t#(_TIF_WORK_SYSCALL_MASK \u0026 0xff), r0\n+\tmov.l\t@(TI_FLAGS,r8), r0\t\t! current_thread_info-\u003eflags\n+\tmov\t#(_TIF_WORK_SYSCALL_MASK \u0026 0xff), r1\n+\tmov\t#(_TIF_WORK_SYSCALL_MASK \u003e\u003e 8), r2\n+\tshll8\tr2\n+\tor\tr2, r1\n+\ttst\tr1, r0\n \tbt/s\twork_pending\n \t tst\t#_TIF_NEED_RESCHED, r0\n \tTRACE_IRQS_ON\ndiff --git a/arch/sh/kernel/ptrace_32.c b/arch/sh/kernel/ptrace_32.c\nindex 8794081483fbd..3deadd71f464c 100644\n--- a/arch/sh/kernel/ptrace_32.c\n+++ b/arch/sh/kernel/ptrace_32.c\n@@ -8,6 +8,7 @@\n * Audit support by Yuichi Nakamura \u003cynakam@hitachisoft.jp\u003e\n */\n #include \u003clinux/kernel.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/sched.h\u003e\n #include \u003clinux/sched/task_stack.h\u003e\n #include \u003clinux/mm.h\u003e\n@@ -476,6 +477,9 @@ asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)\n {\n \tint step;\n \n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \taudit_syscall_exit(regs);\n \n \tif (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))\ndiff --git a/arch/sparc/include/asm/thread_info_32.h b/arch/sparc/include/asm/thread_info_32.h\nindex fdaf7b171e0ac..fbd7af6bb4271 100644\n--- a/arch/sparc/include/asm/thread_info_32.h\n+++ b/arch/sparc/include/asm/thread_info_32.h\n@@ -105,6 +105,7 @@ register struct thread_info *current_thread_info_reg asm(\"g6\");\n #define TIF_NEED_RESCHED\t3\t/* rescheduling necessary */\n #define TIF_RESTORE_SIGMASK\t4\t/* restore signal mask in do_signal() */\n #define TIF_NOTIFY_SIGNAL\t5\t/* signal notifications exist */\n+#define TIF_FD_SLOTS\t\t6\t/* syscall prepared descriptors */\n #define TIF_USEDFPU\t\t8\t/* FPU was used by this task\n \t\t\t\t\t * this quantum (SMP) */\n #define TIF_POLLING_NRFLAG\t9\t/* true if poll_idle() is polling\n@@ -117,6 +118,7 @@ register struct thread_info *current_thread_info_reg asm(\"g6\");\n #define _TIF_SIGPENDING\t\t(1\u003c\u003cTIF_SIGPENDING)\n #define _TIF_NEED_RESCHED\t(1\u003c\u003cTIF_NEED_RESCHED)\n #define _TIF_NOTIFY_SIGNAL\t(1\u003c\u003cTIF_NOTIFY_SIGNAL)\n+#define _TIF_FD_SLOTS\t\t(1\u003c\u003cTIF_FD_SLOTS)\n #define _TIF_USEDFPU\t\t(1\u003c\u003cTIF_USEDFPU)\n #define _TIF_POLLING_NRFLAG\t(1\u003c\u003cTIF_POLLING_NRFLAG)\n \ndiff --git a/arch/sparc/include/asm/thread_info_64.h b/arch/sparc/include/asm/thread_info_64.h\nindex c8a73dff27f80..890ee65d90290 100644\n--- a/arch/sparc/include/asm/thread_info_64.h\n+++ b/arch/sparc/include/asm/thread_info_64.h\n@@ -180,7 +180,7 @@ extern struct thread_info *current_thread_info(void);\n #define TIF_SIGPENDING\t\t2\t/* signal pending */\n #define TIF_NEED_RESCHED\t3\t/* rescheduling necessary */\n #define TIF_NOTIFY_SIGNAL\t4\t/* signal notifications exist */\n-#define TIF_UNALIGNED\t\t5\t/* allowed to do unaligned accesses */\n+#define TIF_FD_SLOTS\t\t5\t/* syscall prepared descriptors */\n #define TIF_UPROBE\t\t6\t/* breakpointed or singlestepped */\n #define TIF_32BIT\t\t7\t/* 32-bit binary */\n #define TIF_NOHZ\t\t8\t/* in adaptive nohz mode */\n@@ -194,12 +194,14 @@ extern struct thread_info *current_thread_info(void);\n #define TIF_MCDPER\t\t12\t/* Precise MCD exception */\n #define TIF_MEMDIE\t\t13\t/* is terminating due to OOM killer */\n #define TIF_POLLING_NRFLAG\t14\n+#define TIF_UNALIGNED\t\t15\t/* allowed to do unaligned accesses */\n \n #define _TIF_SYSCALL_TRACE\t(1\u003c\u003cTIF_SYSCALL_TRACE)\n #define _TIF_NOTIFY_RESUME\t(1\u003c\u003cTIF_NOTIFY_RESUME)\n #define _TIF_SIGPENDING\t\t(1\u003c\u003cTIF_SIGPENDING)\n #define _TIF_NEED_RESCHED\t(1\u003c\u003cTIF_NEED_RESCHED)\n #define _TIF_NOTIFY_SIGNAL\t(1\u003c\u003cTIF_NOTIFY_SIGNAL)\n+#define _TIF_FD_SLOTS\t\t(1\u003c\u003cTIF_FD_SLOTS)\n #define _TIF_UNALIGNED\t\t(1\u003c\u003cTIF_UNALIGNED)\n #define _TIF_UPROBE\t\t(1\u003c\u003cTIF_UPROBE)\n #define _TIF_32BIT\t\t(1\u003c\u003cTIF_32BIT)\n@@ -209,6 +211,11 @@ extern struct thread_info *current_thread_info(void);\n #define _TIF_SYSCALL_TRACEPOINT\t(1\u003c\u003cTIF_SYSCALL_TRACEPOINT)\n #define _TIF_POLLING_NRFLAG\t(1\u003c\u003cTIF_POLLING_NRFLAG)\n \n+/* work to do in syscall_trace_leave() */\n+#define _TIF_SYSCALL_EXIT_WORK\t(_TIF_SYSCALL_TRACE | _TIF_SECCOMP | \\\n+\t\t\t\t _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_TRACEPOINT | \\\n+\t\t\t\t _TIF_NOHZ | _TIF_FD_SLOTS)\n+\n #define _TIF_USER_WORK_MASK\t((0xff \u003c\u003c TI_FLAG_WSAVED_SHIFT) | \\\n \t\t\t\t _TIF_DO_NOTIFY_RESUME_MASK | \\\n \t\t\t\t _TIF_NEED_RESCHED)\ndiff --git a/arch/sparc/kernel/entry.S b/arch/sparc/kernel/entry.S\nindex ea51ef52c952a..d2023b84db9e2 100644\n--- a/arch/sparc/kernel/entry.S\n+++ b/arch/sparc/kernel/entry.S\n@@ -1023,7 +1023,7 @@ ret_sys_call:\n \tld\t[%sp + STACKFRAME_SZ + PT_PSR], %g3\n \tset\tPSR_C, %g2\n \tbgeu\t1f\n-\t andcc\t%l5, _TIF_SYSCALL_TRACE, %g0\n+\t andcc\t%l5, (_TIF_SYSCALL_TRACE | _TIF_FD_SLOTS), %g0\n \n \t/* System call success, clear Carry condition code. */\n \tandn\t%g3, %g2, %g3\ndiff --git a/arch/sparc/kernel/ptrace_32.c b/arch/sparc/kernel/ptrace_32.c\nindex 5991b1731feed..042e606854183 100644\n--- a/arch/sparc/kernel/ptrace_32.c\n+++ b/arch/sparc/kernel/ptrace_32.c\n@@ -11,6 +11,7 @@\n */\n \n #include \u003clinux/kernel.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/sched.h\u003e\n #include \u003clinux/mm.h\u003e\n #include \u003clinux/errno.h\u003e\n@@ -437,6 +438,9 @@ asmlinkage int syscall_trace(struct pt_regs *regs, int syscall_exit_p)\n {\n \tint ret = 0;\n \n+\tif (syscall_exit_p \u0026\u0026 test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \tif (test_thread_flag(TIF_SYSCALL_TRACE)) {\n \t\tif (syscall_exit_p)\n \t\t\tptrace_report_syscall_exit(regs, 0);\ndiff --git a/arch/sparc/kernel/ptrace_64.c b/arch/sparc/kernel/ptrace_64.c\nindex 825ddf55fece3..499b30c505fdd 100644\n--- a/arch/sparc/kernel/ptrace_64.c\n+++ b/arch/sparc/kernel/ptrace_64.c\n@@ -12,6 +12,7 @@\n */\n \n #include \u003clinux/kernel.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/sched.h\u003e\n #include \u003clinux/sched/task_stack.h\u003e\n #include \u003clinux/mm.h\u003e\n@@ -1110,6 +1111,9 @@ asmlinkage void syscall_trace_leave(struct pt_regs *regs)\n \tif (test_thread_flag(TIF_NOHZ))\n \t\tuser_exit();\n \n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \taudit_syscall_exit(regs);\n \n \tif (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))\ndiff --git a/arch/sparc/kernel/syscalls.S b/arch/sparc/kernel/syscalls.S\nindex 96fe8763d70ce..086fb8eaf1331 100644\n--- a/arch/sparc/kernel/syscalls.S\n+++ b/arch/sparc/kernel/syscalls.S\n@@ -269,10 +269,12 @@ ret_sys_call:\n \tldx\t[%sp + PTREGS_OFF + PT_V9_TSTATE], %g3\n \tmov\t%ulo(TSTATE_XCARRY | TSTATE_ICARRY), %g2\n \tsllx\t%g2, 32, %g2\n+\t/* Reload the flags, the syscall may have set TIF_FD_SLOTS. */\n+\tldx\t[%g6 + TI_FLAGS], %l0\n \n \tcmp\t%o0, -ERESTART_RESTARTBLOCK\n \tbgeu,pn\t%xcc, 1f\n-\t andcc\t%l0, (_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT|_TIF_SYSCALL_TRACEPOINT|_TIF_NOHZ), %g0\n+\t andcc\t%l0, _TIF_SYSCALL_EXIT_WORK, %g0\n \tldx\t[%sp + PTREGS_OFF + PT_V9_TNPC], %l1 ! pc = npc\n \n 2:\ndiff --git a/arch/um/include/asm/thread_info.h b/arch/um/include/asm/thread_info.h\nindex 7a6f4dc99fa16..7140516ee0b6c 100644\n--- a/arch/um/include/asm/thread_info.h\n+++ b/arch/um/include/asm/thread_info.h\n@@ -44,6 +44,7 @@ struct thread_info {\n #define TIF_SECCOMP\t\t9\t/* secure computing */\n #define TIF_SINGLESTEP\t\t10\t/* single stepping userspace */\n #define TIF_SYSCALL_TRACEPOINT\t11\t/* syscall tracepoint instrumentation */\n+#define TIF_FD_SLOTS\t\t12\t/* syscall prepared descriptors */\n \n \n #define _TIF_SYSCALL_TRACE\t(1 \u003c\u003c TIF_SYSCALL_TRACE)\n@@ -55,6 +56,7 @@ struct thread_info {\n #define _TIF_NOTIFY_RESUME\t(1 \u003c\u003c TIF_NOTIFY_RESUME)\n #define _TIF_SECCOMP\t\t(1 \u003c\u003c TIF_SECCOMP)\n #define _TIF_SINGLESTEP\t\t(1 \u003c\u003c TIF_SINGLESTEP)\n+#define _TIF_FD_SLOTS\t\t(1 \u003c\u003c TIF_FD_SLOTS)\n \n #define _TIF_WORK_MASK\t\t(_TIF_NEED_RESCHED | _TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL | \\\n \t\t\t\t _TIF_NOTIFY_RESUME)\ndiff --git a/arch/um/kernel/ptrace.c b/arch/um/kernel/ptrace.c\nindex 7da0a5223aa69..f1f89181fb7e1 100644\n--- a/arch/um/kernel/ptrace.c\n+++ b/arch/um/kernel/ptrace.c\n@@ -4,6 +4,7 @@\n */\n \n #include \u003clinux/audit.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/ptrace.h\u003e\n #include \u003clinux/sched.h\u003e\n #include \u003clinux/uaccess.h\u003e\n@@ -142,6 +143,9 @@ void syscall_trace_leave(struct pt_regs *regs)\n {\n \tint ptraced = current-\u003eptrace;\n \n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \taudit_syscall_exit(regs);\n \n \t/* Fake a debug trap */\ndiff --git a/arch/xtensa/include/asm/thread_info.h b/arch/xtensa/include/asm/thread_info.h\nindex 5b74dfc35ef95..0c8eb5dc83982 100644\n--- a/arch/xtensa/include/asm/thread_info.h\n+++ b/arch/xtensa/include/asm/thread_info.h\n@@ -124,6 +124,7 @@ static __always_inline struct thread_info *current_thread_info(void)\n #define TIF_SYSCALL_AUDIT\t9\t/* syscall auditing active */\n #define TIF_SECCOMP\t\t10\t/* secure computing */\n #define TIF_MEMDIE\t\t11\t/* is terminating due to OOM killer */\n+#define TIF_FD_SLOTS\t\t12\t/* syscall prepared descriptors */\n \n #define _TIF_SYSCALL_TRACE\t(1\u003c\u003cTIF_SYSCALL_TRACE)\n #define _TIF_SIGPENDING\t\t(1\u003c\u003cTIF_SIGPENDING)\n@@ -134,6 +135,7 @@ static __always_inline struct thread_info *current_thread_info(void)\n #define _TIF_NOTIFY_RESUME\t(1\u003c\u003cTIF_NOTIFY_RESUME)\n #define _TIF_SYSCALL_AUDIT\t(1\u003c\u003cTIF_SYSCALL_AUDIT)\n #define _TIF_SECCOMP\t\t(1\u003c\u003cTIF_SECCOMP)\n+#define _TIF_FD_SLOTS\t\t(1\u003c\u003cTIF_FD_SLOTS)\n \n #define _TIF_WORK_MASK\t\t(_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | \\\n \t\t\t\t _TIF_SYSCALL_TRACEPOINT | \\\ndiff --git a/arch/xtensa/kernel/entry.S b/arch/xtensa/kernel/entry.S\nindex 272fff587907f..4b95f48736e6b 100644\n--- a/arch/xtensa/kernel/entry.S\n+++ b/arch/xtensa/kernel/entry.S\n@@ -1976,6 +1976,11 @@ ENTRY(system_call)\n 1:\t/* regs-\u003eareg[2] = return_value */\n \n \ts32i\tabi_rv, abi_saved0, PT_AREG2\n+\n+\t/* descriptors reserved by the syscall are committed in do_syscall_trace_leave */\n+\tGET_THREAD_INFO(a4, a1)\n+\tl32i\ta4, a4, TI_FLAGS\n+\tbbsi.l\ta4, TIF_FD_SLOTS, 1f\n \tbnez\tabi_saved1, 1f\n .Lsyscall_exit:\n #if defined(__XTENSA_WINDOWED_ABI__)\ndiff --git a/arch/xtensa/kernel/ptrace.c b/arch/xtensa/kernel/ptrace.c\nindex 364e4fdabb00d..d44d62a56b854 100644\n--- a/arch/xtensa/kernel/ptrace.c\n+++ b/arch/xtensa/kernel/ptrace.c\n@@ -13,6 +13,7 @@\n */\n \n #include \u003clinux/audit.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/errno.h\u003e\n #include \u003clinux/hw_breakpoint.h\u003e\n #include \u003clinux/kernel.h\u003e\n@@ -571,6 +572,9 @@ void do_syscall_trace_leave(struct pt_regs *regs)\n {\n \tint step;\n \n+\tif (test_thread_flag(TIF_FD_SLOTS))\n+\t\tfd_slots_commit(regs);\n+\n \taudit_syscall_exit(regs);\n \n \tif (test_thread_flag(TIF_SYSCALL_TRACEPOINT))\ndiff --git a/drivers/accel/habanalabs/common/memory.c b/drivers/accel/habanalabs/common/memory.c\nindex 361cff5773814..1559442cd56f4 100644\n--- a/drivers/accel/habanalabs/common/memory.c\n+++ b/drivers/accel/habanalabs/common/memory.c\n@@ -1861,11 +1861,13 @@ static int export_dmabuf(struct hl_ctx *ctx,\n {\n \tDEFINE_DMA_BUF_EXPORT_INFO(exp_info);\n \tstruct hl_device *hdev = ctx-\u003ehdev;\n-\tCLASS(get_unused_fd, fd)(flags);\n+\tconst struct fd_slot *fd = fd_prepare(flags);\n \n-\tif (fd \u003c 0) {\n-\t\tdev_err(hdev-\u003edev, \"failed to get a file descriptor for a dma-buf, %d\\n\", fd);\n-\t\treturn fd;\n+\tif (IS_ERR(fd)) {\n+\t\tint rc = PTR_ERR(fd);\n+\n+\t\tdev_err(hdev-\u003edev, \"failed to get a file descriptor for a dma-buf, %d\\n\", rc);\n+\t\treturn rc;\n \t}\n \n \texp_info.ops = \u0026habanalabs_dmabuf_ops;\n@@ -1889,8 +1891,7 @@ static int export_dmabuf(struct hl_ctx *ctx,\n \t */\n \tget_file(ctx-\u003ehpriv-\u003efile_priv-\u003efilp);\n \n-\t*dmabuf_fd = fd;\n-\tfd_install(take_fd(fd), hl_dmabuf-\u003edmabuf-\u003efile);\n+\t*dmabuf_fd = fd_stage(fd, hl_dmabuf-\u003edmabuf-\u003efile);\n \n \treturn 0;\n }\ndiff --git a/drivers/char/tpm/tpm_vtpm_proxy.c b/drivers/char/tpm/tpm_vtpm_proxy.c\nindex b81fd2a537df4..8eb87044596a4 100644\n--- a/drivers/char/tpm/tpm_vtpm_proxy.c\n+++ b/drivers/char/tpm/tpm_vtpm_proxy.c\n@@ -531,8 +531,8 @@ static struct file *vtpm_proxy_create_device(\n \t\t\t\t struct vtpm_proxy_new_dev *vtpm_new_dev)\n {\n \tstruct proxy_dev *proxy_dev;\n-\tint rc, fd;\n \tstruct file *file;\n+\tint rc;\n \n \tif (vtpm_new_dev-\u003eflags \u0026 ~VTPM_PROXY_FLAGS_ALL)\n \t\treturn ERR_PTR(-EOPNOTSUPP);\n@@ -544,20 +544,15 @@ static struct file *vtpm_proxy_create_device(\n \tproxy_dev-\u003eflags = vtpm_new_dev-\u003eflags;\n \n \t/* setup an anonymous file for the server-side */\n-\tfd = get_unused_fd_flags(O_RDWR);\n-\tif (fd \u003c 0) {\n-\t\trc = fd;\n+\tFD_PREPARE(fdf, O_RDWR,\n+\t\t anon_inode_getfile(\"[vtpms]\", \u0026vtpm_proxy_fops, proxy_dev,\n+\t\t\t\t O_RDWR));\n+\tif (IS_ERR(fdf)) {\n+\t\trc = PTR_ERR(fdf);\n \t\tgoto err_delete_proxy_dev;\n \t}\n+\tfile = fd_prepare_file(fdf);\n \n-\tfile = anon_inode_getfile(\"[vtpms]\", \u0026vtpm_proxy_fops, proxy_dev,\n-\t\t\t\t O_RDWR);\n-\tif (IS_ERR(file)) {\n-\t\trc = PTR_ERR(file);\n-\t\tgoto err_put_unused_fd;\n-\t}\n-\n-\t/* from now on we can unwind with put_unused_fd() + fput() */\n \t/* simulate an open() on the server side */\n \tvtpm_proxy_fops_open(file);\n \n@@ -566,16 +561,13 @@ static struct file *vtpm_proxy_create_device(\n \n \tvtpm_proxy_work_start(proxy_dev);\n \n-\tvtpm_new_dev-\u003efd = fd;\n+\tvtpm_new_dev-\u003efd = fd_prepare_fd(fdf);\n \tvtpm_new_dev-\u003emajor = MAJOR(proxy_dev-\u003echip-\u003edev.devt);\n \tvtpm_new_dev-\u003eminor = MINOR(proxy_dev-\u003echip-\u003edev.devt);\n \tvtpm_new_dev-\u003etpm_num = proxy_dev-\u003echip-\u003edev_num;\n \n \treturn file;\n \n-err_put_unused_fd:\n-\tput_unused_fd(fd);\n-\n err_delete_proxy_dev:\n \tvtpm_proxy_delete_proxy_dev(proxy_dev);\n \n@@ -640,13 +632,9 @@ static long vtpmx_ioc_new_dev(struct file *file, unsigned int ioctl,\n \t\treturn PTR_ERR(vtpm_file);\n \n \tif (copy_to_user(vtpm_new_dev_p, \u0026vtpm_new_dev,\n-\t\t\t sizeof(vtpm_new_dev))) {\n-\t\tput_unused_fd(vtpm_new_dev.fd);\n-\t\tfput(vtpm_file);\n+\t\t\t sizeof(vtpm_new_dev)))\n \t\treturn -EFAULT;\n-\t}\n \n-\tfd_install(vtpm_new_dev.fd, vtpm_file);\n \treturn 0;\n }\n \ndiff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c\nindex d504c636dc294..f0d581fad7aec 100644\n--- a/drivers/dma-buf/dma-buf.c\n+++ b/drivers/dma-buf/dma-buf.c\n@@ -440,7 +440,8 @@ static long dma_buf_export_sync_file(struct dma_buf *dmabuf,\n \tenum dma_resv_usage usage;\n \tstruct dma_fence *fence = NULL;\n \tstruct sync_file *sync_file;\n-\tint fd, ret;\n+\tint ret;\n+\tconst struct fd_slot *fd;\n \n \tif (copy_from_user(\u0026arg, user_data, sizeof(arg)))\n \t\treturn -EFAULT;\n@@ -451,14 +452,14 @@ static long dma_buf_export_sync_file(struct dma_buf *dmabuf,\n \tif ((arg.flags \u0026 DMA_BUF_SYNC_RW) == 0)\n \t\treturn -EINVAL;\n \n-\tfd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (fd \u003c 0)\n-\t\treturn fd;\n+\tfd = fd_prepare(O_CLOEXEC);\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n \tusage = dma_resv_usage_rw(arg.flags \u0026 DMA_BUF_SYNC_WRITE);\n \tret = dma_resv_get_singleton(dmabuf-\u003eresv, usage, \u0026fence);\n \tif (ret)\n-\t\tgoto err_put_fd;\n+\t\treturn ret;\n \n \tif (!fence)\n \t\tfence = dma_fence_get_stub();\n@@ -467,26 +468,14 @@ static long dma_buf_export_sync_file(struct dma_buf *dmabuf,\n \n \tdma_fence_put(fence);\n \n-\tif (!sync_file) {\n-\t\tret = -ENOMEM;\n-\t\tgoto err_put_fd;\n-\t}\n-\n-\targ.fd = fd;\n-\tif (copy_to_user(user_data, \u0026arg, sizeof(arg))) {\n-\t\tret = -EFAULT;\n-\t\tgoto err_put_file;\n-\t}\n+\tif (!sync_file)\n+\t\treturn -ENOMEM;\n \n-\tfd_install(fd, sync_file-\u003efile);\n+\targ.fd = fd_stage(fd, sync_file-\u003efile);\n+\tif (copy_to_user(user_data, \u0026arg, sizeof(arg)))\n+\t\treturn -EFAULT;\n \n \treturn 0;\n-\n-err_put_file:\n-\tfput(sync_file-\u003efile);\n-err_put_fd:\n-\tput_unused_fd(fd);\n-\treturn ret;\n }\n \n static long dma_buf_import_sync_file(struct dma_buf *dmabuf,\ndiff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c\nindex 8df20b0218a9e..90852950e4e34 100644\n--- a/drivers/dma-buf/sw_sync.c\n+++ b/drivers/dma-buf/sw_sync.c\n@@ -344,8 +344,7 @@ static int sw_sync_debugfs_release(struct inode *inode, struct file *file)\n static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,\n \t\t\t\t unsigned long arg)\n {\n-\tint fd = get_unused_fd_flags(O_CLOEXEC);\n-\tint err;\n+\tconst struct fd_slot *fd = fd_prepare(O_CLOEXEC);\n \tstruct sync_pt *pt;\n \tstruct sync_file *sync_file;\n \tstruct sw_sync_create_fence_data data;\n@@ -353,41 +352,26 @@ static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,\n \t/* SW sync fence are inherently unsafe and can deadlock the kernel */\n \tadd_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);\n \n-\tif (fd \u003c 0)\n-\t\treturn fd;\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n-\tif (copy_from_user(\u0026data, (void __user *)arg, sizeof(data))) {\n-\t\terr = -EFAULT;\n-\t\tgoto err;\n-\t}\n+\tif (copy_from_user(\u0026data, (void __user *)arg, sizeof(data)))\n+\t\treturn -EFAULT;\n \n \tpt = sync_pt_create(obj, data.value);\n-\tif (!pt) {\n-\t\terr = -ENOMEM;\n-\t\tgoto err;\n-\t}\n+\tif (!pt)\n+\t\treturn -ENOMEM;\n \n \tsync_file = sync_file_create(\u0026pt-\u003ebase);\n \tdma_fence_put(\u0026pt-\u003ebase);\n-\tif (!sync_file) {\n-\t\terr = -ENOMEM;\n-\t\tgoto err;\n-\t}\n-\n-\tdata.fence = fd;\n-\tif (copy_to_user((void __user *)arg, \u0026data, sizeof(data))) {\n-\t\tfput(sync_file-\u003efile);\n-\t\terr = -EFAULT;\n-\t\tgoto err;\n-\t}\n+\tif (!sync_file)\n+\t\treturn -ENOMEM;\n \n-\tfd_install(fd, sync_file-\u003efile);\n+\tdata.fence = fd_stage(fd, sync_file-\u003efile);\n+\tif (copy_to_user((void __user *)arg, \u0026data, sizeof(data)))\n+\t\treturn -EFAULT;\n \n \treturn 0;\n-\n-err:\n-\tput_unused_fd(fd);\n-\treturn err;\n }\n \n static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)\ndiff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c\nindex 2166bbdf7e4a4..316fffc4067e8 100644\n--- a/drivers/dma-buf/sync_file.c\n+++ b/drivers/dma-buf/sync_file.c\n@@ -213,56 +213,34 @@ static __poll_t sync_file_poll(struct file *file, poll_table *wait)\n static long sync_file_ioctl_merge(struct sync_file *sync_file,\n \t\t\t\t unsigned long arg)\n {\n-\tint fd = get_unused_fd_flags(O_CLOEXEC);\n-\tint err;\n+\tconst struct fd_slot *fd = fd_prepare(O_CLOEXEC);\n \tstruct sync_file *fence2, *fence3;\n \tstruct sync_merge_data data;\n \n-\tif (fd \u003c 0)\n-\t\treturn fd;\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n-\tif (copy_from_user(\u0026data, (void __user *)arg, sizeof(data))) {\n-\t\terr = -EFAULT;\n-\t\tgoto err_put_fd;\n-\t}\n+\tif (copy_from_user(\u0026data, (void __user *)arg, sizeof(data)))\n+\t\treturn -EFAULT;\n \n-\tif (data.flags || data.pad) {\n-\t\terr = -EINVAL;\n-\t\tgoto err_put_fd;\n-\t}\n+\tif (data.flags || data.pad)\n+\t\treturn -EINVAL;\n \n \tfence2 = sync_file_fdget(data.fd2);\n-\tif (!fence2) {\n-\t\terr = -ENOENT;\n-\t\tgoto err_put_fd;\n-\t}\n+\tif (!fence2)\n+\t\treturn -ENOENT;\n \n \tdata.name[sizeof(data.name) - 1] = '\\0';\n \tfence3 = sync_file_merge(data.name, sync_file, fence2);\n-\tif (!fence3) {\n-\t\terr = -ENOMEM;\n-\t\tgoto err_put_fence2;\n-\t}\n-\n-\tdata.fence = fd;\n-\tif (copy_to_user((void __user *)arg, \u0026data, sizeof(data))) {\n-\t\terr = -EFAULT;\n-\t\tgoto err_put_fence3;\n-\t}\n-\n-\tfd_install(fd, fence3-\u003efile);\n \tfput(fence2-\u003efile);\n-\treturn 0;\n-\n-err_put_fence3:\n-\tfput(fence3-\u003efile);\n+\tif (!fence3)\n+\t\treturn -ENOMEM;\n \n-err_put_fence2:\n-\tfput(fence2-\u003efile);\n+\tdata.fence = fd_stage(fd, fence3-\u003efile);\n+\tif (copy_to_user((void __user *)arg, \u0026data, sizeof(data)))\n+\t\treturn -EFAULT;\n \n-err_put_fd:\n-\tput_unused_fd(fd);\n-\treturn err;\n+\treturn 0;\n }\n \n static int sync_fill_fence_info(struct dma_fence *fence,\ndiff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c\nindex 9f3b628d57934..4d138db254724 100644\n--- a/drivers/gpio/gpiolib-cdev.c\n+++ b/drivers/gpio/gpiolib-cdev.c\n@@ -377,16 +377,14 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)\n \tFD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,\n \t\t anon_inode_getfile(\"gpio-linehandle\", \u0026linehandle_fileops,\n \t\t\t\t lh, O_RDONLY | O_CLOEXEC));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \tretain_and_null_ptr(lh);\n \n \thandlereq.fd = fd_prepare_fd(fdf);\n \tif (copy_to_user(ip, \u0026handlereq, sizeof(handlereq)))\n \t\treturn -EFAULT;\n \n-\tfd_publish(fdf);\n-\n \tdev_dbg(\u0026gdev-\u003edev, \"registered chardev handle for %d lines\\n\",\n \t\thandlereq.lines);\n \n@@ -1715,16 +1713,14 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip)\n \tFD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,\n \t\t anon_inode_getfile(\"gpio-line\", \u0026line_fileops, lr,\n \t\t\t\t O_RDONLY | O_CLOEXEC));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \tretain_and_null_ptr(lr);\n \n \tulr.fd = fd_prepare_fd(fdf);\n \tif (copy_to_user(ip, \u0026ulr, sizeof(ulr)))\n \t\treturn -EFAULT;\n \n-\tfd_publish(fdf);\n-\n \tdev_dbg(\u0026gdev-\u003edev, \"registered chardev handle for %d lines\\n\",\n \t\tulr.num_lines);\n \n@@ -2115,16 +2111,14 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)\n \tFD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,\n \t\t anon_inode_getfile(\"gpio-event\", \u0026lineevent_fileops, le,\n \t\t\t\t O_RDONLY | O_CLOEXEC));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \tretain_and_null_ptr(le);\n \n \teventreq.fd = fd_prepare_fd(fdf);\n \tif (copy_to_user(ip, \u0026eventreq, sizeof(eventreq)))\n \t\treturn -EFAULT;\n \n-\tfd_publish(fdf);\n-\n \treturn 0;\n }\n \ndiff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c\nindex 03b41f8035206..e5c0f7201cea9 100644\n--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c\n+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c\n@@ -1581,7 +1581,8 @@ int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,\n \tstruct dma_fence *fence;\n \tstruct drm_syncobj *syncobj;\n \tstruct sync_file *sync_file;\n-\tint fd, r;\n+\tconst struct fd_slot *fd;\n+\tint r;\n \n \tfence = amdgpu_cs_get_fence(adev, filp, \u0026info-\u003ein.fence);\n \tif (IS_ERR(fence))\n@@ -1610,21 +1611,18 @@ int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,\n \t\treturn r;\n \n \tcase AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD:\n-\t\tfd = get_unused_fd_flags(O_CLOEXEC);\n-\t\tif (fd \u003c 0) {\n+\t\tfd = fd_prepare(O_CLOEXEC);\n+\t\tif (IS_ERR(fd)) {\n \t\t\tdma_fence_put(fence);\n-\t\t\treturn fd;\n+\t\t\treturn PTR_ERR(fd);\n \t\t}\n \n \t\tsync_file = sync_file_create(fence);\n \t\tdma_fence_put(fence);\n-\t\tif (!sync_file) {\n-\t\t\tput_unused_fd(fd);\n+\t\tif (!sync_file)\n \t\t\treturn -ENOMEM;\n-\t\t}\n \n-\t\tfd_install(fd, sync_file-\u003efile);\n-\t\tinfo-\u003eout.handle = fd;\n+\t\tinfo-\u003eout.handle = fd_stage(fd, sync_file-\u003efile);\n \t\treturn 0;\n \n \tdefault:\ndiff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c\nindex 7fcfc150a7fc1..158abe7e3da65 100644\n--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c\n+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c\n@@ -1988,11 +1988,10 @@ static uint32_t get_process_num_bos(struct kfd_process *p)\n \treturn num_of_bos;\n }\n \n-static int criu_get_prime_handle(struct kgd_mem *mem,\n-\t\t\t\t int flags, u32 *shared_fd,\n-\t\t\t\t struct file **file)\n+static int criu_get_prime_handle(struct kgd_mem *mem, int flags, u32 *shared_fd)\n {\n \tstruct dma_buf *dmabuf;\n+\tconst struct fd_slot *fd;\n \tint ret;\n \n \tret = amdgpu_amdkfd_gpuvm_export_dmabuf(mem, \u0026dmabuf);\n@@ -2001,38 +2000,17 @@ static int criu_get_prime_handle(struct kgd_mem *mem,\n \t\treturn ret;\n \t}\n \n-\tret = get_unused_fd_flags(flags);\n-\tif (ret \u003c 0) {\n+\tfd = fd_prepare(flags);\n+\tif (IS_ERR(fd)) {\n+\t\tret = PTR_ERR(fd);\n \t\tpr_err(\"dmabuf create fd failed, ret:%d\\n\", ret);\n-\t\tgoto out_free_dmabuf;\n+\t\tdma_buf_put(dmabuf);\n+\t\treturn ret;\n \t}\n \n-\t*shared_fd = ret;\n-\t*file = dmabuf-\u003efile;\n+\t/* Installed or dropped with the ioctl's result, nothing to put back. */\n+\t*shared_fd = fd_stage(fd, dmabuf-\u003efile);\n \treturn 0;\n-\n-out_free_dmabuf:\n-\tdma_buf_put(dmabuf);\n-\treturn ret;\n-}\n-\n-static void commit_files(struct file **files,\n-\t\t\t struct kfd_criu_bo_bucket *bo_buckets,\n-\t\t\t unsigned int count,\n-\t\t\t int err)\n-{\n-\twhile (count--) {\n-\t\tstruct file *file = files[count];\n-\n-\t\tif (!file)\n-\t\t\tcontinue;\n-\t\tif (err) {\n-\t\t\tfput(file);\n-\t\t\tput_unused_fd(bo_buckets[count].dmabuf_fd);\n-\t\t} else {\n-\t\t\tfd_install(bo_buckets[count].dmabuf_fd, file);\n-\t\t}\n-\t}\n }\n \n static int criu_checkpoint_bos(struct kfd_process *p,\n@@ -2043,7 +2021,6 @@ static int criu_checkpoint_bos(struct kfd_process *p,\n {\n \tstruct kfd_criu_bo_bucket *bo_buckets;\n \tstruct kfd_criu_bo_priv_data *bo_privs;\n-\tstruct file **files = NULL;\n \tint ret = 0, pdd_index, bo_index = 0, id;\n \tvoid *mem;\n \n@@ -2057,12 +2034,6 @@ static int criu_checkpoint_bos(struct kfd_process *p,\n \t\tgoto exit;\n \t}\n \n-\tfiles = kvcalloc(num_bos, sizeof(struct file *), GFP_KERNEL);\n-\tif (!files) {\n-\t\tret = -ENOMEM;\n-\t\tgoto exit;\n-\t}\n-\n \tfor (pdd_index = 0; pdd_index \u003c p-\u003en_pdds; pdd_index++) {\n \t\tstruct kfd_process_device *pdd = p-\u003epdds[pdd_index];\n \t\tstruct amdgpu_bo *dumper_bo;\n@@ -2105,7 +2076,7 @@ static int criu_checkpoint_bos(struct kfd_process *p,\n \t\t\t\tret = criu_get_prime_handle(kgd_mem,\n \t\t\t\t\t\tbo_bucket-\u003ealloc_flags \u0026\n \t\t\t\t\t\tKFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ? DRM_RDWR : 0,\n-\t\t\t\t\t\t\u0026bo_bucket-\u003edmabuf_fd, \u0026files[bo_index]);\n+\t\t\t\t\t\t\u0026bo_bucket-\u003edmabuf_fd);\n \t\t\t\tif (ret)\n \t\t\t\t\tgoto exit;\n \t\t\t} else {\n@@ -2156,8 +2127,6 @@ static int criu_checkpoint_bos(struct kfd_process *p,\n \t*priv_offset += num_bos * sizeof(*bo_privs);\n \n exit:\n-\tcommit_files(files, bo_buckets, bo_index, ret);\n-\tkvfree(files);\n \tkvfree(bo_buckets);\n \tkvfree(bo_privs);\n \treturn ret;\n@@ -2504,8 +2473,7 @@ static int criu_restore_memory_of_gpu(struct kfd_process_device *pdd,\n \n static int criu_restore_bo(struct kfd_process *p,\n \t\t\t struct kfd_criu_bo_bucket *bo_bucket,\n-\t\t\t struct kfd_criu_bo_priv_data *bo_priv,\n-\t\t\t struct file **file)\n+\t\t\t struct kfd_criu_bo_priv_data *bo_priv)\n {\n \tstruct kfd_process_device *pdd;\n \tstruct kgd_mem *kgd_mem;\n@@ -2557,7 +2525,7 @@ static int criu_restore_bo(struct kfd_process *p,\n \tif (bo_bucket-\u003ealloc_flags\n \t \u0026 (KFD_IOC_ALLOC_MEM_FLAGS_VRAM | KFD_IOC_ALLOC_MEM_FLAGS_GTT)) {\n \t\tret = criu_get_prime_handle(kgd_mem, DRM_RDWR,\n-\t\t\t\t\t \u0026bo_bucket-\u003edmabuf_fd, file);\n+\t\t\t\t\t \u0026bo_bucket-\u003edmabuf_fd);\n \t\tif (ret)\n \t\t\treturn ret;\n \t} else {\n@@ -2574,7 +2542,6 @@ static int criu_restore_bos(struct kfd_process *p,\n {\n \tstruct kfd_criu_bo_bucket *bo_buckets = NULL;\n \tstruct kfd_criu_bo_priv_data *bo_privs = NULL;\n-\tstruct file **files = NULL;\n \tint ret = 0;\n \tuint32_t i = 0;\n \n@@ -2588,12 +2555,6 @@ static int criu_restore_bos(struct kfd_process *p,\n \tif (!bo_buckets)\n \t\treturn -ENOMEM;\n \n-\tfiles = kvcalloc(args-\u003enum_bos, sizeof(struct file *), GFP_KERNEL);\n-\tif (!files) {\n-\t\tret = -ENOMEM;\n-\t\tgoto exit;\n-\t}\n-\n \tret = copy_from_user(bo_buckets, (void __user *)args-\u003ebos,\n \t\t\t args-\u003enum_bos * sizeof(*bo_buckets));\n \tif (ret) {\n@@ -2619,7 +2580,7 @@ static int criu_restore_bos(struct kfd_process *p,\n \n \t/* Create and map new BOs */\n \tfor (; i \u003c args-\u003enum_bos; i++) {\n-\t\tret = criu_restore_bo(p, \u0026bo_buckets[i], \u0026bo_privs[i], \u0026files[i]);\n+\t\tret = criu_restore_bo(p, \u0026bo_buckets[i], \u0026bo_privs[i]);\n \t\tif (ret) {\n \t\t\tpr_debug(\"Failed to restore BO[%d] ret%d\\n\", i, ret);\n \t\t\tgoto exit;\n@@ -2634,8 +2595,6 @@ static int criu_restore_bos(struct kfd_process *p,\n \t\tret = -EFAULT;\n \n exit:\n-\tcommit_files(files, bo_buckets, i, ret);\n-\tkvfree(files);\n \tkvfree(bo_buckets);\n \tkvfree(bo_privs);\n \treturn ret;\ndiff --git a/drivers/gpu/drm/drm_lease.c b/drivers/gpu/drm/drm_lease.c\nindex 5d2cf724cbd7b..b95747cf79a74 100644\n--- a/drivers/gpu/drm/drm_lease.c\n+++ b/drivers/gpu/drm/drm_lease.c\n@@ -484,7 +484,7 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,\n \tstruct file *lessee_file = NULL;\n \tstruct file *lessor_file = lessor_priv-\u003efilp;\n \tstruct drm_file *lessee_priv;\n-\tint fd = -1;\n+\tconst struct fd_slot *fd;\n \tuint32_t *object_ids;\n \n \t/* Can't lease without MODESET */\n@@ -529,10 +529,10 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,\n \t}\n \n \t/* Allocate a file descriptor for the lease */\n-\tfd = get_unused_fd_flags(cl-\u003eflags \u0026 (O_CLOEXEC | O_NONBLOCK));\n-\tif (fd \u003c 0) {\n+\tfd = fd_prepare(cl-\u003eflags \u0026 (O_CLOEXEC | O_NONBLOCK));\n+\tif (IS_ERR(fd)) {\n \t\tidr_destroy(\u0026leases);\n-\t\tret = fd;\n+\t\tret = PTR_ERR(fd);\n \t\tgoto out_lessor;\n \t}\n \n@@ -543,7 +543,7 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,\n \tif (IS_ERR(lessee)) {\n \t\tret = PTR_ERR(lessee);\n \t\tidr_destroy(\u0026leases);\n-\t\tgoto out_leases;\n+\t\tgoto out_lessor;\n \t}\n \n \t/* Clone the lessor file to create a new file for us */\n@@ -562,12 +562,13 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,\n \tlessee_priv-\u003eauthenticated = 1;\n \n \t/* Pass fd back to userspace */\n-\tdrm_dbg_lease(dev, \"Returning fd %d id %d\\n\", fd, lessee-\u003elessee_id);\n-\tcl-\u003efd = fd;\n+\tdrm_dbg_lease(dev, \"Returning fd %d id %d\\n\", fd_prepare_fd(fd),\n+\t\t lessee-\u003elessee_id);\n+\tcl-\u003efd = fd_prepare_fd(fd);\n \tcl-\u003elessee_id = lessee-\u003elessee_id;\n \n \t/* Hook up the fd */\n-\tfd_install(fd, lessee_file);\n+\tfd_stage(fd, lessee_file);\n \n \tdrm_master_put(\u0026lessor);\n \tdrm_dbg_lease(dev, \"drm_mode_create_lease_ioctl succeeded\\n\");\n@@ -576,9 +577,6 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,\n out_lessee:\n \tdrm_master_put(\u0026lessee);\n \n-out_leases:\n-\tput_unused_fd(fd);\n-\n out_lessor:\n \tdrm_master_put(\u0026lessor);\n \tdrm_dbg_lease(dev, \"drm_mode_create_lease_ioctl failed: %d\\n\", ret);\ndiff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c\nindex 9b44c78cd77fc..e33d84ac7256c 100644\n--- a/drivers/gpu/drm/drm_prime.c\n+++ b/drivers/gpu/drm/drm_prime.c\n@@ -513,19 +513,16 @@ int drm_gem_prime_handle_to_fd(struct drm_device *dev,\n \t\t\t int *prime_fd)\n {\n \tstruct dma_buf *dmabuf;\n-\tint fd = get_unused_fd_flags(flags);\n+\tconst struct fd_slot *fd = fd_prepare(flags);\n \n-\tif (fd \u003c 0)\n-\t\treturn fd;\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n \tdmabuf = drm_gem_prime_handle_to_dmabuf(dev, file_priv, handle, flags);\n-\tif (IS_ERR(dmabuf)) {\n-\t\tput_unused_fd(fd);\n+\tif (IS_ERR(dmabuf))\n \t\treturn PTR_ERR(dmabuf);\n-\t}\n \n-\tfd_install(fd, dmabuf-\u003efile);\n-\t*prime_fd = fd;\n+\t*prime_fd = fd_stage(fd, dmabuf-\u003efile);\n \treturn 0;\n }\n EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);\ndiff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c\nindex 2fa170a29a626..621858b6ac437 100644\n--- a/drivers/gpu/drm/drm_syncobj.c\n+++ b/drivers/gpu/drm/drm_syncobj.c\n@@ -663,25 +663,14 @@ static const struct file_operations drm_syncobj_file_fops = {\n */\n int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)\n {\n-\tstruct file *file;\n-\tint fd;\n-\n-\tfd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (fd \u003c 0)\n-\t\treturn fd;\n-\n-\tfile = anon_inode_getfile(\"syncobj_file\",\n-\t\t\t\t \u0026drm_syncobj_file_fops,\n-\t\t\t\t syncobj, 0);\n-\tif (IS_ERR(file)) {\n-\t\tput_unused_fd(fd);\n-\t\treturn PTR_ERR(file);\n-\t}\n+\tFD_PREPARE(fdf, O_CLOEXEC,\n+\t\t anon_inode_getfile(\"syncobj_file\", \u0026drm_syncobj_file_fops,\n+\t\t\t\t syncobj, 0));\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \n \tdrm_syncobj_get(syncobj);\n-\tfd_install(fd, file);\n-\n-\t*p_fd = fd;\n+\t*p_fd = fd_prepare_fd(fdf);\n \treturn 0;\n }\n EXPORT_SYMBOL(drm_syncobj_get_fd);\n@@ -762,31 +751,24 @@ static int drm_syncobj_export_sync_file(struct drm_file *file_private,\n \tint ret;\n \tstruct dma_fence *fence;\n \tstruct sync_file *sync_file;\n-\tint fd = get_unused_fd_flags(O_CLOEXEC);\n+\tconst struct fd_slot *fd = fd_prepare(O_CLOEXEC);\n \n-\tif (fd \u003c 0)\n-\t\treturn fd;\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n \tret = drm_syncobj_find_fence(file_private, handle, point, 0, \u0026fence);\n \tif (ret)\n-\t\tgoto err_put_fd;\n+\t\treturn ret;\n \n \tsync_file = sync_file_create(fence);\n \n \tdma_fence_put(fence);\n \n-\tif (!sync_file) {\n-\t\tret = -EINVAL;\n-\t\tgoto err_put_fd;\n-\t}\n-\n-\tfd_install(fd, sync_file-\u003efile);\n+\tif (!sync_file)\n+\t\treturn -EINVAL;\n \n-\t*p_fd = fd;\n+\t*p_fd = fd_stage(fd, sync_file-\u003efile);\n \treturn 0;\n-err_put_fd:\n-\tput_unused_fd(fd);\n-\treturn ret;\n }\n /**\n * drm_syncobj_open - initializes syncobj file-private structures at devnode open time\ndiff --git a/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c b/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c\nindex 1a77a09b3377e..174f1d66d9ad3 100644\n--- a/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c\n+++ b/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c\n@@ -420,7 +420,7 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,\n \tstruct etnaviv_gpu *gpu;\n \tstruct sync_file *sync_file = NULL;\n \tstruct ww_acquire_ctx ticket;\n-\tint out_fence_fd = -1;\n+\tconst struct fd_slot *out_fence_fd = NULL;\n \tstruct pid *pid = get_pid(task_pid(current));\n \tvoid *stream;\n \tint ret;\n@@ -504,9 +504,9 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,\n \t}\n \n \tif (args-\u003eflags \u0026 ETNA_SUBMIT_FENCE_FD_OUT) {\n-\t\tout_fence_fd = get_unused_fd_flags(O_CLOEXEC);\n-\t\tif (out_fence_fd \u003c 0) {\n-\t\t\tret = out_fence_fd;\n+\t\tout_fence_fd = fd_prepare(O_CLOEXEC);\n+\t\tif (IS_ERR(out_fence_fd)) {\n+\t\t\tret = PTR_ERR(out_fence_fd);\n \t\t\tgoto err_submit_cmds;\n \t\t}\n \t}\n@@ -607,10 +607,10 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,\n \t\t\t */\n \t\t\tgoto err_submit_put;\n \t\t}\n-\t\tfd_install(out_fence_fd, sync_file-\u003efile);\n+\t\tfd_stage(out_fence_fd, sync_file-\u003efile);\n \t}\n \n-\targs-\u003efence_fd = out_fence_fd;\n+\targs-\u003efence_fd = out_fence_fd ? fd_prepare_fd(out_fence_fd) : -1;\n \targs-\u003efence = submit-\u003eout_fence_id;\n \n err_submit_job:\n@@ -623,8 +623,6 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,\n \tww_acquire_fini(\u0026ticket);\n \n err_submit_cmds:\n-\tif (ret \u0026\u0026 (out_fence_fd \u003e= 0))\n-\t\tput_unused_fd(out_fence_fd);\n \tkvfree(stream);\n \tkvfree(bos);\n \tkvfree(relocs);\ndiff --git a/drivers/gpu/drm/msm/msm_gem_submit.c b/drivers/gpu/drm/msm/msm_gem_submit.c\nindex 5862db05297a7..4ddfd07c1480d 100644\n--- a/drivers/gpu/drm/msm/msm_gem_submit.c\n+++ b/drivers/gpu/drm/msm/msm_gem_submit.c\n@@ -558,7 +558,7 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,\n \tstruct drm_syncobj **syncobjs_to_reset = NULL;\n \tstruct sync_file *sync_file = NULL;\n \tunsigned cmds_to_parse;\n-\tint out_fence_fd = -1;\n+\tconst struct fd_slot *out_fence_fd = NULL;\n \tunsigned i;\n \tint ret;\n \n@@ -601,9 +601,9 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,\n \tring = gpu-\u003erb[queue-\u003ering_nr];\n \n \tif (args-\u003eflags \u0026 MSM_SUBMIT_FENCE_FD_OUT) {\n-\t\tout_fence_fd = get_unused_fd_flags(O_CLOEXEC);\n-\t\tif (out_fence_fd \u003c 0) {\n-\t\t\tret = out_fence_fd;\n+\t\tout_fence_fd = fd_prepare(O_CLOEXEC);\n+\t\tif (IS_ERR(out_fence_fd)) {\n+\t\t\tret = PTR_ERR(out_fence_fd);\n \t\t\tgoto out_post_unlock;\n \t\t}\n \t}\n@@ -777,6 +777,8 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,\n \t\tsync_file = sync_file_create(submit-\u003euser_fence);\n \t\tif (!sync_file)\n \t\t\tret = -ENOMEM;\n+\t\telse\n+\t\t\tfd_stage(out_fence_fd, sync_file-\u003efile);\n \t}\n \n \tif (ret)\n@@ -814,15 +816,9 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,\n out_unlock:\n \tmutex_unlock(\u0026queue-\u003elock);\n out_post_unlock:\n-\tif (ret) {\n-\t\tif (out_fence_fd \u003e= 0)\n-\t\t\tput_unused_fd(out_fence_fd);\n-\t\tif (sync_file)\n-\t\t\tfput(sync_file-\u003efile);\n-\t} else if (sync_file) {\n-\t\tfd_install(out_fence_fd, sync_file-\u003efile);\n-\t\targs-\u003efence_fd = out_fence_fd;\n-\t}\n+\t/* A staged sync file and its descriptor are dropped on return. */\n+\tif (!ret \u0026\u0026 sync_file)\n+\t\targs-\u003efence_fd = fd_prepare_fd(out_fence_fd);\n \n \tif (!IS_ERR_OR_NULL(submit)) {\n \t\tmsm_gem_submit_put(submit);\ndiff --git a/drivers/gpu/drm/msm/msm_gem_vma.c b/drivers/gpu/drm/msm/msm_gem_vma.c\nindex c11d021581e03..77d3655667322 100644\n--- a/drivers/gpu/drm/msm/msm_gem_vma.c\n+++ b/drivers/gpu/drm/msm/msm_gem_vma.c\n@@ -1441,7 +1441,7 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)\n \tstruct drm_syncobj **syncobjs_to_reset = NULL;\n \tstruct sync_file *sync_file = NULL;\n \tstruct dma_fence *fence;\n-\tint out_fence_fd = -1;\n+\tconst struct fd_slot *out_fence_fd = NULL;\n \tint ret, nr_bos = 0;\n \tunsigned i;\n \n@@ -1479,9 +1479,9 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)\n \t}\n \n \tif (args-\u003eflags \u0026 MSM_VM_BIND_FENCE_FD_OUT) {\n-\t\tout_fence_fd = get_unused_fd_flags(O_CLOEXEC);\n-\t\tif (out_fence_fd \u003c 0) {\n-\t\t\tret = out_fence_fd;\n+\t\tout_fence_fd = fd_prepare(O_CLOEXEC);\n+\t\tif (IS_ERR(out_fence_fd)) {\n+\t\t\tret = PTR_ERR(out_fence_fd);\n \t\t\tgoto out_post_unlock;\n \t\t}\n \t}\n@@ -1565,6 +1565,8 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)\n \t\tsync_file = sync_file_create(job-\u003efence);\n \t\tif (!sync_file)\n \t\t\tret = -ENOMEM;\n+\t\telse\n+\t\t\tfd_stage(out_fence_fd, sync_file-\u003efile);\n \t}\n \n \tif (ret)\n@@ -1593,15 +1595,9 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)\n out_unlock:\n \tmutex_unlock(\u0026queue-\u003elock);\n out_post_unlock:\n-\tif (ret) {\n-\t\tif (out_fence_fd \u003e= 0)\n-\t\t\tput_unused_fd(out_fence_fd);\n-\t\tif (sync_file)\n-\t\t\tfput(sync_file-\u003efile);\n-\t} else if (sync_file) {\n-\t\tfd_install(out_fence_fd, sync_file-\u003efile);\n-\t\targs-\u003efence_fd = out_fence_fd;\n-\t}\n+\t/* A staged sync file and its descriptor are dropped on return. */\n+\tif (!ret \u0026\u0026 sync_file)\n+\t\targs-\u003efence_fd = fd_prepare_fd(out_fence_fd);\n \n \tif (!IS_ERR_OR_NULL(job)) {\n \t\tif (ret)\ndiff --git a/drivers/gpu/drm/msm/msm_perfcntr.c b/drivers/gpu/drm/msm/msm_perfcntr.c\nindex ce65b1160955e..65c067d39ca0a 100644\n--- a/drivers/gpu/drm/msm/msm_perfcntr.c\n+++ b/drivers/gpu/drm/msm/msm_perfcntr.c\n@@ -543,8 +543,8 @@ msm_ioctl_perfcntr_config(struct drm_device *dev, void *data, struct drm_file *f\n \n \t\tFD_PREPARE(fdf, O_CLOEXEC,\n \t\t\t anon_inode_getfile(\"[msm_perfcntrs]\", \u0026stream_fops, stream, 0));\n-\t\tif (fdf.err)\n-\t\t\treturn fdf.err;\n+\t\tif (IS_ERR(fdf))\n+\t\t\treturn PTR_ERR(fdf);\n \n \t\tINIT_WORK(\u0026stream-\u003esel_work, sel_worker);\n \t\tkthread_init_work(\u0026stream-\u003esample_work, sample_worker);\n@@ -564,7 +564,7 @@ msm_ioctl_perfcntr_config(struct drm_device *dev, void *data, struct drm_file *f\n \n \t\tmsm_perfcntr_resume_locked(perfcntrs-\u003estream);\n \n-\t\tstream_fd = fd_publish(fdf);\n+\t\tstream_fd = fd_prepare_fd(fdf);\n \t} else {\n \t\tkfree(ctx-\u003eperfctx);\n \t\tctx-\u003eperfctx = no_free_ptr(perfctx);\ndiff --git a/drivers/gpu/drm/virtio/virtgpu_submit.c b/drivers/gpu/drm/virtio/virtgpu_submit.c\nindex 32cb1e4aa4258..51b224acb72b0 100644\n--- a/drivers/gpu/drm/virtio/virtgpu_submit.c\n+++ b/drivers/gpu/drm/virtio/virtgpu_submit.c\n@@ -39,7 +39,7 @@ struct virtio_gpu_submit {\n \tstruct virtio_gpu_device *vgdev;\n \tstruct sync_file *sync_file;\n \tstruct drm_file *file;\n-\tint out_fence_fd;\n+\tconst struct fd_slot *out_fence_fd;\n \tu64 fence_ctx;\n \tu32 ring_idx;\n \tvoid *buf;\n@@ -340,14 +340,8 @@ static void virtio_gpu_cleanup_submit(struct virtio_gpu_submit *submit)\n \tif (submit-\u003ebuflist)\n \t\tvirtio_gpu_array_put_free(submit-\u003ebuflist);\n \n-\tif (submit-\u003eout_fence_fd \u003e= 0)\n-\t\tput_unused_fd(submit-\u003eout_fence_fd);\n-\n \tif (submit-\u003eout_fence)\n \t\tdma_fence_put(\u0026submit-\u003eout_fence-\u003ef);\n-\n-\tif (submit-\u003esync_file)\n-\t\tfput(submit-\u003esync_file-\u003efile);\n }\n \n static void virtio_gpu_submit(struct virtio_gpu_submit *submit)\n@@ -363,7 +357,7 @@ static void virtio_gpu_complete_submit(struct virtio_gpu_submit *submit)\n \tsubmit-\u003ebuf = NULL;\n \tsubmit-\u003ebuflist = NULL;\n \tsubmit-\u003esync_file = NULL;\n-\tsubmit-\u003eout_fence_fd = -1;\n+\tsubmit-\u003eout_fence_fd = NULL;\n }\n \n static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,\n@@ -405,7 +399,7 @@ static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,\n \tsubmit-\u003eout_fence = out_fence;\n \tsubmit-\u003efence_ctx = fence_ctx;\n \tsubmit-\u003ering_idx = ring_idx;\n-\tsubmit-\u003eout_fence_fd = -1;\n+\tsubmit-\u003eout_fence_fd = NULL;\n \tsubmit-\u003evfpriv = vfpriv;\n \tsubmit-\u003evgdev = vgdev;\n \tsubmit-\u003eexbuf = exbuf;\n@@ -420,15 +414,15 @@ static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,\n \t\treturn PTR_ERR(submit-\u003ebuf);\n \n \tif (exbuf-\u003eflags \u0026 VIRTGPU_EXECBUF_FENCE_FD_OUT) {\n-\t\terr = get_unused_fd_flags(O_CLOEXEC);\n-\t\tif (err \u003c 0)\n-\t\t\treturn err;\n-\n-\t\tsubmit-\u003eout_fence_fd = err;\n+\t\tsubmit-\u003eout_fence_fd = fd_prepare(O_CLOEXEC);\n+\t\tif (IS_ERR(submit-\u003eout_fence_fd))\n+\t\t\treturn PTR_ERR(submit-\u003eout_fence_fd);\n \n \t\tsubmit-\u003esync_file = sync_file_create(\u0026out_fence-\u003ef);\n \t\tif (!submit-\u003esync_file)\n \t\t\treturn -ENOMEM;\n+\n+\t\tfd_stage(submit-\u003eout_fence_fd, submit-\u003esync_file-\u003efile);\n \t}\n \n \treturn 0;\n@@ -456,12 +450,10 @@ static int virtio_gpu_wait_in_fence(struct virtio_gpu_submit *submit)\n \treturn ret;\n }\n \n-static void virtio_gpu_install_out_fence_fd(struct virtio_gpu_submit *submit)\n+static void virtio_gpu_report_out_fence_fd(struct virtio_gpu_submit *submit)\n {\n-\tif (submit-\u003esync_file) {\n-\t\tsubmit-\u003eexbuf-\u003efence_fd = submit-\u003eout_fence_fd;\n-\t\tfd_install(submit-\u003eout_fence_fd, submit-\u003esync_file-\u003efile);\n-\t}\n+\tif (submit-\u003esync_file)\n+\t\tsubmit-\u003eexbuf-\u003efence_fd = fd_prepare_fd(submit-\u003eout_fence_fd);\n }\n \n static int virtio_gpu_lock_buflist(struct virtio_gpu_submit *submit)\n@@ -534,7 +526,7 @@ int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,\n \t * Set up user-out data after submitting the job to optimize\n \t * the job submission path.\n \t */\n-\tvirtio_gpu_install_out_fence_fd(\u0026submit);\n+\tvirtio_gpu_report_out_fence_fd(\u0026submit);\n \tvirtio_gpu_process_post_deps(\u0026submit);\n \tvirtio_gpu_complete_submit(\u0026submit);\n cleanup:\ndiff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c b/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c\nindex a9136a6523cba..9daa74a134855 100644\n--- a/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c\n+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c\n@@ -4109,15 +4109,17 @@ int vmw_execbuf_process(struct drm_file *file_priv,\n \tuint32_t handle = 0;\n \tint ret;\n \tint32_t out_fence_fd = -1;\n+\tconst struct fd_slot *out_fd = NULL;\n \tstruct sync_file *sync_file = NULL;\n \tDECLARE_VAL_CONTEXT(val_ctx, sw_context, 1);\n \n \tif (flags \u0026 DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {\n-\t\tout_fence_fd = get_unused_fd_flags(O_CLOEXEC);\n-\t\tif (out_fence_fd \u003c 0) {\n+\t\tout_fd = fd_prepare(O_CLOEXEC);\n+\t\tif (IS_ERR(out_fd)) {\n \t\t\tVMW_DEBUG_USER(\"Failed to get a fence fd.\\n\");\n-\t\t\treturn out_fence_fd;\n+\t\t\treturn PTR_ERR(out_fd);\n \t\t}\n+\t\tout_fence_fd = fd_prepare_fd(out_fd);\n \t}\n \n \tif (throttle_us) {\n@@ -4256,28 +4258,21 @@ int vmw_execbuf_process(struct drm_file *file_priv,\n \t\tsync_file = sync_file_create(\u0026fence-\u003ebase);\n \t\tif (!sync_file) {\n \t\t\tVMW_DEBUG_USER(\"Sync file create failed for fence\\n\");\n-\t\t\tput_unused_fd(out_fence_fd);\n+\t\t\t/* The reserved descriptor is released on return. */\n \t\t\tout_fence_fd = -1;\n \n \t\t\t(void) vmw_fence_obj_wait(fence, false, false,\n \t\t\t\t\t\t VMW_FENCE_WAIT_TIMEOUT);\n+\t\t} else {\n+\t\t\t/* Link the fence with the FD created earlier */\n+\t\t\tfd_stage(out_fd, sync_file-\u003efile);\n \t\t}\n \t}\n \n+\t/* A staged sync file is dropped with the ioctl on error. */\n \tret = vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,\n \t\t\t\t user_fence_rep, fence, handle, out_fence_fd);\n \n-\tif (sync_file) {\n-\t\tif (ret) {\n-\t\t\t/* usercopy of fence failed, put the file object */\n-\t\t\tfput(sync_file-\u003efile);\n-\t\t\tput_unused_fd(out_fence_fd);\n-\t\t} else {\n-\t\t\t/* Link the fence with the FD created earlier */\n-\t\t\tfd_install(out_fence_fd, sync_file-\u003efile);\n-\t\t}\n-\t}\n-\n \t/* Don't unreference when handing fence out */\n \tif (unlikely(out_fence != NULL)) {\n \t\t*out_fence = fence;\n@@ -4323,9 +4318,7 @@ int vmw_execbuf_process(struct drm_file *file_priv,\n \tif (header)\n \t\tvmw_cmdbuf_header_free(header);\n out_free_fence_fd:\n-\tif (out_fence_fd \u003e= 0)\n-\t\tput_unused_fd(out_fence_fd);\n-\n+\t/* A reserved descriptor is released on return. */\n \treturn ret;\n }\n \ndiff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c\nindex cc2cfce2aefdb..c95cee0bae856 100644\n--- a/drivers/hv/mshv_root_main.c\n+++ b/drivers/hv/mshv_root_main.c\n@@ -1072,8 +1072,6 @@ mshv_partition_ioctl_create_vp(struct mshv_partition *partition,\n \tstruct mshv_vp *vp;\n \tstruct page *intercept_msg_page, *register_page, *ghcb_page;\n \tstruct hv_stats_page *stats_pages[2];\n-\tstruct file *file;\n-\tint fd;\n \tlong ret;\n \n \tif (copy_from_user(\u0026args, arg, sizeof(args)))\n@@ -1148,19 +1146,14 @@ mshv_partition_ioctl_create_vp(struct mshv_partition *partition,\n \tif (ret)\n \t\tgoto put_partition;\n \n-\tfd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);\n-\tif (fd \u003c 0) {\n-\t\tret = fd;\n+\tFD_PREPARE(fdf, O_RDWR | O_CLOEXEC,\n+\t\t anon_inode_getfile(\"mshv_vp\", \u0026mshv_vp_fops, vp,\n+\t\t\t\t O_RDWR | O_CLOEXEC));\n+\tif (IS_ERR(fdf)) {\n+\t\tret = PTR_ERR(fdf);\n \t\tgoto remove_debugfs_vp;\n \t}\n \n-\tfile = anon_inode_getfile(\"mshv_vp\", \u0026mshv_vp_fops, vp,\n-\t\t\t\t O_RDWR | O_CLOEXEC);\n-\tif (IS_ERR(file)) {\n-\t\tret = PTR_ERR(file);\n-\t\tgoto put_unused_vp_fd;\n-\t}\n-\n \t/* already exclusive with the partition mutex for all ioctls */\n \tpartition-\u003ept_vp_count++;\n \t/*\n@@ -1171,17 +1164,11 @@ mshv_partition_ioctl_create_vp(struct mshv_partition *partition,\n \t */\n \tsmp_store_release(\u0026partition-\u003ept_vp_array[args.vp_index], vp);\n \n-\t/*\n-\t * fd_install() is the userspace-visibility commit point. Must be the\n-\t * last operation that can fail or be observed.\n-\t */\n-\tfd_install(fd, file);\n-\tret = fd;\n+\t/* The syscall exit installs the file; nothing after this can fail. */\n+\tret = fd_prepare_fd(fdf);\n \n \tgoto out;\n \n-put_unused_vp_fd:\n-\tput_unused_fd(fd);\n remove_debugfs_vp:\n \tmshv_debugfs_vp_remove(vp);\n put_partition:\ndiff --git a/drivers/hv/mshv_vtl_main.c b/drivers/hv/mshv_vtl_main.c\nindex 6e3c11c681717..f14203d76da1e 100644\n--- a/drivers/hv/mshv_vtl_main.c\n+++ b/drivers/hv/mshv_vtl_main.c\n@@ -114,29 +114,19 @@ static long\n mshv_ioctl_create_vtl(void __user *user_arg, struct device *module_dev)\n {\n \tstruct mshv_vtl *vtl;\n-\tstruct file *file;\n-\tint fd;\n \n \tvtl = kzalloc_obj(*vtl);\n \tif (!vtl)\n \t\treturn -ENOMEM;\n \n-\tfd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (fd \u003c 0) {\n-\t\tkfree(vtl);\n-\t\treturn fd;\n-\t}\n-\tfile = anon_inode_getfile(\"mshv_vtl\", \u0026mshv_vtl_fops,\n-\t\t\t\t vtl, O_RDWR);\n-\tif (IS_ERR(file)) {\n-\t\tput_unused_fd(fd);\n+\tFD_PREPARE(fdf, O_CLOEXEC,\n+\t\t anon_inode_getfile(\"mshv_vtl\", \u0026mshv_vtl_fops, vtl, O_RDWR));\n+\tif (IS_ERR(fdf)) {\n \t\tkfree(vtl);\n-\t\treturn PTR_ERR(file);\n+\t\treturn PTR_ERR(fdf);\n \t}\n \tvtl-\u003emodule_dev = module_dev;\n-\tfd_install(fd, file);\n-\n-\treturn fd;\n+\treturn fd_prepare_fd(fdf);\n }\n \n static long\ndiff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c\nindex 2c9ec93dff475..9fb15bf82d0ad 100644\n--- a/drivers/iio/industrialio-buffer.c\n+++ b/drivers/iio/industrialio-buffer.c\n@@ -2041,7 +2041,7 @@ static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg\n \tint __user *ival = (int __user *)arg;\n \tstruct iio_dev_buffer_pair *ib;\n \tstruct iio_buffer *buffer;\n-\tint fd, idx, ret;\n+\tint idx, ret, fdno;\n \n \tif (copy_from_user(\u0026idx, ival, sizeof(idx)))\n \t\treturn -EFAULT;\n@@ -2067,26 +2067,18 @@ static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg\n \tib-\u003eindio_dev = indio_dev;\n \tib-\u003ebuffer = buffer;\n \n-\tfd = anon_inode_getfd(\"iio:buffer\", \u0026iio_buffer_chrdev_fileops,\n-\t\t\t ib, O_RDWR | O_CLOEXEC);\n-\tif (fd \u003c 0) {\n-\t\tret = fd;\n+\tFD_PREPARE(fdf, O_RDWR | O_CLOEXEC,\n+\t\t anon_inode_getfile(\"iio:buffer\", \u0026iio_buffer_chrdev_fileops,\n+\t\t\t\t ib, O_RDWR | O_CLOEXEC));\n+\tif (IS_ERR(fdf)) {\n+\t\tret = PTR_ERR(fdf);\n \t\tgoto error_free_ib;\n \t}\n \n-\tif (copy_to_user(ival, \u0026fd, sizeof(fd))) {\n-\t\t/*\n-\t\t * \"Leak\" the fd, as there's not much we can do about this\n-\t\t * anyway. 'fd' might have been closed already, as\n-\t\t * anon_inode_getfd() called fd_install() on it, which made\n-\t\t * it reachable by userland.\n-\t\t *\n-\t\t * Instead of allowing a malicious user to play tricks with\n-\t\t * us, rely on the process exit path to do any necessary\n-\t\t * cleanup, as in releasing the file, if still needed.\n-\t\t */\n+\tfdno = fd_prepare_fd(fdf);\n+\t/* The staged file is dropped with its descriptor if this faults. */\n+\tif (copy_to_user(ival, \u0026fdno, sizeof(fdno)))\n \t\treturn -EFAULT;\n-\t}\n \n \treturn 0;\n \ndiff --git a/drivers/iommu/iommufd/eventq.c b/drivers/iommu/iommufd/eventq.c\nindex 747dd5155121c..f5fdc6cdf0a11 100644\n--- a/drivers/iommu/iommufd/eventq.c\n+++ b/drivers/iommu/iommufd/eventq.c\n@@ -412,9 +412,9 @@ static int iommufd_eventq_fops_release(struct inode *inode, struct file *filep)\n \t\t.release = iommufd_eventq_fops_release, \\\n \t})\n \n-static int iommufd_eventq_init(struct iommufd_eventq *eventq, char *name,\n-\t\t\t struct iommufd_ctx *ictx,\n-\t\t\t const struct file_operations *fops)\n+static const struct fd_slot *iommufd_eventq_init(struct iommufd_eventq *eventq, char *name,\n+\t\t\t\tstruct iommufd_ctx *ictx,\n+\t\t\t\tconst struct file_operations *fops)\n {\n \tstruct file *filep;\n \n@@ -425,14 +425,14 @@ static int iommufd_eventq_init(struct iommufd_eventq *eventq, char *name,\n \t/* The filep is fput() by the core code during failure */\n \tfilep = anon_inode_getfile(name, fops, eventq, O_RDWR);\n \tif (IS_ERR(filep))\n-\t\treturn PTR_ERR(filep);\n+\t\treturn ERR_CAST(filep);\n \n \teventq-\u003eictx = ictx;\n \tiommufd_ctx_get(eventq-\u003eictx);\n \teventq-\u003efilep = filep;\n \trefcount_inc(\u0026eventq-\u003eobj.users);\n \n-\treturn get_unused_fd_flags(O_CLOEXEC);\n+\treturn fd_prepare(O_CLOEXEC);\n }\n \n static const struct file_operations iommufd_fault_fops =\n@@ -442,7 +442,7 @@ int iommufd_fault_alloc(struct iommufd_ucmd *ucmd)\n {\n \tstruct iommu_fault_alloc *cmd = ucmd-\u003ecmd;\n \tstruct iommufd_fault *fault;\n-\tint fdno;\n+\tconst struct fd_slot *fdno;\n \tint rc;\n \n \tif (cmd-\u003eflags)\n@@ -458,22 +458,19 @@ int iommufd_fault_alloc(struct iommufd_ucmd *ucmd)\n \n \tfdno = iommufd_eventq_init(\u0026fault-\u003ecommon, \"[iommufd-pgfault]\",\n \t\t\t\t ucmd-\u003eictx, \u0026iommufd_fault_fops);\n-\tif (fdno \u003c 0)\n-\t\treturn fdno;\n+\tif (IS_ERR(fdno))\n+\t\treturn PTR_ERR(fdno);\n \n \tcmd-\u003eout_fault_id = fault-\u003ecommon.obj.id;\n-\tcmd-\u003eout_fault_fd = fdno;\n+\tcmd-\u003eout_fault_fd = fd_prepare_fd(fdno);\n \n \trc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));\n \tif (rc)\n-\t\tgoto out_put_fdno;\n+\t\treturn rc;\n \n-\tfd_install(fdno, fault-\u003ecommon.filep);\n+\tfd_stage(fdno, fault-\u003ecommon.filep);\n \n \treturn 0;\n-out_put_fdno:\n-\tput_unused_fd(fdno);\n-\treturn rc;\n }\n \n int iommufd_fault_iopf_handler(struct iopf_group *group)\n@@ -506,7 +503,7 @@ int iommufd_veventq_alloc(struct iommufd_ucmd *ucmd)\n \tstruct iommu_veventq_alloc *cmd = ucmd-\u003ecmd;\n \tstruct iommufd_veventq *veventq;\n \tstruct iommufd_viommu *viommu;\n-\tint fdno;\n+\tconst struct fd_slot *fdno;\n \tint rc;\n \n \tif (cmd-\u003eflags || cmd-\u003e__reserved ||\n@@ -543,24 +540,22 @@ int iommufd_veventq_alloc(struct iommufd_ucmd *ucmd)\n \n \tfdno = iommufd_eventq_init(\u0026veventq-\u003ecommon, \"[iommufd-viommu-event]\",\n \t\t\t\t ucmd-\u003eictx, \u0026iommufd_veventq_fops);\n-\tif (fdno \u003c 0) {\n-\t\trc = fdno;\n+\tif (IS_ERR(fdno)) {\n+\t\trc = PTR_ERR(fdno);\n \t\tgoto out_abort;\n \t}\n \n \tcmd-\u003eout_veventq_id = veventq-\u003ecommon.obj.id;\n-\tcmd-\u003eout_veventq_fd = fdno;\n+\tcmd-\u003eout_veventq_fd = fd_prepare_fd(fdno);\n \n \trc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));\n \tif (rc)\n-\t\tgoto out_put_fdno;\n+\t\tgoto out_abort;\n \n \tiommufd_object_finalize(ucmd-\u003eictx, \u0026veventq-\u003ecommon.obj);\n-\tfd_install(fdno, veventq-\u003ecommon.filep);\n+\tfd_stage(fdno, veventq-\u003ecommon.filep);\n \tgoto out_unlock_veventqs;\n \n-out_put_fdno:\n-\tput_unused_fd(fdno);\n out_abort:\n \tiommufd_object_abort_and_destroy(ucmd-\u003eictx, \u0026veventq-\u003ecommon.obj);\n out_unlock_veventqs:\ndiff --git a/drivers/media/mc/mc-request.c b/drivers/media/mc/mc-request.c\nindex 13e77648807c2..c9296bdbee271 100644\n--- a/drivers/media/mc/mc-request.c\n+++ b/drivers/media/mc/mc-request.c\n@@ -316,8 +316,8 @@ int media_request_alloc(struct media_device *mdev, int *alloc_fd)\n \tFD_PREPARE(fdf, O_CLOEXEC,\n \t\t anon_inode_getfile(\"request\", \u0026request_fops, NULL,\n \t\t\t\t O_CLOEXEC));\n-\tif (fdf.err) {\n-\t\tret = fdf.err;\n+\tif (IS_ERR(fdf)) {\n+\t\tret = PTR_ERR(fdf);\n \t\tgoto err_free_req;\n \t}\n \n@@ -328,7 +328,7 @@ int media_request_alloc(struct media_device *mdev, int *alloc_fd)\n \tatomic_inc(\u0026mdev-\u003enum_requests);\n \tdev_dbg(mdev-\u003edev, \"request: allocated %s\\n\", req-\u003edebug_str);\n \n-\t*alloc_fd = fd_publish(fdf);\n+\t*alloc_fd = fd_prepare_fd(fdf);\n \n \treturn 0;\n \ndiff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c\nindex 90fd669636ec1..24b84019bbd79 100644\n--- a/drivers/misc/fastrpc.c\n+++ b/drivers/misc/fastrpc.c\n@@ -7,6 +7,7 @@\n #include \u003clinux/dma-buf.h\u003e\n #include \u003clinux/dma-mapping.h\u003e\n #include \u003clinux/dma-resv.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/idr.h\u003e\n #include \u003clinux/list.h\u003e\n #include \u003clinux/miscdevice.h\u003e\n@@ -1804,6 +1805,7 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)\n \tDEFINE_DMA_BUF_EXPORT_INFO(exp_info);\n \tstruct fastrpc_buf *buf = NULL;\n \tint err;\n+\tconst struct fd_slot *fd;\n \n \tif (copy_from_user(\u0026bp, argp, sizeof(bp)))\n \t\treturn -EFAULT;\n@@ -1822,23 +1824,16 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)\n \t\treturn err;\n \t}\n \n-\tbp.fd = dma_buf_fd(buf-\u003edmabuf, O_ACCMODE);\n-\tif (bp.fd \u003c 0) {\n+\tfd = fd_prepare(O_ACCMODE);\n+\tif (IS_ERR(fd)) {\n \t\tdma_buf_put(buf-\u003edmabuf);\n \t\treturn -EINVAL;\n \t}\n+\tbp.fd = fd_stage(fd, buf-\u003edmabuf-\u003efile);\n \n-\tif (copy_to_user(argp, \u0026bp, sizeof(bp))) {\n-\t\t/*\n-\t\t * The usercopy failed, but we can't do much about it, as\n-\t\t * dma_buf_fd() already called fd_install() and made the\n-\t\t * file descriptor accessible for the current process. It\n-\t\t * might already be closed and dmabuf no longer valid when\n-\t\t * we reach this point. Therefore \"leak\" the fd and rely on\n-\t\t * the process exit path to do any required cleanup.\n-\t\t */\n+\t/* The staged file is dropped with its descriptor if this faults. */\n+\tif (copy_to_user(argp, \u0026bp, sizeof(bp)))\n \t\treturn -EFAULT;\n-\t}\n \n \treturn 0;\n }\ndiff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c\nindex 4a805919bb0c5..721b0d9f0e1f4 100644\n--- a/drivers/misc/ntsync.c\n+++ b/drivers/misc/ntsync.c\n@@ -724,10 +724,10 @@ static int ntsync_obj_get_fd(struct ntsync_obj *obj)\n {\n \tFD_PREPARE(fdf, O_CLOEXEC,\n \t\t anon_inode_getfile(\"ntsync\", \u0026ntsync_obj_fops, obj, O_RDWR));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \tobj-\u003efile = fd_prepare_file(fdf);\n-\treturn fd_publish(fdf);\n+\treturn fd_prepare_fd(fdf);\n }\n \n static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp)\ndiff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c\nindex 423ead48aafe3..93d9d21f4074a 100644\n--- a/drivers/vfio/vfio_main.c\n+++ b/drivers/vfio/vfio_main.c\n@@ -903,28 +903,17 @@ EXPORT_SYMBOL_GPL(vfio_mig_get_next_state);\n static int vfio_ioct_mig_return_fd(struct file *filp, void __user *arg,\n \t\t\t\t struct vfio_device_feature_mig_state *mig)\n {\n-\tint ret;\n-\tint fd;\n+\tFD_PREPARE(fdf, O_CLOEXEC, filp);\n \n-\tfd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (fd \u003c 0) {\n-\t\tret = fd;\n-\t\tgoto out_fput;\n+\tif (IS_ERR(fdf)) {\n+\t\tfput(filp);\n+\t\treturn PTR_ERR(fdf);\n \t}\n \n-\tmig-\u003edata_fd = fd;\n-\tif (copy_to_user(arg, mig, sizeof(*mig))) {\n-\t\tret = -EFAULT;\n-\t\tgoto out_put_unused;\n-\t}\n-\tfd_install(fd, filp);\n+\tmig-\u003edata_fd = fd_prepare_fd(fdf);\n+\tif (copy_to_user(arg, mig, sizeof(*mig)))\n+\t\treturn -EFAULT;\n \treturn 0;\n-\n-out_put_unused:\n-\tput_unused_fd(fd);\n-out_fput:\n-\tfput(filp);\n-\treturn ret;\n }\n \n static int\ndiff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c\nindex c91300a73f502..93c3f00f54433 100644\n--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c\n+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c\n@@ -1589,7 +1589,7 @@ static const struct file_operations ne_enclave_fops = {\n static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_uid)\n {\n \tstruct ne_pci_dev_cmd_reply cmd_reply = {};\n-\tint enclave_fd = -1;\n+\tconst struct fd_slot *enclave_fd = NULL;\n \tstruct file *enclave_file = NULL;\n \tunsigned int i = 0;\n \tstruct ne_enclave *ne_enclave = NULL;\n@@ -1647,9 +1647,9 @@ static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_ui\n \t\tgoto free_cpumask;\n \t}\n \n-\tenclave_fd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (enclave_fd \u003c 0) {\n-\t\trc = enclave_fd;\n+\tenclave_fd = fd_prepare(O_CLOEXEC);\n+\tif (IS_ERR(enclave_fd)) {\n+\t\trc = PTR_ERR(enclave_fd);\n \n \t\tdev_err_ratelimited(ne_misc_dev.this_device,\n \t\t\t\t \"Error in getting unused fd [rc=%d]\\n\", rc);\n@@ -1664,7 +1664,7 @@ static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_ui\n \t\tdev_err_ratelimited(ne_misc_dev.this_device,\n \t\t\t\t \"Error in anon inode get file [rc=%d]\\n\", rc);\n \n-\t\tgoto put_fd;\n+\t\tgoto free_cpumask;\n \t}\n \n \trc = ne_do_request(pdev, SLOT_ALLOC,\n@@ -1688,27 +1688,20 @@ static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_ui\n \n \tlist_add(\u0026ne_enclave-\u003eenclave_list_entry, \u0026ne_pci_dev-\u003eenclaves_list);\n \n-\tif (copy_to_user(slot_uid, \u0026ne_enclave-\u003eslot_uid, sizeof(ne_enclave-\u003eslot_uid))) {\n-\t\t/*\n-\t\t * As we're holding the only reference to 'enclave_file', fput()\n-\t\t * will call ne_enclave_release() which will do a proper cleanup\n-\t\t * of all so far allocated resources, leaving only the unused fd\n-\t\t * for us to free.\n-\t\t */\n-\t\tfput(enclave_file);\n-\t\tput_unused_fd(enclave_fd);\n+\tfd_stage(enclave_fd, enclave_file);\n \n+\t/*\n+\t * The failed ioctl drops the descriptor and with it the only reference\n+\t * to 'enclave_file', so ne_enclave_release() does a proper cleanup of\n+\t * all so far allocated resources.\n+\t */\n+\tif (copy_to_user(slot_uid, \u0026ne_enclave-\u003eslot_uid, sizeof(ne_enclave-\u003eslot_uid)))\n \t\treturn -EFAULT;\n-\t}\n-\n-\tfd_install(enclave_fd, enclave_file);\n \n-\treturn enclave_fd;\n+\treturn fd_prepare_fd(enclave_fd);\n \n put_file:\n \tfput(enclave_file);\n-put_fd:\n-\tput_unused_fd(enclave_fd);\n free_cpumask:\n \tfree_cpumask_var(ne_enclave-\u003evcpu_ids);\n \tfor (i = 0; i \u003c ne_enclave-\u003enr_parent_vm_cores; i++)\ndiff --git a/drivers/xen/gntdev-dmabuf.c b/drivers/xen/gntdev-dmabuf.c\nindex 83b0df4608941..a3ecc7c839741 100644\n--- a/drivers/xen/gntdev-dmabuf.c\n+++ b/drivers/xen/gntdev-dmabuf.c\n@@ -357,10 +357,10 @@ static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)\n {\n \tDEFINE_DMA_BUF_EXPORT_INFO(exp_info);\n \tstruct gntdev_dmabuf *gntdev_dmabuf __free(kfree) = NULL;\n-\tCLASS(get_unused_fd, ret)(O_CLOEXEC);\n+\tconst struct fd_slot *fd = fd_prepare(O_CLOEXEC);\n \n-\tif (ret \u003c 0)\n-\t\treturn ret;\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n \tgntdev_dmabuf = kzalloc_obj(*gntdev_dmabuf);\n \tif (!gntdev_dmabuf)\n@@ -388,17 +388,17 @@ static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)\n \tif (IS_ERR(gntdev_dmabuf-\u003edmabuf))\n \t\treturn PTR_ERR(gntdev_dmabuf-\u003edmabuf);\n \n-\tgntdev_dmabuf-\u003efd = ret;\n-\targs-\u003efd = ret;\n+\tgntdev_dmabuf-\u003efd = fd_prepare_fd(fd);\n+\targs-\u003efd = fd_prepare_fd(fd);\n \n-\tpr_debug(\"Exporting DMA buffer with fd %d\\n\", ret);\n+\tpr_debug(\"Exporting DMA buffer with fd %d\\n\", fd_prepare_fd(fd));\n \n \tget_file(gntdev_dmabuf-\u003epriv-\u003efilp);\n \tmutex_lock(\u0026args-\u003edmabuf_priv-\u003elock);\n \tlist_add(\u0026gntdev_dmabuf-\u003enext, \u0026args-\u003edmabuf_priv-\u003eexp_list);\n \tmutex_unlock(\u0026args-\u003edmabuf_priv-\u003elock);\n \n-\tfd_install(take_fd(ret), no_free_ptr(gntdev_dmabuf)-\u003edmabuf-\u003efile);\n+\tfd_stage(fd, no_free_ptr(gntdev_dmabuf)-\u003edmabuf-\u003efile);\n \treturn 0;\n }\n \ndiff --git a/fs/eventfd.c b/fs/eventfd.c\nindex 9d33a02757d52..a00fea879f9a5 100644\n--- a/fs/eventfd.c\n+++ b/fs/eventfd.c\n@@ -403,12 +403,12 @@ static int do_eventfd(unsigned int count, int flags)\n \tFD_PREPARE(fdf, flags,\n \t\t anon_inode_getfile_fmode(\"[eventfd]\", \u0026eventfd_fops, ctx,\n \t\t\t\t\t flags, FMODE_NOWAIT));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \n \tctx-\u003eid = ida_alloc(\u0026eventfd_ida, GFP_KERNEL);\n \tretain_and_null_ptr(ctx);\n-\treturn fd_publish(fdf);\n+\treturn fd_prepare_fd(fdf);\n }\n \n SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)\ndiff --git a/fs/eventpoll.c b/fs/eventpoll.c\nindex e0c4bf88a8383..81857725f8108 100644\n--- a/fs/eventpoll.c\n+++ b/fs/eventpoll.c\n@@ -2514,12 +2514,12 @@ static int do_epoll_create(int flags)\n \tFD_PREPARE(fdf, O_RDWR | (flags \u0026 O_CLOEXEC),\n \t\t anon_inode_getfile(\"[eventpoll]\", \u0026eventpoll_fops, ep,\n \t\t\t\t O_RDWR | (flags \u0026 O_CLOEXEC)));\n-\tif (fdf.err) {\n+\tif (IS_ERR(fdf)) {\n \t\tep_clear_and_put(ep);\n-\t\treturn fdf.err;\n+\t\treturn PTR_ERR(fdf);\n \t}\n \tep-\u003efile = fd_prepare_file(fdf);\n-\treturn fd_publish(fdf);\n+\treturn fd_prepare_fd(fdf);\n }\n \n SYSCALL_DEFINE1(epoll_create1, int, flags)\ndiff --git a/fs/exec.c b/fs/exec.c\nindex 745f6eb5279e6..1aa0f7b99fc37 100644\n--- a/fs/exec.c\n+++ b/fs/exec.c\n@@ -1159,6 +1159,9 @@ int begin_new_exec(struct linux_binprm * bprm)\n \t */\n \tio_uring_task_cancel();\n \n+\t/* A prepared descriptor would be installed into the new program. */\n+\tVFS_WARN_ON_ONCE(me-\u003efd_slots.nr);\n+\n \t/* Ensure the files table is not shared. */\n \tretval = unshare_files();\n \tif (retval)\ndiff --git a/fs/file.c b/fs/file.c\nindex 628ca07dc4b17..7c05246a5129b 100644\n--- a/fs/file.c\n+++ b/fs/file.c\n@@ -23,6 +23,7 @@\n #include \u003clinux/file_ref.h\u003e\n #include \u003cnet/sock.h\u003e\n #include \u003clinux/init_task.h\u003e\n+#include \u003casm/syscall.h\u003e\n \n #include \"internal.h\"\n \n@@ -630,14 +631,236 @@ static void __put_unused_fd(struct files_struct *files, unsigned int fd)\n \t\tfiles-\u003enext_fd = fd;\n }\n \n-void put_unused_fd(unsigned int fd)\n+/* Release @fd in the table, its slot is the caller's business. */\n+static void fd_release(unsigned int fd)\n {\n \tstruct files_struct *files = current-\u003efiles;\n+\n \tspin_lock(\u0026files-\u003efile_lock);\n \t__put_unused_fd(files, fd);\n \tspin_unlock(\u0026files-\u003efile_lock);\n }\n \n+/* Enough for SCM_MAX_FD, and a page of slots on 4K pages. */\n+#define FD_SLOTS_SPILL_MIN\t256\n+\n+/* Make the syscall exit path call fd_slots_commit(). */\n+#if defined(CONFIG_GENERIC_ENTRY)\n+#define fd_slots_set_work()\tset_syscall_work(FD_SLOTS)\n+#define fd_slots_clear_work()\tclear_syscall_work(FD_SLOTS)\n+#elif defined(TIF_FD_SLOTS)\n+#define fd_slots_set_work()\tset_thread_flag(TIF_FD_SLOTS)\n+#define fd_slots_clear_work()\tclear_thread_flag(TIF_FD_SLOTS)\n+#else\n+#error \"the syscall exit path must commit prepared file descriptors\"\n+#endif\n+\n+static struct fd_slot *fd_slot(struct fd_slots *slots, unsigned int idx)\n+{\n+\tif (idx \u003c FD_SLOTS_INLINE)\n+\t\treturn \u0026slots-\u003einline_slots[idx];\n+\treturn \u0026slots-\u003espill[idx - FD_SLOTS_INLINE];\n+}\n+\n+/* Room for slot @idx in the spill array, doubling as it fills up. */\n+static noinline struct fd_slot *fd_slots_spill(struct fd_slots *slots,\n+\t\t\t\t\t unsigned int idx)\n+{\n+\tunsigned int max = slots-\u003espill_max;\n+\tstruct fd_slot *spill;\n+\n+\tidx -= FD_SLOTS_INLINE;\n+\tif (idx \u003c max)\n+\t\treturn \u0026slots-\u003espill[idx];\n+\n+\tmax = max ? max * 2 : FD_SLOTS_SPILL_MIN;\n+\tspill = kvrealloc(slots-\u003espill, array_size(max, sizeof(*spill)),\n+\t\t\t GFP_KERNEL_ACCOUNT);\n+\tif (!spill)\n+\t\treturn NULL;\n+\tslots-\u003espill = spill;\n+\tslots-\u003espill_max = max;\n+\treturn \u0026spill[idx];\n+}\n+\n+/* Record @fd in the next slot, returns the slot. */\n+static struct fd_slot *fd_slot_record(int fd)\n+{\n+\tstruct fd_slots *slots = \u0026current-\u003efd_slots;\n+\tunsigned int idx = slots-\u003enr;\n+\tstruct fd_slot *slot;\n+\n+\t/* Nothing would ever commit what a kernel thread prepares. */\n+\tVFS_WARN_ON_ONCE(current-\u003eflags \u0026 PF_KTHREAD);\n+\n+\tif (likely(idx \u003c FD_SLOTS_INLINE)) {\n+\t\tslot = \u0026slots-\u003einline_slots[idx];\n+\t} else {\n+\t\tslot = fd_slots_spill(slots, idx);\n+\t\tif (!slot)\n+\t\t\treturn ERR_PTR(-ENOMEM);\n+\t}\n+\tACCESS_PRIVATE(slot, fd) = fd;\n+\tACCESS_PRIVATE(slot, file) = NULL;\n+\tif (!idx)\n+\t\tfd_slots_set_work();\n+\tslots-\u003enr = idx + 1;\n+\treturn slot;\n+}\n+\n+/* The slot holding @fd, if this syscall prepared it. */\n+static inline int fd_slot_find(struct fd_slots *slots, unsigned int fd)\n+{\n+\tunsigned int idx = slots-\u003enr;\n+\n+\twhile (idx--) {\n+\t\tif (ACCESS_PRIVATE(fd_slot(slots, idx), fd) == fd)\n+\t\t\treturn idx;\n+\t}\n+\treturn -1;\n+}\n+\n+/**\n+ * fd_prepare - allocate a descriptor that the syscall exit installs\n+ * @flags: O_CLOEXEC or 0\n+ *\n+ * Returns the prepared slot as a const handle or an error pointer.\n+ */\n+const struct fd_slot *fd_prepare(unsigned flags)\n+{\n+\tstruct fd_slot *slot;\n+\tint fd;\n+\n+\tfd = get_unused_fd_flags(flags);\n+\tif (fd \u003c 0)\n+\t\treturn ERR_PTR(fd);\n+\n+\tslot = fd_slot_record(fd);\n+\tif (IS_ERR(slot))\n+\t\tfd_release(fd);\n+\treturn slot;\n+}\n+EXPORT_SYMBOL(fd_prepare);\n+\n+/**\n+ * fd_stage - attach the file to a prepared slot\n+ * @slot: slot from fd_prepare()\n+ * @file: the file to install, consumed\n+ *\n+ * Returns the number. The syscall exit installs @file there when the syscall\n+ * returns success and drops it when the syscall returns an error.\n+ */\n+int fd_stage(const struct fd_slot *slot, struct file *file)\n+{\n+\tstruct fd_slot *s = (struct fd_slot *)slot;\n+\n+\tVFS_WARN_ON_ONCE(ACCESS_PRIVATE(s, file));\n+\tACCESS_PRIVATE(s, file) = file;\n+\treturn ACCESS_PRIVATE(s, fd);\n+}\n+EXPORT_SYMBOL(fd_stage);\n+\n+/**\n+ * fd_prepare_fd - the descriptor number of a prepared slot\n+ * @slot: slot from fd_prepare()\n+ */\n+int fd_prepare_fd(const struct fd_slot *slot)\n+{\n+\treturn ACCESS_PRIVATE(slot, fd);\n+}\n+EXPORT_SYMBOL(fd_prepare_fd);\n+\n+/**\n+ * fd_prepare_file - the file staged into a slot, to configure before install\n+ * @slot: slot from fd_prepare()\n+ *\n+ * Returns the file handed to fd_stage(), or NULL before one is staged.\n+ */\n+struct file *fd_prepare_file(const struct fd_slot *slot)\n+{\n+\treturn ACCESS_PRIVATE(slot, file);\n+}\n+EXPORT_SYMBOL(fd_prepare_file);\n+\n+/* Install every staged file, release the slots that never got one. */\n+static void fd_slots_install(struct fd_slots *slots)\n+{\n+\tunsigned int idx;\n+\n+\tfor (idx = 0; idx \u003c slots-\u003enr; idx++) {\n+\t\tstruct fd_slot *slot = fd_slot(slots, idx);\n+\n+\t\tif (ACCESS_PRIVATE(slot, file))\n+\t\t\tfd_install(ACCESS_PRIVATE(slot, fd),\n+\t\t\t\t ACCESS_PRIVATE(slot, file));\n+\t\telse\n+\t\t\tfd_release(ACCESS_PRIVATE(slot, fd));\n+\t}\n+}\n+\n+/* Release every slot's descriptor and drop the staged files. */\n+static void fd_slots_drop(struct fd_slots *slots)\n+{\n+\tstruct files_struct *files = current-\u003efiles;\n+\tunsigned int idx;\n+\n+\tspin_lock(\u0026files-\u003efile_lock);\n+\tfor (idx = 0; idx \u003c slots-\u003enr; idx++)\n+\t\t__put_unused_fd(files, ACCESS_PRIVATE(fd_slot(slots, idx), fd));\n+\tspin_unlock(\u0026files-\u003efile_lock);\n+\tfor (idx = 0; idx \u003c slots-\u003enr; idx++) {\n+\t\tstruct fd_slot *slot = fd_slot(slots, idx);\n+\n+\t\tif (ACCESS_PRIVATE(slot, file))\n+\t\t\tfput(ACCESS_PRIVATE(slot, file));\n+\t}\n+}\n+\n+static __always_inline void fd_slots_finish(struct fd_slots *slots, bool failed)\n+{\n+\tif (likely(!failed))\n+\t\tfd_slots_install(slots);\n+\telse\n+\t\tfd_slots_drop(slots);\n+\tslots-\u003enr = 0;\n+\tfd_slots_clear_work();\n+}\n+\n+/* Syscall exit hook, keyed on the return value the caller will see. */\n+void fd_slots_commit(struct pt_regs *regs)\n+{\n+\tfd_slots_finish(\u0026current-\u003efd_slots,\n+\t\t\tsyscall_get_error(current, regs) != 0);\n+}\n+\n+/* Install or drop the prepared descriptors based on @ret. */\n+void __fd_slots_commit(long ret)\n+{\n+\tfd_slots_finish(\u0026current-\u003efd_slots, IS_ERR_VALUE(ret));\n+}\n+\n+void exit_fd_slots(void)\n+{\n+\tstruct fd_slots *slots = \u0026current-\u003efd_slots;\n+\n+\t/* A syscall must not exit with prepared descriptors outstanding. */\n+\tif (WARN_ON_ONCE(slots-\u003enr))\n+\t\tfd_slots_finish(slots, true);\n+\tkvfree(slots-\u003espill);\n+}\n+\n+/**\n+ * put_unused_fd - give a descriptor back before it got a file\n+ * @fd: descriptor returned by get_unused_fd_flags()\n+ *\n+ * Not for a prepared descriptor, the syscall exit releases that one.\n+ */\n+void put_unused_fd(unsigned int fd)\n+{\n+\tVFS_WARN_ON_ONCE(fd_slot_find(\u0026current-\u003efd_slots, fd) \u003e= 0);\n+\tfd_release(fd);\n+}\n+\n EXPORT_SYMBOL(put_unused_fd);\n \n /*\n@@ -1384,25 +1607,27 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)\n */\n int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags)\n {\n-\tint error;\n+\tint fd, error;\n \n \terror = security_file_receive(file);\n \tif (error)\n \t\treturn error;\n \n-\tFD_PREPARE(fdf, o_flags, file);\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n-\tget_file(file);\n+\tfd = get_unused_fd_flags(o_flags);\n+\tif (fd \u003c 0)\n+\t\treturn fd;\n \n \tif (ufd) {\n-\t\terror = put_user(fd_prepare_fd(fdf), ufd);\n-\t\tif (error)\n+\t\terror = put_user(fd, ufd);\n+\t\tif (error) {\n+\t\t\tput_unused_fd(fd);\n \t\t\treturn error;\n+\t\t}\n \t}\n \n-\t__receive_sock(fd_prepare_file(fdf));\n-\treturn fd_publish(fdf);\n+\t__receive_sock(file);\n+\tfd_install(fd, get_file(file));\n+\treturn fd;\n }\n EXPORT_SYMBOL_GPL(receive_fd);\n \ndiff --git a/fs/namespace.c b/fs/namespace.c\nindex 1ecd96c918b33..fba29bcc01646 100644\n--- a/fs/namespace.c\n+++ b/fs/namespace.c\n@@ -4544,9 +4544,9 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,\n \n \tFD_PREPARE(fdf, (flags \u0026 FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0,\n \t\t dentry_open(\u0026new_path, O_PATH, fc-\u003ecred));\n-\tif (fdf.err) {\n+\tif (IS_ERR(fdf)) {\n \t\tdissolve_on_fput(new_path.mnt);\n-\t\treturn fdf.err;\n+\t\treturn PTR_ERR(fdf);\n \t}\n \n \t/*\n@@ -4554,7 +4554,7 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,\n \t * need to unmount it, not just simply put it.\n \t */\n \tfd_prepare_file(fdf)-\u003ef_mode |= FMODE_NEED_UNMOUNT;\n-\treturn fd_publish(fdf);\n+\treturn fd_prepare_fd(fdf);\n }\n \n static inline int vfs_move_mount(const struct path *from_path,\n@@ -5198,8 +5198,8 @@ SYSCALL_DEFINE5(open_tree_attr, int, dfd, const char __user *, filename,\n \t\treturn -EINVAL;\n \n \tFD_PREPARE(fdf, flags, vfs_open_tree(dfd, filename, flags));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \n \tif (uattr) {\n \t\tstruct mount_kattr kattr = {};\n@@ -5220,7 +5220,7 @@ SYSCALL_DEFINE5(open_tree_attr, int, dfd, const char __user *, filename,\n \t\t\treturn ret;\n \t}\n \n-\treturn fd_publish(fdf);\n+\treturn fd_prepare_fd(fdf);\n }\n \n int show_path(struct seq_file *m, struct dentry *root)\ndiff --git a/fs/nsfs.c b/fs/nsfs.c\nindex c3b6ae76594a8..da021c8b49b29 100644\n--- a/fs/nsfs.c\n+++ b/fs/nsfs.c\n@@ -348,8 +348,8 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,\n \t\t\treturn ret;\n \n \t\tFD_PREPARE(fdf, O_CLOEXEC, dentry_open(\u0026path, O_RDONLY, current_cred()));\n-\t\tif (fdf.err)\n-\t\t\treturn fdf.err;\n+\t\tif (IS_ERR(fdf))\n+\t\t\treturn PTR_ERR(fdf);\n \t\t/*\n \t\t * If @uinfo is passed return all information about the\n \t\t * mount namespace as well.\n@@ -357,7 +357,7 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,\n \t\tret = copy_ns_info_to_user(to_mnt_ns(ns), uinfo, usize, \u0026kinfo);\n \t\tif (ret)\n \t\t\treturn ret;\n-\t\tret = fd_publish(fdf);\n+\t\tret = fd_prepare_fd(fdf);\n \t\tbreak;\n \t}\n \tdefault:\ndiff --git a/fs/xfs/xfs_handle.c b/fs/xfs/xfs_handle.c\nindex 0689cade8f74c..174aa3c8260f6 100644\n--- a/fs/xfs/xfs_handle.c\n+++ b/fs/xfs/xfs_handle.c\n@@ -272,8 +272,8 @@ xfs_open_by_handle(\n \tpath.mnt = mntget(parfilp-\u003ef_path.mnt);\n \n \tFD_PREPARE(fdf, 0, dentry_open(\u0026path, hreq-\u003eoflags, cred));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \n \tif (S_ISREG(inode-\u003ei_mode)) {\n \t\tstruct file *filp = fd_prepare_file(fdf);\n@@ -282,7 +282,7 @@ xfs_open_by_handle(\n \t\tfilp-\u003ef_mode |= FMODE_NOCMTIME;\n \t}\n \n-\treturn fd_publish(fdf);\n+\treturn fd_prepare_fd(fdf);\n }\n \n int\ndiff --git a/include/linux/entry-common.h b/include/linux/entry-common.h\nindex 6574b7183c017..2da30e50cf596 100644\n--- a/include/linux/entry-common.h\n+++ b/include/linux/entry-common.h\n@@ -3,6 +3,7 @@\n #define __LINUX_ENTRYCOMMON_H\n \n #include \u003clinux/audit.h\u003e\n+#include \u003clinux/file.h\u003e\n #include \u003clinux/irq-entry-common.h\u003e\n #include \u003clinux/livepatch.h\u003e\n #include \u003clinux/ptrace.h\u003e\n@@ -36,7 +37,8 @@\n \t\t\t\t SYSCALL_WORK_SYSCALL_TRACE |\t\t\\\n \t\t\t\t SYSCALL_WORK_SYSCALL_AUDIT |\t\t\\\n \t\t\t\t SYSCALL_WORK_SYSCALL_USER_DISPATCH |\t\\\n-\t\t\t\t SYSCALL_WORK_SYSCALL_EXIT_TRAP)\n+\t\t\t\t SYSCALL_WORK_SYSCALL_EXIT_TRAP |\t\\\n+\t\t\t\t SYSCALL_WORK_FD_SLOTS)\n \n /**\n * arch_ptrace_report_syscall_permit_entry - Architecture specific wrapper for\n@@ -245,6 +247,10 @@ static __always_inline void syscall_exit_work(struct pt_regs *regs, unsigned lon\n {\n \tbool step;\n \n+\t/* Install or drop the descriptors the syscall prepared. */\n+\tif (work \u0026 SYSCALL_WORK_FD_SLOTS)\n+\t\tfd_slots_commit(regs);\n+\n \t/*\n \t * If the syscall was rolled back due to syscall user dispatching,\n \t * then the tracers below are not invoked for the same reason as\ndiff --git a/include/linux/file.h b/include/linux/file.h\nindex 27484b444d315..45d68e097c05e 100644\n--- a/include/linux/file.h\n+++ b/include/linux/file.h\n@@ -91,6 +91,10 @@ extern bool get_close_on_exec(unsigned int fd);\n extern int __get_unused_fd_flags(unsigned flags, unsigned long nofile);\n extern int get_unused_fd_flags(unsigned flags);\n extern void put_unused_fd(unsigned int fd);\n+void __fd_slots_commit(long ret);\n+struct pt_regs;\n+void fd_slots_commit(struct pt_regs *regs);\n+void exit_fd_slots(void);\n \n DEFINE_CLASS(get_unused_fd, int, if (_T \u003e= 0) put_unused_fd(_T),\n \t get_unused_fd_flags(flags), unsigned flags)\n@@ -118,6 +122,12 @@ DEFINE_FREE(fput, struct file *, if (!IS_ERR_OR_NULL(_T)) fput(_T))\n \n extern void fd_install(unsigned int fd, struct file *file);\n \n+struct fd_slot;\n+const struct fd_slot *fd_prepare(unsigned flags);\n+int fd_stage(const struct fd_slot *slot, struct file *file);\n+int fd_prepare_fd(const struct fd_slot *slot);\n+struct file *fd_prepare_file(const struct fd_slot *slot);\n+\n int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags);\n \n int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags);\n@@ -128,128 +138,46 @@ extern void __fput_sync(struct file *);\n extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;\n \n /*\n- * fd_prepare: Combined fd + file allocation cleanup class.\n- * @err: Error code to indicate if allocation succeeded.\n- * @__fd: Allocated fd (may not be accessed directly)\n- * @__file: Allocated struct file pointer (may not be accessed directly)\n- *\n- * Allocates an fd and a file together. On error paths, automatically cleans\n- * up whichever resource was successfully allocated. Allows flexible file\n- * allocation with different functions per usage.\n- *\n- * Do not use directly.\n- */\n-struct fd_prepare {\n-\ts32 err;\n-\ts32 __fd; /* do not access directly */\n-\tstruct file *__file; /* do not access directly */\n-};\n-\n-/* Typedef for fd_prepare cleanup guards. */\n-typedef struct fd_prepare class_fd_prepare_t;\n-\n-/*\n- * Accessors for fd_prepare class members.\n- * _Generic() is used for zero-cost type safety.\n- */\n-#define fd_prepare_fd(_fdf) \\\n-\t(_Generic((_fdf), struct fd_prepare: (_fdf).__fd))\n-\n-#define fd_prepare_file(_fdf) \\\n-\t(_Generic((_fdf), struct fd_prepare: (_fdf).__file))\n-\n-/* Do not use directly. */\n-static inline void class_fd_prepare_destructor(const struct fd_prepare *fdf)\n-{\n-\tif (unlikely(fdf-\u003e__fd \u003e= 0))\n-\t\tput_unused_fd(fdf-\u003e__fd);\n-\tif (unlikely(!IS_ERR_OR_NULL(fdf-\u003e__file)))\n-\t\tfput(fdf-\u003e__file);\n-}\n-\n-/* Do not use directly. */\n-static inline int class_fd_prepare_lock_err(const struct fd_prepare *fdf)\n-{\n-\tif (unlikely(fdf-\u003eerr))\n-\t\treturn fdf-\u003eerr;\n-\tif (unlikely(fdf-\u003e__fd \u003c 0))\n-\t\treturn fdf-\u003e__fd;\n-\tif (unlikely(IS_ERR(fdf-\u003e__file)))\n-\t\treturn PTR_ERR(fdf-\u003e__file);\n-\tif (unlikely(!fdf-\u003e__file))\n-\t\treturn -ENOMEM;\n-\treturn 0;\n-}\n-\n-/*\n- * __FD_PREPARE_INIT - Helper to initialize fd_prepare class.\n+ * FD_ADD - allocate a descriptor, build the file and install it in one step.\n * @_fd_flags: flags for get_unused_fd_flags()\n- * @_file_owned: expression that returns struct file *\n- *\n- * Returns a struct fd_prepare with fd, file, and err set.\n- * If fd allocation fails, fd will be negative and err will be set. If\n- * fd succeeds but file_init_expr fails, file will be ERR_PTR and err\n- * will be set. The err field is the single source of truth for error\n- * checking.\n- */\n-#define __FD_PREPARE_INIT(_fd_flags, _file_owned) \\\n-\t({ \\\n-\t\tstruct fd_prepare fdf = { \\\n-\t\t\t.__fd = get_unused_fd_flags((_fd_flags)), \\\n-\t\t}; \\\n-\t\tif (likely(fdf.__fd \u003e= 0)) \\\n-\t\t\tfdf.__file = (_file_owned); \\\n-\t\tfdf.err = ACQUIRE_ERR(fd_prepare, \u0026fdf); \\\n-\t\tfdf; \\\n-\t})\n-\n-/*\n- * FD_PREPARE - Macro to declare and initialize an fd_prepare variable.\n- *\n- * Declares and initializes an fd_prepare variable with automatic\n- * cleanup. No separate scope required - cleanup happens when variable\n- * goes out of scope.\n+ * @_file_owned: struct file to take ownership of (can be an expression)\n *\n- * @_fdf: name of struct fd_prepare variable to define\n- * @_fd_flags: flags for get_unused_fd_flags()\n- * @_file_owned: struct file to take ownership of (can be expression)\n+ * The file expression is evaluated only after the descriptor is allocated, so\n+ * a full table does not run its side effects. Drops the file and returns a\n+ * negative errno on failure. Installs immediately: for anything more than a\n+ * bare install-and-return (reporting the number, configuring the file,\n+ * installing several descriptors) use fd_prepare()/fd_stage().\n */\n-#define FD_PREPARE(_fdf, _fd_flags, _file_owned) \\\n-\tCLASS_INIT(fd_prepare, _fdf, __FD_PREPARE_INIT(_fd_flags, _file_owned))\n+#define FD_ADD(_fd_flags, _file_owned)\t\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tint __fd = get_unused_fd_flags(_fd_flags);\t\t\t\\\n+\tif (likely(__fd \u003e= 0)) {\t\t\t\t\t\\\n+\t\tstruct file *__file = (_file_owned);\t\t\t\\\n+\t\tif (unlikely(IS_ERR_OR_NULL(__file))) {\t\t\t\\\n+\t\t\tput_unused_fd(__fd);\t\t\t\t\\\n+\t\t\t__fd = __file ? PTR_ERR(__file) : -ENOMEM;\t\\\n+\t\t} else {\t\t\t\t\t\t\\\n+\t\t\tfd_install(__fd, __file);\t\t\t\\\n+\t\t}\t\t\t\t\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\\\n+\t__fd;\t\t\t\t\t\t\t\t\\\n+})\n \n /*\n- * fd_publish - Publish prepared fd and file to the fd table.\n- * @_fdf: struct fd_prepare variable\n- */\n-#define fd_publish(_fdf) \\\n-\t({ \\\n-\t\tstruct fd_prepare *fdp = \u0026(_fdf); \\\n-\t\tVFS_WARN_ON_ONCE(fdp-\u003eerr); \\\n-\t\tVFS_WARN_ON_ONCE(fdp-\u003e__fd \u003c 0); \\\n-\t\tVFS_WARN_ON_ONCE(IS_ERR_OR_NULL(fdp-\u003e__file)); \\\n-\t\tfd_install(fdp-\u003e__fd, fdp-\u003e__file); \\\n-\t\tretain_and_null_ptr(fdp-\u003e__file); \\\n-\t\ttake_fd(fdp-\u003e__fd); \\\n-\t})\n-\n-/* Do not use directly. */\n-#define __FD_ADD(_fdf, _fd_flags, _file_owned) \\\n-\t({ \\\n-\t\tFD_PREPARE(_fdf, _fd_flags, _file_owned); \\\n-\t\ts32 ret = _fdf.err; \\\n-\t\tif (likely(!ret)) \\\n-\t\t\tret = fd_publish(_fdf); \\\n-\t\tret; \\\n-\t})\n-\n-/*\n- * FD_ADD - Allocate and install an fd and file in one step.\n+ * FD_PREPARE - reserve a descriptor and stage @_file_owned on it for the\n+ * install at syscall exit; declares @_fdf, an fd_prepare() slot.\n+ * @_fdf: name of the const struct fd_slot * to declare\n * @_fd_flags: flags for get_unused_fd_flags()\n- * @_file_owned: struct file to take ownership of\n- *\n- * Returns the allocated fd number, or negative error code on failure.\n+ * @_file_owned: struct file to take ownership of (can be an expression)\n */\n-#define FD_ADD(_fd_flags, _file_owned) \\\n-\t__FD_ADD(__UNIQUE_ID(fd_prepare), _fd_flags, _file_owned)\n+#define FD_PREPARE(_fdf, _fd_flags, _file_owned)\t\t\t\\\n+\tconst struct fd_slot *_fdf = fd_prepare(_fd_flags);\t\t\\\n+\tif (!IS_ERR(_fdf)) {\t\t\t\t\t\t\\\n+\t\tstruct file *__file = (_file_owned);\t\t\t\\\n+\t\tif (unlikely(IS_ERR_OR_NULL(__file)))\t\t\t\\\n+\t\t\t_fdf = __file ? ERR_CAST(__file) : ERR_PTR(-ENOMEM); \\\n+\t\telse\t\t\t\t\t\t\t\\\n+\t\t\tfd_stage(_fdf, __file);\t\t\t\t\\\n+\t}\n \n #endif /* __LINUX_FILE_H */\ndiff --git a/include/linux/sched.h b/include/linux/sched.h\nindex 8b3d47a325cca..52bbf9931908f 100644\n--- a/include/linux/sched.h\n+++ b/include/linux/sched.h\n@@ -832,6 +832,24 @@ struct task_ipi_mask {\n struct task_ipi_mask { };\n #endif\n \n+/* Descriptors this syscall prepared, installed when it returns. */\n+#define FD_SLOTS_INLINE\t2\n+\n+struct file;\n+\n+struct fd_slot {\n+\tstruct file\t\t\t* __private file;\n+\tint\t\t\t\t__private fd;\n+};\n+\n+/* Inline up to FD_SLOTS_INLINE slots, the rest in the spill. */\n+struct fd_slots {\n+\tunsigned int\t\t\tnr;\n+\tunsigned int\t\t\tspill_max;\n+\tstruct fd_slot\t\t\t*spill;\n+\tstruct fd_slot\t\t\tinline_slots[FD_SLOTS_INLINE];\n+};\n+\n struct task_struct {\n #ifdef CONFIG_THREAD_INFO_IN_TASK\n \t/*\n@@ -1206,6 +1224,9 @@ struct task_struct {\n \t/* Open file information: */\n \tstruct files_struct\t\t*files;\n \n+\t/* Descriptors prepared by the current syscall: */\n+\tstruct fd_slots\t\t\tfd_slots;\n+\n #ifdef CONFIG_IO_URING\n \tstruct io_uring_task\t\t*io_uring;\n \tstruct io_restriction\t\t*io_uring_restrict;\ndiff --git a/include/linux/thread_info.h b/include/linux/thread_info.h\nindex 307b8390fc670..c80a874442860 100644\n--- a/include/linux/thread_info.h\n+++ b/include/linux/thread_info.h\n@@ -47,6 +47,7 @@ enum syscall_work_bit {\n \tSYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH,\n \tSYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP,\n \tSYSCALL_WORK_BIT_SYSCALL_RSEQ_SLICE,\n+\tSYSCALL_WORK_BIT_FD_SLOTS,\n };\n \n #define SYSCALL_WORK_SECCOMP\t\t\tBIT(SYSCALL_WORK_BIT_SECCOMP)\n@@ -57,6 +58,7 @@ enum syscall_work_bit {\n #define SYSCALL_WORK_SYSCALL_USER_DISPATCH\tBIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)\n #define SYSCALL_WORK_SYSCALL_EXIT_TRAP\t\tBIT(SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP)\n #define SYSCALL_WORK_SYSCALL_RSEQ_SLICE\t\tBIT(SYSCALL_WORK_BIT_SYSCALL_RSEQ_SLICE)\n+#define SYSCALL_WORK_FD_SLOTS\t\t\tBIT(SYSCALL_WORK_BIT_FD_SLOTS)\n #endif\n \n #include \u003casm/thread_info.h\u003e\ndiff --git a/io_uring/io_uring.c b/io_uring/io_uring.c\nindex 61053421d809a..001c3683bf00d 100644\n--- a/io_uring/io_uring.c\n+++ b/io_uring/io_uring.c\n@@ -870,6 +870,10 @@ bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags)\n \tlockdep_assert(!io_wq_current_is_worker());\n \tlockdep_assert_held(\u0026ctx-\u003euring_lock);\n \n+\t/* Descriptors this CQE reports must be installed before it is visible. */\n+\tif (unlikely(current-\u003efd_slots.nr))\n+\t\t__fd_slots_commit(res);\n+\n \tif (!(ctx-\u003eint_flags \u0026 IO_RING_F_LOCKLESS_CQ)) {\n \t\tspin_lock(\u0026ctx-\u003ecompletion_lock);\n \t\tposted = io_fill_cqe_aux(ctx, req-\u003ecqe.user_data, res, cflags);\n@@ -895,6 +899,8 @@ bool io_req_post_cqe32(struct io_kiocb *req, struct io_uring_cqe cqe[2])\n \tlockdep_assert_held(\u0026ctx-\u003euring_lock);\n \n \tcqe[0].user_data = req-\u003ecqe.user_data;\n+\tif (unlikely(current-\u003efd_slots.nr))\n+\t\t__fd_slots_commit(cqe[0].res);\n \tif (!(ctx-\u003eint_flags \u0026 IO_RING_F_LOCKLESS_CQ)) {\n \t\tspin_lock(\u0026ctx-\u003ecompletion_lock);\n \t\tposted = io_fill_cqe_aux32(ctx, cqe);\n@@ -1365,6 +1371,24 @@ static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def,\n \n #define REQ_ISSUE_SLOW_FLAGS\t(REQ_F_CREDS | REQ_F_ARM_LTIMEOUT)\n \n+/*\n+ * Requests complete from io_uring_enter(), task_work, io-wq workers and the\n+ * SQPOLL thread, and their CQEs are visible before any syscall returns. So\n+ * the descriptors a request reserved are committed per request, before its\n+ * completion is posted.\n+ */\n+static void io_req_fd_reservations(struct io_kiocb *req, int ret)\n+{\n+\tlong res = ret;\n+\n+\t/* A request holding reservations must not go async or be reissued. */\n+\tWARN_ON_ONCE(ret == IOU_ISSUE_SKIP_COMPLETE || ret == IOU_RETRY ||\n+\t\t ret == IOU_REQUEUE);\n+\tif (ret == IOU_COMPLETE)\n+\t\tres = req-\u003ecqe.res;\n+\t__fd_slots_commit(res);\n+}\n+\n static inline int __io_issue_sqe(struct io_kiocb *req,\n \t\t\t\t unsigned int issue_flags,\n \t\t\t\t const struct io_issue_def *def)\n@@ -1385,6 +1409,9 @@ static inline int __io_issue_sqe(struct io_kiocb *req,\n \n \tret = def-\u003eissue(req, issue_flags);\n \n+\tif (unlikely(current-\u003efd_slots.nr))\n+\t\tio_req_fd_reservations(req, ret);\n+\n \tif (!def-\u003eaudit_skip)\n \t\taudit_uring_exit(!ret, ret);\n \ndiff --git a/io_uring/mock_file.c b/io_uring/mock_file.c\nindex b318ed697998e..67164c28ed659 100644\n--- a/io_uring/mock_file.c\n+++ b/io_uring/mock_file.c\n@@ -257,8 +257,8 @@ static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flag\n \tFD_PREPARE(fdf, O_RDWR | O_CLOEXEC,\n \t\t anon_inode_create_getfile(\"[io_uring_mock]\", fops, mf,\n \t\t\t\t\t O_RDWR | O_CLOEXEC, NULL));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \n \tretain_and_null_ptr(mf);\n \tfile = fd_prepare_file(fdf);\n@@ -271,7 +271,6 @@ static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flag\n \tif (copy_to_user(uarg, \u0026mc, uarg_size))\n \t\treturn -EFAULT;\n \n-\tfd_publish(fdf);\n \treturn 0;\n }\n \ndiff --git a/io_uring/zcrx.c b/io_uring/zcrx.c\nindex 1b3b11405daca..d458c614c765f 100644\n--- a/io_uring/zcrx.c\n+++ b/io_uring/zcrx.c\n@@ -795,8 +795,6 @@ static int zcrx_export(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq,\n \t\t struct zcrx_ctrl *ctrl, void __user *arg)\n {\n \tstruct zcrx_ctrl_export *ce = \u0026ctrl-\u003ezc_export;\n-\tstruct file *file;\n-\tint fd;\n \n \tif (!mem_is_zero(ce, sizeof(*ce)))\n \t\treturn -EINVAL;\n@@ -804,27 +802,18 @@ static int zcrx_export(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq,\n \trefcount_inc(\u0026ifq-\u003erefs);\n \trefcount_inc(\u0026ifq-\u003euser_refs);\n \n-\tfile = anon_inode_create_getfile(\"[zcrx]\", \u0026zcrx_box_fops,\n-\t\t\t\t\t ifq, O_CLOEXEC, NULL);\n-\tif (IS_ERR(file)) {\n+\tFD_PREPARE(fdf, O_CLOEXEC,\n+\t\t anon_inode_create_getfile(\"[zcrx]\", \u0026zcrx_box_fops, ifq,\n+\t\t\t\t\t O_CLOEXEC, NULL));\n+\tif (IS_ERR(fdf)) {\n \t\tzcrx_unregister(ifq, NULL);\n-\t\treturn PTR_ERR(file);\n-\t}\n-\n-\tfd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (fd \u003c 0) {\n-\t\tfput(file);\n-\t\treturn fd;\n+\t\treturn PTR_ERR(fdf);\n \t}\n \n-\tce-\u003ezcrx_fd = fd;\n-\tif (copy_to_user(arg, ctrl, sizeof(*ctrl))) {\n-\t\tfput(file);\n-\t\tput_unused_fd(fd);\n+\tce-\u003ezcrx_fd = fd_prepare_fd(fdf);\n+\tif (copy_to_user(arg, ctrl, sizeof(*ctrl)))\n \t\treturn -EFAULT;\n-\t}\n \n-\tfd_install(fd, file);\n \treturn 0;\n }\n \ndiff --git a/kernel/bpf/bpf_iter.c b/kernel/bpf/bpf_iter.c\nindex 14a5fdfa04214..00edc394101bc 100644\n--- a/kernel/bpf/bpf_iter.c\n+++ b/kernel/bpf/bpf_iter.c\n@@ -643,15 +643,15 @@ int bpf_iter_new_fd(struct bpf_link *link)\n \tflags = O_RDONLY | O_CLOEXEC;\n \n \tFD_PREPARE(fdf, flags, anon_inode_getfile(\"bpf_iter\", \u0026bpf_iter_fops, NULL, flags));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \n \titer_link = container_of(link, struct bpf_iter_link, link);\n \terr = prepare_seq_file(fd_prepare_file(fdf), iter_link);\n \tif (err)\n \t\treturn err; /* Automatic cleanup handles fput */\n \n-\treturn fd_publish(fdf);\n+\treturn fd_prepare_fd(fdf);\n }\n \n struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop)\ndiff --git a/kernel/bpf/token.c b/kernel/bpf/token.c\nindex e85a179523f02..5cac7fc5694bd 100644\n--- a/kernel/bpf/token.c\n+++ b/kernel/bpf/token.c\n@@ -169,8 +169,8 @@ int bpf_token_create(union bpf_attr *attr)\n \tFD_PREPARE(fdf, O_CLOEXEC,\n \t\t alloc_file_pseudo(inode, path.mnt, BPF_TOKEN_INODE_NAME,\n \t\t\t\t O_RDWR, \u0026bpf_token_fops));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \n \ttoken = kzalloc_obj(*token, GFP_USER);\n \tif (!token)\n@@ -191,7 +191,7 @@ int bpf_token_create(union bpf_attr *attr)\n \n \tget_user_ns(token-\u003euserns);\n \tfd_prepare_file(fdf)-\u003eprivate_data = no_free_ptr(token);\n-\treturn fd_publish(fdf);\n+\treturn fd_prepare_fd(fdf);\n }\n \n int bpf_token_get_info_by_fd(struct bpf_token *token,\ndiff --git a/kernel/events/core.c b/kernel/events/core.c\nindex a6c8e38a31104..54834a7aca53d 100644\n--- a/kernel/events/core.c\n+++ b/kernel/events/core.c\n@@ -13924,7 +13924,7 @@ SYSCALL_DEFINE5(perf_event_open,\n \tstruct file *event_file = NULL;\n \tstruct task_struct *task = NULL;\n \tstruct pmu *pmu;\n-\tint event_fd;\n+\tconst struct fd_slot *event_fd;\n \tint move_group = 0;\n \tint err;\n \tint f_flags = O_RDWR;\n@@ -13990,9 +13990,9 @@ SYSCALL_DEFINE5(perf_event_open,\n \tif (flags \u0026 PERF_FLAG_FD_CLOEXEC)\n \t\tf_flags |= O_CLOEXEC;\n \n-\tevent_fd = get_unused_fd_flags(f_flags);\n-\tif (event_fd \u003c 0)\n-\t\treturn event_fd;\n+\tevent_fd = fd_prepare(f_flags);\n+\tif (IS_ERR(event_fd))\n+\t\treturn PTR_ERR(event_fd);\n \n \t/*\n \t * Event creation should be under SRCU, see perf_pmu_unregister().\n@@ -14001,15 +14001,11 @@ SYSCALL_DEFINE5(perf_event_open,\n \n \tCLASS(fd, group)(group_fd); // group_fd == -1 =\u003e empty\n \tif (group_fd != -1) {\n-\t\tif (!is_perf_file(group)) {\n-\t\t\terr = -EBADF;\n-\t\t\tgoto err_fd;\n-\t\t}\n+\t\tif (!is_perf_file(group))\n+\t\t\treturn -EBADF;\n \t\tgroup_leader = fd_file(group)-\u003eprivate_data;\n-\t\tif (group_leader-\u003estate \u003c= PERF_EVENT_STATE_EXIT) {\n-\t\t\terr = -ENODEV;\n-\t\t\tgoto err_fd;\n-\t\t}\n+\t\tif (group_leader-\u003estate \u003c= PERF_EVENT_STATE_EXIT)\n+\t\t\treturn -ENODEV;\n \t\tif (flags \u0026 PERF_FLAG_FD_OUTPUT)\n \t\t\toutput_event = group_leader;\n \t\tif (flags \u0026 PERF_FLAG_FD_NO_GROUP)\n@@ -14018,10 +14014,8 @@ SYSCALL_DEFINE5(perf_event_open,\n \n \tif (pid != -1 \u0026\u0026 !(flags \u0026 PERF_FLAG_PID_CGROUP)) {\n \t\ttask = find_lively_task_by_vpid(pid);\n-\t\tif (IS_ERR(task)) {\n-\t\t\terr = PTR_ERR(task);\n-\t\t\tgoto err_fd;\n-\t\t}\n+\t\tif (IS_ERR(task))\n+\t\t\treturn PTR_ERR(task);\n \t}\n \n \tif (task \u0026\u0026 group_leader \u0026\u0026\n@@ -14298,8 +14292,7 @@ SYSCALL_DEFINE5(perf_event_open,\n \t * This ensures destruction of the group leader will find\n \t * the pointer to itself in perf_group_detach().\n \t */\n-\tfd_install(event_fd, event_file);\n-\treturn event_fd;\n+\treturn fd_stage(event_fd, event_file);\n \n err_context:\n \tput_pmu_ctx(event-\u003epmu_ctx);\n@@ -14316,8 +14309,6 @@ SYSCALL_DEFINE5(perf_event_open,\n err_task:\n \tif (task)\n \t\tput_task_struct(task);\n-err_fd:\n-\tput_unused_fd(event_fd);\n \treturn err;\n }\n \ndiff --git a/kernel/exit.c b/kernel/exit.c\nindex 97686af895013..ee108353a62cd 100644\n--- a/kernel/exit.c\n+++ b/kernel/exit.c\n@@ -1000,6 +1000,7 @@ void __noreturn do_exit(long code)\n \n \texit_sem(tsk);\n \texit_shm(tsk);\n+\texit_fd_slots();\n \texit_files(tsk);\n \texit_fs(tsk);\n \tif (group_dead)\ndiff --git a/kernel/fork.c b/kernel/fork.c\nindex 416758c8a3d43..bd0e1e85abb0e 100644\n--- a/kernel/fork.c\n+++ b/kernel/fork.c\n@@ -980,6 +980,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)\n \ttsk-\u003ebtrace_seq = 0;\n #endif\n \ttsk-\u003esplice_pipe = NULL;\n+\tmemset(\u0026tsk-\u003efd_slots, 0, sizeof(tsk-\u003efd_slots));\n \ttsk-\u003etask_frag.page = NULL;\n \ttsk-\u003ewake_q.next = NULL;\n \ttsk-\u003eworker_private = NULL;\n@@ -3216,6 +3217,8 @@ static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp\n \n \tif ((unshare_flags \u0026 CLONE_FILES) \u0026\u0026\n \t (fd \u0026\u0026 atomic_read(\u0026fd-\u003ecount) \u003e 1)) {\n+\t\t/* Prepared descriptors live in the table we are about to leave. */\n+\t\tVFS_WARN_ON_ONCE(current-\u003efd_slots.nr);\n \t\tfd = dup_fd(fd, NULL);\n \t\tif (IS_ERR(fd))\n \t\t\treturn PTR_ERR(fd);\ndiff --git a/kernel/liveupdate/luo_core.c b/kernel/liveupdate/luo_core.c\nindex 1b2bda22902d2..3899bbf30b53f 100644\n--- a/kernel/liveupdate/luo_core.c\n+++ b/kernel/liveupdate/luo_core.c\n@@ -270,61 +270,41 @@ struct luo_device_state {\n static int luo_ioctl_create_session(struct luo_ucmd *ucmd)\n {\n \tstruct liveupdate_ioctl_create_session *argp = ucmd-\u003ecmd;\n+\tconst struct fd_slot *fd;\n \tstruct file *file;\n \tint err;\n \n-\targp-\u003efd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (argp-\u003efd \u003c 0)\n-\t\treturn argp-\u003efd;\n+\tfd = fd_prepare(O_CLOEXEC);\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n \terr = luo_session_create(argp-\u003ename, \u0026file);\n \tif (err)\n-\t\tgoto err_put_fd;\n-\n-\terr = luo_ucmd_respond(ucmd, sizeof(*argp));\n-\tif (err)\n-\t\tgoto err_put_file;\n-\n-\tfd_install(argp-\u003efd, file);\n-\n-\treturn 0;\n+\t\treturn err;\n \n-err_put_file:\n-\tfput(file);\n-err_put_fd:\n-\tput_unused_fd(argp-\u003efd);\n+\targp-\u003efd = fd_stage(fd, file);\n \n-\treturn err;\n+\treturn luo_ucmd_respond(ucmd, sizeof(*argp));\n }\n \n static int luo_ioctl_retrieve_session(struct luo_ucmd *ucmd)\n {\n \tstruct liveupdate_ioctl_retrieve_session *argp = ucmd-\u003ecmd;\n+\tconst struct fd_slot *fd;\n \tstruct file *file;\n \tint err;\n \n-\targp-\u003efd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (argp-\u003efd \u003c 0)\n-\t\treturn argp-\u003efd;\n+\tfd = fd_prepare(O_CLOEXEC);\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n \terr = luo_session_retrieve(argp-\u003ename, \u0026file);\n \tif (err \u003c 0)\n-\t\tgoto err_put_fd;\n-\n-\terr = luo_ucmd_respond(ucmd, sizeof(*argp));\n-\tif (err)\n-\t\tgoto err_put_file;\n-\n-\tfd_install(argp-\u003efd, file);\n-\n-\treturn 0;\n+\t\treturn err;\n \n-err_put_file:\n-\tfput(file);\n-err_put_fd:\n-\tput_unused_fd(argp-\u003efd);\n+\targp-\u003efd = fd_stage(fd, file);\n \n-\treturn err;\n+\treturn luo_ucmd_respond(ucmd, sizeof(*argp));\n }\n \n static int luo_open(struct inode *inodep, struct file *filep)\ndiff --git a/kernel/liveupdate/luo_session.c b/kernel/liveupdate/luo_session.c\nindex f48e9a4185f97..7de35bad1ede9 100644\n--- a/kernel/liveupdate/luo_session.c\n+++ b/kernel/liveupdate/luo_session.c\n@@ -282,33 +282,23 @@ static int luo_session_retrieve_fd(struct luo_session *session,\n \t\t\t\t struct luo_ucmd *ucmd)\n {\n \tstruct liveupdate_session_retrieve_fd *argp = ucmd-\u003ecmd;\n+\tconst struct fd_slot *fd;\n \tstruct file *file;\n \tint err;\n \n-\targp-\u003efd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (argp-\u003efd \u003c 0)\n-\t\treturn argp-\u003efd;\n+\tfd = fd_prepare(O_CLOEXEC);\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n \tmutex_lock(\u0026session-\u003emutex);\n \terr = luo_retrieve_file(\u0026session-\u003efile_set, argp-\u003etoken, \u0026file);\n \tmutex_unlock(\u0026session-\u003emutex);\n \tif (err \u003c 0)\n-\t\tgoto err_put_fd;\n-\n-\terr = luo_ucmd_respond(ucmd, sizeof(*argp));\n-\tif (err)\n-\t\tgoto err_put_file;\n-\n-\tfd_install(argp-\u003efd, file);\n-\n-\treturn 0;\n+\t\treturn err;\n \n-err_put_file:\n-\tfput(file);\n-err_put_fd:\n-\tput_unused_fd(argp-\u003efd);\n+\targp-\u003efd = fd_stage(fd, file);\n \n-\treturn err;\n+\treturn luo_ucmd_respond(ucmd, sizeof(*argp));\n }\n \n static int luo_session_finish(struct luo_session *session,\ndiff --git a/kernel/seccomp.c b/kernel/seccomp.c\nindex 86cf4460d69ea..dc275ab1e031e 100644\n--- a/kernel/seccomp.c\n+++ b/kernel/seccomp.c\n@@ -1957,7 +1957,7 @@ static long seccomp_set_mode_filter(unsigned int flags,\n \tconst unsigned long seccomp_mode = SECCOMP_MODE_FILTER;\n \tstruct seccomp_filter *prepared = NULL;\n \tlong ret = -EINVAL;\n-\tint listener = -1;\n+\tconst struct fd_slot *listener = NULL;\n \tstruct file *listener_f = NULL;\n \n \t/* Validate flags. */\n@@ -1990,15 +1990,14 @@ static long seccomp_set_mode_filter(unsigned int flags,\n \t\treturn PTR_ERR(prepared);\n \n \tif (flags \u0026 SECCOMP_FILTER_FLAG_NEW_LISTENER) {\n-\t\tlistener = get_unused_fd_flags(O_CLOEXEC);\n-\t\tif (listener \u003c 0) {\n-\t\t\tret = listener;\n+\t\tlistener = fd_prepare(O_CLOEXEC);\n+\t\tif (IS_ERR(listener)) {\n+\t\t\tret = PTR_ERR(listener);\n \t\t\tgoto out_free;\n \t\t}\n \n \t\tlistener_f = init_listener(prepared);\n \t\tif (IS_ERR(listener_f)) {\n-\t\t\tput_unused_fd(listener);\n \t\t\tret = PTR_ERR(listener_f);\n \t\t\tgoto out_free;\n \t\t}\n@@ -2038,11 +2037,9 @@ static long seccomp_set_mode_filter(unsigned int flags,\n \t\tif (ret) {\n \t\t\tlistener_f-\u003eprivate_data = NULL;\n \t\t\tfput(listener_f);\n-\t\t\tput_unused_fd(listener);\n \t\t\tseccomp_notify_detach(prepared);\n \t\t} else {\n-\t\t\tfd_install(listener, listener_f);\n-\t\t\tret = listener;\n+\t\t\tret = fd_stage(listener, listener_f);\n \t\t}\n \t}\n out_free:\ndiff --git a/mm/userfaultfd.c b/mm/userfaultfd.c\nindex 23fb68fce000e..b5131fc597e6b 100644\n--- a/mm/userfaultfd.c\n+++ b/mm/userfaultfd.c\n@@ -4809,14 +4809,14 @@ static int new_userfaultfd(int flags)\n \t\t anon_inode_create_getfile(\"[userfaultfd]\", \u0026userfaultfd_fops, ctx,\n \t\t\t\t\t O_RDONLY | (flags \u0026 UFFD_SHARED_FCNTL_FLAGS),\n \t\t\t\t\t NULL));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \n \t/* prevent the mm struct to be freed */\n \tmmgrab(ctx-\u003emm);\n \tfd_prepare_file(fdf)-\u003ef_mode |= FMODE_NOWAIT;\n \tretain_and_null_ptr(ctx);\n-\treturn fd_publish(fdf);\n+\treturn fd_prepare_fd(fdf);\n }\n \n static inline bool userfaultfd_syscall_allowed(int flags)\ndiff --git a/net/core/scm.c b/net/core/scm.c\nindex f0d44ecdb11fc..f05931eef54de 100644\n--- a/net/core/scm.c\n+++ b/net/core/scm.c\n@@ -355,6 +355,7 @@ int scm_recv_one_fd(struct file *f, int __user *ufd, unsigned int flags,\n \t\t bool notrunc)\n {\n \tint error;\n+\tconst struct fd_slot *fd;\n \n \tif (!ufd)\n \t\treturn -EFAULT;\n@@ -363,16 +364,16 @@ int scm_recv_one_fd(struct file *f, int __user *ufd, unsigned int flags,\n \tif (error)\n \t\treturn notrunc ? put_user(error, ufd) : error;\n \n-\tFD_PREPARE(fdf, flags, get_file(f));\n-\tif (fdf.err)\n-\t\treturn fdf.err;\n+\tfd = fd_prepare(flags);\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n-\terror = put_user(fd_prepare_fd(fdf), ufd);\n+\terror = put_user(fd_prepare_fd(fd), ufd);\n \tif (error)\n \t\treturn error;\n \n-\t__receive_sock(fd_prepare_file(fdf));\n-\treturn fd_publish(fdf);\n+\t__receive_sock(f);\n+\treturn fd_stage(fd, get_file(f));\n }\n \n void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm, bool notrunc)\ndiff --git a/net/handshake/netlink.c b/net/handshake/netlink.c\nindex 3fd4fef9bab1a..98d4266e700e9 100644\n--- a/net/handshake/netlink.c\n+++ b/net/handshake/netlink.c\n@@ -106,19 +106,23 @@ int handshake_nl_accept_doit(struct sk_buff *skb, struct genl_info *info)\n \terr = -EAGAIN;\n \treq = handshake_req_next(hn, class);\n \tif (req) {\n-\t\tFD_PREPARE(fdf, O_CLOEXEC, req-\u003ehr_file);\n-\t\tif (fdf.err) {\n+\t\t/* The ack carries the error, sendmsg() succeeds: stage last. */\n+\t\tconst struct fd_slot *fd = fd_prepare(O_CLOEXEC);\n+\n+\t\tif (IS_ERR(fd)) {\n \t\t\tfput(req-\u003ehr_file); /* drop ref from handshake_req_next() */\n-\t\t\terr = fdf.err;\n+\t\t\terr = PTR_ERR(fd);\n \t\t\tgoto out_complete;\n \t\t}\n \n-\t\terr = req-\u003ehr_proto-\u003ehp_accept(req, info, fd_prepare_fd(fdf));\n-\t\tif (err)\n-\t\t\tgoto out_complete; /* Automatic cleanup handles fput */\n+\t\terr = req-\u003ehr_proto-\u003ehp_accept(req, info, fd_prepare_fd(fd));\n+\t\tif (err) {\n+\t\t\tfput(req-\u003ehr_file); /* not staged, drop it by hand */\n+\t\t\tgoto out_complete;\n+\t\t}\n \n-\t\ttrace_handshake_cmd_accept(net, req, req-\u003ehr_sk, fd_prepare_fd(fdf));\n-\t\tfd_publish(fdf);\n+\t\tfd_stage(fd, req-\u003ehr_file);\n+\t\ttrace_handshake_cmd_accept(net, req, req-\u003ehr_sk, fd_prepare_fd(fd));\n \t\treturn 0;\n \t}\n \ndiff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c\nindex 71af69d442f21..2d744ee3d3404 100644\n--- a/net/kcm/kcmsock.c\n+++ b/net/kcm/kcmsock.c\n@@ -1580,14 +1580,13 @@ static int kcm_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)\n \t\tstruct kcm_clone info;\n \n \t\tFD_PREPARE(fdf, 0, kcm_clone(sock));\n-\t\tif (fdf.err)\n-\t\t\treturn fdf.err;\n+\t\tif (IS_ERR(fdf))\n+\t\t\treturn PTR_ERR(fdf);\n \n \t\tinfo.fd = fd_prepare_fd(fdf);\n \t\tif (copy_to_user((void __user *)arg, \u0026info, sizeof(info)))\n \t\t\treturn -EFAULT;\n \n-\t\tfd_publish(fdf);\n \t\terr = 0;\n \t\tbreak;\n \t}\ndiff --git a/net/sctp/socket.c b/net/sctp/socket.c\nindex c7b9e325ec1cd..b75c50af8c169 100644\n--- a/net/sctp/socket.c\n+++ b/net/sctp/socket.c\n@@ -5744,46 +5744,41 @@ static int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id,\n }\n \n static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,\n-\t\t\t\t\t struct file **newfile, unsigned flags)\n+\t\t\t\t\t unsigned flags)\n {\n \tstruct socket *newsock;\n+\tstruct file *newfile;\n+\tconst struct fd_slot *fd;\n \tint retval;\n \n \tretval = sctp_do_peeloff(sk, peeloff-\u003eassocid, \u0026newsock);\n \tif (retval \u003c 0)\n-\t\tgoto out;\n+\t\treturn retval;\n \n-\t/* Map the socket to an unused fd that can be returned to the user. */\n-\tretval = get_unused_fd_flags(flags \u0026 SOCK_CLOEXEC);\n-\tif (retval \u003c 0) {\n+\t/* Map the socket to a reserved fd that can be returned to the user. */\n+\tfd = fd_prepare(flags \u0026 SOCK_CLOEXEC);\n+\tif (IS_ERR(fd)) {\n \t\tsock_release(newsock);\n-\t\tgoto out;\n-\t}\n-\n-\t*newfile = sock_alloc_file(newsock, 0, NULL);\n-\tif (IS_ERR(*newfile)) {\n-\t\tput_unused_fd(retval);\n-\t\tretval = PTR_ERR(*newfile);\n-\t\t*newfile = NULL;\n-\t\treturn retval;\n+\t\treturn PTR_ERR(fd);\n \t}\n \n-\tpr_debug(\"%s: sk:%p, newsk:%p, sd:%d\\n\", __func__, sk, newsock-\u003esk,\n-\t\t retval);\n-\n-\tpeeloff-\u003esd = retval;\n+\tnewfile = sock_alloc_file(newsock, 0, NULL);\n+\tif (IS_ERR(newfile))\n+\t\treturn PTR_ERR(newfile);\n \n \tif (flags \u0026 SOCK_NONBLOCK)\n-\t\t(*newfile)-\u003ef_flags |= O_NONBLOCK;\n-out:\n-\treturn retval;\n+\t\tnewfile-\u003ef_flags |= O_NONBLOCK;\n+\n+\tpeeloff-\u003esd = fd_stage(fd, newfile);\n+\tpr_debug(\"%s: sk:%p, newsk:%p, sd:%d\\n\", __func__, sk, newsock-\u003esk,\n+\t\t peeloff-\u003esd);\n+\treturn peeloff-\u003esd;\n }\n \n static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)\n {\n \tsctp_peeloff_arg_t peeloff;\n-\tstruct file *newfile = NULL;\n-\tint retval = 0;\n+\tint retval;\n \n \tif (len \u003c sizeof(sctp_peeloff_arg_t))\n \t\treturn -EINVAL;\n@@ -5791,24 +5786,17 @@ static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval\n \tif (copy_from_user(\u0026peeloff, optval, len))\n \t\treturn -EFAULT;\n \n-\tretval = sctp_getsockopt_peeloff_common(sk, \u0026peeloff, \u0026newfile, 0);\n+\tretval = sctp_getsockopt_peeloff_common(sk, \u0026peeloff, 0);\n \tif (retval \u003c 0)\n-\t\tgoto out;\n+\t\treturn retval;\n \n \t/* Return the fd mapped to the new socket. */\n-\tif (put_user(len, optlen)) {\n-\t\tfput(newfile);\n-\t\tput_unused_fd(retval);\n+\tif (put_user(len, optlen))\n \t\treturn -EFAULT;\n-\t}\n \n-\tif (copy_to_user(optval, \u0026peeloff, len)) {\n-\t\tfput(newfile);\n-\t\tput_unused_fd(retval);\n+\tif (copy_to_user(optval, \u0026peeloff, len))\n \t\treturn -EFAULT;\n-\t}\n-\tfd_install(retval, newfile);\n-out:\n+\n \treturn retval;\n }\n \n@@ -5816,8 +5804,7 @@ static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,\n \t\t\t\t\t char __user *optval, int __user *optlen)\n {\n \tsctp_peeloff_flags_arg_t peeloff;\n-\tstruct file *newfile = NULL;\n-\tint retval = 0;\n+\tint retval;\n \n \tif (len \u003c sizeof(sctp_peeloff_flags_arg_t))\n \t\treturn -EINVAL;\n@@ -5826,24 +5813,17 @@ static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,\n \t\treturn -EFAULT;\n \n \tretval = sctp_getsockopt_peeloff_common(sk, \u0026peeloff.p_arg,\n-\t\t\t\t\t\t\u0026newfile, peeloff.flags);\n+\t\t\t\t\t\tpeeloff.flags);\n \tif (retval \u003c 0)\n-\t\tgoto out;\n+\t\treturn retval;\n \n \t/* Return the fd mapped to the new socket. */\n-\tif (put_user(len, optlen)) {\n-\t\tfput(newfile);\n-\t\tput_unused_fd(retval);\n+\tif (put_user(len, optlen))\n \t\treturn -EFAULT;\n-\t}\n \n-\tif (copy_to_user(optval, \u0026peeloff, len)) {\n-\t\tfput(newfile);\n-\t\tput_unused_fd(retval);\n+\tif (copy_to_user(optval, \u0026peeloff, len))\n \t\treturn -EFAULT;\n-\t}\n-\tfd_install(retval, newfile);\n-out:\n+\n \treturn retval;\n }\n \ndiff --git a/sound/core/compress_offload.c b/sound/core/compress_offload.c\nindex 23d62fede06e8..94bc226f8db94 100644\n--- a/sound/core/compress_offload.c\n+++ b/sound/core/compress_offload.c\n@@ -1065,7 +1065,8 @@ static u64 snd_compr_seqno_next(struct snd_compr_stream *stream)\n static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_task *utask)\n {\n \tstruct snd_compr_task_runtime *task;\n-\tint retval, fd_i, fd_o;\n+\tint retval;\n+\tconst struct fd_slot *fd_i, *fd_o;\n \n \tif (stream-\u003eruntime-\u003etotal_tasks \u003e= stream-\u003eruntime-\u003efragments)\n \t\treturn -EBUSY;\n@@ -1080,29 +1081,26 @@ static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_\n \tif (retval \u003c 0)\n \t\tgoto cleanup;\n \t/* similar functionality as in dma_buf_fd(), but ensure that both\n-\t file descriptors are allocated before fd_install() */\n+\t file descriptors are reserved before either is staged */\n \tif (!task-\u003einput || !task-\u003einput-\u003efile || !task-\u003eoutput || !task-\u003eoutput-\u003efile) {\n \t\tretval = -EINVAL;\n \t\tgoto free_driver_task;\n \t}\n-\tfd_i = get_unused_fd_flags(O_WRONLY|O_CLOEXEC);\n-\tif (fd_i \u003c 0) {\n-\t\tretval = fd_i;\n+\tfd_i = fd_prepare(O_WRONLY|O_CLOEXEC);\n+\tif (IS_ERR(fd_i)) {\n+\t\tretval = PTR_ERR(fd_i);\n \t\tgoto free_driver_task;\n \t}\n-\tfd_o = get_unused_fd_flags(O_RDONLY|O_CLOEXEC);\n-\tif (fd_o \u003c 0) {\n-\t\tretval = fd_o;\n-\t\tput_unused_fd(fd_i);\n+\tfd_o = fd_prepare(O_RDONLY|O_CLOEXEC);\n+\tif (IS_ERR(fd_o)) {\n+\t\tretval = PTR_ERR(fd_o);\n \t\tgoto free_driver_task;\n \t}\n \t/* keep dmabuf reference until freed with task free ioctl */\n \tget_dma_buf(task-\u003einput);\n \tget_dma_buf(task-\u003eoutput);\n-\tfd_install(fd_i, task-\u003einput-\u003efile);\n-\tfd_install(fd_o, task-\u003eoutput-\u003efile);\n-\tutask-\u003einput_fd = fd_i;\n-\tutask-\u003eoutput_fd = fd_o;\n+\tutask-\u003einput_fd = fd_stage(fd_i, task-\u003einput-\u003efile);\n+\tutask-\u003eoutput_fd = fd_stage(fd_o, task-\u003eoutput-\u003efile);\n \tlist_add_tail(\u0026task-\u003elist, \u0026stream-\u003eruntime-\u003etasks);\n \tstream-\u003eruntime-\u003etotal_tasks++;\n \treturn 0;\ndiff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c\nindex 625e62e1a0318..e0d1cb869342e 100644\n--- a/virt/kvm/guest_memfd.c\n+++ b/virt/kvm/guest_memfd.c\n@@ -526,17 +526,16 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)\n \tstruct gmem_file *f;\n \tstruct inode *inode;\n \tstruct file *file;\n-\tint fd, err;\n+\tconst struct fd_slot *fd;\n+\tint err;\n \n-\tfd = get_unused_fd_flags(0);\n-\tif (fd \u003c 0)\n-\t\treturn fd;\n+\tfd = fd_prepare(0);\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n \tf = kzalloc_obj(*f);\n-\tif (!f) {\n-\t\terr = -ENOMEM;\n-\t\tgoto err_fd;\n-\t}\n+\tif (!f)\n+\t\treturn -ENOMEM;\n \n \t/* __fput() will take care of fops_put(). */\n \tif (!fops_get(\u0026kvm_gmem_fops)) {\n@@ -575,8 +574,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)\n \txa_init(\u0026f-\u003ebindings);\n \tlist_add(\u0026f-\u003eentry, \u0026GMEM_I(inode)-\u003egmem_file_list);\n \n-\tfd_install(fd, file);\n-\treturn fd;\n+\treturn fd_stage(fd, file);\n \n err_inode:\n \tiput(inode);\n@@ -584,8 +582,6 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)\n \tfops_put(\u0026kvm_gmem_fops);\n err_gmem:\n \tkfree(f);\n-err_fd:\n-\tput_unused_fd(fd);\n \treturn err;\n }\n \ndiff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c\nindex 65eb26a0520d8..8d3d47a6c98aa 100644\n--- a/virt/kvm/kvm_main.c\n+++ b/virt/kvm/kvm_main.c\n@@ -4326,27 +4326,18 @@ static const struct file_operations kvm_vcpu_stats_fops = {\n \n static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu)\n {\n-\tint fd;\n-\tstruct file *file;\n \tchar name[15 + ITOA_MAX_LEN + 1];\n \n \tsnprintf(name, sizeof(name), \"kvm-vcpu-stats:%d\", vcpu-\u003evcpu_id);\n \n-\tfd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (fd \u003c 0)\n-\t\treturn fd;\n-\n-\tfile = anon_inode_getfile_fmode(name, \u0026kvm_vcpu_stats_fops, vcpu,\n-\t\t\t\t\tO_RDONLY, FMODE_PREAD);\n-\tif (IS_ERR(file)) {\n-\t\tput_unused_fd(fd);\n-\t\treturn PTR_ERR(file);\n-\t}\n+\tFD_PREPARE(fdf, O_CLOEXEC,\n+\t\t anon_inode_getfile_fmode(name, \u0026kvm_vcpu_stats_fops, vcpu,\n+\t\t\t\t\t O_RDONLY, FMODE_PREAD));\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \n \tkvm_get_kvm(vcpu-\u003ekvm);\n-\tfd_install(fd, file);\n-\n-\treturn fd;\n+\treturn fd_prepare_fd(fdf);\n }\n \n #ifdef CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY\n@@ -5136,24 +5127,14 @@ static const struct file_operations kvm_vm_stats_fops = {\n \n static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm)\n {\n-\tint fd;\n-\tstruct file *file;\n-\n-\tfd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (fd \u003c 0)\n-\t\treturn fd;\n-\n-\tfile = anon_inode_getfile_fmode(\"kvm-vm-stats\",\n-\t\t\t\u0026kvm_vm_stats_fops, kvm, O_RDONLY, FMODE_PREAD);\n-\tif (IS_ERR(file)) {\n-\t\tput_unused_fd(fd);\n-\t\treturn PTR_ERR(file);\n-\t}\n+\tFD_PREPARE(fdf, O_CLOEXEC,\n+\t\t anon_inode_getfile_fmode(\"kvm-vm-stats\", \u0026kvm_vm_stats_fops,\n+\t\t\t\t\t kvm, O_RDONLY, FMODE_PREAD));\n+\tif (IS_ERR(fdf))\n+\t\treturn PTR_ERR(fdf);\n \n \tkvm_get_kvm(kvm);\n-\tfd_install(fd, file);\n-\n-\treturn fd;\n+\treturn fd_prepare_fd(fdf);\n }\n \n #define SANITY_CHECK_MEM_REGION_FIELD(field)\t\t\t\t\t\\\n@@ -5499,21 +5480,20 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(file_is_kvm);\n static int kvm_dev_ioctl_create_vm(unsigned long type)\n {\n \tchar fdname[ITOA_MAX_LEN + 1];\n-\tint r, fd;\n \tstruct kvm *kvm;\n \tstruct file *file;\n+\tconst struct fd_slot *fd;\n+\tint r;\n \n-\tfd = get_unused_fd_flags(O_CLOEXEC);\n-\tif (fd \u003c 0)\n-\t\treturn fd;\n+\tfd = fd_prepare(O_CLOEXEC);\n+\tif (IS_ERR(fd))\n+\t\treturn PTR_ERR(fd);\n \n-\tsnprintf(fdname, sizeof(fdname), \"%d\", fd);\n+\tsnprintf(fdname, sizeof(fdname), \"%d\", fd_prepare_fd(fd));\n \n \tkvm = kvm_create_vm(type, fdname);\n-\tif (IS_ERR(kvm)) {\n-\t\tr = PTR_ERR(kvm);\n-\t\tgoto put_fd;\n-\t}\n+\tif (IS_ERR(kvm))\n+\t\treturn PTR_ERR(kvm);\n \n \tfile = anon_inode_getfile(\"kvm-vm\", \u0026kvm_vm_fops, kvm, O_RDWR);\n \tif (IS_ERR(file)) {\n@@ -5529,13 +5509,10 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)\n \t */\n \tkvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);\n \n-\tfd_install(fd, file);\n-\treturn fd;\n+\treturn fd_stage(fd, file);\n \n put_kvm:\n \tkvm_put_kvm(kvm);\n-put_fd:\n-\tput_unused_fd(fd);\n \treturn r;\n }\n \n"
} |
| 2/1 | 2026/09/15 14:31 | agent | patch-evaluator |
8mModel:core Args: null Results: {
"EnableConfigs": [
"IO_URING",
"USERFAULTFD",
"SECCOMP"
],
"FocusSymbols": [
"fd_prepare",
"fd_stage",
"fd_slots_commit",
"__fd_slots_commit",
"exit_fd_slots",
"io_req_post_cqe",
"scm_recv_one_fd"
],
"Reasoning": "The patch series introduces a core VFS mechanism (fd_slots) to defer file descriptor installation until syscall exit, committing staged files on success and releasing/dropping them on error. It modifies core task lifecycle code (task_struct, fork, exec, exit), architecture syscall exit handling (SYSCALL_WORK_FD_SLOTS in generic entry common), io_uring completion handling, and various syscall/ioctl entrypoints (perf_event_open, userfaultfd, seccomp, eventfd, epoll, scm, etc.). It also introduces multiple new assertions (VFS_WARN_ON_ONCE and WARN_ON_ONCE) in reachability paths including exec, unshare, task exit, and io_uring. Fuzzing is warranted to discover potential descriptor leaks, premature commits/drops, or triggered assertions during complex error unwind or asynchronous completion paths.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit a40598dcddd1056cb33bf7ce7eca154940bc9aaf
Author: syz-cluster <triage@syzkaller.com>
Date: Tue Sep 15 14:31:17 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/alpha/include/asm/thread_info.h b/arch/alpha/include/asm/thread_info.h
index 1552ecca85207..db8465f5fabb2 100644
--- a/arch/alpha/include/asm/thread_info.h
+++ b/arch/alpha/include/asm/thread_info.h
@@ -67,6 +67,7 @@ register unsigned long *current_stack_pointer __asm__ ("$30");
#define TIF_NOTIFY_SIGNAL 5 /* signal notifications exist */
#define TIF_SECCOMP 6 /* seccomp syscall filtering active */
#define TIF_SYSCALL_TRACEPOINT 7 /* syscall tracepoint instrumentation */
+#define TIF_FD_SLOTS 8 /* syscall prepared descriptors */
#define TIF_DIE_IF_KERNEL 9 /* dik recursion lock */
#define TIF_MEMDIE 13 /* is terminating due to OOM killer */
#define TIF_POLLING_NRFLAG 14 /* idle is polling for TIF_NEED_RESCHED */
@@ -80,6 +81,7 @@ register unsigned long *current_stack_pointer __asm__ ("$30");
#define _TIF_SECCOMP (1<<TIF_SECCOMP)
#define _TIF_POLLING_NRFLAG (1<<TIF_POLLING_NRFLAG)
#define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
/*
* Work to do on syscall entry (in entry.S).
diff --git a/arch/alpha/kernel/entry.S b/arch/alpha/kernel/entry.S
index 9f2608de25446..20372e9263591 100644
--- a/arch/alpha/kernel/entry.S
+++ b/arch/alpha/kernel/entry.S
@@ -569,6 +569,13 @@ entSys:
$ret_success:
stq $0, 0($sp)
stq $31, 72($sp) /* a3=0 => no error */
+$syscall_exit:
+ /* Descriptors reserved by the syscall are committed in
+ syscall_trace_leave, off the return path interrupts share. */
+ ldl $17, TI_FLAGS($8)
+ lda $2, _TIF_FD_SLOTS
+ and $17, $2, $2
+ bne $2, $fd_slots_exit
.align 4
.globl ret_from_sys_call
@@ -612,7 +619,7 @@ $syscall_error:
stq $0, 0($sp)
mov $31, $26 /* tell "ret_from_sys_call" we can restart */
stq $1, 72($sp) /* a3 for return */
- br ret_from_sys_call
+ br $syscall_exit
/*
* Do all cleanup when returning from all interrupts and system calls.
@@ -648,6 +655,21 @@ $work_notifysig:
UNDO_SWITCH_STACK
br restore_all
+ .align 4
+$fd_slots_exit:
+ /* Keep the return address and the syscall restart state in
+ $26/$18/$19 across the call. */
+ DO_SWITCH_STACK
+ mov $26, $9
+ mov $18, $10
+ mov $19, $11
+ jsr $26, syscall_trace_leave
+ mov $9, $26
+ mov $10, $18
+ mov $11, $19
+ UNDO_SWITCH_STACK
+ br ret_from_sys_call
+
/*
* PTRACE syscall handler
*/
diff --git a/arch/alpha/kernel/ptrace.c b/arch/alpha/kernel/ptrace.c
index fc8f6cedbb286..736218b34601e 100644
--- a/arch/alpha/kernel/ptrace.c
+++ b/arch/alpha/kernel/ptrace.c
@@ -6,6 +6,7 @@
/* more mutilation by David Mosberger (davidm@azstarnet.com) */
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <linux/mm.h>
@@ -507,6 +508,8 @@ asmlinkage unsigned long syscall_trace_enter(void)
asmlinkage void
syscall_trace_leave(void)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(current_pt_regs());
audit_syscall_exit(current_pt_regs());
if (test_thread_flag(TIF_SYSCALL_TRACE))
ptrace_report_syscall_exit(current_pt_regs(), 0);
diff --git a/arch/arc/include/asm/thread_info.h b/arch/arc/include/asm/thread_info.h
index 255d2c7742190..4c67d4eb434b9 100644
--- a/arch/arc/include/asm/thread_info.h
+++ b/arch/arc/include/asm/thread_info.h
@@ -77,6 +77,7 @@ static inline __attribute_const__ struct thread_info *current_thread_info(void)
#define TIF_NEED_RESCHED 3 /* rescheduling necessary */
#define TIF_SYSCALL_AUDIT 4 /* syscall auditing active */
#define TIF_NOTIFY_SIGNAL 5 /* signal notifications exist */
+#define TIF_FD_SLOTS 6 /* syscall prepared descriptors */
#define TIF_SYSCALL_TRACE 15 /* syscall trace active */
/* true if poll_idle() is polling TIF_NEED_RESCHED */
#define TIF_MEMDIE 16
@@ -88,6 +89,7 @@ static inline __attribute_const__ struct thread_info *current_thread_info(void)
#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED)
#define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT)
#define _TIF_NOTIFY_SIGNAL (1<<TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_MEMDIE (1<<TIF_MEMDIE)
#define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
diff --git a/arch/arc/kernel/entry.S b/arch/arc/kernel/entry.S
index 3c7e74aba6794..f436391526e16 100644
--- a/arch/arc/kernel/entry.S
+++ b/arch/arc/kernel/entry.S
@@ -241,6 +241,11 @@ ENTRY(EV_Trap)
jl [r9]
.Lret_from_system_call:
+ ; descriptors reserved by the syscall are committed in syscall_trace_exit()
+ GET_CURR_THR_INFO_FLAGS r10
+ and.f 0, r10, _TIF_FD_SLOTS
+ bnz tracesys_exit
+
st r0, [sp, PT_r0] ; sys call return value in pt_regs
; fall through to ret_from_exception
diff --git a/arch/arc/kernel/ptrace.c b/arch/arc/kernel/ptrace.c
index fe2b6572e9199..0a0d74b9c7c43 100644
--- a/arch/arc/kernel/ptrace.c
+++ b/arch/arc/kernel/ptrace.c
@@ -3,6 +3,7 @@
* Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
*/
+#include <linux/file.h>
#include <linux/ptrace.h>
#include <linux/sched/task_stack.h>
#include <linux/regset.h>
@@ -355,6 +356,9 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs)
asmlinkage void syscall_trace_exit(struct pt_regs *regs)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
if (test_thread_flag(TIF_SYSCALL_TRACE))
ptrace_report_syscall_exit(regs, 0);
diff --git a/arch/arm/include/asm/thread_info.h b/arch/arm/include/asm/thread_info.h
index 943ffcf069d29..5337449e8409d 100644
--- a/arch/arm/include/asm/thread_info.h
+++ b/arch/arm/include/asm/thread_info.h
@@ -144,6 +144,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp *,
#define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
#define TIF_UPROBE 3 /* breakpointed or singlestepping */
#define TIF_NOTIFY_SIGNAL 4 /* signal notifications exist */
+#define TIF_FD_SLOTS 5 /* syscall prepared descriptors, entry-common.S needs it below 16 */
#define TIF_USING_IWMMXT 17
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
@@ -163,6 +164,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp *,
#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_USING_IWMMXT (1 << TIF_USING_IWMMXT)
/* Checks for any syscall work in entry-common.S */
diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S
index 88336a1292bb7..59a70d90b0b61 100644
--- a/arch/arm/kernel/entry-common.S
+++ b/arch/arm/kernel/entry-common.S
@@ -83,6 +83,8 @@ ENDPROC(ret_fast_syscall)
tst r1, #_TIF_SYSCALL_WORK
bne __sys_trace_return_nosave
+ tst r1, #(_TIF_FD_SLOTS << 16) @ r1 holds TI_FLAGS << 16
+ bne __sys_trace_return_nosave
slow_work_pending:
mov r0, sp @ 'regs'
mov r2, why @ 'syscall'
diff --git a/arch/arm/kernel/ptrace.c b/arch/arm/kernel/ptrace.c
index ed7a2a87a6707..75fe26012fcbc 100644
--- a/arch/arm/kernel/ptrace.c
+++ b/arch/arm/kernel/ptrace.c
@@ -11,6 +11,7 @@
#include <linux/sched/task_stack.h>
#include <linux/mm.h>
#include <linux/elf.h>
+#include <linux/file.h>
#include <linux/smp.h>
#include <linux/ptrace.h>
#include <linux/user.h>
@@ -876,6 +877,9 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs)
asmlinkage void syscall_trace_exit(struct pt_regs *regs)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
/*
* Audit the syscall before anything else, as a debugger may
* come in and change the current registers.
diff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/thread_info.h
index 5d7fe3e153c85..e48006eb98a85 100644
--- a/arch/arm64/include/asm/thread_info.h
+++ b/arch/arm64/include/asm/thread_info.h
@@ -74,6 +74,7 @@ void arch_setup_new_exec(void);
#define TIF_SECCOMP 11 /* syscall secure computing */
#define TIF_SYSCALL_EMU 12 /* syscall emulation active */
#define TIF_PATCH_PENDING 13 /* pending live patching update */
+#define TIF_FD_SLOTS 14 /* syscall prepared descriptors */
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
#define TIF_FREEZE 19
#define TIF_RESTORE_SIGMASK 20
@@ -107,10 +108,11 @@ void arch_setup_new_exec(void);
#define _TIF_MTE_ASYNC_FAULT (1 << TIF_MTE_ASYNC_FAULT)
#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)
#define _TIF_TSC_SIGSEGV (1 << TIF_TSC_SIGSEGV)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
_TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \
- _TIF_SYSCALL_EMU)
+ _TIF_SYSCALL_EMU | _TIF_FD_SLOTS)
#ifdef CONFIG_SHADOW_CALL_STACK
#define INIT_SCS \
diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c
index f743cbec1c3ad..3bedfed977d47 100644
--- a/arch/arm64/kernel/ptrace.c
+++ b/arch/arm64/kernel/ptrace.c
@@ -28,6 +28,7 @@
#include <linux/hw_breakpoint.h>
#include <linux/regset.h>
#include <linux/elf.h>
+#include <linux/file.h>
#include <linux/rseq.h>
#include <asm/compat.h>
@@ -2486,6 +2487,9 @@ void syscall_trace_exit(struct pt_regs *regs)
{
unsigned long flags = read_thread_flags();
+ if (flags & _TIF_FD_SLOTS)
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (flags & _TIF_SYSCALL_TRACEPOINT)
diff --git a/arch/csky/include/asm/thread_info.h b/arch/csky/include/asm/thread_info.h
index fdd4f8ad45acf..59109ce646951 100644
--- a/arch/csky/include/asm/thread_info.h
+++ b/arch/csky/include/asm/thread_info.h
@@ -61,6 +61,7 @@ static inline struct thread_info *current_thread_info(void)
#define TIF_SYSCALL_TRACEPOINT 5 /* syscall tracepoint instrumentation */
#define TIF_SYSCALL_AUDIT 6 /* syscall auditing */
#define TIF_NOTIFY_SIGNAL 7 /* signal notifications exist */
+#define TIF_FD_SLOTS 8 /* syscall prepared descriptors */
#define TIF_POLLING_NRFLAG 16 /* poll_idle() is TIF_NEED_RESCHED */
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
#define TIF_RESTORE_SIGMASK 20 /* restore signal mask in do_signal() */
@@ -73,6 +74,7 @@ static inline struct thread_info *current_thread_info(void)
#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)
#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_UPROBE (1 << TIF_UPROBE)
#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
#define _TIF_MEMDIE (1 << TIF_MEMDIE)
diff --git a/arch/csky/kernel/entry.S b/arch/csky/kernel/entry.S
index 3261f46f22442..6ff3074b9787b 100644
--- a/arch/csky/kernel/entry.S
+++ b/arch/csky/kernel/entry.S
@@ -80,6 +80,15 @@ ENTRY(csky_systemcall)
jsr syscallid
#endif
stw a0, (sp, LSAVE_A0) /* Save return value */
+ /* descriptors reserved by the syscall are committed in syscall_trace_exit() */
+ mov r9, sp
+ bmaski r10, THREAD_SHIFT
+ andn r9, r10
+ ldw r10, (r9, TINFO_FLAGS)
+ lrw r9, _TIF_FD_SLOTS
+ and r10, r9
+ cmpnei r10, 0
+ bt 1f
jmpi ret_from_exception
csky_syscall_trace:
diff --git a/arch/csky/kernel/ptrace.c b/arch/csky/kernel/ptrace.c
index ee2867a1576e9..b19fef9d396dc 100644
--- a/arch/csky/kernel/ptrace.c
+++ b/arch/csky/kernel/ptrace.c
@@ -4,6 +4,7 @@
#include <linux/audit.h>
#include <linux/elf.h>
#include <linux/errno.h>
+#include <linux/file.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/ptrace.h>
@@ -335,6 +336,9 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs)
asmlinkage void syscall_trace_exit(struct pt_regs *regs)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (test_thread_flag(TIF_SYSCALL_TRACE))
diff --git a/arch/hexagon/include/asm/thread_info.h b/arch/hexagon/include/asm/thread_info.h
index e90f280b9ce3e..8558d2d147f87 100644
--- a/arch/hexagon/include/asm/thread_info.h
+++ b/arch/hexagon/include/asm/thread_info.h
@@ -88,6 +88,7 @@ register struct thread_info *__current_thread_info asm(QUOTED_THREADINFO_REG);
#define TIF_SIGPENDING 2 /* signal pending */
#define TIF_NEED_RESCHED 3 /* rescheduling necessary */
#define TIF_SINGLESTEP 4 /* restore ss @ return to usr mode */
+#define TIF_FD_SLOTS 5 /* syscall prepared descriptors */
#define TIF_RESTORE_SIGMASK 6 /* restore sig mask in do_signal() */
#define TIF_NOTIFY_SIGNAL 7 /* signal notifications exist */
/* true if poll_idle() is polling TIF_NEED_RESCHED */
@@ -98,6 +99,7 @@ register struct thread_info *__current_thread_info asm(QUOTED_THREADINFO_REG);
#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
#define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
#define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)
/* work to do on interrupt/exception return - All but TIF_SYSCALL_TRACE */
diff --git a/arch/hexagon/kernel/traps.c b/arch/hexagon/kernel/traps.c
index 6fe0c1b62c1b3..4d15c5212fd78 100644
--- a/arch/hexagon/kernel/traps.c
+++ b/arch/hexagon/kernel/traps.c
@@ -6,6 +6,7 @@
*/
#include <linux/init.h>
+#include <linux/file.h>
#include <linux/sched/signal.h>
#include <linux/sched/debug.h>
#include <linux/sched/task_stack.h>
@@ -381,6 +382,9 @@ void do_trap0(struct pt_regs *regs)
regs->r04, regs->r05);
}
+ if (unlikely(test_thread_flag(TIF_FD_SLOTS)))
+ fd_slots_commit(regs);
+
/* allow strace to get the syscall return state */
if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE)))
ptrace_report_syscall_exit(regs, 0);
diff --git a/arch/m68k/68000/entry.S b/arch/m68k/68000/entry.S
index c257cc415c478..479032af2f2f0 100644
--- a/arch/m68k/68000/entry.S
+++ b/arch/m68k/68000/entry.S
@@ -89,6 +89,16 @@ ENTRY(system_call)
jbsr %a0@
movel %d0,%sp@(PT_OFF_D0) /* save the return value*/
+ /* descriptors reserved by the syscall are committed in syscall_trace_leave */
+ getthreadinfo
+ btst #(TIF_FD_SLOTS%8),%a2@(TINFO_FLAGS+(31-TIF_FD_SLOTS)/8)
+ jeq ret_from_exception
+ subql #4,%sp /* dummy return address */
+ SAVE_SWITCH_STACK
+ jbsr syscall_trace_leave
+ RESTORE_SWITCH_STACK
+ addql #4,%sp
+
ret_from_exception:
btst #5,%sp@(PT_OFF_SR) /* check if returning to kernel*/
jeq Luser_return /* if so, skip resched, signals*/
diff --git a/arch/m68k/coldfire/entry.S b/arch/m68k/coldfire/entry.S
index 4ea08336e2fb0..43e47d4c00187 100644
--- a/arch/m68k/coldfire/entry.S
+++ b/arch/m68k/coldfire/entry.S
@@ -78,6 +78,18 @@ ENTRY(system_call)
movel %d3,%a0
jbsr %a0@
movel %d0,%sp@(PT_OFF_D0) /* save the return value */
+
+ /* descriptors reserved by the syscall are committed in syscall_trace_leave */
+ movel %sp,%d2 /* get thread_info pointer */
+ andl #-THREAD_SIZE,%d2 /* at start of kernel stack */
+ movel %d2,%a0
+ btst #(TIF_FD_SLOTS%8),%a0@(TINFO_FLAGS+(31-TIF_FD_SLOTS)/8)
+ jeq ret_from_exception
+ subql #4,%sp /* dummy return address */
+ SAVE_SWITCH_STACK
+ jbsr syscall_trace_leave
+ RESTORE_SWITCH_STACK
+ addql #4,%sp
jra ret_from_exception
1:
movel #-ENOSYS,%d2 /* strace needs -ENOSYS in PT_OFF_D0 */
diff --git a/arch/m68k/include/asm/thread_info.h b/arch/m68k/include/asm/thread_info.h
index 5cb3ace556222..861b6f523bda4 100644
--- a/arch/m68k/include/asm/thread_info.h
+++ b/arch/m68k/include/asm/thread_info.h
@@ -62,6 +62,7 @@ static inline struct thread_info *current_thread_info(void)
#define TIF_NOTIFY_RESUME 5 /* callback before returning to user */
#define TIF_SIGPENDING 6 /* signal pending */
#define TIF_NEED_RESCHED 7 /* rescheduling necessary */
+#define TIF_FD_SLOTS 12 /* syscall prepared descriptors */
#define TIF_SECCOMP 13 /* seccomp syscall filtering active */
#define TIF_DELAYED_TRACE 14 /* single step a syscall */
#define TIF_SYSCALL_TRACE 15 /* syscall trace active */
@@ -71,6 +72,7 @@ static inline struct thread_info *current_thread_info(void)
#define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
#define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_DELAYED_TRACE (1 << TIF_DELAYED_TRACE)
#define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
diff --git a/arch/m68k/kernel/entry.S b/arch/m68k/kernel/entry.S
index 338b474910f74..9e4471f681652 100644
--- a/arch/m68k/kernel/entry.S
+++ b/arch/m68k/kernel/entry.S
@@ -229,6 +229,8 @@ ret_from_syscall:
syscall_exit_work:
btst #5,%sp@(PT_OFF_SR) | check if returning to kernel
bnes 1b | if so, skip resched, signals
+ btst #4,%a1@(TINFO_FLAGS+2) | TIF_FD_SLOTS: commit descriptors
+ jne do_trace_exit
lslw #1,%d0
jcs do_trace_exit
jmi do_delayed_trace
diff --git a/arch/m68k/kernel/ptrace.c b/arch/m68k/kernel/ptrace.c
index 4575f6487a9aa..07103e6ddeb04 100644
--- a/arch/m68k/kernel/ptrace.c
+++ b/arch/m68k/kernel/ptrace.c
@@ -11,6 +11,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <linux/mm.h>
@@ -289,6 +290,8 @@ asmlinkage int syscall_trace_enter(void)
asmlinkage void syscall_trace_leave(void)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(task_pt_regs(current));
if (test_thread_flag(TIF_SYSCALL_TRACE))
ptrace_report_syscall_exit(task_pt_regs(current), 0);
}
diff --git a/arch/microblaze/include/asm/thread_info.h b/arch/microblaze/include/asm/thread_info.h
index 0153f7c2717c9..ee2f76589696b 100644
--- a/arch/microblaze/include/asm/thread_info.h
+++ b/arch/microblaze/include/asm/thread_info.h
@@ -103,6 +103,7 @@ static inline struct thread_info *current_thread_info(void)
#define TIF_SINGLESTEP 4
#define TIF_NOTIFY_SIGNAL 5 /* signal notifications exist */
#define TIF_MEMDIE 6 /* is terminating due to OOM killer */
+#define TIF_FD_SLOTS 7 /* syscall prepared descriptors */
#define TIF_SYSCALL_AUDIT 9 /* syscall auditing active */
#define TIF_SECCOMP 10 /* secure computing */
@@ -118,10 +119,12 @@ static inline struct thread_info *current_thread_info(void)
#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
/* work to do in syscall trace */
#define _TIF_WORK_SYSCALL_MASK (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | \
- _TIF_SYSCALL_AUDIT | _TIF_SECCOMP)
+ _TIF_SYSCALL_AUDIT | _TIF_SECCOMP | \
+ _TIF_FD_SLOTS)
/* work to do on interrupt/exception return */
#define _TIF_WORK_MASK 0x0000FFFE
diff --git a/arch/microblaze/kernel/ptrace.c b/arch/microblaze/kernel/ptrace.c
index 236264e932d64..e6951fea85937 100644
--- a/arch/microblaze/kernel/ptrace.c
+++ b/arch/microblaze/kernel/ptrace.c
@@ -25,6 +25,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
@@ -156,6 +157,9 @@ asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
{
int step;
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
step = test_thread_flag(TIF_SINGLESTEP);
diff --git a/arch/mips/include/asm/thread_info.h b/arch/mips/include/asm/thread_info.h
index 2707dad260dd7..79c81696e9c19 100644
--- a/arch/mips/include/asm/thread_info.h
+++ b/arch/mips/include/asm/thread_info.h
@@ -114,6 +114,7 @@ register unsigned long current_stack_pointer __asm__("sp");
#define TIF_NOTIFY_RESUME 5 /* callback before returning to user */
#define TIF_UPROBE 6 /* breakpointed or singlestepping */
#define TIF_NOTIFY_SIGNAL 7 /* signal notifications exist */
+#define TIF_FD_SLOTS 8 /* syscall prepared descriptors */
#define TIF_RESTORE_SIGMASK 9 /* restore signal mask in do_signal() */
#define TIF_USEDFPU 16 /* FPU was used by this task this quantum (SMP) */
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
@@ -152,6 +153,7 @@ register unsigned long current_stack_pointer __asm__("sp");
#define _TIF_USEDMSA (1<<TIF_USEDMSA)
#define _TIF_MSA_CTX_LIVE (1<<TIF_MSA_CTX_LIVE)
#define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_WORK_SYSCALL_ENTRY (_TIF_NOHZ | _TIF_SYSCALL_TRACE | \
_TIF_SYSCALL_AUDIT | \
@@ -159,7 +161,8 @@ register unsigned long current_stack_pointer __asm__("sp");
/* work to do in syscall_trace_leave() */
#define _TIF_WORK_SYSCALL_EXIT (_TIF_NOHZ | _TIF_SYSCALL_TRACE | \
- _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_TRACEPOINT)
+ _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_TRACEPOINT | \
+ _TIF_FD_SLOTS)
/* work to do on interrupt/exception return */
#define _TIF_WORK_MASK \
diff --git a/arch/mips/kernel/ptrace.c b/arch/mips/kernel/ptrace.c
index 17dc7249b8cac..d68eab59bcc21 100644
--- a/arch/mips/kernel/ptrace.c
+++ b/arch/mips/kernel/ptrace.c
@@ -17,6 +17,7 @@
#include <linux/compiler.h>
#include <linux/context_tracking.h>
#include <linux/elf.h>
+#include <linux/file.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
@@ -1364,6 +1365,9 @@ asmlinkage void syscall_trace_leave(struct pt_regs *regs)
*/
user_exit();
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
diff --git a/arch/nios2/include/asm/thread_info.h b/arch/nios2/include/asm/thread_info.h
index 83df79286d62e..7f5816e1b3d6e 100644
--- a/arch/nios2/include/asm/thread_info.h
+++ b/arch/nios2/include/asm/thread_info.h
@@ -78,6 +78,7 @@ static inline struct thread_info *current_thread_info(void)
#define TIF_SECCOMP 5 /* secure computing */
#define TIF_SYSCALL_AUDIT 6 /* syscall auditing active */
#define TIF_NOTIFY_SIGNAL 7 /* signal notifications exist */
+#define TIF_FD_SLOTS 8 /* syscall prepared descriptors */
#define TIF_RESTORE_SIGMASK 9 /* restore signal mask in do_signal() */
#define TIF_POLLING_NRFLAG 16 /* true if poll_idle() is polling
@@ -90,6 +91,7 @@ static inline struct thread_info *current_thread_info(void)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_RESTORE_SIGMASK (1 << TIF_RESTORE_SIGMASK)
#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
diff --git a/arch/nios2/kernel/entry.S b/arch/nios2/kernel/entry.S
index dd40dfd908e59..b3a99a6db379e 100644
--- a/arch/nios2/kernel/entry.S
+++ b/arch/nios2/kernel/entry.S
@@ -222,6 +222,11 @@ translate_rc_and_ret:
stw r2, PT_R2(sp)
stw r1, PT_R7(sp)
end_translate_rc_and_ret:
+ /* Descriptors reserved by the syscall are committed in
+ * do_syscall_trace_exit, take the traced exit path for them. */
+ GET_THREAD_INFO r11
+ ldw r11, TI_FLAGS(r11)
+ BTBNZ r11, r11, TIF_FD_SLOTS, end_translate_rc_and_ret2
ret_from_exception:
ldw r1, PT_ESTATUS(sp)
diff --git a/arch/nios2/kernel/ptrace.c b/arch/nios2/kernel/ptrace.c
index 2cd7e1ecbd74b..5cee1b717662f 100644
--- a/arch/nios2/kernel/ptrace.c
+++ b/arch/nios2/kernel/ptrace.c
@@ -9,6 +9,7 @@
#include <linux/elf.h>
#include <linux/errno.h>
+#include <linux/file.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/ptrace.h>
@@ -140,6 +141,8 @@ asmlinkage int do_syscall_trace_enter(void)
asmlinkage void do_syscall_trace_exit(void)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(task_pt_regs(current));
if (test_thread_flag(TIF_SYSCALL_TRACE))
ptrace_report_syscall_exit(task_pt_regs(current), 0);
}
diff --git a/arch/openrisc/include/asm/thread_info.h b/arch/openrisc/include/asm/thread_info.h
index e338fff7efb0e..3d8a710a809d2 100644
--- a/arch/openrisc/include/asm/thread_info.h
+++ b/arch/openrisc/include/asm/thread_info.h
@@ -92,6 +92,7 @@ register struct thread_info *current_thread_info_reg asm("r10");
* mode
*/
#define TIF_NOTIFY_SIGNAL 5 /* signal notifications exist */
+#define TIF_FD_SLOTS 6 /* syscall prepared descriptors */
#define TIF_SYSCALL_TRACEPOINT 8 /* for ftrace syscall instrumentation */
#define TIF_RESTORE_SIGMASK 9
#define TIF_POLLING_NRFLAG 16 /* true if poll_idle() is polling * TIF_NEED_RESCHED
@@ -104,12 +105,14 @@ register struct thread_info *current_thread_info_reg asm("r10");
#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED)
#define _TIF_SINGLESTEP (1<<TIF_SINGLESTEP)
#define _TIF_NOTIFY_SIGNAL (1<<TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_POLLING_NRFLAG (1<<TIF_POLLING_NRFLAG)
/* Work to do when returning from interrupt/exception */
/* For OpenRISC, this is anything in the LSW other than syscall trace */
-#define _TIF_WORK_MASK (0xff & ~(_TIF_SYSCALL_TRACE|_TIF_SINGLESTEP))
+#define _TIF_WORK_MASK (0xff & ~(_TIF_SYSCALL_TRACE|_TIF_SINGLESTEP| \
+ _TIF_FD_SLOTS))
#endif /* __KERNEL__ */
diff --git a/arch/openrisc/kernel/entry.S b/arch/openrisc/kernel/entry.S
index 18e68680471eb..c70265c4fe555 100644
--- a/arch/openrisc/kernel/entry.S
+++ b/arch/openrisc/kernel/entry.S
@@ -701,10 +701,11 @@ _syscall_show_regs:
#endif
_syscall_check_trace_leave:
- /* r30 is a callee-saved register so this should still hold the
- * _TIF_SYSCALL_TRACE flag from _syscall_check_trace_enter above...
+ /* Reload the flags, the syscall may have set TIF_FD_SLOTS.
* _syscall_trace_leave expects syscall result to be in pt_regs->r11.
*/
+ l.lwz r30,TI_FLAGS(r10)
+ l.andi r30,r30,(_TIF_SYSCALL_TRACE|_TIF_FD_SLOTS)
l.sfne r30,r0
l.bf _syscall_trace_leave
l.nop
diff --git a/arch/openrisc/kernel/ptrace.c b/arch/openrisc/kernel/ptrace.c
index 287a9718f0c60..71614b36e5162 100644
--- a/arch/openrisc/kernel/ptrace.c
+++ b/arch/openrisc/kernel/ptrace.c
@@ -13,6 +13,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <linux/string.h>
@@ -311,6 +312,9 @@ asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
{
int step;
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
step = test_thread_flag(TIF_SINGLESTEP);
diff --git a/arch/parisc/include/asm/thread_info.h b/arch/parisc/include/asm/thread_info.h
index b283738bb6dab..14a40c53689f9 100644
--- a/arch/parisc/include/asm/thread_info.h
+++ b/arch/parisc/include/asm/thread_info.h
@@ -52,6 +52,7 @@ struct thread_info {
#define TIF_SECCOMP 11 /* secure computing */
#define TIF_SYSCALL_TRACEPOINT 12 /* syscall tracepoint instrumentation */
#define TIF_NONBLOCK_WARNING 13 /* warned about wrong O_NONBLOCK usage */
+#define TIF_FD_SLOTS 14 /* syscall prepared descriptors */
#define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
@@ -65,6 +66,7 @@ struct thread_info {
#define _TIF_BLOCKSTEP (1 << TIF_BLOCKSTEP)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | \
_TIF_NEED_RESCHED | _TIF_NOTIFY_SIGNAL)
diff --git a/arch/parisc/kernel/entry.S b/arch/parisc/kernel/entry.S
index e04c5d806c105..c5807cf6fbd8d 100644
--- a/arch/parisc/kernel/entry.S
+++ b/arch/parisc/kernel/entry.S
@@ -1732,6 +1732,8 @@ syscall_check_resched:
/* check for reschedule */
mfctl %cr30,%r19
LDREG TASK_TI_FLAGS(%r19),%r19 /* long */
+ /* descriptors reserved by the syscall are committed in do_syscall_trace_exit */
+ bb,<,n %r19, 31-TIF_FD_SLOTS, syscall_do_fd_slots /* forward */
bb,<,n %r19, 31-TIF_NEED_RESCHED, syscall_do_resched /* forward */
.import do_signal,code
@@ -1765,6 +1767,16 @@ syscall_do_signal:
b,n syscall_check_sig
+syscall_do_fd_slots:
+ mfctl %cr30,%r1
+#ifdef CONFIG_64BIT
+ ldo -16(%r30),%r29 /* Reference param save area */
+#endif
+ ldo TASK_REGS(%r1),%r26 /* struct pt_regs *regs */
+ BL do_syscall_trace_exit,%r2
+ nop
+ b,n syscall_check_resched
+
syscall_restore:
mfctl %cr30,%r1
diff --git a/arch/parisc/kernel/ptrace.c b/arch/parisc/kernel/ptrace.c
index 1d9e210702e1f..f142766792afa 100644
--- a/arch/parisc/kernel/ptrace.c
+++ b/arch/parisc/kernel/ptrace.c
@@ -9,6 +9,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/smp.h>
@@ -381,6 +382,9 @@ void do_syscall_trace_exit(struct pt_regs *regs)
int stepping = test_thread_flag(TIF_SINGLESTEP) ||
test_thread_flag(TIF_BLOCKSTEP);
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
diff --git a/arch/powerpc/platforms/cell/spufs/inode.c b/arch/powerpc/platforms/cell/spufs/inode.c
index 2b54afb31529f..b3e9204e70312 100644
--- a/arch/powerpc/platforms/cell/spufs/inode.c
+++ b/arch/powerpc/platforms/cell/spufs/inode.c
@@ -266,10 +266,10 @@ spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
static int spufs_context_open(const struct path *path)
{
FD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred()));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
fd_prepare_file(fdf)->f_op = &spufs_context_fops;
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
static struct spu_context *
@@ -499,10 +499,10 @@ static int spufs_gang_open(const struct path *path)
* in error path of *_open().
*/
FD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred()));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
fd_prepare_file(fdf)->f_op = &spufs_gang_fops;
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
static int spufs_create_gang(struct inode *inode,
diff --git a/arch/sh/include/asm/thread_info.h b/arch/sh/include/asm/thread_info.h
index 471db51730361..d976cf21f3463 100644
--- a/arch/sh/include/asm/thread_info.h
+++ b/arch/sh/include/asm/thread_info.h
@@ -106,6 +106,7 @@ extern void init_thread_xstate(void);
#define TIF_SECCOMP 6 /* secure computing */
#define TIF_NOTIFY_RESUME 7 /* callback before returning to user */
#define TIF_SYSCALL_TRACEPOINT 8 /* for ftrace syscall instrumentation */
+#define TIF_FD_SLOTS 9 /* syscall prepared descriptors */
#define TIF_POLLING_NRFLAG 17 /* true if poll_idle() is polling TIF_NEED_RESCHED */
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
@@ -118,22 +119,25 @@ extern void init_thread_xstate(void);
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
/* work to do in syscall trace */
#define _TIF_WORK_SYSCALL_MASK (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | \
_TIF_SYSCALL_AUDIT | _TIF_SECCOMP | \
- _TIF_SYSCALL_TRACEPOINT)
+ _TIF_SYSCALL_TRACEPOINT | _TIF_FD_SLOTS)
/* work to do on any return to u-space */
#define _TIF_ALLWORK_MASK (_TIF_SYSCALL_TRACE | _TIF_SIGPENDING | \
_TIF_NEED_RESCHED | _TIF_SYSCALL_AUDIT | \
_TIF_SINGLESTEP | _TIF_NOTIFY_RESUME | \
- _TIF_SYSCALL_TRACEPOINT | _TIF_NOTIFY_SIGNAL)
+ _TIF_SYSCALL_TRACEPOINT | _TIF_NOTIFY_SIGNAL | \
+ _TIF_FD_SLOTS)
/* work to do on interrupt/exception return */
#define _TIF_WORK_MASK (_TIF_ALLWORK_MASK & ~(_TIF_SYSCALL_TRACE | \
- _TIF_SYSCALL_AUDIT | _TIF_SINGLESTEP))
+ _TIF_SYSCALL_AUDIT | _TIF_SINGLESTEP | \
+ _TIF_FD_SLOTS))
/*
* Thread-synchronous status.
diff --git a/arch/sh/kernel/entry-common.S b/arch/sh/kernel/entry-common.S
index 91ab2607a1ff9..593a482e85f52 100644
--- a/arch/sh/kernel/entry-common.S
+++ b/arch/sh/kernel/entry-common.S
@@ -164,9 +164,13 @@ work_resched:
.align 2
syscall_exit_work:
- ! r0: current_thread_info->flags
! r8: current_thread_info
- tst #(_TIF_WORK_SYSCALL_MASK & 0xff), r0
+ mov.l @(TI_FLAGS,r8), r0 ! current_thread_info->flags
+ mov #(_TIF_WORK_SYSCALL_MASK & 0xff), r1
+ mov #(_TIF_WORK_SYSCALL_MASK >> 8), r2
+ shll8 r2
+ or r2, r1
+ tst r1, r0
bt/s work_pending
tst #_TIF_NEED_RESCHED, r0
TRACE_IRQS_ON
diff --git a/arch/sh/kernel/ptrace_32.c b/arch/sh/kernel/ptrace_32.c
index 8794081483fbd..3deadd71f464c 100644
--- a/arch/sh/kernel/ptrace_32.c
+++ b/arch/sh/kernel/ptrace_32.c
@@ -8,6 +8,7 @@
* Audit support by Yuichi Nakamura <ynakam@hitachisoft.jp>
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <linux/mm.h>
@@ -476,6 +477,9 @@ asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
{
int step;
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
diff --git a/arch/sparc/include/asm/thread_info_32.h b/arch/sparc/include/asm/thread_info_32.h
index fdaf7b171e0ac..fbd7af6bb4271 100644
--- a/arch/sparc/include/asm/thread_info_32.h
+++ b/arch/sparc/include/asm/thread_info_32.h
@@ -105,6 +105,7 @@ register struct thread_info *current_thread_info_reg asm("g6");
#define TIF_NEED_RESCHED 3 /* rescheduling necessary */
#define TIF_RESTORE_SIGMASK 4 /* restore signal mask in do_signal() */
#define TIF_NOTIFY_SIGNAL 5 /* signal notifications exist */
+#define TIF_FD_SLOTS 6 /* syscall prepared descriptors */
#define TIF_USEDFPU 8 /* FPU was used by this task
* this quantum (SMP) */
#define TIF_POLLING_NRFLAG 9 /* true if poll_idle() is polling
@@ -117,6 +118,7 @@ register struct thread_info *current_thread_info_reg asm("g6");
#define _TIF_SIGPENDING (1<<TIF_SIGPENDING)
#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED)
#define _TIF_NOTIFY_SIGNAL (1<<TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_USEDFPU (1<<TIF_USEDFPU)
#define _TIF_POLLING_NRFLAG (1<<TIF_POLLING_NRFLAG)
diff --git a/arch/sparc/include/asm/thread_info_64.h b/arch/sparc/include/asm/thread_info_64.h
index c8a73dff27f80..890ee65d90290 100644
--- a/arch/sparc/include/asm/thread_info_64.h
+++ b/arch/sparc/include/asm/thread_info_64.h
@@ -180,7 +180,7 @@ extern struct thread_info *current_thread_info(void);
#define TIF_SIGPENDING 2 /* signal pending */
#define TIF_NEED_RESCHED 3 /* rescheduling necessary */
#define TIF_NOTIFY_SIGNAL 4 /* signal notifications exist */
-#define TIF_UNALIGNED 5 /* allowed to do unaligned accesses */
+#define TIF_FD_SLOTS 5 /* syscall prepared descriptors */
#define TIF_UPROBE 6 /* breakpointed or singlestepped */
#define TIF_32BIT 7 /* 32-bit binary */
#define TIF_NOHZ 8 /* in adaptive nohz mode */
@@ -194,12 +194,14 @@ extern struct thread_info *current_thread_info(void);
#define TIF_MCDPER 12 /* Precise MCD exception */
#define TIF_MEMDIE 13 /* is terminating due to OOM killer */
#define TIF_POLLING_NRFLAG 14
+#define TIF_UNALIGNED 15 /* allowed to do unaligned accesses */
#define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
#define _TIF_NOTIFY_RESUME (1<<TIF_NOTIFY_RESUME)
#define _TIF_SIGPENDING (1<<TIF_SIGPENDING)
#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED)
#define _TIF_NOTIFY_SIGNAL (1<<TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_UNALIGNED (1<<TIF_UNALIGNED)
#define _TIF_UPROBE (1<<TIF_UPROBE)
#define _TIF_32BIT (1<<TIF_32BIT)
@@ -209,6 +211,11 @@ extern struct thread_info *current_thread_info(void);
#define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
#define _TIF_POLLING_NRFLAG (1<<TIF_POLLING_NRFLAG)
+/* work to do in syscall_trace_leave() */
+#define _TIF_SYSCALL_EXIT_WORK (_TIF_SYSCALL_TRACE | _TIF_SECCOMP | \
+ _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_TRACEPOINT | \
+ _TIF_NOHZ | _TIF_FD_SLOTS)
+
#define _TIF_USER_WORK_MASK ((0xff << TI_FLAG_WSAVED_SHIFT) | \
_TIF_DO_NOTIFY_RESUME_MASK | \
_TIF_NEED_RESCHED)
diff --git a/arch/sparc/kernel/entry.S b/arch/sparc/kernel/entry.S
index ea51ef52c952a..d2023b84db9e2 100644
--- a/arch/sparc/kernel/entry.S
+++ b/arch/sparc/kernel/entry.S
@@ -1023,7 +1023,7 @@ ret_sys_call:
ld [%sp + STACKFRAME_SZ + PT_PSR], %g3
set PSR_C, %g2
bgeu 1f
- andcc %l5, _TIF_SYSCALL_TRACE, %g0
+ andcc %l5, (_TIF_SYSCALL_TRACE | _TIF_FD_SLOTS), %g0
/* System call success, clear Carry condition code. */
andn %g3, %g2, %g3
diff --git a/arch/sparc/kernel/ptrace_32.c b/arch/sparc/kernel/ptrace_32.c
index 5991b1731feed..042e606854183 100644
--- a/arch/sparc/kernel/ptrace_32.c
+++ b/arch/sparc/kernel/ptrace_32.c
@@ -11,6 +11,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/errno.h>
@@ -437,6 +438,9 @@ asmlinkage int syscall_trace(struct pt_regs *regs, int syscall_exit_p)
{
int ret = 0;
+ if (syscall_exit_p && test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
if (test_thread_flag(TIF_SYSCALL_TRACE)) {
if (syscall_exit_p)
ptrace_report_syscall_exit(regs, 0);
diff --git a/arch/sparc/kernel/ptrace_64.c b/arch/sparc/kernel/ptrace_64.c
index 825ddf55fece3..499b30c505fdd 100644
--- a/arch/sparc/kernel/ptrace_64.c
+++ b/arch/sparc/kernel/ptrace_64.c
@@ -12,6 +12,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <linux/mm.h>
@@ -1110,6 +1111,9 @@ asmlinkage void syscall_trace_leave(struct pt_regs *regs)
if (test_thread_flag(TIF_NOHZ))
user_exit();
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
diff --git a/arch/sparc/kernel/syscalls.S b/arch/sparc/kernel/syscalls.S
index 96fe8763d70ce..086fb8eaf1331 100644
--- a/arch/sparc/kernel/syscalls.S
+++ b/arch/sparc/kernel/syscalls.S
@@ -269,10 +269,12 @@ ret_sys_call:
ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %g3
mov %ulo(TSTATE_XCARRY | TSTATE_ICARRY), %g2
sllx %g2, 32, %g2
+ /* Reload the flags, the syscall may have set TIF_FD_SLOTS. */
+ ldx [%g6 + TI_FLAGS], %l0
cmp %o0, -ERESTART_RESTARTBLOCK
bgeu,pn %xcc, 1f
- andcc %l0, (_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT|_TIF_SYSCALL_TRACEPOINT|_TIF_NOHZ), %g0
+ andcc %l0, _TIF_SYSCALL_EXIT_WORK, %g0
ldx [%sp + PTREGS_OFF + PT_V9_TNPC], %l1 ! pc = npc
2:
diff --git a/arch/um/include/asm/thread_info.h b/arch/um/include/asm/thread_info.h
index 7a6f4dc99fa16..7140516ee0b6c 100644
--- a/arch/um/include/asm/thread_info.h
+++ b/arch/um/include/asm/thread_info.h
@@ -44,6 +44,7 @@ struct thread_info {
#define TIF_SECCOMP 9 /* secure computing */
#define TIF_SINGLESTEP 10 /* single stepping userspace */
#define TIF_SYSCALL_TRACEPOINT 11 /* syscall tracepoint instrumentation */
+#define TIF_FD_SLOTS 12 /* syscall prepared descriptors */
#define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
@@ -55,6 +56,7 @@ struct thread_info {
#define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL | \
_TIF_NOTIFY_RESUME)
diff --git a/arch/um/kernel/ptrace.c b/arch/um/kernel/ptrace.c
index 7da0a5223aa69..f1f89181fb7e1 100644
--- a/arch/um/kernel/ptrace.c
+++ b/arch/um/kernel/ptrace.c
@@ -4,6 +4,7 @@
*/
#include <linux/audit.h>
+#include <linux/file.h>
#include <linux/ptrace.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
@@ -142,6 +143,9 @@ void syscall_trace_leave(struct pt_regs *regs)
{
int ptraced = current->ptrace;
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
/* Fake a debug trap */
diff --git a/arch/xtensa/include/asm/thread_info.h b/arch/xtensa/include/asm/thread_info.h
index 5b74dfc35ef95..0c8eb5dc83982 100644
--- a/arch/xtensa/include/asm/thread_info.h
+++ b/arch/xtensa/include/asm/thread_info.h
@@ -124,6 +124,7 @@ static __always_inline struct thread_info *current_thread_info(void)
#define TIF_SYSCALL_AUDIT 9 /* syscall auditing active */
#define TIF_SECCOMP 10 /* secure computing */
#define TIF_MEMDIE 11 /* is terminating due to OOM killer */
+#define TIF_FD_SLOTS 12 /* syscall prepared descriptors */
#define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
#define _TIF_SIGPENDING (1<<TIF_SIGPENDING)
@@ -134,6 +135,7 @@ static __always_inline struct thread_info *current_thread_info(void)
#define _TIF_NOTIFY_RESUME (1<<TIF_NOTIFY_RESUME)
#define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT)
#define _TIF_SECCOMP (1<<TIF_SECCOMP)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_WORK_MASK (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | \
_TIF_SYSCALL_TRACEPOINT | \
diff --git a/arch/xtensa/kernel/entry.S b/arch/xtensa/kernel/entry.S
index 272fff587907f..4b95f48736e6b 100644
--- a/arch/xtensa/kernel/entry.S
+++ b/arch/xtensa/kernel/entry.S
@@ -1976,6 +1976,11 @@ ENTRY(system_call)
1: /* regs->areg[2] = return_value */
s32i abi_rv, abi_saved0, PT_AREG2
+
+ /* descriptors reserved by the syscall are committed in do_syscall_trace_leave */
+ GET_THREAD_INFO(a4, a1)
+ l32i a4, a4, TI_FLAGS
+ bbsi.l a4, TIF_FD_SLOTS, 1f
bnez abi_saved1, 1f
.Lsyscall_exit:
#if defined(__XTENSA_WINDOWED_ABI__)
diff --git a/arch/xtensa/kernel/ptrace.c b/arch/xtensa/kernel/ptrace.c
index 364e4fdabb00d..d44d62a56b854 100644
--- a/arch/xtensa/kernel/ptrace.c
+++ b/arch/xtensa/kernel/ptrace.c
@@ -13,6 +13,7 @@
*/
#include <linux/audit.h>
+#include <linux/file.h>
#include <linux/errno.h>
#include <linux/hw_breakpoint.h>
#include <linux/kernel.h>
@@ -571,6 +572,9 @@ void do_syscall_trace_leave(struct pt_regs *regs)
{
int step;
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
diff --git a/drivers/accel/habanalabs/common/memory.c b/drivers/accel/habanalabs/common/memory.c
index 361cff5773814..1559442cd56f4 100644
--- a/drivers/accel/habanalabs/common/memory.c
+++ b/drivers/accel/habanalabs/common/memory.c
@@ -1861,11 +1861,13 @@ static int export_dmabuf(struct hl_ctx *ctx,
{
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
struct hl_device *hdev = ctx->hdev;
- CLASS(get_unused_fd, fd)(flags);
+ const struct fd_slot *fd = fd_prepare(flags);
- if (fd < 0) {
- dev_err(hdev->dev, "failed to get a file descriptor for a dma-buf, %d\n", fd);
- return fd;
+ if (IS_ERR(fd)) {
+ int rc = PTR_ERR(fd);
+
+ dev_err(hdev->dev, "failed to get a file descriptor for a dma-buf, %d\n", rc);
+ return rc;
}
exp_info.ops = &habanalabs_dmabuf_ops;
@@ -1889,8 +1891,7 @@ static int export_dmabuf(struct hl_ctx *ctx,
*/
get_file(ctx->hpriv->file_priv->filp);
- *dmabuf_fd = fd;
- fd_install(take_fd(fd), hl_dmabuf->dmabuf->file);
+ *dmabuf_fd = fd_stage(fd, hl_dmabuf->dmabuf->file);
return 0;
}
diff --git a/drivers/char/tpm/tpm_vtpm_proxy.c b/drivers/char/tpm/tpm_vtpm_proxy.c
index b81fd2a537df4..8eb87044596a4 100644
--- a/drivers/char/tpm/tpm_vtpm_proxy.c
+++ b/drivers/char/tpm/tpm_vtpm_proxy.c
@@ -531,8 +531,8 @@ static struct file *vtpm_proxy_create_device(
struct vtpm_proxy_new_dev *vtpm_new_dev)
{
struct proxy_dev *proxy_dev;
- int rc, fd;
struct file *file;
+ int rc;
if (vtpm_new_dev->flags & ~VTPM_PROXY_FLAGS_ALL)
return ERR_PTR(-EOPNOTSUPP);
@@ -544,20 +544,15 @@ static struct file *vtpm_proxy_create_device(
proxy_dev->flags = vtpm_new_dev->flags;
/* setup an anonymous file for the server-side */
- fd = get_unused_fd_flags(O_RDWR);
- if (fd < 0) {
- rc = fd;
+ FD_PREPARE(fdf, O_RDWR,
+ anon_inode_getfile("[vtpms]", &vtpm_proxy_fops, proxy_dev,
+ O_RDWR));
+ if (IS_ERR(fdf)) {
+ rc = PTR_ERR(fdf);
goto err_delete_proxy_dev;
}
+ file = fd_prepare_file(fdf);
- file = anon_inode_getfile("[vtpms]", &vtpm_proxy_fops, proxy_dev,
- O_RDWR);
- if (IS_ERR(file)) {
- rc = PTR_ERR(file);
- goto err_put_unused_fd;
- }
-
- /* from now on we can unwind with put_unused_fd() + fput() */
/* simulate an open() on the server side */
vtpm_proxy_fops_open(file);
@@ -566,16 +561,13 @@ static struct file *vtpm_proxy_create_device(
vtpm_proxy_work_start(proxy_dev);
- vtpm_new_dev->fd = fd;
+ vtpm_new_dev->fd = fd_prepare_fd(fdf);
vtpm_new_dev->major = MAJOR(proxy_dev->chip->dev.devt);
vtpm_new_dev->minor = MINOR(proxy_dev->chip->dev.devt);
vtpm_new_dev->tpm_num = proxy_dev->chip->dev_num;
return file;
-err_put_unused_fd:
- put_unused_fd(fd);
-
err_delete_proxy_dev:
vtpm_proxy_delete_proxy_dev(proxy_dev);
@@ -640,13 +632,9 @@ static long vtpmx_ioc_new_dev(struct file *file, unsigned int ioctl,
return PTR_ERR(vtpm_file);
if (copy_to_user(vtpm_new_dev_p, &vtpm_new_dev,
- sizeof(vtpm_new_dev))) {
- put_unused_fd(vtpm_new_dev.fd);
- fput(vtpm_file);
+ sizeof(vtpm_new_dev)))
return -EFAULT;
- }
- fd_install(vtpm_new_dev.fd, vtpm_file);
return 0;
}
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index d504c636dc294..f0d581fad7aec 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -440,7 +440,8 @@ static long dma_buf_export_sync_file(struct dma_buf *dmabuf,
enum dma_resv_usage usage;
struct dma_fence *fence = NULL;
struct sync_file *sync_file;
- int fd, ret;
+ int ret;
+ const struct fd_slot *fd;
if (copy_from_user(&arg, user_data, sizeof(arg)))
return -EFAULT;
@@ -451,14 +452,14 @@ static long dma_buf_export_sync_file(struct dma_buf *dmabuf,
if ((arg.flags & DMA_BUF_SYNC_RW) == 0)
return -EINVAL;
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0)
- return fd;
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
usage = dma_resv_usage_rw(arg.flags & DMA_BUF_SYNC_WRITE);
ret = dma_resv_get_singleton(dmabuf->resv, usage, &fence);
if (ret)
- goto err_put_fd;
+ return ret;
if (!fence)
fence = dma_fence_get_stub();
@@ -467,26 +468,14 @@ static long dma_buf_export_sync_file(struct dma_buf *dmabuf,
dma_fence_put(fence);
- if (!sync_file) {
- ret = -ENOMEM;
- goto err_put_fd;
- }
-
- arg.fd = fd;
- if (copy_to_user(user_data, &arg, sizeof(arg))) {
- ret = -EFAULT;
- goto err_put_file;
- }
+ if (!sync_file)
+ return -ENOMEM;
- fd_install(fd, sync_file->file);
+ arg.fd = fd_stage(fd, sync_file->file);
+ if (copy_to_user(user_data, &arg, sizeof(arg)))
+ return -EFAULT;
return 0;
-
-err_put_file:
- fput(sync_file->file);
-err_put_fd:
- put_unused_fd(fd);
- return ret;
}
static long dma_buf_import_sync_file(struct dma_buf *dmabuf,
diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
index 8df20b0218a9e..90852950e4e34 100644
--- a/drivers/dma-buf/sw_sync.c
+++ b/drivers/dma-buf/sw_sync.c
@@ -344,8 +344,7 @@ static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
unsigned long arg)
{
- int fd = get_unused_fd_flags(O_CLOEXEC);
- int err;
+ const struct fd_slot *fd = fd_prepare(O_CLOEXEC);
struct sync_pt *pt;
struct sync_file *sync_file;
struct sw_sync_create_fence_data data;
@@ -353,41 +352,26 @@ static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
/* SW sync fence are inherently unsafe and can deadlock the kernel */
add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
- if (fd < 0)
- return fd;
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
- if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
- err = -EFAULT;
- goto err;
- }
+ if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
+ return -EFAULT;
pt = sync_pt_create(obj, data.value);
- if (!pt) {
- err = -ENOMEM;
- goto err;
- }
+ if (!pt)
+ return -ENOMEM;
sync_file = sync_file_create(&pt->base);
dma_fence_put(&pt->base);
- if (!sync_file) {
- err = -ENOMEM;
- goto err;
- }
-
- data.fence = fd;
- if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
- fput(sync_file->file);
- err = -EFAULT;
- goto err;
- }
+ if (!sync_file)
+ return -ENOMEM;
- fd_install(fd, sync_file->file);
+ data.fence = fd_stage(fd, sync_file->file);
+ if (copy_to_user((void __user *)arg, &data, sizeof(data)))
+ return -EFAULT;
return 0;
-
-err:
- put_unused_fd(fd);
- return err;
}
static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c
index 2166bbdf7e4a4..316fffc4067e8 100644
--- a/drivers/dma-buf/sync_file.c
+++ b/drivers/dma-buf/sync_file.c
@@ -213,56 +213,34 @@ static __poll_t sync_file_poll(struct file *file, poll_table *wait)
static long sync_file_ioctl_merge(struct sync_file *sync_file,
unsigned long arg)
{
- int fd = get_unused_fd_flags(O_CLOEXEC);
- int err;
+ const struct fd_slot *fd = fd_prepare(O_CLOEXEC);
struct sync_file *fence2, *fence3;
struct sync_merge_data data;
- if (fd < 0)
- return fd;
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
- if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
- err = -EFAULT;
- goto err_put_fd;
- }
+ if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
+ return -EFAULT;
- if (data.flags || data.pad) {
- err = -EINVAL;
- goto err_put_fd;
- }
+ if (data.flags || data.pad)
+ return -EINVAL;
fence2 = sync_file_fdget(data.fd2);
- if (!fence2) {
- err = -ENOENT;
- goto err_put_fd;
- }
+ if (!fence2)
+ return -ENOENT;
data.name[sizeof(data.name) - 1] = '\0';
fence3 = sync_file_merge(data.name, sync_file, fence2);
- if (!fence3) {
- err = -ENOMEM;
- goto err_put_fence2;
- }
-
- data.fence = fd;
- if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
- err = -EFAULT;
- goto err_put_fence3;
- }
-
- fd_install(fd, fence3->file);
fput(fence2->file);
- return 0;
-
-err_put_fence3:
- fput(fence3->file);
+ if (!fence3)
+ return -ENOMEM;
-err_put_fence2:
- fput(fence2->file);
+ data.fence = fd_stage(fd, fence3->file);
+ if (copy_to_user((void __user *)arg, &data, sizeof(data)))
+ return -EFAULT;
-err_put_fd:
- put_unused_fd(fd);
- return err;
+ return 0;
}
static int sync_fill_fence_info(struct dma_fence *fence,
diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
index 9f3b628d57934..4d138db254724 100644
--- a/drivers/gpio/gpiolib-cdev.c
+++ b/drivers/gpio/gpiolib-cdev.c
@@ -377,16 +377,14 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
FD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,
anon_inode_getfile("gpio-linehandle", &linehandle_fileops,
lh, O_RDONLY | O_CLOEXEC));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
retain_and_null_ptr(lh);
handlereq.fd = fd_prepare_fd(fdf);
if (copy_to_user(ip, &handlereq, sizeof(handlereq)))
return -EFAULT;
- fd_publish(fdf);
-
dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
handlereq.lines);
@@ -1715,16 +1713,14 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip)
FD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,
anon_inode_getfile("gpio-line", &line_fileops, lr,
O_RDONLY | O_CLOEXEC));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
retain_and_null_ptr(lr);
ulr.fd = fd_prepare_fd(fdf);
if (copy_to_user(ip, &ulr, sizeof(ulr)))
return -EFAULT;
- fd_publish(fdf);
-
dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
ulr.num_lines);
@@ -2115,16 +2111,14 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
FD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,
anon_inode_getfile("gpio-event", &lineevent_fileops, le,
O_RDONLY | O_CLOEXEC));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
retain_and_null_ptr(le);
eventreq.fd = fd_prepare_fd(fdf);
if (copy_to_user(ip, &eventreq, sizeof(eventreq)))
return -EFAULT;
- fd_publish(fdf);
-
return 0;
}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
index 03b41f8035206..e5c0f7201cea9 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
@@ -1581,7 +1581,8 @@ int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
struct dma_fence *fence;
struct drm_syncobj *syncobj;
struct sync_file *sync_file;
- int fd, r;
+ const struct fd_slot *fd;
+ int r;
fence = amdgpu_cs_get_fence(adev, filp, &info->in.fence);
if (IS_ERR(fence))
@@ -1610,21 +1611,18 @@ int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
return r;
case AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD:
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0) {
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd)) {
dma_fence_put(fence);
- return fd;
+ return PTR_ERR(fd);
}
sync_file = sync_file_create(fence);
dma_fence_put(fence);
- if (!sync_file) {
- put_unused_fd(fd);
+ if (!sync_file)
return -ENOMEM;
- }
- fd_install(fd, sync_file->file);
- info->out.handle = fd;
+ info->out.handle = fd_stage(fd, sync_file->file);
return 0;
default:
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 7fcfc150a7fc1..158abe7e3da65 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -1988,11 +1988,10 @@ static uint32_t get_process_num_bos(struct kfd_process *p)
return num_of_bos;
}
-static int criu_get_prime_handle(struct kgd_mem *mem,
- int flags, u32 *shared_fd,
- struct file **file)
+static int criu_get_prime_handle(struct kgd_mem *mem, int flags, u32 *shared_fd)
{
struct dma_buf *dmabuf;
+ const struct fd_slot *fd;
int ret;
ret = amdgpu_amdkfd_gpuvm_export_dmabuf(mem, &dmabuf);
@@ -2001,38 +2000,17 @@ static int criu_get_prime_handle(struct kgd_mem *mem,
return ret;
}
- ret = get_unused_fd_flags(flags);
- if (ret < 0) {
+ fd = fd_prepare(flags);
+ if (IS_ERR(fd)) {
+ ret = PTR_ERR(fd);
pr_err("dmabuf create fd failed, ret:%d\n", ret);
- goto out_free_dmabuf;
+ dma_buf_put(dmabuf);
+ return ret;
}
- *shared_fd = ret;
- *file = dmabuf->file;
+ /* Installed or dropped with the ioctl's result, nothing to put back. */
+ *shared_fd = fd_stage(fd, dmabuf->file);
return 0;
-
-out_free_dmabuf:
- dma_buf_put(dmabuf);
- return ret;
-}
-
-static void commit_files(struct file **files,
- struct kfd_criu_bo_bucket *bo_buckets,
- unsigned int count,
- int err)
-{
- while (count--) {
- struct file *file = files[count];
-
- if (!file)
- continue;
- if (err) {
- fput(file);
- put_unused_fd(bo_buckets[count].dmabuf_fd);
- } else {
- fd_install(bo_buckets[count].dmabuf_fd, file);
- }
- }
}
static int criu_checkpoint_bos(struct kfd_process *p,
@@ -2043,7 +2021,6 @@ static int criu_checkpoint_bos(struct kfd_process *p,
{
struct kfd_criu_bo_bucket *bo_buckets;
struct kfd_criu_bo_priv_data *bo_privs;
- struct file **files = NULL;
int ret = 0, pdd_index, bo_index = 0, id;
void *mem;
@@ -2057,12 +2034,6 @@ static int criu_checkpoint_bos(struct kfd_process *p,
goto exit;
}
- files = kvcalloc(num_bos, sizeof(struct file *), GFP_KERNEL);
- if (!files) {
- ret = -ENOMEM;
- goto exit;
- }
-
for (pdd_index = 0; pdd_index < p->n_pdds; pdd_index++) {
struct kfd_process_device *pdd = p->pdds[pdd_index];
struct amdgpu_bo *dumper_bo;
@@ -2105,7 +2076,7 @@ static int criu_checkpoint_bos(struct kfd_process *p,
ret = criu_get_prime_handle(kgd_mem,
bo_bucket->alloc_flags &
KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ? DRM_RDWR : 0,
- &bo_bucket->dmabuf_fd, &files[bo_index]);
+ &bo_bucket->dmabuf_fd);
if (ret)
goto exit;
} else {
@@ -2156,8 +2127,6 @@ static int criu_checkpoint_bos(struct kfd_process *p,
*priv_offset += num_bos * sizeof(*bo_privs);
exit:
- commit_files(files, bo_buckets, bo_index, ret);
- kvfree(files);
kvfree(bo_buckets);
kvfree(bo_privs);
return ret;
@@ -2504,8 +2473,7 @@ static int criu_restore_memory_of_gpu(struct kfd_process_device *pdd,
static int criu_restore_bo(struct kfd_process *p,
struct kfd_criu_bo_bucket *bo_bucket,
- struct kfd_criu_bo_priv_data *bo_priv,
- struct file **file)
+ struct kfd_criu_bo_priv_data *bo_priv)
{
struct kfd_process_device *pdd;
struct kgd_mem *kgd_mem;
@@ -2557,7 +2525,7 @@ static int criu_restore_bo(struct kfd_process *p,
if (bo_bucket->alloc_flags
& (KFD_IOC_ALLOC_MEM_FLAGS_VRAM | KFD_IOC_ALLOC_MEM_FLAGS_GTT)) {
ret = criu_get_prime_handle(kgd_mem, DRM_RDWR,
- &bo_bucket->dmabuf_fd, file);
+ &bo_bucket->dmabuf_fd);
if (ret)
return ret;
} else {
@@ -2574,7 +2542,6 @@ static int criu_restore_bos(struct kfd_process *p,
{
struct kfd_criu_bo_bucket *bo_buckets = NULL;
struct kfd_criu_bo_priv_data *bo_privs = NULL;
- struct file **files = NULL;
int ret = 0;
uint32_t i = 0;
@@ -2588,12 +2555,6 @@ static int criu_restore_bos(struct kfd_process *p,
if (!bo_buckets)
return -ENOMEM;
- files = kvcalloc(args->num_bos, sizeof(struct file *), GFP_KERNEL);
- if (!files) {
- ret = -ENOMEM;
- goto exit;
- }
-
ret = copy_from_user(bo_buckets, (void __user *)args->bos,
args->num_bos * sizeof(*bo_buckets));
if (ret) {
@@ -2619,7 +2580,7 @@ static int criu_restore_bos(struct kfd_process *p,
/* Create and map new BOs */
for (; i < args->num_bos; i++) {
- ret = criu_restore_bo(p, &bo_buckets[i], &bo_privs[i], &files[i]);
+ ret = criu_restore_bo(p, &bo_buckets[i], &bo_privs[i]);
if (ret) {
pr_debug("Failed to restore BO[%d] ret%d\n", i, ret);
goto exit;
@@ -2634,8 +2595,6 @@ static int criu_restore_bos(struct kfd_process *p,
ret = -EFAULT;
exit:
- commit_files(files, bo_buckets, i, ret);
- kvfree(files);
kvfree(bo_buckets);
kvfree(bo_privs);
return ret;
diff --git a/drivers/gpu/drm/drm_lease.c b/drivers/gpu/drm/drm_lease.c
index 5d2cf724cbd7b..b95747cf79a74 100644
--- a/drivers/gpu/drm/drm_lease.c
+++ b/drivers/gpu/drm/drm_lease.c
@@ -484,7 +484,7 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
struct file *lessee_file = NULL;
struct file *lessor_file = lessor_priv->filp;
struct drm_file *lessee_priv;
- int fd = -1;
+ const struct fd_slot *fd;
uint32_t *object_ids;
/* Can't lease without MODESET */
@@ -529,10 +529,10 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
}
/* Allocate a file descriptor for the lease */
- fd = get_unused_fd_flags(cl->flags & (O_CLOEXEC | O_NONBLOCK));
- if (fd < 0) {
+ fd = fd_prepare(cl->flags & (O_CLOEXEC | O_NONBLOCK));
+ if (IS_ERR(fd)) {
idr_destroy(&leases);
- ret = fd;
+ ret = PTR_ERR(fd);
goto out_lessor;
}
@@ -543,7 +543,7 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
if (IS_ERR(lessee)) {
ret = PTR_ERR(lessee);
idr_destroy(&leases);
- goto out_leases;
+ goto out_lessor;
}
/* Clone the lessor file to create a new file for us */
@@ -562,12 +562,13 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
lessee_priv->authenticated = 1;
/* Pass fd back to userspace */
- drm_dbg_lease(dev, "Returning fd %d id %d\n", fd, lessee->lessee_id);
- cl->fd = fd;
+ drm_dbg_lease(dev, "Returning fd %d id %d\n", fd_prepare_fd(fd),
+ lessee->lessee_id);
+ cl->fd = fd_prepare_fd(fd);
cl->lessee_id = lessee->lessee_id;
/* Hook up the fd */
- fd_install(fd, lessee_file);
+ fd_stage(fd, lessee_file);
drm_master_put(&lessor);
drm_dbg_lease(dev, "drm_mode_create_lease_ioctl succeeded\n");
@@ -576,9 +577,6 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
out_lessee:
drm_master_put(&lessee);
-out_leases:
- put_unused_fd(fd);
-
out_lessor:
drm_master_put(&lessor);
drm_dbg_lease(dev, "drm_mode_create_lease_ioctl failed: %d\n", ret);
diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c
index 9b44c78cd77fc..e33d84ac7256c 100644
--- a/drivers/gpu/drm/drm_prime.c
+++ b/drivers/gpu/drm/drm_prime.c
@@ -513,19 +513,16 @@ int drm_gem_prime_handle_to_fd(struct drm_device *dev,
int *prime_fd)
{
struct dma_buf *dmabuf;
- int fd = get_unused_fd_flags(flags);
+ const struct fd_slot *fd = fd_prepare(flags);
- if (fd < 0)
- return fd;
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
dmabuf = drm_gem_prime_handle_to_dmabuf(dev, file_priv, handle, flags);
- if (IS_ERR(dmabuf)) {
- put_unused_fd(fd);
+ if (IS_ERR(dmabuf))
return PTR_ERR(dmabuf);
- }
- fd_install(fd, dmabuf->file);
- *prime_fd = fd;
+ *prime_fd = fd_stage(fd, dmabuf->file);
return 0;
}
EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c
index 2fa170a29a626..621858b6ac437 100644
--- a/drivers/gpu/drm/drm_syncobj.c
+++ b/drivers/gpu/drm/drm_syncobj.c
@@ -663,25 +663,14 @@ static const struct file_operations drm_syncobj_file_fops = {
*/
int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
{
- struct file *file;
- int fd;
-
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- file = anon_inode_getfile("syncobj_file",
- &drm_syncobj_file_fops,
- syncobj, 0);
- if (IS_ERR(file)) {
- put_unused_fd(fd);
- return PTR_ERR(file);
- }
+ FD_PREPARE(fdf, O_CLOEXEC,
+ anon_inode_getfile("syncobj_file", &drm_syncobj_file_fops,
+ syncobj, 0));
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
drm_syncobj_get(syncobj);
- fd_install(fd, file);
-
- *p_fd = fd;
+ *p_fd = fd_prepare_fd(fdf);
return 0;
}
EXPORT_SYMBOL(drm_syncobj_get_fd);
@@ -762,31 +751,24 @@ static int drm_syncobj_export_sync_file(struct drm_file *file_private,
int ret;
struct dma_fence *fence;
struct sync_file *sync_file;
- int fd = get_unused_fd_flags(O_CLOEXEC);
+ const struct fd_slot *fd = fd_prepare(O_CLOEXEC);
- if (fd < 0)
- return fd;
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
ret = drm_syncobj_find_fence(file_private, handle, point, 0, &fence);
if (ret)
- goto err_put_fd;
+ return ret;
sync_file = sync_file_create(fence);
dma_fence_put(fence);
- if (!sync_file) {
- ret = -EINVAL;
- goto err_put_fd;
- }
-
- fd_install(fd, sync_file->file);
+ if (!sync_file)
+ return -EINVAL;
- *p_fd = fd;
+ *p_fd = fd_stage(fd, sync_file->file);
return 0;
-err_put_fd:
- put_unused_fd(fd);
- return ret;
}
/**
* drm_syncobj_open - initializes syncobj file-private structures at devnode open time
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c b/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c
index 1a77a09b3377e..174f1d66d9ad3 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c
@@ -420,7 +420,7 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
struct etnaviv_gpu *gpu;
struct sync_file *sync_file = NULL;
struct ww_acquire_ctx ticket;
- int out_fence_fd = -1;
+ const struct fd_slot *out_fence_fd = NULL;
struct pid *pid = get_pid(task_pid(current));
void *stream;
int ret;
@@ -504,9 +504,9 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
}
if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
- out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
- if (out_fence_fd < 0) {
- ret = out_fence_fd;
+ out_fence_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(out_fence_fd)) {
+ ret = PTR_ERR(out_fence_fd);
goto err_submit_cmds;
}
}
@@ -607,10 +607,10 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
*/
goto err_submit_put;
}
- fd_install(out_fence_fd, sync_file->file);
+ fd_stage(out_fence_fd, sync_file->file);
}
- args->fence_fd = out_fence_fd;
+ args->fence_fd = out_fence_fd ? fd_prepare_fd(out_fence_fd) : -1;
args->fence = submit->out_fence_id;
err_submit_job:
@@ -623,8 +623,6 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
ww_acquire_fini(&ticket);
err_submit_cmds:
- if (ret && (out_fence_fd >= 0))
- put_unused_fd(out_fence_fd);
kvfree(stream);
kvfree(bos);
kvfree(relocs);
diff --git a/drivers/gpu/drm/msm/msm_gem_submit.c b/drivers/gpu/drm/msm/msm_gem_submit.c
index 5862db05297a7..4ddfd07c1480d 100644
--- a/drivers/gpu/drm/msm/msm_gem_submit.c
+++ b/drivers/gpu/drm/msm/msm_gem_submit.c
@@ -558,7 +558,7 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
struct drm_syncobj **syncobjs_to_reset = NULL;
struct sync_file *sync_file = NULL;
unsigned cmds_to_parse;
- int out_fence_fd = -1;
+ const struct fd_slot *out_fence_fd = NULL;
unsigned i;
int ret;
@@ -601,9 +601,9 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
ring = gpu->rb[queue->ring_nr];
if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
- out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
- if (out_fence_fd < 0) {
- ret = out_fence_fd;
+ out_fence_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(out_fence_fd)) {
+ ret = PTR_ERR(out_fence_fd);
goto out_post_unlock;
}
}
@@ -777,6 +777,8 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
sync_file = sync_file_create(submit->user_fence);
if (!sync_file)
ret = -ENOMEM;
+ else
+ fd_stage(out_fence_fd, sync_file->file);
}
if (ret)
@@ -814,15 +816,9 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
out_unlock:
mutex_unlock(&queue->lock);
out_post_unlock:
- if (ret) {
- if (out_fence_fd >= 0)
- put_unused_fd(out_fence_fd);
- if (sync_file)
- fput(sync_file->file);
- } else if (sync_file) {
- fd_install(out_fence_fd, sync_file->file);
- args->fence_fd = out_fence_fd;
- }
+ /* A staged sync file and its descriptor are dropped on return. */
+ if (!ret && sync_file)
+ args->fence_fd = fd_prepare_fd(out_fence_fd);
if (!IS_ERR_OR_NULL(submit)) {
msm_gem_submit_put(submit);
diff --git a/drivers/gpu/drm/msm/msm_gem_vma.c b/drivers/gpu/drm/msm/msm_gem_vma.c
index c11d021581e03..77d3655667322 100644
--- a/drivers/gpu/drm/msm/msm_gem_vma.c
+++ b/drivers/gpu/drm/msm/msm_gem_vma.c
@@ -1441,7 +1441,7 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
struct drm_syncobj **syncobjs_to_reset = NULL;
struct sync_file *sync_file = NULL;
struct dma_fence *fence;
- int out_fence_fd = -1;
+ const struct fd_slot *out_fence_fd = NULL;
int ret, nr_bos = 0;
unsigned i;
@@ -1479,9 +1479,9 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
}
if (args->flags & MSM_VM_BIND_FENCE_FD_OUT) {
- out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
- if (out_fence_fd < 0) {
- ret = out_fence_fd;
+ out_fence_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(out_fence_fd)) {
+ ret = PTR_ERR(out_fence_fd);
goto out_post_unlock;
}
}
@@ -1565,6 +1565,8 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
sync_file = sync_file_create(job->fence);
if (!sync_file)
ret = -ENOMEM;
+ else
+ fd_stage(out_fence_fd, sync_file->file);
}
if (ret)
@@ -1593,15 +1595,9 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
out_unlock:
mutex_unlock(&queue->lock);
out_post_unlock:
- if (ret) {
- if (out_fence_fd >= 0)
- put_unused_fd(out_fence_fd);
- if (sync_file)
- fput(sync_file->file);
- } else if (sync_file) {
- fd_install(out_fence_fd, sync_file->file);
- args->fence_fd = out_fence_fd;
- }
+ /* A staged sync file and its descriptor are dropped on return. */
+ if (!ret && sync_file)
+ args->fence_fd = fd_prepare_fd(out_fence_fd);
if (!IS_ERR_OR_NULL(job)) {
if (ret)
diff --git a/drivers/gpu/drm/msm/msm_perfcntr.c b/drivers/gpu/drm/msm/msm_perfcntr.c
index ce65b1160955e..65c067d39ca0a 100644
--- a/drivers/gpu/drm/msm/msm_perfcntr.c
+++ b/drivers/gpu/drm/msm/msm_perfcntr.c
@@ -543,8 +543,8 @@ msm_ioctl_perfcntr_config(struct drm_device *dev, void *data, struct drm_file *f
FD_PREPARE(fdf, O_CLOEXEC,
anon_inode_getfile("[msm_perfcntrs]", &stream_fops, stream, 0));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
INIT_WORK(&stream->sel_work, sel_worker);
kthread_init_work(&stream->sample_work, sample_worker);
@@ -564,7 +564,7 @@ msm_ioctl_perfcntr_config(struct drm_device *dev, void *data, struct drm_file *f
msm_perfcntr_resume_locked(perfcntrs->stream);
- stream_fd = fd_publish(fdf);
+ stream_fd = fd_prepare_fd(fdf);
} else {
kfree(ctx->perfctx);
ctx->perfctx = no_free_ptr(perfctx);
diff --git a/drivers/gpu/drm/virtio/virtgpu_submit.c b/drivers/gpu/drm/virtio/virtgpu_submit.c
index 32cb1e4aa4258..51b224acb72b0 100644
--- a/drivers/gpu/drm/virtio/virtgpu_submit.c
+++ b/drivers/gpu/drm/virtio/virtgpu_submit.c
@@ -39,7 +39,7 @@ struct virtio_gpu_submit {
struct virtio_gpu_device *vgdev;
struct sync_file *sync_file;
struct drm_file *file;
- int out_fence_fd;
+ const struct fd_slot *out_fence_fd;
u64 fence_ctx;
u32 ring_idx;
void *buf;
@@ -340,14 +340,8 @@ static void virtio_gpu_cleanup_submit(struct virtio_gpu_submit *submit)
if (submit->buflist)
virtio_gpu_array_put_free(submit->buflist);
- if (submit->out_fence_fd >= 0)
- put_unused_fd(submit->out_fence_fd);
-
if (submit->out_fence)
dma_fence_put(&submit->out_fence->f);
-
- if (submit->sync_file)
- fput(submit->sync_file->file);
}
static void virtio_gpu_submit(struct virtio_gpu_submit *submit)
@@ -363,7 +357,7 @@ static void virtio_gpu_complete_submit(struct virtio_gpu_submit *submit)
submit->buf = NULL;
submit->buflist = NULL;
submit->sync_file = NULL;
- submit->out_fence_fd = -1;
+ submit->out_fence_fd = NULL;
}
static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,
@@ -405,7 +399,7 @@ static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,
submit->out_fence = out_fence;
submit->fence_ctx = fence_ctx;
submit->ring_idx = ring_idx;
- submit->out_fence_fd = -1;
+ submit->out_fence_fd = NULL;
submit->vfpriv = vfpriv;
submit->vgdev = vgdev;
submit->exbuf = exbuf;
@@ -420,15 +414,15 @@ static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,
return PTR_ERR(submit->buf);
if (exbuf->flags & VIRTGPU_EXECBUF_FENCE_FD_OUT) {
- err = get_unused_fd_flags(O_CLOEXEC);
- if (err < 0)
- return err;
-
- submit->out_fence_fd = err;
+ submit->out_fence_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(submit->out_fence_fd))
+ return PTR_ERR(submit->out_fence_fd);
submit->sync_file = sync_file_create(&out_fence->f);
if (!submit->sync_file)
return -ENOMEM;
+
+ fd_stage(submit->out_fence_fd, submit->sync_file->file);
}
return 0;
@@ -456,12 +450,10 @@ static int virtio_gpu_wait_in_fence(struct virtio_gpu_submit *submit)
return ret;
}
-static void virtio_gpu_install_out_fence_fd(struct virtio_gpu_submit *submit)
+static void virtio_gpu_report_out_fence_fd(struct virtio_gpu_submit *submit)
{
- if (submit->sync_file) {
- submit->exbuf->fence_fd = submit->out_fence_fd;
- fd_install(submit->out_fence_fd, submit->sync_file->file);
- }
+ if (submit->sync_file)
+ submit->exbuf->fence_fd = fd_prepare_fd(submit->out_fence_fd);
}
static int virtio_gpu_lock_buflist(struct virtio_gpu_submit *submit)
@@ -534,7 +526,7 @@ int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
* Set up user-out data after submitting the job to optimize
* the job submission path.
*/
- virtio_gpu_install_out_fence_fd(&submit);
+ virtio_gpu_report_out_fence_fd(&submit);
virtio_gpu_process_post_deps(&submit);
virtio_gpu_complete_submit(&submit);
cleanup:
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c b/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c
index a9136a6523cba..9daa74a134855 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c
@@ -4109,15 +4109,17 @@ int vmw_execbuf_process(struct drm_file *file_priv,
uint32_t handle = 0;
int ret;
int32_t out_fence_fd = -1;
+ const struct fd_slot *out_fd = NULL;
struct sync_file *sync_file = NULL;
DECLARE_VAL_CONTEXT(val_ctx, sw_context, 1);
if (flags & DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {
- out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
- if (out_fence_fd < 0) {
+ out_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(out_fd)) {
VMW_DEBUG_USER("Failed to get a fence fd.\n");
- return out_fence_fd;
+ return PTR_ERR(out_fd);
}
+ out_fence_fd = fd_prepare_fd(out_fd);
}
if (throttle_us) {
@@ -4256,28 +4258,21 @@ int vmw_execbuf_process(struct drm_file *file_priv,
sync_file = sync_file_create(&fence->base);
if (!sync_file) {
VMW_DEBUG_USER("Sync file create failed for fence\n");
- put_unused_fd(out_fence_fd);
+ /* The reserved descriptor is released on return. */
out_fence_fd = -1;
(void) vmw_fence_obj_wait(fence, false, false,
VMW_FENCE_WAIT_TIMEOUT);
+ } else {
+ /* Link the fence with the FD created earlier */
+ fd_stage(out_fd, sync_file->file);
}
}
+ /* A staged sync file is dropped with the ioctl on error. */
ret = vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
user_fence_rep, fence, handle, out_fence_fd);
- if (sync_file) {
- if (ret) {
- /* usercopy of fence failed, put the file object */
- fput(sync_file->file);
- put_unused_fd(out_fence_fd);
- } else {
- /* Link the fence with the FD created earlier */
- fd_install(out_fence_fd, sync_file->file);
- }
- }
-
/* Don't unreference when handing fence out */
if (unlikely(out_fence != NULL)) {
*out_fence = fence;
@@ -4323,9 +4318,7 @@ int vmw_execbuf_process(struct drm_file *file_priv,
if (header)
vmw_cmdbuf_header_free(header);
out_free_fence_fd:
- if (out_fence_fd >= 0)
- put_unused_fd(out_fence_fd);
-
+ /* A reserved descriptor is released on return. */
return ret;
}
diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
index cc2cfce2aefdb..c95cee0bae856 100644
--- a/drivers/hv/mshv_root_main.c
+++ b/drivers/hv/mshv_root_main.c
@@ -1072,8 +1072,6 @@ mshv_partition_ioctl_create_vp(struct mshv_partition *partition,
struct mshv_vp *vp;
struct page *intercept_msg_page, *register_page, *ghcb_page;
struct hv_stats_page *stats_pages[2];
- struct file *file;
- int fd;
long ret;
if (copy_from_user(&args, arg, sizeof(args)))
@@ -1148,19 +1146,14 @@ mshv_partition_ioctl_create_vp(struct mshv_partition *partition,
if (ret)
goto put_partition;
- fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
- if (fd < 0) {
- ret = fd;
+ FD_PREPARE(fdf, O_RDWR | O_CLOEXEC,
+ anon_inode_getfile("mshv_vp", &mshv_vp_fops, vp,
+ O_RDWR | O_CLOEXEC));
+ if (IS_ERR(fdf)) {
+ ret = PTR_ERR(fdf);
goto remove_debugfs_vp;
}
- file = anon_inode_getfile("mshv_vp", &mshv_vp_fops, vp,
- O_RDWR | O_CLOEXEC);
- if (IS_ERR(file)) {
- ret = PTR_ERR(file);
- goto put_unused_vp_fd;
- }
-
/* already exclusive with the partition mutex for all ioctls */
partition->pt_vp_count++;
/*
@@ -1171,17 +1164,11 @@ mshv_partition_ioctl_create_vp(struct mshv_partition *partition,
*/
smp_store_release(&partition->pt_vp_array[args.vp_index], vp);
- /*
- * fd_install() is the userspace-visibility commit point. Must be the
- * last operation that can fail or be observed.
- */
- fd_install(fd, file);
- ret = fd;
+ /* The syscall exit installs the file; nothing after this can fail. */
+ ret = fd_prepare_fd(fdf);
goto out;
-put_unused_vp_fd:
- put_unused_fd(fd);
remove_debugfs_vp:
mshv_debugfs_vp_remove(vp);
put_partition:
diff --git a/drivers/hv/mshv_vtl_main.c b/drivers/hv/mshv_vtl_main.c
index 6e3c11c681717..f14203d76da1e 100644
--- a/drivers/hv/mshv_vtl_main.c
+++ b/drivers/hv/mshv_vtl_main.c
@@ -114,29 +114,19 @@ static long
mshv_ioctl_create_vtl(void __user *user_arg, struct device *module_dev)
{
struct mshv_vtl *vtl;
- struct file *file;
- int fd;
vtl = kzalloc_obj(*vtl);
if (!vtl)
return -ENOMEM;
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0) {
- kfree(vtl);
- return fd;
- }
- file = anon_inode_getfile("mshv_vtl", &mshv_vtl_fops,
- vtl, O_RDWR);
- if (IS_ERR(file)) {
- put_unused_fd(fd);
+ FD_PREPARE(fdf, O_CLOEXEC,
+ anon_inode_getfile("mshv_vtl", &mshv_vtl_fops, vtl, O_RDWR));
+ if (IS_ERR(fdf)) {
kfree(vtl);
- return PTR_ERR(file);
+ return PTR_ERR(fdf);
}
vtl->module_dev = module_dev;
- fd_install(fd, file);
-
- return fd;
+ return fd_prepare_fd(fdf);
}
static long
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 2c9ec93dff475..9fb15bf82d0ad 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -2041,7 +2041,7 @@ static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg
int __user *ival = (int __user *)arg;
struct iio_dev_buffer_pair *ib;
struct iio_buffer *buffer;
- int fd, idx, ret;
+ int idx, ret, fdno;
if (copy_from_user(&idx, ival, sizeof(idx)))
return -EFAULT;
@@ -2067,26 +2067,18 @@ static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg
ib->indio_dev = indio_dev;
ib->buffer = buffer;
- fd = anon_inode_getfd("iio:buffer", &iio_buffer_chrdev_fileops,
- ib, O_RDWR | O_CLOEXEC);
- if (fd < 0) {
- ret = fd;
+ FD_PREPARE(fdf, O_RDWR | O_CLOEXEC,
+ anon_inode_getfile("iio:buffer", &iio_buffer_chrdev_fileops,
+ ib, O_RDWR | O_CLOEXEC));
+ if (IS_ERR(fdf)) {
+ ret = PTR_ERR(fdf);
goto error_free_ib;
}
- if (copy_to_user(ival, &fd, sizeof(fd))) {
- /*
- * "Leak" the fd, as there's not much we can do about this
- * anyway. 'fd' might have been closed already, as
- * anon_inode_getfd() called fd_install() on it, which made
- * it reachable by userland.
- *
- * Instead of allowing a malicious user to play tricks with
- * us, rely on the process exit path to do any necessary
- * cleanup, as in releasing the file, if still needed.
- */
+ fdno = fd_prepare_fd(fdf);
+ /* The staged file is dropped with its descriptor if this faults. */
+ if (copy_to_user(ival, &fdno, sizeof(fdno)))
return -EFAULT;
- }
return 0;
diff --git a/drivers/iommu/iommufd/eventq.c b/drivers/iommu/iommufd/eventq.c
index 747dd5155121c..f5fdc6cdf0a11 100644
--- a/drivers/iommu/iommufd/eventq.c
+++ b/drivers/iommu/iommufd/eventq.c
@@ -412,9 +412,9 @@ static int iommufd_eventq_fops_release(struct inode *inode, struct file *filep)
.release = iommufd_eventq_fops_release, \
})
-static int iommufd_eventq_init(struct iommufd_eventq *eventq, char *name,
- struct iommufd_ctx *ictx,
- const struct file_operations *fops)
+static const struct fd_slot *iommufd_eventq_init(struct iommufd_eventq *eventq, char *name,
+ struct iommufd_ctx *ictx,
+ const struct file_operations *fops)
{
struct file *filep;
@@ -425,14 +425,14 @@ static int iommufd_eventq_init(struct iommufd_eventq *eventq, char *name,
/* The filep is fput() by the core code during failure */
filep = anon_inode_getfile(name, fops, eventq, O_RDWR);
if (IS_ERR(filep))
- return PTR_ERR(filep);
+ return ERR_CAST(filep);
eventq->ictx = ictx;
iommufd_ctx_get(eventq->ictx);
eventq->filep = filep;
refcount_inc(&eventq->obj.users);
- return get_unused_fd_flags(O_CLOEXEC);
+ return fd_prepare(O_CLOEXEC);
}
static const struct file_operations iommufd_fault_fops =
@@ -442,7 +442,7 @@ int iommufd_fault_alloc(struct iommufd_ucmd *ucmd)
{
struct iommu_fault_alloc *cmd = ucmd->cmd;
struct iommufd_fault *fault;
- int fdno;
+ const struct fd_slot *fdno;
int rc;
if (cmd->flags)
@@ -458,22 +458,19 @@ int iommufd_fault_alloc(struct iommufd_ucmd *ucmd)
fdno = iommufd_eventq_init(&fault->common, "[iommufd-pgfault]",
ucmd->ictx, &iommufd_fault_fops);
- if (fdno < 0)
- return fdno;
+ if (IS_ERR(fdno))
+ return PTR_ERR(fdno);
cmd->out_fault_id = fault->common.obj.id;
- cmd->out_fault_fd = fdno;
+ cmd->out_fault_fd = fd_prepare_fd(fdno);
rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
if (rc)
- goto out_put_fdno;
+ return rc;
- fd_install(fdno, fault->common.filep);
+ fd_stage(fdno, fault->common.filep);
return 0;
-out_put_fdno:
- put_unused_fd(fdno);
- return rc;
}
int iommufd_fault_iopf_handler(struct iopf_group *group)
@@ -506,7 +503,7 @@ int iommufd_veventq_alloc(struct iommufd_ucmd *ucmd)
struct iommu_veventq_alloc *cmd = ucmd->cmd;
struct iommufd_veventq *veventq;
struct iommufd_viommu *viommu;
- int fdno;
+ const struct fd_slot *fdno;
int rc;
if (cmd->flags || cmd->__reserved ||
@@ -543,24 +540,22 @@ int iommufd_veventq_alloc(struct iommufd_ucmd *ucmd)
fdno = iommufd_eventq_init(&veventq->common, "[iommufd-viommu-event]",
ucmd->ictx, &iommufd_veventq_fops);
- if (fdno < 0) {
- rc = fdno;
+ if (IS_ERR(fdno)) {
+ rc = PTR_ERR(fdno);
goto out_abort;
}
cmd->out_veventq_id = veventq->common.obj.id;
- cmd->out_veventq_fd = fdno;
+ cmd->out_veventq_fd = fd_prepare_fd(fdno);
rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
if (rc)
- goto out_put_fdno;
+ goto out_abort;
iommufd_object_finalize(ucmd->ictx, &veventq->common.obj);
- fd_install(fdno, veventq->common.filep);
+ fd_stage(fdno, veventq->common.filep);
goto out_unlock_veventqs;
-out_put_fdno:
- put_unused_fd(fdno);
out_abort:
iommufd_object_abort_and_destroy(ucmd->ictx, &veventq->common.obj);
out_unlock_veventqs:
diff --git a/drivers/media/mc/mc-request.c b/drivers/media/mc/mc-request.c
index 13e77648807c2..c9296bdbee271 100644
--- a/drivers/media/mc/mc-request.c
+++ b/drivers/media/mc/mc-request.c
@@ -316,8 +316,8 @@ int media_request_alloc(struct media_device *mdev, int *alloc_fd)
FD_PREPARE(fdf, O_CLOEXEC,
anon_inode_getfile("request", &request_fops, NULL,
O_CLOEXEC));
- if (fdf.err) {
- ret = fdf.err;
+ if (IS_ERR(fdf)) {
+ ret = PTR_ERR(fdf);
goto err_free_req;
}
@@ -328,7 +328,7 @@ int media_request_alloc(struct media_device *mdev, int *alloc_fd)
atomic_inc(&mdev->num_requests);
dev_dbg(mdev->dev, "request: allocated %s\n", req->debug_str);
- *alloc_fd = fd_publish(fdf);
+ *alloc_fd = fd_prepare_fd(fdf);
return 0;
diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index 90fd669636ec1..24b84019bbd79 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -7,6 +7,7 @@
#include <linux/dma-buf.h>
#include <linux/dma-mapping.h>
#include <linux/dma-resv.h>
+#include <linux/file.h>
#include <linux/idr.h>
#include <linux/list.h>
#include <linux/miscdevice.h>
@@ -1804,6 +1805,7 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
struct fastrpc_buf *buf = NULL;
int err;
+ const struct fd_slot *fd;
if (copy_from_user(&bp, argp, sizeof(bp)))
return -EFAULT;
@@ -1822,23 +1824,16 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
return err;
}
- bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
- if (bp.fd < 0) {
+ fd = fd_prepare(O_ACCMODE);
+ if (IS_ERR(fd)) {
dma_buf_put(buf->dmabuf);
return -EINVAL;
}
+ bp.fd = fd_stage(fd, buf->dmabuf->file);
- if (copy_to_user(argp, &bp, sizeof(bp))) {
- /*
- * The usercopy failed, but we can't do much about it, as
- * dma_buf_fd() already called fd_install() and made the
- * file descriptor accessible for the current process. It
- * might already be closed and dmabuf no longer valid when
- * we reach this point. Therefore "leak" the fd and rely on
- * the process exit path to do any required cleanup.
- */
+ /* The staged file is dropped with its descriptor if this faults. */
+ if (copy_to_user(argp, &bp, sizeof(bp)))
return -EFAULT;
- }
return 0;
}
diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c
index 4a805919bb0c5..721b0d9f0e1f4 100644
--- a/drivers/misc/ntsync.c
+++ b/drivers/misc/ntsync.c
@@ -724,10 +724,10 @@ static int ntsync_obj_get_fd(struct ntsync_obj *obj)
{
FD_PREPARE(fdf, O_CLOEXEC,
anon_inode_getfile("ntsync", &ntsync_obj_fops, obj, O_RDWR));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
obj->file = fd_prepare_file(fdf);
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp)
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 423ead48aafe3..93d9d21f4074a 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -903,28 +903,17 @@ EXPORT_SYMBOL_GPL(vfio_mig_get_next_state);
static int vfio_ioct_mig_return_fd(struct file *filp, void __user *arg,
struct vfio_device_feature_mig_state *mig)
{
- int ret;
- int fd;
+ FD_PREPARE(fdf, O_CLOEXEC, filp);
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0) {
- ret = fd;
- goto out_fput;
+ if (IS_ERR(fdf)) {
+ fput(filp);
+ return PTR_ERR(fdf);
}
- mig->data_fd = fd;
- if (copy_to_user(arg, mig, sizeof(*mig))) {
- ret = -EFAULT;
- goto out_put_unused;
- }
- fd_install(fd, filp);
+ mig->data_fd = fd_prepare_fd(fdf);
+ if (copy_to_user(arg, mig, sizeof(*mig)))
+ return -EFAULT;
return 0;
-
-out_put_unused:
- put_unused_fd(fd);
-out_fput:
- fput(filp);
- return ret;
}
static int
diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
index c91300a73f502..93c3f00f54433 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
@@ -1589,7 +1589,7 @@ static const struct file_operations ne_enclave_fops = {
static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_uid)
{
struct ne_pci_dev_cmd_reply cmd_reply = {};
- int enclave_fd = -1;
+ const struct fd_slot *enclave_fd = NULL;
struct file *enclave_file = NULL;
unsigned int i = 0;
struct ne_enclave *ne_enclave = NULL;
@@ -1647,9 +1647,9 @@ static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_ui
goto free_cpumask;
}
- enclave_fd = get_unused_fd_flags(O_CLOEXEC);
- if (enclave_fd < 0) {
- rc = enclave_fd;
+ enclave_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(enclave_fd)) {
+ rc = PTR_ERR(enclave_fd);
dev_err_ratelimited(ne_misc_dev.this_device,
"Error in getting unused fd [rc=%d]\n", rc);
@@ -1664,7 +1664,7 @@ static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_ui
dev_err_ratelimited(ne_misc_dev.this_device,
"Error in anon inode get file [rc=%d]\n", rc);
- goto put_fd;
+ goto free_cpumask;
}
rc = ne_do_request(pdev, SLOT_ALLOC,
@@ -1688,27 +1688,20 @@ static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_ui
list_add(&ne_enclave->enclave_list_entry, &ne_pci_dev->enclaves_list);
- if (copy_to_user(slot_uid, &ne_enclave->slot_uid, sizeof(ne_enclave->slot_uid))) {
- /*
- * As we're holding the only reference to 'enclave_file', fput()
- * will call ne_enclave_release() which will do a proper cleanup
- * of all so far allocated resources, leaving only the unused fd
- * for us to free.
- */
- fput(enclave_file);
- put_unused_fd(enclave_fd);
+ fd_stage(enclave_fd, enclave_file);
+ /*
+ * The failed ioctl drops the descriptor and with it the only reference
+ * to 'enclave_file', so ne_enclave_release() does a proper cleanup of
+ * all so far allocated resources.
+ */
+ if (copy_to_user(slot_uid, &ne_enclave->slot_uid, sizeof(ne_enclave->slot_uid)))
return -EFAULT;
- }
-
- fd_install(enclave_fd, enclave_file);
- return enclave_fd;
+ return fd_prepare_fd(enclave_fd);
put_file:
fput(enclave_file);
-put_fd:
- put_unused_fd(enclave_fd);
free_cpumask:
free_cpumask_var(ne_enclave->vcpu_ids);
for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
diff --git a/drivers/xen/gntdev-dmabuf.c b/drivers/xen/gntdev-dmabuf.c
index 83b0df4608941..a3ecc7c839741 100644
--- a/drivers/xen/gntdev-dmabuf.c
+++ b/drivers/xen/gntdev-dmabuf.c
@@ -357,10 +357,10 @@ static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)
{
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
struct gntdev_dmabuf *gntdev_dmabuf __free(kfree) = NULL;
- CLASS(get_unused_fd, ret)(O_CLOEXEC);
+ const struct fd_slot *fd = fd_prepare(O_CLOEXEC);
- if (ret < 0)
- return ret;
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
gntdev_dmabuf = kzalloc_obj(*gntdev_dmabuf);
if (!gntdev_dmabuf)
@@ -388,17 +388,17 @@ static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)
if (IS_ERR(gntdev_dmabuf->dmabuf))
return PTR_ERR(gntdev_dmabuf->dmabuf);
- gntdev_dmabuf->fd = ret;
- args->fd = ret;
+ gntdev_dmabuf->fd = fd_prepare_fd(fd);
+ args->fd = fd_prepare_fd(fd);
- pr_debug("Exporting DMA buffer with fd %d\n", ret);
+ pr_debug("Exporting DMA buffer with fd %d\n", fd_prepare_fd(fd));
get_file(gntdev_dmabuf->priv->filp);
mutex_lock(&args->dmabuf_priv->lock);
list_add(&gntdev_dmabuf->next, &args->dmabuf_priv->exp_list);
mutex_unlock(&args->dmabuf_priv->lock);
- fd_install(take_fd(ret), no_free_ptr(gntdev_dmabuf)->dmabuf->file);
+ fd_stage(fd, no_free_ptr(gntdev_dmabuf)->dmabuf->file);
return 0;
}
diff --git a/fs/eventfd.c b/fs/eventfd.c
index 9d33a02757d52..a00fea879f9a5 100644
--- a/fs/eventfd.c
+++ b/fs/eventfd.c
@@ -403,12 +403,12 @@ static int do_eventfd(unsigned int count, int flags)
FD_PREPARE(fdf, flags,
anon_inode_getfile_fmode("[eventfd]", &eventfd_fops, ctx,
flags, FMODE_NOWAIT));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
ctx->id = ida_alloc(&eventfd_ida, GFP_KERNEL);
retain_and_null_ptr(ctx);
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
diff --git a/fs/eventpoll.c b/fs/eventpoll.c
index e0c4bf88a8383..81857725f8108 100644
--- a/fs/eventpoll.c
+++ b/fs/eventpoll.c
@@ -2514,12 +2514,12 @@ static int do_epoll_create(int flags)
FD_PREPARE(fdf, O_RDWR | (flags & O_CLOEXEC),
anon_inode_getfile("[eventpoll]", &eventpoll_fops, ep,
O_RDWR | (flags & O_CLOEXEC)));
- if (fdf.err) {
+ if (IS_ERR(fdf)) {
ep_clear_and_put(ep);
- return fdf.err;
+ return PTR_ERR(fdf);
}
ep->file = fd_prepare_file(fdf);
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
SYSCALL_DEFINE1(epoll_create1, int, flags)
diff --git a/fs/exec.c b/fs/exec.c
index 745f6eb5279e6..1aa0f7b99fc37 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1159,6 +1159,9 @@ int begin_new_exec(struct linux_binprm * bprm)
*/
io_uring_task_cancel();
+ /* A prepared descriptor would be installed into the new program. */
+ VFS_WARN_ON_ONCE(me->fd_slots.nr);
+
/* Ensure the files table is not shared. */
retval = unshare_files();
if (retval)
diff --git a/fs/file.c b/fs/file.c
index 628ca07dc4b17..7c05246a5129b 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -23,6 +23,7 @@
#include <linux/file_ref.h>
#include <net/sock.h>
#include <linux/init_task.h>
+#include <asm/syscall.h>
#include "internal.h"
@@ -630,14 +631,236 @@ static void __put_unused_fd(struct files_struct *files, unsigned int fd)
files->next_fd = fd;
}
-void put_unused_fd(unsigned int fd)
+/* Release @fd in the table, its slot is the caller's business. */
+static void fd_release(unsigned int fd)
{
struct files_struct *files = current->files;
+
spin_lock(&files->file_lock);
__put_unused_fd(files, fd);
spin_unlock(&files->file_lock);
}
+/* Enough for SCM_MAX_FD, and a page of slots on 4K pages. */
+#define FD_SLOTS_SPILL_MIN 256
+
+/* Make the syscall exit path call fd_slots_commit(). */
+#if defined(CONFIG_GENERIC_ENTRY)
+#define fd_slots_set_work() set_syscall_work(FD_SLOTS)
+#define fd_slots_clear_work() clear_syscall_work(FD_SLOTS)
+#elif defined(TIF_FD_SLOTS)
+#define fd_slots_set_work() set_thread_flag(TIF_FD_SLOTS)
+#define fd_slots_clear_work() clear_thread_flag(TIF_FD_SLOTS)
+#else
+#error "the syscall exit path must commit prepared file descriptors"
+#endif
+
+static struct fd_slot *fd_slot(struct fd_slots *slots, unsigned int idx)
+{
+ if (idx < FD_SLOTS_INLINE)
+ return &slots->inline_slots[idx];
+ return &slots->spill[idx - FD_SLOTS_INLINE];
+}
+
+/* Room for slot @idx in the spill array, doubling as it fills up. */
+static noinline struct fd_slot *fd_slots_spill(struct fd_slots *slots,
+ unsigned int idx)
+{
+ unsigned int max = slots->spill_max;
+ struct fd_slot *spill;
+
+ idx -= FD_SLOTS_INLINE;
+ if (idx < max)
+ return &slots->spill[idx];
+
+ max = max ? max * 2 : FD_SLOTS_SPILL_MIN;
+ spill = kvrealloc(slots->spill, array_size(max, sizeof(*spill)),
+ GFP_KERNEL_ACCOUNT);
+ if (!spill)
+ return NULL;
+ slots->spill = spill;
+ slots->spill_max = max;
+ return &spill[idx];
+}
+
+/* Record @fd in the next slot, returns the slot. */
+static struct fd_slot *fd_slot_record(int fd)
+{
+ struct fd_slots *slots = ¤t->fd_slots;
+ unsigned int idx = slots->nr;
+ struct fd_slot *slot;
+
+ /* Nothing would ever commit what a kernel thread prepares. */
+ VFS_WARN_ON_ONCE(current->flags & PF_KTHREAD);
+
+ if (likely(idx < FD_SLOTS_INLINE)) {
+ slot = &slots->inline_slots[idx];
+ } else {
+ slot = fd_slots_spill(slots, idx);
+ if (!slot)
+ return ERR_PTR(-ENOMEM);
+ }
+ ACCESS_PRIVATE(slot, fd) = fd;
+ ACCESS_PRIVATE(slot, file) = NULL;
+ if (!idx)
+ fd_slots_set_work();
+ slots->nr = idx + 1;
+ return slot;
+}
+
+/* The slot holding @fd, if this syscall prepared it. */
+static inline int fd_slot_find(struct fd_slots *slots, unsigned int fd)
+{
+ unsigned int idx = slots->nr;
+
+ while (idx--) {
+ if (ACCESS_PRIVATE(fd_slot(slots, idx), fd) == fd)
+ return idx;
+ }
+ return -1;
+}
+
+/**
+ * fd_prepare - allocate a descriptor that the syscall exit installs
+ * @flags: O_CLOEXEC or 0
+ *
+ * Returns the prepared slot as a const handle or an error pointer.
+ */
+const struct fd_slot *fd_prepare(unsigned flags)
+{
+ struct fd_slot *slot;
+ int fd;
+
+ fd = get_unused_fd_flags(flags);
+ if (fd < 0)
+ return ERR_PTR(fd);
+
+ slot = fd_slot_record(fd);
+ if (IS_ERR(slot))
+ fd_release(fd);
+ return slot;
+}
+EXPORT_SYMBOL(fd_prepare);
+
+/**
+ * fd_stage - attach the file to a prepared slot
+ * @slot: slot from fd_prepare()
+ * @file: the file to install, consumed
+ *
+ * Returns the number. The syscall exit installs @file there when the syscall
+ * returns success and drops it when the syscall returns an error.
+ */
+int fd_stage(const struct fd_slot *slot, struct file *file)
+{
+ struct fd_slot *s = (struct fd_slot *)slot;
+
+ VFS_WARN_ON_ONCE(ACCESS_PRIVATE(s, file));
+ ACCESS_PRIVATE(s, file) = file;
+ return ACCESS_PRIVATE(s, fd);
+}
+EXPORT_SYMBOL(fd_stage);
+
+/**
+ * fd_prepare_fd - the descriptor number of a prepared slot
+ * @slot: slot from fd_prepare()
+ */
+int fd_prepare_fd(const struct fd_slot *slot)
+{
+ return ACCESS_PRIVATE(slot, fd);
+}
+EXPORT_SYMBOL(fd_prepare_fd);
+
+/**
+ * fd_prepare_file - the file staged into a slot, to configure before install
+ * @slot: slot from fd_prepare()
+ *
+ * Returns the file handed to fd_stage(), or NULL before one is staged.
+ */
+struct file *fd_prepare_file(const struct fd_slot *slot)
+{
+ return ACCESS_PRIVATE(slot, file);
+}
+EXPORT_SYMBOL(fd_prepare_file);
+
+/* Install every staged file, release the slots that never got one. */
+static void fd_slots_install(struct fd_slots *slots)
+{
+ unsigned int idx;
+
+ for (idx = 0; idx < slots->nr; idx++) {
+ struct fd_slot *slot = fd_slot(slots, idx);
+
+ if (ACCESS_PRIVATE(slot, file))
+ fd_install(ACCESS_PRIVATE(slot, fd),
+ ACCESS_PRIVATE(slot, file));
+ else
+ fd_release(ACCESS_PRIVATE(slot, fd));
+ }
+}
+
+/* Release every slot's descriptor and drop the staged files. */
+static void fd_slots_drop(struct fd_slots *slots)
+{
+ struct files_struct *files = current->files;
+ unsigned int idx;
+
+ spin_lock(&files->file_lock);
+ for (idx = 0; idx < slots->nr; idx++)
+ __put_unused_fd(files, ACCESS_PRIVATE(fd_slot(slots, idx), fd));
+ spin_unlock(&files->file_lock);
+ for (idx = 0; idx < slots->nr; idx++) {
+ struct fd_slot *slot = fd_slot(slots, idx);
+
+ if (ACCESS_PRIVATE(slot, file))
+ fput(ACCESS_PRIVATE(slot, file));
+ }
+}
+
+static __always_inline void fd_slots_finish(struct fd_slots *slots, bool failed)
+{
+ if (likely(!failed))
+ fd_slots_install(slots);
+ else
+ fd_slots_drop(slots);
+ slots->nr = 0;
+ fd_slots_clear_work();
+}
+
+/* Syscall exit hook, keyed on the return value the caller will see. */
+void fd_slots_commit(struct pt_regs *regs)
+{
+ fd_slots_finish(¤t->fd_slots,
+ syscall_get_error(current, regs) != 0);
+}
+
+/* Install or drop the prepared descriptors based on @ret. */
+void __fd_slots_commit(long ret)
+{
+ fd_slots_finish(¤t->fd_slots, IS_ERR_VALUE(ret));
+}
+
+void exit_fd_slots(void)
+{
+ struct fd_slots *slots = ¤t->fd_slots;
+
+ /* A syscall must not exit with prepared descriptors outstanding. */
+ if (WARN_ON_ONCE(slots->nr))
+ fd_slots_finish(slots, true);
+ kvfree(slots->spill);
+}
+
+/**
+ * put_unused_fd - give a descriptor back before it got a file
+ * @fd: descriptor returned by get_unused_fd_flags()
+ *
+ * Not for a prepared descriptor, the syscall exit releases that one.
+ */
+void put_unused_fd(unsigned int fd)
+{
+ VFS_WARN_ON_ONCE(fd_slot_find(¤t->fd_slots, fd) >= 0);
+ fd_release(fd);
+}
+
EXPORT_SYMBOL(put_unused_fd);
/*
@@ -1384,25 +1607,27 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)
*/
int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags)
{
- int error;
+ int fd, error;
error = security_file_receive(file);
if (error)
return error;
- FD_PREPARE(fdf, o_flags, file);
- if (fdf.err)
- return fdf.err;
- get_file(file);
+ fd = get_unused_fd_flags(o_flags);
+ if (fd < 0)
+ return fd;
if (ufd) {
- error = put_user(fd_prepare_fd(fdf), ufd);
- if (error)
+ error = put_user(fd, ufd);
+ if (error) {
+ put_unused_fd(fd);
return error;
+ }
}
- __receive_sock(fd_prepare_file(fdf));
- return fd_publish(fdf);
+ __receive_sock(file);
+ fd_install(fd, get_file(file));
+ return fd;
}
EXPORT_SYMBOL_GPL(receive_fd);
diff --git a/fs/namespace.c b/fs/namespace.c
index 1ecd96c918b33..fba29bcc01646 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -4544,9 +4544,9 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
FD_PREPARE(fdf, (flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0,
dentry_open(&new_path, O_PATH, fc->cred));
- if (fdf.err) {
+ if (IS_ERR(fdf)) {
dissolve_on_fput(new_path.mnt);
- return fdf.err;
+ return PTR_ERR(fdf);
}
/*
@@ -4554,7 +4554,7 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
* need to unmount it, not just simply put it.
*/
fd_prepare_file(fdf)->f_mode |= FMODE_NEED_UNMOUNT;
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
static inline int vfs_move_mount(const struct path *from_path,
@@ -5198,8 +5198,8 @@ SYSCALL_DEFINE5(open_tree_attr, int, dfd, const char __user *, filename,
return -EINVAL;
FD_PREPARE(fdf, flags, vfs_open_tree(dfd, filename, flags));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
if (uattr) {
struct mount_kattr kattr = {};
@@ -5220,7 +5220,7 @@ SYSCALL_DEFINE5(open_tree_attr, int, dfd, const char __user *, filename,
return ret;
}
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
int show_path(struct seq_file *m, struct dentry *root)
diff --git a/fs/nsfs.c b/fs/nsfs.c
index c3b6ae76594a8..da021c8b49b29 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -348,8 +348,8 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
return ret;
FD_PREPARE(fdf, O_CLOEXEC, dentry_open(&path, O_RDONLY, current_cred()));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
/*
* If @uinfo is passed return all information about the
* mount namespace as well.
@@ -357,7 +357,7 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
ret = copy_ns_info_to_user(to_mnt_ns(ns), uinfo, usize, &kinfo);
if (ret)
return ret;
- ret = fd_publish(fdf);
+ ret = fd_prepare_fd(fdf);
break;
}
default:
diff --git a/fs/xfs/xfs_handle.c b/fs/xfs/xfs_handle.c
index 0689cade8f74c..174aa3c8260f6 100644
--- a/fs/xfs/xfs_handle.c
+++ b/fs/xfs/xfs_handle.c
@@ -272,8 +272,8 @@ xfs_open_by_handle(
path.mnt = mntget(parfilp->f_path.mnt);
FD_PREPARE(fdf, 0, dentry_open(&path, hreq->oflags, cred));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
if (S_ISREG(inode->i_mode)) {
struct file *filp = fd_prepare_file(fdf);
@@ -282,7 +282,7 @@ xfs_open_by_handle(
filp->f_mode |= FMODE_NOCMTIME;
}
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
int
diff --git a/include/linux/entry-common.h b/include/linux/entry-common.h
index 6574b7183c017..2da30e50cf596 100644
--- a/include/linux/entry-common.h
+++ b/include/linux/entry-common.h
@@ -3,6 +3,7 @@
#define __LINUX_ENTRYCOMMON_H
#include <linux/audit.h>
+#include <linux/file.h>
#include <linux/irq-entry-common.h>
#include <linux/livepatch.h>
#include <linux/ptrace.h>
@@ -36,7 +37,8 @@
SYSCALL_WORK_SYSCALL_TRACE | \
SYSCALL_WORK_SYSCALL_AUDIT | \
SYSCALL_WORK_SYSCALL_USER_DISPATCH | \
- SYSCALL_WORK_SYSCALL_EXIT_TRAP)
+ SYSCALL_WORK_SYSCALL_EXIT_TRAP | \
+ SYSCALL_WORK_FD_SLOTS)
/**
* arch_ptrace_report_syscall_permit_entry - Architecture specific wrapper for
@@ -245,6 +247,10 @@ static __always_inline void syscall_exit_work(struct pt_regs *regs, unsigned lon
{
bool step;
+ /* Install or drop the descriptors the syscall prepared. */
+ if (work & SYSCALL_WORK_FD_SLOTS)
+ fd_slots_commit(regs);
+
/*
* If the syscall was rolled back due to syscall user dispatching,
* then the tracers below are not invoked for the same reason as
diff --git a/include/linux/file.h b/include/linux/file.h
index 27484b444d315..45d68e097c05e 100644
--- a/include/linux/file.h
+++ b/include/linux/file.h
@@ -91,6 +91,10 @@ extern bool get_close_on_exec(unsigned int fd);
extern int __get_unused_fd_flags(unsigned flags, unsigned long nofile);
extern int get_unused_fd_flags(unsigned flags);
extern void put_unused_fd(unsigned int fd);
+void __fd_slots_commit(long ret);
+struct pt_regs;
+void fd_slots_commit(struct pt_regs *regs);
+void exit_fd_slots(void);
DEFINE_CLASS(get_unused_fd, int, if (_T >= 0) put_unused_fd(_T),
get_unused_fd_flags(flags), unsigned flags)
@@ -118,6 +122,12 @@ DEFINE_FREE(fput, struct file *, if (!IS_ERR_OR_NULL(_T)) fput(_T))
extern void fd_install(unsigned int fd, struct file *file);
+struct fd_slot;
+const struct fd_slot *fd_prepare(unsigned flags);
+int fd_stage(const struct fd_slot *slot, struct file *file);
+int fd_prepare_fd(const struct fd_slot *slot);
+struct file *fd_prepare_file(const struct fd_slot *slot);
+
int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags);
int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags);
@@ -128,128 +138,46 @@ extern void __fput_sync(struct file *);
extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
/*
- * fd_prepare: Combined fd + file allocation cleanup class.
- * @err: Error code to indicate if allocation succeeded.
- * @__fd: Allocated fd (may not be accessed directly)
- * @__file: Allocated struct file pointer (may not be accessed directly)
- *
- * Allocates an fd and a file together. On error paths, automatically cleans
- * up whichever resource was successfully allocated. Allows flexible file
- * allocation with different functions per usage.
- *
- * Do not use directly.
- */
-struct fd_prepare {
- s32 err;
- s32 __fd; /* do not access directly */
- struct file *__file; /* do not access directly */
-};
-
-/* Typedef for fd_prepare cleanup guards. */
-typedef struct fd_prepare class_fd_prepare_t;
-
-/*
- * Accessors for fd_prepare class members.
- * _Generic() is used for zero-cost type safety.
- */
-#define fd_prepare_fd(_fdf) \
- (_Generic((_fdf), struct fd_prepare: (_fdf).__fd))
-
-#define fd_prepare_file(_fdf) \
- (_Generic((_fdf), struct fd_prepare: (_fdf).__file))
-
-/* Do not use directly. */
-static inline void class_fd_prepare_destructor(const struct fd_prepare *fdf)
-{
- if (unlikely(fdf->__fd >= 0))
- put_unused_fd(fdf->__fd);
- if (unlikely(!IS_ERR_OR_NULL(fdf->__file)))
- fput(fdf->__file);
-}
-
-/* Do not use directly. */
-static inline int class_fd_prepare_lock_err(const struct fd_prepare *fdf)
-{
- if (unlikely(fdf->err))
- return fdf->err;
- if (unlikely(fdf->__fd < 0))
- return fdf->__fd;
- if (unlikely(IS_ERR(fdf->__file)))
- return PTR_ERR(fdf->__file);
- if (unlikely(!fdf->__file))
- return -ENOMEM;
- return 0;
-}
-
-/*
- * __FD_PREPARE_INIT - Helper to initialize fd_prepare class.
+ * FD_ADD - allocate a descriptor, build the file and install it in one step.
* @_fd_flags: flags for get_unused_fd_flags()
- * @_file_owned: expression that returns struct file *
- *
- * Returns a struct fd_prepare with fd, file, and err set.
- * If fd allocation fails, fd will be negative and err will be set. If
- * fd succeeds but file_init_expr fails, file will be ERR_PTR and err
- * will be set. The err field is the single source of truth for error
- * checking.
- */
-#define __FD_PREPARE_INIT(_fd_flags, _file_owned) \
- ({ \
- struct fd_prepare fdf = { \
- .__fd = get_unused_fd_flags((_fd_flags)), \
- }; \
- if (likely(fdf.__fd >= 0)) \
- fdf.__file = (_file_owned); \
- fdf.err = ACQUIRE_ERR(fd_prepare, &fdf); \
- fdf; \
- })
-
-/*
- * FD_PREPARE - Macro to declare and initialize an fd_prepare variable.
- *
- * Declares and initializes an fd_prepare variable with automatic
- * cleanup. No separate scope required - cleanup happens when variable
- * goes out of scope.
+ * @_file_owned: struct file to take ownership of (can be an expression)
*
- * @_fdf: name of struct fd_prepare variable to define
- * @_fd_flags: flags for get_unused_fd_flags()
- * @_file_owned: struct file to take ownership of (can be expression)
+ * The file expression is evaluated only after the descriptor is allocated, so
+ * a full table does not run its side effects. Drops the file and returns a
+ * negative errno on failure. Installs immediately: for anything more than a
+ * bare install-and-return (reporting the number, configuring the file,
+ * installing several descriptors) use fd_prepare()/fd_stage().
*/
-#define FD_PREPARE(_fdf, _fd_flags, _file_owned) \
- CLASS_INIT(fd_prepare, _fdf, __FD_PREPARE_INIT(_fd_flags, _file_owned))
+#define FD_ADD(_fd_flags, _file_owned) \
+({ \
+ int __fd = get_unused_fd_flags(_fd_flags); \
+ if (likely(__fd >= 0)) { \
+ struct file *__file = (_file_owned); \
+ if (unlikely(IS_ERR_OR_NULL(__file))) { \
+ put_unused_fd(__fd); \
+ __fd = __file ? PTR_ERR(__file) : -ENOMEM; \
+ } else { \
+ fd_install(__fd, __file); \
+ } \
+ } \
+ __fd; \
+})
/*
- * fd_publish - Publish prepared fd and file to the fd table.
- * @_fdf: struct fd_prepare variable
- */
-#define fd_publish(_fdf) \
- ({ \
- struct fd_prepare *fdp = &(_fdf); \
- VFS_WARN_ON_ONCE(fdp->err); \
- VFS_WARN_ON_ONCE(fdp->__fd < 0); \
- VFS_WARN_ON_ONCE(IS_ERR_OR_NULL(fdp->__file)); \
- fd_install(fdp->__fd, fdp->__file); \
- retain_and_null_ptr(fdp->__file); \
- take_fd(fdp->__fd); \
- })
-
-/* Do not use directly. */
-#define __FD_ADD(_fdf, _fd_flags, _file_owned) \
- ({ \
- FD_PREPARE(_fdf, _fd_flags, _file_owned); \
- s32 ret = _fdf.err; \
- if (likely(!ret)) \
- ret = fd_publish(_fdf); \
- ret; \
- })
-
-/*
- * FD_ADD - Allocate and install an fd and file in one step.
+ * FD_PREPARE - reserve a descriptor and stage @_file_owned on it for the
+ * install at syscall exit; declares @_fdf, an fd_prepare() slot.
+ * @_fdf: name of the const struct fd_slot * to declare
* @_fd_flags: flags for get_unused_fd_flags()
- * @_file_owned: struct file to take ownership of
- *
- * Returns the allocated fd number, or negative error code on failure.
+ * @_file_owned: struct file to take ownership of (can be an expression)
*/
-#define FD_ADD(_fd_flags, _file_owned) \
- __FD_ADD(__UNIQUE_ID(fd_prepare), _fd_flags, _file_owned)
+#define FD_PREPARE(_fdf, _fd_flags, _file_owned) \
+ const struct fd_slot *_fdf = fd_prepare(_fd_flags); \
+ if (!IS_ERR(_fdf)) { \
+ struct file *__file = (_file_owned); \
+ if (unlikely(IS_ERR_OR_NULL(__file))) \
+ _fdf = __file ? ERR_CAST(__file) : ERR_PTR(-ENOMEM); \
+ else \
+ fd_stage(_fdf, __file); \
+ }
#endif /* __LINUX_FILE_H */
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 8b3d47a325cca..52bbf9931908f 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -832,6 +832,24 @@ struct task_ipi_mask {
struct task_ipi_mask { };
#endif
+/* Descriptors this syscall prepared, installed when it returns. */
+#define FD_SLOTS_INLINE 2
+
+struct file;
+
+struct fd_slot {
+ struct file * __private file;
+ int __private fd;
+};
+
+/* Inline up to FD_SLOTS_INLINE slots, the rest in the spill. */
+struct fd_slots {
+ unsigned int nr;
+ unsigned int spill_max;
+ struct fd_slot *spill;
+ struct fd_slot inline_slots[FD_SLOTS_INLINE];
+};
+
struct task_struct {
#ifdef CONFIG_THREAD_INFO_IN_TASK
/*
@@ -1206,6 +1224,9 @@ struct task_struct {
/* Open file information: */
struct files_struct *files;
+ /* Descriptors prepared by the current syscall: */
+ struct fd_slots fd_slots;
+
#ifdef CONFIG_IO_URING
struct io_uring_task *io_uring;
struct io_restriction *io_uring_restrict;
diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
index 307b8390fc670..c80a874442860 100644
--- a/include/linux/thread_info.h
+++ b/include/linux/thread_info.h
@@ -47,6 +47,7 @@ enum syscall_work_bit {
SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH,
SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP,
SYSCALL_WORK_BIT_SYSCALL_RSEQ_SLICE,
+ SYSCALL_WORK_BIT_FD_SLOTS,
};
#define SYSCALL_WORK_SECCOMP BIT(SYSCALL_WORK_BIT_SECCOMP)
@@ -57,6 +58,7 @@ enum syscall_work_bit {
#define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)
#define SYSCALL_WORK_SYSCALL_EXIT_TRAP BIT(SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP)
#define SYSCALL_WORK_SYSCALL_RSEQ_SLICE BIT(SYSCALL_WORK_BIT_SYSCALL_RSEQ_SLICE)
+#define SYSCALL_WORK_FD_SLOTS BIT(SYSCALL_WORK_BIT_FD_SLOTS)
#endif
#include <asm/thread_info.h>
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 61053421d809a..001c3683bf00d 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -870,6 +870,10 @@ bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags)
lockdep_assert(!io_wq_current_is_worker());
lockdep_assert_held(&ctx->uring_lock);
+ /* Descriptors this CQE reports must be installed before it is visible. */
+ if (unlikely(current->fd_slots.nr))
+ __fd_slots_commit(res);
+
if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
spin_lock(&ctx->completion_lock);
posted = io_fill_cqe_aux(ctx, req->cqe.user_data, res, cflags);
@@ -895,6 +899,8 @@ bool io_req_post_cqe32(struct io_kiocb *req, struct io_uring_cqe cqe[2])
lockdep_assert_held(&ctx->uring_lock);
cqe[0].user_data = req->cqe.user_data;
+ if (unlikely(current->fd_slots.nr))
+ __fd_slots_commit(cqe[0].res);
if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
spin_lock(&ctx->completion_lock);
posted = io_fill_cqe_aux32(ctx, cqe);
@@ -1365,6 +1371,24 @@ static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def,
#define REQ_ISSUE_SLOW_FLAGS (REQ_F_CREDS | REQ_F_ARM_LTIMEOUT)
+/*
+ * Requests complete from io_uring_enter(), task_work, io-wq workers and the
+ * SQPOLL thread, and their CQEs are visible before any syscall returns. So
+ * the descriptors a request reserved are committed per request, before its
+ * completion is posted.
+ */
+static void io_req_fd_reservations(struct io_kiocb *req, int ret)
+{
+ long res = ret;
+
+ /* A request holding reservations must not go async or be reissued. */
+ WARN_ON_ONCE(ret == IOU_ISSUE_SKIP_COMPLETE || ret == IOU_RETRY ||
+ ret == IOU_REQUEUE);
+ if (ret == IOU_COMPLETE)
+ res = req->cqe.res;
+ __fd_slots_commit(res);
+}
+
static inline int __io_issue_sqe(struct io_kiocb *req,
unsigned int issue_flags,
const struct io_issue_def *def)
@@ -1385,6 +1409,9 @@ static inline int __io_issue_sqe(struct io_kiocb *req,
ret = def->issue(req, issue_flags);
+ if (unlikely(current->fd_slots.nr))
+ io_req_fd_reservations(req, ret);
+
if (!def->audit_skip)
audit_uring_exit(!ret, ret);
diff --git a/io_uring/mock_file.c b/io_uring/mock_file.c
index b318ed697998e..67164c28ed659 100644
--- a/io_uring/mock_file.c
+++ b/io_uring/mock_file.c
@@ -257,8 +257,8 @@ static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flag
FD_PREPARE(fdf, O_RDWR | O_CLOEXEC,
anon_inode_create_getfile("[io_uring_mock]", fops, mf,
O_RDWR | O_CLOEXEC, NULL));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
retain_and_null_ptr(mf);
file = fd_prepare_file(fdf);
@@ -271,7 +271,6 @@ static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flag
if (copy_to_user(uarg, &mc, uarg_size))
return -EFAULT;
- fd_publish(fdf);
return 0;
}
diff --git a/io_uring/zcrx.c b/io_uring/zcrx.c
index 1b3b11405daca..d458c614c765f 100644
--- a/io_uring/zcrx.c
+++ b/io_uring/zcrx.c
@@ -795,8 +795,6 @@ static int zcrx_export(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq,
struct zcrx_ctrl *ctrl, void __user *arg)
{
struct zcrx_ctrl_export *ce = &ctrl->zc_export;
- struct file *file;
- int fd;
if (!mem_is_zero(ce, sizeof(*ce)))
return -EINVAL;
@@ -804,27 +802,18 @@ static int zcrx_export(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq,
refcount_inc(&ifq->refs);
refcount_inc(&ifq->user_refs);
- file = anon_inode_create_getfile("[zcrx]", &zcrx_box_fops,
- ifq, O_CLOEXEC, NULL);
- if (IS_ERR(file)) {
+ FD_PREPARE(fdf, O_CLOEXEC,
+ anon_inode_create_getfile("[zcrx]", &zcrx_box_fops, ifq,
+ O_CLOEXEC, NULL));
+ if (IS_ERR(fdf)) {
zcrx_unregister(ifq, NULL);
- return PTR_ERR(file);
- }
-
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0) {
- fput(file);
- return fd;
+ return PTR_ERR(fdf);
}
- ce->zcrx_fd = fd;
- if (copy_to_user(arg, ctrl, sizeof(*ctrl))) {
- fput(file);
- put_unused_fd(fd);
+ ce->zcrx_fd = fd_prepare_fd(fdf);
+ if (copy_to_user(arg, ctrl, sizeof(*ctrl)))
return -EFAULT;
- }
- fd_install(fd, file);
return 0;
}
diff --git a/kernel/bpf/bpf_iter.c b/kernel/bpf/bpf_iter.c
index 14a5fdfa04214..00edc394101bc 100644
--- a/kernel/bpf/bpf_iter.c
+++ b/kernel/bpf/bpf_iter.c
@@ -643,15 +643,15 @@ int bpf_iter_new_fd(struct bpf_link *link)
flags = O_RDONLY | O_CLOEXEC;
FD_PREPARE(fdf, flags, anon_inode_getfile("bpf_iter", &bpf_iter_fops, NULL, flags));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
iter_link = container_of(link, struct bpf_iter_link, link);
err = prepare_seq_file(fd_prepare_file(fdf), iter_link);
if (err)
return err; /* Automatic cleanup handles fput */
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop)
diff --git a/kernel/bpf/token.c b/kernel/bpf/token.c
index e85a179523f02..5cac7fc5694bd 100644
--- a/kernel/bpf/token.c
+++ b/kernel/bpf/token.c
@@ -169,8 +169,8 @@ int bpf_token_create(union bpf_attr *attr)
FD_PREPARE(fdf, O_CLOEXEC,
alloc_file_pseudo(inode, path.mnt, BPF_TOKEN_INODE_NAME,
O_RDWR, &bpf_token_fops));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
token = kzalloc_obj(*token, GFP_USER);
if (!token)
@@ -191,7 +191,7 @@ int bpf_token_create(union bpf_attr *attr)
get_user_ns(token->userns);
fd_prepare_file(fdf)->private_data = no_free_ptr(token);
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
int bpf_token_get_info_by_fd(struct bpf_token *token,
diff --git a/kernel/events/core.c b/kernel/events/core.c
index a6c8e38a31104..54834a7aca53d 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -13924,7 +13924,7 @@ SYSCALL_DEFINE5(perf_event_open,
struct file *event_file = NULL;
struct task_struct *task = NULL;
struct pmu *pmu;
- int event_fd;
+ const struct fd_slot *event_fd;
int move_group = 0;
int err;
int f_flags = O_RDWR;
@@ -13990,9 +13990,9 @@ SYSCALL_DEFINE5(perf_event_open,
if (flags & PERF_FLAG_FD_CLOEXEC)
f_flags |= O_CLOEXEC;
- event_fd = get_unused_fd_flags(f_flags);
- if (event_fd < 0)
- return event_fd;
+ event_fd = fd_prepare(f_flags);
+ if (IS_ERR(event_fd))
+ return PTR_ERR(event_fd);
/*
* Event creation should be under SRCU, see perf_pmu_unregister().
@@ -14001,15 +14001,11 @@ SYSCALL_DEFINE5(perf_event_open,
CLASS(fd, group)(group_fd); // group_fd == -1 => empty
if (group_fd != -1) {
- if (!is_perf_file(group)) {
- err = -EBADF;
- goto err_fd;
- }
+ if (!is_perf_file(group))
+ return -EBADF;
group_leader = fd_file(group)->private_data;
- if (group_leader->state <= PERF_EVENT_STATE_EXIT) {
- err = -ENODEV;
- goto err_fd;
- }
+ if (group_leader->state <= PERF_EVENT_STATE_EXIT)
+ return -ENODEV;
if (flags & PERF_FLAG_FD_OUTPUT)
output_event = group_leader;
if (flags & PERF_FLAG_FD_NO_GROUP)
@@ -14018,10 +14014,8 @@ SYSCALL_DEFINE5(perf_event_open,
if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
task = find_lively_task_by_vpid(pid);
- if (IS_ERR(task)) {
- err = PTR_ERR(task);
- goto err_fd;
- }
+ if (IS_ERR(task))
+ return PTR_ERR(task);
}
if (task && group_leader &&
@@ -14298,8 +14292,7 @@ SYSCALL_DEFINE5(perf_event_open,
* This ensures destruction of the group leader will find
* the pointer to itself in perf_group_detach().
*/
- fd_install(event_fd, event_file);
- return event_fd;
+ return fd_stage(event_fd, event_file);
err_context:
put_pmu_ctx(event->pmu_ctx);
@@ -14316,8 +14309,6 @@ SYSCALL_DEFINE5(perf_event_open,
err_task:
if (task)
put_task_struct(task);
-err_fd:
- put_unused_fd(event_fd);
return err;
}
diff --git a/kernel/exit.c b/kernel/exit.c
index 97686af895013..ee108353a62cd 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -1000,6 +1000,7 @@ void __noreturn do_exit(long code)
exit_sem(tsk);
exit_shm(tsk);
+ exit_fd_slots();
exit_files(tsk);
exit_fs(tsk);
if (group_dead)
diff --git a/kernel/fork.c b/kernel/fork.c
index 416758c8a3d43..bd0e1e85abb0e 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -980,6 +980,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
tsk->btrace_seq = 0;
#endif
tsk->splice_pipe = NULL;
+ memset(&tsk->fd_slots, 0, sizeof(tsk->fd_slots));
tsk->task_frag.page = NULL;
tsk->wake_q.next = NULL;
tsk->worker_private = NULL;
@@ -3216,6 +3217,8 @@ static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp
if ((unshare_flags & CLONE_FILES) &&
(fd && atomic_read(&fd->count) > 1)) {
+ /* Prepared descriptors live in the table we are about to leave. */
+ VFS_WARN_ON_ONCE(current->fd_slots.nr);
fd = dup_fd(fd, NULL);
if (IS_ERR(fd))
return PTR_ERR(fd);
diff --git a/kernel/liveupdate/luo_core.c b/kernel/liveupdate/luo_core.c
index 1b2bda22902d2..3899bbf30b53f 100644
--- a/kernel/liveupdate/luo_core.c
+++ b/kernel/liveupdate/luo_core.c
@@ -270,61 +270,41 @@ struct luo_device_state {
static int luo_ioctl_create_session(struct luo_ucmd *ucmd)
{
struct liveupdate_ioctl_create_session *argp = ucmd->cmd;
+ const struct fd_slot *fd;
struct file *file;
int err;
- argp->fd = get_unused_fd_flags(O_CLOEXEC);
- if (argp->fd < 0)
- return argp->fd;
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
err = luo_session_create(argp->name, &file);
if (err)
- goto err_put_fd;
-
- err = luo_ucmd_respond(ucmd, sizeof(*argp));
- if (err)
- goto err_put_file;
-
- fd_install(argp->fd, file);
-
- return 0;
+ return err;
-err_put_file:
- fput(file);
-err_put_fd:
- put_unused_fd(argp->fd);
+ argp->fd = fd_stage(fd, file);
- return err;
+ return luo_ucmd_respond(ucmd, sizeof(*argp));
}
static int luo_ioctl_retrieve_session(struct luo_ucmd *ucmd)
{
struct liveupdate_ioctl_retrieve_session *argp = ucmd->cmd;
+ const struct fd_slot *fd;
struct file *file;
int err;
- argp->fd = get_unused_fd_flags(O_CLOEXEC);
- if (argp->fd < 0)
- return argp->fd;
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
err = luo_session_retrieve(argp->name, &file);
if (err < 0)
- goto err_put_fd;
-
- err = luo_ucmd_respond(ucmd, sizeof(*argp));
- if (err)
- goto err_put_file;
-
- fd_install(argp->fd, file);
-
- return 0;
+ return err;
-err_put_file:
- fput(file);
-err_put_fd:
- put_unused_fd(argp->fd);
+ argp->fd = fd_stage(fd, file);
- return err;
+ return luo_ucmd_respond(ucmd, sizeof(*argp));
}
static int luo_open(struct inode *inodep, struct file *filep)
diff --git a/kernel/liveupdate/luo_session.c b/kernel/liveupdate/luo_session.c
index f48e9a4185f97..7de35bad1ede9 100644
--- a/kernel/liveupdate/luo_session.c
+++ b/kernel/liveupdate/luo_session.c
@@ -282,33 +282,23 @@ static int luo_session_retrieve_fd(struct luo_session *session,
struct luo_ucmd *ucmd)
{
struct liveupdate_session_retrieve_fd *argp = ucmd->cmd;
+ const struct fd_slot *fd;
struct file *file;
int err;
- argp->fd = get_unused_fd_flags(O_CLOEXEC);
- if (argp->fd < 0)
- return argp->fd;
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
mutex_lock(&session->mutex);
err = luo_retrieve_file(&session->file_set, argp->token, &file);
mutex_unlock(&session->mutex);
if (err < 0)
- goto err_put_fd;
-
- err = luo_ucmd_respond(ucmd, sizeof(*argp));
- if (err)
- goto err_put_file;
-
- fd_install(argp->fd, file);
-
- return 0;
+ return err;
-err_put_file:
- fput(file);
-err_put_fd:
- put_unused_fd(argp->fd);
+ argp->fd = fd_stage(fd, file);
- return err;
+ return luo_ucmd_respond(ucmd, sizeof(*argp));
}
static int luo_session_finish(struct luo_session *session,
diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index 86cf4460d69ea..dc275ab1e031e 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -1957,7 +1957,7 @@ static long seccomp_set_mode_filter(unsigned int flags,
const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
struct seccomp_filter *prepared = NULL;
long ret = -EINVAL;
- int listener = -1;
+ const struct fd_slot *listener = NULL;
struct file *listener_f = NULL;
/* Validate flags. */
@@ -1990,15 +1990,14 @@ static long seccomp_set_mode_filter(unsigned int flags,
return PTR_ERR(prepared);
if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
- listener = get_unused_fd_flags(O_CLOEXEC);
- if (listener < 0) {
- ret = listener;
+ listener = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(listener)) {
+ ret = PTR_ERR(listener);
goto out_free;
}
listener_f = init_listener(prepared);
if (IS_ERR(listener_f)) {
- put_unused_fd(listener);
ret = PTR_ERR(listener_f);
goto out_free;
}
@@ -2038,11 +2037,9 @@ static long seccomp_set_mode_filter(unsigned int flags,
if (ret) {
listener_f->private_data = NULL;
fput(listener_f);
- put_unused_fd(listener);
seccomp_notify_detach(prepared);
} else {
- fd_install(listener, listener_f);
- ret = listener;
+ ret = fd_stage(listener, listener_f);
}
}
out_free:
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 23fb68fce000e..b5131fc597e6b 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -4809,14 +4809,14 @@ static int new_userfaultfd(int flags)
anon_inode_create_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
O_RDONLY | (flags & UFFD_SHARED_FCNTL_FLAGS),
NULL));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
/* prevent the mm struct to be freed */
mmgrab(ctx->mm);
fd_prepare_file(fdf)->f_mode |= FMODE_NOWAIT;
retain_and_null_ptr(ctx);
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
static inline bool userfaultfd_syscall_allowed(int flags)
diff --git a/net/core/scm.c b/net/core/scm.c
index f0d44ecdb11fc..f05931eef54de 100644
--- a/net/core/scm.c
+++ b/net/core/scm.c
@@ -355,6 +355,7 @@ int scm_recv_one_fd(struct file *f, int __user *ufd, unsigned int flags,
bool notrunc)
{
int error;
+ const struct fd_slot *fd;
if (!ufd)
return -EFAULT;
@@ -363,16 +364,16 @@ int scm_recv_one_fd(struct file *f, int __user *ufd, unsigned int flags,
if (error)
return notrunc ? put_user(error, ufd) : error;
- FD_PREPARE(fdf, flags, get_file(f));
- if (fdf.err)
- return fdf.err;
+ fd = fd_prepare(flags);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
- error = put_user(fd_prepare_fd(fdf), ufd);
+ error = put_user(fd_prepare_fd(fd), ufd);
if (error)
return error;
- __receive_sock(fd_prepare_file(fdf));
- return fd_publish(fdf);
+ __receive_sock(f);
+ return fd_stage(fd, get_file(f));
}
void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm, bool notrunc)
diff --git a/net/handshake/netlink.c b/net/handshake/netlink.c
index 3fd4fef9bab1a..98d4266e700e9 100644
--- a/net/handshake/netlink.c
+++ b/net/handshake/netlink.c
@@ -106,19 +106,23 @@ int handshake_nl_accept_doit(struct sk_buff *skb, struct genl_info *info)
err = -EAGAIN;
req = handshake_req_next(hn, class);
if (req) {
- FD_PREPARE(fdf, O_CLOEXEC, req->hr_file);
- if (fdf.err) {
+ /* The ack carries the error, sendmsg() succeeds: stage last. */
+ const struct fd_slot *fd = fd_prepare(O_CLOEXEC);
+
+ if (IS_ERR(fd)) {
fput(req->hr_file); /* drop ref from handshake_req_next() */
- err = fdf.err;
+ err = PTR_ERR(fd);
goto out_complete;
}
- err = req->hr_proto->hp_accept(req, info, fd_prepare_fd(fdf));
- if (err)
- goto out_complete; /* Automatic cleanup handles fput */
+ err = req->hr_proto->hp_accept(req, info, fd_prepare_fd(fd));
+ if (err) {
+ fput(req->hr_file); /* not staged, drop it by hand */
+ goto out_complete;
+ }
- trace_handshake_cmd_accept(net, req, req->hr_sk, fd_prepare_fd(fdf));
- fd_publish(fdf);
+ fd_stage(fd, req->hr_file);
+ trace_handshake_cmd_accept(net, req, req->hr_sk, fd_prepare_fd(fd));
return 0;
}
diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c
index 71af69d442f21..2d744ee3d3404 100644
--- a/net/kcm/kcmsock.c
+++ b/net/kcm/kcmsock.c
@@ -1580,14 +1580,13 @@ static int kcm_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
struct kcm_clone info;
FD_PREPARE(fdf, 0, kcm_clone(sock));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
info.fd = fd_prepare_fd(fdf);
if (copy_to_user((void __user *)arg, &info, sizeof(info)))
return -EFAULT;
- fd_publish(fdf);
err = 0;
break;
}
diff --git a/net/sctp/socket.c b/net/sctp/socket.c
index c7b9e325ec1cd..b75c50af8c169 100644
--- a/net/sctp/socket.c
+++ b/net/sctp/socket.c
@@ -5744,46 +5744,41 @@ static int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id,
}
static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
- struct file **newfile, unsigned flags)
+ unsigned flags)
{
struct socket *newsock;
+ struct file *newfile;
+ const struct fd_slot *fd;
int retval;
retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
if (retval < 0)
- goto out;
+ return retval;
- /* Map the socket to an unused fd that can be returned to the user. */
- retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
- if (retval < 0) {
+ /* Map the socket to a reserved fd that can be returned to the user. */
+ fd = fd_prepare(flags & SOCK_CLOEXEC);
+ if (IS_ERR(fd)) {
sock_release(newsock);
- goto out;
- }
-
- *newfile = sock_alloc_file(newsock, 0, NULL);
- if (IS_ERR(*newfile)) {
- put_unused_fd(retval);
- retval = PTR_ERR(*newfile);
- *newfile = NULL;
- return retval;
+ return PTR_ERR(fd);
}
- pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
- retval);
-
- peeloff->sd = retval;
+ newfile = sock_alloc_file(newsock, 0, NULL);
+ if (IS_ERR(newfile))
+ return PTR_ERR(newfile);
if (flags & SOCK_NONBLOCK)
- (*newfile)->f_flags |= O_NONBLOCK;
-out:
- return retval;
+ newfile->f_flags |= O_NONBLOCK;
+
+ peeloff->sd = fd_stage(fd, newfile);
+ pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
+ peeloff->sd);
+ return peeloff->sd;
}
static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
{
sctp_peeloff_arg_t peeloff;
- struct file *newfile = NULL;
- int retval = 0;
+ int retval;
if (len < sizeof(sctp_peeloff_arg_t))
return -EINVAL;
@@ -5791,24 +5786,17 @@ static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval
if (copy_from_user(&peeloff, optval, len))
return -EFAULT;
- retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
+ retval = sctp_getsockopt_peeloff_common(sk, &peeloff, 0);
if (retval < 0)
- goto out;
+ return retval;
/* Return the fd mapped to the new socket. */
- if (put_user(len, optlen)) {
- fput(newfile);
- put_unused_fd(retval);
+ if (put_user(len, optlen))
return -EFAULT;
- }
- if (copy_to_user(optval, &peeloff, len)) {
- fput(newfile);
- put_unused_fd(retval);
+ if (copy_to_user(optval, &peeloff, len))
return -EFAULT;
- }
- fd_install(retval, newfile);
-out:
+
return retval;
}
@@ -5816,8 +5804,7 @@ static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
char __user *optval, int __user *optlen)
{
sctp_peeloff_flags_arg_t peeloff;
- struct file *newfile = NULL;
- int retval = 0;
+ int retval;
if (len < sizeof(sctp_peeloff_flags_arg_t))
return -EINVAL;
@@ -5826,24 +5813,17 @@ static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
return -EFAULT;
retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
- &newfile, peeloff.flags);
+ peeloff.flags);
if (retval < 0)
- goto out;
+ return retval;
/* Return the fd mapped to the new socket. */
- if (put_user(len, optlen)) {
- fput(newfile);
- put_unused_fd(retval);
+ if (put_user(len, optlen))
return -EFAULT;
- }
- if (copy_to_user(optval, &peeloff, len)) {
- fput(newfile);
- put_unused_fd(retval);
+ if (copy_to_user(optval, &peeloff, len))
return -EFAULT;
- }
- fd_install(retval, newfile);
-out:
+
return retval;
}
diff --git a/sound/core/compress_offload.c b/sound/core/compress_offload.c
index 23d62fede06e8..94bc226f8db94 100644
--- a/sound/core/compress_offload.c
+++ b/sound/core/compress_offload.c
@@ -1065,7 +1065,8 @@ static u64 snd_compr_seqno_next(struct snd_compr_stream *stream)
static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_task *utask)
{
struct snd_compr_task_runtime *task;
- int retval, fd_i, fd_o;
+ int retval;
+ const struct fd_slot *fd_i, *fd_o;
if (stream->runtime->total_tasks >= stream->runtime->fragments)
return -EBUSY;
@@ -1080,29 +1081,26 @@ static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_
if (retval < 0)
goto cleanup;
/* similar functionality as in dma_buf_fd(), but ensure that both
- file descriptors are allocated before fd_install() */
+ file descriptors are reserved before either is staged */
if (!task->input || !task->input->file || !task->output || !task->output->file) {
retval = -EINVAL;
goto free_driver_task;
}
- fd_i = get_unused_fd_flags(O_WRONLY|O_CLOEXEC);
- if (fd_i < 0) {
- retval = fd_i;
+ fd_i = fd_prepare(O_WRONLY|O_CLOEXEC);
+ if (IS_ERR(fd_i)) {
+ retval = PTR_ERR(fd_i);
goto free_driver_task;
}
- fd_o = get_unused_fd_flags(O_RDONLY|O_CLOEXEC);
- if (fd_o < 0) {
- retval = fd_o;
- put_unused_fd(fd_i);
+ fd_o = fd_prepare(O_RDONLY|O_CLOEXEC);
+ if (IS_ERR(fd_o)) {
+ retval = PTR_ERR(fd_o);
goto free_driver_task;
}
/* keep dmabuf reference until freed with task free ioctl */
get_dma_buf(task->input);
get_dma_buf(task->output);
- fd_install(fd_i, task->input->file);
- fd_install(fd_o, task->output->file);
- utask->input_fd = fd_i;
- utask->output_fd = fd_o;
+ utask->input_fd = fd_stage(fd_i, task->input->file);
+ utask->output_fd = fd_stage(fd_o, task->output->file);
list_add_tail(&task->list, &stream->runtime->tasks);
stream->runtime->total_tasks++;
return 0;
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 625e62e1a0318..e0d1cb869342e 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -526,17 +526,16 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)
struct gmem_file *f;
struct inode *inode;
struct file *file;
- int fd, err;
+ const struct fd_slot *fd;
+ int err;
- fd = get_unused_fd_flags(0);
- if (fd < 0)
- return fd;
+ fd = fd_prepare(0);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
f = kzalloc_obj(*f);
- if (!f) {
- err = -ENOMEM;
- goto err_fd;
- }
+ if (!f)
+ return -ENOMEM;
/* __fput() will take care of fops_put(). */
if (!fops_get(&kvm_gmem_fops)) {
@@ -575,8 +574,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)
xa_init(&f->bindings);
list_add(&f->entry, &GMEM_I(inode)->gmem_file_list);
- fd_install(fd, file);
- return fd;
+ return fd_stage(fd, file);
err_inode:
iput(inode);
@@ -584,8 +582,6 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)
fops_put(&kvm_gmem_fops);
err_gmem:
kfree(f);
-err_fd:
- put_unused_fd(fd);
return err;
}
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 65eb26a0520d8..8d3d47a6c98aa 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4326,27 +4326,18 @@ static const struct file_operations kvm_vcpu_stats_fops = {
static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu)
{
- int fd;
- struct file *file;
char name[15 + ITOA_MAX_LEN + 1];
snprintf(name, sizeof(name), "kvm-vcpu-stats:%d", vcpu->vcpu_id);
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- file = anon_inode_getfile_fmode(name, &kvm_vcpu_stats_fops, vcpu,
- O_RDONLY, FMODE_PREAD);
- if (IS_ERR(file)) {
- put_unused_fd(fd);
- return PTR_ERR(file);
- }
+ FD_PREPARE(fdf, O_CLOEXEC,
+ anon_inode_getfile_fmode(name, &kvm_vcpu_stats_fops, vcpu,
+ O_RDONLY, FMODE_PREAD));
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
kvm_get_kvm(vcpu->kvm);
- fd_install(fd, file);
-
- return fd;
+ return fd_prepare_fd(fdf);
}
#ifdef CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY
@@ -5136,24 +5127,14 @@ static const struct file_operations kvm_vm_stats_fops = {
static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm)
{
- int fd;
- struct file *file;
-
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- file = anon_inode_getfile_fmode("kvm-vm-stats",
- &kvm_vm_stats_fops, kvm, O_RDONLY, FMODE_PREAD);
- if (IS_ERR(file)) {
- put_unused_fd(fd);
- return PTR_ERR(file);
- }
+ FD_PREPARE(fdf, O_CLOEXEC,
+ anon_inode_getfile_fmode("kvm-vm-stats", &kvm_vm_stats_fops,
+ kvm, O_RDONLY, FMODE_PREAD));
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
kvm_get_kvm(kvm);
- fd_install(fd, file);
-
- return fd;
+ return fd_prepare_fd(fdf);
}
#define SANITY_CHECK_MEM_REGION_FIELD(field) \
@@ -5499,21 +5480,20 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(file_is_kvm);
static int kvm_dev_ioctl_create_vm(unsigned long type)
{
char fdname[ITOA_MAX_LEN + 1];
- int r, fd;
struct kvm *kvm;
struct file *file;
+ const struct fd_slot *fd;
+ int r;
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0)
- return fd;
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
- snprintf(fdname, sizeof(fdname), "%d", fd);
+ snprintf(fdname, sizeof(fdname), "%d", fd_prepare_fd(fd));
kvm = kvm_create_vm(type, fdname);
- if (IS_ERR(kvm)) {
- r = PTR_ERR(kvm);
- goto put_fd;
- }
+ if (IS_ERR(kvm))
+ return PTR_ERR(kvm);
file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
if (IS_ERR(file)) {
@@ -5529,13 +5509,10 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)
*/
kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);
- fd_install(fd, file);
- return fd;
+ return fd_stage(fd, file);
put_kvm:
kvm_put_kvm(kvm);
-put_fd:
- put_unused_fd(fd);
return r;
}
|
| 3/2 | 2026/09/15 14:31 | llm | patch-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 63601 output: 29 thoughts: 1379 Thoughts: **Kernel Patch Review for Syzkaller Fuzzing** Okay, let's get down to brass tacks. I've got a kernel patch series here, and the goal is to determine if it's a good candidate for fuzzing with syzkaller on amd64. I'm going to apply the core triage philosophy: reachability is king, I won't blindly trust claims, and new or modified assertions in reachable code are a must-fuzz. If reachable executable code is changed or refactored, it's getting a closer look. I'll keep the negative criteria in mind: non-kernel, non-executable changes, unreachable hardware, or execution paths that are driver teardown or arch-exclusive to something other than amd64 are a no-go. Now for the positive criteria: core subsystems (mm, locking, BPF, scheduler, networking, VFS, syscall handling) are prime targets. Refactorings and cleanups in reachable code? Yes. Runtime assertions and defensive checks in reachable paths? Absolutely. Reachable drivers and protocols? Definitely. I also want to focus fuzzing on non-ubiquitous kernel functions, targeting specific feature logic and entrypoints, while avoiding generic lifecycle hot-paths. Let's dive into the patch series. It looks like the main thrust is introducing an `fd_slots` mechanism for file descriptor preparation and automatic commit/drop at syscall return. That's a fundamental change, impacting how file descriptors are managed across the kernel. Specifically, it touches things like: * Adding `TIF_FD_SLOTS` and `SYSCALL_WORK_FD_SLOTS` across architectures (including the generic entry point used by amd64). * Adding `struct fd_slots` to `task_struct`. * Extensive modifications to `fs/file.c` to handle the new `fd_prepare()`, `fd_stage()`, `fd_slots_*` and other functions. * Modifications to `include/linux/entry-common.h` to call `fd_slots_commit` at syscall exit. * Changes to `include/linux/sched.h`, `kernel/fork.c`, `kernel/exit.c`, and `fs/exec.c` involving `fd_slots` and including new `VFS_WARN_ON_ONCE` assertions. * Modifications to `io_uring` to integrate with this new mechanism. * And, crucially, it ripples out to a huge number of callers throughout the kernel, including `bpf`, `perf_event_open`, `seccomp`, `userfaultfd`, network code (`scm`, `sctp`, `netlink`, `kcm`), file system code (`eventfd`, `eventpoll`, `namespace`, `nsfs`, `xfs`), virtualization (`kvm`), and a wide variety of drivers (`dma-buf`, `drm`, `tpm`, `iio`, `iommu`, `media`, `misc`, and `vfio`). This is a deep dive into VFS and syscall exit handling for file descriptor management. This is a game changer for how file descriptors are published or aborted across syscalls, ioctls, socket calls, io\_uring, and a plethora of other kernel operations, including the insertion of new assertions. Based on this analysis, I'm confident that this is *definitely* `WorthFuzzing=true`. Now, let's ensure amd64 benefits from this. Does amd64 use `entry-common.h`, specifically `CONFIG_GENERIC_ENTRY`? A quick grep in `arch/x86` should tell me that. |
| 4/2 | 2026/09/15 14:32 | tool | grepper |
0mArgs:{
"Expression": "GENERIC_ENTRY",
"PathPrefix": "arch/x86"
}Results: {
"Output": "arch/x86/Kconfig=58=config X86\n--\narch/x86/Kconfig-174-\tselect GENERIC_EARLY_IOREMAP\narch/x86/Kconfig:175:\tselect GENERIC_ENTRY\narch/x86/Kconfig-176-\tselect GENERIC_IOMAP\n"
} |
| 5/2 | 2026/09/15 14:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 65896 output: 33 thoughts: 274 Thoughts: **Analyzing the x86 System Call Entry and File Descriptor Handling**
Okay, so I've confirmed that the x86 architecture is indeed using `GENERIC_ENTRY`, which is excellent. This means we're utilizing `include/linux/entry-common.h`, and that's where the interesting bits are. Specifically, the preprocessor directives:
```c
#if defined(CONFIG_GENERIC_ENTRY)
#define fd_slots_set_work() set_syscall_work(FD_SLOTS)
#define fd_slots_clear_work() clear_syscall_work(FD_SLOTS)
```
are pivotal. This tells me we're dealing with a generic approach to system call entry/exit and that it includes this concept of "FD_SLOTS," which is a clear indicator that file descriptor management is going to be central here. And there it is, `syscall_exit_work` in that same file:
```c
static __always_inline void syscall_exit_work(struct pt_regs *regs, unsigned long work)
{
bool step;
/* Install or drop the descriptors the syscall prepared. */
if (work & SYSCALL_WORK_FD_SLOTS)
fd_slots_commit(regs);
}
```
This confirms my suspicion: `fd_slots_commit` is being invoked on syscall exit if `SYSCALL_WORK_FD_SLOTS` is set. Now, I need to understand what this `fd_slots_commit` is actually *doing*. Time to dive into `fs/file.c` and analyze the relevant functions. I've already identified the key functions I need to review: `fd_prepare`, `fd_stage`, `fd_prepare_fd`, `fd_prepare_file`, `fd_slots_commit`, `__fd_slots_commit`, `exit_fd_slots`, and `receive_fd`. I'll be looking carefully at the code surrounding these functions to get a complete picture of how the file descriptor mechanism works at a low level within the kernel, specifically in relation to system call entry and exit. This is going to be good.
|
| 6/2 | 2026/09/15 14:32 | tool | read-file |
0mArgs:{
"File": "fs/file.c",
"FirstLine": 645,
"LineCount": 85
}Results: {
"Contents": " 645:\t#define FD_SLOTS_SPILL_MIN\t256\n 646:\t\n 647:\t/* Make the syscall exit path call fd_slots_commit(). */\n 648:\t#if defined(CONFIG_GENERIC_ENTRY)\n 649:\t#define fd_slots_set_work()\tset_syscall_work(FD_SLOTS)\n 650:\t#define fd_slots_clear_work()\tclear_syscall_work(FD_SLOTS)\n 651:\t#elif defined(TIF_FD_SLOTS)\n 652:\t#define fd_slots_set_work()\tset_thread_flag(TIF_FD_SLOTS)\n 653:\t#define fd_slots_clear_work()\tclear_thread_flag(TIF_FD_SLOTS)\n 654:\t#else\n 655:\t#error \"the syscall exit path must commit prepared file descriptors\"\n 656:\t#endif\n 657:\t\n 658:\tstatic struct fd_slot *fd_slot(struct fd_slots *slots, unsigned int idx)\n 659:\t{\n 660:\t\tif (idx \u003c FD_SLOTS_INLINE)\n 661:\t\t\treturn \u0026slots-\u003einline_slots[idx];\n 662:\t\treturn \u0026slots-\u003espill[idx - FD_SLOTS_INLINE];\n 663:\t}\n 664:\t\n 665:\t/* Room for slot @idx in the spill array, doubling as it fills up. */\n 666:\tstatic noinline struct fd_slot *fd_slots_spill(struct fd_slots *slots,\n 667:\t\t\t\t\t\t unsigned int idx)\n 668:\t{\n 669:\t\tunsigned int max = slots-\u003espill_max;\n 670:\t\tstruct fd_slot *spill;\n 671:\t\n 672:\t\tidx -= FD_SLOTS_INLINE;\n 673:\t\tif (idx \u003c max)\n 674:\t\t\treturn \u0026slots-\u003espill[idx];\n 675:\t\n 676:\t\tmax = max ? max * 2 : FD_SLOTS_SPILL_MIN;\n 677:\t\tspill = kvrealloc(slots-\u003espill, array_size(max, sizeof(*spill)),\n 678:\t\t\t\t GFP_KERNEL_ACCOUNT);\n 679:\t\tif (!spill)\n 680:\t\t\treturn NULL;\n 681:\t\tslots-\u003espill = spill;\n 682:\t\tslots-\u003espill_max = max;\n 683:\t\treturn \u0026spill[idx];\n 684:\t}\n 685:\t\n 686:\t/* Record @fd in the next slot, returns the slot. */\n 687:\tstatic struct fd_slot *fd_slot_record(int fd)\n 688:\t{\n 689:\t\tstruct fd_slots *slots = \u0026current-\u003efd_slots;\n 690:\t\tunsigned int idx = slots-\u003enr;\n 691:\t\tstruct fd_slot *slot;\n 692:\t\n 693:\t\t/* Nothing would ever commit what a kernel thread prepares. */\n 694:\t\tVFS_WARN_ON_ONCE(current-\u003eflags \u0026 PF_KTHREAD);\n 695:\t\n 696:\t\tif (likely(idx \u003c FD_SLOTS_INLINE)) {\n 697:\t\t\tslot = \u0026slots-\u003einline_slots[idx];\n 698:\t\t} else {\n 699:\t\t\tslot = fd_slots_spill(slots, idx);\n 700:\t\t\tif (!slot)\n 701:\t\t\t\treturn ERR_PTR(-ENOMEM);\n 702:\t\t}\n 703:\t\tACCESS_PRIVATE(slot, fd) = fd;\n 704:\t\tACCESS_PRIVATE(slot, file) = NULL;\n 705:\t\tif (!idx)\n 706:\t\t\tfd_slots_set_work();\n 707:\t\tslots-\u003enr = idx + 1;\n 708:\t\treturn slot;\n 709:\t}\n 710:\t\n 711:\t/* The slot holding @fd, if this syscall prepared it. */\n 712:\tstatic inline int fd_slot_find(struct fd_slots *slots, unsigned int fd)\n 713:\t{\n 714:\t\tunsigned int idx = slots-\u003enr;\n 715:\t\n 716:\t\twhile (idx--) {\n 717:\t\t\tif (ACCESS_PRIVATE(fd_slot(slots, idx), fd) == fd)\n 718:\t\t\t\treturn idx;\n 719:\t\t}\n 720:\t\treturn -1;\n 721:\t}\n 722:\t\n 723:\t/**\n 724:\t * fd_prepare - allocate a descriptor that the syscall exit installs\n 725:\t * @flags: O_CLOEXEC or 0\n 726:\t *\n 727:\t * Returns the prepared slot as a const handle or an error pointer.\n 728:\t */\n 729:\tconst struct fd_slot *fd_prepare(unsigned flags)\n"
} |
| 7/2 | 2026/09/15 14:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 68001 output: 33 thoughts: 13 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Diving into fs/file.c** Okay, time to dig deeper. I need to understand this file system interaction better, and `fs/file.c` seems like the logical place to start. I'm thinking, this is where a lot of the core file handling logic should reside. Let's see how things are structured down there... I'm looking for the key functions that manage file operations at the kernel level. Let's see if there are any obvious bottlenecks or performance considerations evident in the structure or algorithms being used... |
| 8/2 | 2026/09/15 14:32 | tool | read-file |
0mArgs:{
"File": "fs/file.c",
"FirstLine": 730,
"LineCount": 85
}Results: {
"Contents": " 730:\t{\n 731:\t\tstruct fd_slot *slot;\n 732:\t\tint fd;\n 733:\t\n 734:\t\tfd = get_unused_fd_flags(flags);\n 735:\t\tif (fd \u003c 0)\n 736:\t\t\treturn ERR_PTR(fd);\n 737:\t\n 738:\t\tslot = fd_slot_record(fd);\n 739:\t\tif (IS_ERR(slot))\n 740:\t\t\tfd_release(fd);\n 741:\t\treturn slot;\n 742:\t}\n 743:\tEXPORT_SYMBOL(fd_prepare);\n 744:\t\n 745:\t/**\n 746:\t * fd_stage - attach the file to a prepared slot\n 747:\t * @slot: slot from fd_prepare()\n 748:\t * @file: the file to install, consumed\n 749:\t *\n 750:\t * Returns the number. The syscall exit installs @file there when the syscall\n 751:\t * returns success and drops it when the syscall returns an error.\n 752:\t */\n 753:\tint fd_stage(const struct fd_slot *slot, struct file *file)\n 754:\t{\n 755:\t\tstruct fd_slot *s = (struct fd_slot *)slot;\n 756:\t\n 757:\t\tVFS_WARN_ON_ONCE(ACCESS_PRIVATE(s, file));\n 758:\t\tACCESS_PRIVATE(s, file) = file;\n 759:\t\treturn ACCESS_PRIVATE(s, fd);\n 760:\t}\n 761:\tEXPORT_SYMBOL(fd_stage);\n 762:\t\n 763:\t/**\n 764:\t * fd_prepare_fd - the descriptor number of a prepared slot\n 765:\t * @slot: slot from fd_prepare()\n 766:\t */\n 767:\tint fd_prepare_fd(const struct fd_slot *slot)\n 768:\t{\n 769:\t\treturn ACCESS_PRIVATE(slot, fd);\n 770:\t}\n 771:\tEXPORT_SYMBOL(fd_prepare_fd);\n 772:\t\n 773:\t/**\n 774:\t * fd_prepare_file - the file staged into a slot, to configure before install\n 775:\t * @slot: slot from fd_prepare()\n 776:\t *\n 777:\t * Returns the file handed to fd_stage(), or NULL before one is staged.\n 778:\t */\n 779:\tstruct file *fd_prepare_file(const struct fd_slot *slot)\n 780:\t{\n 781:\t\treturn ACCESS_PRIVATE(slot, file);\n 782:\t}\n 783:\tEXPORT_SYMBOL(fd_prepare_file);\n 784:\t\n 785:\t/* Install every staged file, release the slots that never got one. */\n 786:\tstatic void fd_slots_install(struct fd_slots *slots)\n 787:\t{\n 788:\t\tunsigned int idx;\n 789:\t\n 790:\t\tfor (idx = 0; idx \u003c slots-\u003enr; idx++) {\n 791:\t\t\tstruct fd_slot *slot = fd_slot(slots, idx);\n 792:\t\n 793:\t\t\tif (ACCESS_PRIVATE(slot, file))\n 794:\t\t\t\tfd_install(ACCESS_PRIVATE(slot, fd),\n 795:\t\t\t\t\t ACCESS_PRIVATE(slot, file));\n 796:\t\t\telse\n 797:\t\t\t\tfd_release(ACCESS_PRIVATE(slot, fd));\n 798:\t\t}\n 799:\t}\n 800:\t\n 801:\t/* Release every slot's descriptor and drop the staged files. */\n 802:\tstatic void fd_slots_drop(struct fd_slots *slots)\n 803:\t{\n 804:\t\tstruct files_struct *files = current-\u003efiles;\n 805:\t\tunsigned int idx;\n 806:\t\n 807:\t\tspin_lock(\u0026files-\u003efile_lock);\n 808:\t\tfor (idx = 0; idx \u003c slots-\u003enr; idx++)\n 809:\t\t\t__put_unused_fd(files, ACCESS_PRIVATE(fd_slot(slots, idx), fd));\n 810:\t\tspin_unlock(\u0026files-\u003efile_lock);\n 811:\t\tfor (idx = 0; idx \u003c slots-\u003enr; idx++) {\n 812:\t\t\tstruct fd_slot *slot = fd_slot(slots, idx);\n 813:\t\n 814:\t\t\tif (ACCESS_PRIVATE(slot, file))\n"
} |
| 9/2 | 2026/09/15 14:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 69408 output: 33 thoughts: 21 Thoughts: Here's my take on those lines in `fs/file.c`. **Diving into `fs/file.c` - Lines 815-870** Okay, I need to take a look at lines 815-870 in `fs/file.c`. My spidey-sense is tingling, and I have a feeling something interesting is happening in this section of the code. Let me quickly refresh my memory on the surrounding context... Hmm, judging by the line numbers, this likely falls within the area dealing with *file* operations, maybe within the core `file_operations` structure or perhaps related to an internal function dealing with file access/management. It's probably handling read, write, or some form of file descriptor management. Let's quickly scan the code and grasp what kind of operations are described in those lines. Are we dealing with locking, error handling, permission checks, data transfers, or something else entirely? The comments are going to be key here. Depending on the nature of the file system, this could be anything from a simple wrapper to a more complex routine. I’ll be looking for any hints about potential bottlenecks, subtle concurrency issues, or optimization opportunities in the way it handles file I/O requests. Let's see if this is an area where we can fine-tune something for increased performance or reliability. My goal is to determine the function of the section and identify any areas of interest that could influence the larger system. |
| 10/2 | 2026/09/15 14:32 | tool | read-file |
0mArgs:{
"File": "fs/file.c",
"FirstLine": 815,
"LineCount": 55
}Results: {
"Contents": " 815:\t\t\t\tfput(ACCESS_PRIVATE(slot, file));\n 816:\t\t}\n 817:\t}\n 818:\t\n 819:\tstatic __always_inline void fd_slots_finish(struct fd_slots *slots, bool failed)\n 820:\t{\n 821:\t\tif (likely(!failed))\n 822:\t\t\tfd_slots_install(slots);\n 823:\t\telse\n 824:\t\t\tfd_slots_drop(slots);\n 825:\t\tslots-\u003enr = 0;\n 826:\t\tfd_slots_clear_work();\n 827:\t}\n 828:\t\n 829:\t/* Syscall exit hook, keyed on the return value the caller will see. */\n 830:\tvoid fd_slots_commit(struct pt_regs *regs)\n 831:\t{\n 832:\t\tfd_slots_finish(\u0026current-\u003efd_slots,\n 833:\t\t\t\tsyscall_get_error(current, regs) != 0);\n 834:\t}\n 835:\t\n 836:\t/* Install or drop the prepared descriptors based on @ret. */\n 837:\tvoid __fd_slots_commit(long ret)\n 838:\t{\n 839:\t\tfd_slots_finish(\u0026current-\u003efd_slots, IS_ERR_VALUE(ret));\n 840:\t}\n 841:\t\n 842:\tvoid exit_fd_slots(void)\n 843:\t{\n 844:\t\tstruct fd_slots *slots = \u0026current-\u003efd_slots;\n 845:\t\n 846:\t\t/* A syscall must not exit with prepared descriptors outstanding. */\n 847:\t\tif (WARN_ON_ONCE(slots-\u003enr))\n 848:\t\t\tfd_slots_finish(slots, true);\n 849:\t\tkvfree(slots-\u003espill);\n 850:\t}\n 851:\t\n 852:\t/**\n 853:\t * put_unused_fd - give a descriptor back before it got a file\n 854:\t * @fd: descriptor returned by get_unused_fd_flags()\n 855:\t *\n 856:\t * Not for a prepared descriptor, the syscall exit releases that one.\n 857:\t */\n 858:\tvoid put_unused_fd(unsigned int fd)\n 859:\t{\n 860:\t\tVFS_WARN_ON_ONCE(fd_slot_find(\u0026current-\u003efd_slots, fd) \u003e= 0);\n 861:\t\tfd_release(fd);\n 862:\t}\n 863:\t\n 864:\tEXPORT_SYMBOL(put_unused_fd);\n 865:\t\n 866:\t/*\n 867:\t * Install a file pointer in the fd array while it is being resized.\n 868:\t *\n 869:\t * We need to make sure our update to the array does not get lost as the resizing\n"
} |
| 11/2 | 2026/09/15 14:32 | llm | patch-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 70559 output: 25 thoughts: 966 Thoughts: **Fuzzing Target Analysis** Okay, this looks *very* promising as a fuzzing target. It's touching core VFS code (`fs/file.c`) and syscall exit paths (`include/linux/entry-common.h`) which are always good starting points for kernel fuzzing. The introduction of these new functions: `fd_prepare()`, `fd_stage()`, `fd_slots_commit()`, `__fd_slots_commit()`, and `exit_fd_slots()` is a significant change, increasing attack surface substantially. The added `VFS_WARN_ON_ONCE` checks throughout the code (with those `PF_KTHREAD`, `ACCESS_PRIVATE(s, file)` checks, as well as checks related to `fd_slot_find` and `me->fd_slots.nr`) are a clear sign of important hardening measures, and where there's hardening, there is often room for bugs. The fact that it modifies `io_uring` and interacts with several ioctls and syscalls (like `perf_event_open`, `eventfd`, `epoll_create1`, `userfaultfd`, `seccomp`, `fsmount`, `open_tree_attr`, etc.) means that it's going to be exercising many different code paths. This is a very interesting target because of this broad interaction! Now, for configuration. I need to nail down the exact configuration requirements for successful fuzzing. The core VFS code in `fs/file.c` is always enabled, so that's good. But I need to identify any non-standard or specific configurations required for those modified components. The obvious ones are: `CONFIG_IO_URING`, `CONFIG_PERF_EVENTS`, `CONFIG_USERFAULTFD`, `CONFIG_SECCOMP`, and `CONFIG_KVM` to hit the various entry points touched. The main goal here is to determine whether these configs are strictly *required* to compile the code (which isn't the case for VFS/file.c) or, are they required to *reach* the relevant code paths. Section 5 stresses the importance of not making assumptions and digging into the Kconfig files and `#ifdef` guards. Okay, let's see. `fs/file.c` is unconditionally built. But to exercise code that's related to io_uring, perf, userfault, etc, we need specific config options to enable the code paths. The section specifically requests the config *without* the `CONFIG_` prefix. Ah, I just realized I need to check for any *other* dependencies. Let me see... I need to look for `#ifdef` in `fs/file.c`... Ah, there it is! Lines 645-660 have a `#if defined(CONFIG_GENERIC_ENTRY)`. Okay, so `CONFIG_GENERIC_ENTRY` needs to be enabled for that code to be reached. Let me check the kernel config... Ah, right, on x86, `GENERIC_ENTRY` is selected by `X86`. So `GENERIC_ENTRY` is required, even if it's selected by another option. That's good to know. Now, let me check the kernel to see if there are any config options that guard other features, like `io_uring`, `virtio_gpu`, `kvm`, and anything else. Wait, what's this `kernel/liveupdate/` directory in the diff? Time to grep for that and see what it's about. |
| 12/2 | 2026/09/15 14:34 | tool | grepper |
0mArgs:{
"Expression": "luo_core",
"PathPrefix": "kernel"
}Results: {
"Output": "kernel/liveupdate/Makefile=3=luo-y :=\t\t\t\t\t\t\t\t\\\nkernel/liveupdate/Makefile-4-\t\tkho_block.o\t\t\t\t\t\t\\\nkernel/liveupdate/Makefile:5:\t\tluo_core.o\t\t\t\t\t\t\\\nkernel/liveupdate/Makefile-6-\t\tluo_file.o\t\t\t\t\t\t\\\n"
} |
| 13/2 | 2026/09/15 14:34 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 72392 output: 32 thoughts: 12 Thoughts: Here's a summary of my immediate thought process: **Diving into the Live Update Makefile** Okay, so I need to understand something about the live update process. My first instinct is to dive into the `kernel/liveupdate/Makefile`. This is the logical starting point for me. It's where the build configuration and dependencies for the live update component are defined. I expect to find the rules for building the relevant binaries, the linking process, and likely some crucial include paths or macros that are central to how live updates are implemented. I'm hoping to quickly grasp the high-level build flow and identify the key targets. From there, I can pinpoint which specific parts of the process I need to focus on to get the information I am seeking. |
| 14/2 | 2026/09/15 14:34 | tool | read-file |
0mArgs:{
"File": "kernel/liveupdate/Makefile",
"FirstLine": 1,
"LineCount": 20
}Results: {
"Contents": " 1:\t# SPDX-License-Identifier: GPL-2.0\n 2:\t\n 3:\tluo-y :=\t\t\t\t\t\t\t\t\\\n 4:\t\t\tkho_block.o\t\t\t\t\t\t\\\n 5:\t\t\tluo_core.o\t\t\t\t\t\t\\\n 6:\t\t\tluo_file.o\t\t\t\t\t\t\\\n 7:\t\t\tluo_flb.o\t\t\t\t\t\t\\\n 8:\t\t\tluo_session.o\n 9:\t\n 10:\tobj-$(CONFIG_KEXEC_HANDOVER)\t\t+= kexec_handover.o\n 11:\tobj-$(CONFIG_KEXEC_HANDOVER_DEBUGFS)\t+= kexec_handover_debugfs.o\n 12:\t\n 13:\tobj-$(CONFIG_LIVEUPDATE)\t\t+= luo.o\n"
} |
| 15/2 | 2026/09/15 14:34 | llm | patch-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 72791 output: 28 thoughts: 659 Thoughts: **My Thought Process on Targeted Kernel Fuzzing**
Alright, let's break this down. First, `CONFIG_LIVEUPDATE` is clearly relevant to `luo`. But what about `io_uring`? Ah, `CONFIG_IO_URING`, of course. Now, the real question is, what kernel configuration options are *essential* to actually compile and then hit the modified code? The changed code is scattered across `fs/file.c`, `include/linux/entry-common.h`, `include/linux/file.h`, and files like `fs/eventfd.c` and `fs/eventpoll.c`.
Aha! Even with a fairly standard kernel config, the fundamental changes in `fs/file.c` are going to be baked into almost every single system call that involves preparing or installing file descriptors (think `eventfd`, `epoll_create1`, and the like). That's a good starting point.
But hold on... let's not forget about `IO_URING`. `IO_URING` is key because it has its own special logic, right? Specifically, this snippet:
```c
static void io_req_fd_reservations(struct io_kiocb *req, int ret)
...
if (unlikely(current->fd_slots.nr))
__fd_slots_commit(res);
```
Okay, let's double-check `FocusSymbols`. That's where we make this really targeted. Let's recall section 4. "When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer: - AVOID UBIQUITOUS LIFECYCLE HOT-PATHS: Do NOT list generic, ubiquitous functions called by almost every program in the corpus (including, but not limited to: general memory allocators and deallocators, page fault and trap handlers, or core synchronization primitives; this is not an exhaustive list). Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused", which severely dilutes fuzzing effort away from the actual changes. - TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS: List functions that specifically implement the logic being added or altered, or direct API entrypoints for the subsystem feature under review. - HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h): Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters. If the changes are primarily in static inline helpers, identify non-static, feature-specific caller functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers)."
So, what are our target symbols here? Functions that *implement the logic* of our changes or are entrypoints for the affected features.
Let's focus on functions within `fs/file.c`. `fd_prepare` is an exported function via `EXPORT_SYMBOL`, so it's accessible. But is it ubiquitous? Let me think... it's only called by certain specific system calls or drivers when they're prepping file descriptors (like `perf_event_open`, `kvm create vm`, etc.). Wait... standard `open()` *doesn't* call `fd_prepare`. That's important! Let me confirm that by searching the source code for functions that are called in the `open` path. Let's dig into `do_sys_openat2` (or similar) in `fs/open.c` to see if regular `open` utilizes `fd_prepare` or `get_unused_fd_flags`!
|
| 16/2 | 2026/09/15 14:35 | tool | grepper |
0mError:no matches Args: {
"Expression": "fd_prepare",
"PathPrefix": "fs/open.c"
}Results: {
"error": "no matches"
} |
| 17/2 | 2026/09/15 14:35 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 74266 output: 30 thoughts: 21 Thoughts: Here's a first-person summary, tailored for an expert audience, based on the provided thought: **Diving into `fs/open.c` for File Descriptor Management** Okay, so I need to refresh my memory on how `open` deals with file descriptors. Specifically, I want to understand the mechanics within the kernel itself. Time to crack open `fs/open.c`. I'm interested in the underlying fd allocation and management strategy that's used. Let's see how they're handling potential race conditions, resource limits, and the general flow of `open` from a descriptor perspective. Hopefully, this will give me a clear picture of its internals. |
| 18/2 | 2026/09/15 14:35 | tool | grepper |
0mError:no matches Args: {
"Expression": "get_unused_fd",
"PathPrefix": "fs/open.c"
}Results: {
"error": "no matches"
} |
| 19/2 | 2026/09/15 14:35 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 74473 output: 21 thoughts: 19 Thoughts: Here's my thought process, as requested: **Investigating the Caller Landscape of `fd_prepare`** Okay, so I need to understand the call graph for `fd_prepare` within the kernel. My first instinct, and honestly, the most efficient approach for a broad overview like this, is to reach for a trusty `grep`. I'll run `grep` across the kernel source tree, specifically looking for instances where `fd_prepare` is invoked. This will give me a raw list of potential callers. From there, I can start to build a more comprehensive picture. This initial `grep` is a crucial first step to map out the landscape of how and where `fd_prepare` is being used. It will serve as the foundation for the deeper dives I'll inevitably need to conduct later. |
| 20/2 | 2026/09/15 14:36 | tool | grepper |
0mArgs:{
"Expression": "\\bfd_prepare\\b"
}Results: {
"Output": "drivers/accel/habanalabs/common/memory.c=1858=static int export_dmabuf(struct hl_ctx *ctx,\n--\ndrivers/accel/habanalabs/common/memory.c-1863-\tstruct hl_device *hdev = ctx-\u003ehdev;\ndrivers/accel/habanalabs/common/memory.c:1864:\tconst struct fd_slot *fd = fd_prepare(flags);\ndrivers/accel/habanalabs/common/memory.c-1865-\n--\ndrivers/dma-buf/dma-buf.c=436=static long dma_buf_export_sync_file(struct dma_buf *dmabuf,\n--\ndrivers/dma-buf/dma-buf.c-454-\ndrivers/dma-buf/dma-buf.c:455:\tfd = fd_prepare(O_CLOEXEC);\ndrivers/dma-buf/dma-buf.c-456-\tif (IS_ERR(fd))\n--\ndrivers/dma-buf/sw_sync.c=344=static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,\n--\ndrivers/dma-buf/sw_sync.c-346-{\ndrivers/dma-buf/sw_sync.c:347:\tconst struct fd_slot *fd = fd_prepare(O_CLOEXEC);\ndrivers/dma-buf/sw_sync.c-348-\tstruct sync_pt *pt;\n--\ndrivers/dma-buf/sync_file.c=213=static long sync_file_ioctl_merge(struct sync_file *sync_file,\n--\ndrivers/dma-buf/sync_file.c-215-{\ndrivers/dma-buf/sync_file.c:216:\tconst struct fd_slot *fd = fd_prepare(O_CLOEXEC);\ndrivers/dma-buf/sync_file.c-217-\tstruct sync_file *fence2, *fence3;\n--\ndrivers/gpu/drm/amd/amdgpu/amdgpu_cs.c=1576=int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,\n--\ndrivers/gpu/drm/amd/amdgpu/amdgpu_cs.c-1613-\tcase AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD:\ndrivers/gpu/drm/amd/amdgpu/amdgpu_cs.c:1614:\t\tfd = fd_prepare(O_CLOEXEC);\ndrivers/gpu/drm/amd/amdgpu/amdgpu_cs.c-1615-\t\tif (IS_ERR(fd)) {\n--\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c=1991=static int criu_get_prime_handle(struct kgd_mem *mem, int flags, u32 *shared_fd)\n--\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c-2002-\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c:2003:\tfd = fd_prepare(flags);\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c-2004-\tif (IS_ERR(fd)) {\n--\ndrivers/gpu/drm/drm_lease.c=475=int drm_mode_create_lease_ioctl(struct drm_device *dev,\n--\ndrivers/gpu/drm/drm_lease.c-531-\t/* Allocate a file descriptor for the lease */\ndrivers/gpu/drm/drm_lease.c:532:\tfd = fd_prepare(cl-\u003eflags \u0026 (O_CLOEXEC | O_NONBLOCK));\ndrivers/gpu/drm/drm_lease.c-533-\tif (IS_ERR(fd)) {\n--\ndrivers/gpu/drm/drm_prime.c=510=int drm_gem_prime_handle_to_fd(struct drm_device *dev,\n--\ndrivers/gpu/drm/drm_prime.c-515-\tstruct dma_buf *dmabuf;\ndrivers/gpu/drm/drm_prime.c:516:\tconst struct fd_slot *fd = fd_prepare(flags);\ndrivers/gpu/drm/drm_prime.c-517-\n--\ndrivers/gpu/drm/drm_syncobj.c=748=static int drm_syncobj_export_sync_file(struct drm_file *file_private,\n--\ndrivers/gpu/drm/drm_syncobj.c-753-\tstruct sync_file *sync_file;\ndrivers/gpu/drm/drm_syncobj.c:754:\tconst struct fd_slot *fd = fd_prepare(O_CLOEXEC);\ndrivers/gpu/drm/drm_syncobj.c-755-\n--\ndrivers/gpu/drm/etnaviv/etnaviv_gem_submit.c=410=int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,\n--\ndrivers/gpu/drm/etnaviv/etnaviv_gem_submit.c-506-\tif (args-\u003eflags \u0026 ETNA_SUBMIT_FENCE_FD_OUT) {\ndrivers/gpu/drm/etnaviv/etnaviv_gem_submit.c:507:\t\tout_fence_fd = fd_prepare(O_CLOEXEC);\ndrivers/gpu/drm/etnaviv/etnaviv_gem_submit.c-508-\t\tif (IS_ERR(out_fence_fd)) {\n--\ndrivers/gpu/drm/msm/msm_gem_submit.c=546=int msm_ioctl_gem_submit(struct drm_device *dev, void *data,\n--\ndrivers/gpu/drm/msm/msm_gem_submit.c-603-\tif (args-\u003eflags \u0026 MSM_SUBMIT_FENCE_FD_OUT) {\ndrivers/gpu/drm/msm/msm_gem_submit.c:604:\t\tout_fence_fd = fd_prepare(O_CLOEXEC);\ndrivers/gpu/drm/msm/msm_gem_submit.c-605-\t\tif (IS_ERR(out_fence_fd)) {\n--\ndrivers/gpu/drm/msm/msm_gem_vma.c=1431=msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)\n--\ndrivers/gpu/drm/msm/msm_gem_vma.c-1481-\tif (args-\u003eflags \u0026 MSM_VM_BIND_FENCE_FD_OUT) {\ndrivers/gpu/drm/msm/msm_gem_vma.c:1482:\t\tout_fence_fd = fd_prepare(O_CLOEXEC);\ndrivers/gpu/drm/msm/msm_gem_vma.c-1483-\t\tif (IS_ERR(out_fence_fd)) {\n--\ndrivers/gpu/drm/virtio/virtgpu_submit.c=363=static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,\n--\ndrivers/gpu/drm/virtio/virtgpu_submit.c-416-\tif (exbuf-\u003eflags \u0026 VIRTGPU_EXECBUF_FENCE_FD_OUT) {\ndrivers/gpu/drm/virtio/virtgpu_submit.c:417:\t\tsubmit-\u003eout_fence_fd = fd_prepare(O_CLOEXEC);\ndrivers/gpu/drm/virtio/virtgpu_submit.c-418-\t\tif (IS_ERR(submit-\u003eout_fence_fd))\n--\ndrivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c=4098=int vmw_execbuf_process(struct drm_file *file_priv,\n--\ndrivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c-4116-\tif (flags \u0026 DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {\ndrivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c:4117:\t\tout_fd = fd_prepare(O_CLOEXEC);\ndrivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c-4118-\t\tif (IS_ERR(out_fd)) {\n--\ndrivers/iommu/iommufd/eventq.c=415=static const struct fd_slot *iommufd_eventq_init(struct iommufd_eventq *eventq, char *name,\n--\ndrivers/iommu/iommufd/eventq.c-434-\ndrivers/iommu/iommufd/eventq.c:435:\treturn fd_prepare(O_CLOEXEC);\ndrivers/iommu/iommufd/eventq.c-436-}\n--\ndrivers/misc/fastrpc.c=1802=static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)\n--\ndrivers/misc/fastrpc.c-1826-\ndrivers/misc/fastrpc.c:1827:\tfd = fd_prepare(O_ACCMODE);\ndrivers/misc/fastrpc.c-1828-\tif (IS_ERR(fd)) {\n--\ndrivers/virt/nitro_enclaves/ne_misc_dev.c=1589=static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_uid)\n--\ndrivers/virt/nitro_enclaves/ne_misc_dev.c-1649-\ndrivers/virt/nitro_enclaves/ne_misc_dev.c:1650:\tenclave_fd = fd_prepare(O_CLOEXEC);\ndrivers/virt/nitro_enclaves/ne_misc_dev.c-1651-\tif (IS_ERR(enclave_fd)) {\n--\ndrivers/xen/gntdev-dmabuf.c=356=static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)\n--\ndrivers/xen/gntdev-dmabuf.c-359-\tstruct gntdev_dmabuf *gntdev_dmabuf __free(kfree) = NULL;\ndrivers/xen/gntdev-dmabuf.c:360:\tconst struct fd_slot *fd = fd_prepare(O_CLOEXEC);\ndrivers/xen/gntdev-dmabuf.c-361-\n--\nfs/file.c=712=static inline int fd_slot_find(struct fd_slots *slots, unsigned int fd)\n--\nfs/file.c-723-/**\nfs/file.c:724: * fd_prepare - allocate a descriptor that the syscall exit installs\nfs/file.c-725- * @flags: O_CLOEXEC or 0\n--\nfs/file.c-728- */\nfs/file.c:729:const struct fd_slot *fd_prepare(unsigned flags)\nfs/file.c-730-{\n--\nfs/file.c-742-}\nfs/file.c:743:EXPORT_SYMBOL(fd_prepare);\nfs/file.c-744-\n--\nfs/file.c-746- * fd_stage - attach the file to a prepared slot\nfs/file.c:747: * @slot: slot from fd_prepare()\nfs/file.c-748- * @file: the file to install, consumed\n--\nfs/file.c=761=EXPORT_SYMBOL(fd_stage);\n--\nfs/file.c-764- * fd_prepare_fd - the descriptor number of a prepared slot\nfs/file.c:765: * @slot: slot from fd_prepare()\nfs/file.c-766- */\n--\nfs/file.c=771=EXPORT_SYMBOL(fd_prepare_fd);\n--\nfs/file.c-774- * fd_prepare_file - the file staged into a slot, to configure before install\nfs/file.c:775: * @slot: slot from fd_prepare()\nfs/file.c-776- *\n--\ninclude/linux/file.h=125=struct fd_slot;\ninclude/linux/file.h:126:const struct fd_slot *fd_prepare(unsigned flags);\ninclude/linux/file.h-127-int fd_stage(const struct fd_slot *slot, struct file *file);\n--\ninclude/linux/file.h=138=extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;\n--\ninclude/linux/file.h-148- * bare install-and-return (reporting the number, configuring the file,\ninclude/linux/file.h:149: * installing several descriptors) use fd_prepare()/fd_stage().\ninclude/linux/file.h-150- */\n--\ninclude/linux/file.h-167- * FD_PREPARE - reserve a descriptor and stage @_file_owned on it for the\ninclude/linux/file.h:168: * install at syscall exit; declares @_fdf, an fd_prepare() slot.\ninclude/linux/file.h-169- * @_fdf: name of the const struct fd_slot * to declare\n--\ninclude/linux/file.h-173-#define FD_PREPARE(_fdf, _fd_flags, _file_owned)\t\t\t\\\ninclude/linux/file.h:174:\tconst struct fd_slot *_fdf = fd_prepare(_fd_flags);\t\t\\\ninclude/linux/file.h-175-\tif (!IS_ERR(_fdf)) {\t\t\t\t\t\t\\\n--\nkernel/events/core.c=13915=SYSCALL_DEFINE5(perf_event_open,\n--\nkernel/events/core.c-13992-\nkernel/events/core.c:13993:\tevent_fd = fd_prepare(f_flags);\nkernel/events/core.c-13994-\tif (IS_ERR(event_fd))\n--\nkernel/liveupdate/luo_core.c=270=static int luo_ioctl_create_session(struct luo_ucmd *ucmd)\n--\nkernel/liveupdate/luo_core.c-276-\nkernel/liveupdate/luo_core.c:277:\tfd = fd_prepare(O_CLOEXEC);\nkernel/liveupdate/luo_core.c-278-\tif (IS_ERR(fd))\n--\nkernel/liveupdate/luo_core.c=290=static int luo_ioctl_retrieve_session(struct luo_ucmd *ucmd)\n--\nkernel/liveupdate/luo_core.c-296-\nkernel/liveupdate/luo_core.c:297:\tfd = fd_prepare(O_CLOEXEC);\nkernel/liveupdate/luo_core.c-298-\tif (IS_ERR(fd))\n--\nkernel/liveupdate/luo_session.c=281=static int luo_session_retrieve_fd(struct luo_session *session,\n--\nkernel/liveupdate/luo_session.c-288-\nkernel/liveupdate/luo_session.c:289:\tfd = fd_prepare(O_CLOEXEC);\nkernel/liveupdate/luo_session.c-290-\tif (IS_ERR(fd))\n--\nkernel/seccomp.c=1954=static long seccomp_set_mode_filter(unsigned int flags,\n--\nkernel/seccomp.c-1992-\tif (flags \u0026 SECCOMP_FILTER_FLAG_NEW_LISTENER) {\nkernel/seccomp.c:1993:\t\tlistener = fd_prepare(O_CLOEXEC);\nkernel/seccomp.c-1994-\t\tif (IS_ERR(listener)) {\n--\nnet/core/scm.c=354=int scm_recv_one_fd(struct file *f, int __user *ufd, unsigned int flags,\n--\nnet/core/scm.c-366-\nnet/core/scm.c:367:\tfd = fd_prepare(flags);\nnet/core/scm.c-368-\tif (IS_ERR(fd))\n--\nnet/handshake/netlink.c=90=int handshake_nl_accept_doit(struct sk_buff *skb, struct genl_info *info)\n--\nnet/handshake/netlink.c-109-\t\t/* The ack carries the error, sendmsg() succeeds: stage last. */\nnet/handshake/netlink.c:110:\t\tconst struct fd_slot *fd = fd_prepare(O_CLOEXEC);\nnet/handshake/netlink.c-111-\n--\nnet/sctp/socket.c=5746=static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,\n--\nnet/sctp/socket.c-5758-\t/* Map the socket to a reserved fd that can be returned to the user. */\nnet/sctp/socket.c:5759:\tfd = fd_prepare(flags \u0026 SOCK_CLOEXEC);\nnet/sctp/socket.c-5760-\tif (IS_ERR(fd)) {\n--\nsound/core/compress_offload.c=1065=static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_task *utask)\n--\nsound/core/compress_offload.c-1088-\t}\nsound/core/compress_offload.c:1089:\tfd_i = fd_prepare(O_WRONLY|O_CLOEXEC);\nsound/core/compress_offload.c-1090-\tif (IS_ERR(fd_i)) {\n--\nsound/core/compress_offload.c-1093-\t}\nsound/core/compress_offload.c:1094:\tfd_o = fd_prepare(O_RDONLY|O_CLOEXEC);\nsound/core/compress_offload.c-1095-\tif (IS_ERR(fd_o)) {\n--\nvirt/kvm/guest_memfd.c=523=static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)\n--\nvirt/kvm/guest_memfd.c-531-\nvirt/kvm/guest_memfd.c:532:\tfd = fd_prepare(0);\nvirt/kvm/guest_memfd.c-533-\tif (IS_ERR(fd))\n--\nvirt/kvm/kvm_main.c=5480=static int kvm_dev_ioctl_create_vm(unsigned long type)\n--\nvirt/kvm/kvm_main.c-5487-\nvirt/kvm/kvm_main.c:5488:\tfd = fd_prepare(O_CLOEXEC);\nvirt/kvm/kvm_main.c-5489-\tif (IS_ERR(fd))\n"
} |
| 21/2 | 2026/09/15 14:36 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 79524 output: 20 thoughts: 43 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Unraveling the `FD_PREPARE` Call Stack** Right, so I'm seeing that `FD_PREPARE` is, rather elegantly, calling `fd_prepare()` – interesting recursion, or perhaps a more intricate design pattern? My initial thought is, what's *initiating* this whole process? I need to understand the control flow, so I'm instantly thinking, let's trace back. That's when I decide to immediately grep for `FD_PREPARE`. Time to get a quick overview of *every* instance, see where it's defined, how it's used across the codebase, and get a better picture of the calling context. The grep will quickly map the relationships and I can start forming a mental model of how it all ties together! From there I can delve deeper, as necessary. |
| 22/2 | 2026/09/15 14:36 | tool | grepper |
0mArgs:{
"Expression": "FD_PREPARE\\("
}Results: {
"Output": "arch/powerpc/platforms/cell/spufs/inode.c=266=static int spufs_context_open(const struct path *path)\narch/powerpc/platforms/cell/spufs/inode.c-267-{\narch/powerpc/platforms/cell/spufs/inode.c:268:\tFD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred()));\narch/powerpc/platforms/cell/spufs/inode.c-269-\tif (IS_ERR(fdf))\n--\narch/powerpc/platforms/cell/spufs/inode.c=495=static int spufs_gang_open(const struct path *path)\n--\narch/powerpc/platforms/cell/spufs/inode.c-500-\t */\narch/powerpc/platforms/cell/spufs/inode.c:501:\tFD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred()));\narch/powerpc/platforms/cell/spufs/inode.c-502-\tif (IS_ERR(fdf))\n--\ndrivers/char/tpm/tpm_vtpm_proxy.c=530=static struct file *vtpm_proxy_create_device(\n--\ndrivers/char/tpm/tpm_vtpm_proxy.c-546-\t/* setup an anonymous file for the server-side */\ndrivers/char/tpm/tpm_vtpm_proxy.c:547:\tFD_PREPARE(fdf, O_RDWR,\ndrivers/char/tpm/tpm_vtpm_proxy.c-548-\t\t anon_inode_getfile(\"[vtpms]\", \u0026vtpm_proxy_fops, proxy_dev,\n--\ndrivers/gpio/gpiolib-cdev.c=303=static int linehandle_create(struct gpio_device *gdev, void __user *ip)\n--\ndrivers/gpio/gpiolib-cdev.c-376-\ndrivers/gpio/gpiolib-cdev.c:377:\tFD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,\ndrivers/gpio/gpiolib-cdev.c-378-\t\t anon_inode_getfile(\"gpio-linehandle\", \u0026linehandle_fileops,\n--\ndrivers/gpio/gpiolib-cdev.c=1602=static int linereq_create(struct gpio_device *gdev, void __user *ip)\n--\ndrivers/gpio/gpiolib-cdev.c-1712-\ndrivers/gpio/gpiolib-cdev.c:1713:\tFD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,\ndrivers/gpio/gpiolib-cdev.c-1714-\t\t anon_inode_getfile(\"gpio-line\", \u0026line_fileops, lr,\n--\ndrivers/gpio/gpiolib-cdev.c=2002=static int lineevent_create(struct gpio_device *gdev, void __user *ip)\n--\ndrivers/gpio/gpiolib-cdev.c-2110-\ndrivers/gpio/gpiolib-cdev.c:2111:\tFD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,\ndrivers/gpio/gpiolib-cdev.c-2112-\t\t anon_inode_getfile(\"gpio-event\", \u0026lineevent_fileops, le,\n--\ndrivers/gpu/drm/drm_syncobj.c=664=int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)\ndrivers/gpu/drm/drm_syncobj.c-665-{\ndrivers/gpu/drm/drm_syncobj.c:666:\tFD_PREPARE(fdf, O_CLOEXEC,\ndrivers/gpu/drm/drm_syncobj.c-667-\t\t anon_inode_getfile(\"syncobj_file\", \u0026drm_syncobj_file_fops,\n--\ndrivers/gpu/drm/msm/msm_perfcntr.c=370=msm_ioctl_perfcntr_config(struct drm_device *dev, void *data, struct drm_file *file)\n--\ndrivers/gpu/drm/msm/msm_perfcntr.c-543-\ndrivers/gpu/drm/msm/msm_perfcntr.c:544:\t\tFD_PREPARE(fdf, O_CLOEXEC,\ndrivers/gpu/drm/msm/msm_perfcntr.c-545-\t\t\t anon_inode_getfile(\"[msm_perfcntrs]\", \u0026stream_fops, stream, 0));\n--\ndrivers/hv/mshv_root_main.c=1068=mshv_partition_ioctl_create_vp(struct mshv_partition *partition,\n--\ndrivers/hv/mshv_root_main.c-1148-\ndrivers/hv/mshv_root_main.c:1149:\tFD_PREPARE(fdf, O_RDWR | O_CLOEXEC,\ndrivers/hv/mshv_root_main.c-1150-\t\t anon_inode_getfile(\"mshv_vp\", \u0026mshv_vp_fops, vp,\n--\ndrivers/hv/mshv_vtl_main.c=114=mshv_ioctl_create_vtl(void __user *user_arg, struct device *module_dev)\n--\ndrivers/hv/mshv_vtl_main.c-121-\ndrivers/hv/mshv_vtl_main.c:122:\tFD_PREPARE(fdf, O_CLOEXEC,\ndrivers/hv/mshv_vtl_main.c-123-\t\t anon_inode_getfile(\"mshv_vtl\", \u0026mshv_vtl_fops, vtl, O_RDWR));\n--\ndrivers/iio/industrialio-buffer.c=2038=static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg)\n--\ndrivers/iio/industrialio-buffer.c-2069-\ndrivers/iio/industrialio-buffer.c:2070:\tFD_PREPARE(fdf, O_RDWR | O_CLOEXEC,\ndrivers/iio/industrialio-buffer.c-2071-\t\t anon_inode_getfile(\"iio:buffer\", \u0026iio_buffer_chrdev_fileops,\n--\ndrivers/media/mc/mc-request.c=289=int media_request_alloc(struct media_device *mdev, int *alloc_fd)\n--\ndrivers/media/mc/mc-request.c-315-\ndrivers/media/mc/mc-request.c:316:\tFD_PREPARE(fdf, O_CLOEXEC,\ndrivers/media/mc/mc-request.c-317-\t\t anon_inode_getfile(\"request\", \u0026request_fops, NULL,\n--\ndrivers/misc/ntsync.c=723=static int ntsync_obj_get_fd(struct ntsync_obj *obj)\ndrivers/misc/ntsync.c-724-{\ndrivers/misc/ntsync.c:725:\tFD_PREPARE(fdf, O_CLOEXEC,\ndrivers/misc/ntsync.c-726-\t\t anon_inode_getfile(\"ntsync\", \u0026ntsync_obj_fops, obj, O_RDWR));\n--\ndrivers/vfio/vfio_main.c=903=static int vfio_ioct_mig_return_fd(struct file *filp, void __user *arg,\n--\ndrivers/vfio/vfio_main.c-905-{\ndrivers/vfio/vfio_main.c:906:\tFD_PREPARE(fdf, O_CLOEXEC, filp);\ndrivers/vfio/vfio_main.c-907-\n--\nfs/eventfd.c=379=static int do_eventfd(unsigned int count, int flags)\n--\nfs/eventfd.c-402-\nfs/eventfd.c:403:\tFD_PREPARE(fdf, flags,\nfs/eventfd.c-404-\t\t anon_inode_getfile_fmode(\"[eventfd]\", \u0026eventfd_fops, ctx,\n--\nfs/eventpoll.c=2494=static int do_epoll_create(int flags)\n--\nfs/eventpoll.c-2513-\t */\nfs/eventpoll.c:2514:\tFD_PREPARE(fdf, O_RDWR | (flags \u0026 O_CLOEXEC),\nfs/eventpoll.c-2515-\t\t anon_inode_getfile(\"[eventpoll]\", \u0026eventpoll_fops, ep,\n--\nfs/namespace.c=4442=SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,\n--\nfs/namespace.c-4544-\nfs/namespace.c:4545:\tFD_PREPARE(fdf, (flags \u0026 FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0,\nfs/namespace.c-4546-\t\t dentry_open(\u0026new_path, O_PATH, fc-\u003ecred));\n--\nfs/namespace.c=5193=SYSCALL_DEFINE5(open_tree_attr, int, dfd, const char __user *, filename,\n--\nfs/namespace.c-5199-\nfs/namespace.c:5200:\tFD_PREPARE(fdf, flags, vfs_open_tree(dfd, filename, flags));\nfs/namespace.c-5201-\tif (IS_ERR(fdf))\n--\nfs/nsfs.c=213=static long ns_ioctl(struct file *filp, unsigned int ioctl,\n--\nfs/nsfs.c-349-\nfs/nsfs.c:350:\t\tFD_PREPARE(fdf, O_CLOEXEC, dentry_open(\u0026path, O_RDONLY, current_cred()));\nfs/nsfs.c-351-\t\tif (IS_ERR(fdf))\n--\nfs/xfs/xfs_handle.c=231=xfs_open_by_handle(\n--\nfs/xfs/xfs_handle.c-273-\nfs/xfs/xfs_handle.c:274:\tFD_PREPARE(fdf, 0, dentry_open(\u0026path, hreq-\u003eoflags, cred));\nfs/xfs/xfs_handle.c-275-\tif (IS_ERR(fdf))\n--\ninclude/linux/file.h=138=extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;\n--\ninclude/linux/file.h-172- */\ninclude/linux/file.h:173:#define FD_PREPARE(_fdf, _fd_flags, _file_owned)\t\t\t\\\ninclude/linux/file.h-174-\tconst struct fd_slot *_fdf = fd_prepare(_fd_flags);\t\t\\\n--\nio_uring/mock_file.c=210=static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flags)\n--\nio_uring/mock_file.c-256-\nio_uring/mock_file.c:257:\tFD_PREPARE(fdf, O_RDWR | O_CLOEXEC,\nio_uring/mock_file.c-258-\t\t anon_inode_create_getfile(\"[io_uring_mock]\", fops, mf,\n--\nio_uring/zcrx.c=794=static int zcrx_export(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq,\n--\nio_uring/zcrx.c-804-\nio_uring/zcrx.c:805:\tFD_PREPARE(fdf, O_CLOEXEC,\nio_uring/zcrx.c-806-\t\t anon_inode_create_getfile(\"[zcrx]\", \u0026zcrx_box_fops, ifq,\n--\nkernel/bpf/bpf_iter.c=634=int bpf_iter_new_fd(struct bpf_link *link)\n--\nkernel/bpf/bpf_iter.c-644-\nkernel/bpf/bpf_iter.c:645:\tFD_PREPARE(fdf, flags, anon_inode_getfile(\"bpf_iter\", \u0026bpf_iter_fops, NULL, flags));\nkernel/bpf/bpf_iter.c-646-\tif (IS_ERR(fdf))\n--\nkernel/bpf/token.c=112=int bpf_token_create(union bpf_attr *attr)\n--\nkernel/bpf/token.c-168-\nkernel/bpf/token.c:169:\tFD_PREPARE(fdf, O_CLOEXEC,\nkernel/bpf/token.c-170-\t\t alloc_file_pseudo(inode, path.mnt, BPF_TOKEN_INODE_NAME,\n--\nmm/userfaultfd.c=4784=static int new_userfaultfd(int flags)\n--\nmm/userfaultfd.c-4807-\nmm/userfaultfd.c:4808:\tFD_PREPARE(fdf, flags \u0026 UFFD_SHARED_FCNTL_FLAGS,\nmm/userfaultfd.c-4809-\t\t anon_inode_create_getfile(\"[userfaultfd]\", \u0026userfaultfd_fops, ctx,\n--\nnet/handshake/handshake-test.c=362=static void handshake_req_cancel_test2(struct kunit *test)\n--\nnet/handshake/handshake-test.c-396-\tKUNIT_ASSERT_PTR_EQ(test, req, next);\nnet/handshake/handshake-test.c:397:\t/* Simulate FD_PREPARE() consuming the file reference handed\nnet/handshake/handshake-test.c-398-\t * off by handshake_req_next(); see handshake_nl_accept_doit().\n--\nnet/handshake/handshake-test.c=412=static void handshake_req_cancel_test3(struct kunit *test)\n--\nnet/handshake/handshake-test.c-446-\tKUNIT_ASSERT_PTR_EQ(test, req, next);\nnet/handshake/handshake-test.c:447:\t/* Simulate FD_PREPARE() consuming the file reference handed\nnet/handshake/handshake-test.c-448-\t * off by handshake_req_next(); see handshake_nl_accept_doit().\n--\nnet/handshake/request.c=169=static bool remove_pending(struct handshake_net *hn, struct handshake_req *req)\n--\nnet/handshake/request.c-189- * On a non-NULL return, the caller owns an extra reference\nnet/handshake/request.c:190: * on @req-\u003ehr_file. FD_PREPARE() consumes it on success; on\nnet/handshake/request.c:191: * the FD_PREPARE() failure path the caller must fput() it.\nnet/handshake/request.c-192- *\n--\nnet/handshake/request.c=195=struct handshake_req *handshake_req_next(struct handshake_net *hn, int class)\n--\nnet/handshake/request.c-206-\t\t * hn_lock. A concurrent handshake_req_cancel() can drop\nnet/handshake/request.c:207:\t\t * hr_file before accept reaches FD_PREPARE(); this extra\nnet/handshake/request.c:208:\t\t * reference keeps the file alive until FD_PREPARE() takes\nnet/handshake/request.c-209-\t\t * ownership.\n--\nnet/kcm/kcmsock.c=1554=static int kcm_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)\n--\nnet/kcm/kcmsock.c-1581-\nnet/kcm/kcmsock.c:1582:\t\tFD_PREPARE(fdf, 0, kcm_clone(sock));\nnet/kcm/kcmsock.c-1583-\t\tif (IS_ERR(fdf))\n--\nvirt/kvm/kvm_main.c=4327=static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu)\n--\nvirt/kvm/kvm_main.c-4332-\nvirt/kvm/kvm_main.c:4333:\tFD_PREPARE(fdf, O_CLOEXEC,\nvirt/kvm/kvm_main.c-4334-\t\t anon_inode_getfile_fmode(name, \u0026kvm_vcpu_stats_fops, vcpu,\n--\nvirt/kvm/kvm_main.c=5128=static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm)\nvirt/kvm/kvm_main.c-5129-{\nvirt/kvm/kvm_main.c:5130:\tFD_PREPARE(fdf, O_CLOEXEC,\nvirt/kvm/kvm_main.c-5131-\t\t anon_inode_getfile_fmode(\"kvm-vm-stats\", \u0026kvm_vm_stats_fops,\n"
} |
| 23/2 | 2026/09/15 14:36 | llm | patch-evaluator |
2mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 84245 output: 28 thoughts: 2865 Thoughts: **Analysis of `FD_PREPARE` and Related File Descriptor Management in the Linux Kernel**
Okay, here's what I'm seeing. This `FD_PREPARE` concept is used *everywhere*. It touches so many subsystems: eventfds, epoll, namespaces, XFS, BPF, perf events, seccomp, userfaultfd, SCM, SCTP, KVM, io_uring... the list goes on. This `fd_prepare` thing is clearly a fundamental building block.
Now, let's drill down into the core implementation in `fs/file.c`. We've got `fd_prepare`, `fd_stage`, `fd_prepare_fd`, `fd_prepare_file`, `fd_slots_commit`, `__fd_slots_commit`, `exit_fd_slots`, and `put_unused_fd` all involved. And look at all these new `VFS_WARN_ON_ONCE` assertions! It seems there are several new checks to ensure that things are being handled correctly. This is a big clue that there are potentially some vulnerabilities to be found.
Specifically, I'm noting the presence of assertions in places like `io_req_fd_reservations`, `begin_new_exec`, `unshare_fd`, and `exit_fd_slots`. These assertions seem designed to catch races, leaks, or corruption related to prepared descriptors. If there's a failure or asynchronous operation, or an unshare/execve, these assertions *should* trip if things aren't correctly handled. The potential is there for use-after-free, double-free, file table corruption, or fd leaks.
The question now is how to focus the fuzzer. Let's think about FocusSymbols. According to the guidelines, we should avoid ubiquitous functions and target specific logic. `fd_slots_commit` is key because it is called via a `SYSCALL_WORK_FD_SLOTS` check, and that is only set when a `fd_prepare()` is called. In other words, this happens *only* with the feature and not on every syscall. This means that while its called on almost every syscall exit, this function is *targeted*.
Okay, so I think the key functions to target are:
* `fd_prepare`
* `fd_stage`
* `fd_slots_commit`
* `__fd_slots_commit`
* `exit_fd_slots`
Those are *precisely* the new subsystem functions.
Let's look at the other locations that were changed. The others seem relevant too:
* `scm_recv_one_fd`
* `io_req_post_cqe`
Now, regarding `EnableConfigs`. We need to determine what configurations are needed for the code to compile and be reached. `fs/file.c` is always built. However, the io_uring component is only compiled when `CONFIG_IO_URING` is enabled. Similarly, `USERFAULTFD`, `SECCOMP`, and `KVM` are all gated by their respective `CONFIG_` options. I need to make sure I note `CONFIG_IO_URING`, and so on.
Let's go with the following as a base:
* FocusSymbols:
* `fd_prepare`
* `fd_stage`
* `fd_slots_commit`
* `__fd_slots_commit`
* `exit_fd_slots`
* `scm_recv_one_fd`
* `io_req_post_cqe`
* EnableConfigs:
* `IO_URING`
* `USERFAULTFD`
* `SECCOMP`
* `KVM`
* `BPF_SYSCALL`
* `PERF_EVENTS`
|
| 24/2 | 2026/09/15 14:38 | tool | grepper |
0mArgs:{
"Expression": "config IO_URING",
"PathPrefix": "init/Kconfig"
}Results: {
"Output": "init/Kconfig=1952=config AIO\n--\ninit/Kconfig-1959-\ninit/Kconfig:1960:config IO_URING\ninit/Kconfig-1961-\tbool \"Enable IO uring support\" if EXPERT\n--\ninit/Kconfig=1969=config GCOV_PROFILE_URING\n--\ninit/Kconfig-1981-\ninit/Kconfig:1982:config IO_URING_MOCK_FILE\ninit/Kconfig-1983-\ttristate \"Enable io_uring mock files (Experimental)\" if EXPERT\n"
} |
| 25/2 | 2026/09/15 14:38 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 88159 output: 17 thoughts: 337 Thoughts: **My Initial Assessment of the Patch** Okay, let's dive in. First, I need to check `IO_URING_MOCK_FILE`. Hmm, looks like the patch touched `io_uring/mock_file.c`, specifically this function: `static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flags)`. Alright, got it. And also, `io_uring/zcrx.c`, where there's a modification to `zcrx_export`. Now, I need to understand the dependencies. Let me see if there are any related configurations in `fs/Kconfig`, perhaps related to `userfaultfd` or something similar. Ah, yes, `USERFAULTFD` is there, and `SECCOMP` is in `arch/Kconfig`. The critical question now: Are any of these configs *required* for the core changes? It looks like the heart of the matter lies within `fs/file.c` and `include/linux/entry-common.h`, which are, as expected, unconditionally built into `vmlinux` for the amd64 architecture. However, to actually *exercise* the code paths modified in `io_uring`, `userfaultfd`, `seccomp`, etc., enabling `IO_URING`, `USERFAULTFD`, and `SECCOMP` will definitely be beneficial to my testing. So, if I'm providing a list of configurations, I should definitely include `["IO_URING", "USERFAULTFD", "SECCOMP"]`. But are there any others? Should I also be checking for something like `LIVEUPDATE`? Let's figure out what `CONFIG_LIVEUPDATE` is all about. I should check `kernel/liveupdate/Kconfig` or grep for `config LIVEUPDATE` to be sure. |
| 26/2 | 2026/09/15 14:38 | tool | grepper |
0mArgs:{
"Expression": "config LIVEUPDATE"
}Results: {
"Output": "kernel/liveupdate/Kconfig=38=config KEXEC_HANDOVER_ENABLE_DEFAULT\n--\nkernel/liveupdate/Kconfig-51-\nkernel/liveupdate/Kconfig:52:config LIVEUPDATE\nkernel/liveupdate/Kconfig-53-\tbool \"Live Update Orchestrator\"\n--\nkernel/liveupdate/Kconfig-72-\nkernel/liveupdate/Kconfig:73:config LIVEUPDATE_MEMFD\nkernel/liveupdate/Kconfig-74-\tbool \"Live update support for memfd\"\n--\nlib/Kconfig.debug=2872=config CONTEXT_ANALYSIS_TEST\n--\nlib/Kconfig.debug-2885-\nlib/Kconfig.debug:2886:config LIVEUPDATE_TEST\nlib/Kconfig.debug-2887-\tbool \"Live Update Kernel Test\"\n"
} |
| 27/2 | 2026/09/15 14:38 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 89121 output: 287 thoughts: 893 Thoughts: **My Assessment of the `IO_URING` Fuzzing Strategy** Okay, let's break this down. From what I see, `IO_URING` is definitely a key area for fuzzing. The direct handling within `io_uring.c` with those explicit checks like `__fd_slots_commit(res)` when `current->fd_slots.nr` is not zero and the call to `io_req_fd_reservations()` with that `WARN_ON_ONCE` are flashing neon signs. Plus, the fact that syzkaller is already giving it heavy fuzzing support makes it a prime candidate. Now, let's confirm the requirements. `WorthFuzzing` seems correct here, and the reasoning is concise. The `FocusSymbols` list is critical. Initially, I had a thought about `fd_prepare_fd`, but it's a simple accessor and probably inlined. However, the core functions are: `fd_prepare`, `fd_stage`, `fd_slots_commit`, `__fd_slots_commit`, and `exit_fd_slots`. I confirmed they are non-static functions within `fs/file.c`, which is a good sign. And `io_req_post_cqe` which is called in `io_uring.c` and `scm_recv_one_fd` in `net/core/scm.c` also need attention. Regarding `EnableConfigs`, since we're dealing with fundamental VFS changes and amd64 entry/exit logic, no *specific* non-default configs are *required* to compile or even reach the touched functions. However, if we think of optional subsystems that *interact* with `fd_slots`, then `IO_URING` (with its dedicated commit/abort logic and assertions), `USERFAULTFD`, `SECCOMP`, and `PERF_EVENTS` are all on the table. Listing just `["IO_URING"]` or even `["IO_URING", "USERFAULTFD", "SECCOMP"]` seems accurate enough. Finally, the sanity check on `WorthFuzzing`: core subsystem changes? Yep, VFS, syscall handling, the scheduler, fork/exec/exit, io_uring, networking - it's all affected. Are there new assertions? Absolutely, with the `VFS_WARN_ON_ONCE` and `WARN_ON_ONCE` sprinkled throughout. And, does it modify reachable code on amd64? Yes. Therefore, `WorthFuzzing = true`. I'm confident in this analysis. Everything appears to be well-considered. I'm ready to call `set-results`. |
| 28/2 | 2026/09/15 14:39 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"IO_URING",
"USERFAULTFD",
"SECCOMP"
],
"FocusSymbols": [
"fd_prepare",
"fd_stage",
"fd_slots_commit",
"__fd_slots_commit",
"exit_fd_slots",
"io_req_post_cqe",
"scm_recv_one_fd"
],
"Reasoning": "The patch series introduces a core VFS mechanism (fd_slots) to defer file descriptor installation until syscall exit, committing staged files on success and releasing/dropping them on error. It modifies core task lifecycle code (task_struct, fork, exec, exit), architecture syscall exit handling (SYSCALL_WORK_FD_SLOTS in generic entry common), io_uring completion handling, and various syscall/ioctl entrypoints (perf_event_open, userfaultfd, seccomp, eventfd, epoll, scm, etc.). It also introduces multiple new assertions (VFS_WARN_ON_ONCE and WARN_ON_ONCE) in reachability paths including exec, unshare, task exit, and io_uring. Fuzzing is warranted to discover potential descriptor leaks, premature commits/drops, or triggered assertions during complex error unwind or asynchronous completion paths.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"IO_URING",
"USERFAULTFD",
"SECCOMP"
],
"FocusSymbols": [
"fd_prepare",
"fd_stage",
"fd_slots_commit",
"__fd_slots_commit",
"exit_fd_slots",
"io_req_post_cqe",
"scm_recv_one_fd"
],
"Reasoning": "The patch series introduces a core VFS mechanism (fd_slots) to defer file descriptor installation until syscall exit, committing staged files on success and releasing/dropping them on error. It modifies core task lifecycle code (task_struct, fork, exec, exit), architecture syscall exit handling (SYSCALL_WORK_FD_SLOTS in generic entry common), io_uring completion handling, and various syscall/ioctl entrypoints (perf_event_open, userfaultfd, seccomp, eventfd, epoll, scm, etc.). It also introduces multiple new assertions (VFS_WARN_ON_ONCE and WARN_ON_ONCE) in reachability paths including exec, unshare, task exit, and io_uring. Fuzzing is warranted to discover potential descriptor leaks, premature commits/drops, or triggered assertions during complex error unwind or asynchronous completion paths.",
"WorthFuzzing": true
} |
| 29/1 | 2026/09/15 14:39 | action | If |
18mArgs:{
"WorthFuzzing": true
}Results: null |
| 30/2 | 2026/09/15 14:39 | agent | kmsan-evaluator |
18mModel:core Args: null Results: {
"KMSANReasoning": "The patch series refactors file descriptor reservation and installation across syscalls, io_uring, and drivers by deferring publication to syscall exit time via `fd_prepare()`, `fd_stage()`, and `fd_slots_commit()`.\n\nFrom a KMSAN perspective:\n1. `task_struct::fd_slots` is explicitly zeroed on task duplication via `memset(\u0026tsk-\u003efd_slots, 0, sizeof(tsk-\u003efd_slots))` in `dup_task_struct()`.\n2. When recording a slot in `fd_slot_record()`, both fields of `struct fd_slot` (`fd` and `file`) are unconditionally initialized before `slots-\u003enr` is incremented. All loops in `fd_slots_install()`, `fd_slots_drop()`, and `fd_slot_find()` strictly iterate over indices `0` to `slots-\u003enr - 1`, ensuring uninitialized spill buffer elements are never read.\n3. No kernel-to-user memory info-leaks are introduced. The data copied to userspace in the converted call sites (such as ioctl output arguments or `put_user` for fd numbers) consists of explicitly initialized scalar descriptors or structs that were either zeroed or populated from `copy_from_user()`.\n4. The risks introduced by this patch relate to descriptor/file reference counting (e.g. potential double free or use-after-free on error paths), locking (`files-\u003efile_lock`), and architecture-specific syscall exit register state. These hazards are addressed by KASAN, LOCKDEP, and standard kernel debugging facilities, not KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit a40598dcddd1056cb33bf7ce7eca154940bc9aaf
Author: syz-cluster <triage@syzkaller.com>
Date: Tue Sep 15 14:31:17 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/alpha/include/asm/thread_info.h b/arch/alpha/include/asm/thread_info.h
index 1552ecca85207..db8465f5fabb2 100644
--- a/arch/alpha/include/asm/thread_info.h
+++ b/arch/alpha/include/asm/thread_info.h
@@ -67,6 +67,7 @@ register unsigned long *current_stack_pointer __asm__ ("$30");
#define TIF_NOTIFY_SIGNAL 5 /* signal notifications exist */
#define TIF_SECCOMP 6 /* seccomp syscall filtering active */
#define TIF_SYSCALL_TRACEPOINT 7 /* syscall tracepoint instrumentation */
+#define TIF_FD_SLOTS 8 /* syscall prepared descriptors */
#define TIF_DIE_IF_KERNEL 9 /* dik recursion lock */
#define TIF_MEMDIE 13 /* is terminating due to OOM killer */
#define TIF_POLLING_NRFLAG 14 /* idle is polling for TIF_NEED_RESCHED */
@@ -80,6 +81,7 @@ register unsigned long *current_stack_pointer __asm__ ("$30");
#define _TIF_SECCOMP (1<<TIF_SECCOMP)
#define _TIF_POLLING_NRFLAG (1<<TIF_POLLING_NRFLAG)
#define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
/*
* Work to do on syscall entry (in entry.S).
diff --git a/arch/alpha/kernel/entry.S b/arch/alpha/kernel/entry.S
index 9f2608de25446..20372e9263591 100644
--- a/arch/alpha/kernel/entry.S
+++ b/arch/alpha/kernel/entry.S
@@ -569,6 +569,13 @@ entSys:
$ret_success:
stq $0, 0($sp)
stq $31, 72($sp) /* a3=0 => no error */
+$syscall_exit:
+ /* Descriptors reserved by the syscall are committed in
+ syscall_trace_leave, off the return path interrupts share. */
+ ldl $17, TI_FLAGS($8)
+ lda $2, _TIF_FD_SLOTS
+ and $17, $2, $2
+ bne $2, $fd_slots_exit
.align 4
.globl ret_from_sys_call
@@ -612,7 +619,7 @@ $syscall_error:
stq $0, 0($sp)
mov $31, $26 /* tell "ret_from_sys_call" we can restart */
stq $1, 72($sp) /* a3 for return */
- br ret_from_sys_call
+ br $syscall_exit
/*
* Do all cleanup when returning from all interrupts and system calls.
@@ -648,6 +655,21 @@ $work_notifysig:
UNDO_SWITCH_STACK
br restore_all
+ .align 4
+$fd_slots_exit:
+ /* Keep the return address and the syscall restart state in
+ $26/$18/$19 across the call. */
+ DO_SWITCH_STACK
+ mov $26, $9
+ mov $18, $10
+ mov $19, $11
+ jsr $26, syscall_trace_leave
+ mov $9, $26
+ mov $10, $18
+ mov $11, $19
+ UNDO_SWITCH_STACK
+ br ret_from_sys_call
+
/*
* PTRACE syscall handler
*/
diff --git a/arch/alpha/kernel/ptrace.c b/arch/alpha/kernel/ptrace.c
index fc8f6cedbb286..736218b34601e 100644
--- a/arch/alpha/kernel/ptrace.c
+++ b/arch/alpha/kernel/ptrace.c
@@ -6,6 +6,7 @@
/* more mutilation by David Mosberger (davidm@azstarnet.com) */
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <linux/mm.h>
@@ -507,6 +508,8 @@ asmlinkage unsigned long syscall_trace_enter(void)
asmlinkage void
syscall_trace_leave(void)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(current_pt_regs());
audit_syscall_exit(current_pt_regs());
if (test_thread_flag(TIF_SYSCALL_TRACE))
ptrace_report_syscall_exit(current_pt_regs(), 0);
diff --git a/arch/arc/include/asm/thread_info.h b/arch/arc/include/asm/thread_info.h
index 255d2c7742190..4c67d4eb434b9 100644
--- a/arch/arc/include/asm/thread_info.h
+++ b/arch/arc/include/asm/thread_info.h
@@ -77,6 +77,7 @@ static inline __attribute_const__ struct thread_info *current_thread_info(void)
#define TIF_NEED_RESCHED 3 /* rescheduling necessary */
#define TIF_SYSCALL_AUDIT 4 /* syscall auditing active */
#define TIF_NOTIFY_SIGNAL 5 /* signal notifications exist */
+#define TIF_FD_SLOTS 6 /* syscall prepared descriptors */
#define TIF_SYSCALL_TRACE 15 /* syscall trace active */
/* true if poll_idle() is polling TIF_NEED_RESCHED */
#define TIF_MEMDIE 16
@@ -88,6 +89,7 @@ static inline __attribute_const__ struct thread_info *current_thread_info(void)
#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED)
#define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT)
#define _TIF_NOTIFY_SIGNAL (1<<TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_MEMDIE (1<<TIF_MEMDIE)
#define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
diff --git a/arch/arc/kernel/entry.S b/arch/arc/kernel/entry.S
index 3c7e74aba6794..f436391526e16 100644
--- a/arch/arc/kernel/entry.S
+++ b/arch/arc/kernel/entry.S
@@ -241,6 +241,11 @@ ENTRY(EV_Trap)
jl [r9]
.Lret_from_system_call:
+ ; descriptors reserved by the syscall are committed in syscall_trace_exit()
+ GET_CURR_THR_INFO_FLAGS r10
+ and.f 0, r10, _TIF_FD_SLOTS
+ bnz tracesys_exit
+
st r0, [sp, PT_r0] ; sys call return value in pt_regs
; fall through to ret_from_exception
diff --git a/arch/arc/kernel/ptrace.c b/arch/arc/kernel/ptrace.c
index fe2b6572e9199..0a0d74b9c7c43 100644
--- a/arch/arc/kernel/ptrace.c
+++ b/arch/arc/kernel/ptrace.c
@@ -3,6 +3,7 @@
* Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
*/
+#include <linux/file.h>
#include <linux/ptrace.h>
#include <linux/sched/task_stack.h>
#include <linux/regset.h>
@@ -355,6 +356,9 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs)
asmlinkage void syscall_trace_exit(struct pt_regs *regs)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
if (test_thread_flag(TIF_SYSCALL_TRACE))
ptrace_report_syscall_exit(regs, 0);
diff --git a/arch/arm/include/asm/thread_info.h b/arch/arm/include/asm/thread_info.h
index 943ffcf069d29..5337449e8409d 100644
--- a/arch/arm/include/asm/thread_info.h
+++ b/arch/arm/include/asm/thread_info.h
@@ -144,6 +144,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp *,
#define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
#define TIF_UPROBE 3 /* breakpointed or singlestepping */
#define TIF_NOTIFY_SIGNAL 4 /* signal notifications exist */
+#define TIF_FD_SLOTS 5 /* syscall prepared descriptors, entry-common.S needs it below 16 */
#define TIF_USING_IWMMXT 17
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
@@ -163,6 +164,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp *,
#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_USING_IWMMXT (1 << TIF_USING_IWMMXT)
/* Checks for any syscall work in entry-common.S */
diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S
index 88336a1292bb7..59a70d90b0b61 100644
--- a/arch/arm/kernel/entry-common.S
+++ b/arch/arm/kernel/entry-common.S
@@ -83,6 +83,8 @@ ENDPROC(ret_fast_syscall)
tst r1, #_TIF_SYSCALL_WORK
bne __sys_trace_return_nosave
+ tst r1, #(_TIF_FD_SLOTS << 16) @ r1 holds TI_FLAGS << 16
+ bne __sys_trace_return_nosave
slow_work_pending:
mov r0, sp @ 'regs'
mov r2, why @ 'syscall'
diff --git a/arch/arm/kernel/ptrace.c b/arch/arm/kernel/ptrace.c
index ed7a2a87a6707..75fe26012fcbc 100644
--- a/arch/arm/kernel/ptrace.c
+++ b/arch/arm/kernel/ptrace.c
@@ -11,6 +11,7 @@
#include <linux/sched/task_stack.h>
#include <linux/mm.h>
#include <linux/elf.h>
+#include <linux/file.h>
#include <linux/smp.h>
#include <linux/ptrace.h>
#include <linux/user.h>
@@ -876,6 +877,9 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs)
asmlinkage void syscall_trace_exit(struct pt_regs *regs)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
/*
* Audit the syscall before anything else, as a debugger may
* come in and change the current registers.
diff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/thread_info.h
index 5d7fe3e153c85..e48006eb98a85 100644
--- a/arch/arm64/include/asm/thread_info.h
+++ b/arch/arm64/include/asm/thread_info.h
@@ -74,6 +74,7 @@ void arch_setup_new_exec(void);
#define TIF_SECCOMP 11 /* syscall secure computing */
#define TIF_SYSCALL_EMU 12 /* syscall emulation active */
#define TIF_PATCH_PENDING 13 /* pending live patching update */
+#define TIF_FD_SLOTS 14 /* syscall prepared descriptors */
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
#define TIF_FREEZE 19
#define TIF_RESTORE_SIGMASK 20
@@ -107,10 +108,11 @@ void arch_setup_new_exec(void);
#define _TIF_MTE_ASYNC_FAULT (1 << TIF_MTE_ASYNC_FAULT)
#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)
#define _TIF_TSC_SIGSEGV (1 << TIF_TSC_SIGSEGV)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
_TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \
- _TIF_SYSCALL_EMU)
+ _TIF_SYSCALL_EMU | _TIF_FD_SLOTS)
#ifdef CONFIG_SHADOW_CALL_STACK
#define INIT_SCS \
diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c
index f743cbec1c3ad..3bedfed977d47 100644
--- a/arch/arm64/kernel/ptrace.c
+++ b/arch/arm64/kernel/ptrace.c
@@ -28,6 +28,7 @@
#include <linux/hw_breakpoint.h>
#include <linux/regset.h>
#include <linux/elf.h>
+#include <linux/file.h>
#include <linux/rseq.h>
#include <asm/compat.h>
@@ -2486,6 +2487,9 @@ void syscall_trace_exit(struct pt_regs *regs)
{
unsigned long flags = read_thread_flags();
+ if (flags & _TIF_FD_SLOTS)
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (flags & _TIF_SYSCALL_TRACEPOINT)
diff --git a/arch/csky/include/asm/thread_info.h b/arch/csky/include/asm/thread_info.h
index fdd4f8ad45acf..59109ce646951 100644
--- a/arch/csky/include/asm/thread_info.h
+++ b/arch/csky/include/asm/thread_info.h
@@ -61,6 +61,7 @@ static inline struct thread_info *current_thread_info(void)
#define TIF_SYSCALL_TRACEPOINT 5 /* syscall tracepoint instrumentation */
#define TIF_SYSCALL_AUDIT 6 /* syscall auditing */
#define TIF_NOTIFY_SIGNAL 7 /* signal notifications exist */
+#define TIF_FD_SLOTS 8 /* syscall prepared descriptors */
#define TIF_POLLING_NRFLAG 16 /* poll_idle() is TIF_NEED_RESCHED */
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
#define TIF_RESTORE_SIGMASK 20 /* restore signal mask in do_signal() */
@@ -73,6 +74,7 @@ static inline struct thread_info *current_thread_info(void)
#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)
#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_UPROBE (1 << TIF_UPROBE)
#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
#define _TIF_MEMDIE (1 << TIF_MEMDIE)
diff --git a/arch/csky/kernel/entry.S b/arch/csky/kernel/entry.S
index 3261f46f22442..6ff3074b9787b 100644
--- a/arch/csky/kernel/entry.S
+++ b/arch/csky/kernel/entry.S
@@ -80,6 +80,15 @@ ENTRY(csky_systemcall)
jsr syscallid
#endif
stw a0, (sp, LSAVE_A0) /* Save return value */
+ /* descriptors reserved by the syscall are committed in syscall_trace_exit() */
+ mov r9, sp
+ bmaski r10, THREAD_SHIFT
+ andn r9, r10
+ ldw r10, (r9, TINFO_FLAGS)
+ lrw r9, _TIF_FD_SLOTS
+ and r10, r9
+ cmpnei r10, 0
+ bt 1f
jmpi ret_from_exception
csky_syscall_trace:
diff --git a/arch/csky/kernel/ptrace.c b/arch/csky/kernel/ptrace.c
index ee2867a1576e9..b19fef9d396dc 100644
--- a/arch/csky/kernel/ptrace.c
+++ b/arch/csky/kernel/ptrace.c
@@ -4,6 +4,7 @@
#include <linux/audit.h>
#include <linux/elf.h>
#include <linux/errno.h>
+#include <linux/file.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/ptrace.h>
@@ -335,6 +336,9 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs)
asmlinkage void syscall_trace_exit(struct pt_regs *regs)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (test_thread_flag(TIF_SYSCALL_TRACE))
diff --git a/arch/hexagon/include/asm/thread_info.h b/arch/hexagon/include/asm/thread_info.h
index e90f280b9ce3e..8558d2d147f87 100644
--- a/arch/hexagon/include/asm/thread_info.h
+++ b/arch/hexagon/include/asm/thread_info.h
@@ -88,6 +88,7 @@ register struct thread_info *__current_thread_info asm(QUOTED_THREADINFO_REG);
#define TIF_SIGPENDING 2 /* signal pending */
#define TIF_NEED_RESCHED 3 /* rescheduling necessary */
#define TIF_SINGLESTEP 4 /* restore ss @ return to usr mode */
+#define TIF_FD_SLOTS 5 /* syscall prepared descriptors */
#define TIF_RESTORE_SIGMASK 6 /* restore sig mask in do_signal() */
#define TIF_NOTIFY_SIGNAL 7 /* signal notifications exist */
/* true if poll_idle() is polling TIF_NEED_RESCHED */
@@ -98,6 +99,7 @@ register struct thread_info *__current_thread_info asm(QUOTED_THREADINFO_REG);
#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
#define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
#define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)
/* work to do on interrupt/exception return - All but TIF_SYSCALL_TRACE */
diff --git a/arch/hexagon/kernel/traps.c b/arch/hexagon/kernel/traps.c
index 6fe0c1b62c1b3..4d15c5212fd78 100644
--- a/arch/hexagon/kernel/traps.c
+++ b/arch/hexagon/kernel/traps.c
@@ -6,6 +6,7 @@
*/
#include <linux/init.h>
+#include <linux/file.h>
#include <linux/sched/signal.h>
#include <linux/sched/debug.h>
#include <linux/sched/task_stack.h>
@@ -381,6 +382,9 @@ void do_trap0(struct pt_regs *regs)
regs->r04, regs->r05);
}
+ if (unlikely(test_thread_flag(TIF_FD_SLOTS)))
+ fd_slots_commit(regs);
+
/* allow strace to get the syscall return state */
if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE)))
ptrace_report_syscall_exit(regs, 0);
diff --git a/arch/m68k/68000/entry.S b/arch/m68k/68000/entry.S
index c257cc415c478..479032af2f2f0 100644
--- a/arch/m68k/68000/entry.S
+++ b/arch/m68k/68000/entry.S
@@ -89,6 +89,16 @@ ENTRY(system_call)
jbsr %a0@
movel %d0,%sp@(PT_OFF_D0) /* save the return value*/
+ /* descriptors reserved by the syscall are committed in syscall_trace_leave */
+ getthreadinfo
+ btst #(TIF_FD_SLOTS%8),%a2@(TINFO_FLAGS+(31-TIF_FD_SLOTS)/8)
+ jeq ret_from_exception
+ subql #4,%sp /* dummy return address */
+ SAVE_SWITCH_STACK
+ jbsr syscall_trace_leave
+ RESTORE_SWITCH_STACK
+ addql #4,%sp
+
ret_from_exception:
btst #5,%sp@(PT_OFF_SR) /* check if returning to kernel*/
jeq Luser_return /* if so, skip resched, signals*/
diff --git a/arch/m68k/coldfire/entry.S b/arch/m68k/coldfire/entry.S
index 4ea08336e2fb0..43e47d4c00187 100644
--- a/arch/m68k/coldfire/entry.S
+++ b/arch/m68k/coldfire/entry.S
@@ -78,6 +78,18 @@ ENTRY(system_call)
movel %d3,%a0
jbsr %a0@
movel %d0,%sp@(PT_OFF_D0) /* save the return value */
+
+ /* descriptors reserved by the syscall are committed in syscall_trace_leave */
+ movel %sp,%d2 /* get thread_info pointer */
+ andl #-THREAD_SIZE,%d2 /* at start of kernel stack */
+ movel %d2,%a0
+ btst #(TIF_FD_SLOTS%8),%a0@(TINFO_FLAGS+(31-TIF_FD_SLOTS)/8)
+ jeq ret_from_exception
+ subql #4,%sp /* dummy return address */
+ SAVE_SWITCH_STACK
+ jbsr syscall_trace_leave
+ RESTORE_SWITCH_STACK
+ addql #4,%sp
jra ret_from_exception
1:
movel #-ENOSYS,%d2 /* strace needs -ENOSYS in PT_OFF_D0 */
diff --git a/arch/m68k/include/asm/thread_info.h b/arch/m68k/include/asm/thread_info.h
index 5cb3ace556222..861b6f523bda4 100644
--- a/arch/m68k/include/asm/thread_info.h
+++ b/arch/m68k/include/asm/thread_info.h
@@ -62,6 +62,7 @@ static inline struct thread_info *current_thread_info(void)
#define TIF_NOTIFY_RESUME 5 /* callback before returning to user */
#define TIF_SIGPENDING 6 /* signal pending */
#define TIF_NEED_RESCHED 7 /* rescheduling necessary */
+#define TIF_FD_SLOTS 12 /* syscall prepared descriptors */
#define TIF_SECCOMP 13 /* seccomp syscall filtering active */
#define TIF_DELAYED_TRACE 14 /* single step a syscall */
#define TIF_SYSCALL_TRACE 15 /* syscall trace active */
@@ -71,6 +72,7 @@ static inline struct thread_info *current_thread_info(void)
#define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
#define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_DELAYED_TRACE (1 << TIF_DELAYED_TRACE)
#define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
diff --git a/arch/m68k/kernel/entry.S b/arch/m68k/kernel/entry.S
index 338b474910f74..9e4471f681652 100644
--- a/arch/m68k/kernel/entry.S
+++ b/arch/m68k/kernel/entry.S
@@ -229,6 +229,8 @@ ret_from_syscall:
syscall_exit_work:
btst #5,%sp@(PT_OFF_SR) | check if returning to kernel
bnes 1b | if so, skip resched, signals
+ btst #4,%a1@(TINFO_FLAGS+2) | TIF_FD_SLOTS: commit descriptors
+ jne do_trace_exit
lslw #1,%d0
jcs do_trace_exit
jmi do_delayed_trace
diff --git a/arch/m68k/kernel/ptrace.c b/arch/m68k/kernel/ptrace.c
index 4575f6487a9aa..07103e6ddeb04 100644
--- a/arch/m68k/kernel/ptrace.c
+++ b/arch/m68k/kernel/ptrace.c
@@ -11,6 +11,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <linux/mm.h>
@@ -289,6 +290,8 @@ asmlinkage int syscall_trace_enter(void)
asmlinkage void syscall_trace_leave(void)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(task_pt_regs(current));
if (test_thread_flag(TIF_SYSCALL_TRACE))
ptrace_report_syscall_exit(task_pt_regs(current), 0);
}
diff --git a/arch/microblaze/include/asm/thread_info.h b/arch/microblaze/include/asm/thread_info.h
index 0153f7c2717c9..ee2f76589696b 100644
--- a/arch/microblaze/include/asm/thread_info.h
+++ b/arch/microblaze/include/asm/thread_info.h
@@ -103,6 +103,7 @@ static inline struct thread_info *current_thread_info(void)
#define TIF_SINGLESTEP 4
#define TIF_NOTIFY_SIGNAL 5 /* signal notifications exist */
#define TIF_MEMDIE 6 /* is terminating due to OOM killer */
+#define TIF_FD_SLOTS 7 /* syscall prepared descriptors */
#define TIF_SYSCALL_AUDIT 9 /* syscall auditing active */
#define TIF_SECCOMP 10 /* secure computing */
@@ -118,10 +119,12 @@ static inline struct thread_info *current_thread_info(void)
#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
/* work to do in syscall trace */
#define _TIF_WORK_SYSCALL_MASK (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | \
- _TIF_SYSCALL_AUDIT | _TIF_SECCOMP)
+ _TIF_SYSCALL_AUDIT | _TIF_SECCOMP | \
+ _TIF_FD_SLOTS)
/* work to do on interrupt/exception return */
#define _TIF_WORK_MASK 0x0000FFFE
diff --git a/arch/microblaze/kernel/ptrace.c b/arch/microblaze/kernel/ptrace.c
index 236264e932d64..e6951fea85937 100644
--- a/arch/microblaze/kernel/ptrace.c
+++ b/arch/microblaze/kernel/ptrace.c
@@ -25,6 +25,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
@@ -156,6 +157,9 @@ asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
{
int step;
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
step = test_thread_flag(TIF_SINGLESTEP);
diff --git a/arch/mips/include/asm/thread_info.h b/arch/mips/include/asm/thread_info.h
index 2707dad260dd7..79c81696e9c19 100644
--- a/arch/mips/include/asm/thread_info.h
+++ b/arch/mips/include/asm/thread_info.h
@@ -114,6 +114,7 @@ register unsigned long current_stack_pointer __asm__("sp");
#define TIF_NOTIFY_RESUME 5 /* callback before returning to user */
#define TIF_UPROBE 6 /* breakpointed or singlestepping */
#define TIF_NOTIFY_SIGNAL 7 /* signal notifications exist */
+#define TIF_FD_SLOTS 8 /* syscall prepared descriptors */
#define TIF_RESTORE_SIGMASK 9 /* restore signal mask in do_signal() */
#define TIF_USEDFPU 16 /* FPU was used by this task this quantum (SMP) */
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
@@ -152,6 +153,7 @@ register unsigned long current_stack_pointer __asm__("sp");
#define _TIF_USEDMSA (1<<TIF_USEDMSA)
#define _TIF_MSA_CTX_LIVE (1<<TIF_MSA_CTX_LIVE)
#define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_WORK_SYSCALL_ENTRY (_TIF_NOHZ | _TIF_SYSCALL_TRACE | \
_TIF_SYSCALL_AUDIT | \
@@ -159,7 +161,8 @@ register unsigned long current_stack_pointer __asm__("sp");
/* work to do in syscall_trace_leave() */
#define _TIF_WORK_SYSCALL_EXIT (_TIF_NOHZ | _TIF_SYSCALL_TRACE | \
- _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_TRACEPOINT)
+ _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_TRACEPOINT | \
+ _TIF_FD_SLOTS)
/* work to do on interrupt/exception return */
#define _TIF_WORK_MASK \
diff --git a/arch/mips/kernel/ptrace.c b/arch/mips/kernel/ptrace.c
index 17dc7249b8cac..d68eab59bcc21 100644
--- a/arch/mips/kernel/ptrace.c
+++ b/arch/mips/kernel/ptrace.c
@@ -17,6 +17,7 @@
#include <linux/compiler.h>
#include <linux/context_tracking.h>
#include <linux/elf.h>
+#include <linux/file.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
@@ -1364,6 +1365,9 @@ asmlinkage void syscall_trace_leave(struct pt_regs *regs)
*/
user_exit();
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
diff --git a/arch/nios2/include/asm/thread_info.h b/arch/nios2/include/asm/thread_info.h
index 83df79286d62e..7f5816e1b3d6e 100644
--- a/arch/nios2/include/asm/thread_info.h
+++ b/arch/nios2/include/asm/thread_info.h
@@ -78,6 +78,7 @@ static inline struct thread_info *current_thread_info(void)
#define TIF_SECCOMP 5 /* secure computing */
#define TIF_SYSCALL_AUDIT 6 /* syscall auditing active */
#define TIF_NOTIFY_SIGNAL 7 /* signal notifications exist */
+#define TIF_FD_SLOTS 8 /* syscall prepared descriptors */
#define TIF_RESTORE_SIGMASK 9 /* restore signal mask in do_signal() */
#define TIF_POLLING_NRFLAG 16 /* true if poll_idle() is polling
@@ -90,6 +91,7 @@ static inline struct thread_info *current_thread_info(void)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_RESTORE_SIGMASK (1 << TIF_RESTORE_SIGMASK)
#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
diff --git a/arch/nios2/kernel/entry.S b/arch/nios2/kernel/entry.S
index dd40dfd908e59..b3a99a6db379e 100644
--- a/arch/nios2/kernel/entry.S
+++ b/arch/nios2/kernel/entry.S
@@ -222,6 +222,11 @@ translate_rc_and_ret:
stw r2, PT_R2(sp)
stw r1, PT_R7(sp)
end_translate_rc_and_ret:
+ /* Descriptors reserved by the syscall are committed in
+ * do_syscall_trace_exit, take the traced exit path for them. */
+ GET_THREAD_INFO r11
+ ldw r11, TI_FLAGS(r11)
+ BTBNZ r11, r11, TIF_FD_SLOTS, end_translate_rc_and_ret2
ret_from_exception:
ldw r1, PT_ESTATUS(sp)
diff --git a/arch/nios2/kernel/ptrace.c b/arch/nios2/kernel/ptrace.c
index 2cd7e1ecbd74b..5cee1b717662f 100644
--- a/arch/nios2/kernel/ptrace.c
+++ b/arch/nios2/kernel/ptrace.c
@@ -9,6 +9,7 @@
#include <linux/elf.h>
#include <linux/errno.h>
+#include <linux/file.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/ptrace.h>
@@ -140,6 +141,8 @@ asmlinkage int do_syscall_trace_enter(void)
asmlinkage void do_syscall_trace_exit(void)
{
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(task_pt_regs(current));
if (test_thread_flag(TIF_SYSCALL_TRACE))
ptrace_report_syscall_exit(task_pt_regs(current), 0);
}
diff --git a/arch/openrisc/include/asm/thread_info.h b/arch/openrisc/include/asm/thread_info.h
index e338fff7efb0e..3d8a710a809d2 100644
--- a/arch/openrisc/include/asm/thread_info.h
+++ b/arch/openrisc/include/asm/thread_info.h
@@ -92,6 +92,7 @@ register struct thread_info *current_thread_info_reg asm("r10");
* mode
*/
#define TIF_NOTIFY_SIGNAL 5 /* signal notifications exist */
+#define TIF_FD_SLOTS 6 /* syscall prepared descriptors */
#define TIF_SYSCALL_TRACEPOINT 8 /* for ftrace syscall instrumentation */
#define TIF_RESTORE_SIGMASK 9
#define TIF_POLLING_NRFLAG 16 /* true if poll_idle() is polling * TIF_NEED_RESCHED
@@ -104,12 +105,14 @@ register struct thread_info *current_thread_info_reg asm("r10");
#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED)
#define _TIF_SINGLESTEP (1<<TIF_SINGLESTEP)
#define _TIF_NOTIFY_SIGNAL (1<<TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_POLLING_NRFLAG (1<<TIF_POLLING_NRFLAG)
/* Work to do when returning from interrupt/exception */
/* For OpenRISC, this is anything in the LSW other than syscall trace */
-#define _TIF_WORK_MASK (0xff & ~(_TIF_SYSCALL_TRACE|_TIF_SINGLESTEP))
+#define _TIF_WORK_MASK (0xff & ~(_TIF_SYSCALL_TRACE|_TIF_SINGLESTEP| \
+ _TIF_FD_SLOTS))
#endif /* __KERNEL__ */
diff --git a/arch/openrisc/kernel/entry.S b/arch/openrisc/kernel/entry.S
index 18e68680471eb..c70265c4fe555 100644
--- a/arch/openrisc/kernel/entry.S
+++ b/arch/openrisc/kernel/entry.S
@@ -701,10 +701,11 @@ _syscall_show_regs:
#endif
_syscall_check_trace_leave:
- /* r30 is a callee-saved register so this should still hold the
- * _TIF_SYSCALL_TRACE flag from _syscall_check_trace_enter above...
+ /* Reload the flags, the syscall may have set TIF_FD_SLOTS.
* _syscall_trace_leave expects syscall result to be in pt_regs->r11.
*/
+ l.lwz r30,TI_FLAGS(r10)
+ l.andi r30,r30,(_TIF_SYSCALL_TRACE|_TIF_FD_SLOTS)
l.sfne r30,r0
l.bf _syscall_trace_leave
l.nop
diff --git a/arch/openrisc/kernel/ptrace.c b/arch/openrisc/kernel/ptrace.c
index 287a9718f0c60..71614b36e5162 100644
--- a/arch/openrisc/kernel/ptrace.c
+++ b/arch/openrisc/kernel/ptrace.c
@@ -13,6 +13,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <linux/string.h>
@@ -311,6 +312,9 @@ asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
{
int step;
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
step = test_thread_flag(TIF_SINGLESTEP);
diff --git a/arch/parisc/include/asm/thread_info.h b/arch/parisc/include/asm/thread_info.h
index b283738bb6dab..14a40c53689f9 100644
--- a/arch/parisc/include/asm/thread_info.h
+++ b/arch/parisc/include/asm/thread_info.h
@@ -52,6 +52,7 @@ struct thread_info {
#define TIF_SECCOMP 11 /* secure computing */
#define TIF_SYSCALL_TRACEPOINT 12 /* syscall tracepoint instrumentation */
#define TIF_NONBLOCK_WARNING 13 /* warned about wrong O_NONBLOCK usage */
+#define TIF_FD_SLOTS 14 /* syscall prepared descriptors */
#define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
@@ -65,6 +66,7 @@ struct thread_info {
#define _TIF_BLOCKSTEP (1 << TIF_BLOCKSTEP)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | \
_TIF_NEED_RESCHED | _TIF_NOTIFY_SIGNAL)
diff --git a/arch/parisc/kernel/entry.S b/arch/parisc/kernel/entry.S
index e04c5d806c105..c5807cf6fbd8d 100644
--- a/arch/parisc/kernel/entry.S
+++ b/arch/parisc/kernel/entry.S
@@ -1732,6 +1732,8 @@ syscall_check_resched:
/* check for reschedule */
mfctl %cr30,%r19
LDREG TASK_TI_FLAGS(%r19),%r19 /* long */
+ /* descriptors reserved by the syscall are committed in do_syscall_trace_exit */
+ bb,<,n %r19, 31-TIF_FD_SLOTS, syscall_do_fd_slots /* forward */
bb,<,n %r19, 31-TIF_NEED_RESCHED, syscall_do_resched /* forward */
.import do_signal,code
@@ -1765,6 +1767,16 @@ syscall_do_signal:
b,n syscall_check_sig
+syscall_do_fd_slots:
+ mfctl %cr30,%r1
+#ifdef CONFIG_64BIT
+ ldo -16(%r30),%r29 /* Reference param save area */
+#endif
+ ldo TASK_REGS(%r1),%r26 /* struct pt_regs *regs */
+ BL do_syscall_trace_exit,%r2
+ nop
+ b,n syscall_check_resched
+
syscall_restore:
mfctl %cr30,%r1
diff --git a/arch/parisc/kernel/ptrace.c b/arch/parisc/kernel/ptrace.c
index 1d9e210702e1f..f142766792afa 100644
--- a/arch/parisc/kernel/ptrace.c
+++ b/arch/parisc/kernel/ptrace.c
@@ -9,6 +9,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/smp.h>
@@ -381,6 +382,9 @@ void do_syscall_trace_exit(struct pt_regs *regs)
int stepping = test_thread_flag(TIF_SINGLESTEP) ||
test_thread_flag(TIF_BLOCKSTEP);
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
diff --git a/arch/powerpc/platforms/cell/spufs/inode.c b/arch/powerpc/platforms/cell/spufs/inode.c
index 2b54afb31529f..b3e9204e70312 100644
--- a/arch/powerpc/platforms/cell/spufs/inode.c
+++ b/arch/powerpc/platforms/cell/spufs/inode.c
@@ -266,10 +266,10 @@ spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
static int spufs_context_open(const struct path *path)
{
FD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred()));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
fd_prepare_file(fdf)->f_op = &spufs_context_fops;
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
static struct spu_context *
@@ -499,10 +499,10 @@ static int spufs_gang_open(const struct path *path)
* in error path of *_open().
*/
FD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred()));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
fd_prepare_file(fdf)->f_op = &spufs_gang_fops;
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
static int spufs_create_gang(struct inode *inode,
diff --git a/arch/sh/include/asm/thread_info.h b/arch/sh/include/asm/thread_info.h
index 471db51730361..d976cf21f3463 100644
--- a/arch/sh/include/asm/thread_info.h
+++ b/arch/sh/include/asm/thread_info.h
@@ -106,6 +106,7 @@ extern void init_thread_xstate(void);
#define TIF_SECCOMP 6 /* secure computing */
#define TIF_NOTIFY_RESUME 7 /* callback before returning to user */
#define TIF_SYSCALL_TRACEPOINT 8 /* for ftrace syscall instrumentation */
+#define TIF_FD_SLOTS 9 /* syscall prepared descriptors */
#define TIF_POLLING_NRFLAG 17 /* true if poll_idle() is polling TIF_NEED_RESCHED */
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
@@ -118,22 +119,25 @@ extern void init_thread_xstate(void);
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
/* work to do in syscall trace */
#define _TIF_WORK_SYSCALL_MASK (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | \
_TIF_SYSCALL_AUDIT | _TIF_SECCOMP | \
- _TIF_SYSCALL_TRACEPOINT)
+ _TIF_SYSCALL_TRACEPOINT | _TIF_FD_SLOTS)
/* work to do on any return to u-space */
#define _TIF_ALLWORK_MASK (_TIF_SYSCALL_TRACE | _TIF_SIGPENDING | \
_TIF_NEED_RESCHED | _TIF_SYSCALL_AUDIT | \
_TIF_SINGLESTEP | _TIF_NOTIFY_RESUME | \
- _TIF_SYSCALL_TRACEPOINT | _TIF_NOTIFY_SIGNAL)
+ _TIF_SYSCALL_TRACEPOINT | _TIF_NOTIFY_SIGNAL | \
+ _TIF_FD_SLOTS)
/* work to do on interrupt/exception return */
#define _TIF_WORK_MASK (_TIF_ALLWORK_MASK & ~(_TIF_SYSCALL_TRACE | \
- _TIF_SYSCALL_AUDIT | _TIF_SINGLESTEP))
+ _TIF_SYSCALL_AUDIT | _TIF_SINGLESTEP | \
+ _TIF_FD_SLOTS))
/*
* Thread-synchronous status.
diff --git a/arch/sh/kernel/entry-common.S b/arch/sh/kernel/entry-common.S
index 91ab2607a1ff9..593a482e85f52 100644
--- a/arch/sh/kernel/entry-common.S
+++ b/arch/sh/kernel/entry-common.S
@@ -164,9 +164,13 @@ work_resched:
.align 2
syscall_exit_work:
- ! r0: current_thread_info->flags
! r8: current_thread_info
- tst #(_TIF_WORK_SYSCALL_MASK & 0xff), r0
+ mov.l @(TI_FLAGS,r8), r0 ! current_thread_info->flags
+ mov #(_TIF_WORK_SYSCALL_MASK & 0xff), r1
+ mov #(_TIF_WORK_SYSCALL_MASK >> 8), r2
+ shll8 r2
+ or r2, r1
+ tst r1, r0
bt/s work_pending
tst #_TIF_NEED_RESCHED, r0
TRACE_IRQS_ON
diff --git a/arch/sh/kernel/ptrace_32.c b/arch/sh/kernel/ptrace_32.c
index 8794081483fbd..3deadd71f464c 100644
--- a/arch/sh/kernel/ptrace_32.c
+++ b/arch/sh/kernel/ptrace_32.c
@@ -8,6 +8,7 @@
* Audit support by Yuichi Nakamura <ynakam@hitachisoft.jp>
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <linux/mm.h>
@@ -476,6 +477,9 @@ asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
{
int step;
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
diff --git a/arch/sparc/include/asm/thread_info_32.h b/arch/sparc/include/asm/thread_info_32.h
index fdaf7b171e0ac..fbd7af6bb4271 100644
--- a/arch/sparc/include/asm/thread_info_32.h
+++ b/arch/sparc/include/asm/thread_info_32.h
@@ -105,6 +105,7 @@ register struct thread_info *current_thread_info_reg asm("g6");
#define TIF_NEED_RESCHED 3 /* rescheduling necessary */
#define TIF_RESTORE_SIGMASK 4 /* restore signal mask in do_signal() */
#define TIF_NOTIFY_SIGNAL 5 /* signal notifications exist */
+#define TIF_FD_SLOTS 6 /* syscall prepared descriptors */
#define TIF_USEDFPU 8 /* FPU was used by this task
* this quantum (SMP) */
#define TIF_POLLING_NRFLAG 9 /* true if poll_idle() is polling
@@ -117,6 +118,7 @@ register struct thread_info *current_thread_info_reg asm("g6");
#define _TIF_SIGPENDING (1<<TIF_SIGPENDING)
#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED)
#define _TIF_NOTIFY_SIGNAL (1<<TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_USEDFPU (1<<TIF_USEDFPU)
#define _TIF_POLLING_NRFLAG (1<<TIF_POLLING_NRFLAG)
diff --git a/arch/sparc/include/asm/thread_info_64.h b/arch/sparc/include/asm/thread_info_64.h
index c8a73dff27f80..890ee65d90290 100644
--- a/arch/sparc/include/asm/thread_info_64.h
+++ b/arch/sparc/include/asm/thread_info_64.h
@@ -180,7 +180,7 @@ extern struct thread_info *current_thread_info(void);
#define TIF_SIGPENDING 2 /* signal pending */
#define TIF_NEED_RESCHED 3 /* rescheduling necessary */
#define TIF_NOTIFY_SIGNAL 4 /* signal notifications exist */
-#define TIF_UNALIGNED 5 /* allowed to do unaligned accesses */
+#define TIF_FD_SLOTS 5 /* syscall prepared descriptors */
#define TIF_UPROBE 6 /* breakpointed or singlestepped */
#define TIF_32BIT 7 /* 32-bit binary */
#define TIF_NOHZ 8 /* in adaptive nohz mode */
@@ -194,12 +194,14 @@ extern struct thread_info *current_thread_info(void);
#define TIF_MCDPER 12 /* Precise MCD exception */
#define TIF_MEMDIE 13 /* is terminating due to OOM killer */
#define TIF_POLLING_NRFLAG 14
+#define TIF_UNALIGNED 15 /* allowed to do unaligned accesses */
#define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
#define _TIF_NOTIFY_RESUME (1<<TIF_NOTIFY_RESUME)
#define _TIF_SIGPENDING (1<<TIF_SIGPENDING)
#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED)
#define _TIF_NOTIFY_SIGNAL (1<<TIF_NOTIFY_SIGNAL)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_UNALIGNED (1<<TIF_UNALIGNED)
#define _TIF_UPROBE (1<<TIF_UPROBE)
#define _TIF_32BIT (1<<TIF_32BIT)
@@ -209,6 +211,11 @@ extern struct thread_info *current_thread_info(void);
#define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
#define _TIF_POLLING_NRFLAG (1<<TIF_POLLING_NRFLAG)
+/* work to do in syscall_trace_leave() */
+#define _TIF_SYSCALL_EXIT_WORK (_TIF_SYSCALL_TRACE | _TIF_SECCOMP | \
+ _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_TRACEPOINT | \
+ _TIF_NOHZ | _TIF_FD_SLOTS)
+
#define _TIF_USER_WORK_MASK ((0xff << TI_FLAG_WSAVED_SHIFT) | \
_TIF_DO_NOTIFY_RESUME_MASK | \
_TIF_NEED_RESCHED)
diff --git a/arch/sparc/kernel/entry.S b/arch/sparc/kernel/entry.S
index ea51ef52c952a..d2023b84db9e2 100644
--- a/arch/sparc/kernel/entry.S
+++ b/arch/sparc/kernel/entry.S
@@ -1023,7 +1023,7 @@ ret_sys_call:
ld [%sp + STACKFRAME_SZ + PT_PSR], %g3
set PSR_C, %g2
bgeu 1f
- andcc %l5, _TIF_SYSCALL_TRACE, %g0
+ andcc %l5, (_TIF_SYSCALL_TRACE | _TIF_FD_SLOTS), %g0
/* System call success, clear Carry condition code. */
andn %g3, %g2, %g3
diff --git a/arch/sparc/kernel/ptrace_32.c b/arch/sparc/kernel/ptrace_32.c
index 5991b1731feed..042e606854183 100644
--- a/arch/sparc/kernel/ptrace_32.c
+++ b/arch/sparc/kernel/ptrace_32.c
@@ -11,6 +11,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/errno.h>
@@ -437,6 +438,9 @@ asmlinkage int syscall_trace(struct pt_regs *regs, int syscall_exit_p)
{
int ret = 0;
+ if (syscall_exit_p && test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
if (test_thread_flag(TIF_SYSCALL_TRACE)) {
if (syscall_exit_p)
ptrace_report_syscall_exit(regs, 0);
diff --git a/arch/sparc/kernel/ptrace_64.c b/arch/sparc/kernel/ptrace_64.c
index 825ddf55fece3..499b30c505fdd 100644
--- a/arch/sparc/kernel/ptrace_64.c
+++ b/arch/sparc/kernel/ptrace_64.c
@@ -12,6 +12,7 @@
*/
#include <linux/kernel.h>
+#include <linux/file.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <linux/mm.h>
@@ -1110,6 +1111,9 @@ asmlinkage void syscall_trace_leave(struct pt_regs *regs)
if (test_thread_flag(TIF_NOHZ))
user_exit();
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
diff --git a/arch/sparc/kernel/syscalls.S b/arch/sparc/kernel/syscalls.S
index 96fe8763d70ce..086fb8eaf1331 100644
--- a/arch/sparc/kernel/syscalls.S
+++ b/arch/sparc/kernel/syscalls.S
@@ -269,10 +269,12 @@ ret_sys_call:
ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %g3
mov %ulo(TSTATE_XCARRY | TSTATE_ICARRY), %g2
sllx %g2, 32, %g2
+ /* Reload the flags, the syscall may have set TIF_FD_SLOTS. */
+ ldx [%g6 + TI_FLAGS], %l0
cmp %o0, -ERESTART_RESTARTBLOCK
bgeu,pn %xcc, 1f
- andcc %l0, (_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT|_TIF_SYSCALL_TRACEPOINT|_TIF_NOHZ), %g0
+ andcc %l0, _TIF_SYSCALL_EXIT_WORK, %g0
ldx [%sp + PTREGS_OFF + PT_V9_TNPC], %l1 ! pc = npc
2:
diff --git a/arch/um/include/asm/thread_info.h b/arch/um/include/asm/thread_info.h
index 7a6f4dc99fa16..7140516ee0b6c 100644
--- a/arch/um/include/asm/thread_info.h
+++ b/arch/um/include/asm/thread_info.h
@@ -44,6 +44,7 @@ struct thread_info {
#define TIF_SECCOMP 9 /* secure computing */
#define TIF_SINGLESTEP 10 /* single stepping userspace */
#define TIF_SYSCALL_TRACEPOINT 11 /* syscall tracepoint instrumentation */
+#define TIF_FD_SLOTS 12 /* syscall prepared descriptors */
#define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
@@ -55,6 +56,7 @@ struct thread_info {
#define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP)
+#define _TIF_FD_SLOTS (1 << TIF_FD_SLOTS)
#define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL | \
_TIF_NOTIFY_RESUME)
diff --git a/arch/um/kernel/ptrace.c b/arch/um/kernel/ptrace.c
index 7da0a5223aa69..f1f89181fb7e1 100644
--- a/arch/um/kernel/ptrace.c
+++ b/arch/um/kernel/ptrace.c
@@ -4,6 +4,7 @@
*/
#include <linux/audit.h>
+#include <linux/file.h>
#include <linux/ptrace.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
@@ -142,6 +143,9 @@ void syscall_trace_leave(struct pt_regs *regs)
{
int ptraced = current->ptrace;
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
/* Fake a debug trap */
diff --git a/arch/xtensa/include/asm/thread_info.h b/arch/xtensa/include/asm/thread_info.h
index 5b74dfc35ef95..0c8eb5dc83982 100644
--- a/arch/xtensa/include/asm/thread_info.h
+++ b/arch/xtensa/include/asm/thread_info.h
@@ -124,6 +124,7 @@ static __always_inline struct thread_info *current_thread_info(void)
#define TIF_SYSCALL_AUDIT 9 /* syscall auditing active */
#define TIF_SECCOMP 10 /* secure computing */
#define TIF_MEMDIE 11 /* is terminating due to OOM killer */
+#define TIF_FD_SLOTS 12 /* syscall prepared descriptors */
#define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
#define _TIF_SIGPENDING (1<<TIF_SIGPENDING)
@@ -134,6 +135,7 @@ static __always_inline struct thread_info *current_thread_info(void)
#define _TIF_NOTIFY_RESUME (1<<TIF_NOTIFY_RESUME)
#define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT)
#define _TIF_SECCOMP (1<<TIF_SECCOMP)
+#define _TIF_FD_SLOTS (1<<TIF_FD_SLOTS)
#define _TIF_WORK_MASK (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | \
_TIF_SYSCALL_TRACEPOINT | \
diff --git a/arch/xtensa/kernel/entry.S b/arch/xtensa/kernel/entry.S
index 272fff587907f..4b95f48736e6b 100644
--- a/arch/xtensa/kernel/entry.S
+++ b/arch/xtensa/kernel/entry.S
@@ -1976,6 +1976,11 @@ ENTRY(system_call)
1: /* regs->areg[2] = return_value */
s32i abi_rv, abi_saved0, PT_AREG2
+
+ /* descriptors reserved by the syscall are committed in do_syscall_trace_leave */
+ GET_THREAD_INFO(a4, a1)
+ l32i a4, a4, TI_FLAGS
+ bbsi.l a4, TIF_FD_SLOTS, 1f
bnez abi_saved1, 1f
.Lsyscall_exit:
#if defined(__XTENSA_WINDOWED_ABI__)
diff --git a/arch/xtensa/kernel/ptrace.c b/arch/xtensa/kernel/ptrace.c
index 364e4fdabb00d..d44d62a56b854 100644
--- a/arch/xtensa/kernel/ptrace.c
+++ b/arch/xtensa/kernel/ptrace.c
@@ -13,6 +13,7 @@
*/
#include <linux/audit.h>
+#include <linux/file.h>
#include <linux/errno.h>
#include <linux/hw_breakpoint.h>
#include <linux/kernel.h>
@@ -571,6 +572,9 @@ void do_syscall_trace_leave(struct pt_regs *regs)
{
int step;
+ if (test_thread_flag(TIF_FD_SLOTS))
+ fd_slots_commit(regs);
+
audit_syscall_exit(regs);
if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
diff --git a/drivers/accel/habanalabs/common/memory.c b/drivers/accel/habanalabs/common/memory.c
index 361cff5773814..1559442cd56f4 100644
--- a/drivers/accel/habanalabs/common/memory.c
+++ b/drivers/accel/habanalabs/common/memory.c
@@ -1861,11 +1861,13 @@ static int export_dmabuf(struct hl_ctx *ctx,
{
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
struct hl_device *hdev = ctx->hdev;
- CLASS(get_unused_fd, fd)(flags);
+ const struct fd_slot *fd = fd_prepare(flags);
- if (fd < 0) {
- dev_err(hdev->dev, "failed to get a file descriptor for a dma-buf, %d\n", fd);
- return fd;
+ if (IS_ERR(fd)) {
+ int rc = PTR_ERR(fd);
+
+ dev_err(hdev->dev, "failed to get a file descriptor for a dma-buf, %d\n", rc);
+ return rc;
}
exp_info.ops = &habanalabs_dmabuf_ops;
@@ -1889,8 +1891,7 @@ static int export_dmabuf(struct hl_ctx *ctx,
*/
get_file(ctx->hpriv->file_priv->filp);
- *dmabuf_fd = fd;
- fd_install(take_fd(fd), hl_dmabuf->dmabuf->file);
+ *dmabuf_fd = fd_stage(fd, hl_dmabuf->dmabuf->file);
return 0;
}
diff --git a/drivers/char/tpm/tpm_vtpm_proxy.c b/drivers/char/tpm/tpm_vtpm_proxy.c
index b81fd2a537df4..8eb87044596a4 100644
--- a/drivers/char/tpm/tpm_vtpm_proxy.c
+++ b/drivers/char/tpm/tpm_vtpm_proxy.c
@@ -531,8 +531,8 @@ static struct file *vtpm_proxy_create_device(
struct vtpm_proxy_new_dev *vtpm_new_dev)
{
struct proxy_dev *proxy_dev;
- int rc, fd;
struct file *file;
+ int rc;
if (vtpm_new_dev->flags & ~VTPM_PROXY_FLAGS_ALL)
return ERR_PTR(-EOPNOTSUPP);
@@ -544,20 +544,15 @@ static struct file *vtpm_proxy_create_device(
proxy_dev->flags = vtpm_new_dev->flags;
/* setup an anonymous file for the server-side */
- fd = get_unused_fd_flags(O_RDWR);
- if (fd < 0) {
- rc = fd;
+ FD_PREPARE(fdf, O_RDWR,
+ anon_inode_getfile("[vtpms]", &vtpm_proxy_fops, proxy_dev,
+ O_RDWR));
+ if (IS_ERR(fdf)) {
+ rc = PTR_ERR(fdf);
goto err_delete_proxy_dev;
}
+ file = fd_prepare_file(fdf);
- file = anon_inode_getfile("[vtpms]", &vtpm_proxy_fops, proxy_dev,
- O_RDWR);
- if (IS_ERR(file)) {
- rc = PTR_ERR(file);
- goto err_put_unused_fd;
- }
-
- /* from now on we can unwind with put_unused_fd() + fput() */
/* simulate an open() on the server side */
vtpm_proxy_fops_open(file);
@@ -566,16 +561,13 @@ static struct file *vtpm_proxy_create_device(
vtpm_proxy_work_start(proxy_dev);
- vtpm_new_dev->fd = fd;
+ vtpm_new_dev->fd = fd_prepare_fd(fdf);
vtpm_new_dev->major = MAJOR(proxy_dev->chip->dev.devt);
vtpm_new_dev->minor = MINOR(proxy_dev->chip->dev.devt);
vtpm_new_dev->tpm_num = proxy_dev->chip->dev_num;
return file;
-err_put_unused_fd:
- put_unused_fd(fd);
-
err_delete_proxy_dev:
vtpm_proxy_delete_proxy_dev(proxy_dev);
@@ -640,13 +632,9 @@ static long vtpmx_ioc_new_dev(struct file *file, unsigned int ioctl,
return PTR_ERR(vtpm_file);
if (copy_to_user(vtpm_new_dev_p, &vtpm_new_dev,
- sizeof(vtpm_new_dev))) {
- put_unused_fd(vtpm_new_dev.fd);
- fput(vtpm_file);
+ sizeof(vtpm_new_dev)))
return -EFAULT;
- }
- fd_install(vtpm_new_dev.fd, vtpm_file);
return 0;
}
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index d504c636dc294..f0d581fad7aec 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -440,7 +440,8 @@ static long dma_buf_export_sync_file(struct dma_buf *dmabuf,
enum dma_resv_usage usage;
struct dma_fence *fence = NULL;
struct sync_file *sync_file;
- int fd, ret;
+ int ret;
+ const struct fd_slot *fd;
if (copy_from_user(&arg, user_data, sizeof(arg)))
return -EFAULT;
@@ -451,14 +452,14 @@ static long dma_buf_export_sync_file(struct dma_buf *dmabuf,
if ((arg.flags & DMA_BUF_SYNC_RW) == 0)
return -EINVAL;
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0)
- return fd;
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
usage = dma_resv_usage_rw(arg.flags & DMA_BUF_SYNC_WRITE);
ret = dma_resv_get_singleton(dmabuf->resv, usage, &fence);
if (ret)
- goto err_put_fd;
+ return ret;
if (!fence)
fence = dma_fence_get_stub();
@@ -467,26 +468,14 @@ static long dma_buf_export_sync_file(struct dma_buf *dmabuf,
dma_fence_put(fence);
- if (!sync_file) {
- ret = -ENOMEM;
- goto err_put_fd;
- }
-
- arg.fd = fd;
- if (copy_to_user(user_data, &arg, sizeof(arg))) {
- ret = -EFAULT;
- goto err_put_file;
- }
+ if (!sync_file)
+ return -ENOMEM;
- fd_install(fd, sync_file->file);
+ arg.fd = fd_stage(fd, sync_file->file);
+ if (copy_to_user(user_data, &arg, sizeof(arg)))
+ return -EFAULT;
return 0;
-
-err_put_file:
- fput(sync_file->file);
-err_put_fd:
- put_unused_fd(fd);
- return ret;
}
static long dma_buf_import_sync_file(struct dma_buf *dmabuf,
diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
index 8df20b0218a9e..90852950e4e34 100644
--- a/drivers/dma-buf/sw_sync.c
+++ b/drivers/dma-buf/sw_sync.c
@@ -344,8 +344,7 @@ static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
unsigned long arg)
{
- int fd = get_unused_fd_flags(O_CLOEXEC);
- int err;
+ const struct fd_slot *fd = fd_prepare(O_CLOEXEC);
struct sync_pt *pt;
struct sync_file *sync_file;
struct sw_sync_create_fence_data data;
@@ -353,41 +352,26 @@ static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
/* SW sync fence are inherently unsafe and can deadlock the kernel */
add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
- if (fd < 0)
- return fd;
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
- if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
- err = -EFAULT;
- goto err;
- }
+ if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
+ return -EFAULT;
pt = sync_pt_create(obj, data.value);
- if (!pt) {
- err = -ENOMEM;
- goto err;
- }
+ if (!pt)
+ return -ENOMEM;
sync_file = sync_file_create(&pt->base);
dma_fence_put(&pt->base);
- if (!sync_file) {
- err = -ENOMEM;
- goto err;
- }
-
- data.fence = fd;
- if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
- fput(sync_file->file);
- err = -EFAULT;
- goto err;
- }
+ if (!sync_file)
+ return -ENOMEM;
- fd_install(fd, sync_file->file);
+ data.fence = fd_stage(fd, sync_file->file);
+ if (copy_to_user((void __user *)arg, &data, sizeof(data)))
+ return -EFAULT;
return 0;
-
-err:
- put_unused_fd(fd);
- return err;
}
static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c
index 2166bbdf7e4a4..316fffc4067e8 100644
--- a/drivers/dma-buf/sync_file.c
+++ b/drivers/dma-buf/sync_file.c
@@ -213,56 +213,34 @@ static __poll_t sync_file_poll(struct file *file, poll_table *wait)
static long sync_file_ioctl_merge(struct sync_file *sync_file,
unsigned long arg)
{
- int fd = get_unused_fd_flags(O_CLOEXEC);
- int err;
+ const struct fd_slot *fd = fd_prepare(O_CLOEXEC);
struct sync_file *fence2, *fence3;
struct sync_merge_data data;
- if (fd < 0)
- return fd;
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
- if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
- err = -EFAULT;
- goto err_put_fd;
- }
+ if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
+ return -EFAULT;
- if (data.flags || data.pad) {
- err = -EINVAL;
- goto err_put_fd;
- }
+ if (data.flags || data.pad)
+ return -EINVAL;
fence2 = sync_file_fdget(data.fd2);
- if (!fence2) {
- err = -ENOENT;
- goto err_put_fd;
- }
+ if (!fence2)
+ return -ENOENT;
data.name[sizeof(data.name) - 1] = '\0';
fence3 = sync_file_merge(data.name, sync_file, fence2);
- if (!fence3) {
- err = -ENOMEM;
- goto err_put_fence2;
- }
-
- data.fence = fd;
- if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
- err = -EFAULT;
- goto err_put_fence3;
- }
-
- fd_install(fd, fence3->file);
fput(fence2->file);
- return 0;
-
-err_put_fence3:
- fput(fence3->file);
+ if (!fence3)
+ return -ENOMEM;
-err_put_fence2:
- fput(fence2->file);
+ data.fence = fd_stage(fd, fence3->file);
+ if (copy_to_user((void __user *)arg, &data, sizeof(data)))
+ return -EFAULT;
-err_put_fd:
- put_unused_fd(fd);
- return err;
+ return 0;
}
static int sync_fill_fence_info(struct dma_fence *fence,
diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
index 9f3b628d57934..4d138db254724 100644
--- a/drivers/gpio/gpiolib-cdev.c
+++ b/drivers/gpio/gpiolib-cdev.c
@@ -377,16 +377,14 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
FD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,
anon_inode_getfile("gpio-linehandle", &linehandle_fileops,
lh, O_RDONLY | O_CLOEXEC));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
retain_and_null_ptr(lh);
handlereq.fd = fd_prepare_fd(fdf);
if (copy_to_user(ip, &handlereq, sizeof(handlereq)))
return -EFAULT;
- fd_publish(fdf);
-
dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
handlereq.lines);
@@ -1715,16 +1713,14 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip)
FD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,
anon_inode_getfile("gpio-line", &line_fileops, lr,
O_RDONLY | O_CLOEXEC));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
retain_and_null_ptr(lr);
ulr.fd = fd_prepare_fd(fdf);
if (copy_to_user(ip, &ulr, sizeof(ulr)))
return -EFAULT;
- fd_publish(fdf);
-
dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
ulr.num_lines);
@@ -2115,16 +2111,14 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
FD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,
anon_inode_getfile("gpio-event", &lineevent_fileops, le,
O_RDONLY | O_CLOEXEC));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
retain_and_null_ptr(le);
eventreq.fd = fd_prepare_fd(fdf);
if (copy_to_user(ip, &eventreq, sizeof(eventreq)))
return -EFAULT;
- fd_publish(fdf);
-
return 0;
}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
index 03b41f8035206..e5c0f7201cea9 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
@@ -1581,7 +1581,8 @@ int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
struct dma_fence *fence;
struct drm_syncobj *syncobj;
struct sync_file *sync_file;
- int fd, r;
+ const struct fd_slot *fd;
+ int r;
fence = amdgpu_cs_get_fence(adev, filp, &info->in.fence);
if (IS_ERR(fence))
@@ -1610,21 +1611,18 @@ int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
return r;
case AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD:
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0) {
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd)) {
dma_fence_put(fence);
- return fd;
+ return PTR_ERR(fd);
}
sync_file = sync_file_create(fence);
dma_fence_put(fence);
- if (!sync_file) {
- put_unused_fd(fd);
+ if (!sync_file)
return -ENOMEM;
- }
- fd_install(fd, sync_file->file);
- info->out.handle = fd;
+ info->out.handle = fd_stage(fd, sync_file->file);
return 0;
default:
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 7fcfc150a7fc1..158abe7e3da65 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -1988,11 +1988,10 @@ static uint32_t get_process_num_bos(struct kfd_process *p)
return num_of_bos;
}
-static int criu_get_prime_handle(struct kgd_mem *mem,
- int flags, u32 *shared_fd,
- struct file **file)
+static int criu_get_prime_handle(struct kgd_mem *mem, int flags, u32 *shared_fd)
{
struct dma_buf *dmabuf;
+ const struct fd_slot *fd;
int ret;
ret = amdgpu_amdkfd_gpuvm_export_dmabuf(mem, &dmabuf);
@@ -2001,38 +2000,17 @@ static int criu_get_prime_handle(struct kgd_mem *mem,
return ret;
}
- ret = get_unused_fd_flags(flags);
- if (ret < 0) {
+ fd = fd_prepare(flags);
+ if (IS_ERR(fd)) {
+ ret = PTR_ERR(fd);
pr_err("dmabuf create fd failed, ret:%d\n", ret);
- goto out_free_dmabuf;
+ dma_buf_put(dmabuf);
+ return ret;
}
- *shared_fd = ret;
- *file = dmabuf->file;
+ /* Installed or dropped with the ioctl's result, nothing to put back. */
+ *shared_fd = fd_stage(fd, dmabuf->file);
return 0;
-
-out_free_dmabuf:
- dma_buf_put(dmabuf);
- return ret;
-}
-
-static void commit_files(struct file **files,
- struct kfd_criu_bo_bucket *bo_buckets,
- unsigned int count,
- int err)
-{
- while (count--) {
- struct file *file = files[count];
-
- if (!file)
- continue;
- if (err) {
- fput(file);
- put_unused_fd(bo_buckets[count].dmabuf_fd);
- } else {
- fd_install(bo_buckets[count].dmabuf_fd, file);
- }
- }
}
static int criu_checkpoint_bos(struct kfd_process *p,
@@ -2043,7 +2021,6 @@ static int criu_checkpoint_bos(struct kfd_process *p,
{
struct kfd_criu_bo_bucket *bo_buckets;
struct kfd_criu_bo_priv_data *bo_privs;
- struct file **files = NULL;
int ret = 0, pdd_index, bo_index = 0, id;
void *mem;
@@ -2057,12 +2034,6 @@ static int criu_checkpoint_bos(struct kfd_process *p,
goto exit;
}
- files = kvcalloc(num_bos, sizeof(struct file *), GFP_KERNEL);
- if (!files) {
- ret = -ENOMEM;
- goto exit;
- }
-
for (pdd_index = 0; pdd_index < p->n_pdds; pdd_index++) {
struct kfd_process_device *pdd = p->pdds[pdd_index];
struct amdgpu_bo *dumper_bo;
@@ -2105,7 +2076,7 @@ static int criu_checkpoint_bos(struct kfd_process *p,
ret = criu_get_prime_handle(kgd_mem,
bo_bucket->alloc_flags &
KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ? DRM_RDWR : 0,
- &bo_bucket->dmabuf_fd, &files[bo_index]);
+ &bo_bucket->dmabuf_fd);
if (ret)
goto exit;
} else {
@@ -2156,8 +2127,6 @@ static int criu_checkpoint_bos(struct kfd_process *p,
*priv_offset += num_bos * sizeof(*bo_privs);
exit:
- commit_files(files, bo_buckets, bo_index, ret);
- kvfree(files);
kvfree(bo_buckets);
kvfree(bo_privs);
return ret;
@@ -2504,8 +2473,7 @@ static int criu_restore_memory_of_gpu(struct kfd_process_device *pdd,
static int criu_restore_bo(struct kfd_process *p,
struct kfd_criu_bo_bucket *bo_bucket,
- struct kfd_criu_bo_priv_data *bo_priv,
- struct file **file)
+ struct kfd_criu_bo_priv_data *bo_priv)
{
struct kfd_process_device *pdd;
struct kgd_mem *kgd_mem;
@@ -2557,7 +2525,7 @@ static int criu_restore_bo(struct kfd_process *p,
if (bo_bucket->alloc_flags
& (KFD_IOC_ALLOC_MEM_FLAGS_VRAM | KFD_IOC_ALLOC_MEM_FLAGS_GTT)) {
ret = criu_get_prime_handle(kgd_mem, DRM_RDWR,
- &bo_bucket->dmabuf_fd, file);
+ &bo_bucket->dmabuf_fd);
if (ret)
return ret;
} else {
@@ -2574,7 +2542,6 @@ static int criu_restore_bos(struct kfd_process *p,
{
struct kfd_criu_bo_bucket *bo_buckets = NULL;
struct kfd_criu_bo_priv_data *bo_privs = NULL;
- struct file **files = NULL;
int ret = 0;
uint32_t i = 0;
@@ -2588,12 +2555,6 @@ static int criu_restore_bos(struct kfd_process *p,
if (!bo_buckets)
return -ENOMEM;
- files = kvcalloc(args->num_bos, sizeof(struct file *), GFP_KERNEL);
- if (!files) {
- ret = -ENOMEM;
- goto exit;
- }
-
ret = copy_from_user(bo_buckets, (void __user *)args->bos,
args->num_bos * sizeof(*bo_buckets));
if (ret) {
@@ -2619,7 +2580,7 @@ static int criu_restore_bos(struct kfd_process *p,
/* Create and map new BOs */
for (; i < args->num_bos; i++) {
- ret = criu_restore_bo(p, &bo_buckets[i], &bo_privs[i], &files[i]);
+ ret = criu_restore_bo(p, &bo_buckets[i], &bo_privs[i]);
if (ret) {
pr_debug("Failed to restore BO[%d] ret%d\n", i, ret);
goto exit;
@@ -2634,8 +2595,6 @@ static int criu_restore_bos(struct kfd_process *p,
ret = -EFAULT;
exit:
- commit_files(files, bo_buckets, i, ret);
- kvfree(files);
kvfree(bo_buckets);
kvfree(bo_privs);
return ret;
diff --git a/drivers/gpu/drm/drm_lease.c b/drivers/gpu/drm/drm_lease.c
index 5d2cf724cbd7b..b95747cf79a74 100644
--- a/drivers/gpu/drm/drm_lease.c
+++ b/drivers/gpu/drm/drm_lease.c
@@ -484,7 +484,7 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
struct file *lessee_file = NULL;
struct file *lessor_file = lessor_priv->filp;
struct drm_file *lessee_priv;
- int fd = -1;
+ const struct fd_slot *fd;
uint32_t *object_ids;
/* Can't lease without MODESET */
@@ -529,10 +529,10 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
}
/* Allocate a file descriptor for the lease */
- fd = get_unused_fd_flags(cl->flags & (O_CLOEXEC | O_NONBLOCK));
- if (fd < 0) {
+ fd = fd_prepare(cl->flags & (O_CLOEXEC | O_NONBLOCK));
+ if (IS_ERR(fd)) {
idr_destroy(&leases);
- ret = fd;
+ ret = PTR_ERR(fd);
goto out_lessor;
}
@@ -543,7 +543,7 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
if (IS_ERR(lessee)) {
ret = PTR_ERR(lessee);
idr_destroy(&leases);
- goto out_leases;
+ goto out_lessor;
}
/* Clone the lessor file to create a new file for us */
@@ -562,12 +562,13 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
lessee_priv->authenticated = 1;
/* Pass fd back to userspace */
- drm_dbg_lease(dev, "Returning fd %d id %d\n", fd, lessee->lessee_id);
- cl->fd = fd;
+ drm_dbg_lease(dev, "Returning fd %d id %d\n", fd_prepare_fd(fd),
+ lessee->lessee_id);
+ cl->fd = fd_prepare_fd(fd);
cl->lessee_id = lessee->lessee_id;
/* Hook up the fd */
- fd_install(fd, lessee_file);
+ fd_stage(fd, lessee_file);
drm_master_put(&lessor);
drm_dbg_lease(dev, "drm_mode_create_lease_ioctl succeeded\n");
@@ -576,9 +577,6 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
out_lessee:
drm_master_put(&lessee);
-out_leases:
- put_unused_fd(fd);
-
out_lessor:
drm_master_put(&lessor);
drm_dbg_lease(dev, "drm_mode_create_lease_ioctl failed: %d\n", ret);
diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c
index 9b44c78cd77fc..e33d84ac7256c 100644
--- a/drivers/gpu/drm/drm_prime.c
+++ b/drivers/gpu/drm/drm_prime.c
@@ -513,19 +513,16 @@ int drm_gem_prime_handle_to_fd(struct drm_device *dev,
int *prime_fd)
{
struct dma_buf *dmabuf;
- int fd = get_unused_fd_flags(flags);
+ const struct fd_slot *fd = fd_prepare(flags);
- if (fd < 0)
- return fd;
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
dmabuf = drm_gem_prime_handle_to_dmabuf(dev, file_priv, handle, flags);
- if (IS_ERR(dmabuf)) {
- put_unused_fd(fd);
+ if (IS_ERR(dmabuf))
return PTR_ERR(dmabuf);
- }
- fd_install(fd, dmabuf->file);
- *prime_fd = fd;
+ *prime_fd = fd_stage(fd, dmabuf->file);
return 0;
}
EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c
index 2fa170a29a626..621858b6ac437 100644
--- a/drivers/gpu/drm/drm_syncobj.c
+++ b/drivers/gpu/drm/drm_syncobj.c
@@ -663,25 +663,14 @@ static const struct file_operations drm_syncobj_file_fops = {
*/
int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
{
- struct file *file;
- int fd;
-
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- file = anon_inode_getfile("syncobj_file",
- &drm_syncobj_file_fops,
- syncobj, 0);
- if (IS_ERR(file)) {
- put_unused_fd(fd);
- return PTR_ERR(file);
- }
+ FD_PREPARE(fdf, O_CLOEXEC,
+ anon_inode_getfile("syncobj_file", &drm_syncobj_file_fops,
+ syncobj, 0));
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
drm_syncobj_get(syncobj);
- fd_install(fd, file);
-
- *p_fd = fd;
+ *p_fd = fd_prepare_fd(fdf);
return 0;
}
EXPORT_SYMBOL(drm_syncobj_get_fd);
@@ -762,31 +751,24 @@ static int drm_syncobj_export_sync_file(struct drm_file *file_private,
int ret;
struct dma_fence *fence;
struct sync_file *sync_file;
- int fd = get_unused_fd_flags(O_CLOEXEC);
+ const struct fd_slot *fd = fd_prepare(O_CLOEXEC);
- if (fd < 0)
- return fd;
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
ret = drm_syncobj_find_fence(file_private, handle, point, 0, &fence);
if (ret)
- goto err_put_fd;
+ return ret;
sync_file = sync_file_create(fence);
dma_fence_put(fence);
- if (!sync_file) {
- ret = -EINVAL;
- goto err_put_fd;
- }
-
- fd_install(fd, sync_file->file);
+ if (!sync_file)
+ return -EINVAL;
- *p_fd = fd;
+ *p_fd = fd_stage(fd, sync_file->file);
return 0;
-err_put_fd:
- put_unused_fd(fd);
- return ret;
}
/**
* drm_syncobj_open - initializes syncobj file-private structures at devnode open time
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c b/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c
index 1a77a09b3377e..174f1d66d9ad3 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c
@@ -420,7 +420,7 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
struct etnaviv_gpu *gpu;
struct sync_file *sync_file = NULL;
struct ww_acquire_ctx ticket;
- int out_fence_fd = -1;
+ const struct fd_slot *out_fence_fd = NULL;
struct pid *pid = get_pid(task_pid(current));
void *stream;
int ret;
@@ -504,9 +504,9 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
}
if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) {
- out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
- if (out_fence_fd < 0) {
- ret = out_fence_fd;
+ out_fence_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(out_fence_fd)) {
+ ret = PTR_ERR(out_fence_fd);
goto err_submit_cmds;
}
}
@@ -607,10 +607,10 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
*/
goto err_submit_put;
}
- fd_install(out_fence_fd, sync_file->file);
+ fd_stage(out_fence_fd, sync_file->file);
}
- args->fence_fd = out_fence_fd;
+ args->fence_fd = out_fence_fd ? fd_prepare_fd(out_fence_fd) : -1;
args->fence = submit->out_fence_id;
err_submit_job:
@@ -623,8 +623,6 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
ww_acquire_fini(&ticket);
err_submit_cmds:
- if (ret && (out_fence_fd >= 0))
- put_unused_fd(out_fence_fd);
kvfree(stream);
kvfree(bos);
kvfree(relocs);
diff --git a/drivers/gpu/drm/msm/msm_gem_submit.c b/drivers/gpu/drm/msm/msm_gem_submit.c
index 5862db05297a7..4ddfd07c1480d 100644
--- a/drivers/gpu/drm/msm/msm_gem_submit.c
+++ b/drivers/gpu/drm/msm/msm_gem_submit.c
@@ -558,7 +558,7 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
struct drm_syncobj **syncobjs_to_reset = NULL;
struct sync_file *sync_file = NULL;
unsigned cmds_to_parse;
- int out_fence_fd = -1;
+ const struct fd_slot *out_fence_fd = NULL;
unsigned i;
int ret;
@@ -601,9 +601,9 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
ring = gpu->rb[queue->ring_nr];
if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
- out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
- if (out_fence_fd < 0) {
- ret = out_fence_fd;
+ out_fence_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(out_fence_fd)) {
+ ret = PTR_ERR(out_fence_fd);
goto out_post_unlock;
}
}
@@ -777,6 +777,8 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
sync_file = sync_file_create(submit->user_fence);
if (!sync_file)
ret = -ENOMEM;
+ else
+ fd_stage(out_fence_fd, sync_file->file);
}
if (ret)
@@ -814,15 +816,9 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
out_unlock:
mutex_unlock(&queue->lock);
out_post_unlock:
- if (ret) {
- if (out_fence_fd >= 0)
- put_unused_fd(out_fence_fd);
- if (sync_file)
- fput(sync_file->file);
- } else if (sync_file) {
- fd_install(out_fence_fd, sync_file->file);
- args->fence_fd = out_fence_fd;
- }
+ /* A staged sync file and its descriptor are dropped on return. */
+ if (!ret && sync_file)
+ args->fence_fd = fd_prepare_fd(out_fence_fd);
if (!IS_ERR_OR_NULL(submit)) {
msm_gem_submit_put(submit);
diff --git a/drivers/gpu/drm/msm/msm_gem_vma.c b/drivers/gpu/drm/msm/msm_gem_vma.c
index c11d021581e03..77d3655667322 100644
--- a/drivers/gpu/drm/msm/msm_gem_vma.c
+++ b/drivers/gpu/drm/msm/msm_gem_vma.c
@@ -1441,7 +1441,7 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
struct drm_syncobj **syncobjs_to_reset = NULL;
struct sync_file *sync_file = NULL;
struct dma_fence *fence;
- int out_fence_fd = -1;
+ const struct fd_slot *out_fence_fd = NULL;
int ret, nr_bos = 0;
unsigned i;
@@ -1479,9 +1479,9 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
}
if (args->flags & MSM_VM_BIND_FENCE_FD_OUT) {
- out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
- if (out_fence_fd < 0) {
- ret = out_fence_fd;
+ out_fence_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(out_fence_fd)) {
+ ret = PTR_ERR(out_fence_fd);
goto out_post_unlock;
}
}
@@ -1565,6 +1565,8 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
sync_file = sync_file_create(job->fence);
if (!sync_file)
ret = -ENOMEM;
+ else
+ fd_stage(out_fence_fd, sync_file->file);
}
if (ret)
@@ -1593,15 +1595,9 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
out_unlock:
mutex_unlock(&queue->lock);
out_post_unlock:
- if (ret) {
- if (out_fence_fd >= 0)
- put_unused_fd(out_fence_fd);
- if (sync_file)
- fput(sync_file->file);
- } else if (sync_file) {
- fd_install(out_fence_fd, sync_file->file);
- args->fence_fd = out_fence_fd;
- }
+ /* A staged sync file and its descriptor are dropped on return. */
+ if (!ret && sync_file)
+ args->fence_fd = fd_prepare_fd(out_fence_fd);
if (!IS_ERR_OR_NULL(job)) {
if (ret)
diff --git a/drivers/gpu/drm/msm/msm_perfcntr.c b/drivers/gpu/drm/msm/msm_perfcntr.c
index ce65b1160955e..65c067d39ca0a 100644
--- a/drivers/gpu/drm/msm/msm_perfcntr.c
+++ b/drivers/gpu/drm/msm/msm_perfcntr.c
@@ -543,8 +543,8 @@ msm_ioctl_perfcntr_config(struct drm_device *dev, void *data, struct drm_file *f
FD_PREPARE(fdf, O_CLOEXEC,
anon_inode_getfile("[msm_perfcntrs]", &stream_fops, stream, 0));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
INIT_WORK(&stream->sel_work, sel_worker);
kthread_init_work(&stream->sample_work, sample_worker);
@@ -564,7 +564,7 @@ msm_ioctl_perfcntr_config(struct drm_device *dev, void *data, struct drm_file *f
msm_perfcntr_resume_locked(perfcntrs->stream);
- stream_fd = fd_publish(fdf);
+ stream_fd = fd_prepare_fd(fdf);
} else {
kfree(ctx->perfctx);
ctx->perfctx = no_free_ptr(perfctx);
diff --git a/drivers/gpu/drm/virtio/virtgpu_submit.c b/drivers/gpu/drm/virtio/virtgpu_submit.c
index 32cb1e4aa4258..51b224acb72b0 100644
--- a/drivers/gpu/drm/virtio/virtgpu_submit.c
+++ b/drivers/gpu/drm/virtio/virtgpu_submit.c
@@ -39,7 +39,7 @@ struct virtio_gpu_submit {
struct virtio_gpu_device *vgdev;
struct sync_file *sync_file;
struct drm_file *file;
- int out_fence_fd;
+ const struct fd_slot *out_fence_fd;
u64 fence_ctx;
u32 ring_idx;
void *buf;
@@ -340,14 +340,8 @@ static void virtio_gpu_cleanup_submit(struct virtio_gpu_submit *submit)
if (submit->buflist)
virtio_gpu_array_put_free(submit->buflist);
- if (submit->out_fence_fd >= 0)
- put_unused_fd(submit->out_fence_fd);
-
if (submit->out_fence)
dma_fence_put(&submit->out_fence->f);
-
- if (submit->sync_file)
- fput(submit->sync_file->file);
}
static void virtio_gpu_submit(struct virtio_gpu_submit *submit)
@@ -363,7 +357,7 @@ static void virtio_gpu_complete_submit(struct virtio_gpu_submit *submit)
submit->buf = NULL;
submit->buflist = NULL;
submit->sync_file = NULL;
- submit->out_fence_fd = -1;
+ submit->out_fence_fd = NULL;
}
static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,
@@ -405,7 +399,7 @@ static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,
submit->out_fence = out_fence;
submit->fence_ctx = fence_ctx;
submit->ring_idx = ring_idx;
- submit->out_fence_fd = -1;
+ submit->out_fence_fd = NULL;
submit->vfpriv = vfpriv;
submit->vgdev = vgdev;
submit->exbuf = exbuf;
@@ -420,15 +414,15 @@ static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,
return PTR_ERR(submit->buf);
if (exbuf->flags & VIRTGPU_EXECBUF_FENCE_FD_OUT) {
- err = get_unused_fd_flags(O_CLOEXEC);
- if (err < 0)
- return err;
-
- submit->out_fence_fd = err;
+ submit->out_fence_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(submit->out_fence_fd))
+ return PTR_ERR(submit->out_fence_fd);
submit->sync_file = sync_file_create(&out_fence->f);
if (!submit->sync_file)
return -ENOMEM;
+
+ fd_stage(submit->out_fence_fd, submit->sync_file->file);
}
return 0;
@@ -456,12 +450,10 @@ static int virtio_gpu_wait_in_fence(struct virtio_gpu_submit *submit)
return ret;
}
-static void virtio_gpu_install_out_fence_fd(struct virtio_gpu_submit *submit)
+static void virtio_gpu_report_out_fence_fd(struct virtio_gpu_submit *submit)
{
- if (submit->sync_file) {
- submit->exbuf->fence_fd = submit->out_fence_fd;
- fd_install(submit->out_fence_fd, submit->sync_file->file);
- }
+ if (submit->sync_file)
+ submit->exbuf->fence_fd = fd_prepare_fd(submit->out_fence_fd);
}
static int virtio_gpu_lock_buflist(struct virtio_gpu_submit *submit)
@@ -534,7 +526,7 @@ int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
* Set up user-out data after submitting the job to optimize
* the job submission path.
*/
- virtio_gpu_install_out_fence_fd(&submit);
+ virtio_gpu_report_out_fence_fd(&submit);
virtio_gpu_process_post_deps(&submit);
virtio_gpu_complete_submit(&submit);
cleanup:
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c b/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c
index a9136a6523cba..9daa74a134855 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c
@@ -4109,15 +4109,17 @@ int vmw_execbuf_process(struct drm_file *file_priv,
uint32_t handle = 0;
int ret;
int32_t out_fence_fd = -1;
+ const struct fd_slot *out_fd = NULL;
struct sync_file *sync_file = NULL;
DECLARE_VAL_CONTEXT(val_ctx, sw_context, 1);
if (flags & DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {
- out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
- if (out_fence_fd < 0) {
+ out_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(out_fd)) {
VMW_DEBUG_USER("Failed to get a fence fd.\n");
- return out_fence_fd;
+ return PTR_ERR(out_fd);
}
+ out_fence_fd = fd_prepare_fd(out_fd);
}
if (throttle_us) {
@@ -4256,28 +4258,21 @@ int vmw_execbuf_process(struct drm_file *file_priv,
sync_file = sync_file_create(&fence->base);
if (!sync_file) {
VMW_DEBUG_USER("Sync file create failed for fence\n");
- put_unused_fd(out_fence_fd);
+ /* The reserved descriptor is released on return. */
out_fence_fd = -1;
(void) vmw_fence_obj_wait(fence, false, false,
VMW_FENCE_WAIT_TIMEOUT);
+ } else {
+ /* Link the fence with the FD created earlier */
+ fd_stage(out_fd, sync_file->file);
}
}
+ /* A staged sync file is dropped with the ioctl on error. */
ret = vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
user_fence_rep, fence, handle, out_fence_fd);
- if (sync_file) {
- if (ret) {
- /* usercopy of fence failed, put the file object */
- fput(sync_file->file);
- put_unused_fd(out_fence_fd);
- } else {
- /* Link the fence with the FD created earlier */
- fd_install(out_fence_fd, sync_file->file);
- }
- }
-
/* Don't unreference when handing fence out */
if (unlikely(out_fence != NULL)) {
*out_fence = fence;
@@ -4323,9 +4318,7 @@ int vmw_execbuf_process(struct drm_file *file_priv,
if (header)
vmw_cmdbuf_header_free(header);
out_free_fence_fd:
- if (out_fence_fd >= 0)
- put_unused_fd(out_fence_fd);
-
+ /* A reserved descriptor is released on return. */
return ret;
}
diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
index cc2cfce2aefdb..c95cee0bae856 100644
--- a/drivers/hv/mshv_root_main.c
+++ b/drivers/hv/mshv_root_main.c
@@ -1072,8 +1072,6 @@ mshv_partition_ioctl_create_vp(struct mshv_partition *partition,
struct mshv_vp *vp;
struct page *intercept_msg_page, *register_page, *ghcb_page;
struct hv_stats_page *stats_pages[2];
- struct file *file;
- int fd;
long ret;
if (copy_from_user(&args, arg, sizeof(args)))
@@ -1148,19 +1146,14 @@ mshv_partition_ioctl_create_vp(struct mshv_partition *partition,
if (ret)
goto put_partition;
- fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
- if (fd < 0) {
- ret = fd;
+ FD_PREPARE(fdf, O_RDWR | O_CLOEXEC,
+ anon_inode_getfile("mshv_vp", &mshv_vp_fops, vp,
+ O_RDWR | O_CLOEXEC));
+ if (IS_ERR(fdf)) {
+ ret = PTR_ERR(fdf);
goto remove_debugfs_vp;
}
- file = anon_inode_getfile("mshv_vp", &mshv_vp_fops, vp,
- O_RDWR | O_CLOEXEC);
- if (IS_ERR(file)) {
- ret = PTR_ERR(file);
- goto put_unused_vp_fd;
- }
-
/* already exclusive with the partition mutex for all ioctls */
partition->pt_vp_count++;
/*
@@ -1171,17 +1164,11 @@ mshv_partition_ioctl_create_vp(struct mshv_partition *partition,
*/
smp_store_release(&partition->pt_vp_array[args.vp_index], vp);
- /*
- * fd_install() is the userspace-visibility commit point. Must be the
- * last operation that can fail or be observed.
- */
- fd_install(fd, file);
- ret = fd;
+ /* The syscall exit installs the file; nothing after this can fail. */
+ ret = fd_prepare_fd(fdf);
goto out;
-put_unused_vp_fd:
- put_unused_fd(fd);
remove_debugfs_vp:
mshv_debugfs_vp_remove(vp);
put_partition:
diff --git a/drivers/hv/mshv_vtl_main.c b/drivers/hv/mshv_vtl_main.c
index 6e3c11c681717..f14203d76da1e 100644
--- a/drivers/hv/mshv_vtl_main.c
+++ b/drivers/hv/mshv_vtl_main.c
@@ -114,29 +114,19 @@ static long
mshv_ioctl_create_vtl(void __user *user_arg, struct device *module_dev)
{
struct mshv_vtl *vtl;
- struct file *file;
- int fd;
vtl = kzalloc_obj(*vtl);
if (!vtl)
return -ENOMEM;
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0) {
- kfree(vtl);
- return fd;
- }
- file = anon_inode_getfile("mshv_vtl", &mshv_vtl_fops,
- vtl, O_RDWR);
- if (IS_ERR(file)) {
- put_unused_fd(fd);
+ FD_PREPARE(fdf, O_CLOEXEC,
+ anon_inode_getfile("mshv_vtl", &mshv_vtl_fops, vtl, O_RDWR));
+ if (IS_ERR(fdf)) {
kfree(vtl);
- return PTR_ERR(file);
+ return PTR_ERR(fdf);
}
vtl->module_dev = module_dev;
- fd_install(fd, file);
-
- return fd;
+ return fd_prepare_fd(fdf);
}
static long
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 2c9ec93dff475..9fb15bf82d0ad 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -2041,7 +2041,7 @@ static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg
int __user *ival = (int __user *)arg;
struct iio_dev_buffer_pair *ib;
struct iio_buffer *buffer;
- int fd, idx, ret;
+ int idx, ret, fdno;
if (copy_from_user(&idx, ival, sizeof(idx)))
return -EFAULT;
@@ -2067,26 +2067,18 @@ static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg
ib->indio_dev = indio_dev;
ib->buffer = buffer;
- fd = anon_inode_getfd("iio:buffer", &iio_buffer_chrdev_fileops,
- ib, O_RDWR | O_CLOEXEC);
- if (fd < 0) {
- ret = fd;
+ FD_PREPARE(fdf, O_RDWR | O_CLOEXEC,
+ anon_inode_getfile("iio:buffer", &iio_buffer_chrdev_fileops,
+ ib, O_RDWR | O_CLOEXEC));
+ if (IS_ERR(fdf)) {
+ ret = PTR_ERR(fdf);
goto error_free_ib;
}
- if (copy_to_user(ival, &fd, sizeof(fd))) {
- /*
- * "Leak" the fd, as there's not much we can do about this
- * anyway. 'fd' might have been closed already, as
- * anon_inode_getfd() called fd_install() on it, which made
- * it reachable by userland.
- *
- * Instead of allowing a malicious user to play tricks with
- * us, rely on the process exit path to do any necessary
- * cleanup, as in releasing the file, if still needed.
- */
+ fdno = fd_prepare_fd(fdf);
+ /* The staged file is dropped with its descriptor if this faults. */
+ if (copy_to_user(ival, &fdno, sizeof(fdno)))
return -EFAULT;
- }
return 0;
diff --git a/drivers/iommu/iommufd/eventq.c b/drivers/iommu/iommufd/eventq.c
index 747dd5155121c..f5fdc6cdf0a11 100644
--- a/drivers/iommu/iommufd/eventq.c
+++ b/drivers/iommu/iommufd/eventq.c
@@ -412,9 +412,9 @@ static int iommufd_eventq_fops_release(struct inode *inode, struct file *filep)
.release = iommufd_eventq_fops_release, \
})
-static int iommufd_eventq_init(struct iommufd_eventq *eventq, char *name,
- struct iommufd_ctx *ictx,
- const struct file_operations *fops)
+static const struct fd_slot *iommufd_eventq_init(struct iommufd_eventq *eventq, char *name,
+ struct iommufd_ctx *ictx,
+ const struct file_operations *fops)
{
struct file *filep;
@@ -425,14 +425,14 @@ static int iommufd_eventq_init(struct iommufd_eventq *eventq, char *name,
/* The filep is fput() by the core code during failure */
filep = anon_inode_getfile(name, fops, eventq, O_RDWR);
if (IS_ERR(filep))
- return PTR_ERR(filep);
+ return ERR_CAST(filep);
eventq->ictx = ictx;
iommufd_ctx_get(eventq->ictx);
eventq->filep = filep;
refcount_inc(&eventq->obj.users);
- return get_unused_fd_flags(O_CLOEXEC);
+ return fd_prepare(O_CLOEXEC);
}
static const struct file_operations iommufd_fault_fops =
@@ -442,7 +442,7 @@ int iommufd_fault_alloc(struct iommufd_ucmd *ucmd)
{
struct iommu_fault_alloc *cmd = ucmd->cmd;
struct iommufd_fault *fault;
- int fdno;
+ const struct fd_slot *fdno;
int rc;
if (cmd->flags)
@@ -458,22 +458,19 @@ int iommufd_fault_alloc(struct iommufd_ucmd *ucmd)
fdno = iommufd_eventq_init(&fault->common, "[iommufd-pgfault]",
ucmd->ictx, &iommufd_fault_fops);
- if (fdno < 0)
- return fdno;
+ if (IS_ERR(fdno))
+ return PTR_ERR(fdno);
cmd->out_fault_id = fault->common.obj.id;
- cmd->out_fault_fd = fdno;
+ cmd->out_fault_fd = fd_prepare_fd(fdno);
rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
if (rc)
- goto out_put_fdno;
+ return rc;
- fd_install(fdno, fault->common.filep);
+ fd_stage(fdno, fault->common.filep);
return 0;
-out_put_fdno:
- put_unused_fd(fdno);
- return rc;
}
int iommufd_fault_iopf_handler(struct iopf_group *group)
@@ -506,7 +503,7 @@ int iommufd_veventq_alloc(struct iommufd_ucmd *ucmd)
struct iommu_veventq_alloc *cmd = ucmd->cmd;
struct iommufd_veventq *veventq;
struct iommufd_viommu *viommu;
- int fdno;
+ const struct fd_slot *fdno;
int rc;
if (cmd->flags || cmd->__reserved ||
@@ -543,24 +540,22 @@ int iommufd_veventq_alloc(struct iommufd_ucmd *ucmd)
fdno = iommufd_eventq_init(&veventq->common, "[iommufd-viommu-event]",
ucmd->ictx, &iommufd_veventq_fops);
- if (fdno < 0) {
- rc = fdno;
+ if (IS_ERR(fdno)) {
+ rc = PTR_ERR(fdno);
goto out_abort;
}
cmd->out_veventq_id = veventq->common.obj.id;
- cmd->out_veventq_fd = fdno;
+ cmd->out_veventq_fd = fd_prepare_fd(fdno);
rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
if (rc)
- goto out_put_fdno;
+ goto out_abort;
iommufd_object_finalize(ucmd->ictx, &veventq->common.obj);
- fd_install(fdno, veventq->common.filep);
+ fd_stage(fdno, veventq->common.filep);
goto out_unlock_veventqs;
-out_put_fdno:
- put_unused_fd(fdno);
out_abort:
iommufd_object_abort_and_destroy(ucmd->ictx, &veventq->common.obj);
out_unlock_veventqs:
diff --git a/drivers/media/mc/mc-request.c b/drivers/media/mc/mc-request.c
index 13e77648807c2..c9296bdbee271 100644
--- a/drivers/media/mc/mc-request.c
+++ b/drivers/media/mc/mc-request.c
@@ -316,8 +316,8 @@ int media_request_alloc(struct media_device *mdev, int *alloc_fd)
FD_PREPARE(fdf, O_CLOEXEC,
anon_inode_getfile("request", &request_fops, NULL,
O_CLOEXEC));
- if (fdf.err) {
- ret = fdf.err;
+ if (IS_ERR(fdf)) {
+ ret = PTR_ERR(fdf);
goto err_free_req;
}
@@ -328,7 +328,7 @@ int media_request_alloc(struct media_device *mdev, int *alloc_fd)
atomic_inc(&mdev->num_requests);
dev_dbg(mdev->dev, "request: allocated %s\n", req->debug_str);
- *alloc_fd = fd_publish(fdf);
+ *alloc_fd = fd_prepare_fd(fdf);
return 0;
diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index 90fd669636ec1..24b84019bbd79 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -7,6 +7,7 @@
#include <linux/dma-buf.h>
#include <linux/dma-mapping.h>
#include <linux/dma-resv.h>
+#include <linux/file.h>
#include <linux/idr.h>
#include <linux/list.h>
#include <linux/miscdevice.h>
@@ -1804,6 +1805,7 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
struct fastrpc_buf *buf = NULL;
int err;
+ const struct fd_slot *fd;
if (copy_from_user(&bp, argp, sizeof(bp)))
return -EFAULT;
@@ -1822,23 +1824,16 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
return err;
}
- bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
- if (bp.fd < 0) {
+ fd = fd_prepare(O_ACCMODE);
+ if (IS_ERR(fd)) {
dma_buf_put(buf->dmabuf);
return -EINVAL;
}
+ bp.fd = fd_stage(fd, buf->dmabuf->file);
- if (copy_to_user(argp, &bp, sizeof(bp))) {
- /*
- * The usercopy failed, but we can't do much about it, as
- * dma_buf_fd() already called fd_install() and made the
- * file descriptor accessible for the current process. It
- * might already be closed and dmabuf no longer valid when
- * we reach this point. Therefore "leak" the fd and rely on
- * the process exit path to do any required cleanup.
- */
+ /* The staged file is dropped with its descriptor if this faults. */
+ if (copy_to_user(argp, &bp, sizeof(bp)))
return -EFAULT;
- }
return 0;
}
diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c
index 4a805919bb0c5..721b0d9f0e1f4 100644
--- a/drivers/misc/ntsync.c
+++ b/drivers/misc/ntsync.c
@@ -724,10 +724,10 @@ static int ntsync_obj_get_fd(struct ntsync_obj *obj)
{
FD_PREPARE(fdf, O_CLOEXEC,
anon_inode_getfile("ntsync", &ntsync_obj_fops, obj, O_RDWR));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
obj->file = fd_prepare_file(fdf);
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp)
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 423ead48aafe3..93d9d21f4074a 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -903,28 +903,17 @@ EXPORT_SYMBOL_GPL(vfio_mig_get_next_state);
static int vfio_ioct_mig_return_fd(struct file *filp, void __user *arg,
struct vfio_device_feature_mig_state *mig)
{
- int ret;
- int fd;
+ FD_PREPARE(fdf, O_CLOEXEC, filp);
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0) {
- ret = fd;
- goto out_fput;
+ if (IS_ERR(fdf)) {
+ fput(filp);
+ return PTR_ERR(fdf);
}
- mig->data_fd = fd;
- if (copy_to_user(arg, mig, sizeof(*mig))) {
- ret = -EFAULT;
- goto out_put_unused;
- }
- fd_install(fd, filp);
+ mig->data_fd = fd_prepare_fd(fdf);
+ if (copy_to_user(arg, mig, sizeof(*mig)))
+ return -EFAULT;
return 0;
-
-out_put_unused:
- put_unused_fd(fd);
-out_fput:
- fput(filp);
- return ret;
}
static int
diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
index c91300a73f502..93c3f00f54433 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
@@ -1589,7 +1589,7 @@ static const struct file_operations ne_enclave_fops = {
static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_uid)
{
struct ne_pci_dev_cmd_reply cmd_reply = {};
- int enclave_fd = -1;
+ const struct fd_slot *enclave_fd = NULL;
struct file *enclave_file = NULL;
unsigned int i = 0;
struct ne_enclave *ne_enclave = NULL;
@@ -1647,9 +1647,9 @@ static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_ui
goto free_cpumask;
}
- enclave_fd = get_unused_fd_flags(O_CLOEXEC);
- if (enclave_fd < 0) {
- rc = enclave_fd;
+ enclave_fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(enclave_fd)) {
+ rc = PTR_ERR(enclave_fd);
dev_err_ratelimited(ne_misc_dev.this_device,
"Error in getting unused fd [rc=%d]\n", rc);
@@ -1664,7 +1664,7 @@ static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_ui
dev_err_ratelimited(ne_misc_dev.this_device,
"Error in anon inode get file [rc=%d]\n", rc);
- goto put_fd;
+ goto free_cpumask;
}
rc = ne_do_request(pdev, SLOT_ALLOC,
@@ -1688,27 +1688,20 @@ static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_ui
list_add(&ne_enclave->enclave_list_entry, &ne_pci_dev->enclaves_list);
- if (copy_to_user(slot_uid, &ne_enclave->slot_uid, sizeof(ne_enclave->slot_uid))) {
- /*
- * As we're holding the only reference to 'enclave_file', fput()
- * will call ne_enclave_release() which will do a proper cleanup
- * of all so far allocated resources, leaving only the unused fd
- * for us to free.
- */
- fput(enclave_file);
- put_unused_fd(enclave_fd);
+ fd_stage(enclave_fd, enclave_file);
+ /*
+ * The failed ioctl drops the descriptor and with it the only reference
+ * to 'enclave_file', so ne_enclave_release() does a proper cleanup of
+ * all so far allocated resources.
+ */
+ if (copy_to_user(slot_uid, &ne_enclave->slot_uid, sizeof(ne_enclave->slot_uid)))
return -EFAULT;
- }
-
- fd_install(enclave_fd, enclave_file);
- return enclave_fd;
+ return fd_prepare_fd(enclave_fd);
put_file:
fput(enclave_file);
-put_fd:
- put_unused_fd(enclave_fd);
free_cpumask:
free_cpumask_var(ne_enclave->vcpu_ids);
for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
diff --git a/drivers/xen/gntdev-dmabuf.c b/drivers/xen/gntdev-dmabuf.c
index 83b0df4608941..a3ecc7c839741 100644
--- a/drivers/xen/gntdev-dmabuf.c
+++ b/drivers/xen/gntdev-dmabuf.c
@@ -357,10 +357,10 @@ static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)
{
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
struct gntdev_dmabuf *gntdev_dmabuf __free(kfree) = NULL;
- CLASS(get_unused_fd, ret)(O_CLOEXEC);
+ const struct fd_slot *fd = fd_prepare(O_CLOEXEC);
- if (ret < 0)
- return ret;
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
gntdev_dmabuf = kzalloc_obj(*gntdev_dmabuf);
if (!gntdev_dmabuf)
@@ -388,17 +388,17 @@ static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)
if (IS_ERR(gntdev_dmabuf->dmabuf))
return PTR_ERR(gntdev_dmabuf->dmabuf);
- gntdev_dmabuf->fd = ret;
- args->fd = ret;
+ gntdev_dmabuf->fd = fd_prepare_fd(fd);
+ args->fd = fd_prepare_fd(fd);
- pr_debug("Exporting DMA buffer with fd %d\n", ret);
+ pr_debug("Exporting DMA buffer with fd %d\n", fd_prepare_fd(fd));
get_file(gntdev_dmabuf->priv->filp);
mutex_lock(&args->dmabuf_priv->lock);
list_add(&gntdev_dmabuf->next, &args->dmabuf_priv->exp_list);
mutex_unlock(&args->dmabuf_priv->lock);
- fd_install(take_fd(ret), no_free_ptr(gntdev_dmabuf)->dmabuf->file);
+ fd_stage(fd, no_free_ptr(gntdev_dmabuf)->dmabuf->file);
return 0;
}
diff --git a/fs/eventfd.c b/fs/eventfd.c
index 9d33a02757d52..a00fea879f9a5 100644
--- a/fs/eventfd.c
+++ b/fs/eventfd.c
@@ -403,12 +403,12 @@ static int do_eventfd(unsigned int count, int flags)
FD_PREPARE(fdf, flags,
anon_inode_getfile_fmode("[eventfd]", &eventfd_fops, ctx,
flags, FMODE_NOWAIT));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
ctx->id = ida_alloc(&eventfd_ida, GFP_KERNEL);
retain_and_null_ptr(ctx);
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
diff --git a/fs/eventpoll.c b/fs/eventpoll.c
index e0c4bf88a8383..81857725f8108 100644
--- a/fs/eventpoll.c
+++ b/fs/eventpoll.c
@@ -2514,12 +2514,12 @@ static int do_epoll_create(int flags)
FD_PREPARE(fdf, O_RDWR | (flags & O_CLOEXEC),
anon_inode_getfile("[eventpoll]", &eventpoll_fops, ep,
O_RDWR | (flags & O_CLOEXEC)));
- if (fdf.err) {
+ if (IS_ERR(fdf)) {
ep_clear_and_put(ep);
- return fdf.err;
+ return PTR_ERR(fdf);
}
ep->file = fd_prepare_file(fdf);
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
SYSCALL_DEFINE1(epoll_create1, int, flags)
diff --git a/fs/exec.c b/fs/exec.c
index 745f6eb5279e6..1aa0f7b99fc37 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1159,6 +1159,9 @@ int begin_new_exec(struct linux_binprm * bprm)
*/
io_uring_task_cancel();
+ /* A prepared descriptor would be installed into the new program. */
+ VFS_WARN_ON_ONCE(me->fd_slots.nr);
+
/* Ensure the files table is not shared. */
retval = unshare_files();
if (retval)
diff --git a/fs/file.c b/fs/file.c
index 628ca07dc4b17..7c05246a5129b 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -23,6 +23,7 @@
#include <linux/file_ref.h>
#include <net/sock.h>
#include <linux/init_task.h>
+#include <asm/syscall.h>
#include "internal.h"
@@ -630,14 +631,236 @@ static void __put_unused_fd(struct files_struct *files, unsigned int fd)
files->next_fd = fd;
}
-void put_unused_fd(unsigned int fd)
+/* Release @fd in the table, its slot is the caller's business. */
+static void fd_release(unsigned int fd)
{
struct files_struct *files = current->files;
+
spin_lock(&files->file_lock);
__put_unused_fd(files, fd);
spin_unlock(&files->file_lock);
}
+/* Enough for SCM_MAX_FD, and a page of slots on 4K pages. */
+#define FD_SLOTS_SPILL_MIN 256
+
+/* Make the syscall exit path call fd_slots_commit(). */
+#if defined(CONFIG_GENERIC_ENTRY)
+#define fd_slots_set_work() set_syscall_work(FD_SLOTS)
+#define fd_slots_clear_work() clear_syscall_work(FD_SLOTS)
+#elif defined(TIF_FD_SLOTS)
+#define fd_slots_set_work() set_thread_flag(TIF_FD_SLOTS)
+#define fd_slots_clear_work() clear_thread_flag(TIF_FD_SLOTS)
+#else
+#error "the syscall exit path must commit prepared file descriptors"
+#endif
+
+static struct fd_slot *fd_slot(struct fd_slots *slots, unsigned int idx)
+{
+ if (idx < FD_SLOTS_INLINE)
+ return &slots->inline_slots[idx];
+ return &slots->spill[idx - FD_SLOTS_INLINE];
+}
+
+/* Room for slot @idx in the spill array, doubling as it fills up. */
+static noinline struct fd_slot *fd_slots_spill(struct fd_slots *slots,
+ unsigned int idx)
+{
+ unsigned int max = slots->spill_max;
+ struct fd_slot *spill;
+
+ idx -= FD_SLOTS_INLINE;
+ if (idx < max)
+ return &slots->spill[idx];
+
+ max = max ? max * 2 : FD_SLOTS_SPILL_MIN;
+ spill = kvrealloc(slots->spill, array_size(max, sizeof(*spill)),
+ GFP_KERNEL_ACCOUNT);
+ if (!spill)
+ return NULL;
+ slots->spill = spill;
+ slots->spill_max = max;
+ return &spill[idx];
+}
+
+/* Record @fd in the next slot, returns the slot. */
+static struct fd_slot *fd_slot_record(int fd)
+{
+ struct fd_slots *slots = ¤t->fd_slots;
+ unsigned int idx = slots->nr;
+ struct fd_slot *slot;
+
+ /* Nothing would ever commit what a kernel thread prepares. */
+ VFS_WARN_ON_ONCE(current->flags & PF_KTHREAD);
+
+ if (likely(idx < FD_SLOTS_INLINE)) {
+ slot = &slots->inline_slots[idx];
+ } else {
+ slot = fd_slots_spill(slots, idx);
+ if (!slot)
+ return ERR_PTR(-ENOMEM);
+ }
+ ACCESS_PRIVATE(slot, fd) = fd;
+ ACCESS_PRIVATE(slot, file) = NULL;
+ if (!idx)
+ fd_slots_set_work();
+ slots->nr = idx + 1;
+ return slot;
+}
+
+/* The slot holding @fd, if this syscall prepared it. */
+static inline int fd_slot_find(struct fd_slots *slots, unsigned int fd)
+{
+ unsigned int idx = slots->nr;
+
+ while (idx--) {
+ if (ACCESS_PRIVATE(fd_slot(slots, idx), fd) == fd)
+ return idx;
+ }
+ return -1;
+}
+
+/**
+ * fd_prepare - allocate a descriptor that the syscall exit installs
+ * @flags: O_CLOEXEC or 0
+ *
+ * Returns the prepared slot as a const handle or an error pointer.
+ */
+const struct fd_slot *fd_prepare(unsigned flags)
+{
+ struct fd_slot *slot;
+ int fd;
+
+ fd = get_unused_fd_flags(flags);
+ if (fd < 0)
+ return ERR_PTR(fd);
+
+ slot = fd_slot_record(fd);
+ if (IS_ERR(slot))
+ fd_release(fd);
+ return slot;
+}
+EXPORT_SYMBOL(fd_prepare);
+
+/**
+ * fd_stage - attach the file to a prepared slot
+ * @slot: slot from fd_prepare()
+ * @file: the file to install, consumed
+ *
+ * Returns the number. The syscall exit installs @file there when the syscall
+ * returns success and drops it when the syscall returns an error.
+ */
+int fd_stage(const struct fd_slot *slot, struct file *file)
+{
+ struct fd_slot *s = (struct fd_slot *)slot;
+
+ VFS_WARN_ON_ONCE(ACCESS_PRIVATE(s, file));
+ ACCESS_PRIVATE(s, file) = file;
+ return ACCESS_PRIVATE(s, fd);
+}
+EXPORT_SYMBOL(fd_stage);
+
+/**
+ * fd_prepare_fd - the descriptor number of a prepared slot
+ * @slot: slot from fd_prepare()
+ */
+int fd_prepare_fd(const struct fd_slot *slot)
+{
+ return ACCESS_PRIVATE(slot, fd);
+}
+EXPORT_SYMBOL(fd_prepare_fd);
+
+/**
+ * fd_prepare_file - the file staged into a slot, to configure before install
+ * @slot: slot from fd_prepare()
+ *
+ * Returns the file handed to fd_stage(), or NULL before one is staged.
+ */
+struct file *fd_prepare_file(const struct fd_slot *slot)
+{
+ return ACCESS_PRIVATE(slot, file);
+}
+EXPORT_SYMBOL(fd_prepare_file);
+
+/* Install every staged file, release the slots that never got one. */
+static void fd_slots_install(struct fd_slots *slots)
+{
+ unsigned int idx;
+
+ for (idx = 0; idx < slots->nr; idx++) {
+ struct fd_slot *slot = fd_slot(slots, idx);
+
+ if (ACCESS_PRIVATE(slot, file))
+ fd_install(ACCESS_PRIVATE(slot, fd),
+ ACCESS_PRIVATE(slot, file));
+ else
+ fd_release(ACCESS_PRIVATE(slot, fd));
+ }
+}
+
+/* Release every slot's descriptor and drop the staged files. */
+static void fd_slots_drop(struct fd_slots *slots)
+{
+ struct files_struct *files = current->files;
+ unsigned int idx;
+
+ spin_lock(&files->file_lock);
+ for (idx = 0; idx < slots->nr; idx++)
+ __put_unused_fd(files, ACCESS_PRIVATE(fd_slot(slots, idx), fd));
+ spin_unlock(&files->file_lock);
+ for (idx = 0; idx < slots->nr; idx++) {
+ struct fd_slot *slot = fd_slot(slots, idx);
+
+ if (ACCESS_PRIVATE(slot, file))
+ fput(ACCESS_PRIVATE(slot, file));
+ }
+}
+
+static __always_inline void fd_slots_finish(struct fd_slots *slots, bool failed)
+{
+ if (likely(!failed))
+ fd_slots_install(slots);
+ else
+ fd_slots_drop(slots);
+ slots->nr = 0;
+ fd_slots_clear_work();
+}
+
+/* Syscall exit hook, keyed on the return value the caller will see. */
+void fd_slots_commit(struct pt_regs *regs)
+{
+ fd_slots_finish(¤t->fd_slots,
+ syscall_get_error(current, regs) != 0);
+}
+
+/* Install or drop the prepared descriptors based on @ret. */
+void __fd_slots_commit(long ret)
+{
+ fd_slots_finish(¤t->fd_slots, IS_ERR_VALUE(ret));
+}
+
+void exit_fd_slots(void)
+{
+ struct fd_slots *slots = ¤t->fd_slots;
+
+ /* A syscall must not exit with prepared descriptors outstanding. */
+ if (WARN_ON_ONCE(slots->nr))
+ fd_slots_finish(slots, true);
+ kvfree(slots->spill);
+}
+
+/**
+ * put_unused_fd - give a descriptor back before it got a file
+ * @fd: descriptor returned by get_unused_fd_flags()
+ *
+ * Not for a prepared descriptor, the syscall exit releases that one.
+ */
+void put_unused_fd(unsigned int fd)
+{
+ VFS_WARN_ON_ONCE(fd_slot_find(¤t->fd_slots, fd) >= 0);
+ fd_release(fd);
+}
+
EXPORT_SYMBOL(put_unused_fd);
/*
@@ -1384,25 +1607,27 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)
*/
int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags)
{
- int error;
+ int fd, error;
error = security_file_receive(file);
if (error)
return error;
- FD_PREPARE(fdf, o_flags, file);
- if (fdf.err)
- return fdf.err;
- get_file(file);
+ fd = get_unused_fd_flags(o_flags);
+ if (fd < 0)
+ return fd;
if (ufd) {
- error = put_user(fd_prepare_fd(fdf), ufd);
- if (error)
+ error = put_user(fd, ufd);
+ if (error) {
+ put_unused_fd(fd);
return error;
+ }
}
- __receive_sock(fd_prepare_file(fdf));
- return fd_publish(fdf);
+ __receive_sock(file);
+ fd_install(fd, get_file(file));
+ return fd;
}
EXPORT_SYMBOL_GPL(receive_fd);
diff --git a/fs/namespace.c b/fs/namespace.c
index 1ecd96c918b33..fba29bcc01646 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -4544,9 +4544,9 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
FD_PREPARE(fdf, (flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0,
dentry_open(&new_path, O_PATH, fc->cred));
- if (fdf.err) {
+ if (IS_ERR(fdf)) {
dissolve_on_fput(new_path.mnt);
- return fdf.err;
+ return PTR_ERR(fdf);
}
/*
@@ -4554,7 +4554,7 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
* need to unmount it, not just simply put it.
*/
fd_prepare_file(fdf)->f_mode |= FMODE_NEED_UNMOUNT;
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
static inline int vfs_move_mount(const struct path *from_path,
@@ -5198,8 +5198,8 @@ SYSCALL_DEFINE5(open_tree_attr, int, dfd, const char __user *, filename,
return -EINVAL;
FD_PREPARE(fdf, flags, vfs_open_tree(dfd, filename, flags));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
if (uattr) {
struct mount_kattr kattr = {};
@@ -5220,7 +5220,7 @@ SYSCALL_DEFINE5(open_tree_attr, int, dfd, const char __user *, filename,
return ret;
}
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
int show_path(struct seq_file *m, struct dentry *root)
diff --git a/fs/nsfs.c b/fs/nsfs.c
index c3b6ae76594a8..da021c8b49b29 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -348,8 +348,8 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
return ret;
FD_PREPARE(fdf, O_CLOEXEC, dentry_open(&path, O_RDONLY, current_cred()));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
/*
* If @uinfo is passed return all information about the
* mount namespace as well.
@@ -357,7 +357,7 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
ret = copy_ns_info_to_user(to_mnt_ns(ns), uinfo, usize, &kinfo);
if (ret)
return ret;
- ret = fd_publish(fdf);
+ ret = fd_prepare_fd(fdf);
break;
}
default:
diff --git a/fs/xfs/xfs_handle.c b/fs/xfs/xfs_handle.c
index 0689cade8f74c..174aa3c8260f6 100644
--- a/fs/xfs/xfs_handle.c
+++ b/fs/xfs/xfs_handle.c
@@ -272,8 +272,8 @@ xfs_open_by_handle(
path.mnt = mntget(parfilp->f_path.mnt);
FD_PREPARE(fdf, 0, dentry_open(&path, hreq->oflags, cred));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
if (S_ISREG(inode->i_mode)) {
struct file *filp = fd_prepare_file(fdf);
@@ -282,7 +282,7 @@ xfs_open_by_handle(
filp->f_mode |= FMODE_NOCMTIME;
}
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
int
diff --git a/include/linux/entry-common.h b/include/linux/entry-common.h
index 6574b7183c017..2da30e50cf596 100644
--- a/include/linux/entry-common.h
+++ b/include/linux/entry-common.h
@@ -3,6 +3,7 @@
#define __LINUX_ENTRYCOMMON_H
#include <linux/audit.h>
+#include <linux/file.h>
#include <linux/irq-entry-common.h>
#include <linux/livepatch.h>
#include <linux/ptrace.h>
@@ -36,7 +37,8 @@
SYSCALL_WORK_SYSCALL_TRACE | \
SYSCALL_WORK_SYSCALL_AUDIT | \
SYSCALL_WORK_SYSCALL_USER_DISPATCH | \
- SYSCALL_WORK_SYSCALL_EXIT_TRAP)
+ SYSCALL_WORK_SYSCALL_EXIT_TRAP | \
+ SYSCALL_WORK_FD_SLOTS)
/**
* arch_ptrace_report_syscall_permit_entry - Architecture specific wrapper for
@@ -245,6 +247,10 @@ static __always_inline void syscall_exit_work(struct pt_regs *regs, unsigned lon
{
bool step;
+ /* Install or drop the descriptors the syscall prepared. */
+ if (work & SYSCALL_WORK_FD_SLOTS)
+ fd_slots_commit(regs);
+
/*
* If the syscall was rolled back due to syscall user dispatching,
* then the tracers below are not invoked for the same reason as
diff --git a/include/linux/file.h b/include/linux/file.h
index 27484b444d315..45d68e097c05e 100644
--- a/include/linux/file.h
+++ b/include/linux/file.h
@@ -91,6 +91,10 @@ extern bool get_close_on_exec(unsigned int fd);
extern int __get_unused_fd_flags(unsigned flags, unsigned long nofile);
extern int get_unused_fd_flags(unsigned flags);
extern void put_unused_fd(unsigned int fd);
+void __fd_slots_commit(long ret);
+struct pt_regs;
+void fd_slots_commit(struct pt_regs *regs);
+void exit_fd_slots(void);
DEFINE_CLASS(get_unused_fd, int, if (_T >= 0) put_unused_fd(_T),
get_unused_fd_flags(flags), unsigned flags)
@@ -118,6 +122,12 @@ DEFINE_FREE(fput, struct file *, if (!IS_ERR_OR_NULL(_T)) fput(_T))
extern void fd_install(unsigned int fd, struct file *file);
+struct fd_slot;
+const struct fd_slot *fd_prepare(unsigned flags);
+int fd_stage(const struct fd_slot *slot, struct file *file);
+int fd_prepare_fd(const struct fd_slot *slot);
+struct file *fd_prepare_file(const struct fd_slot *slot);
+
int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags);
int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags);
@@ -128,128 +138,46 @@ extern void __fput_sync(struct file *);
extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
/*
- * fd_prepare: Combined fd + file allocation cleanup class.
- * @err: Error code to indicate if allocation succeeded.
- * @__fd: Allocated fd (may not be accessed directly)
- * @__file: Allocated struct file pointer (may not be accessed directly)
- *
- * Allocates an fd and a file together. On error paths, automatically cleans
- * up whichever resource was successfully allocated. Allows flexible file
- * allocation with different functions per usage.
- *
- * Do not use directly.
- */
-struct fd_prepare {
- s32 err;
- s32 __fd; /* do not access directly */
- struct file *__file; /* do not access directly */
-};
-
-/* Typedef for fd_prepare cleanup guards. */
-typedef struct fd_prepare class_fd_prepare_t;
-
-/*
- * Accessors for fd_prepare class members.
- * _Generic() is used for zero-cost type safety.
- */
-#define fd_prepare_fd(_fdf) \
- (_Generic((_fdf), struct fd_prepare: (_fdf).__fd))
-
-#define fd_prepare_file(_fdf) \
- (_Generic((_fdf), struct fd_prepare: (_fdf).__file))
-
-/* Do not use directly. */
-static inline void class_fd_prepare_destructor(const struct fd_prepare *fdf)
-{
- if (unlikely(fdf->__fd >= 0))
- put_unused_fd(fdf->__fd);
- if (unlikely(!IS_ERR_OR_NULL(fdf->__file)))
- fput(fdf->__file);
-}
-
-/* Do not use directly. */
-static inline int class_fd_prepare_lock_err(const struct fd_prepare *fdf)
-{
- if (unlikely(fdf->err))
- return fdf->err;
- if (unlikely(fdf->__fd < 0))
- return fdf->__fd;
- if (unlikely(IS_ERR(fdf->__file)))
- return PTR_ERR(fdf->__file);
- if (unlikely(!fdf->__file))
- return -ENOMEM;
- return 0;
-}
-
-/*
- * __FD_PREPARE_INIT - Helper to initialize fd_prepare class.
+ * FD_ADD - allocate a descriptor, build the file and install it in one step.
* @_fd_flags: flags for get_unused_fd_flags()
- * @_file_owned: expression that returns struct file *
- *
- * Returns a struct fd_prepare with fd, file, and err set.
- * If fd allocation fails, fd will be negative and err will be set. If
- * fd succeeds but file_init_expr fails, file will be ERR_PTR and err
- * will be set. The err field is the single source of truth for error
- * checking.
- */
-#define __FD_PREPARE_INIT(_fd_flags, _file_owned) \
- ({ \
- struct fd_prepare fdf = { \
- .__fd = get_unused_fd_flags((_fd_flags)), \
- }; \
- if (likely(fdf.__fd >= 0)) \
- fdf.__file = (_file_owned); \
- fdf.err = ACQUIRE_ERR(fd_prepare, &fdf); \
- fdf; \
- })
-
-/*
- * FD_PREPARE - Macro to declare and initialize an fd_prepare variable.
- *
- * Declares and initializes an fd_prepare variable with automatic
- * cleanup. No separate scope required - cleanup happens when variable
- * goes out of scope.
+ * @_file_owned: struct file to take ownership of (can be an expression)
*
- * @_fdf: name of struct fd_prepare variable to define
- * @_fd_flags: flags for get_unused_fd_flags()
- * @_file_owned: struct file to take ownership of (can be expression)
+ * The file expression is evaluated only after the descriptor is allocated, so
+ * a full table does not run its side effects. Drops the file and returns a
+ * negative errno on failure. Installs immediately: for anything more than a
+ * bare install-and-return (reporting the number, configuring the file,
+ * installing several descriptors) use fd_prepare()/fd_stage().
*/
-#define FD_PREPARE(_fdf, _fd_flags, _file_owned) \
- CLASS_INIT(fd_prepare, _fdf, __FD_PREPARE_INIT(_fd_flags, _file_owned))
+#define FD_ADD(_fd_flags, _file_owned) \
+({ \
+ int __fd = get_unused_fd_flags(_fd_flags); \
+ if (likely(__fd >= 0)) { \
+ struct file *__file = (_file_owned); \
+ if (unlikely(IS_ERR_OR_NULL(__file))) { \
+ put_unused_fd(__fd); \
+ __fd = __file ? PTR_ERR(__file) : -ENOMEM; \
+ } else { \
+ fd_install(__fd, __file); \
+ } \
+ } \
+ __fd; \
+})
/*
- * fd_publish - Publish prepared fd and file to the fd table.
- * @_fdf: struct fd_prepare variable
- */
-#define fd_publish(_fdf) \
- ({ \
- struct fd_prepare *fdp = &(_fdf); \
- VFS_WARN_ON_ONCE(fdp->err); \
- VFS_WARN_ON_ONCE(fdp->__fd < 0); \
- VFS_WARN_ON_ONCE(IS_ERR_OR_NULL(fdp->__file)); \
- fd_install(fdp->__fd, fdp->__file); \
- retain_and_null_ptr(fdp->__file); \
- take_fd(fdp->__fd); \
- })
-
-/* Do not use directly. */
-#define __FD_ADD(_fdf, _fd_flags, _file_owned) \
- ({ \
- FD_PREPARE(_fdf, _fd_flags, _file_owned); \
- s32 ret = _fdf.err; \
- if (likely(!ret)) \
- ret = fd_publish(_fdf); \
- ret; \
- })
-
-/*
- * FD_ADD - Allocate and install an fd and file in one step.
+ * FD_PREPARE - reserve a descriptor and stage @_file_owned on it for the
+ * install at syscall exit; declares @_fdf, an fd_prepare() slot.
+ * @_fdf: name of the const struct fd_slot * to declare
* @_fd_flags: flags for get_unused_fd_flags()
- * @_file_owned: struct file to take ownership of
- *
- * Returns the allocated fd number, or negative error code on failure.
+ * @_file_owned: struct file to take ownership of (can be an expression)
*/
-#define FD_ADD(_fd_flags, _file_owned) \
- __FD_ADD(__UNIQUE_ID(fd_prepare), _fd_flags, _file_owned)
+#define FD_PREPARE(_fdf, _fd_flags, _file_owned) \
+ const struct fd_slot *_fdf = fd_prepare(_fd_flags); \
+ if (!IS_ERR(_fdf)) { \
+ struct file *__file = (_file_owned); \
+ if (unlikely(IS_ERR_OR_NULL(__file))) \
+ _fdf = __file ? ERR_CAST(__file) : ERR_PTR(-ENOMEM); \
+ else \
+ fd_stage(_fdf, __file); \
+ }
#endif /* __LINUX_FILE_H */
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 8b3d47a325cca..52bbf9931908f 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -832,6 +832,24 @@ struct task_ipi_mask {
struct task_ipi_mask { };
#endif
+/* Descriptors this syscall prepared, installed when it returns. */
+#define FD_SLOTS_INLINE 2
+
+struct file;
+
+struct fd_slot {
+ struct file * __private file;
+ int __private fd;
+};
+
+/* Inline up to FD_SLOTS_INLINE slots, the rest in the spill. */
+struct fd_slots {
+ unsigned int nr;
+ unsigned int spill_max;
+ struct fd_slot *spill;
+ struct fd_slot inline_slots[FD_SLOTS_INLINE];
+};
+
struct task_struct {
#ifdef CONFIG_THREAD_INFO_IN_TASK
/*
@@ -1206,6 +1224,9 @@ struct task_struct {
/* Open file information: */
struct files_struct *files;
+ /* Descriptors prepared by the current syscall: */
+ struct fd_slots fd_slots;
+
#ifdef CONFIG_IO_URING
struct io_uring_task *io_uring;
struct io_restriction *io_uring_restrict;
diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
index 307b8390fc670..c80a874442860 100644
--- a/include/linux/thread_info.h
+++ b/include/linux/thread_info.h
@@ -47,6 +47,7 @@ enum syscall_work_bit {
SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH,
SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP,
SYSCALL_WORK_BIT_SYSCALL_RSEQ_SLICE,
+ SYSCALL_WORK_BIT_FD_SLOTS,
};
#define SYSCALL_WORK_SECCOMP BIT(SYSCALL_WORK_BIT_SECCOMP)
@@ -57,6 +58,7 @@ enum syscall_work_bit {
#define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)
#define SYSCALL_WORK_SYSCALL_EXIT_TRAP BIT(SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP)
#define SYSCALL_WORK_SYSCALL_RSEQ_SLICE BIT(SYSCALL_WORK_BIT_SYSCALL_RSEQ_SLICE)
+#define SYSCALL_WORK_FD_SLOTS BIT(SYSCALL_WORK_BIT_FD_SLOTS)
#endif
#include <asm/thread_info.h>
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 61053421d809a..001c3683bf00d 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -870,6 +870,10 @@ bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags)
lockdep_assert(!io_wq_current_is_worker());
lockdep_assert_held(&ctx->uring_lock);
+ /* Descriptors this CQE reports must be installed before it is visible. */
+ if (unlikely(current->fd_slots.nr))
+ __fd_slots_commit(res);
+
if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
spin_lock(&ctx->completion_lock);
posted = io_fill_cqe_aux(ctx, req->cqe.user_data, res, cflags);
@@ -895,6 +899,8 @@ bool io_req_post_cqe32(struct io_kiocb *req, struct io_uring_cqe cqe[2])
lockdep_assert_held(&ctx->uring_lock);
cqe[0].user_data = req->cqe.user_data;
+ if (unlikely(current->fd_slots.nr))
+ __fd_slots_commit(cqe[0].res);
if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
spin_lock(&ctx->completion_lock);
posted = io_fill_cqe_aux32(ctx, cqe);
@@ -1365,6 +1371,24 @@ static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def,
#define REQ_ISSUE_SLOW_FLAGS (REQ_F_CREDS | REQ_F_ARM_LTIMEOUT)
+/*
+ * Requests complete from io_uring_enter(), task_work, io-wq workers and the
+ * SQPOLL thread, and their CQEs are visible before any syscall returns. So
+ * the descriptors a request reserved are committed per request, before its
+ * completion is posted.
+ */
+static void io_req_fd_reservations(struct io_kiocb *req, int ret)
+{
+ long res = ret;
+
+ /* A request holding reservations must not go async or be reissued. */
+ WARN_ON_ONCE(ret == IOU_ISSUE_SKIP_COMPLETE || ret == IOU_RETRY ||
+ ret == IOU_REQUEUE);
+ if (ret == IOU_COMPLETE)
+ res = req->cqe.res;
+ __fd_slots_commit(res);
+}
+
static inline int __io_issue_sqe(struct io_kiocb *req,
unsigned int issue_flags,
const struct io_issue_def *def)
@@ -1385,6 +1409,9 @@ static inline int __io_issue_sqe(struct io_kiocb *req,
ret = def->issue(req, issue_flags);
+ if (unlikely(current->fd_slots.nr))
+ io_req_fd_reservations(req, ret);
+
if (!def->audit_skip)
audit_uring_exit(!ret, ret);
diff --git a/io_uring/mock_file.c b/io_uring/mock_file.c
index b318ed697998e..67164c28ed659 100644
--- a/io_uring/mock_file.c
+++ b/io_uring/mock_file.c
@@ -257,8 +257,8 @@ static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flag
FD_PREPARE(fdf, O_RDWR | O_CLOEXEC,
anon_inode_create_getfile("[io_uring_mock]", fops, mf,
O_RDWR | O_CLOEXEC, NULL));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
retain_and_null_ptr(mf);
file = fd_prepare_file(fdf);
@@ -271,7 +271,6 @@ static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flag
if (copy_to_user(uarg, &mc, uarg_size))
return -EFAULT;
- fd_publish(fdf);
return 0;
}
diff --git a/io_uring/zcrx.c b/io_uring/zcrx.c
index 1b3b11405daca..d458c614c765f 100644
--- a/io_uring/zcrx.c
+++ b/io_uring/zcrx.c
@@ -795,8 +795,6 @@ static int zcrx_export(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq,
struct zcrx_ctrl *ctrl, void __user *arg)
{
struct zcrx_ctrl_export *ce = &ctrl->zc_export;
- struct file *file;
- int fd;
if (!mem_is_zero(ce, sizeof(*ce)))
return -EINVAL;
@@ -804,27 +802,18 @@ static int zcrx_export(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq,
refcount_inc(&ifq->refs);
refcount_inc(&ifq->user_refs);
- file = anon_inode_create_getfile("[zcrx]", &zcrx_box_fops,
- ifq, O_CLOEXEC, NULL);
- if (IS_ERR(file)) {
+ FD_PREPARE(fdf, O_CLOEXEC,
+ anon_inode_create_getfile("[zcrx]", &zcrx_box_fops, ifq,
+ O_CLOEXEC, NULL));
+ if (IS_ERR(fdf)) {
zcrx_unregister(ifq, NULL);
- return PTR_ERR(file);
- }
-
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0) {
- fput(file);
- return fd;
+ return PTR_ERR(fdf);
}
- ce->zcrx_fd = fd;
- if (copy_to_user(arg, ctrl, sizeof(*ctrl))) {
- fput(file);
- put_unused_fd(fd);
+ ce->zcrx_fd = fd_prepare_fd(fdf);
+ if (copy_to_user(arg, ctrl, sizeof(*ctrl)))
return -EFAULT;
- }
- fd_install(fd, file);
return 0;
}
diff --git a/kernel/bpf/bpf_iter.c b/kernel/bpf/bpf_iter.c
index 14a5fdfa04214..00edc394101bc 100644
--- a/kernel/bpf/bpf_iter.c
+++ b/kernel/bpf/bpf_iter.c
@@ -643,15 +643,15 @@ int bpf_iter_new_fd(struct bpf_link *link)
flags = O_RDONLY | O_CLOEXEC;
FD_PREPARE(fdf, flags, anon_inode_getfile("bpf_iter", &bpf_iter_fops, NULL, flags));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
iter_link = container_of(link, struct bpf_iter_link, link);
err = prepare_seq_file(fd_prepare_file(fdf), iter_link);
if (err)
return err; /* Automatic cleanup handles fput */
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop)
diff --git a/kernel/bpf/token.c b/kernel/bpf/token.c
index e85a179523f02..5cac7fc5694bd 100644
--- a/kernel/bpf/token.c
+++ b/kernel/bpf/token.c
@@ -169,8 +169,8 @@ int bpf_token_create(union bpf_attr *attr)
FD_PREPARE(fdf, O_CLOEXEC,
alloc_file_pseudo(inode, path.mnt, BPF_TOKEN_INODE_NAME,
O_RDWR, &bpf_token_fops));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
token = kzalloc_obj(*token, GFP_USER);
if (!token)
@@ -191,7 +191,7 @@ int bpf_token_create(union bpf_attr *attr)
get_user_ns(token->userns);
fd_prepare_file(fdf)->private_data = no_free_ptr(token);
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
int bpf_token_get_info_by_fd(struct bpf_token *token,
diff --git a/kernel/events/core.c b/kernel/events/core.c
index a6c8e38a31104..54834a7aca53d 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -13924,7 +13924,7 @@ SYSCALL_DEFINE5(perf_event_open,
struct file *event_file = NULL;
struct task_struct *task = NULL;
struct pmu *pmu;
- int event_fd;
+ const struct fd_slot *event_fd;
int move_group = 0;
int err;
int f_flags = O_RDWR;
@@ -13990,9 +13990,9 @@ SYSCALL_DEFINE5(perf_event_open,
if (flags & PERF_FLAG_FD_CLOEXEC)
f_flags |= O_CLOEXEC;
- event_fd = get_unused_fd_flags(f_flags);
- if (event_fd < 0)
- return event_fd;
+ event_fd = fd_prepare(f_flags);
+ if (IS_ERR(event_fd))
+ return PTR_ERR(event_fd);
/*
* Event creation should be under SRCU, see perf_pmu_unregister().
@@ -14001,15 +14001,11 @@ SYSCALL_DEFINE5(perf_event_open,
CLASS(fd, group)(group_fd); // group_fd == -1 => empty
if (group_fd != -1) {
- if (!is_perf_file(group)) {
- err = -EBADF;
- goto err_fd;
- }
+ if (!is_perf_file(group))
+ return -EBADF;
group_leader = fd_file(group)->private_data;
- if (group_leader->state <= PERF_EVENT_STATE_EXIT) {
- err = -ENODEV;
- goto err_fd;
- }
+ if (group_leader->state <= PERF_EVENT_STATE_EXIT)
+ return -ENODEV;
if (flags & PERF_FLAG_FD_OUTPUT)
output_event = group_leader;
if (flags & PERF_FLAG_FD_NO_GROUP)
@@ -14018,10 +14014,8 @@ SYSCALL_DEFINE5(perf_event_open,
if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
task = find_lively_task_by_vpid(pid);
- if (IS_ERR(task)) {
- err = PTR_ERR(task);
- goto err_fd;
- }
+ if (IS_ERR(task))
+ return PTR_ERR(task);
}
if (task && group_leader &&
@@ -14298,8 +14292,7 @@ SYSCALL_DEFINE5(perf_event_open,
* This ensures destruction of the group leader will find
* the pointer to itself in perf_group_detach().
*/
- fd_install(event_fd, event_file);
- return event_fd;
+ return fd_stage(event_fd, event_file);
err_context:
put_pmu_ctx(event->pmu_ctx);
@@ -14316,8 +14309,6 @@ SYSCALL_DEFINE5(perf_event_open,
err_task:
if (task)
put_task_struct(task);
-err_fd:
- put_unused_fd(event_fd);
return err;
}
diff --git a/kernel/exit.c b/kernel/exit.c
index 97686af895013..ee108353a62cd 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -1000,6 +1000,7 @@ void __noreturn do_exit(long code)
exit_sem(tsk);
exit_shm(tsk);
+ exit_fd_slots();
exit_files(tsk);
exit_fs(tsk);
if (group_dead)
diff --git a/kernel/fork.c b/kernel/fork.c
index 416758c8a3d43..bd0e1e85abb0e 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -980,6 +980,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
tsk->btrace_seq = 0;
#endif
tsk->splice_pipe = NULL;
+ memset(&tsk->fd_slots, 0, sizeof(tsk->fd_slots));
tsk->task_frag.page = NULL;
tsk->wake_q.next = NULL;
tsk->worker_private = NULL;
@@ -3216,6 +3217,8 @@ static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp
if ((unshare_flags & CLONE_FILES) &&
(fd && atomic_read(&fd->count) > 1)) {
+ /* Prepared descriptors live in the table we are about to leave. */
+ VFS_WARN_ON_ONCE(current->fd_slots.nr);
fd = dup_fd(fd, NULL);
if (IS_ERR(fd))
return PTR_ERR(fd);
diff --git a/kernel/liveupdate/luo_core.c b/kernel/liveupdate/luo_core.c
index 1b2bda22902d2..3899bbf30b53f 100644
--- a/kernel/liveupdate/luo_core.c
+++ b/kernel/liveupdate/luo_core.c
@@ -270,61 +270,41 @@ struct luo_device_state {
static int luo_ioctl_create_session(struct luo_ucmd *ucmd)
{
struct liveupdate_ioctl_create_session *argp = ucmd->cmd;
+ const struct fd_slot *fd;
struct file *file;
int err;
- argp->fd = get_unused_fd_flags(O_CLOEXEC);
- if (argp->fd < 0)
- return argp->fd;
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
err = luo_session_create(argp->name, &file);
if (err)
- goto err_put_fd;
-
- err = luo_ucmd_respond(ucmd, sizeof(*argp));
- if (err)
- goto err_put_file;
-
- fd_install(argp->fd, file);
-
- return 0;
+ return err;
-err_put_file:
- fput(file);
-err_put_fd:
- put_unused_fd(argp->fd);
+ argp->fd = fd_stage(fd, file);
- return err;
+ return luo_ucmd_respond(ucmd, sizeof(*argp));
}
static int luo_ioctl_retrieve_session(struct luo_ucmd *ucmd)
{
struct liveupdate_ioctl_retrieve_session *argp = ucmd->cmd;
+ const struct fd_slot *fd;
struct file *file;
int err;
- argp->fd = get_unused_fd_flags(O_CLOEXEC);
- if (argp->fd < 0)
- return argp->fd;
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
err = luo_session_retrieve(argp->name, &file);
if (err < 0)
- goto err_put_fd;
-
- err = luo_ucmd_respond(ucmd, sizeof(*argp));
- if (err)
- goto err_put_file;
-
- fd_install(argp->fd, file);
-
- return 0;
+ return err;
-err_put_file:
- fput(file);
-err_put_fd:
- put_unused_fd(argp->fd);
+ argp->fd = fd_stage(fd, file);
- return err;
+ return luo_ucmd_respond(ucmd, sizeof(*argp));
}
static int luo_open(struct inode *inodep, struct file *filep)
diff --git a/kernel/liveupdate/luo_session.c b/kernel/liveupdate/luo_session.c
index f48e9a4185f97..7de35bad1ede9 100644
--- a/kernel/liveupdate/luo_session.c
+++ b/kernel/liveupdate/luo_session.c
@@ -282,33 +282,23 @@ static int luo_session_retrieve_fd(struct luo_session *session,
struct luo_ucmd *ucmd)
{
struct liveupdate_session_retrieve_fd *argp = ucmd->cmd;
+ const struct fd_slot *fd;
struct file *file;
int err;
- argp->fd = get_unused_fd_flags(O_CLOEXEC);
- if (argp->fd < 0)
- return argp->fd;
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
mutex_lock(&session->mutex);
err = luo_retrieve_file(&session->file_set, argp->token, &file);
mutex_unlock(&session->mutex);
if (err < 0)
- goto err_put_fd;
-
- err = luo_ucmd_respond(ucmd, sizeof(*argp));
- if (err)
- goto err_put_file;
-
- fd_install(argp->fd, file);
-
- return 0;
+ return err;
-err_put_file:
- fput(file);
-err_put_fd:
- put_unused_fd(argp->fd);
+ argp->fd = fd_stage(fd, file);
- return err;
+ return luo_ucmd_respond(ucmd, sizeof(*argp));
}
static int luo_session_finish(struct luo_session *session,
diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index 86cf4460d69ea..dc275ab1e031e 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -1957,7 +1957,7 @@ static long seccomp_set_mode_filter(unsigned int flags,
const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
struct seccomp_filter *prepared = NULL;
long ret = -EINVAL;
- int listener = -1;
+ const struct fd_slot *listener = NULL;
struct file *listener_f = NULL;
/* Validate flags. */
@@ -1990,15 +1990,14 @@ static long seccomp_set_mode_filter(unsigned int flags,
return PTR_ERR(prepared);
if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
- listener = get_unused_fd_flags(O_CLOEXEC);
- if (listener < 0) {
- ret = listener;
+ listener = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(listener)) {
+ ret = PTR_ERR(listener);
goto out_free;
}
listener_f = init_listener(prepared);
if (IS_ERR(listener_f)) {
- put_unused_fd(listener);
ret = PTR_ERR(listener_f);
goto out_free;
}
@@ -2038,11 +2037,9 @@ static long seccomp_set_mode_filter(unsigned int flags,
if (ret) {
listener_f->private_data = NULL;
fput(listener_f);
- put_unused_fd(listener);
seccomp_notify_detach(prepared);
} else {
- fd_install(listener, listener_f);
- ret = listener;
+ ret = fd_stage(listener, listener_f);
}
}
out_free:
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 23fb68fce000e..b5131fc597e6b 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -4809,14 +4809,14 @@ static int new_userfaultfd(int flags)
anon_inode_create_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
O_RDONLY | (flags & UFFD_SHARED_FCNTL_FLAGS),
NULL));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
/* prevent the mm struct to be freed */
mmgrab(ctx->mm);
fd_prepare_file(fdf)->f_mode |= FMODE_NOWAIT;
retain_and_null_ptr(ctx);
- return fd_publish(fdf);
+ return fd_prepare_fd(fdf);
}
static inline bool userfaultfd_syscall_allowed(int flags)
diff --git a/net/core/scm.c b/net/core/scm.c
index f0d44ecdb11fc..f05931eef54de 100644
--- a/net/core/scm.c
+++ b/net/core/scm.c
@@ -355,6 +355,7 @@ int scm_recv_one_fd(struct file *f, int __user *ufd, unsigned int flags,
bool notrunc)
{
int error;
+ const struct fd_slot *fd;
if (!ufd)
return -EFAULT;
@@ -363,16 +364,16 @@ int scm_recv_one_fd(struct file *f, int __user *ufd, unsigned int flags,
if (error)
return notrunc ? put_user(error, ufd) : error;
- FD_PREPARE(fdf, flags, get_file(f));
- if (fdf.err)
- return fdf.err;
+ fd = fd_prepare(flags);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
- error = put_user(fd_prepare_fd(fdf), ufd);
+ error = put_user(fd_prepare_fd(fd), ufd);
if (error)
return error;
- __receive_sock(fd_prepare_file(fdf));
- return fd_publish(fdf);
+ __receive_sock(f);
+ return fd_stage(fd, get_file(f));
}
void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm, bool notrunc)
diff --git a/net/handshake/netlink.c b/net/handshake/netlink.c
index 3fd4fef9bab1a..98d4266e700e9 100644
--- a/net/handshake/netlink.c
+++ b/net/handshake/netlink.c
@@ -106,19 +106,23 @@ int handshake_nl_accept_doit(struct sk_buff *skb, struct genl_info *info)
err = -EAGAIN;
req = handshake_req_next(hn, class);
if (req) {
- FD_PREPARE(fdf, O_CLOEXEC, req->hr_file);
- if (fdf.err) {
+ /* The ack carries the error, sendmsg() succeeds: stage last. */
+ const struct fd_slot *fd = fd_prepare(O_CLOEXEC);
+
+ if (IS_ERR(fd)) {
fput(req->hr_file); /* drop ref from handshake_req_next() */
- err = fdf.err;
+ err = PTR_ERR(fd);
goto out_complete;
}
- err = req->hr_proto->hp_accept(req, info, fd_prepare_fd(fdf));
- if (err)
- goto out_complete; /* Automatic cleanup handles fput */
+ err = req->hr_proto->hp_accept(req, info, fd_prepare_fd(fd));
+ if (err) {
+ fput(req->hr_file); /* not staged, drop it by hand */
+ goto out_complete;
+ }
- trace_handshake_cmd_accept(net, req, req->hr_sk, fd_prepare_fd(fdf));
- fd_publish(fdf);
+ fd_stage(fd, req->hr_file);
+ trace_handshake_cmd_accept(net, req, req->hr_sk, fd_prepare_fd(fd));
return 0;
}
diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c
index 71af69d442f21..2d744ee3d3404 100644
--- a/net/kcm/kcmsock.c
+++ b/net/kcm/kcmsock.c
@@ -1580,14 +1580,13 @@ static int kcm_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
struct kcm_clone info;
FD_PREPARE(fdf, 0, kcm_clone(sock));
- if (fdf.err)
- return fdf.err;
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
info.fd = fd_prepare_fd(fdf);
if (copy_to_user((void __user *)arg, &info, sizeof(info)))
return -EFAULT;
- fd_publish(fdf);
err = 0;
break;
}
diff --git a/net/sctp/socket.c b/net/sctp/socket.c
index c7b9e325ec1cd..b75c50af8c169 100644
--- a/net/sctp/socket.c
+++ b/net/sctp/socket.c
@@ -5744,46 +5744,41 @@ static int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id,
}
static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
- struct file **newfile, unsigned flags)
+ unsigned flags)
{
struct socket *newsock;
+ struct file *newfile;
+ const struct fd_slot *fd;
int retval;
retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
if (retval < 0)
- goto out;
+ return retval;
- /* Map the socket to an unused fd that can be returned to the user. */
- retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
- if (retval < 0) {
+ /* Map the socket to a reserved fd that can be returned to the user. */
+ fd = fd_prepare(flags & SOCK_CLOEXEC);
+ if (IS_ERR(fd)) {
sock_release(newsock);
- goto out;
- }
-
- *newfile = sock_alloc_file(newsock, 0, NULL);
- if (IS_ERR(*newfile)) {
- put_unused_fd(retval);
- retval = PTR_ERR(*newfile);
- *newfile = NULL;
- return retval;
+ return PTR_ERR(fd);
}
- pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
- retval);
-
- peeloff->sd = retval;
+ newfile = sock_alloc_file(newsock, 0, NULL);
+ if (IS_ERR(newfile))
+ return PTR_ERR(newfile);
if (flags & SOCK_NONBLOCK)
- (*newfile)->f_flags |= O_NONBLOCK;
-out:
- return retval;
+ newfile->f_flags |= O_NONBLOCK;
+
+ peeloff->sd = fd_stage(fd, newfile);
+ pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
+ peeloff->sd);
+ return peeloff->sd;
}
static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
{
sctp_peeloff_arg_t peeloff;
- struct file *newfile = NULL;
- int retval = 0;
+ int retval;
if (len < sizeof(sctp_peeloff_arg_t))
return -EINVAL;
@@ -5791,24 +5786,17 @@ static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval
if (copy_from_user(&peeloff, optval, len))
return -EFAULT;
- retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
+ retval = sctp_getsockopt_peeloff_common(sk, &peeloff, 0);
if (retval < 0)
- goto out;
+ return retval;
/* Return the fd mapped to the new socket. */
- if (put_user(len, optlen)) {
- fput(newfile);
- put_unused_fd(retval);
+ if (put_user(len, optlen))
return -EFAULT;
- }
- if (copy_to_user(optval, &peeloff, len)) {
- fput(newfile);
- put_unused_fd(retval);
+ if (copy_to_user(optval, &peeloff, len))
return -EFAULT;
- }
- fd_install(retval, newfile);
-out:
+
return retval;
}
@@ -5816,8 +5804,7 @@ static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
char __user *optval, int __user *optlen)
{
sctp_peeloff_flags_arg_t peeloff;
- struct file *newfile = NULL;
- int retval = 0;
+ int retval;
if (len < sizeof(sctp_peeloff_flags_arg_t))
return -EINVAL;
@@ -5826,24 +5813,17 @@ static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
return -EFAULT;
retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
- &newfile, peeloff.flags);
+ peeloff.flags);
if (retval < 0)
- goto out;
+ return retval;
/* Return the fd mapped to the new socket. */
- if (put_user(len, optlen)) {
- fput(newfile);
- put_unused_fd(retval);
+ if (put_user(len, optlen))
return -EFAULT;
- }
- if (copy_to_user(optval, &peeloff, len)) {
- fput(newfile);
- put_unused_fd(retval);
+ if (copy_to_user(optval, &peeloff, len))
return -EFAULT;
- }
- fd_install(retval, newfile);
-out:
+
return retval;
}
diff --git a/sound/core/compress_offload.c b/sound/core/compress_offload.c
index 23d62fede06e8..94bc226f8db94 100644
--- a/sound/core/compress_offload.c
+++ b/sound/core/compress_offload.c
@@ -1065,7 +1065,8 @@ static u64 snd_compr_seqno_next(struct snd_compr_stream *stream)
static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_task *utask)
{
struct snd_compr_task_runtime *task;
- int retval, fd_i, fd_o;
+ int retval;
+ const struct fd_slot *fd_i, *fd_o;
if (stream->runtime->total_tasks >= stream->runtime->fragments)
return -EBUSY;
@@ -1080,29 +1081,26 @@ static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_
if (retval < 0)
goto cleanup;
/* similar functionality as in dma_buf_fd(), but ensure that both
- file descriptors are allocated before fd_install() */
+ file descriptors are reserved before either is staged */
if (!task->input || !task->input->file || !task->output || !task->output->file) {
retval = -EINVAL;
goto free_driver_task;
}
- fd_i = get_unused_fd_flags(O_WRONLY|O_CLOEXEC);
- if (fd_i < 0) {
- retval = fd_i;
+ fd_i = fd_prepare(O_WRONLY|O_CLOEXEC);
+ if (IS_ERR(fd_i)) {
+ retval = PTR_ERR(fd_i);
goto free_driver_task;
}
- fd_o = get_unused_fd_flags(O_RDONLY|O_CLOEXEC);
- if (fd_o < 0) {
- retval = fd_o;
- put_unused_fd(fd_i);
+ fd_o = fd_prepare(O_RDONLY|O_CLOEXEC);
+ if (IS_ERR(fd_o)) {
+ retval = PTR_ERR(fd_o);
goto free_driver_task;
}
/* keep dmabuf reference until freed with task free ioctl */
get_dma_buf(task->input);
get_dma_buf(task->output);
- fd_install(fd_i, task->input->file);
- fd_install(fd_o, task->output->file);
- utask->input_fd = fd_i;
- utask->output_fd = fd_o;
+ utask->input_fd = fd_stage(fd_i, task->input->file);
+ utask->output_fd = fd_stage(fd_o, task->output->file);
list_add_tail(&task->list, &stream->runtime->tasks);
stream->runtime->total_tasks++;
return 0;
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 625e62e1a0318..e0d1cb869342e 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -526,17 +526,16 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)
struct gmem_file *f;
struct inode *inode;
struct file *file;
- int fd, err;
+ const struct fd_slot *fd;
+ int err;
- fd = get_unused_fd_flags(0);
- if (fd < 0)
- return fd;
+ fd = fd_prepare(0);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
f = kzalloc_obj(*f);
- if (!f) {
- err = -ENOMEM;
- goto err_fd;
- }
+ if (!f)
+ return -ENOMEM;
/* __fput() will take care of fops_put(). */
if (!fops_get(&kvm_gmem_fops)) {
@@ -575,8 +574,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)
xa_init(&f->bindings);
list_add(&f->entry, &GMEM_I(inode)->gmem_file_list);
- fd_install(fd, file);
- return fd;
+ return fd_stage(fd, file);
err_inode:
iput(inode);
@@ -584,8 +582,6 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)
fops_put(&kvm_gmem_fops);
err_gmem:
kfree(f);
-err_fd:
- put_unused_fd(fd);
return err;
}
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 65eb26a0520d8..8d3d47a6c98aa 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4326,27 +4326,18 @@ static const struct file_operations kvm_vcpu_stats_fops = {
static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu)
{
- int fd;
- struct file *file;
char name[15 + ITOA_MAX_LEN + 1];
snprintf(name, sizeof(name), "kvm-vcpu-stats:%d", vcpu->vcpu_id);
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- file = anon_inode_getfile_fmode(name, &kvm_vcpu_stats_fops, vcpu,
- O_RDONLY, FMODE_PREAD);
- if (IS_ERR(file)) {
- put_unused_fd(fd);
- return PTR_ERR(file);
- }
+ FD_PREPARE(fdf, O_CLOEXEC,
+ anon_inode_getfile_fmode(name, &kvm_vcpu_stats_fops, vcpu,
+ O_RDONLY, FMODE_PREAD));
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
kvm_get_kvm(vcpu->kvm);
- fd_install(fd, file);
-
- return fd;
+ return fd_prepare_fd(fdf);
}
#ifdef CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY
@@ -5136,24 +5127,14 @@ static const struct file_operations kvm_vm_stats_fops = {
static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm)
{
- int fd;
- struct file *file;
-
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- file = anon_inode_getfile_fmode("kvm-vm-stats",
- &kvm_vm_stats_fops, kvm, O_RDONLY, FMODE_PREAD);
- if (IS_ERR(file)) {
- put_unused_fd(fd);
- return PTR_ERR(file);
- }
+ FD_PREPARE(fdf, O_CLOEXEC,
+ anon_inode_getfile_fmode("kvm-vm-stats", &kvm_vm_stats_fops,
+ kvm, O_RDONLY, FMODE_PREAD));
+ if (IS_ERR(fdf))
+ return PTR_ERR(fdf);
kvm_get_kvm(kvm);
- fd_install(fd, file);
-
- return fd;
+ return fd_prepare_fd(fdf);
}
#define SANITY_CHECK_MEM_REGION_FIELD(field) \
@@ -5499,21 +5480,20 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(file_is_kvm);
static int kvm_dev_ioctl_create_vm(unsigned long type)
{
char fdname[ITOA_MAX_LEN + 1];
- int r, fd;
struct kvm *kvm;
struct file *file;
+ const struct fd_slot *fd;
+ int r;
- fd = get_unused_fd_flags(O_CLOEXEC);
- if (fd < 0)
- return fd;
+ fd = fd_prepare(O_CLOEXEC);
+ if (IS_ERR(fd))
+ return PTR_ERR(fd);
- snprintf(fdname, sizeof(fdname), "%d", fd);
+ snprintf(fdname, sizeof(fdname), "%d", fd_prepare_fd(fd));
kvm = kvm_create_vm(type, fdname);
- if (IS_ERR(kvm)) {
- r = PTR_ERR(kvm);
- goto put_fd;
- }
+ if (IS_ERR(kvm))
+ return PTR_ERR(kvm);
file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
if (IS_ERR(file)) {
@@ -5529,13 +5509,10 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)
*/
kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);
- fd_install(fd, file);
- return fd;
+ return fd_stage(fd, file);
put_kvm:
kvm_put_kvm(kvm);
-put_fd:
- put_unused_fd(fd);
return r;
}
|
| 31/3 | 2026/09/15 14:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 62813 output: 45 thoughts: 2051 Thoughts: **KMSAN Assessment: Deferring File Descriptor Publication** Alright, let's break this down. This patch fundamentally alters how file descriptors are managed within the kernel, specifically delaying their publication until the syscall exits or discarding them if the syscall fails. This involves a new `fd_prepare()`, `fd_stage()`, and associated structures like `fd_slots` within the `task_struct`. My primary concern, as always, is whether this introduces any *new* risks of uninitialized memory reads that KMSAN would catch, but KASAN or other tools would miss. Previously, we'd have this straightforward sequence: allocate an FD, do some stuff, and then `fd_install()`. Now, we stage the FD with `fd_prepare()`, attach the file with `fd_stage()`, and only commit it during syscall exit. The critical aspect here is ensuring that no uninitialized memory makes its way to userspace or is used in any conditional logic. Let's dissect this piece by piece: 1. **`fd_slots` and `task_struct`:** The addition of `struct fd_slots` to `task_struct` and its zeroing during `dup_task_struct` (fork) seems safe. The `memset` ensures that freshly created tasks start with clean slots. 2. **`fd_prepare()` and `fd_slot_record()`:** These functions are the core of the new mechanism. `fd_slot_record()` allocates a file descriptor and records it in `current->fd_slots`. Crucially, I see that within `fd_slot_record()` both `slot->fd` and `slot->file` are explicitly initialized: `slot->fd = fd; slot->file = NULL;`. The `slots->nr` is also tracked, which seems fine as well. The spilling mechanism with `kvrealloc` also looks reasonable, because all slots are initialized. 3. **`fd_stage()`:** This simply attaches the `file` pointer to a prepared slot. The file pointer is assigned, which is good. 4. **`syscall_exit_work` and `fd_slots_commit()` / `fd_slots_drop()`:** The commit and drop logic is the actual commit. `fd_slots_commit` loops from 0 up to `slots->nr`. The important thing here is that during the loop, every single slot was initialized via `fd_slot_record()`, so there's no way to read uninitialized memory there. Same with `fd_slots_drop`. Finally the `slots->nr = 0` sets it back to clean state. 5. **`exit_fd_slots()`:** This deals with any remaining slots during task exit. There's no reading of uninitialized memory. Now, the main concern is when this new mechanism is called. Let's look at the callers of `fd_prepare()`, `fd_stage()`, etc. that were changed in the patch: * **`drivers/accel/habanalabs/common/memory.c`:** The code writes into the dma buffer. So it should not be uninitialized memory. * **`drivers/char/tpm/tpm_vtpm_proxy.c`:** Looks like it first stages a fd, and then copies data to user space. It could be KMSAN violation. Let's make sure the data being copied is initialized. After inspecting `vtpmx_ioc_new_dev`, I confirm that the structure `vtpm_new_dev` is properly initialized before being copied to user space. **Conclusion:** Based on this analysis, I'm confident that this patch does not introduce any new *significant* risks of uninitialized memory reads. All of the key data structures appear to be fully initialized before being read or passed to user space. Therefore, I will set `NeedsKMSAN=false`. |
| 32/3 | 2026/09/15 14:40 | tool | grepper |
0mArgs:{
"Expression": "vtpmx_ioc_new_dev",
"PathPrefix": "drivers/char/tpm/tpm_vtpm_proxy.c"
}Results: {
"Output": "drivers/char/tpm/tpm_vtpm_proxy.c=580=static void vtpm_proxy_delete_device(struct proxy_dev *proxy_dev)\n--\ndrivers/char/tpm/tpm_vtpm_proxy.c-601-/**\ndrivers/char/tpm/tpm_vtpm_proxy.c:602: * vtpmx_ioc_new_dev - handler for the %VTPM_PROXY_IOC_NEW_DEV ioctl\ndrivers/char/tpm/tpm_vtpm_proxy.c-603- * @file:\t/dev/vtpmx\n--\ndrivers/char/tpm/tpm_vtpm_proxy.c-612- */\ndrivers/char/tpm/tpm_vtpm_proxy.c:613:static long vtpmx_ioc_new_dev(struct file *file, unsigned int ioctl,\ndrivers/char/tpm/tpm_vtpm_proxy.c-614-\t\t\t unsigned long arg)\n--\ndrivers/char/tpm/tpm_vtpm_proxy.c=647=static long vtpmx_fops_ioctl(struct file *f, unsigned int ioctl,\n--\ndrivers/char/tpm/tpm_vtpm_proxy.c-651-\tcase VTPM_PROXY_IOC_NEW_DEV:\ndrivers/char/tpm/tpm_vtpm_proxy.c:652:\t\treturn vtpmx_ioc_new_dev(f, ioctl, arg);\ndrivers/char/tpm/tpm_vtpm_proxy.c-653-\tdefault:\n"
} |
| 33/3 | 2026/09/15 14:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 66154 output: 44 thoughts: 32 Thoughts: Here's my summary, as though I'm reading and processing the provided code snippet: **Diving into the TPM Virtualization Proxy** Okay, time to get my hands dirty and dig into the `tpm_vtpm_proxy.c` file within the `drivers/char/tpm` directory. Specifically, I need to focus on lines 610 through 645. That's usually where the meat of a core function or crucial data structure lies. I'm expecting to see something pretty critical for understanding the proxy's interaction with the virtual TPM, likely related to command handling or state management. Given the file name, I'd anticipate this section deals with how the proxy interprets and passes through commands, and handles any necessary transformations between the virtual and physical TPM. I'll be looking closely at the data structures being used here. The `tpm_vtpm_proxy` driver is a critical piece, and understanding this section will give me insight into how its core functionality works. It's time to see what this specific code segment reveals about command routing, error handling, or any special considerations the proxy has to take into account. |
| 34/3 | 2026/09/15 14:40 | tool | read-file |
0mArgs:{
"File": "drivers/char/tpm/tpm_vtpm_proxy.c",
"FirstLine": 610,
"LineCount": 35
}Results: {
"Contents": " 610:\t * will be provided with a file descriptor to communicate with the clients and\n 611:\t * major and minor numbers for the TPM device.\n 612:\t */\n 613:\tstatic long vtpmx_ioc_new_dev(struct file *file, unsigned int ioctl,\n 614:\t\t\t\t unsigned long arg)\n 615:\t{\n 616:\t\tvoid __user *argp = (void __user *)arg;\n 617:\t\tstruct vtpm_proxy_new_dev __user *vtpm_new_dev_p;\n 618:\t\tstruct vtpm_proxy_new_dev vtpm_new_dev;\n 619:\t\tstruct file *vtpm_file;\n 620:\t\n 621:\t\tif (!capable(CAP_SYS_ADMIN))\n 622:\t\t\treturn -EPERM;\n 623:\t\n 624:\t\tvtpm_new_dev_p = argp;\n 625:\t\n 626:\t\tif (copy_from_user(\u0026vtpm_new_dev, vtpm_new_dev_p,\n 627:\t\t\t\t sizeof(vtpm_new_dev)))\n 628:\t\t\treturn -EFAULT;\n 629:\t\n 630:\t\tvtpm_file = vtpm_proxy_create_device(\u0026vtpm_new_dev);\n 631:\t\tif (IS_ERR(vtpm_file))\n 632:\t\t\treturn PTR_ERR(vtpm_file);\n 633:\t\n 634:\t\tif (copy_to_user(vtpm_new_dev_p, \u0026vtpm_new_dev,\n 635:\t\t\t\t sizeof(vtpm_new_dev)))\n 636:\t\t\treturn -EFAULT;\n 637:\t\n 638:\t\treturn 0;\n 639:\t}\n 640:\t\n 641:\t/*\n 642:\t * vtpmx_fops_ioctl: ioctl on /dev/vtpmx\n 643:\t *\n 644:\t * Return:\n"
} |
| 35/3 | 2026/09/15 14:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 67020 output: 35 thoughts: 543 Thoughts: **Analyzing Structure Initialization in Kernel Code** Okay, so I'm looking at this `vtpm` driver code and I see this `copy_from_user` call initializing the `vtpm_new_dev` struct before `vtpm_proxy_create_device` is even called. That's good practice. I want to see if this pattern is common or if there's a rationale for this specific usage. Let's dive into some other kernel code, specifically around DMA buffers since that's something I'm very familiar with. I'm going to start by checking `drivers/dma-buf/dma-buf.c`. Aha, `dma_buf_export_sync_file` shows the same technique. We see `arg` being populated via `copy_from_user`, some modification happening to `arg.fd`, and then `copy_to_user` sending the modified structure back. Consistent. Moving on to `drivers/dma-buf/sw_sync.c`, in `sw_sync_ioctl_create_fence`, the structure `data` is initialized with `copy_from_user` as well. Again, the familiar pattern: the structure is brought in from user space, modified (specifically the `data.fence` field), and then sent back using `copy_to_user`. This seems to be the preferred method. Let's also look at `drivers/dma-buf/sync_file.c`, where `sync_file_ioctl_merge` follows the same initialization pattern with `data` in this case. I'm noting this is a pattern, which makes me think this is considered safe/best practice. Just to be sure, let's look at a different subsystem: `drivers/gpio/gpiolib-cdev.c`. In this case, `handlereq.fd` is set first and then copied to user space. It makes sense, as the prepare function is modifying that field. Now I should check how `handlereq`, `ulr`, and `eventreq` are initialized in `linehandle_create`, `linereq_create`, and `lineevent_create`. I want to see if the structure initialization happens the same way, or if these structures are handled differently. This will give me a more complete picture of kernel data handling practices. |
| 36/3 | 2026/09/15 14:40 | tool | grepper |
0mArgs:{
"Expression": "handlereq",
"PathPrefix": "drivers/gpio/gpiolib-cdev.c"
}Results: {
"Output": "drivers/gpio/gpiolib-cdev.c=303=static int linehandle_create(struct gpio_device *gdev, void __user *ip)\ndrivers/gpio/gpiolib-cdev.c-304-{\ndrivers/gpio/gpiolib-cdev.c:305:\tstruct gpiohandle_request handlereq;\ndrivers/gpio/gpiolib-cdev.c-306-\tstruct linehandle_state *lh __free(linehandle_free) = NULL;\n--\ndrivers/gpio/gpiolib-cdev.c-309-\ndrivers/gpio/gpiolib-cdev.c:310:\tif (copy_from_user(\u0026handlereq, ip, sizeof(handlereq)))\ndrivers/gpio/gpiolib-cdev.c-311-\t\treturn -EFAULT;\ndrivers/gpio/gpiolib-cdev.c:312:\tif ((handlereq.lines == 0) || (handlereq.lines \u003e GPIOHANDLES_MAX))\ndrivers/gpio/gpiolib-cdev.c-313-\t\treturn -EINVAL;\ndrivers/gpio/gpiolib-cdev.c-314-\ndrivers/gpio/gpiolib-cdev.c:315:\tlflags = handlereq.flags;\ndrivers/gpio/gpiolib-cdev.c-316-\n--\ndrivers/gpio/gpiolib-cdev.c-325-\ndrivers/gpio/gpiolib-cdev.c:326:\tif (handlereq.consumer_label[0] != '\\0') {\ndrivers/gpio/gpiolib-cdev.c-327-\t\t/* label is only initialized if consumer_label is set */\ndrivers/gpio/gpiolib-cdev.c:328:\t\tlh-\u003elabel = kstrndup(handlereq.consumer_label,\ndrivers/gpio/gpiolib-cdev.c:329:\t\t\t\t sizeof(handlereq.consumer_label) - 1,\ndrivers/gpio/gpiolib-cdev.c-330-\t\t\t\t GFP_KERNEL);\n--\ndrivers/gpio/gpiolib-cdev.c-334-\ndrivers/gpio/gpiolib-cdev.c:335:\tlh-\u003enum_descs = handlereq.lines;\ndrivers/gpio/gpiolib-cdev.c-336-\ndrivers/gpio/gpiolib-cdev.c-337-\t/* Request each GPIO */\ndrivers/gpio/gpiolib-cdev.c:338:\tfor (i = 0; i \u003c handlereq.lines; i++) {\ndrivers/gpio/gpiolib-cdev.c:339:\t\tu32 offset = handlereq.lineoffsets[i];\ndrivers/gpio/gpiolib-cdev.c-340-\t\tstruct gpio_desc *desc = gpio_device_get_desc(gdev, offset);\n--\ndrivers/gpio/gpiolib-cdev.c-348-\t\tlh-\u003edescs[i] = desc;\ndrivers/gpio/gpiolib-cdev.c:349:\t\tlinehandle_flags_to_desc_flags(handlereq.flags, \u0026desc-\u003eflags);\ndrivers/gpio/gpiolib-cdev.c-350-\n--\ndrivers/gpio/gpiolib-cdev.c-359-\t\tif (lflags \u0026 GPIOHANDLE_REQUEST_OUTPUT) {\ndrivers/gpio/gpiolib-cdev.c:360:\t\t\tint val = !!handlereq.default_values[i];\ndrivers/gpio/gpiolib-cdev.c-361-\n--\ndrivers/gpio/gpiolib-cdev.c-383-\ndrivers/gpio/gpiolib-cdev.c:384:\thandlereq.fd = fd_prepare_fd(fdf);\ndrivers/gpio/gpiolib-cdev.c:385:\tif (copy_to_user(ip, \u0026handlereq, sizeof(handlereq)))\ndrivers/gpio/gpiolib-cdev.c-386-\t\treturn -EFAULT;\n--\ndrivers/gpio/gpiolib-cdev.c-388-\tdev_dbg(\u0026gdev-\u003edev, \"registered chardev handle for %d lines\\n\",\ndrivers/gpio/gpiolib-cdev.c:389:\t\thandlereq.lines);\ndrivers/gpio/gpiolib-cdev.c-390-\n"
} |
| 37/3 | 2026/09/15 14:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 69212 output: 43 thoughts: 175 Thoughts: **Analyzing Kernel Memory Allocation and File Descriptor Handling** Okay, so I'm diving into this code. It looks like everything's being initialized via `copy_from_user`, which is the first thing I need to keep in mind. Now, I need to understand how file descriptors are being handled, so let's check `drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c`. Right, here we see `info->out.handle = fd_stage(fd, sync_file->file);`. The crucial piece is `info` being passed in there, so I'll need to remember that context. Now, let's switch gears and investigate `drivers/gpu/drm/amd/amdkfd/kfd_chardev.c`. I'm specifically looking at `criu_checkpoint_bos`. Ah, here we see memory allocation. It looks like `bo_buckets` is being allocated using `kvcalloc`, either with `bo_buckets = kvcalloc(num_bos, sizeof(*bo_buckets), GFP_KERNEL);` or something similar with `bo_privs`. Hold on a second, I need to verify exactly *how* these `bo_buckets` and `bo_privs` are being allocated within this file. The allocation strategy is important, and I need to be completely sure about how it's done for this specific scenario. |
| 38/3 | 2026/09/15 14:40 | tool | grepper |
0mArgs:{
"Expression": "bo_buckets =",
"PathPrefix": "drivers/gpu/drm/amd/amdkfd/kfd_chardev.c"
}Results: {
"Output": "drivers/gpu/drm/amd/amdkfd/kfd_chardev.c=2016=static int criu_checkpoint_bos(struct kfd_process *p,\n--\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c-2026-\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c:2027:\tbo_buckets = kvcalloc(num_bos, sizeof(*bo_buckets), GFP_KERNEL);\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c-2028-\tif (!bo_buckets)\n--\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c=2538=static int criu_restore_bos(struct kfd_process *p,\n--\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c-2542-{\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c:2543:\tstruct kfd_criu_bo_bucket *bo_buckets = NULL;\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c-2544-\tstruct kfd_criu_bo_priv_data *bo_privs = NULL;\n--\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c-2553-\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c:2554:\tbo_buckets = kvmalloc_objs(*bo_buckets, args-\u003enum_bos);\ndrivers/gpu/drm/amd/amdkfd/kfd_chardev.c-2555-\tif (!bo_buckets)\n"
} |
| 39/3 | 2026/09/15 14:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 70166 output: 25 thoughts: 1157 Thoughts: **Analyzing File Descriptor Vulnerabilities** Okay, let's break down this potential file descriptor leak issue across the kernel. My initial thought process is to systematically examine code patterns where file descriptors are handled and potentially copied to user space. I'm looking for scenarios where a file descriptor might be prepared (e.g., via `fd_prepare_fd` or `fd_stage`), but the data structure containing it might not be properly initialized before being exposed to userspace via `copy_to_user`. I'm starting from the first example and then using that approach to explore other examples. First, in `criu_checkpoint_bos`, the `bo_buckets` array, which contains file descriptors, is allocated using `kvcalloc` and then copied to user space. Crucially, it is zeroed before the copy, so that's okay. Then, `drivers/iio/industrialio-buffer.c` shows `fd_prepare_fd` being used to create a file descriptor (`fdno`), and then the `fdno` (which is an `int`) is directly copied to user space. This seems safe because it's just the int value. In `drivers/iommu/iommufd/eventq.c`, `cmd->out_fault_fd` gets a file descriptor via `fd_prepare_fd`. The crucial thing here is that `cmd` is fully initialized before the potentially leaked field gets set, so this also seems safe. Now, `drivers/misc/fastrpc.c`. The code grabs data from userspace, stores a fd in the `bp` struct, and then copies `bp` back to user space. It appears `bp` is read from user with `copy_from_user` and then a field is assigned to it before it is copied back with `copy_to_user`. So, *theoretically*, if the user provides a malicious value, there *could* be a risk, but the kernel is probably careful. This bears more investigation if that's the area of interest. Next, `drivers/vfio/vfio_main.c`: Here, `mig->data_fd` is assigned with a file descriptor created with `fd_prepare_fd`, and then the entire `mig` structure is copied to user space. The important factor is that `mig` is a structure passed in from user space, so, similarly to `fastrpc`, we *could* have the same theoretical risk. `drivers/virt/nitro_enclaves/ne_misc_dev.c` looks safe because `ne_enclave->slot_uid` appears to be initialized before it is copied to the user. Moving on to `net/sctp/socket.c`, a `peeloff` structure is received from user space, populated with a file descriptor (`peeloff.sd = fd_stage(fd, newfile);`), and then copied back to user space. Again, the data structure is fully initialized before the `copy_to_user`, which should be okay. `sound/core/compress_offload.c`: `utask->input_fd` and `utask->output_fd` are assigned via `fd_stage`, and then later used. This seems fine as only a few fields are used. In `io_uring/io_uring.c`, the `io_req_post_cqe` functions reference `current->fd_slots`, but the code suggests a commit operation occurs which implies this is handled correctly. Finally, the `fs/file.c` section is about the `fd_slots` data structure. It looks like it is zeroed when a task is created using `dup_task_struct` and `init_task`. This suggests potential file descriptor leakage is a small risk because the file descriptors are likely initialized on creation. Therefore, across these examples, I'm identifying a few patterns. 1. Direct copying of an `int` containing the file descriptor is fine. 2. When an entire structure is copied to user space after a file descriptor is assigned, there might be a risk. If this field is part of a large data structure passed in by the user, and is not initialized, then it needs scrutiny. 3. Structures initialized within the kernel prior to copying seem safe. I need to focus on where data from user space is combined with file descriptors from `fd_prepare_fd` or `fd_stage` before copying them back to the user to identify potential vulnerabilities. My primary focus is on structures passed *into* the kernel with file descriptors that are modified before being sent back to the user, particularly if the initial user-provided values are not carefully validated or explicitly zeroed. |
| 40/3 | 2026/09/15 14:41 | tool | grepper |
0mError:no matches Args: {
"Expression": "fd_slots",
"PathPrefix": "init/"
}Results: {
"error": "no matches"
} |
| 41/3 | 2026/09/15 14:41 | llm | kmsan-evaluator |
15mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 72366 output: 29 thoughts: 3498 Thoughts: **File Descriptor Spill Slot Analysis** Okay, so I'm diving deep into this file descriptor spill slot code, trying to see if there are any subtle uninitialized memory vulnerabilities introduced by these changes. Starting with the basics: `init_task` has a statically allocated `.data` section, so any uninitialized fields will be zeroed, and `dup_task_struct` also ensures initial zeroing using `memset`. This seems safe from the get-go. Now, looking at `fd_slot_record`, the core function. I can see that `slots->nr` is incremented after `fd_slot_spill` returns. If `fd_slot_spill` fails to allocate memory (returns `-ENOMEM`), `slots->nr` *isn't* incremented. This means if we hit that error case, there's a potential for an out-of-bounds read later if the code isn't careful. Then `fd_prepare` checks and releases the FD but doesn't actually remove the uninitialized memory. `FD_PREPARE` can return an error if `_file_owned` fails, setting `_fdf` to an error pointer and not incrementing `slots->nr` or zeroing the memory. Let's check every place `slots->spill` is accessed and see if any code reads beyond the initialized area: `fd_slot`, `fd_slot_find`, `fd_slots_install`, and `fd_slots_drop`. They all seem to be okay, because the loop limits are controlled by `slots->nr`, and that is checked in the other functions, ensuring we're only reading valid, initialized data. Now, thinking about `fd_slots_finish`, where `slots->nr` is set to zero, and `kvfree` on the `slots->spill` data. And if a syscall returns an error with the `FD_PREPARE` path, it calls `fd_slots_drop`. Inside `fd_slots_drop` we call `__put_unused_fd` for all initialized slots and call `fput` if the slot isn't `NULL`. Crucially, `fd_slot_record` initialized `slot->file` as `NULL`. So even if there was an error, the code ensures that `fput` isn't called on a potentially uninitialized file pointer! What about `FD_ADD`? It doesn't even use `fd_slots`, so we can dismiss it. Next, I'm checking the architecture-specific syscall entry/exit code for the changes and the `TIF_FD_SLOTS` check, and it appears the changes are safe in this context. And for generic entry, it's calling `fd_slots_commit`, which cleans up after. So, that's not it. Finally, I need to verify that we're not leaking any uninitialized data to userspace in these new patches. I must check all places where kernel structures are passed to user space. I've checked the changed code for `copy_to_user`, `put_user`, etc. and it looks like `put_user`, `fd_stage` and `fd_prepare_fd`, are all safe. I've also checked the modified drivers and functions and it appears there's nothing wrong in `fs/file.c`, `net/core/scm.c`, and the driver files. Everything's passing and returning values that have been initialized correctly. |
| 42/3 | 2026/09/15 14:56 | tool | grepper |
0mArgs:{
"Expression": "kcm_clone",
"PathPrefix": "net/kcm/"
}Results: {
"Output": "net/kcm/kcmsock.c=1521=static struct proto kcm_proto = {\n--\nnet/kcm/kcmsock.c-1527-/* Clone a kcm socket. */\nnet/kcm/kcmsock.c:1528:static struct file *kcm_clone(struct socket *osock)\nnet/kcm/kcmsock.c-1529-{\n--\nnet/kcm/kcmsock.c=1554=static int kcm_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)\n--\nnet/kcm/kcmsock.c-1579-\tcase SIOCKCMCLONE: {\nnet/kcm/kcmsock.c:1580:\t\tstruct kcm_clone info;\nnet/kcm/kcmsock.c-1581-\nnet/kcm/kcmsock.c:1582:\t\tFD_PREPARE(fdf, 0, kcm_clone(sock));\nnet/kcm/kcmsock.c-1583-\t\tif (IS_ERR(fdf))\n"
} |
| 43/3 | 2026/09/15 14:56 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 76932 output: 39 thoughts: 28 Thoughts: Here's a summary of my thought process, as if I were the one reading `net/kcm/kcmsock.c`: **Diving into the KCM Socket Code** Okay, time to get my hands dirty. Let's pull up lines 1575 through 1600 of `net/kcm/kcmsock.c`. I need to understand what's happening in this particular section of the code. Based on the context around it, I'm expecting this to deal with [**insert my area of expectation here** – *e.g., connection establishment, data transmission buffering, error handling related to a specific KCM socket function*]. I'm quickly scanning for key function calls, variables, and conditional statements. I'll be looking for any signs of the core KCM logic in this area. It's important to grasp how this part interacts with the broader architecture. I'm noting the data structures used; are they internal to the module, or are they interacting with other parts of the network stack or user space? What's the thread model here, and how does this code handle concurrency? I'm paying attention to any locking mechanisms to see how they're used to avoid race conditions. Are there any obvious potential performance bottlenecks? Are they using memory management correctly, or is there any potential for leaks? I'm looking for any signs of the issue I'm investigating. This is where the detective work begins. |
| 44/3 | 2026/09/15 14:56 | tool | read-file |
0mArgs:{
"File": "net/kcm/kcmsock.c",
"FirstLine": 1575,
"LineCount": 25
}Results: {
"Contents": "1575:\t\t\terr = kcm_unattach_ioctl(sock, \u0026info);\n1576:\t\n1577:\t\t\tbreak;\n1578:\t\t}\n1579:\t\tcase SIOCKCMCLONE: {\n1580:\t\t\tstruct kcm_clone info;\n1581:\t\n1582:\t\t\tFD_PREPARE(fdf, 0, kcm_clone(sock));\n1583:\t\t\tif (IS_ERR(fdf))\n1584:\t\t\t\treturn PTR_ERR(fdf);\n1585:\t\n1586:\t\t\tinfo.fd = fd_prepare_fd(fdf);\n1587:\t\t\tif (copy_to_user((void __user *)arg, \u0026info, sizeof(info)))\n1588:\t\t\t\treturn -EFAULT;\n1589:\t\n1590:\t\t\terr = 0;\n1591:\t\t\tbreak;\n1592:\t\t}\n1593:\t\tdefault:\n1594:\t\t\terr = -ENOIOCTLCMD;\n1595:\t\t\tbreak;\n1596:\t\t}\n1597:\t\n1598:\t\treturn err;\n1599:\t}\n"
} |
| 45/3 | 2026/09/15 14:56 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 77630 output: 27 thoughts: 62 Thoughts: **Diving into the KCM Socket Cloning Mechanism** Okay, hold on a second! My attention's grabbed by something in `net/kcm/kcmsock.c`. I see `SIOCKCMCLONE` and, more importantly, `struct kcm_clone info;`. The name `kcm_clone` immediately piques my interest. I need to understand what this structure *is*. Time to dig into the kernel source. I'm going to quickly grep for any instance of `struct kcm_clone`. Let's see what the broader picture is, and what this cloning mechanism actually *does* within the KCM context. Gotta get a handle on the data that this structure encapsulates! |
| 46/3 | 2026/09/15 14:56 | tool | grepper |
0mArgs:{
"Expression": "struct kcm_clone",
"PathPrefix": "include/"
}Results: {
"Output": "include/uapi/linux/kcm.h=23=struct kcm_unattach {\n--\ninclude/uapi/linux/kcm.h-26-\ninclude/uapi/linux/kcm.h:27:struct kcm_clone {\ninclude/uapi/linux/kcm.h-28-\tint fd;\n"
} |
| 47/3 | 2026/09/15 14:56 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 77965 output: 38 thoughts: 17 Thoughts: Here's my summary, as though I'm reading the provided text: **Diving into the Kernel's KCM Header** Alright, let's crack open `include/uapi/linux/kcm.h`. Time to see what KCM (Key Control Manager, I assume) is up to. I need to get a clear picture of how this thing is structured and what its interface looks like at the userspace level. This is where the magic (or the headache) begins – understanding the kernel's perspective on key management, and what we, the userspace folks, are exposed to. I'm hoping for well-defined structures, decent commenting, and a good sense of the overall architecture. Let's see if this header file lives up to my expectations. I expect to see definitions for the ioctls, data structures, and any other API elements related to key control in the Linux kernel. This file holds the key (pun intended!) to how we'll interact with the kernel's key management features. |
| 48/3 | 2026/09/15 14:56 | tool | read-file |
0mArgs:{
"File": "include/uapi/linux/kcm.h",
"FirstLine": 25,
"LineCount": 15
}Results: {
"Contents": " 25:\t};\n 26:\t\n 27:\tstruct kcm_clone {\n 28:\t\tint fd;\n 29:\t};\n 30:\t\n 31:\t#define SIOCKCMATTACH\t(SIOCPROTOPRIVATE + 0)\n 32:\t#define SIOCKCMUNATTACH\t(SIOCPROTOPRIVATE + 1)\n 33:\t#define SIOCKCMCLONE\t(SIOCPROTOPRIVATE + 2)\n 34:\t\n 35:\t#define KCMPROTO_CONNECTED\t0\n 36:\t\n 37:\t/* Socket options */\n 38:\t#define KCM_RECV_DISABLE\t1\n 39:\t\n"
} |
| 49/3 | 2026/09/15 14:56 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 78432 output: 37 thoughts: 585 Thoughts: **Analyzing the FD Preparation Logic** Okay, so I'm looking at this `kcm_ioctl` change, and initially, it seems pretty straightforward. The `struct kcm_clone` is just an `int fd;` with no padding, which is initialized with `fd_prepare_fd` using the output of `FD_PREPARE`. The code then copies this to userspace. The patch itself only swapped `fdf.err` check with `IS_ERR(fdf)` and removes `fd_publish(fdf)`. I remember reading the old code, and now I'm certain that the code was already handling the error check correctly. Since this `info.fd = fd_prepare_fd(fdf)` was happening before the changes, it means it's still being handled in the same manner. This patch seems more like a stylistic change. Just to be absolutely sure, let me take a look at `io_uring/mock_file.c`, because I remember seeing a similar pattern there with the `FD_PREPARE` and `fd_prepare_file` functions. Specifically, let's see what's happening around lines 250-280. Yup, it's the same pattern! The same `FD_PREPARE`, the `IS_ERR` check, `fd_prepare_file`, and then a userspace copy. And also, that `fd_publish` call is removed! So this pattern exists in more than one place, and it confirms my initial thought. This is just a refactoring for consistency and to remove redundant code. |
| 50/3 | 2026/09/15 14:56 | tool | read-file |
0mArgs:{
"File": "io_uring/mock_file.c",
"FirstLine": 250,
"LineCount": 30
}Results: {
"Contents": " 250:\t\tmf-\u003esize = mc.file_size;\n 251:\t\tmf-\u003erw_delay_ns = mc.rw_delay_ns;\n 252:\t\tif (mc.flags \u0026 IORING_MOCK_CREATE_F_POLL) {\n 253:\t\t\tfops = \u0026io_mock_poll_fops;\n 254:\t\t\tmf-\u003epollable = true;\n 255:\t\t}\n 256:\t\n 257:\t\tFD_PREPARE(fdf, O_RDWR | O_CLOEXEC,\n 258:\t\t\t anon_inode_create_getfile(\"[io_uring_mock]\", fops, mf,\n 259:\t\t\t\t\t\t O_RDWR | O_CLOEXEC, NULL));\n 260:\t\tif (IS_ERR(fdf))\n 261:\t\t\treturn PTR_ERR(fdf);\n 262:\t\n 263:\t\tretain_and_null_ptr(mf);\n 264:\t\tfile = fd_prepare_file(fdf);\n 265:\t\tfile-\u003ef_mode |= FMODE_READ | FMODE_CAN_READ | FMODE_WRITE |\n 266:\t\t\t\tFMODE_CAN_WRITE | FMODE_LSEEK;\n 267:\t\tif (mc.flags \u0026 IORING_MOCK_CREATE_F_SUPPORT_NOWAIT)\n 268:\t\t\tfile-\u003ef_mode |= FMODE_NOWAIT;\n 269:\t\n 270:\t\tmc.out_fd = fd_prepare_fd(fdf);\n 271:\t\tif (copy_to_user(uarg, \u0026mc, uarg_size))\n 272:\t\t\treturn -EFAULT;\n 273:\t\n 274:\t\treturn 0;\n 275:\t}\n 276:\t\n 277:\tstatic int io_probe_mock(struct io_uring_cmd *cmd)\n 278:\t{\n 279:\t\tconst struct io_uring_sqe *sqe = cmd-\u003esqe;\n"
} |
| 51/3 | 2026/09/15 14:56 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 79932 output: 37 thoughts: 95 Thoughts: Here's my thought process, as it stands: **Debugging io_uring/zcrx.c and File Descriptor Handling** Okay, so the `mc` variable, that's what was passed in at the beginning of `io_create_mock_file`. Got it. Now I need to trace this further. I remember that we were looking at the `io_uring/zcrx.c` file. Right, let's look at what's happening there. First, I need to understand how the file descriptor is being handled, so I'll check `ce->zcrx_fd = fd_prepare_fd(fdf)`. Next, it seems like we're copying some data to user space: `copy_to_user(arg, ctrl, sizeof(*ctrl))`. I need to figure out what `arg` and `ctrl` represent. My focus should be on `zcrx_export` now. I need to understand its role in this whole process and how it relates to the file descriptor and the data being copied to user space. It probably is doing something significant. Let's see what's happening there. |
| 52/3 | 2026/09/15 14:57 | tool | read-file |
0mArgs:{
"File": "io_uring/zcrx.c",
"FirstLine": 790,
"LineCount": 35
}Results: {
"Contents": " 790:\t\t.owner\t\t= THIS_MODULE,\n 791:\t\t.release\t= zcrx_box_release,\n 792:\t};\n 793:\t\n 794:\tstatic int zcrx_export(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq,\n 795:\t\t\t struct zcrx_ctrl *ctrl, void __user *arg)\n 796:\t{\n 797:\t\tstruct zcrx_ctrl_export *ce = \u0026ctrl-\u003ezc_export;\n 798:\t\n 799:\t\tif (!mem_is_zero(ce, sizeof(*ce)))\n 800:\t\t\treturn -EINVAL;\n 801:\t\n 802:\t\trefcount_inc(\u0026ifq-\u003erefs);\n 803:\t\trefcount_inc(\u0026ifq-\u003euser_refs);\n 804:\t\n 805:\t\tFD_PREPARE(fdf, O_CLOEXEC,\n 806:\t\t\t anon_inode_create_getfile(\"[zcrx]\", \u0026zcrx_box_fops, ifq,\n 807:\t\t\t\t\t\t O_CLOEXEC, NULL));\n 808:\t\tif (IS_ERR(fdf)) {\n 809:\t\t\tzcrx_unregister(ifq, NULL);\n 810:\t\t\treturn PTR_ERR(fdf);\n 811:\t\t}\n 812:\t\n 813:\t\tce-\u003ezcrx_fd = fd_prepare_fd(fdf);\n 814:\t\tif (copy_to_user(arg, ctrl, sizeof(*ctrl)))\n 815:\t\t\treturn -EFAULT;\n 816:\t\n 817:\t\treturn 0;\n 818:\t}\n 819:\t\n 820:\tstatic int import_zcrx(struct io_ring_ctx *ctx,\n 821:\t\t\t struct io_uring_zcrx_ifq_reg __user *arg,\n 822:\t\t\t struct io_uring_zcrx_ifq_reg *reg)\n 823:\t{\n 824:\t\tstruct io_zcrx_ifq *ifq;\n"
} |
| 53/3 | 2026/09/15 14:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 80888 output: 34 thoughts: 29 Thoughts: Here's my analysis of that snippet: **Debugging a Potential Call Chain** Okay, so `ctrl` was *copied* from the user by `zcrx_ctrl`. Hmm, that's interesting and probably worth keeping in mind. What does that imply in this context? I don't see any other information so I need to know where I can go next. I'm now thinking I want to figure out where `zcrx_export` is called, because that seems like the next logical step and this would reveal useful information about the flow and the use of `ctrl`. I need to figure out the **caller** of `zcrx_export`. If I can trace back where `zcrx_export` is invoked, I can understand how `ctrl`, having been copied by `zcrx_ctrl`, is actually being *used*. This should give me some context about what's going on. |
| 54/3 | 2026/09/15 14:57 | tool | grepper |
0mArgs:{
"Expression": "zcrx_export",
"PathPrefix": "io_uring/zcrx.c"
}Results: {
"Output": "io_uring/zcrx.c=789=static const struct file_operations zcrx_box_fops = {\n--\nio_uring/zcrx.c-793-\nio_uring/zcrx.c:794:static int zcrx_export(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq,\nio_uring/zcrx.c-795-\t\t struct zcrx_ctrl *ctrl, void __user *arg)\n--\nio_uring/zcrx.c=1584=int io_zcrx_ctrl(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)\n--\nio_uring/zcrx.c-1606-\tcase ZCRX_CTRL_EXPORT:\nio_uring/zcrx.c:1607:\t\treturn zcrx_export(ctx, zcrx, \u0026ctrl, arg);\nio_uring/zcrx.c-1608-\tcase ZCRX_CTRL_ARM_EVENT:\n"
} |
| 55/3 | 2026/09/15 14:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 81422 output: 38 thoughts: 34 Thoughts: Here's a summary of my mental process: **Diving into io_uring's ZCRX Handling** Okay, so I'm digging into `io_uring` and specifically the ZCRX functionality. Time to start with the source code, as always. Let's see... I'm going to jump straight into `io_uring/zcrx.c`, and I'm targeting roughly line 1590. That's usually a good starting point for understanding a core piece of this function. Let's see what interesting things are happening there. I expect a critical part of the implementation. |
| 56/3 | 2026/09/15 14:57 | tool | read-file |
0mArgs:{
"File": "io_uring/zcrx.c",
"FirstLine": 1584,
"LineCount": 30
}Results: {
"Contents": "1584:\tint io_zcrx_ctrl(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)\n1585:\t{\n1586:\t\tstruct zcrx_ctrl ctrl;\n1587:\t\tstruct io_zcrx_ifq *zcrx;\n1588:\t\n1589:\t\tBUILD_BUG_ON(sizeof(ctrl.zc_export) != sizeof(ctrl.zc_flush));\n1590:\t\tBUILD_BUG_ON(sizeof(ctrl.zc_export) != sizeof(ctrl.zc_arm_event));\n1591:\t\n1592:\t\tif (nr_args)\n1593:\t\t\treturn -EINVAL;\n1594:\t\tif (copy_from_user(\u0026ctrl, arg, sizeof(ctrl)))\n1595:\t\t\treturn -EFAULT;\n1596:\t\tif (!mem_is_zero(\u0026ctrl.__resv, sizeof(ctrl.__resv)))\n1597:\t\t\treturn -EFAULT;\n1598:\t\n1599:\t\tzcrx = xa_load(\u0026ctx-\u003ezcrx_ctxs, ctrl.zcrx_id);\n1600:\t\tif (!zcrx)\n1601:\t\t\treturn -ENXIO;\n1602:\t\n1603:\t\tswitch (ctrl.op) {\n1604:\t\tcase ZCRX_CTRL_FLUSH_RQ:\n1605:\t\t\treturn zcrx_flush_rq(ctx, zcrx, \u0026ctrl);\n1606:\t\tcase ZCRX_CTRL_EXPORT:\n1607:\t\t\treturn zcrx_export(ctx, zcrx, \u0026ctrl, arg);\n1608:\t\tcase ZCRX_CTRL_ARM_EVENT:\n1609:\t\t\treturn zcrx_arm_notif(ctx, zcrx, \u0026ctrl);\n1610:\t\tcase ZCRX_CTRL_ADD_AREA:\n1611:\t\t\treturn zcrx_ctrl_add_area(ctx, zcrx, \u0026ctrl);\n1612:\t\t}\n1613:\t\n"
} |
| 57/3 | 2026/09/15 14:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 82170 output: 32 thoughts: 292 Thoughts: **Analyzing the Live Update IOCTL Call Flow** Okay, so I've got this `copy_from_user` call setting up the `ctrl` structure, presumably containing the arguments for the IOCTL. Now, I need to understand what happens to that data. Let's dig into `kernel/liveupdate/luo_core.c` and `kernel/liveupdate/luo_session.c` to see how these arguments are processed, specifically focusing on `luo_ioctl_create_session`, `luo_ioctl_retrieve_session`, and `luo_session_retrieve_fd`. Starting with `luo_ioctl_create_session`, I see the core logic: a `liveupdate_ioctl_create_session` structure is pulled from `ucmd->cmd`. Hmm, `ucmd->cmd` is the caller's buffer. The code then creates a file descriptor using `fd_prepare` with `O_CLOEXEC` – good, ensures the file descriptor isn't inherited by subsequent `exec` calls. A new session is created with `luo_session_create`, and a file is returned, which is then staged using `fd_stage`. The newly created file descriptor (`argp->fd`) is returned to the user through `luo_ucmd_respond`. This is crucial. It seems `argp` contains the file descriptor. I need to understand how the caller is using this file descriptor. Also, need to see where `ucmd->cmd` is coming from, and then how this `luo_ucmd_respond` is returning the result to the caller. Is there a race condition? Maybe. Essentially, a file is created representing the new session, and a file descriptor to that file is given to the caller. That's a classic IPC pattern. |
| 58/3 | 2026/09/15 14:57 | tool | grepper |
0mArgs:{
"Expression": "luo_ucmd_respond",
"PathPrefix": "kernel/liveupdate/"
}Results: {
"Output": "kernel/liveupdate/luo_core.c=270=static int luo_ioctl_create_session(struct luo_ucmd *ucmd)\n--\nkernel/liveupdate/luo_core.c-286-\nkernel/liveupdate/luo_core.c:287:\treturn luo_ucmd_respond(ucmd, sizeof(*argp));\nkernel/liveupdate/luo_core.c-288-}\n--\nkernel/liveupdate/luo_core.c=290=static int luo_ioctl_retrieve_session(struct luo_ucmd *ucmd)\n--\nkernel/liveupdate/luo_core.c-306-\nkernel/liveupdate/luo_core.c:307:\treturn luo_ucmd_respond(ucmd, sizeof(*argp));\nkernel/liveupdate/luo_core.c-308-}\n--\nkernel/liveupdate/luo_internal.h=15=struct luo_ucmd {\n--\nkernel/liveupdate/luo_internal.h-20-\nkernel/liveupdate/luo_internal.h:21:static inline int luo_ucmd_respond(struct luo_ucmd *ucmd,\nkernel/liveupdate/luo_internal.h-22-\t\t\t\t size_t kernel_cmd_size)\n--\nkernel/liveupdate/luo_session.c=263=static int luo_session_preserve_fd(struct luo_session *session,\n--\nkernel/liveupdate/luo_session.c-273-\nkernel/liveupdate/luo_session.c:274:\terr = luo_ucmd_respond(ucmd, sizeof(*argp));\nkernel/liveupdate/luo_session.c-275-\tif (err)\n--\nkernel/liveupdate/luo_session.c=281=static int luo_session_retrieve_fd(struct luo_session *session,\n--\nkernel/liveupdate/luo_session.c-300-\nkernel/liveupdate/luo_session.c:301:\treturn luo_ucmd_respond(ucmd, sizeof(*argp));\nkernel/liveupdate/luo_session.c-302-}\n--\nkernel/liveupdate/luo_session.c=304=static int luo_session_finish(struct luo_session *session,\n--\nkernel/liveupdate/luo_session.c-316-\nkernel/liveupdate/luo_session.c:317:\treturn luo_ucmd_respond(ucmd, sizeof(*argp));\nkernel/liveupdate/luo_session.c-318-}\n--\nkernel/liveupdate/luo_session.c=320=static int luo_session_get_name(struct luo_session *session,\n--\nkernel/liveupdate/luo_session.c-329-\nkernel/liveupdate/luo_session.c:330:\treturn luo_ucmd_respond(ucmd, sizeof(*argp));\nkernel/liveupdate/luo_session.c-331-}\n"
} |
| 59/3 | 2026/09/15 14:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 83660 output: 38 thoughts: 27 Thoughts: Here's a summary of my thought process, as if I were the one thinking it: **Diving into the Luo Response** Okay, time to get into the nitty-gritty of the live update mechanism. I need to understand how the system responds to a `luo_ucmd`. Let's start by digging into the `luo_ucmd_respond` function. Specifically, I'll be looking in `kernel/liveupdate/luo_internal.h`. That header file is usually where these internal function definitions are kept, and it's where the heart of the Luo response implementation likely lives. This is where the magic happens, so I'll be meticulously examining the code to understand the control flow and data structures involved. Let's see what secrets this section of the codebase holds. |
| 60/3 | 2026/09/15 14:57 | tool | read-file |
0mArgs:{
"File": "kernel/liveupdate/luo_internal.h",
"FirstLine": 15,
"LineCount": 25
}Results: {
"Contents": " 15:\tstruct luo_ucmd {\n 16:\t\tvoid __user *ubuffer;\n 17:\t\tu32 user_size;\n 18:\t\tvoid *cmd;\n 19:\t};\n 20:\t\n 21:\tstatic inline int luo_ucmd_respond(struct luo_ucmd *ucmd,\n 22:\t\t\t\t\t size_t kernel_cmd_size)\n 23:\t{\n 24:\t\t/*\n 25:\t\t * Copy the minimum of what the user provided and what we actually\n 26:\t\t * have.\n 27:\t\t */\n 28:\t\tif (copy_to_user(ucmd-\u003eubuffer, ucmd-\u003ecmd,\n 29:\t\t\t\t min_t(size_t, ucmd-\u003euser_size, kernel_cmd_size))) {\n 30:\t\t\treturn -EFAULT;\n 31:\t\t}\n 32:\t\treturn 0;\n 33:\t}\n 34:\t\n 35:\t/*\n 36:\t * Handles a deserialization failure: devices and memory is in unpredictable\n 37:\t * state.\n 38:\t *\n 39:\t * Continuing the boot process after a failure is dangerous because it could\n"
} |
| 61/3 | 2026/09/15 14:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 84219 output: 31 thoughts: 14 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Tracing the Origin of `ucmd->cmd`** Okay, so I need to track down the source of `ucmd->cmd`. This is likely a key data structure or command string, so understanding where it's populated is crucial. I'm going to need to dig into the relevant code, potentially stepping through the execution flow if static analysis isn't immediately revealing. My initial focus will be on finding where the `ucmd` structure is allocated and then populated. I'll be looking for assignments to the `cmd` member, likely within functions related to command parsing, input handling, or perhaps even configuration loading. Is it passed in as an argument to a function call or loaded from config or user input? I need to quickly identify the points of initialization and modification. I'll need to check for any potential buffer overflows or security vulnerabilities while I'm at it, because this seems like an important and possibly security-critical location in the code. I'll look for memory management issues that could lead to vulnerabilities down the line. Finally, once I've got a handle on how this `cmd` string is used, I should be able to assess potential security implications and ensure the integrity of any associated operations. |
| 62/3 | 2026/09/15 14:57 | tool | grepper |
0mArgs:{
"Expression": "struct luo_ucmd",
"PathPrefix": "kernel/liveupdate/"
}Results: {
"Output": "kernel/liveupdate/luo_core.c=265=struct luo_device_state {\n--\nkernel/liveupdate/luo_core.c-269-\nkernel/liveupdate/luo_core.c:270:static int luo_ioctl_create_session(struct luo_ucmd *ucmd)\nkernel/liveupdate/luo_core.c-271-{\n--\nkernel/liveupdate/luo_core.c-289-\nkernel/liveupdate/luo_core.c:290:static int luo_ioctl_retrieve_session(struct luo_ucmd *ucmd)\nkernel/liveupdate/luo_core.c-291-{\n--\nkernel/liveupdate/luo_core.c=343=struct luo_ioctl_op {\n--\nkernel/liveupdate/luo_core.c-346-\tunsigned int ioctl_num;\nkernel/liveupdate/luo_core.c:347:\tint (*execute)(struct luo_ucmd *ucmd);\nkernel/liveupdate/luo_core.c-348-};\n--\nkernel/liveupdate/luo_core.c=367=static long luo_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)\n--\nkernel/liveupdate/luo_core.c-369-\tconst struct luo_ioctl_op *op;\nkernel/liveupdate/luo_core.c:370:\tstruct luo_ucmd ucmd = {};\nkernel/liveupdate/luo_core.c-371-\tunion ucmd_buffer buf;\n--\nkernel/liveupdate/luo_internal.h-14-\nkernel/liveupdate/luo_internal.h:15:struct luo_ucmd {\nkernel/liveupdate/luo_internal.h-16-\tvoid __user *ubuffer;\n--\nkernel/liveupdate/luo_internal.h-20-\nkernel/liveupdate/luo_internal.h:21:static inline int luo_ucmd_respond(struct luo_ucmd *ucmd,\nkernel/liveupdate/luo_internal.h-22-\t\t\t\t size_t kernel_cmd_size)\n--\nkernel/liveupdate/luo_session.c=263=static int luo_session_preserve_fd(struct luo_session *session,\nkernel/liveupdate/luo_session.c:264:\t\t\t\t struct luo_ucmd *ucmd)\nkernel/liveupdate/luo_session.c-265-{\n--\nkernel/liveupdate/luo_session.c=281=static int luo_session_retrieve_fd(struct luo_session *session,\nkernel/liveupdate/luo_session.c:282:\t\t\t\t struct luo_ucmd *ucmd)\nkernel/liveupdate/luo_session.c-283-{\n--\nkernel/liveupdate/luo_session.c=304=static int luo_session_finish(struct luo_session *session,\nkernel/liveupdate/luo_session.c:305:\t\t\t struct luo_ucmd *ucmd)\nkernel/liveupdate/luo_session.c-306-{\n--\nkernel/liveupdate/luo_session.c=320=static int luo_session_get_name(struct luo_session *session,\nkernel/liveupdate/luo_session.c:321:\t\t\t\tstruct luo_ucmd *ucmd)\nkernel/liveupdate/luo_session.c-322-{\n--\nkernel/liveupdate/luo_session.c=347=struct luo_ioctl_op {\n--\nkernel/liveupdate/luo_session.c-351-\tenum luo_ioctl_type type;\nkernel/liveupdate/luo_session.c:352:\tint (*execute)(struct luo_session *session, struct luo_ucmd *ucmd);\nkernel/liveupdate/luo_session.c-353-};\n--\nkernel/liveupdate/luo_session.c=394=static long luo_session_ioctl(struct file *filep, unsigned int cmd,\n--\nkernel/liveupdate/luo_session.c-398-\tconst struct luo_ioctl_op *op;\nkernel/liveupdate/luo_session.c:399:\tstruct luo_ucmd ucmd = {};\nkernel/liveupdate/luo_session.c-400-\tunion ucmd_buffer buf;\n"
} |
| 63/3 | 2026/09/15 14:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 85664 output: 39 thoughts: 43 Thoughts: Here's my summary, as if I were thinking these things: **Analyzing `buf` and `kernel/liveupdate/luo_core.c`** Okay, so I'm looking at this code and trying to figure out what's happening with this buffer, `buf`. The immediate question is, where does it come from? It looks like it's either copied from user space, which would be a potential security vector, or it's being initialized with `memset`, which is usually safer, but still needs to be examined. Let me zoom in on that. To get a better handle on this, I really need to examine the context of how `buf` is being used and where it's being sourced. The best bet is to check lines 365 through 385 in `kernel/liveupdate/luo_core.c`. Hopefully, those lines will shed some light on the source and purpose of the `buf` variable, which will help me understand its implications. |
| 64/3 | 2026/09/15 14:57 | tool | read-file |
0mArgs:{
"File": "kernel/liveupdate/luo_core.c",
"FirstLine": 365,
"LineCount": 25
}Results: {
"Contents": " 365:\t};\n 366:\t\n 367:\tstatic long luo_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)\n 368:\t{\n 369:\t\tconst struct luo_ioctl_op *op;\n 370:\t\tstruct luo_ucmd ucmd = {};\n 371:\t\tunion ucmd_buffer buf;\n 372:\t\tunsigned int nr;\n 373:\t\tint err;\n 374:\t\n 375:\t\tnr = _IOC_NR(cmd);\n 376:\t\tif (nr - LIVEUPDATE_CMD_BASE \u003e= ARRAY_SIZE(luo_ioctl_ops))\n 377:\t\t\treturn -EINVAL;\n 378:\t\n 379:\t\tucmd.ubuffer = (void __user *)arg;\n 380:\t\terr = get_user(ucmd.user_size, (u32 __user *)ucmd.ubuffer);\n 381:\t\tif (err)\n 382:\t\t\treturn err;\n 383:\t\n 384:\t\top = \u0026luo_ioctl_ops[nr - LIVEUPDATE_CMD_BASE];\n 385:\t\tif (op-\u003eioctl_num != cmd)\n 386:\t\t\treturn -ENOIOCTLCMD;\n 387:\t\tif (ucmd.user_size \u003c op-\u003emin_size)\n 388:\t\t\treturn -EINVAL;\n 389:\t\n"
} |
| 65/3 | 2026/09/15 14:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 86342 output: 39 thoughts: 24 Thoughts: Okay, let's dive into this `luo_ioctl` function, specifically lines 388 through 410. I need to understand its role within the larger context. This is likely a critical section for controlling the IO operations, and as an expert, I need to get the architecture right. My experience suggests that this is where the low-level interactions with the underlying hardware, or the kernel, are being managed. I'll need to dissect each line with precision, looking for potential areas of concern: locking strategies, error handling, command decoding, and data validation. The specific device this is interacting with will be the key to understanding the specific code. What assumptions are being made about the data format being passed in? What security considerations have been accounted for? A well-written `ioctl` function is essential for a robust system, and this segment appears to be crucial. |
| 66/3 | 2026/09/15 14:57 | tool | read-file |
0mArgs:{
"File": "kernel/liveupdate/luo_core.c",
"FirstLine": 388,
"LineCount": 25
}Results: {
"Contents": " 388:\t\t\treturn -EINVAL;\n 389:\t\n 390:\t\tucmd.cmd = \u0026buf;\n 391:\t\terr = copy_struct_from_user(ucmd.cmd, op-\u003esize, ucmd.ubuffer,\n 392:\t\t\t\t\t ucmd.user_size);\n 393:\t\tif (err)\n 394:\t\t\treturn err;\n 395:\t\n 396:\t\treturn op-\u003eexecute(\u0026ucmd);\n 397:\t}\n 398:\t\n 399:\tstatic const struct file_operations luo_fops = {\n 400:\t\t.owner\t\t= THIS_MODULE,\n 401:\t\t.open\t\t= luo_open,\n 402:\t\t.release\t= luo_release,\n 403:\t\t.unlocked_ioctl\t= luo_ioctl,\n 404:\t};\n 405:\t\n 406:\tstatic struct luo_device_state luo_dev = {\n 407:\t\t.miscdev = {\n 408:\t\t\t.minor = MISC_DYNAMIC_MINOR,\n 409:\t\t\t.name = \"liveupdate\",\n 410:\t\t\t.fops = \u0026luo_fops,\n 411:\t\t},\n 412:\t\t.in_use = ATOMIC_INIT(0),\n"
} |
| 67/3 | 2026/09/15 14:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 86951 output: 386 thoughts: 2214 Thoughts: **KMSAN Assessment: A Thorough Review** Okay, so I've been dissecting this patch and its potential impact on kernel security, specifically concerning uninitialized memory and the need for KMSAN. The core change here is the re-architecting of file descriptor (fd) management, deferring allocation and linking of the fd to the file until syscall exit. This avoids the problems of leaked or dangling descriptors if an error occurs mid-syscall. I've carefully examined how `fd_prepare()` and `fd_stage()` fit into various subsystems and drivers – etnaviv, msm, vmwgfx, virtgpu, DRM, BPF, userfaultfd, perf, seccomp, namespaces, events, spufs, gntdev, kvm, even memfd. It seems pervasive, but the key is understanding how this affects uninitialized memory access. Initially, I had a knee-jerk, I need to check every single user of this. But now the patch is better and there are only two functions affected: `fd_prepare_fd()` and `fd_stage()`. I've gone through the four KMSAN criteria. Does this patch introduce: 1. Kernel structures sent to user space with uninitialized fields? No, the fd is prepared first, and then the copy to user is safe. 2. Conditional logic depending on uninitialized data? No. `current->fd_slots` is initialized and slots are explicitly initialized before use. 3. Allocation of complex data structures with uninitialized fields? No. `fd_slots` fields are explicitly assigned. 4. Bounds checks that can expose uninitialized bytes? No. The patch itself doesn't directly introduce or expose uninitialized memory reads or information leaks to userspace. All these look like no. It's safe. It's all initialized before it's passed. Now, what *are* the risks? Well, there are: * Locking issues: Incorrect acquisition or dropping of locks during syscall exit. LOCKDEP will catch that. * Architecture bugs: Register corruption across the syscall exit, which results in CPU crashes and will be caught during standard testing. * Reference counting / UAF: Double-freeing or using `struct file` after freeing, double installs or incorrect drops of file descriptors. KASAN is my friend here. * io\_uring and fork/exec invariants: There's nothing that is unsafe here. So, while there are *risks*, they're not the "uninitialized memory" kind that KMSAN is designed to catch. Standard KASAN, LOCKDEP, and general testing should cover these. KMSAN would be redundant here, and KMSAN is expensive in resources. I've stepped back, reviewed, and double-checked the reasoning. **NeedsKMSAN = false.** The patch does not require a dedicated KMSAN session. My reasoning is thorough. I'll now prepare the KMSANReasoning documentation that articulates these points. It will contrast KASAN's vs KMSAN's applicability and detail the nature of the patch, and the analysis of any uninitialized memory risks and contrast it with KASAN, LOCKDEP and other standard bug detectors. All is clear. Time to call `set-results`. |
| 68/3 | 2026/09/15 14:58 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series refactors file descriptor reservation and installation across syscalls, io_uring, and drivers by deferring publication to syscall exit time via `fd_prepare()`, `fd_stage()`, and `fd_slots_commit()`.\n\nFrom a KMSAN perspective:\n1. `task_struct::fd_slots` is explicitly zeroed on task duplication via `memset(\u0026tsk-\u003efd_slots, 0, sizeof(tsk-\u003efd_slots))` in `dup_task_struct()`.\n2. When recording a slot in `fd_slot_record()`, both fields of `struct fd_slot` (`fd` and `file`) are unconditionally initialized before `slots-\u003enr` is incremented. All loops in `fd_slots_install()`, `fd_slots_drop()`, and `fd_slot_find()` strictly iterate over indices `0` to `slots-\u003enr - 1`, ensuring uninitialized spill buffer elements are never read.\n3. No kernel-to-user memory info-leaks are introduced. The data copied to userspace in the converted call sites (such as ioctl output arguments or `put_user` for fd numbers) consists of explicitly initialized scalar descriptors or structs that were either zeroed or populated from `copy_from_user()`.\n4. The risks introduced by this patch relate to descriptor/file reference counting (e.g. potential double free or use-after-free on error paths), locking (`files-\u003efile_lock`), and architecture-specific syscall exit register state. These hazards are addressed by KASAN, LOCKDEP, and standard kernel debugging facilities, not KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series refactors file descriptor reservation and installation across syscalls, io_uring, and drivers by deferring publication to syscall exit time via `fd_prepare()`, `fd_stage()`, and `fd_slots_commit()`.\n\nFrom a KMSAN perspective:\n1. `task_struct::fd_slots` is explicitly zeroed on task duplication via `memset(\u0026tsk-\u003efd_slots, 0, sizeof(tsk-\u003efd_slots))` in `dup_task_struct()`.\n2. When recording a slot in `fd_slot_record()`, both fields of `struct fd_slot` (`fd` and `file`) are unconditionally initialized before `slots-\u003enr` is incremented. All loops in `fd_slots_install()`, `fd_slots_drop()`, and `fd_slot_find()` strictly iterate over indices `0` to `slots-\u003enr - 1`, ensuring uninitialized spill buffer elements are never read.\n3. No kernel-to-user memory info-leaks are introduced. The data copied to userspace in the converted call sites (such as ioctl output arguments or `put_user` for fd numbers) consists of explicitly initialized scalar descriptors or structs that were either zeroed or populated from `copy_from_user()`.\n4. The risks introduced by this patch relate to descriptor/file reference counting (e.g. potential double free or use-after-free on error paths), locking (`files-\u003efile_lock`), and architecture-specific syscall exit register state. These hazards are addressed by KASAN, LOCKDEP, and standard kernel debugging facilities, not KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|