From patchwork Fri May 19 07:40:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13247757 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF091C77B7F for ; Fri, 19 May 2023 07:41:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 34836900008; Fri, 19 May 2023 03:41:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F6F2900003; Fri, 19 May 2023 03:41:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 14B89900008; Fri, 19 May 2023 03:41:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id F1D64900003 for ; Fri, 19 May 2023 03:41:17 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B9D8516099D for ; Fri, 19 May 2023 07:41:17 +0000 (UTC) X-FDA: 80806208994.22.9E203FA Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf30.hostedemail.com (Postfix) with ESMTP id F282480016 for ; Fri, 19 May 2023 07:41:15 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CQGiE8Uq; spf=pass (imf30.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684482076; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HJSls9joKnzamhDgZoRi/AKfts16br7tyBuA30GIBac=; b=QY2C8juFDPW/wwMQj/0SmeqX+mkOdSvMY4c3sZPaAKrEpm0CPwI7ExaizuEEOdLII5cTN+ e4uT7pkzhyrZsNSYWymqf+Om8LJpWj6kMap7BLtAfTfp+oKcn3udrtWJ6CvxuNTHPfdDWr ut0ttZwXmSearc2Rn3UNv6vKB+L6UtE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684482076; a=rsa-sha256; cv=none; b=MhAq850JpKPd+Ar57yENIoJJxMXXbnEV8cDeP5abS4Hq6BCJ6rwWEfL+m/TnbkuFHYnpK5 1VT+/0KtRCjMcl0S/udFelNR7udhfqI/06BKKjEaUy1zmoKPjfYqBZ5R+cOTLtNCbdUO/R 8wF49A7axzkCapj5DN/Ff6uDIaWhwH0= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CQGiE8Uq; spf=pass (imf30.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684482075; 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=HJSls9joKnzamhDgZoRi/AKfts16br7tyBuA30GIBac=; b=CQGiE8UqMr9jcRe9axTcrKq+cI+7GSi7q5/skysxPFw52ESeth/625+JuWqnqOJxrkkg4S hmWVZXpEjb+7lWC3KweMUhAW8p4nKXUmnLzoPLgzRD+hkOkO585x4Pv7SdxdOfV0E1027i /aPCjFG1WUdC//9qYbgV/TiByRKaY8I= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-662-unilBiGRMYCJnhOgY90jPw-1; Fri, 19 May 2023 03:41:13 -0400 X-MC-Unique: unilBiGRMYCJnhOgY90jPw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B0BCD29AB3E8; Fri, 19 May 2023 07:41:12 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.221]) by smtp.corp.redhat.com (Postfix) with ESMTP id 894042166B28; Fri, 19 May 2023 07:41:10 +0000 (UTC) From: David Howells To: Jens Axboe , Al Viro , Christoph Hellwig Cc: David Howells , Matthew Wilcox , Jan Kara , Jeff Layton , David Hildenbrand , Jason Gunthorpe , Logan Gunthorpe , Hillf Danton , Christian Brauner , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Christoph Hellwig Subject: [PATCH v20 03/32] splice: Make direct_read_splice() limit to eof where appropriate Date: Fri, 19 May 2023 08:40:18 +0100 Message-Id: <20230519074047.1739879-4-dhowells@redhat.com> In-Reply-To: <20230519074047.1739879-1-dhowells@redhat.com> References: <20230519074047.1739879-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Rspamd-Queue-Id: F282480016 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: qeq9456euhxg35ydsd4g5wpnazhqrsao X-HE-Tag: 1684482075-726476 X-HE-Meta: U2FsdGVkX1+fV6VgP8gby3OZ07C8jntPmwFsdz1qrQpQmzEY5VtWpQJ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bH7VbeHO3gLAUs7pkzzbtAiW2xz+FzsAFp9CeYZrNMeRVIhI4Nj1vcf2+bYp36fRc8dZZpsjZoKcFmTYodP3VOOYEi4eTwhgKZafdtoOtdbKSNjZbmudkITbh1+psX6BY5yxkOulSBGYxZwSSMRz8t5sZh7cKUi83ONY+nIPrgayPasXGHLJOfcYaJpWwHDp1jSYszFIMIs8SbcjRVF1dEHc1p/5ZFtOtjqrnxGjWiq3vKgrPErnvRUyLbqvbaXrabLJprPc2km3QaoA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Make direct_read_splice() limit the read to the end of the file for regular files and block devices, thereby reducing the amount of allocation it will do in such a case. This means that the blockdev code doesn't require any special handling as filemap_read_splice() also limits to i_size. Signed-off-by: David Howells cc: Christoph Hellwig cc: Al Viro cc: Jens Axboe cc: linux-block@vger.kernel.org cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org --- fs/splice.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fs/splice.c b/fs/splice.c index 4db3eee49423..89c8516554d1 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -315,6 +315,19 @@ ssize_t direct_splice_read(struct file *in, loff_t *ppos, size_t used, npages, chunk, remain, keep = 0; int i; + if (!len) + return 0; + + if (S_ISREG(file_inode(in)->i_mode) || + S_ISBLK(file_inode(in)->i_mode)) { + loff_t i_size = i_size_read(in->f_mapping->host); + + if (*ppos >= i_size) + return 0; + if (len > i_size - *ppos) + len = i_size - *ppos; + } + /* Work out how much data we can actually add into the pipe */ used = pipe_occupancy(pipe->head, pipe->tail); npages = max_t(ssize_t, pipe->max_usage - used, 0);