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: 57567 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp202592wrn; Wed, 15 Feb 2023 05:55:10 -0800 (PST) X-Google-Smtp-Source: AK7set87xIGZ/qgSRCNT5bA3UfC2S084dSdQkBv8vrbA0IKblLpnx1e7VQRxYq01r+6VDCvK3DPH X-Received: by 2002:a50:fc11:0:b0:4ac:b4f2:ce41 with SMTP id i17-20020a50fc11000000b004acb4f2ce41mr2374668edr.8.1676469310022; Wed, 15 Feb 2023 05:55:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676469310; cv=none; d=google.com; s=arc-20160816; b=bu76LQ5/Ypw+Ca9/InG/dOtm0+3J8fnbe6iVsylWUmOVY/YIiirZG6d5As8eFRBpHi yKed243nQR65mpFF8GPr0F1YddPzH+VEO5sf2t5SKz6UWRf4ABPa3cD79RGT0f/5lEpS TUV64Q494lHaItfFx7p89kUyAzBu0kgV1FUzSFYO9dT8l53dJXoq+SKOSI8cg/tkWM8O gGedrIR37ccjHUa4n/SHo4k7gYvRSrMv33mSVllqaIUKALtibjOj0/agAagjAl3/T44e LvcdOVbLjVfHizSq4d/ccCaWmwbjidcW/M3GyfunJuwi0otBjHthlYo9lkztMk5ryDKZ gTFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:content-transfer-encoding :content-id:mime-version:subject:cc:to:references:in-reply-to:from :organization:dkim-signature; bh=T0SsL9tL69e2AfNVunWu+DTWNqFzKgbmUmmJkonx498=; b=xNFVNbZYKqVmRExBzCaaEFtzOACctB371STygZBlSwiBIfw+EMSNKCjV2ipuJEICfZ cGrdMeTYMgLnUB/WVdQC3OXxpHz0ZldYIJohBoo3xTfYq2EOyZrCwQwxces7xvJKHkpq znY0LwxyfwKau9mfwknken1SivkqeIJWS49ESYdzcdPJP1VANwRCY0T4Ew11QDW3BXqT bMt+6Bw8UcgmwhteA0kYaV4X0pqeUC6v+p7IisFDm+9/mHz6ZyWXuklpp1opR7kilPOk K/CCg60kQ8zPEqX29FU7xP28CJEvbsQjhE1hqgg1BMlV9TwUTgU7RBhMKg9dQmJ7NrjD ReTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=d3EAN6F3; 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 bq6-20020a056402214600b0049b935e07e4si14091076edb.207.2023.02.15.05.54.46; Wed, 15 Feb 2023 05:55:10 -0800 (PST) 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=d3EAN6F3; 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 S229595AbjBONnz (ORCPT + 99 others); Wed, 15 Feb 2023 08:43:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229485AbjBONnx (ORCPT ); Wed, 15 Feb 2023 08:43:53 -0500 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 B04BA39CC1 for ; Wed, 15 Feb 2023 05:42:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676468549; 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=d3EAN6F3YjsAkjZWdiqOmk1lxkH2Jg+u5wX7PFku9HC0H5mGORXsbQCs3yIKcnyO5dZvVi Ujmv6fwJXiy0Gxj46J3xpLocQMEmFrIV+6w2nFUQvvA1vnmbprPkAP+X0Zzaf9I1fCv+9p 9cJCdZThDnATBYrl9pUWtC2/+i72R4s= 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-Spam-Status: No, score=-2.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?1757905469986427765?= X-GMAIL-MSGID: =?utf-8?q?1757905483291493397?= How about the attached? I won't fold it down for the moment, but it could be pushed along later. David --- 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);