From patchwork Mon Nov 11 20:17:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13871218 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 51811D3ABF4 for ; Mon, 11 Nov 2024 20:19:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 61F866B0099; Mon, 11 Nov 2024 15:19:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5CE716B009A; Mon, 11 Nov 2024 15:19:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 46ED46B009B; Mon, 11 Nov 2024 15:19:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 28F776B0099 for ; Mon, 11 Nov 2024 15:19:10 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 93F8C121ABE for ; Mon, 11 Nov 2024 20:19:09 +0000 (UTC) X-FDA: 82774926360.03.4E744C5 Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) by imf13.hostedemail.com (Postfix) with ESMTP id ADEE82001D for ; Mon, 11 Nov 2024 20:18:25 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=KK7oIYbp; spf=none (imf13.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.222.171) 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=1731356294; 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: references:dkim-signature; bh=vZOzWH6oaRX5zBw6SWTo1BBrfJV7I9UbqRC6LQ/o1Dw=; b=aIVSsARLm+pq4s8HxwhagxxKkbQpDD2TqcOvPpYuu/auPxHlfewcMBNyn7Bq4NZBcucP/S LZXOelrX+usaL3Yv/88hIwb1s1gBEJYvZ0zz9enrqHba1nGFYhtUmusbZjZHsKzfTXxbXo hDpyAVBYh55j0pctZrKdxuAHeVW3YtQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731356294; a=rsa-sha256; cv=none; b=5bFG1GqnwAVecm1XCu2FtjGlBZS+4H1LOXlN+ckn0nC31cYIyZYUgEaW93DHGfl9db/3+r Fea9ADgp8HcU6Z0qL5rnfxwl5AzMzffTFRBr2mA1Y8e0o843LLuUkIw8mEszIAjCOOMpY2 MI7SjFkmJH4VmXWzSYoAgg3XmvFGraM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=KK7oIYbp; spf=none (imf13.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.222.171) smtp.mailfrom=josef@toxicpanda.com; dmarc=none Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-7b1418058bbso332546285a.3 for ; Mon, 11 Nov 2024 12:19:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731356347; x=1731961147; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=vZOzWH6oaRX5zBw6SWTo1BBrfJV7I9UbqRC6LQ/o1Dw=; b=KK7oIYbpfw3ch6njQ04kqMLg84u0iAx4sZw7ZELuEAMnoQyh0h19vYnMxVwnR5YGy8 TO7vZaVc1hoTcj86ILVzute0Tu/WsWjCYJoG3bLFiUCsZYJFj+8/YdkGFqJZjRPdghIy PD0rSQnYWPbsMxYd86tYf81Q4awSh7RH7RH8SVBxj4BmQtSbtF9+o/JEbJRuLCzMteNn 7xG/6uFzA8nFAbc6WbXwionquAfm0ugjbu2rXmgEQ5DLhrq/NHiUBdneZxX1ZguaI7ov a2Fd6/EiisnjT6QNEC2RZoW3/KbPxgpQidatAnKXlDMeLQaFv7P+IqkTxLWGZ+aVA/2O nK7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731356347; x=1731961147; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vZOzWH6oaRX5zBw6SWTo1BBrfJV7I9UbqRC6LQ/o1Dw=; b=rHX/hkfiEQGeH3dweqWLvP2Z9kVkg3+IACNxCkKMLszo44OtrDGscF/UDyB5Pt4PgB bwD7GAI2le90no0v+0EeGsN6vxIrYNOMAGwhDkTLwqsAhQVwC5Uwg6pOw68MTzXctyf6 TOn/dqV0J/3rWPGMjJaytMS/OI6c9aLkBLmIb86S5hvId1F3ljN/l3BqIz8VORIWOHex cYaFH3CcsgtTI9HXGqmyJIdgOM17Y4/2jxHiL0Hx9xxHSjxEpunXkGdWpOmWYPQ2+iw7 ZaVZj5GLZy+KpkT6V5UkPQpOL9UvyezKgyaZ3YTQpWna3X7M0c2cPWTV7t73M15md9c4 UUFg== X-Forwarded-Encrypted: i=1; AJvYcCUs/eG1HsdS3UJuX1o4TNvqL4OaHWojXIlNx09BySnyv2Jut654CyFH+lCE/rt/U2a2Ohyn9UkN0w==@kvack.org X-Gm-Message-State: AOJu0YyhHytV0A0PnCVgjHbevtAZ18eSwiXHl/k6oCXtRNwIfnIRRRtP BqtRW+9qWT2Z1IOkIla6So76+TCo5gqqhBR3SaLeQFmGOU/UkHKWKHqsC+WVzV0= X-Google-Smtp-Source: AGHT+IFacC1xPqIxXNxCPQqSe1nFqrjTge/tXsUCiXF5RzZu3TnzD62FWh15sejnpZsyvhuNw+LCaA== X-Received: by 2002:a05:6214:5f04:b0:6cb:eabb:13d3 with SMTP id 6a1803df08f44-6d39e19d7a4mr198971316d6.36.1731356346753; Mon, 11 Nov 2024 12:19:06 -0800 (PST) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6d3bf23cc01sm20137136d6.122.2024.11.11.12.19.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:19:06 -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 v6 00/17] fanotify: add pre-content hooks Date: Mon, 11 Nov 2024 15:17:49 -0500 Message-ID: X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Stat-Signature: gocepqpd84ahr6gbpds9porouckfn8ar X-Rspam-User: X-Rspamd-Queue-Id: ADEE82001D X-Rspamd-Server: rspam02 X-HE-Tag: 1731356305-559973 X-HE-Meta: U2FsdGVkX18GO9mp2yr67EC4SKNLQM+4LZjRskeq3pHWDB4DgF0VXraleXY7RmY70L21JcsOqxV1lc1c4V7xbG/p/NtXJ0LPo6yg2LozUIrrhr5dT9H814tuDunh6zwegjLYLS2v6hr3Y5BtpykW5PerUWHhK78Zu1cHepwL/a5QrBr/gYssstmf5MF+xYhlzRxzl2WrrQQO4Wfi7kbquKvOqqjFPHFrudV9lWznVj4wyh+8ZbUjLXucBOgvgh4jtmKk1qbqHoS35dvd7FKQQJU8V/Veu0Zge94nNpEIdEcvhk7+UqvwRAcXEy6YIF/LISQi7qHNYQrTrYPVqaFy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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: v5: https://lore.kernel.org/linux-fsdevel/cover.1725481503.git.josef@toxicpanda.com/ v4: https://lore.kernel.org/linux-fsdevel/cover.1723670362.git.josef@toxicpanda.com/ v3: https://lore.kernel.org/linux-fsdevel/cover.1723228772.git.josef@toxicpanda.com/ v2: https://lore.kernel.org/linux-fsdevel/cover.1723144881.git.josef@toxicpanda.com/ v1: https://lore.kernel.org/linux-fsdevel/cover.1721931241.git.josef@toxicpanda.com/ v5->v6: - Linus had problems with this and rejected Jan's PR (https://lore.kernel.org/linux-fsdevel/20240923110348.tbwihs42dxxltabc@quack3/), so I'm respinning this series to address his concerns. Hopefully this is more acceptable. - Change the page fault hooks to happen only in the case where we have to add a page, not where there exists pages already. - Amir added a hook to truncate. - We made the flag per SB instead of per fstype, Amir wanted this because of some potential issues with other file system specific work he's doing. - Dropped the bcachefs patch, there were some concerns that we were doing something wrong, and it's not a huge deal to not have this feature for now. - Unfortunately the xfs write fault path still has to do the page fault hook before we know if we have a page or not, this is because of the locking that's done before we get to the part where we know if we have a page already or not, so that's the path that is still the same from last iteration. - I've re-validated this series with btrfs, xfs, and ext4 to make sure I didn't break anything. v4->v5: - Cleaned up the various "I'll fix it on commit" notes that Jan made since I had to respin the series anyway. - Renamed the filemap pagefault helper for fsnotify per Christians suggestion. - Added a FS_ALLOW_HSM flag per Jan's comments, based on Amir's rough sketch. - Added a patch to disable btrfs defrag on pre-content watched files. - Added a patch to turn on FS_ALLOW_HSM for all the file systems that I tested. - Added two fstests (which will be posted separately) to validate everything, re-validated the series with btrfs, xfs, ext4, and bcachefs to make sure I didn't break anything. v3->v4: - Trying to send a final verson Friday at 5pm before you go on vacation is a recipe for silly mistakes, fixed the xfs handling yet again, per Christoph's review. - Reworked the file system helper so it's handling of fpin was a little less silly, per Chinner's suggestion. - Updated the return values to not or in VM_FAULT_RETRY, as we have a comment in filemap_fault that says if VM_FAULT_ERROR is set we won't have VM_FAULT_RETRY set. v2->v3: - Fix the pagefault path to do MAY_ACCESS instead, updated the perm handler to emit PRE_ACCESS in this case, so we can avoid the extraneous perm event as per Amir's suggestion. - Reworked the exported helper so the per-filesystem changes are much smaller, per Amir's suggestion. - Fixed the screwup for DAX writes per Chinner's suggestion. - Added Christian's reviewed-by's where appropriate. v1->v2: - reworked the page fault logic based on Jan's suggestion and turned it into a helper. - Added 3 patches per-fs where we need to call the fsnotify helper from their ->fault handlers. - Disabled readahead in the case that there's a pre-content watch in place. - Disabled huge faults when there's a pre-content watch in place (entirely because it's untested, theoretically it should be straightforward to do). - Updated the command numbers. - Addressed the random spelling/grammer mistakes that Jan pointed out. - Addressed the other random nits from Jan. --- Original email --- Hello, These are the patches for the bare bones pre-content fanotify support. The majority of this work is Amir's, my contribution to this has solely been around adding the page fault hooks, testing and validating everything. I'm sending it because Amir is traveling a bunch, and I touched it last so I'm going to take all the hate and he can take all the credit. There is a PoC that I've been using to validate this work, you can find the git repo here https://github.com/josefbacik/remote-fetch This consists of 3 different tools. 1. populate. This just creates all the stub files in the directory from the source directory. Just run ./populate ~/linux ~/hsm-linux and it'll recursively create all of the stub files and directories. 2. remote-fetch. This is the actual PoC, you just point it at the source and destination directory and then you can do whatever. ./remote-fetch ~/linux ~/hsm-linux. 3. mmap-validate. This was to validate the pagefault thing, this is likely what will be turned into the selftest with remote-fetch. It creates a file and then you can validate the file matches the right pattern with both normal reads and mmap. Normally I do something like ./mmap-validate create ~/src/foo ./populate ~/src ~/dst ./rmeote-fetch ~/src ~/dst ./mmap-validate validate ~/dst/foo I did a bunch of testing, I also got some performance numbers. I copied a kernel tree, and then did remote-fetch, and then make -j4 Normal real 9m49.709s user 28m11.372s sys 4m57.304s HSM real 10m6.454s user 29m10.517s sys 5m2.617s So ~17 seconds more to build with HSM. I then did a make mrproper on both trees to see the size [root@fedora ~]# du -hs /src/linux 1.6G /src/linux [root@fedora ~]# du -hs dst 125M dst This mirrors the sort of savings we've seen in production. Meta has had these patches (minus the page fault patch) deployed in production for almost a year with our own utility for doing on-demand package fetching. The savings from this has been pretty significant. The page-fault hooks are necessary for the last thing we need, which is on-demand range fetching of executables. Some of our binaries are several gigs large, having the ability to remote fetch them on demand is a huge win for us not only with space savings, but with startup time of containers. There will be tests for this going into LTP once we're satisfied with the patches and they're on their way upstream. Thanks, Josef Amir Goldstein (9): fanotify: rename a misnamed constant fanotify: reserve event bit of deprecated FAN_DIR_MODIFY fsnotify: introduce pre-content permission events fsnotify: pass optional file access range in pre-content event fsnotify: generate pre-content permission event on open fsnotify: generate pre-content permission event on truncate fanotify: introduce FAN_PRE_ACCESS permission event fanotify: report file range info with pre-content events fanotify: allow to set errno in FAN_DENY permission response Josef Bacik (8): fanotify: don't skip extra event info if no info_mode is set fanotify: add a helper to check for pre content events fanotify: disable readahead if we have pre-content watches mm: don't allow huge faults for files with pre content watches fsnotify: generate pre-content permission event on page fault xfs: add pre-content fsnotify hook for write faults btrfs: disable defrag on pre-content watched files fs: enable pre-content events on supported file systems fs/btrfs/ioctl.c | 9 +++ fs/btrfs/super.c | 5 +- fs/ext4/super.c | 3 + fs/namei.c | 10 ++- fs/notify/fanotify/fanotify.c | 33 ++++++-- fs/notify/fanotify/fanotify.h | 15 ++++ fs/notify/fanotify/fanotify_user.c | 120 +++++++++++++++++++++++------ fs/notify/fsnotify.c | 18 ++++- fs/open.c | 31 +++++--- fs/xfs/xfs_file.c | 4 + fs/xfs/xfs_super.c | 2 +- include/linux/fanotify.h | 19 +++-- include/linux/fs.h | 1 + include/linux/fsnotify.h | 73 ++++++++++++++++-- include/linux/fsnotify_backend.h | 59 +++++++++++++- include/linux/mm.h | 1 + include/uapi/linux/fanotify.h | 18 +++++ mm/filemap.c | 90 ++++++++++++++++++++++ mm/memory.c | 22 ++++++ mm/readahead.c | 13 ++++ security/selinux/hooks.c | 3 +- 21 files changed, 491 insertions(+), 58 deletions(-)