From patchwork Tue Jun 25 12:29:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13711048 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 B6F78C30653 for ; Tue, 25 Jun 2024 12:29:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 313206B02ED; Tue, 25 Jun 2024 08:29:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C3AC6B02F9; Tue, 25 Jun 2024 08:29:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 116E76B02FD; Tue, 25 Jun 2024 08:29:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E58A26B02ED for ; Tue, 25 Jun 2024 08:29:17 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 98A641417F9 for ; Tue, 25 Jun 2024 12:29:17 +0000 (UTC) X-FDA: 82269341154.18.50575F3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf26.hostedemail.com (Postfix) with ESMTP id B3A4214000D for ; Tue, 25 Jun 2024 12:29:15 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IJRGFhlr; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf26.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=1719318543; a=rsa-sha256; cv=none; b=OH9TtvK99WhLIxJapOFd9i/hoYPepw/0/s+snfp58ykYzy28lSbgEtOkeKvyCC58hFwISU u09OnJIzWreizGnVYSuh5t2vUq3giki+Aaw0GU/bynz9C9oKdOiih8+rxPXLubL3l9/Rr6 VXn2ByaGyvWNpsJBcwhwTcDlVO7Pb2E= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IJRGFhlr; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf26.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=1719318543; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id: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:dkim-signature; bh=2kf2p5udqDpsF3HW6ICyyWXNK+Uj/DfC2j20t1IouqU=; b=2Ekekh10vgZM9c/MLWwDI94mZa9hhuojRPdNGcTcId8qSiw4RH9Lrn6zQ/hJtwEU2j41vH 8wQKhnv3J4Uhq532o15tpRryTi8+Mz3MFAlxo4n39hzI/AtkGB94EAKCq3BnfEgr/7qXTJ qaPSjgid8MtcPuZ4/JHrIREjGRJ7NgY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719318555; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: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=2kf2p5udqDpsF3HW6ICyyWXNK+Uj/DfC2j20t1IouqU=; b=IJRGFhlriHywG04iVPYXpipLhEXqB0KxE8D+eTpluTV67r3INWk5MUp1vfJIX2FGZhGR9+ UxL07bFKvfRQpBG2MBZyBQzaJRPxpvPd/41S1K32EEs/TuNotjD+qioQxd6IemwaNkXGdx NnVN9G1tolimeW5tvttIuauYc6Rw5jc= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-581-dQ5jPXmyNFuSlLtPhDch-Q-1; Tue, 25 Jun 2024 08:29:13 -0400 X-MC-Unique: dQ5jPXmyNFuSlLtPhDch-Q-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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 mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6C96319560B8; Tue, 25 Jun 2024 12:29:11 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.111]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 466F31956087; Tue, 25 Jun 2024 12:29:08 +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: <614257.1719228181@warthog.procyon.org.uk> References: <614257.1719228181@warthog.procyon.org.uk> Cc: dhowells@redhat.com, Christian Brauner , Matthew Wilcox , Jeff Layton , netfs@lists.linux.dev, v9fs@lists.linux.dev, linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] netfs: Fix netfs_page_mkwrite() to check folio->mapping is valid MIME-Version: 1.0 Content-ID: <780210.1719318546.1@warthog.procyon.org.uk> Date: Tue, 25 Jun 2024 13:29:06 +0100 Message-ID: <780211.1719318546@warthog.procyon.org.uk> X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: B3A4214000D X-Stat-Signature: c96dr1hqr8mkuiwyedmr4cq3pj4y351x X-Rspam-User: X-HE-Tag: 1719318555-737454 X-HE-Meta: U2FsdGVkX19V8n+p1YA0Zxsmu4YGNyGRndHUx0jdSomqEp34kabo0yJnLV438mTRJuN/kGXJjzGZF8gH77umgaMrY/e4hYeez83+NLKsL8EPNhcplwia7kK7CbV/LckaxOH+ScQfv1xgHp3wE93sNcOe+M+GnlGH/r2Ty9AmuSgWBqynA3W9k7nVdoRP1KukWQotli7eEbFZkWjPj/Eo7HUXQW6NEwAztc/IAmBSmnU1a1Td/MYuysPLhSj4vtXvHf60ww1RCg1FoOBoP7n3US8XbnOlFZnpU3KzhE5wlXJ12NgOQgJewha7QwDj1PbNmm+LgqHwK9RuTavrZKvsYY9XRXx6coaT/RZ9xueuSs+GH5PLCGhSMq8FtA9Zum+ZwbNUqi5Q3pAEStd4aO/DwjQnkjDN3a5BvaGPUk9W+o+29npqhGaejiNku7yx53Jjzm7WDhcPvopuQmTOrz3FqPEUdfJ+hdIrmQzmC92GaljOSojHyIRShSRmXsy2iNqy2Zvv3mgZWlzqcSX3W7V76CB+pBCqxpITPBHFExsvLMIEruh87CB1EgJDsS44egyMcb9DIHrYF2qx0oRErS5ZlASryLwJn/UbbPpHZkCw9/cNzLwxrE9k6CCSjVtsYRzHT0PpVRquSqid9Ox5CssqwLLk8/aIQVBinIXSePKscysuRdFsT5e//KlrB+MOLlj3wf1n72nj2JupE7tFJ78K+08QiIAH+Lg8juQ3LD/lj7nx8IDppeYDvEJD4NeRPUfFparBctKF9Oehw5wNC/EthviPxbqCj1YZFxY5OE+1yjI8CjkHmMIoMT3rQLKq2KWiMATSNLjSVxN8vrzkcZ9IDMo3XXLMLH4EJwlPb9W5Dn4Ueg7UhPIrGf3i3ryslXqLw38GLM2vsTKMP6B6LA7nDusyr4KLQKRidWft3wOCN8SG3PTOISEUyoqHPmpOQGa/PChfVD8gZwvJB1DgmiR tm/z9zKI fw0Of+3WXCXSZ1fMS1EzDhrgZsR0BudK+IBD2YsAaO9JVFD6TblWjyJaMwXQdTB/PEIbIbZu9KQLZQpDO/ZIZNwQZocq1YUzyteareNaoQ0KgL/PaBmh7Dg/Y20PlguDw7ozk3WTA3MljyJl/oLh5MTpGgZ66WmnluJaYcmVzhRaKcoIvExNrYdyUJhw8euLcmOCiG14dgMm+5/9rXQwMqRPY9nexZCPiAM2YkaLlTtr6WDS79yy39zmBAIxcOI7sZjiKEoB2KzVoFTsI2sI9Fj4GiMw/YCep5ugv7ggl/iXjsmUQ+F4EokQZQVxN9SxELDghyaTjiyVnBEnGH3cXt9FbrJI6rBrjrvyk8FupSwCl3OHHbQrlBT2+t4TO9RRswrmW/juBFHTmxBi9q3jC3uG8olm8jRMacP4Z0u0CvfdrkugkYyI4Pr8J566NmTjvajGLn/xj1HcxGZDpZVeXnlceeqMaOMQ47j4vfXYHV5vEHrvvVi/xo8g6/553RehEGpg1XwA0bpQ2O6SBA1xPXilNKrDILuLgoeJmbAX6JY16KvEMDz78fcB0xAU3DdrD1M6BgR1cygDhxKA= 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: Fix netfs_page_mkwrite() to check that folio->mapping is valid once it has taken the folio lock (as filemap_page_mkwrite() does). Without this, generic/247 occasionally oopses with something like the following: BUG: kernel NULL pointer dereference, address: 0000000000000000 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page RIP: 0010:trace_event_raw_event_netfs_folio+0x61/0xc0 ... Call Trace: ? __die_body+0x1a/0x60 ? page_fault_oops+0x6e/0xa0 ? exc_page_fault+0xc2/0xe0 ? asm_exc_page_fault+0x22/0x30 ? trace_event_raw_event_netfs_folio+0x61/0xc0 trace_netfs_folio+0x39/0x40 netfs_page_mkwrite+0x14c/0x1d0 do_page_mkwrite+0x50/0x90 do_pte_missing+0x184/0x200 __handle_mm_fault+0x42d/0x500 handle_mm_fault+0x121/0x1f0 do_user_addr_fault+0x23e/0x3c0 exc_page_fault+0xc2/0xe0 asm_exc_page_fault+0x22/0x30 This is due to the invalidate_inode_pages2_range() issued at the end of the DIO write interfering with the mmap'd writes. Fixes: 102a7e2c598c ("netfs: Allow buffered shared-writeable mmap through netfs_page_mkwrite()") Signed-off-by: David Howells cc: Matthew Wilcox cc: Jeff Layton cc: netfs@lists.linux.dev cc: v9fs@lists.linux.dev cc: linux-afs@lists.infradead.org cc: linux-cifs@vger.kernel.org cc: linux-mm@kvack.org cc: linux-fsdevel@vger.kernel.org Reviewed-by: Jeff Layton --- Changes ======= ver #2) - Actually unlock the folio rather than returning VM_FAULT_LOCKED with VM_FAULT_NOPAGE. fs/netfs/buffered_write.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fs/netfs/buffered_write.c b/fs/netfs/buffered_write.c index 07bc1fd43530..270f8ebf8328 100644 --- a/fs/netfs/buffered_write.c +++ b/fs/netfs/buffered_write.c @@ -523,6 +523,7 @@ vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_gr struct netfs_group *group; struct folio *folio = page_folio(vmf->page); struct file *file = vmf->vma->vm_file; + struct address_space *mapping = file->f_mapping; struct inode *inode = file_inode(file); struct netfs_inode *ictx = netfs_inode(inode); vm_fault_t ret = VM_FAULT_RETRY; @@ -534,6 +535,11 @@ vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_gr if (folio_lock_killable(folio) < 0) goto out; + if (folio->mapping != mapping) { + folio_unlock(folio); + ret = VM_FAULT_NOPAGE; + goto out; + } if (folio_wait_writeback_killable(folio)) { ret = VM_FAULT_LOCKED; @@ -549,7 +555,7 @@ vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_gr group = netfs_folio_group(folio); if (group != netfs_group && group != NETFS_FOLIO_COPY_TO_CACHE) { folio_unlock(folio); - err = filemap_fdatawait_range(inode->i_mapping, + err = filemap_fdatawait_range(mapping, folio_pos(folio), folio_pos(folio) + folio_size(folio)); switch (err) {