From patchwork Thu Nov 21 11:22:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 13881885 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 0E35ED6ED09 for ; Thu, 21 Nov 2024 11:23:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AE60C6B0096; Thu, 21 Nov 2024 06:22:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A60C6B00A2; Thu, 21 Nov 2024 06:22:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DAABF6B009B; Thu, 21 Nov 2024 06:22:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D3F5F6B00A1 for ; Thu, 21 Nov 2024 06:22:31 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 75A70161282 for ; Thu, 21 Nov 2024 11:22:31 +0000 (UTC) X-FDA: 82809863136.11.E6A33D1 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf21.hostedemail.com (Postfix) with ESMTP id 4E7C31C000F for ; Thu, 21 Nov 2024 11:20:49 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=jKPO1Z1G; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=4d1OUtVW; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=jKPO1Z1G; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=4d1OUtVW; dmarc=none; spf=pass (imf21.hostedemail.com: domain of jack@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=jack@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732188057; 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=iyarj81CZbF/Jsa/EC6lcSupJZHvi+LDtQmZwDOCsoE=; b=VxqhSmjlacE9lgE0FCNZH48601mxAElElJ8rMzkdWDruMyz/CZJU8WXxqhDHmy+T4r0flm h7aQE9BvEjVTPnpgJAUMkh/J0MOgwX5J5ElA34ub8z0GmsIzilfpcXMF51pdTCILIHCytd SSkXORz9J0iJmUt43oSLs0/s3KZeCng= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=jKPO1Z1G; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=4d1OUtVW; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=jKPO1Z1G; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=4d1OUtVW; dmarc=none; spf=pass (imf21.hostedemail.com: domain of jack@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=jack@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732188057; a=rsa-sha256; cv=none; b=B/S4FkkSS35aEt/cUjFApRtGsRasgsFeoM+OO85mSl540rHzo2gTt3ZZQexrCO9VhgKruf E7Wa13jIQCCGkLAPWhhizUTgMK8qvQVdu+wWrdBCbmPWwD0hxjUgLxMxaewtpB+kYxAs6R xPmWFgOU36oDQUOiBlWbpN4T9Ci7eYI= Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id BE23F21A06; Thu, 21 Nov 2024 11:22:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1732188144; h=from:from:reply-to: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=iyarj81CZbF/Jsa/EC6lcSupJZHvi+LDtQmZwDOCsoE=; b=jKPO1Z1GidhhNrPp2pu9qj2kFjjc3dy94J+eMzruv0+R1Aa2O6kBDWxg7acRGKcRTKzISs CZkFgK4/3H/D6d5XymxigwJFtLKwgs0eaYdSWJeqLtm7nCYDzeK8YaZSMSd3bJ49xiuq2x TrUKYZJBWpDc7Uztgx4OWe2lbJuuLjw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1732188144; h=from:from:reply-to: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=iyarj81CZbF/Jsa/EC6lcSupJZHvi+LDtQmZwDOCsoE=; b=4d1OUtVWF5W8SbiMXPDGd9W7jZwCJcKVnqisqtEMsfvZEZBqd0txWZ2e+TpS/wRlGI5jX9 AfahCH6T7DADdkAA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1732188144; h=from:from:reply-to: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=iyarj81CZbF/Jsa/EC6lcSupJZHvi+LDtQmZwDOCsoE=; b=jKPO1Z1GidhhNrPp2pu9qj2kFjjc3dy94J+eMzruv0+R1Aa2O6kBDWxg7acRGKcRTKzISs CZkFgK4/3H/D6d5XymxigwJFtLKwgs0eaYdSWJeqLtm7nCYDzeK8YaZSMSd3bJ49xiuq2x TrUKYZJBWpDc7Uztgx4OWe2lbJuuLjw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1732188144; h=from:from:reply-to: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=iyarj81CZbF/Jsa/EC6lcSupJZHvi+LDtQmZwDOCsoE=; b=4d1OUtVWF5W8SbiMXPDGd9W7jZwCJcKVnqisqtEMsfvZEZBqd0txWZ2e+TpS/wRlGI5jX9 AfahCH6T7DADdkAA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id B118813927; Thu, 21 Nov 2024 11:22:24 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id XE1BK/AXP2dGfwAAD6G6ig (envelope-from ); Thu, 21 Nov 2024 11:22:24 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 40E83A08E1; Thu, 21 Nov 2024 12:22:24 +0100 (CET) From: Jan Kara To: Cc: Amir Goldstein , Josef Bacik , brauner@kernel.org, Linus Torvalds , Al Viro , linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-mm@kvack.org, Jan Kara Subject: [PATCH 15/19] fsnotify: generate pre-content permission event on page fault Date: Thu, 21 Nov 2024 12:22:14 +0100 Message-Id: <20241121112218.8249-16-jack@suse.cz> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20241121112218.8249-1-jack@suse.cz> References: <20241121112218.8249-1-jack@suse.cz> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 4E7C31C000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WRCelXYoirktQg5uyrJHZ0lhz2Lh//sr1rB1mmM6mxvm5ln0Qpp8nHW6RvkST5UKOZgZeGoB21lvMNDdzl0UegS1uMvWW098CAiybNglcsbNh9884dPG66eENRll5ZxQpiN8bA38yQsMEqDw+yW5lKFw9+QMgtXPghxGAfdRebOCLRZvNxZgGzWCN5Lgv7aPPVfZgAyVVCUGcRhpq3BRZMtlgnbv1ZGMKBBL5HKHALy/qVmP6sw1mAYuoHe0YovRw5YyWgICMb99FzET3WqpANO5EkZ3fPiVYou8m9Bv7YiCO4jAinZhx3rSD+FSgcemXFIcFM+0ZWfjshQMVlBks6X8QeoSB6Fdw6yRBKvk1mnylPZyHGy 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: From: Josef Bacik FS_PRE_ACCESS will be generated on page fault depending on the faulting method. This pre-content event is meant to be used by hierarchical storage managers that want to fill in the file content on first read access. Export a simple helper that file systems that have their own ->fault() will use, and have a more complicated helper to be do fancy things in filemap_fault. Signed-off-by: Josef Bacik Signed-off-by: Jan Kara Link: https://patch.msgid.link/aa56c50ce81b1fd18d7f5d71dd2dfced5eba9687.1731684329.git.josef@toxicpanda.com --- include/linux/mm.h | 1 + mm/filemap.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++ mm/nommu.c | 7 +++++ 3 files changed, 86 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index ecf63d2b0582..e9077ab16972 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3405,6 +3405,7 @@ extern vm_fault_t filemap_fault(struct vm_fault *vmf); extern vm_fault_t filemap_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff); extern vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf); +extern vm_fault_t filemap_fsnotify_fault(struct vm_fault *vmf); extern unsigned long stack_guard_gap; /* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */ diff --git a/mm/filemap.c b/mm/filemap.c index 98f15dccff89..0648bb568259 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include "internal.h" @@ -3287,6 +3288,52 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf) return ret; } +/** + * filemap_fsnotify_fault - maybe emit a pre-content event. + * @vmf: struct vm_fault containing details of the fault. + * @folio: the folio we're faulting in. + * + * If we have a pre-content watch on this file we will emit an event for this + * range. If we return anything the fault caller should return immediately, we + * will return VM_FAULT_RETRY if we had to emit an event, which will trigger the + * fault again and then the fault handler will run the second time through. + * + * This is meant to be called with the folio that we will be filling in to make + * sure the event is emitted for the correct range. + * + * Return: a bitwise-OR of %VM_FAULT_ codes, 0 if nothing happened. + */ +vm_fault_t filemap_fsnotify_fault(struct vm_fault *vmf) +{ + struct file *fpin = NULL; + int mask = (vmf->flags & FAULT_FLAG_WRITE) ? MAY_WRITE : MAY_ACCESS; + loff_t pos = vmf->pgoff >> PAGE_SHIFT; + size_t count = PAGE_SIZE; + vm_fault_t ret; + + /* + * We already did this and now we're retrying with everything locked, + * don't emit the event and continue. + */ + if (vmf->flags & FAULT_FLAG_TRIED) + return 0; + + /* No watches, we're done. */ + if (likely(!FMODE_FSNOTIFY_HSM(vmf->vma->vm_file->f_mode))) + return 0; + + fpin = maybe_unlock_mmap_for_io(vmf, fpin); + if (!fpin) + return VM_FAULT_SIGBUS; + + ret = fsnotify_file_area_perm(fpin, mask, &pos, count); + fput(fpin); + if (ret) + return VM_FAULT_SIGBUS; + return VM_FAULT_RETRY; +} +EXPORT_SYMBOL_GPL(filemap_fsnotify_fault); + /** * filemap_fault - read in file data for page fault handling * @vmf: struct vm_fault containing details of the fault @@ -3390,6 +3437,37 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) * or because readahead was otherwise unable to retrieve it. */ if (unlikely(!folio_test_uptodate(folio))) { + /* + * If this is a precontent file we have can now emit an event to + * try and populate the folio. + */ + if (!(vmf->flags & FAULT_FLAG_TRIED) && + unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) { + loff_t pos = folio_pos(folio); + size_t count = folio_size(folio); + + /* We're NOWAIT, we have to retry. */ + if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) { + folio_unlock(folio); + goto out_retry; + } + + if (mapping_locked) + filemap_invalidate_unlock_shared(mapping); + mapping_locked = false; + + folio_unlock(folio); + fpin = maybe_unlock_mmap_for_io(vmf, fpin); + if (!fpin) + goto out_retry; + + error = fsnotify_file_area_perm(fpin, MAY_ACCESS, &pos, + count); + if (error) + ret = VM_FAULT_SIGBUS; + goto out_retry; + } + /* * If the invalidate lock is not held, the folio was in cache * and uptodate and now it is not. Strange but possible since we diff --git a/mm/nommu.c b/mm/nommu.c index 385b0c15add8..a8789faa49ee 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -1614,6 +1614,13 @@ int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, } EXPORT_SYMBOL(remap_vmalloc_range); +vm_fault_t filemap_fsnotify_fault(struct vm_fault *vmf) +{ + BUG(); + return 0; +} +EXPORT_SYMBOL_GPL(filemap_fsnotify_fault); + vm_fault_t filemap_fault(struct vm_fault *vmf) { BUG();