From patchwork Fri Apr 26 11:15:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13644675 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 B91A5C4345F for ; Fri, 26 Apr 2024 11:15:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E54AF6B0082; Fri, 26 Apr 2024 07:15:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E03766B0083; Fri, 26 Apr 2024 07:15:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF1D16B0087; Fri, 26 Apr 2024 07:15:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B15FF6B0082 for ; Fri, 26 Apr 2024 07:15:27 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 553A1C06F0 for ; Fri, 26 Apr 2024 11:15:27 +0000 (UTC) X-FDA: 82051427094.16.23583EB Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf16.hostedemail.com (Postfix) with ESMTP id A2DC318000C for ; Fri, 26 Apr 2024 11:15:24 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OVcUdTRv; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714130124; 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: references:dkim-signature; bh=V5UQl+A/q/jFNTFxR4sxGrh2z0OS8H/kdmBGvgOenXw=; b=SDt5iSo722F66czyBS989w6itIkk/KHzD8SxN3ECKTrYZU9B4vjwlgLQkxOXiLFWzX0tlc Gxioqc/YPrOAWzNmvNNPZZWnOXsg7TFaMh1t+vrBU1InbCaTI0IFy2IcVfszVbiUCjTi8C 2mGclUrya1nQ446Hz7GDB4V/LBZ9RfQ= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OVcUdTRv; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714130124; a=rsa-sha256; cv=none; b=r/Kb8tGoAw3NgGW9nG69AfUno0BJtlmWcZ6urGnNykoTjliMfjX0AdAMqMziV83jE3uSCj Wlq7YlxAgyLglhHkS8Yk3LRGvwlq1PwVxlDAMv3c4HVGLNPA4W8WvlHZzXHuYXpZpF3B/m pXUZvYWnJcK4jfDgz/0FkXVZAhHeOTM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714130124; 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; bh=V5UQl+A/q/jFNTFxR4sxGrh2z0OS8H/kdmBGvgOenXw=; b=OVcUdTRvW9nzwL58GUMmvloVBezd6td1hVsMpqYORveoRiykwgv6boVRWeU+RpGqq64r9i iJVE+wN6hXmjXIaeuOlTywqFRiIEbGWjKgE8d6gZeWBBS22MvzBCTDedIM1VaNjW7TQnsf iB6M6OxNSyYIrjOZoT8L2RGt97yIQBU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-573-pPepkYvEP3aJNht168TYdQ-1; Fri, 26 Apr 2024 07:15:20 -0400 X-MC-Unique: pPepkYvEP3aJNht168TYdQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9B15918065AD; Fri, 26 Apr 2024 11:15:19 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.200]) by smtp.corp.redhat.com (Postfix) with ESMTP id A0034AC68; Fri, 26 Apr 2024 11:15:16 +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 To: Christian Brauner cc: dhowells@redhat.com, Jeff Layton , Eric Van Hensbergen , Latchesar Ionkov , Dominique Martinet , Christian Schoenebeck , Marc Dionne , netfs@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, v9fs@lists.linux.dev, linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] netfs: Fix the pre-flush when appending to a file in writethrough mode MIME-Version: 1.0 Content-ID: <2150447.1714130115.1@warthog.procyon.org.uk> Date: Fri, 26 Apr 2024 12:15:15 +0100 Message-ID: <2150448.1714130115@warthog.procyon.org.uk> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: A2DC318000C X-Stat-Signature: fpspk1ei4bx4kqz8kn4sammioq71tkw3 X-HE-Tag: 1714130124-507225 X-HE-Meta: U2FsdGVkX18yec/k95KZbqX5c1h+QRtJ6V4TFmSC8ZydeclojRtDINhLbiWHTALRdSnRdIJgAHs5z9Wygo0s0oMjyi8cWIQcS4weSYjuZ5Z0sPzFVH43R3AgucgU6Fd1eHKK5EGTpid/jYlmYgVmTKtHoQcO7ssaMf3dHB7t5S30MV09feHcf4JCzP6tQUqDYReVQokDeMgzyNSktMmvKnTwhw9emGtmNz07qekn4xVxpgv142UpCxMmtZYqGgAqAHVxtjMlijMbzveUKku7cPXqxVCEXClX6L6sJYnAIiY/7ijdNbHc1WmjS/AJmzSn5M/4bdP2/e543hBp38E2R+N7YUUMyMvZyhEkjnMc3L8ibUKyOU/wBEbJj7x/EheokwrORksMEVy9tsQ+lMdpNhYik8M2DcgIbgsatuoN+aIAYznQs3SeqxKnH1PqcmTQPz4gkK2HsDA3G4K0fz9NMAnuincEaiytKP0kE+n5KbL6u737bNda7+cim3JmbyghfeGfFv/j0fP2jEGt5da4dtWNfAqxJpnIjuQx9Sw8RUIU5RYSxrXKPTRer85DJpFl03MKAsaImYDr0SiykFe9dayWbmEwBENg1if+WZuzEJ8kN+/6cmfJUy/79+OgmmSRQMAfiNjO8GnNF8a0dhF18mC3Wsj/eNaRd8QGO8kRRZLr3icEROSDpSTuvonwmjEpPKgLxbnP3Nw3D6yGwL/JNo5GIIeKhJ6Sk0HDXi354DoHifu4rxxqbWjvpuABoUiLmS4G+wNkBIc4c+pt+jeEmkuEq/t48OpljrkClj6g00bsdGeRosS+JXz2/cDaqaRfGtgCdD78OEBudbu4VHbsQ5ewon7l50iy/tuNKd6uGS8= 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: List-Subscribe: List-Unsubscribe: In netfs_perform_write(), when the file is marked NETFS_ICTX_WRITETHROUGH or O_*SYNC or RWF_*SYNC was specified, write-through caching is performed on a buffered file. When setting up for write-through, we flush any conflicting writes in the region and wait for the write to complete, failing if there's a write error to return. The issue arises if we're writing at or above the EOF position because we skip the flush and - more importantly - the wait. This becomes a problem if there's a partial folio at the end of the file that is being written out and we want to make a write to it too. Both the already-running write and the write we start both want to clear the writeback mark, but whoever is second causes a warning looking something like: ------------[ cut here ]------------ R=00000012: folio 11 is not under writeback WARNING: CPU: 34 PID: 654 at fs/netfs/write_collect.c:105 ... CPU: 34 PID: 654 Comm: kworker/u386:27 Tainted: G S ... ... Workqueue: events_unbound netfs_write_collection_worker ... RIP: 0010:netfs_writeback_lookup_folio Fix this by making the flush-and-wait unconditional. It will do nothing if there are no folios in the pagecache and will return quickly if there are no folios in the region specified. Further, move the WBC attachment above the flush call as the flush is going to attach a WBC and detach it again if it is not present - and since we need one anyway we might as well share it. Fixes: 41d8e7673a77 ("netfs: Implement a write-through caching option") Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-lkp/202404161031.468b84f-oliver.sang@intel.com Signed-off-by: David Howells cc: Eric Van Hensbergen cc: Latchesar Ionkov cc: Dominique Martinet cc: Christian Schoenebeck cc: Marc Dionne cc: netfs@lists.linux.dev cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org cc: v9fs@lists.linux.dev cc: linux-afs@lists.infradead.org cc: linux-cifs@vger.kernel.org Reviewed-by: Jeffrey Layton --- fs/netfs/buffered_write.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/fs/netfs/buffered_write.c b/fs/netfs/buffered_write.c index 9a0d32e4b422..07aff231926c 100644 --- a/fs/netfs/buffered_write.c +++ b/fs/netfs/buffered_write.c @@ -172,15 +172,14 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter, if (unlikely(test_bit(NETFS_ICTX_WRITETHROUGH, &ctx->flags) || iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) ) { - if (pos < i_size_read(inode)) { - ret = filemap_write_and_wait_range(mapping, pos, pos + iter->count); - if (ret < 0) { - goto out; - } - } - wbc_attach_fdatawrite_inode(&wbc, mapping->host); + ret = filemap_write_and_wait_range(mapping, pos, pos + iter->count); + if (ret < 0) { + wbc_detach_inode(&wbc); + goto out; + } + wreq = netfs_begin_writethrough(iocb, iter->count); if (IS_ERR(wreq)) { wbc_detach_inode(&wbc);