Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3728925pxv; Mon, 28 Jun 2021 11:19:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzBA0yYq97XgcFMG5GDYPpYlX7+j2m9eOdlbqO1hzdcRxiTNQem6HDqjq9wc0no8YkgIVKL X-Received: by 2002:a05:6402:358a:: with SMTP id y10mr14165837edc.125.1624904346613; Mon, 28 Jun 2021 11:19:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624904346; cv=none; d=google.com; s=arc-20160816; b=enie9vMRk/mDpuWvYTW4kNdlhHfB7OmyTRf+hHCLICFb2kZ1v465b5askkCGWvMJA7 U4/2xBqPucoBIaMS7bMeZNU6+/cpv7VKMtssO0PVqi4eGtoGOL/1dyYeKbfOx+vdvDQa 45fMN6wVdpK3Di5/uWv77zbrza262eF8ucpqKNTd4pBT353DMXZGBQE8FDTSVVjrJ+Xi vXo6j/aFtqZEguVWDDlGivOxQ0mF8m2OmpqlduksLao9PuYEDMivJxmtnb39jn/tsXZ4 NaVMI5cHrfOXB1JTb7wnup+snPnIqVZf1+BOjxYPPZaCk7Ce0ZK51iE+uGQrrRtxHOsl cerA== 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=jdWr080xmqhNaFr0Ldbrm44I0TPiwUwke9EX64LpyPM=; b=zj2qGKQ3557XXnJFpK80GbsVjDc4rEJuvrp1rJcybwggpAS/Rr9IvKJxdgi5D8u5dG yZwUHCXOl9kKaql8SFS2xQr23bXX35/npRo72OrF6AyOVGxZnxO8gZdqgW02xINlAYZS NEQ2YovajQHJXfZvUUp6xPURMSDXKjGZg3tUStZsMPRfm82BoySVt7Uc3Nbr3vP0w32I i2LOnSEtpsfuaP66e43RZq0pJ+d1KCv4d/O2b/xd8JGyhVHwTx5JZ/Uf5CkgXXL6x8g2 8moZ9DAudMxyUZS2yuqTHTUj8dBhOjaZsHeebgXRLghf1qsKwTrsvwL0Tv//eT95u6un WRJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eFo0Bbwx; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 3si13708252ejr.392.2021.06.28.11.18.42; Mon, 28 Jun 2021 11:19:06 -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=@kernel.org header.s=k20201202 header.b=eFo0Bbwx; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234266AbhF1O3X (ORCPT + 99 others); Mon, 28 Jun 2021 10:29:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:55128 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233050AbhF1OXi (ORCPT ); Mon, 28 Jun 2021 10:23:38 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 96AE161C83; Mon, 28 Jun 2021 14:20:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624890018; bh=QijCrKyhnozrfuGrRc6q4/RO0Eg7RkF09iYWG4/G5Bw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eFo0Bbwxd7CosZD6hMOQTv2qq25v9fbgPz1kb6/1wcnp1z/8SqpH9m/480GkWoxWi R/Aw3XqQn+oyQ3Sf91sGmRmE8Zyq1evFFAEZA6BQXMWp9aXcsdDREWhK7kbGxa9wfT BOgVFEUN84c1jYwSmGWsJLpAzB0KeJHjmCv8xAXFT0ICHqx8WyOHSJPglu9EbZO4xG +w666iHA4M7u5aM/cFsmKLwUM4CKrNlaKnHfJm6zAXnu74qs1cXDrGOEJA/VSTa5MS KfG75Rj4eDMAe+wkfQOHS/3909abRt9zYNRFOsoCe6W1oEzAaGhdWboO2ja8t7Cs+h v/fJKn3Z1ob/w== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jeff Layton , Andrew W Elble , David Howells , Matthew Wilcox , ceph-devel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH 5.12 103/110] netfs: fix test for whether we can skip read when writing beyond EOF Date: Mon, 28 Jun 2021 10:18:21 -0400 Message-Id: <20210628141828.31757-104-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210628141828.31757-1-sashal@kernel.org> References: <20210628141828.31757-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.12.14-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.12.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.12.14-rc1 X-KernelTest-Deadline: 2021-06-30T14:18+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jeff Layton commit 827a746f405d25f79560c7868474aec5aee174e1 upstream. It's not sufficient to skip reading when the pos is beyond the EOF. There may be data at the head of the page that we need to fill in before the write. Add a new helper function that corrects and clarifies the logic of when we can skip reads, and have it only zero out the part of the page that won't have data copied in for the write. Finally, don't set the page Uptodate after zeroing. It's not up to date since the write data won't have been copied in yet. [DH made the following changes: - Prefixed the new function with "netfs_". - Don't call zero_user_segments() for a full-page write. - Altered the beyond-last-page check to avoid a DIV instruction and got rid of then-redundant zero-length file check. ] [ Note: this fix is commit 827a746f405d in mainline kernels. The original bug was in ceph, but got lifted into the fs/netfs library for v5.13. This backport should apply to stable kernels v5.10 though v5.12. ] Fixes: e1b1240c1ff5f ("netfs: Add write_begin helper") Reported-by: Andrew W Elble Signed-off-by: Jeff Layton Signed-off-by: David Howells Reviewed-by: Matthew Wilcox (Oracle) cc: ceph-devel@vger.kernel.org Link: https://lore.kernel.org/r/20210613233345.113565-1-jlayton@kernel.org/ Link: https://lore.kernel.org/r/162367683365.460125.4467036947364047314.stgit@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/162391826758.1173366.11794946719301590013.stgit@warthog.procyon.org.uk/ # v2 Signed-off-by: Greg Kroah-Hartman --- fs/ceph/addr.c | 54 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c index 26e66436f005..c000fe338f7e 100644 --- a/fs/ceph/addr.c +++ b/fs/ceph/addr.c @@ -1302,6 +1302,45 @@ ceph_find_incompatible(struct page *page) return NULL; } +/** + * prep_noread_page - prep a page for writing without reading first + * @page: page being prepared + * @pos: starting position for the write + * @len: length of write + * + * In some cases, write_begin doesn't need to read at all: + * - full page write + * - file is currently zero-length + * - write that lies in a page that is completely beyond EOF + * - write that covers the the page from start to EOF or beyond it + * + * If any of these criteria are met, then zero out the unwritten parts + * of the page and return true. Otherwise, return false. + */ +static bool skip_page_read(struct page *page, loff_t pos, size_t len) +{ + struct inode *inode = page->mapping->host; + loff_t i_size = i_size_read(inode); + size_t offset = offset_in_page(pos); + + /* Full page write */ + if (offset == 0 && len >= PAGE_SIZE) + return true; + + /* pos beyond last page in the file */ + if (pos - offset >= i_size) + goto zero_out; + + /* write that covers the whole page from start to EOF or beyond it */ + if (offset == 0 && (pos + len) >= i_size) + goto zero_out; + + return false; +zero_out: + zero_user_segments(page, 0, offset, offset + len, PAGE_SIZE); + return true; +} + /* * We are only allowed to write into/dirty the page if the page is * clean, or already dirty within the same snap context. @@ -1315,7 +1354,6 @@ static int ceph_write_begin(struct file *file, struct address_space *mapping, struct ceph_snap_context *snapc; struct page *page = NULL; pgoff_t index = pos >> PAGE_SHIFT; - int pos_in_page = pos & ~PAGE_MASK; int r = 0; dout("write_begin file %p inode %p page %p %d~%d\n", file, inode, page, (int)pos, (int)len); @@ -1350,19 +1388,9 @@ static int ceph_write_begin(struct file *file, struct address_space *mapping, break; } - /* - * In some cases we don't need to read at all: - * - full page write - * - write that lies completely beyond EOF - * - write that covers the the page from start to EOF or beyond it - */ - if ((pos_in_page == 0 && len == PAGE_SIZE) || - (pos >= i_size_read(inode)) || - (pos_in_page == 0 && (pos + len) >= i_size_read(inode))) { - zero_user_segments(page, 0, pos_in_page, - pos_in_page + len, PAGE_SIZE); + /* No need to read in some cases */ + if (skip_page_read(page, pos, len)) break; - } /* * We need to read it. If we get back -EINPROGRESS, then the page was -- 2.30.2