From patchwork Wed Oct 30 16:54:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13856887 Received: from mail-io1-f51.google.com (mail-io1-f51.google.com [209.85.166.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79064216DF4 for ; Wed, 30 Oct 2024 16:56:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730307365; cv=none; b=GBeFpG9XJM6abs81kOPe4PGxM+m/SWHy9U789fRJQOjwv+YKNi0y3nnzeW7xZacrUJrtqTg5KzcM/CYz4fKWsG39s4dlmgRffPnCxTRF6aQUG/1eZ0Rd9p0o8BTwxMeqar11ywhPQBYIkptGwAT1TExkLpkgUgWAsj3gLtWGnQQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730307365; c=relaxed/simple; bh=lcGdlJ+3vLsvMeLLoZ9QUn/wXitDr6CVaNPwpzL4eIc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rTsQ6hE87XZr3Eciv2+eYv0CvwknJ9eZz7aeo70oWu7+lb+SAWT0ps0YmlQLW/l5lTK+4qKS+E4ifeEF/LI4IUP1T/bGxNSzi2adAaLUc6IsDq2DzNBNFVxVrMvjz2nRMPGsFWNThEwYUTGQYimsHbTB358Te+Kft6kzL9gE/JU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=sjInozAr; arc=none smtp.client-ip=209.85.166.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="sjInozAr" Received: by mail-io1-f51.google.com with SMTP id ca18e2360f4ac-83abe7fc77eso496939f.0 for ; Wed, 30 Oct 2024 09:56:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1730307361; x=1730912161; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=blxrd0FPf8Rf/M17PamX1CjchvMtlhApoYVKVRAkJZM=; b=sjInozArN3HC2B26UWDcVg6kCZw7+vWiHXxeTRyEcSCXkQIGW3s+h37YCiQ/XyNZ25 JuvpRdPVjITQhZ5XxLhJlsop5/ITNEr/mMkoDqAlgjEKJHsPjWOUaTOrU7mqDOAw5Gw4 cB/r3OLAyVBK+Mzd3NX7Iq61IZYGPwPeqkoo7UG9OHfc2/yuMF+JGnuvJeFqAu2Mgc7F QIvoBDGEq0n/gQ0gUf6Zucr3Jc44NaxYfx+VkcspLA9TdnGwA5Bkzb5nhA1ZAAiLHPJB e4p9xSmSHnuJ4VjRafGDS9sUy8JpUown5ISfacsjMTJ+qZ29V5u5WKy40RGz6PpmwbbU ltvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730307361; x=1730912161; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=blxrd0FPf8Rf/M17PamX1CjchvMtlhApoYVKVRAkJZM=; b=NHksszIpjLavcIosCn5cYZEEA1Y+doUf4ur4grC9SbgyiuTG7BP+CvyZ9z8DFTvEZj iacojkPChkNXc5qzKBl3gSX7DYDGr8RghL0Nc8Bqy2N+MsazSogqK7IuNk+NL3mj4B8X 7PsuwuFTvtmU0MmL0F24yJkKevIRdtZVE8OowvBUyfcdFre7OtWicm6UlNjAQuLI3ia3 Tzx9iviY+sxOpjId5MqoqzBieg4qwx0eEHA7/KZR4JP+5gqiDzA79grFhljG+LusqvNc JPmbtUTDjlk+lVbhBBdmMsDln9fqHQwJ6TBnuFxeM9oPR+WqdLaxnDch2kti2OSxnCkG 7txg== X-Gm-Message-State: AOJu0YyECIfUf5zKlDaz73hCyeVnVbFHdkGY1TJmTK84YMtnuZUEnhKL PZXjbci1GtrKHWKfMx43QIFaDgZ0QM+1BkADDxisOB01SUYoqD55UA3dncVRbL2pCGj9yivIy5Z cUjo= X-Google-Smtp-Source: AGHT+IFNx87notWmFat7BB2ag4wPbQrXYjFoBbrRzLi6WLQVYiVypcpFMrXJDdS81ZnrkScJotjqvw== X-Received: by 2002:a05:6602:1603:b0:83a:f443:875 with SMTP id ca18e2360f4ac-83b1c5cd7bdmr1123032839f.15.1730307360896; Wed, 30 Oct 2024 09:56:00 -0700 (PDT) Received: from localhost.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4dc727505fdsm2980035173.120.2024.10.30.09.55.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Oct 2024 09:55:59 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 1/2] io_uring/rsrc: allow cloning at an offset Date: Wed, 30 Oct 2024 10:54:14 -0600 Message-ID: <20241030165556.64918-2-axboe@kernel.dk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241030165556.64918-1-axboe@kernel.dk> References: <20241030165556.64918-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Right now buffer cloning is an all-or-nothing kind of thing - either the whole table is cloned from a source to a destination ring, or nothing at all. However, it's not always desired to clone the whole thing. Allow for the application to specify a source and destination offset, and a number of buffers to clone. If the destination offset is non-zero, then allocate sparse nodes upfront. Signed-off-by: Jens Axboe --- include/uapi/linux/io_uring.h | 5 ++++- io_uring/rsrc.c | 36 +++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index 024745283783..cc8dbe78c126 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -719,7 +719,10 @@ enum { struct io_uring_clone_buffers { __u32 src_fd; __u32 flags; - __u32 pad[6]; + __u32 src_off; + __u32 dst_off; + __u32 nr; + __u32 pad[3]; }; struct io_uring_buf { diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index af60d9f597be..4c149dc42fd7 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -924,10 +924,11 @@ int io_import_fixed(int ddir, struct iov_iter *iter, return 0; } -static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctx) +static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctx, + struct io_uring_clone_buffers *arg) { + int i, ret, nbufs, off, nr; struct io_rsrc_data data; - int i, ret, nbufs; /* * Drop our own lock here. We'll setup the data we need and reference @@ -940,11 +941,33 @@ static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctx nbufs = src_ctx->buf_table.nr; if (!nbufs) goto out_unlock; - ret = io_rsrc_data_alloc(&data, nbufs); + ret = -EINVAL; + if (!arg->nr) + arg->nr = nbufs; + else if (arg->nr > nbufs) + goto out_unlock; + ret = -EOVERFLOW; + if (check_add_overflow(arg->nr, arg->src_off, &off)) + goto out_unlock; + if (off > nbufs) + goto out_unlock; + if (check_add_overflow(arg->nr, arg->dst_off, &off)) + goto out_unlock; + ret = -EINVAL; + if (off > IORING_MAX_REG_BUFFERS) + goto out_unlock; + ret = io_rsrc_data_alloc(&data, off); if (ret) goto out_unlock; - for (i = 0; i < nbufs; i++) { + /* fill empty/sparse nodes, if needed */ + for (i = 0; i < arg->dst_off; i++) + data.nodes[i] = rsrc_empty_node; + + off = arg->dst_off; + i = arg->src_off; + nr = arg->nr; + while (nr--) { struct io_rsrc_node *dst_node, *src_node; src_node = io_rsrc_node_lookup(&src_ctx->buf_table, i); @@ -960,7 +983,8 @@ static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctx refcount_inc(&src_node->buf->refs); dst_node->buf = src_node->buf; } - data.nodes[i] = dst_node; + data.nodes[off++] = dst_node; + i++; } /* Have a ref on the bufs now, drop src lock and re-grab our own lock */ @@ -1015,7 +1039,7 @@ int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg) file = io_uring_register_get_file(buf.src_fd, registered_src); if (IS_ERR(file)) return PTR_ERR(file); - ret = io_clone_buffers(ctx, file->private_data); + ret = io_clone_buffers(ctx, file->private_data, &buf); if (!registered_src) fput(file); return ret; From patchwork Wed Oct 30 16:54:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13856888 Received: from mail-io1-f54.google.com (mail-io1-f54.google.com [209.85.166.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E7CF9217903 for ; Wed, 30 Oct 2024 16:56:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730307366; cv=none; b=eGepnNAG8D4FdXL0dNAb2KYLHGh8udZF32709IsxmF11sylzH5adVgsCB1+J1WaRgXLoPqczi9lXX5ziFZz5rPDqZ1Fu/KHDOIDZG8+Bmr+Jv3Ihu32f4/YxlSb5i2KCs4uUjKLiko3HOt+Mv5nmGMVSp9n4t1aPqUQ+x7WbfOk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730307366; c=relaxed/simple; bh=OM6XGvWvIT4znwOiWvt6RGkPUZDnayJ6RUCn6Wp0rvI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sd4Cd+0G+0DWXxEn2iykERUi6nU4CrO5ZMgRJc+pTB2AYzCgXKjZFtMLtdUjqp+VfFcaQ0fZT/AK84DFDs3kEMNArAbqNMRuLfj6M07P0fLGEcYdVdg1GbTloR88Xx/cZ6SylI12TWdRTi+SvuhlX9u4KsCnHgNpukb/8j9UR4w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=HzziUcGR; arc=none smtp.client-ip=209.85.166.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="HzziUcGR" Received: by mail-io1-f54.google.com with SMTP id ca18e2360f4ac-83aa3ced341so259594239f.0 for ; Wed, 30 Oct 2024 09:56:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1730307362; x=1730912162; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=r8oicw4JG1QlM3BuTkCfyvAy/EKiuoKsDBMw093ebqQ=; b=HzziUcGRgf9/m9pZTgDzbqFdmNPmkZ43OfOwAxV7oecRTHJuU+rxj8phWJClSHgHx7 4mdMxA2bqhsq+PYIMLmo3vM7uYqjVUkDCapgLJCNh315cLrnKzkY0w50JgzgQgdSGozK JLoVwHjwOHYDA8CZu/uvcdH1xo02e6++RbZ9O++rh6S6nA+b93mAr00lPha4fT/qTTgt qMhR0+NvxWGHK+KQUT/Gxg8j1I756g0Hd0sBXbRg5+iy9QN8JefNL0eAZXwcyVa5wwFH efzzuEBpHW7AcST78pbXH9VRNi4Adgs74d9fKKbRswMvC2SLgyxyvOcOwE1BQ7MFs3ZG R7Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730307362; x=1730912162; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=r8oicw4JG1QlM3BuTkCfyvAy/EKiuoKsDBMw093ebqQ=; b=r6lJnOypS/5VdTzSNi4OZkQ49W9A+G+cn6NWbHbGvEHRPOF8aWvUv1OY81SmBcWE3D 3vKEVeZ7iV9+DqLaxSebPW4RoQ1YeAo09+CMlZ6RP4pg2rm/COM7qdN9BUpGUA/cCw01 Qf2OgF6Qg3/hoHGtNWfQRfWKEGXBPq2PAtHqJ0LE6RsODvcJ/l2AL3CkR/KQNl15iHsX zQVPyrs/i1t1JTLsGyK6sku4w00tVnQX92+nt9pc2xG9ru4jzpFt0lnlHFQVf2vj3L+i AUfl914B+oxTmyqOpn12NOVK2/4qlz9/QNscV+NeHHl2yMRibg6+X//d8lCry8t0DVQN wjug== X-Gm-Message-State: AOJu0YyKilV8dIl0ivkfl0xe+pN3Obm0qXR6QK4xMqN6KBZjYkUaxhb5 m+kUFbwg9lePL8dHMLks5x2UrfF5PxUSThNtfQN44MBlcFwC8ir9pO6ED0RRMp2Y7DWfGSoFE2+ eaSg= X-Google-Smtp-Source: AGHT+IEhPViuRXPoSPJGuYWOWBfVLZsG1ySCxWaRN7rOSj+ZoyslBamofPTbtBQzAFQ4wXVx2JxFAg== X-Received: by 2002:a05:6602:2d95:b0:832:40d0:902a with SMTP id ca18e2360f4ac-83b64fb5fbdmr31938239f.6.1730307362484; Wed, 30 Oct 2024 09:56:02 -0700 (PDT) Received: from localhost.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4dc727505fdsm2980035173.120.2024.10.30.09.56.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Oct 2024 09:56:01 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 2/2] io_uring/rsrc: allow cloning with node replacements Date: Wed, 30 Oct 2024 10:54:15 -0600 Message-ID: <20241030165556.64918-3-axboe@kernel.dk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241030165556.64918-1-axboe@kernel.dk> References: <20241030165556.64918-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently cloning a buffer table will fail if the destination already has a table. But it should be possible to use it to replace existing elements. Add a IORING_REGISTER_DST_REPLACE cloning flag, which if set, will allow the destination to already having a buffer table. If that is the case, then entries designated by offset + nr buffers will be replaced if they already exist. Note that it's allowed to use IORING_REGISTER_DST_REPLACE and not have an existing table, in which case it'll work just like not having the flag set and an empty table - it'll just assign the newly created table for that case. Signed-off-by: Jens Axboe --- include/uapi/linux/io_uring.h | 3 ++- io_uring/rsrc.c | 24 ++++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index cc8dbe78c126..ce58c4590de6 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -713,7 +713,8 @@ struct io_uring_clock_register { }; enum { - IORING_REGISTER_SRC_REGISTERED = 1, + IORING_REGISTER_SRC_REGISTERED = (1U << 0), + IORING_REGISTER_DST_REPLACE = (1U << 1), }; struct io_uring_clone_buffers { diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index 4c149dc42fd7..9829c51105ed 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -990,9 +990,25 @@ static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctx /* Have a ref on the bufs now, drop src lock and re-grab our own lock */ mutex_unlock(&src_ctx->uring_lock); mutex_lock(&ctx->uring_lock); - if (!ctx->buf_table.nr) { + + /* + * Not replacing, or replacing an empty table. Just install the + * new table. + */ + if (!(arg->flags & IORING_REGISTER_DST_REPLACE) || !ctx->buf_table.nr) { ctx->buf_table = data; return 0; + } else if (arg->flags & IORING_REGISTER_DST_REPLACE) { + /* put nodes in overlapping spots, if any */ + for (i = arg->src_off; i < arg->nr; i++) { + if (data.nodes[i] == rsrc_empty_node) + continue; + io_reset_rsrc_node(&ctx->buf_table, i); + ctx->buf_table.nodes[i] = data.nodes[i]; + data.nodes[i] = NULL; + } + io_rsrc_data_free(&data); + return 0; } mutex_unlock(&ctx->uring_lock); @@ -1026,12 +1042,12 @@ int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg) struct file *file; int ret; - if (ctx->buf_table.nr) - return -EBUSY; if (copy_from_user(&buf, arg, sizeof(buf))) return -EFAULT; - if (buf.flags & ~IORING_REGISTER_SRC_REGISTERED) + if (buf.flags & ~(IORING_REGISTER_SRC_REGISTERED|IORING_REGISTER_DST_REPLACE)) return -EINVAL; + if (!(buf.flags & IORING_REGISTER_DST_REPLACE) && ctx->buf_table.nr) + return -EBUSY; if (memchr_inv(buf.pad, 0, sizeof(buf.pad))) return -EINVAL;