From patchwork Tue Nov 12 17:55:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13872738 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 0D1CFD42BBA for ; Tue, 12 Nov 2024 17:57:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D2226B00A5; Tue, 12 Nov 2024 12:56:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 059EC6B0108; Tue, 12 Nov 2024 12:56:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D728E6B00A5; Tue, 12 Nov 2024 12:56:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B1DDC6B0106 for ; Tue, 12 Nov 2024 12:56:51 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6EB6DA04DC for ; Tue, 12 Nov 2024 17:56:51 +0000 (UTC) X-FDA: 82778198244.14.7D3D8BA Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) by imf23.hostedemail.com (Postfix) with ESMTP id 16217140007 for ; Tue, 12 Nov 2024 17:56:21 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=w5tg5gWQ; dmarc=none; spf=none (imf23.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.182) smtp.mailfrom=josef@toxicpanda.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731434147; a=rsa-sha256; cv=none; b=FzmvYoWLTGJqa8EnnfCXn1IoSkYn3NSlvBhCqnl3MVHei33axxJVy3efRTiL8AJdlBbKp/ SAWMdRPTfRzDwqCwoSdsxcXz1SnV6D2nMOmzO/v1UW/8U/LmTa3iL63v2+NZhwxvAlKLK7 DJuDJH+8Zai+RCNHdDgQbz2P/BcywbY= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=w5tg5gWQ; dmarc=none; spf=none (imf23.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.182) smtp.mailfrom=josef@toxicpanda.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731434147; 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=DKrH1r2LzuvlRLkIW99g50GCI+7PqnnQMlTA3Xoxniw=; b=09WSHuqPlcjiia8od8NOj7VIFwzpfOlhw14xRrCgZaV5Z755RLsqr1Sp3DLIBPUvvoNFtT 5bpoa57gq+CFW8eWeE6cxVYuTvZYIZPjaY6UrB/yorKOwKYIYBzaH1HMEgmD9dDT6syBCz 27UBRzJpFtViZjNClwqIeGssj+V6+QA= Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-6ea5b97e31cso50199907b3.1 for ; Tue, 12 Nov 2024 09:56:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731434209; x=1732039009; 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=DKrH1r2LzuvlRLkIW99g50GCI+7PqnnQMlTA3Xoxniw=; b=w5tg5gWQ/gpWnYve2uoKKADAocLukQkic5cQI9u1oYCRYTDmRsR7TyhSIPThfyYlHD T3oXYDJaLV5m0yE6WNgMTj3q5QXqNbH+WA4z5SqSTYRWtbH6UL1GKNo/5PM5K0kWHZin FarvWAWdOddM7YRqaBbUcV9L1mb3L4QWcMbjaUyBW2XUH/z/Yp71EhXuQ+eKEZlwDmU+ cKtkf94gO7bQaiVD3twSrHZSrwwxrrK9M7AxxNEDTUkfWLRcsSmmpkAEis0KZzm5r8Td IP85RyB6Z0UerlVeIn7vnohbK/JfuUaiJCLtBGfkgwDrkQD1rlWuqTEe2n1QmWDrkEJQ UHCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731434209; x=1732039009; 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=DKrH1r2LzuvlRLkIW99g50GCI+7PqnnQMlTA3Xoxniw=; b=nthXSRpc0/ZK6NenmhNmnwgZW2LdIs4z596IvuGBBgdMGyrHt19En1pCmWpOHmTMfY orrywV0dnWFZVK6jH7LbfzFL14lDpDsRWhv79EOqtiB9CxbsIk0n0xavgkmE3FifXDuO OYMQowBVD/o9ZbpGAGm732m2CJgolHhhTOVMdVeZtikgohnriErowWSHq3ZhSJ8ULjeu SlvM1g8HKoH8iWJqpIDrBf20Z++hRsHDikU+MTSup7Vn/piWT6JHAIYBtfoCR2XsgeWZ 1We0cGsFKSxiJ3uJSiMRuovoHBOTinFAWPgoSgQx8iW5Ll1uqZKRChYJIcwQE/e2Q9Hx UDuA== X-Forwarded-Encrypted: i=1; AJvYcCW2ktSQ//gX0xyiYg3isVpFA2+3Ulh2ThMr1Z4mI+tKwfB+hgCvsbSUVrnHQvNzn1vHGTELDyofrA==@kvack.org X-Gm-Message-State: AOJu0YxiHzN8x9lbH3La21IgoNJaBOYRvsJrGu/FcZkyqTactw0h5I2H i+wfBCzTYZgWzwYc/UDy8L857hWDYS/wJeSth31nYXD59mKpW/IJb1WLRx5ragc= X-Google-Smtp-Source: AGHT+IFJc2RNjc1G/OBWSTi5fyS4IghkV6OUbtpYdZWmaUR5D0i4/YYlt6JaXitbsIbc6IfGCQnj9g== X-Received: by 2002:a05:690c:3510:b0:6e3:c92c:1d57 with SMTP id 00721157ae682-6eadc0a271emr140258467b3.2.1731434208685; Tue, 12 Nov 2024 09:56:48 -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-6eaceb65a75sm26238187b3.97.2024.11.12.09.56.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 09:56:48 -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 13/18] fanotify: disable readahead if we have pre-content watches Date: Tue, 12 Nov 2024 12:55:28 -0500 Message-ID: <23edd657a315f2e5ed648f8dba6d34898b4af85b.1731433903.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 16217140007 X-Rspamd-Server: rspam01 X-Stat-Signature: wkptyqae6iaihre6wkb63hqpx9s1a8uw X-HE-Tag: 1731434181-414041 X-HE-Meta: U2FsdGVkX1//IwkV/NvUQs0WkrKiPgyt8/DdPpMKn7qanW8wn/m+g4QPECK3qgx8ceObRA0TJFSEQIn2LPe5O9hx5biQNW6+tJHiER+wyJ20UjwqP+UlGrGiCk+Ogd5bKPxvo0/1dIGu8Da7E7fKwX+pAkISg/BSuAXcnvvr6vHNtXu5v14oaJGimrJvMhRVJ9i1A/6SJDlfAN7E2jAThxH8KLaVu8TY2e6WmewjY/B9Z8uEbIqiqWPlN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60JsfQtrQTBnsQwK934lSchyplNsrceZmCXrJOF0lrce62ZcASKB3zrClZknf0brnIRztrZo9MhqAd1D8CywwOcZOu+hFzSTzQFxv1CwrpKXsvZfMBMAAi1XeIVUwyCO3Pr2pAmNu6rzoJnPxuV5TQ5wI99r98beq X-Bogosity: Ham, tests=bogofilter, spamicity=0.000091, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: With page faults we can trigger readahead on the file, and then subsequent faults can find these pages and insert them into the file without emitting an fanotify event. To avoid this case, disable readahead if we have pre-content watches on the file. This way we are guaranteed to get an event for every range we attempt to access on a pre-content watched file. Reviewed-by: Christian Brauner Signed-off-by: Josef Bacik --- mm/filemap.c | 12 ++++++++++++ mm/readahead.c | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/mm/filemap.c b/mm/filemap.c index 196779e8e396..68ea596f6905 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3151,6 +3151,14 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf) unsigned long vm_flags = vmf->vma->vm_flags; unsigned int mmap_miss; + /* + * If we have pre-content watches we need to disable readahead to make + * sure that we don't populate our mapping with 0 filled pages that we + * never emitted an event for. + */ + if (fsnotify_file_has_pre_content_watches(file)) + return fpin; + #ifdef CONFIG_TRANSPARENT_HUGEPAGE /* Use the readahead code, even if readahead is disabled */ if ((vm_flags & VM_HUGEPAGE) && HPAGE_PMD_ORDER <= MAX_PAGECACHE_ORDER) { @@ -3219,6 +3227,10 @@ static struct file *do_async_mmap_readahead(struct vm_fault *vmf, struct file *fpin = NULL; unsigned int mmap_miss; + /* See comment in do_sync_mmap_readahead. */ + if (fsnotify_file_has_pre_content_watches(file)) + return fpin; + /* If we don't want any read-ahead, don't bother */ if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages) return fpin; diff --git a/mm/readahead.c b/mm/readahead.c index 9a807727d809..277c2061fc82 100644 --- a/mm/readahead.c +++ b/mm/readahead.c @@ -128,6 +128,7 @@ #include #include #include +#include #include "internal.h" @@ -544,6 +545,14 @@ void page_cache_sync_ra(struct readahead_control *ractl, unsigned long max_pages, contig_count; pgoff_t prev_index, miss; + /* + * If we have pre-content watches we need to disable readahead to make + * sure that we don't find 0 filled pages in cache that we never emitted + * events for. + */ + if (ractl->file && fsnotify_file_has_pre_content_watches(ractl->file)) + return; + /* * Even if readahead is disabled, issue this request as readahead * as we'll need it to satisfy the requested range. The forced @@ -622,6 +631,10 @@ void page_cache_async_ra(struct readahead_control *ractl, if (!ra->ra_pages) return; + /* See the comment in page_cache_sync_ra. */ + if (ractl->file && fsnotify_file_has_pre_content_watches(ractl->file)) + return; + /* * Same bit is used for PG_readahead and PG_reclaim. */