From patchwork Wed Feb 15 13:42:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13141683 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 B20F0C636D4 for ; Wed, 15 Feb 2023 13:42:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4A09B6B0074; Wed, 15 Feb 2023 08:42:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 44FF36B0075; Wed, 15 Feb 2023 08:42:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3172B6B0078; Wed, 15 Feb 2023 08:42:32 -0500 (EST) 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 226F96B0074 for ; Wed, 15 Feb 2023 08:42:32 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EA81F1A0642 for ; Wed, 15 Feb 2023 13:42:31 +0000 (UTC) X-FDA: 80469640902.02.681FE53 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf16.hostedemail.com (Postfix) with ESMTP id 083E6180003 for ; Wed, 15 Feb 2023 13:42:28 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="Vc/F5VtK"; spf=pass (imf16.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=1676468549; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=T0SsL9tL69e2AfNVunWu+DTWNqFzKgbmUmmJkonx498=; b=MIHRM85SL64L4vCA3tvqhwNgrIYf2SXGhD4M9jlHL11Sg3NPwajiz0q0j02oQKBzRNlwGR 7XJuqrIB/B9pXAyY0j1SDGf8UY0WfG1c0CW7qnyD0TpLMxsNOu7+L3TapRQJuzqEsRgNrn GIni9wi/558j93JfsCLugclI7VogzAs= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="Vc/F5VtK"; spf=pass (imf16.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676468549; a=rsa-sha256; cv=none; b=Qn0DMwzgHE2IkU4vX9sGffb19deWBbUqooN2ITlCBo1YF+hOdxuhG+pMv0TK53cEkHA0V7 5draIqtW8qveW/BvtXxfH/cwXVBz0lPlvzZjrGG16aimItO9tIFdmk1MvTByJVJPG8TVCH oKfYYxBa/P7alnvtt8HXRba4GsBydx0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676468548; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T0SsL9tL69e2AfNVunWu+DTWNqFzKgbmUmmJkonx498=; b=Vc/F5VtKMjo8atDM6msHq0v38BlJa/uEj8nlGz/Qu5kpsxTFM9CvvrvIxJJ2hEkg1uqcqe WKTXYAHjRKfimMHJ3FkjFtAajzool8By1GBHQWYbHwtFLXifVTyRM831lS2Rtr8vismdrE kWVtTT4u1l+I8v7TPOjfivBL2ENvGHU= 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-440-5wCxiagSM1-7Z8llImWl5g-1; Wed, 15 Feb 2023 08:42:25 -0500 X-MC-Unique: 5wCxiagSM1-7Z8llImWl5g-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 E18BD3C0D195; Wed, 15 Feb 2023 13:42:23 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 734682166B30; Wed, 15 Feb 2023 13:42:21 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 From: David Howells In-Reply-To: References: <20230209102954.528942-1-dhowells@redhat.com> <20230209102954.528942-2-dhowells@redhat.com> <909202.1675959337@warthog.procyon.org.uk> To: Christoph Hellwig Cc: dhowells@redhat.com, Matthew Wilcox , Jens Axboe , Al Viro , Jan Kara , Jeff Layton , David Hildenbrand , Jason Gunthorpe , Logan Gunthorpe , Hillf Danton , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, syzbot+a440341a59e3b7142895@syzkaller.appspotmail.com, Christoph Hellwig , John Hubbard Subject: [PATCH] splice: Clean up direct_splice_read() a bit MIME-Version: 1.0 Content-ID: <3113837.1676468540.1@warthog.procyon.org.uk> Date: Wed, 15 Feb 2023 13:42:20 +0000 Message-ID: <3113838.1676468540@warthog.procyon.org.uk> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 99gow9cjqur6mtjnkiff4f88dxm94e8n X-Rspamd-Queue-Id: 083E6180003 X-HE-Tag: 1676468548-795010 X-HE-Meta: U2FsdGVkX19tdab5aQNdF3wVdJLQvZ0U0xQQr7D4faNnsrWz1LHVdq29X90ZXZnWKj9s9erMMD8IfeZ24M9HMk/cK0o6mUcqqyDp2pgL3MqgZl7FisqTfDbiLUfkbRXyWqEwgimuapSEASoq5tv3beEghyiknny+KuSAV7zdZ51VKHlPbTTVpud50uQAFl7eymK1X4WhmbNv162szY9v/Gf17lJT9gXQhHEbNdqljATGV/6g85oYxFDLl5NmV6hPaUqhzZyszgqVqDd4HE/1aFpN4Sg1AJG/LCy5rN+CDUEf0e3aHyMdR4sEl+q+7cbwlouScNtehf0GDMVxsBvwNquKN9tHfA8VHher3hIcO0liqmbfpIU72qHe6AJqmyE3DBT+HJ9F6TcKH7H87I5LD4p2JNmZj0jh9agn5X0QKPwYvw8Ps/NHkvXMoL3vxwxcX1UDkwNzGgHSF8TAEWms5DuG/scFq+qkTHtsj7BUvI9tqn6tJtQmBp9oY6H4G5VBqFLCLmFXG5VRl0WzBOTs2+Tc2dRQ9m9LkK/zjx0mBkh5Q6W0K6bFPP+7HzPRbws45bYrzw3QlvH6edWeKxAzOjlKTv9fQ1aDsREagFu7NuqT6Qh/Ka0i26CcpaMR5jiMpRYA/9pJKWZqnIoTsFxlssLpkQT9PvJhEPkyuBRelmHbSeLUby8wIoAWmgEyik4P2yEzIrq4NdFRk+GrBgtyiwcRkZmA3ptNFkKVAN7oXi3E6CAl8jM3yqyHriPUTh62cVq+gUtjNgrPSYFekn6NUmIXWieXFhuLxyfgONwIHTuWnlfx514xa6nN7T9EPI5FmG+GXpF4bulZJ2flTgMeRCCMGZ8+YObrEQG3W07ofTXv1rzquySyR0qGzW+hRhBEWmxJAnzJS1GbsIOdwvC2n9LSqK1khgbW3V5pRc0Y+MgYo8Jj0MuC7jvow46Qz6jAVLFGqmoMiOXrTITRsVe ZCafJV/I ZlEXe80q/jPYEieszUCRRlXZaKsB2Uh+BLeaVjGtq8G22dAMgf8IzAwafRmBcVkk4JAg/Th6ckFDup8JwjPnQkrvni89z0W79cf2MxFfti+5NyjyyZbFydFBngOkflWZG57kNPrleH/tVyny4sQrr7VQZAV9Ibf/YMqy1izn6qZPelXBsf7wJnMirBAIh3y/32Janh16o8RvteeppdM/aRcSiF1NdJis/tjd5D7BFdt2vQjQwWxVDR/MRqbtWUah0pr9Wy6jW5pzJKFr+Ey7qG8eDqSqBbovUcQGhTXuO7h9lDJc1a85O7gZo58zj+7ecgLH/78lqbLaF43YXZ01haKDUoanukmdJqeW7En/5hfxalBu5XG5fB3jqpaPpFyRfY3/Z1lKtx0KZ3GomY374r2KTCHux8iPUVlz2mB4uGyQiz7VZPZX3sOmcTGtNisFwfRbF9kH0dIoYVw1JUDQ/622ml+Vreb+WkS7lNee4jBM5hmp8wubejyiaPCkTHqhxlRAmV5hxYevKoJ+dKX/T77KPT5Km/gOakz2qoqGfo495aMUa6PNEc1B9AqbWXwKLsoY9SiMlJDRskDfE288PB5BxJjMDWdHNepmd 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: How about the attached? I won't fold it down for the moment, but it could be pushed along later. David Signed-off-by: David Howells --- splice: Clean up direct_splice_read() a bit 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 cc: 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 diff --git a/fs/splice.c b/fs/splice.c index 9e798c901087..572d3e2a669a 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -295,7 +295,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 */ @@ -332,11 +332,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) { @@ -349,14 +346,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);