From patchwork Thu Nov 3 08:50:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 14763 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp401943wru; Thu, 3 Nov 2022 01:58:44 -0700 (PDT) X-Google-Smtp-Source: AMsMyM63yJXLkbwVjYJQ6Sj+lmcxKUWn4FcImUOvD+rg/kogRCRVWIBLGLsv5t3vPAl+imGvEQGw X-Received: by 2002:a17:906:1282:b0:7ad:f8df:5f98 with SMTP id k2-20020a170906128200b007adf8df5f98mr9304907ejb.627.1667465924525; Thu, 03 Nov 2022 01:58:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667465924; cv=none; d=google.com; s=arc-20160816; b=Ed9emNU5rLv9fsESBu6qBvxAcuzAJRtNOIeFZ+b8K5Jp2n5cDFSVRc3F/6m/I4nNIy h6b1a/lBOCY7z9MPhn1Gaoc3LVO8WWGDr4VRVjaN3XcomIC4rWX0n13vwGd9+kHY6l4H 4261rrW+Ah8bF9OB/hRPfJBCUlxPwQC6vPXkBmPv+3N/UqRLRyC35+I64DXVKYE0SWGk f9Snj6A2eSDSFl/QwKMFtSJN1MaoxtYcrXUDZGRYtXFmtqQpNPTaK6jtndkVsgUUlNoS mpNDEW9RtU96VTeKyKITEl+oqqwAnea/Iq5bCi69H+XOorslzFZ8jU+dzrJHS4Q3rXF+ K+hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=aDA3qRRkp7ROYhYReLuXeczugOI8k0pmRRjImuHFcO0=; b=QfeTpiEAVLFX2WYWBAwYPTPeNs6cyrJ/t/hijniiTBieiTwvp77z+4AZ8jUNf4H5nh Lwyn5TMC35SUtjSlDeJ8H5b6ZVYKHJtiH438iTk76wYQlusL6vFrNjZ0w5uzZgStYuch LTssMl9spQSdn9lxOb2I9US7IGMusbcsfSR7LY0P/TAErr2UUjkZ9TpMOx+HGS9y5yBp lzqiip1IAUW3bO4lfZ/5luwMboQi4cv4JbJSdzcl1QmZ5UeyxlDHfmad3/fIKgWUybjA VkNpCDtau5CUw4y5K32MVNFaDJHcw0bRbyMaFL8udCpzTGhuNWp6L+MU51sjQenM8k8C r//w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IVPOsepP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hb38-20020a170907162600b007307fd1b9bbsi549316ejc.589.2022.11.03.01.58.20; Thu, 03 Nov 2022 01:58:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IVPOsepP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231423AbiKCIvn (ORCPT + 99 others); Thu, 3 Nov 2022 04:51:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231372AbiKCIvf (ORCPT ); Thu, 3 Nov 2022 04:51:35 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75B44271C for ; Thu, 3 Nov 2022 01:50:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667465433; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aDA3qRRkp7ROYhYReLuXeczugOI8k0pmRRjImuHFcO0=; b=IVPOsepPKfM5J5FlvC/veAOdx7mZ9/uChmtGcVBlYxLgbvAIBHfZQ5YDL1cd15S53ffaoE GLTq+p2hznakqX93CSJB2kCgPng93RaHbhjgstVxaWbBohU+kf8iRn30dUffOHR7Tw48xN WyVk/sGBQCT1GTmT7lnHYrloG4s9AYc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-652-7Pg4MZE_O6GN3gizwamFpg-1; Thu, 03 Nov 2022 04:50:29 -0400 X-MC-Unique: 7Pg4MZE_O6GN3gizwamFpg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ECD5A811E75; Thu, 3 Nov 2022 08:50:28 +0000 (UTC) Received: from localhost (ovpn-8-20.pek2.redhat.com [10.72.8.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 086592084836; Thu, 3 Nov 2022 08:50:27 +0000 (UTC) From: Ming Lei To: Jens Axboe , io-uring@vger.kernel.org Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Miklos Szeredi , Stefan Hajnoczi , ZiyangZhang , Ming Lei Subject: [RFC PATCH 1/4] io_uring/splice: support do_splice_direct Date: Thu, 3 Nov 2022 16:50:01 +0800 Message-Id: <20221103085004.1029763-2-ming.lei@redhat.com> In-Reply-To: <20221103085004.1029763-1-ming.lei@redhat.com> References: <20221103085004.1029763-1-ming.lei@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748464748853279956?= X-GMAIL-MSGID: =?utf-8?q?1748464748853279956?= do_splice_direct() has at least two advantages: 1) the extra pipe isn't required from user viewpoint, so userspace code can be simplified, meantime easy to relax current pipe limit since curret->splice_pipe is used for direct splice 2) in some situation, it isn't good to expose file data via ->splice_read() to userspace, such as the coming ublk driver's zero copy support, request pages will be spliced to pipe for supporting zero copy, and if it is READ, userspace may read data of kernel pages, and direct splice can avoid this kind of info leaks Signed-off-by: Ming Lei --- fs/read_write.c | 5 +++-- include/linux/splice.h | 3 +++ io_uring/splice.c | 13 ++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/fs/read_write.c b/fs/read_write.c index 328ce8cf9a85..98869d15e884 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -1253,7 +1253,7 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, goto fput_out; file_start_write(out.file); retval = do_splice_direct(in.file, &pos, out.file, &out_pos, - count, fl); + count, fl | SPLICE_F_DIRECT); file_end_write(out.file); } else { if (out.file->f_flags & O_NONBLOCK) @@ -1389,7 +1389,8 @@ ssize_t generic_copy_file_range(struct file *file_in, loff_t pos_in, size_t len, unsigned int flags) { return do_splice_direct(file_in, &pos_in, file_out, &pos_out, - len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0); + len > MAX_RW_COUNT ? MAX_RW_COUNT : len, + SPLICE_F_DIRECT); } EXPORT_SYMBOL(generic_copy_file_range); diff --git a/include/linux/splice.h b/include/linux/splice.h index a55179fd60fc..9121624ad198 100644 --- a/include/linux/splice.h +++ b/include/linux/splice.h @@ -23,6 +23,9 @@ #define SPLICE_F_ALL (SPLICE_F_MOVE|SPLICE_F_NONBLOCK|SPLICE_F_MORE|SPLICE_F_GIFT) +/* used for io_uring interface only */ +#define SPLICE_F_DIRECT (0x10) /* direct splice and user needn't provide pipe */ + /* * Passed to the actors */ diff --git a/io_uring/splice.c b/io_uring/splice.c index 53e4232d0866..c11ea4cd1c7e 100644 --- a/io_uring/splice.c +++ b/io_uring/splice.c @@ -27,7 +27,8 @@ static int __io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) { struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice); - unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; + unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL | + SPLICE_F_DIRECT; sp->len = READ_ONCE(sqe->len); sp->flags = READ_ONCE(sqe->splice_flags); @@ -109,8 +110,14 @@ int io_splice(struct io_kiocb *req, unsigned int issue_flags) poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; - if (sp->len) - ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); + if (sp->len) { + if (flags & SPLICE_F_DIRECT) + ret = do_splice_direct(in, poff_in, out, poff_out, + sp->len, flags); + else + ret = do_splice(in, poff_in, out, poff_out, sp->len, + flags); + } if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) io_put_file(in);