From patchwork Wed Sep 4 20:28:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791368 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 ECD14CD4F25 for ; Wed, 4 Sep 2024 20:29:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 607966B017B; Wed, 4 Sep 2024 16:29:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 58B556B017D; Wed, 4 Sep 2024 16:29:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3B8456B017B; Wed, 4 Sep 2024 16:29:41 -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 18B2E6B0179 for ; Wed, 4 Sep 2024 16:29:41 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CA1351A052F for ; Wed, 4 Sep 2024 20:29:40 +0000 (UTC) X-FDA: 82528196520.15.A230D56 Received: from mail-yb1-f175.google.com (mail-yb1-f175.google.com [209.85.219.175]) by imf14.hostedemail.com (Postfix) with ESMTP id 09BA5100013 for ; Wed, 4 Sep 2024 20:29:38 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=heToXNcQ; dmarc=none; spf=none (imf14.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.219.175) smtp.mailfrom=josef@toxicpanda.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725481754; a=rsa-sha256; cv=none; b=1u2Essj/yQNarrBj0/nLKhDn5BylosgWTWrw5Z3ImMnF0nbv/oQSwWlB3zVJAbPmlYvvQv tILUKhMas9gdmJGS+9lLe34B8CiV44wsYC2CtcGDt+X1aZn6GucYplPI/lVg5yCPKUfIum aZuMwUZ+cHrz65y7ldNqBI2gcAhMQvc= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=heToXNcQ; dmarc=none; spf=none (imf14.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.219.175) smtp.mailfrom=josef@toxicpanda.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725481754; 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=TdpAgT058g8D2uASHL0DyNBaXA20e/8A0vSavuo8wh4=; b=VzFmsj/qmOtNPsYwuq0D1NV/6GTGgb6ZUz55uhAhstfCf9IrGFmJPjy9JcFaXiP6Mqo9E/ g9N0+LJdYFYBwZRfMJ0fKO+N9lqWE0BSscpdqDIZgjMI4Saau4cA5Sr7fqT21m8lvPJu9U sP1T1keOXH2KS8bJrxaYxONtkAdK+ro= Received: by mail-yb1-f175.google.com with SMTP id 3f1490d57ef6-e1a9dc3f0a3so86543276.0 for ; Wed, 04 Sep 2024 13:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481778; x=1726086578; 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=TdpAgT058g8D2uASHL0DyNBaXA20e/8A0vSavuo8wh4=; b=heToXNcQ8dEx7O60WE5MsLXEzXwwigMSMQmYQ4xGGr1/2irC6nc4yUiKceWhJ2svhg yo6whsHWiWBRw3lvj3cWsnK4Hig1HnXwCV2jluXyCrfqd+FsBddodlqsZhJW8SWWc9el QIg6MQzscIN1fQEURUPXsLCxyHJb6dw9Zb+TR57hYQdX0FF8ObGtWpQzdGjUtMGN4PyJ FmeDnZRJf7jcVoA/9J7zbUimhKCk7PyroRdeBmGYmFNyGtQrHMVjQrmdtoKFvv1VjCyK FFp6NittvPfiD4+nm3D4j9+Otq6T9oFIC5ApXH4W7o4hPLMzpzA3bWHBWB3IB49mB12n +XzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481778; x=1726086578; 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=TdpAgT058g8D2uASHL0DyNBaXA20e/8A0vSavuo8wh4=; b=Ga1G10C8Cp8Zw9fsQdU6lLEikeJ4kcRy6NlwLc3YWFfl6NP64Ukan6LSQ/SdkBdUk7 ziEAl9e6N3hQfsfHHeNpniugzcLySShwiQv9apKKcChCIRnxDSMDjPdZtoEVRdd/Txfm 261voVPyn3U3XDTeZvONHMLhZ+8UKsQ4HIdcrPjzA4Kmr/fnKaJLuzxpqmkLFuohko0v PplFcVXXiWh7nbCHQpX5kO5tuki7WyiigAoKCpGGxkTHYvHpEeUgdpxNCnY3oF5x3xgK +1dXUMJQM/b25CnkVUwTtJXyFC+jpNlCsifUxY9Nwfabrpk6UCpfz6L3zVrFZ/dK4lRk JbDg== X-Forwarded-Encrypted: i=1; AJvYcCWPCdaKCFLHwritOhZOd6kRw5T/Yv+eF1y6vCE1SGkv77z2NfGLi9JqcbT4GEWgdm+vdrTmVkPKUQ==@kvack.org X-Gm-Message-State: AOJu0Yz3siwfiCqHTyYexnrtJpjJP7/yGMsxJwQHgys+cNs7XcJx+EPO Z/xZfj6DfaKWAx5c/RlC7xd0W/mO02p0paBSFfANX6znY+fYjKNsNXp16VB6sY8= X-Google-Smtp-Source: AGHT+IHeHitsPcQ1kfVrPGq0b4GF0aiDUGxULm3YTFoSiEMXAET19y3p8EGpUVDmPCTd+KLj6bMEOQ== X-Received: by 2002:a05:6902:70a:b0:e0e:7b3d:53fe with SMTP id 3f1490d57ef6-e1a79fd884emr23296230276.18.1725481778061; Wed, 04 Sep 2024 13:29:38 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c520418d34sm1551586d6.119.2024.09.04.13.29.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:37 -0700 (PDT) From: Josef Bacik To: kernel-team@fb.com, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, brauner@kernel.org, linux-xfs@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v5 14/18] fsnotify: generate pre-content permission event on page fault Date: Wed, 4 Sep 2024 16:28:04 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 09BA5100013 X-Rspamd-Server: rspam01 X-Stat-Signature: cwpyn1gjfdbctr1sbiur5tc44ms83bgn X-HE-Tag: 1725481778-461018 X-HE-Meta: U2FsdGVkX188pN89W/cSykq6evjI0LUOM7TQlQHv3z9j9eAbMoEiEAaJHEXMszHdsvu2GJh7clHy5UKTUK9YcZ74oUUpqZ8WsF2KKQUY2DlMRoPh0rbwjY+YaYD6FfdAOmlC7GwF+qsGv9p1aW+8QEWovgOfG9paWV3cYD9hjI13LlXL5RZy8U1XNu8J5ysTJOl7or+VWNDQ86hwlXF5iPPL0kqTMDWJd4wznVv7Fef1iyHDwz+C7MN9tnG27eT8X23wggn5kxZiZhqFDX5yWEqg77S31T2O+q8Ww1r2Iiori02N7Aw0T4hwT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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 | 116 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 110 insertions(+), 7 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index ab3d78116043..89665732b404 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3503,6 +3503,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 8b1684b62177..b2d29947ce7f 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include "internal.h" @@ -3112,13 +3113,13 @@ static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio, * that. If we didn't pin a file then we return NULL. The file that is * returned needs to be fput()'ed when we're done with it. */ -static struct file *do_sync_mmap_readahead(struct vm_fault *vmf) +static struct file *do_sync_mmap_readahead(struct vm_fault *vmf, + struct file *fpin) { struct file *file = vmf->vma->vm_file; struct file_ra_state *ra = &file->f_ra; struct address_space *mapping = file->f_mapping; DEFINE_READAHEAD(ractl, file, ra, mapping, vmf->pgoff); - struct file *fpin = NULL; unsigned long vm_flags = vmf->vma->vm_flags; unsigned int mmap_miss; @@ -3190,12 +3191,12 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf) * was pinned if we have to drop the mmap_lock in order to do IO. */ static struct file *do_async_mmap_readahead(struct vm_fault *vmf, - struct folio *folio) + struct folio *folio, + struct file *fpin) { struct file *file = vmf->vma->vm_file; struct file_ra_state *ra = &file->f_ra; DEFINE_READAHEAD(ractl, file, ra, file->f_mapping, vmf->pgoff); - struct file *fpin = NULL; unsigned int mmap_miss; /* See comment in do_sync_mmap_readahead. */ @@ -3260,6 +3261,93 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf) return ret; } +/* + * If we have pre-content watches on this file we will need to emit an event for + * this range. We will handle dropping the lock and emitting the event. + * + * If FAULT_FLAG_RETRY_NOWAIT is set then we'll return VM_FAULT_RETRY. + * + * If no event was emitted then *fpin will be NULL and we will return 0. + * + * If any error occurred we will return VM_FAULT_SIGBUS, *fpin could still be + * set and will need to have fput() called on it. + * + * If we emitted the event then we will return 0 and *fpin will be set, this + * must have fput() called on it, and the caller must call VM_FAULT_RETRY after + * any other operations it does in order to re-fault the page and make sure the + * appropriate locking is maintained. + * + * Return: the appropriate vm_fault_t return code, 0 on success. + */ +static vm_fault_t __filemap_fsnotify_fault(struct vm_fault *vmf, + struct file **fpin) +{ + struct file *file = vmf->vma->vm_file; + loff_t pos = vmf->pgoff << PAGE_SHIFT; + int mask = (vmf->flags & FAULT_FLAG_WRITE) ? MAY_WRITE : MAY_ACCESS; + int 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, return NULL. */ + if (!fsnotify_file_has_pre_content_watches(file)) + return 0; + + /* We are NOWAIT, we can't wait, just return EAGAIN. */ + if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) + return VM_FAULT_RETRY; + + /* + * If this fails then we're not allowed to drop the fault lock, return a + * SIGBUS so we don't errantly populate pagecache with bogus data for + * this file. + */ + *fpin = maybe_unlock_mmap_for_io(vmf, *fpin); + if (*fpin == NULL) + return VM_FAULT_SIGBUS; + + /* + * We can't fput(*fpin) at this point because we could have been passed + * in fpin from a previous call. + */ + ret = fsnotify_file_area_perm(*fpin, mask, &pos, PAGE_SIZE); + if (ret) + return VM_FAULT_SIGBUS; + + return 0; +} + +/** + * filemap_fsnotify_fault - maybe emit a pre-content event. + * @vmf: struct vm_fault containing details of the fault. + * + * 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. + * + * 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; + vm_fault_t ret; + + ret = __filemap_fsnotify_fault(vmf, &fpin); + if (fpin) { + fput(fpin); + if (!ret) + ret = VM_FAULT_RETRY; + } + return ret; +} +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 @@ -3299,6 +3387,17 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) if (unlikely(index >= max_idx)) return VM_FAULT_SIGBUS; + /* + * If we have pre-content watchers then we need to generate events on + * page fault so that we can populate any data before the fault. + */ + ret = __filemap_fsnotify_fault(vmf, &fpin); + if (unlikely(ret)) { + if (fpin) + fput(fpin); + return ret; + } + /* * Do we have something in the page cache already? */ @@ -3309,21 +3408,24 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) * the lock. */ if (!(vmf->flags & FAULT_FLAG_TRIED)) - fpin = do_async_mmap_readahead(vmf, folio); + fpin = do_async_mmap_readahead(vmf, folio, fpin); if (unlikely(!folio_test_uptodate(folio))) { filemap_invalidate_lock_shared(mapping); mapping_locked = true; } } else { ret = filemap_fault_recheck_pte_none(vmf); - if (unlikely(ret)) + if (unlikely(ret)) { + if (fpin) + goto out_retry; return ret; + } /* No page in the page cache at all */ count_vm_event(PGMAJFAULT); count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT); ret = VM_FAULT_MAJOR; - fpin = do_sync_mmap_readahead(vmf); + fpin = do_sync_mmap_readahead(vmf, fpin); retry_find: /* * See comment in filemap_create_folio() why we need