From patchwork Tue Nov 12 17:55:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13872740 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 84A85D42BBE for ; Tue, 12 Nov 2024 17:57:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 780FA6B0108; Tue, 12 Nov 2024 12:56:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E3596B0109; Tue, 12 Nov 2024 12:56:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 50D2E6B010A; Tue, 12 Nov 2024 12:56:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 325DE6B0108 for ; Tue, 12 Nov 2024 12:56:55 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DA71F160464 for ; Tue, 12 Nov 2024 17:56:54 +0000 (UTC) X-FDA: 82778195850.20.A985FF1 Received: from mail-yw1-f173.google.com (mail-yw1-f173.google.com [209.85.128.173]) by imf13.hostedemail.com (Postfix) with ESMTP id C4E7620007 for ; Tue, 12 Nov 2024 17:56:09 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=JnUeUD3D; spf=none (imf13.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.173) 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=1731434020; 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=uxhSuaJocgL72UVrggFE5s8XUkT4L5kR3eXh3bvh7i3eH27b6smIzZgoMFntF/4Hi6MSZ7 4HXCFH4ZE3M5zuSKbfbyan3yjEOCgKe6rRxgbguHbvz1Cob2ju9Gjih0gn8hGd2BF8AX7V vtaMHmGsccJi5qEHh5WyfGDPvyxxJ54= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=JnUeUD3D; spf=none (imf13.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.173) smtp.mailfrom=josef@toxicpanda.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731434020; a=rsa-sha256; cv=none; b=YKHxorB7TDRMxK7ZXTznoLTvmX/lWY1CesNyrHi1MmAjAdWZDAVz70O7XHfRFDOtHetnYZ qJTAzoFPbKC77EANKxWZbGxDG+vQTAHou9WSG8apHokvvZHBS/sla9z1VrHGEW9aWFuYzD 5Ge756XPU3AWNcNXV5ixoa8JBIXT20A= Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-6e5b7cd1ef5so51433637b3.1 for ; Tue, 12 Nov 2024 09:56:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731434212; x=1732039012; 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=JnUeUD3DHhVybNwrcMFLg7vB/Yq0cHqoSG93uWD4urXjlv9At+PvmjAawU5KP+Op7s pnXJP2wx5omMk1rQwdjGwyZRthFjtr4uLBRRdBDRjJ8lE8bHVvNIVbONJvUIHpIZcBeg WvwcJ3TAv/wGyx0wOz7/l/btrEcNCw6xCAN6k0YEq3wHz+yHmClxvbnAlnQZsJc7LOPJ t05HCjFF5vYzEdO2HMfO3tveRYJEuGV52aKmLwo/WcPR3C4ZEBzmVXK/nCGQgPe+ubfv HUtYva4u9oa4bx9AHOsvtOU367yeK/FmgB+rzcEpRS1lT9QsH8RMnV0NG6rjUn7g/XJO frAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731434212; x=1732039012; 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=JZNAHf+0bq00GsMaSAU/05qdl5zd6bL5jiXA8c5aELqSOimi4Q1J53337yst6Qa4c2 mRHRV5iZ9MQmifta427DhSQPSpU8GWGrfqxiBWFf2p3YyEPX4WS2flYbEXh40rl3sjs9 SMqQa5w98To4VvbD7FfOwq4EB/9u6cSO/a+bCPIwYMBgAyNjKkuDMjSxnBKhpXD8K1s+ 8UlbdOVhiViSNn6fWk299tyNPFeipaCVMeLosHrkSE3urdYS0ul3rGEloWaXe9je+C4G VUvUq2HpNM157x3svIrTBjSj/Z0bXzN+BclxXu6ho6OY3DIqOsCxpiNXajaE+IlV+f36 prrA== X-Forwarded-Encrypted: i=1; AJvYcCWVqmvtZahk0xHN6ucEl65gjcfbd+IYr0CogxQMZnhlbwFpXpJEylx/EC4/Pg/9iYH9YVsfQnI6kQ==@kvack.org X-Gm-Message-State: AOJu0YzMBFrB9VBGlhbYzMe0ZToOYJm2tV+7OHXtNdYKwEPSMJtnlmUq M0AILDQbPZymDAjXJQ0YIAhy80fCKgFuYwxZH0XNu2mJRvYylYrXCimsImprfHA= X-Google-Smtp-Source: AGHT+IFoXHSrvSv1nHXDianvdqh8AvWvvCdruw2IfTMCI7MfgJuOUf9jQwkCjXgIqLVtzlih0erFEw== X-Received: by 2002:a05:690c:700e:b0:6ea:8dad:c3cf with SMTP id 00721157ae682-6eadddbc58emr168920227b3.20.1731434212150; Tue, 12 Nov 2024 09:56:52 -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-6eaceb0cdc8sm26499817b3.65.2024.11.12.09.56.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 09:56:51 -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, linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org, linux-ext4@vger.kernel.org Subject: [PATCH v7 15/18] fsnotify: generate pre-content permission event on page fault Date: Tue, 12 Nov 2024 12:55:30 -0500 Message-ID: <8d223ba40c3ad28dcf9369bf16c3182baa925e59.1731433903.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C4E7620007 X-Stat-Signature: tem4bt4u4g4qaarixdr653k78q7witab X-Rspam-User: X-HE-Tag: 1731434169-268269 X-HE-Meta: U2FsdGVkX1++TXRMXNYujocaTjIRYuV9uaHNkYxYG4ZpXC1C9W5+ArFeISYy16MbKvYK6PGzhmTCMQ1rW4hHr2TaeaLkovPX35rpF34nO4uSFR9UPVPBo2G9OBrrnR+SSaKd4eGaMO/wp0vU8/NtoypGLtP9qfK5XVB+KjYabACjIy0HyPjqILi/nvLY136edJoIzidUkoW/8Gc0qY1Y6ODBaTzVfW9LcuwPF7wnGoC9Tjeyhuqp1MHm6V7HAoRZ5VIB97V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8T/l4Oaj+IAdn5ODxZFRDMIgaCRyoQ5o1Lsyh572siA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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