From patchwork Fri Nov 15 15:30:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13876453 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 3E95DD68BC6 for ; Fri, 15 Nov 2024 15:32:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 565D16B00A6; Fri, 15 Nov 2024 10:31:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 517976B00A7; Fri, 15 Nov 2024 10:31:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38FDF6B00A8; Fri, 15 Nov 2024 10:31:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 19F3A6B00A6 for ; Fri, 15 Nov 2024 10:31:58 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CE38E1219CB for ; Fri, 15 Nov 2024 15:31:57 +0000 (UTC) X-FDA: 82788718824.05.653B53B Received: from mail-yb1-f172.google.com (mail-yb1-f172.google.com [209.85.219.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 916E81C0005 for ; Fri, 15 Nov 2024 15:30:58 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=07cwN6yr; spf=none (imf20.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.219.172) smtp.mailfrom=josef@toxicpanda.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731684626; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3KCTaRjNmkxmxaKf/dEMrgpxbgs8A3iRugI4cag6Bm4=; b=8VWx7ooN5oF3dacNc/UYjTWZAAr34Wq1i7kAOx+0osaubu9IZPtZfG9Oqn/9wfe145bwBq tP6Sqy5mAVv/VREqTQQJIioi+TZyoDrlutN0nUPe63d73Byet33UQqEGK8DObsNejlIIsG B2BrHx0D4sojgwQegEmiApDcXqRhgP0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731684626; a=rsa-sha256; cv=none; b=MiDPPJLiWHt+kbMe4cKDiGVx+TX7doj+wEBUWD/+U7gNAOsYzzSqtwiRXSbbwe3Dn/7oaZ XCE7wEj6xGmgr4eVo4qHQMmI/wtVHLnJeLV2X8NEA5zQL/Gsg2eu98HTkbnCvc0G/DL62h 8ZZg9mD0nMoZTO2PtXwT8PbpKsf2TEY= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=07cwN6yr; spf=none (imf20.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.219.172) smtp.mailfrom=josef@toxicpanda.com; dmarc=none Received: by mail-yb1-f172.google.com with SMTP id 3f1490d57ef6-e38158560d4so2003115276.2 for ; Fri, 15 Nov 2024 07:31:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731684714; x=1732289514; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=3KCTaRjNmkxmxaKf/dEMrgpxbgs8A3iRugI4cag6Bm4=; b=07cwN6yrdjKh4zfeoa1z3hHkXsqvvFR2TZRo1xhY1CTN/ZkX3a7zlvONTd3d+5T81L giILYyUwY1swVolFShDGMAHGdGalA5ei99TTtrGDvtDu1v0ZiMgVB3ONqhjA8Nv9rXtj eGYD1M0c8lAW0nWuMaQ5IE/QsEBjAs6AB13Nd3nIngOv0mgrXiK1ezKIe/E5n96r6CN5 i9qU6SwYGT2lrfGVXKXqjyht2PQIOGZU2L4EbexGK3WInD64sIUVXwZuYAeb0b714d0B YvDb6c26Ei6P6QjtQ/gMhtX4e+l6jp9je5+f/ieUd5eFswbXk8MDws/qFjCu5s1CKZy2 oAFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731684714; x=1732289514; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3KCTaRjNmkxmxaKf/dEMrgpxbgs8A3iRugI4cag6Bm4=; b=rgSOR1zrzMpH99GclZzlZmaZJwm7bnqwtHdJ7i9QnX6ycy6zpAEEneZ6HUcpgROvtl XEibijhWiku0nC6RKX2XbxxXHtTZ537dP8h/2nt6NNiSd612MdkGDFqgRctvrrsidze5 WDUREik76jCZ+g3wABeTAJ3Sre+sIbxG/tLmCRNvVaTS5oOng1j+JgMJrPqxJsyoubH1 nN5ChLSowmohcbz9sXRRV4Q7txwLTkvIDmfbl6gv4MPQ3w/DVt4wSamUdl/dG20FW+Ok mhuMKNBUav5xukrzer8nM6T4BnpfUCYpoQgHRuEr7mC1KYNt9OQfMc79womcEs4sz5L0 XyRA== X-Forwarded-Encrypted: i=1; AJvYcCVhxMjimepF67pzJLj1i0audZ+sNZxa1OoC06vMkZmEm5KnO33sOO2FaquC9ieEjjCqZc9+l78QnA==@kvack.org X-Gm-Message-State: AOJu0YxvptSMeFKGrU4lVTrbEOVmBtMCs/6U/M4VEjDuGMAaAwCAMhFS FzsZUELAvT1ePFrjP3Aqq5cwGdsEAHvhfkabgUSxkPxJtvHVFMHULncgD0hmeag= X-Google-Smtp-Source: AGHT+IFmVYcZJStgSp9a9E1dsOyyT4rBH0daYbq6mRod+diiaSsdzS2Ed6cQkqbS64804RotiUDKFQ== X-Received: by 2002:a05:690c:9:b0:6e2:a129:1623 with SMTP id 00721157ae682-6ee55c69ee9mr29748387b3.38.1731684714337; Fri, 15 Nov 2024 07:31:54 -0800 (PST) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6ee4444ccdfsm7704207b3.120.2024.11.15.07.31.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2024 07:31:53 -0800 (PST) From: Josef Bacik To: kernel-team@fb.com, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, brauner@kernel.org, torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org, linux-ext4@vger.kernel.org Subject: [PATCH v8 16/19] fsnotify: generate pre-content permission event on page fault Date: Fri, 15 Nov 2024 10:30:29 -0500 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Stat-Signature: a8dfqs1rmd7rj67drerh9cm5c7beeekb X-Rspamd-Queue-Id: 916E81C0005 X-Rspam-User: X-HE-Tag: 1731684658-241048 X-HE-Meta: U2FsdGVkX1//QXQmMYHKYo/olUOpUOT9gRc/sgWP5MUqgXYwrhRnVLvhK8MjOn+BP4RzJY3SyKhcDexBlK/ohfw3STIj6b2ZPfkvMk6Hc3yQEednDBfwVwbdalD4tMNQIK6+WtLzHzZF/9UsTDw6UW/nXtKv1+WcOhVkt5fku95uGKYLehu3OzQ419AF6tUiZR3yZ+ybqKhjUH7CldhAvZWMBiK9vnGDeFY9JirR7D4NRfEHmXC6I9PLMFsrMBuUWnVk9FqdV2H30XAamt/qlW6B9a5Z0UYdr+Ax/yFub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hYGW5wPaKNyrv+GQJG5RZJtnfNIXjszb1HHH8VpEbQ6/g3dhFVez1kLgNGWMGejwAk5t0+ywupEW8nPNxeSM2OjrRqvyLbUYCW0cr0GdP946hREKxjEtBTs6zig5Rh18Fa8XvuX402+22+Ozn1oPqcjx/LsZzw== 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: FS_PRE_ACCESS or FS_PRE_MODIFY 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 with in filemap_fault. Signed-off-by: Josef Bacik --- include/linux/mm.h | 1 + mm/filemap.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 01c5e7a4489f..90155ef8599a 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3406,6 +3406,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 68ea596f6905..0bf7d645dec5 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include "internal.h" @@ -3289,6 +3290,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 (!fsnotify_file_has_pre_content_watches(vmf->vma->vm_file)) + 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 @@ -3392,6 +3439,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) && + fsnotify_file_has_pre_content_watches(file)) { + 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