Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1485563pxb; Fri, 27 Aug 2021 09:52:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzvcFssnYNSRdp1Hq84++Ag/J+r+N3OsqHxw9yxUWqhVWzPYAOkcYc3VwVDJLRX9RzmoQzu X-Received: by 2002:a05:6402:4243:: with SMTP id g3mr10715691edb.85.1630083135801; Fri, 27 Aug 2021 09:52:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630083135; cv=none; d=google.com; s=arc-20160816; b=JpPPR/RlN7sc4VqIfhkcIlXYEJFxwGh4ZEkQ0D9q/9735qm4pnPuvKARra5lGlblOX hTzqB0f1T1HvXA+2T+IxDSZQDGRbCaqbL6uI3rXHpXYG5cNtSCaEFk2li1tppRLgBsAy DqpoUktDtKhNNO//2SjltDQ/OIMuNZ5un704ZYJzqflFsm4icB0RTXx74Q/+Sorjnmdo g+mQ4ODonengfgjtwdAZ5tBYNWX66H2260nG/fSvrSKj02swtRAh4n768B8Ew0lKI2S5 naRvFbfsXVZs6NEBXBpJtRrZpov4hru3UfmIfOC8zY0a4E/BG8RBej03IWpHzo1/cSwG 4gXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4j1OG2p6DTT4+Yhos82eNs9DFoXSsHGHLLWHqfJx7CQ=; b=QY21WzB29yN1fkjdbhLH7XVfC7ZtXiZuMnP9Zb7k9NIFay/jS9wn3hwZCPbeWxIsRh 10FdQ+LxHmgXWPdTnMmLRxr7qOK6bVChv4gyhrTPllrWVTd3BQvTQEBGq1Kk3NFtrqM/ DrrKYWj9t9ERj/YtxmjzgfXqo+uawg0dzCUhFPEfWcvVb64zfK0xgDoerxhmujQOTEyy kyTxe+gdK2q1DrljQhFe4iNxXD0jdvYIlNbXcd1CIPgXGTRUXtQLrifrIuJ0juVSMP7x +3ffWsYRsChsKFDiXUjYzYPBUBZWAfUQkjRJZwpyeI6AT43TuF1gg9FMTbtWS9ojRP09 7Pzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QS0fpHAK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i16si3502397ejw.721.2021.08.27.09.51.52; Fri, 27 Aug 2021 09:52:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QS0fpHAK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S235413AbhH0Qu4 (ORCPT + 99 others); Fri, 27 Aug 2021 12:50:56 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:48735 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234192AbhH0Quz (ORCPT ); Fri, 27 Aug 2021 12:50:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630083005; 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=4j1OG2p6DTT4+Yhos82eNs9DFoXSsHGHLLWHqfJx7CQ=; b=QS0fpHAKoU5Tsni7Ec7qysSEJMO74iRXefY7zBKnrpno/lNwTFuj9AIPFoKeNdYDEl33ea YkeGHYOhmatnHL86ymDoHrm0EhVyab+Fzu7jxfr0JKuuhp29mzf2xQ60F/6jiIB87Zs/wx YU3E8yUxFzDdYj+3dwM1EM8T1sDBuUU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-484-b2zp5C_TM6aSs4E_jURSFw-1; Fri, 27 Aug 2021 12:50:04 -0400 X-MC-Unique: b2zp5C_TM6aSs4E_jURSFw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 369F1C73A1; Fri, 27 Aug 2021 16:50:03 +0000 (UTC) Received: from max.com (unknown [10.40.194.206]) by smtp.corp.redhat.com (Postfix) with ESMTP id 65FDC60C81; Fri, 27 Aug 2021 16:49:52 +0000 (UTC) From: Andreas Gruenbacher To: Linus Torvalds , Alexander Viro , Christoph Hellwig , "Darrick J. Wong" Cc: Jan Kara , Matthew Wilcox , cluster-devel@redhat.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, ocfs2-devel@oss.oracle.com, Andreas Gruenbacher Subject: [PATCH v7 04/19] iov_iter: Turn iov_iter_fault_in_readable into fault_in_iov_iter_readable Date: Fri, 27 Aug 2021 18:49:11 +0200 Message-Id: <20210827164926.1726765-5-agruenba@redhat.com> In-Reply-To: <20210827164926.1726765-1-agruenba@redhat.com> References: <20210827164926.1726765-1-agruenba@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Turn iov_iter_fault_in_readable into a function that returns the number of bytes not faulted in (similar to copy_to_user) instead of returning a non-zero value when any of the requested pages couldn't be faulted in. This supports the existing users that require all pages to be faulted in as well as new users that are happy if any pages can be faulted in at all. Rename iov_iter_fault_in_readable to fault_in_iov_iter_readable to make sure that this change doesn't silently break things. Signed-off-by: Andreas Gruenbacher --- fs/btrfs/file.c | 2 +- fs/f2fs/file.c | 2 +- fs/fuse/file.c | 2 +- fs/iomap/buffered-io.c | 2 +- fs/ntfs/file.c | 2 +- include/linux/uio.h | 2 +- lib/iov_iter.c | 33 +++++++++++++++++++++------------ mm/filemap.c | 2 +- 8 files changed, 28 insertions(+), 19 deletions(-) diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index ee34497500e1..281c77cfe91a 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -1698,7 +1698,7 @@ static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb, * Fault pages before locking them in prepare_pages * to avoid recursive lock */ - if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) { + if (unlikely(fault_in_iov_iter_readable(i, write_bytes))) { ret = -EFAULT; break; } diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 6afd4562335f..b04b6c909a8b 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -4259,7 +4259,7 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) size_t target_size = 0; int err; - if (iov_iter_fault_in_readable(from, iov_iter_count(from))) + if (fault_in_iov_iter_readable(from, iov_iter_count(from))) set_inode_flag(inode, FI_NO_PREALLOC); if ((iocb->ki_flags & IOCB_NOWAIT)) { diff --git a/fs/fuse/file.c b/fs/fuse/file.c index 97f860cfc195..da49ef71dab5 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -1160,7 +1160,7 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia, again: err = -EFAULT; - if (iov_iter_fault_in_readable(ii, bytes)) + if (fault_in_iov_iter_readable(ii, bytes)) break; err = -ENOMEM; diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index 87ccb3438bec..7dc42dd3a724 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -749,7 +749,7 @@ iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data, * same page as we're writing to, without it being marked * up-to-date. */ - if (unlikely(iov_iter_fault_in_readable(i, bytes))) { + if (unlikely(fault_in_iov_iter_readable(i, bytes))) { status = -EFAULT; break; } diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c index ab4f3362466d..a43adeacd930 100644 --- a/fs/ntfs/file.c +++ b/fs/ntfs/file.c @@ -1829,7 +1829,7 @@ static ssize_t ntfs_perform_write(struct file *file, struct iov_iter *i, * pages being swapped out between us bringing them into memory * and doing the actual copying. */ - if (unlikely(iov_iter_fault_in_readable(i, bytes))) { + if (unlikely(fault_in_iov_iter_readable(i, bytes))) { status = -EFAULT; break; } diff --git a/include/linux/uio.h b/include/linux/uio.h index 82c3c3e819e0..12d30246c2e9 100644 --- a/include/linux/uio.h +++ b/include/linux/uio.h @@ -119,7 +119,7 @@ size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes, struct iov_iter *i); void iov_iter_advance(struct iov_iter *i, size_t bytes); void iov_iter_revert(struct iov_iter *i, size_t bytes); -int iov_iter_fault_in_readable(const struct iov_iter *i, size_t bytes); +size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t bytes); size_t iov_iter_single_seg_count(const struct iov_iter *i); size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, struct iov_iter *i); diff --git a/lib/iov_iter.c b/lib/iov_iter.c index 069cedd9d7b4..082ab155496d 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -430,33 +430,42 @@ static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t by } /* + * fault_in_iov_iter_readable - fault in iov iterator for reading + * @i: iterator + * @size: maximum length + * * Fault in one or more iovecs of the given iov_iter, to a maximum length of - * bytes. For each iovec, fault in each page that constitutes the iovec. + * @size. For each iovec, fault in each page that constitutes the iovec. + * + * Returns the number of bytes not faulted in (like copy_to_user() and + * copy_from_user()). * - * Return 0 on success, or non-zero if the memory could not be accessed (i.e. - * because it is an invalid address). + * Always returns 0 for non-userspace iterators. */ -int iov_iter_fault_in_readable(const struct iov_iter *i, size_t bytes) +size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size) { if (iter_is_iovec(i)) { + size_t count = min(size, iov_iter_count(i)); const struct iovec *p; size_t skip; - if (bytes > i->count) - bytes = i->count; - for (p = i->iov, skip = i->iov_offset; bytes; p++, skip = 0) { - size_t len = min(bytes, p->iov_len - skip); + size -= count; + for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) { + size_t len = min(count, p->iov_len - skip); + size_t ret; if (unlikely(!len)) continue; - if (fault_in_readable(p->iov_base + skip, len)) - return -EFAULT; - bytes -= len; + ret = fault_in_readable(p->iov_base + skip, len); + count -= len - ret; + if (ret) + break; } + return count + size; } return 0; } -EXPORT_SYMBOL(iov_iter_fault_in_readable); +EXPORT_SYMBOL(fault_in_iov_iter_readable); void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov, unsigned long nr_segs, diff --git a/mm/filemap.c b/mm/filemap.c index 4dec3bc7752e..83af8a534339 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3643,7 +3643,7 @@ ssize_t generic_perform_write(struct file *file, * same page as we're writing to, without it being marked * up-to-date. */ - if (unlikely(iov_iter_fault_in_readable(i, bytes))) { + if (unlikely(fault_in_iov_iter_readable(i, bytes))) { status = -EFAULT; break; } -- 2.26.3