From patchwork Wed Mar 15 16:35:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13176335 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 485A0C61DA4 for ; Wed, 15 Mar 2023 16:36:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D3CD16B007D; Wed, 15 Mar 2023 12:36:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CEC9E6B007E; Wed, 15 Mar 2023 12:36:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B66826B0080; Wed, 15 Mar 2023 12:36:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id A5C856B007D for ; Wed, 15 Mar 2023 12:36:12 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7C963C014F for ; Wed, 15 Mar 2023 16:36:12 +0000 (UTC) X-FDA: 80571684984.16.3FF985F 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 B4D1380021 for ; Wed, 15 Mar 2023 16:36:09 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hDdlS7x0; 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=1678898169; 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=imlbxN8+e/h/7fdYpvOZBQo8e/sPfLa+LUB+/4kjN58=; b=qUfHIeJrD2DSr8hndnFG+M96sDN+GFyr7+y6lhAQlcunDbVN6J5aOUBY5TOWrNVAe2CUl1 lpsU+N+sS1b+bUiClRaNP8ZJNalhHddnS8a3h7SLwSYMlRGChelSuel2MDWcbT2xJEE9gt OZitAv4H2/48aJD37EVoPl1IpUlLKTY= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hDdlS7x0; 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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678898169; a=rsa-sha256; cv=none; b=eTMRIM9P48EeeEBk0/8KZD91CH9kILgbfmOm2pBgmBmtZYHb3d4B5PcmCXK0FH7o6YGO3v LKXREhe8rZ+LKLlEASA52KNqGFNAyR9Jxb6oQ4cxFrEbMdJiB0a/serua5RBZKJ/dJKhTf VehP2+wJOPONQ+N1CAUemx6IcG8MS4A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678898169; 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=imlbxN8+e/h/7fdYpvOZBQo8e/sPfLa+LUB+/4kjN58=; b=hDdlS7x0Kx7k9bTPLPOWdC6ADdKBuox3fHHmDLawq2HdACBj4gwu1SqQlA7eGYy0WJhWsL U588eiSoRgFX/k8WDCGkCxRiIVEqSMTu3QU3pyLEdfbOs43puz6oZxkZ59h1GRvJhMXOaA hypMvdrozCJ+OuU97IAaq1A30mXubXA= 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-296-Ki0JtdioOVChXpS3qKCPXQ-1; Wed, 15 Mar 2023 12:36:06 -0400 X-MC-Unique: Ki0JtdioOVChXpS3qKCPXQ-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 2BDF888B7A0; Wed, 15 Mar 2023 16:36:02 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7DE122166B26; Wed, 15 Mar 2023 16:35:59 +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, Daniel Golle , Guenter Roeck , Christoph Hellwig , John Hubbard , Hugh Dickins Subject: [PATCH v19 03/15] shmem: Implement splice-read Date: Wed, 15 Mar 2023 16:35:37 +0000 Message-Id: <20230315163549.295454-4-dhowells@redhat.com> In-Reply-To: <20230315163549.295454-1-dhowells@redhat.com> References: <20230315163549.295454-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Stat-Signature: rw5krwkgey4ey16f18u535iiieg1gp6s X-Rspam-User: X-Rspamd-Queue-Id: B4D1380021 X-Rspamd-Server: rspam06 X-HE-Tag: 1678898169-52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ufScRy3hmN1646+XbtVpL4QtrNW3WBvQ/8bl178LByjMQMbI9pcEs15OUlvHgz6C4hp7OW0zn38F6HsNMc4OoWg8CNznus4tSlCqwDFiSQhelFkK3SKuUNDmZ1ULG9RkdirdsxWKj6zUjdg9nMI+0q5j20bvj9N5DteQVTyeF0uU3VBXIWE4ELchDv4mGqU7NmRbS2GMqpBWqneMBMAjM7qhMS+yhAIaWN9HqvLPZ5N2SVJhqSH3MFsP24KViadfJT8T8nUnDnbqWS0knX6qVIT7AWf16wcZmggeaMa1BdvxuvA7yNLCcwZc7jvn8X5rGG+e86F0NQiYRxPas+LdRysOSVICY7AK1QTechZvzBscr9Q4YqE5NwztzRsnjFFRk1HRDsrs4F5fxFufawbyHjYa513dXKT0DBz55+DZI5hamA0wRSI4QE1emy1rc1jLXWiBtqj+tqag5NHlxllJkjwomJqQsVVSK8Y3IuV8C4JYLDqksA== 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: The new filemap_splice_read() has an implicit expectation via filemap_get_pages() that ->read_folio() exists if ->readahead() doesn't fully populate the pagecache of the file it is reading from[1], potentially leading to a jump to NULL if this doesn't exist. shmem, however, (and by extension, tmpfs, ramfs and rootfs), doesn't have ->read_folio(), Work around this by equipping shmem with its own splice-read implementation, based on filemap_splice_read(), but able to paste in zero_page when there's a page missing. Signed-off-by: David Howells cc: Daniel Golle cc: Guenter Roeck cc: Christoph Hellwig cc: Jens Axboe cc: Al Viro cc: John Hubbard cc: David Hildenbrand cc: Matthew Wilcox cc: Hugh Dickins cc: linux-block@vger.kernel.org cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org Link: https://lore.kernel.org/r/Y+pdHFFTk1TTEBsO@makrotopia.org/ [1] --- Notes: ver #19) - Remove a missed get_page() on the zero page. ver #18) - Don't take/release a ref on the zero page. mm/shmem.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 1 deletion(-) diff --git a/mm/shmem.c b/mm/shmem.c index 448f393d8ab2..a0c268dcf7b8 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2719,6 +2719,138 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) return retval ? retval : error; } +static bool zero_pipe_buf_get(struct pipe_inode_info *pipe, + struct pipe_buffer *buf) +{ + return true; +} + +static void zero_pipe_buf_release(struct pipe_inode_info *pipe, + struct pipe_buffer *buf) +{ +} + +static bool zero_pipe_buf_try_steal(struct pipe_inode_info *pipe, + struct pipe_buffer *buf) +{ + return false; +} + +static const struct pipe_buf_operations zero_pipe_buf_ops = { + .release = zero_pipe_buf_release, + .try_steal = zero_pipe_buf_try_steal, + .get = zero_pipe_buf_get, +}; + +static size_t splice_zeropage_into_pipe(struct pipe_inode_info *pipe, + loff_t fpos, size_t size) +{ + size_t offset = fpos & ~PAGE_MASK; + + size = min_t(size_t, size, PAGE_SIZE - offset); + + if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { + struct pipe_buffer *buf = pipe_head_buf(pipe); + + *buf = (struct pipe_buffer) { + .ops = &zero_pipe_buf_ops, + .page = ZERO_PAGE(0), + .offset = offset, + .len = size, + }; + pipe->head++; + } + + return size; +} + +static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, + struct pipe_inode_info *pipe, + size_t len, unsigned int flags) +{ + struct inode *inode = file_inode(in); + struct address_space *mapping = inode->i_mapping; + struct folio *folio = NULL; + size_t total_spliced = 0, used, npages, n, part; + loff_t isize; + int error = 0; + + /* 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); + len = min_t(size_t, len, npages * PAGE_SIZE); + + do { + if (*ppos >= i_size_read(inode)) + break; + + error = shmem_get_folio(inode, *ppos / PAGE_SIZE, &folio, SGP_READ); + if (error) { + if (error == -EINVAL) + error = 0; + break; + } + if (folio) { + folio_unlock(folio); + + if (folio_test_hwpoison(folio)) { + error = -EIO; + break; + } + } + + /* + * i_size must be checked after we know the pages are Uptodate. + * + * Checking i_size after the check allows us to calculate + * the correct value for "nr", which means the zero-filled + * part of the page is not copied back to userspace (unless + * another truncate extends the file - this is desired though). + */ + isize = i_size_read(inode); + if (unlikely(*ppos >= isize)) + break; + part = min_t(loff_t, isize - *ppos, len); + + if (folio) { + /* + * If users can be writing to this page using arbitrary + * virtual addresses, take care about potential aliasing + * before reading the page on the kernel side. + */ + if (mapping_writably_mapped(mapping)) + flush_dcache_folio(folio); + folio_mark_accessed(folio); + /* + * Ok, we have the page, and it's up-to-date, so we can + * now splice it into the pipe. + */ + n = splice_folio_into_pipe(pipe, folio, *ppos, part); + folio_put(folio); + folio = NULL; + } else { + n = splice_zeropage_into_pipe(pipe, *ppos, len); + } + + if (!n) + break; + len -= n; + total_spliced += n; + *ppos += n; + in->f_ra.prev_pos = *ppos; + if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) + break; + + cond_resched(); + } while (len); + + if (folio) + folio_put(folio); + + file_accessed(in); + return total_spliced ? total_spliced : error; +} + static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) { struct address_space *mapping = file->f_mapping; @@ -3938,7 +4070,7 @@ static const struct file_operations shmem_file_operations = { .read_iter = shmem_file_read_iter, .write_iter = generic_file_write_iter, .fsync = noop_fsync, - .splice_read = generic_file_splice_read, + .splice_read = shmem_file_splice_read, .splice_write = iter_file_splice_write, .fallocate = shmem_fallocate, #endif