Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>,
	Jann Horn <jannh@google.com>,  Jan Kara <jack@suse.cz>,
	Ingo Molnar <mingo@redhat.com>,
	 Peter Zijlstra <peterz@infradead.org>,
	linux-fsdevel@vger.kernel.org,  linux-kernel@vger.kernel.org,
	linux-mm@kvack.org,  Oleg Nesterov <oleg@redhat.com>,
	linux-alpha@vger.kernel.org,  linux-snps-arc@lists.infradead.org,
	linux-arm-kernel@lists.infradead.org,
	 linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org,
	 linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org,
	 linux-openrisc@vger.kernel.org, linux-parisc@vger.kernel.org,
	 linux-sh@vger.kernel.org, sparclinux@vger.kernel.org,
	 linux-um@lists.infradead.org, Jens Axboe <axboe@kernel.dk>,
	 io-uring@vger.kernel.org, netdev@vger.kernel.org,
	 linuxppc-dev@lists.ozlabs.org, linux-gpio@vger.kernel.org,
	 linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org,
	 bpf@vger.kernel.org, David Airlie <airlied@redhat.com>,
	 virtualization@lists.linux.dev, kvm@vger.kernel.org,
	 kexec@lists.infradead.org, linux-hyperv@vger.kernel.org,
	 "Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH RFC POC 26/50] dma-buf: stop unwinding sync file descriptors by hand
Date: Tue, 15 Sep 2026 13:31:12 +0200	[thread overview]
Message-ID: <20260915-work-fd-reserve-unify-folded-v1-26-4d5217d6b246@kernel.org> (raw)
In-Reply-To: <20260915-work-fd-reserve-unify-folded-v1-0-4d5217d6b246@kernel.org>

Rely on the fd exit path machinery.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 drivers/dma-buf/dma-buf.c   | 33 ++++++++++------------------
 drivers/dma-buf/sw_sync.c   | 40 +++++++++++-----------------------
 drivers/dma-buf/sync_file.c | 52 +++++++++++++--------------------------------
 3 files changed, 38 insertions(+), 87 deletions(-)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index d504c636dc29..f0d581fad7ae 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 8df20b0218a9..90852950e4e3 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 2166bbdf7e4a..316fffc4067e 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,

-- 
2.53.0



  parent reply	other threads:[~2026-09-15 11:34 UTC|newest]

Thread overview: 108+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-15 11:30 [PATCH RFC POC 00/50] file: handle files on syscall exit Christian Brauner
2026-09-15 11:30 ` [PATCH RFC POC 01/50] file: install " Christian Brauner
2026-09-15 11:55   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 02/50] entry: commit fds " Christian Brauner
2026-09-15 11:52   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 03/50] alpha: " Christian Brauner
2026-09-15 11:50   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 04/50] ARC: " Christian Brauner
2026-09-15 11:48   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 05/50] ARM: " Christian Brauner
2026-09-15 11:59   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 06/50] arm64: " Christian Brauner
2026-09-15 11:48   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 07/50] csky: " Christian Brauner
2026-09-15 11:47   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 08/50] hexagon: " Christian Brauner
2026-09-15 11:49   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 09/50] m68k: " Christian Brauner
2026-09-15 11:50   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 10/50] microblaze: " Christian Brauner
2026-09-15 11:48   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 11/50] MIPS: " Christian Brauner
2026-09-15 11:45   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 12/50] nios2: " Christian Brauner
2026-09-15 11:54   ` sashiko-bot
2026-09-15 11:30 ` [PATCH RFC POC 13/50] openrisc: " Christian Brauner
2026-09-15 12:02   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 14/50] parisc: " Christian Brauner
2026-09-15 11:59   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 15/50] sh: " Christian Brauner
2026-09-15 11:55   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 16/50] sparc: " Christian Brauner
2026-09-15 11:57   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 17/50] um: " Christian Brauner
2026-09-15 11:55   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 18/50] xtensa: " Christian Brauner
2026-09-15 12:00   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 19/50] file: require the syscall exit hook from every architecture Christian Brauner
2026-09-15 11:54   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 20/50] file: warn when the descriptor table is unshared with slots Christian Brauner
2026-09-15 12:02   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 21/50] io_uring: commit fds per request Christian Brauner
2026-09-15 12:11   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 22/50] net: install SCM_RIGHTS descriptors when recvmsg() returns Christian Brauner
2026-09-15 12:03   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 23/50] file: open-code receive_fd()'s immediate install Christian Brauner
2026-09-15 12:00   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 24/50] file: make FD_ADD() a standalone " Christian Brauner
2026-09-15 12:01   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 25/50] file: reimplement FD_PREPARE() on the deferred fd_prepare() path Christian Brauner
2026-09-15 12:05   ` sashiko-bot
2026-09-15 11:31 ` Christian Brauner [this message]
2026-09-15 12:05   ` [PATCH RFC POC 26/50] dma-buf: stop unwinding sync file descriptors by hand sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 27/50] drm/amdkfd: stop collecting CRIU dma-buf descriptors for a final install Christian Brauner
2026-09-15 12:04   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 28/50] drm/msm: install the out-fence descriptor when the ioctl returns Christian Brauner
2026-09-15 12:04   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 29/50] drm/virtio: " Christian Brauner
2026-09-15 12:11   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 30/50] drm/vmwgfx: " Christian Brauner
2026-09-15 12:09   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 31/50] vfio: install the migration data " Christian Brauner
2026-09-15 12:07   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 32/50] liveupdate: install the session descriptors " Christian Brauner
2026-09-15 12:09   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 33/50] io_uring/zcrx: install the exported descriptor when the request returns Christian Brauner
2026-09-15 12:09   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 34/50] sctp: install the peeloff descriptor when the syscall returns Christian Brauner
2026-09-15 12:14   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 35/50] ALSA: compress: install the task descriptors when the ioctl returns Christian Brauner
2026-09-15 12:13   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 36/50] nitro_enclaves: install the enclave descriptor " Christian Brauner
2026-09-15 12:13   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 37/50] tpm: vtpm_proxy: install the server " Christian Brauner
2026-09-15 12:14   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 38/50] perf: stop putting the event descriptor back on failure Christian Brauner
2026-09-15 12:12   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 39/50] seccomp: stop putting the listener " Christian Brauner
2026-09-15 12:14   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 40/50] KVM: stop putting descriptors " Christian Brauner
2026-09-15 12:15   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 41/50] KVM: guest_memfd: stop putting the descriptor " Christian Brauner
2026-09-15 12:16   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 42/50] drm: stop unwinding descriptors by hand Christian Brauner
2026-09-15 12:17   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 43/50] drm/amdgpu: stop unwinding the fence descriptor " Christian Brauner
2026-09-15 12:14   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 44/50] drm/etnaviv: install the out-fence descriptor when the ioctl returns Christian Brauner
2026-09-15 12:15   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 45/50] accel/habanalabs: stop putting the dma-buf descriptor back on failure Christian Brauner
2026-09-15 12:18   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 46/50] xen/gntdev-dmabuf: stop putting the " Christian Brauner
2026-09-15 12:21   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 47/50] iio: buffer: install the buffer descriptor when the ioctl returns Christian Brauner
2026-09-15 12:18   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 48/50] misc: fastrpc: install the dma-buf " Christian Brauner
2026-09-15 12:20   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 49/50] iommufd: stop putting descriptors back on failure Christian Brauner
2026-09-15 12:21   ` sashiko-bot
2026-09-15 11:31 ` [PATCH RFC POC 50/50] Drivers: hv: mshv: " Christian Brauner
2026-09-15 12:19   ` sashiko-bot
2026-09-15 16:02 ` [PATCH RFC POC 00/50] file: handle files on syscall exit Linus Torvalds
2026-09-15 22:21   ` Rob Clark
2026-09-15 22:54     ` Linus Torvalds
2026-09-16  7:09   ` Christian Brauner
2026-09-15 17:51 ` Jann Horn
2026-09-15 19:08   ` Linus Torvalds
2026-09-16  9:50     ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260915-work-fd-reserve-unify-folded-v1-26-4d5217d6b246@kernel.org \
    --to=brauner@kernel.org \
    --cc=airlied@redhat.com \
    --cc=axboe@kernel.dk \
    --cc=bpf@vger.kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=io-uring@vger.kernel.org \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=kexec@lists.infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-alpha@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-csky@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-hexagon@vger.kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-m68k@lists.linux-m68k.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-openrisc@vger.kernel.org \
    --cc=linux-parisc@vger.kernel.org \
    --cc=linux-sh@vger.kernel.org \
    --cc=linux-snps-arc@lists.infradead.org \
    --cc=linux-um@lists.infradead.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=mingo@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sparclinux@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox