From patchwork Fri May 19 07:40:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13247759 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 2D435C7EE2A for ; Fri, 19 May 2023 07:41:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BABD790000A; Fri, 19 May 2023 03:41:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B3502900003; Fri, 19 May 2023 03:41:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9AEE590000A; Fri, 19 May 2023 03:41:24 -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 84CDA900003 for ; Fri, 19 May 2023 03:41:24 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5B70EA09E8 for ; Fri, 19 May 2023 07:41:24 +0000 (UTC) X-FDA: 80806209288.08.A695F9B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf12.hostedemail.com (Postfix) with ESMTP id A43D540013 for ; Fri, 19 May 2023 07:41:22 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bke55Y4t; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf12.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684482082; 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=sXlwBEApq0PjRnmqqIdGIYb9yjH9GX5fVKdD8XUANt8=; b=h6nLmoexVtBWLWymO90X0sgtxnspidDBbzbICWHF8jFTYPKDw80r3UAbPPzNOPA5GBQ3Oi bjNbLf3Ck1EblzyKIfaNEOxUMEqqzZQ2f1XuPOwU5Fx+Ngi3vtDXkqGZO0TsehoD3SBfF/ cfwZxQE7IiUninxGUvUkhFyay3DQ+hQ= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bke55Y4t; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf12.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684482082; a=rsa-sha256; cv=none; b=pZBp2eUaVj8+sQpIePnpv7qYG+NO7MWdFmrnudHM5+15GaDba1oRDrQhRKMB1gyDoifULO Vcohls+hsVOskNnkMXKsoIN9BwkvxX9xBqTsarmFWOk74gghZ0X84qdZ2+X4wIxhoVKARQ LjPwKkbUm+4VFP+dy62kNY9L2PxfKv4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684482082; 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=sXlwBEApq0PjRnmqqIdGIYb9yjH9GX5fVKdD8XUANt8=; b=bke55Y4tkS5A7duHl1U8gjo/oH/lcZCoMhyPHkoSVYX+flsjJn6u93M5oPB4cqI1DiLorx a4lA+zjjEByjiyTlR8DubY+GGL3nR8UWb9ES2mqd0YU5YOwzJ1Eegn5FMp8Vyzxb4LLYQh dv/4yuUZ6k0KL2a7UAEXyQtD4llCa2Y= 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-1-IyiQq-xAM9m13EaOaEApEQ-1; Fri, 19 May 2023 03:41:10 -0400 X-MC-Unique: IyiQq-xAM9m13EaOaEApEQ-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 E69E5380450F; Fri, 19 May 2023 07:41:09 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.221]) by smtp.corp.redhat.com (Postfix) with ESMTP id B1B6F2166B25; Fri, 19 May 2023 07:41:07 +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 , John Hubbard Subject: [PATCH v20 02/32] splice: Clean up direct_splice_read() a bit Date: Fri, 19 May 2023 08:40:17 +0100 Message-Id: <20230519074047.1739879-3-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: A43D540013 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: agx4xpm7c3kxy4ghj9ukhyssakzp7ryg X-HE-Tag: 1684482082-93894 X-HE-Meta: U2FsdGVkX1+9N60a+t7PSS7h/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foPcMaEkwU4nRoCXPiPo9h48x88TPRKumSV6oOVFzDz20zOw2IBPoQqB1zbniF5jJ7Gly8ucDeeBDlVMUxOHpisxi1t1CMYDCYuQEXUQaG9hGsx3DmI1sg7JxBoxO9eKr8F4CFfHh5wG/ZqScmIHLZtdsUQbXRd5JyvoXcxi2DFWxww2GwV1tRr/pzL2qYAzu8myOpIlX0yWpSKARaTAlsTK7kywZpR4s3hGH0saLz2MDr1KBjmGLYNuhR41+lNU5bubHITUjGa+HMkSpMPmEeDuPdRGR2+WspBf2SEb/7TsjnUBZKLkeP9GrZWmV/HJHAgWUbq2ukxX6+aMEqbmd+C12uH4zsBvtsyiBs0jQoSxc+tqkDnDHICO2mdkyF0/d 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: Do a couple of cleanups to direct_splice_read(): (1) Cast to struct page **, not void *. (2) Simplify the calculation of the number of pages to keep/reclaim in direct_splice_read(). Suggested-by: Christoph Hellwig Signed-off-by: David Howells Reviewed-by: Christoph Hellwig cc: Jens Axboe cc: Al Viro cc: David Hildenbrand cc: John Hubbard cc: linux-mm@kvack.org cc: linux-block@vger.kernel.org cc: linux-fsdevel@vger.kernel.org --- fs/splice.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/fs/splice.c b/fs/splice.c index 3e06611d19ae..4db3eee49423 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -312,7 +312,7 @@ ssize_t direct_splice_read(struct file *in, loff_t *ppos, struct kiocb kiocb; struct page **pages; ssize_t ret; - size_t used, npages, chunk, remain, reclaim; + size_t used, npages, chunk, remain, keep = 0; int i; /* Work out how much data we can actually add into the pipe */ @@ -326,7 +326,7 @@ ssize_t direct_splice_read(struct file *in, loff_t *ppos, if (!bv) return -ENOMEM; - pages = (void *)(bv + npages); + pages = (struct page **)(bv + npages); npages = alloc_pages_bulk_array(GFP_USER, npages, pages); if (!npages) { kfree(bv); @@ -349,11 +349,8 @@ ssize_t direct_splice_read(struct file *in, loff_t *ppos, kiocb.ki_pos = *ppos; ret = call_read_iter(in, &kiocb, &to); - reclaim = npages * PAGE_SIZE; - remain = 0; if (ret > 0) { - reclaim -= ret; - remain = ret; + keep = DIV_ROUND_UP(ret, PAGE_SIZE); *ppos = kiocb.ki_pos; file_accessed(in); } else if (ret < 0) { @@ -366,14 +363,12 @@ ssize_t direct_splice_read(struct file *in, loff_t *ppos, } /* Free any pages that didn't get touched at all. */ - reclaim /= PAGE_SIZE; - if (reclaim) { - npages -= reclaim; - release_pages(pages + npages, reclaim); - } + if (keep < npages) + release_pages(pages + keep, npages - keep); /* Push the remaining pages into the pipe. */ - for (i = 0; i < npages; i++) { + remain = ret; + for (i = 0; i < keep; i++) { struct pipe_buffer *buf = pipe_head_buf(pipe); chunk = min_t(size_t, remain, PAGE_SIZE);