From patchwork Wed Aug 14 21:25:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764155 Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 684B11C7B87 for ; Wed, 14 Aug 2024 21:26:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670769; cv=none; b=GUmuXT8vDBQYtN+aMMgLVq+CDfD99gc2m8fsUZiVAuE3YvYgSYJ5wG+bNJYcIZpaeXq5QZDidvqrKkIEatS00z3SEAVR+/CVyyl2QHF/jf/sEGL9UoYcMrs/LiIQEAP1I14ib2M5SFjVX24KIBZKc1IcKdRN/UvGjXZTk0kXre0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670769; c=relaxed/simple; bh=2T/bTyebmmWzgd79hiDdf0J2vl5OhNotJm/x2K/voWI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R09XrT9fXBOmRro4xH7fByYWSqxKFhKwTEXh/jldz2KyIsQM6OuXnXXgOuFlF0l2HIOAF6qn0XG+mv0x/u2sPQIRxmf5VEVSWbWA/6egg0bI4URHXhUM7SmUPddl3WDIGQRVT4D8rNR9x1LoMx6w+xyB1BVzY9i/48h3g5m10tg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=GFA9QIbu; arc=none smtp.client-ip=209.85.160.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="GFA9QIbu" Received: by mail-qt1-f176.google.com with SMTP id d75a77b69052e-44ff398cefcso1840871cf.0 for ; Wed, 14 Aug 2024 14:26:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670765; x=1724275565; darn=vger.kernel.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=rE3z53SxlgArsKnVp16449O9VmLlGGSseshi4KP5xk8=; b=GFA9QIbuFZu9qCRWztyBNw9BUbbzamMlRQWn2BHBujatWP2EdMryo9iv7xDIo9nOkX IMZ3acXfJtDRwBFtx0LpSlHGWW0RYylHhawiZLHG6QbtdLzFcSIxRhQ1nLmRglzFNlR9 IRu/g6pJMx+2MHJBEf4DmobXu03h0MpmMdRza1m0ewKjdH9i8NV//h411MKMIGnvhLzU JUsSi5koXlP7xHEteq84R5vVdLoQnwYhZw+v7nSzZlvO8S9tnpSIDjY/uqzS0MLcLZhn S3EXoTbrC3aFyg+4FSNlvnVF9BAw2dSlML/Wew1otabc8WylfHJUcLHLLid7AsZUphV+ ISfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670765; x=1724275565; 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=rE3z53SxlgArsKnVp16449O9VmLlGGSseshi4KP5xk8=; b=hy91QfIuPowH8hh8sNK2mDpEhdYcWhqMxD5dhqBK3KAwBLmgPcQQpG8Z4QVA+3WB2i w6P9J1ly1FpEjIvg+KT9xbb1wS6jeZyqkdkuK0dGEb859e2mBu2iGz0WLwize4w1THHU am4HcgGl0jLdeWBZX82b90cZ/ZV0hjGRIqX4EodgWgOewK8p1fAYJ8ss081a9yXhLbpT dX1Dy5AnPqYdv+S//tziud4fFpUbHQh30BmNElM1oNNJ7IQTFzf3GfHSrzEZqxmSYoyU h9e/9tExY8CfQfuJy4vzCxo4mq3e7QHsiiOeoS5QyYL7sNo+F/uwPxmkE5iuuxQLb9eD p03Q== X-Forwarded-Encrypted: i=1; AJvYcCUcrMgDzCgQYGvUwuLY0NHqVq6VRkc28Uh9tKJmoWyHf3yAgnrrHKYNGHAt9kRJu1oZbcl0mOnMcnkwMJ7Zx/4sf08ZyrTkYGlu X-Gm-Message-State: AOJu0YwsEbcXxkc8KJLSOvJ6WWVkNlWoPN79pFnaQLaJe4K5COLAdj4u S/NVvgo42ZlxrrIqeOGQ3YKkcbe5zhznQhlpL0OgG4WKrsr55EE923MpZLWuxIvg+9PgbKNykpR P X-Google-Smtp-Source: AGHT+IFOosLEfbUYy+rk3DgRd8S4qUKsWD9x8eVobALgVUhAo3EcRpZRN6epvvOoKaA1rXEtmh1uXA== X-Received: by 2002:ac8:6f0b:0:b0:447:ea03:454b with SMTP id d75a77b69052e-4535bbc9e18mr41862931cf.51.1723670765231; Wed, 14 Aug 2024 14:26:05 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45369ff3465sm558691cf.31.2024.08.14.14.26.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:04 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 01/16] fanotify: don't skip extra event info if no info_mode is set Date: Wed, 14 Aug 2024 17:25:19 -0400 Message-ID: <6a659625a0d08fae894cc47352453a6be2579788.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 New pre-content events will be path events but they will also carry additional range information. Remove the optimization to skip checking whether info structures need to be generated for path events. This results in no change in generated info structures for existing events. Signed-off-by: Josef Bacik --- fs/notify/fanotify/fanotify_user.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 9ec313e9f6e1..2e2fba8a9d20 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -160,9 +160,6 @@ static size_t fanotify_event_len(unsigned int info_mode, int fh_len; int dot_len = 0; - if (!info_mode) - return event_len; - if (fanotify_is_error_event(event->mask)) event_len += FANOTIFY_ERROR_INFO_LEN; @@ -740,12 +737,10 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, if (fanotify_is_perm_event(event->mask)) FANOTIFY_PERM(event)->fd = fd; - if (info_mode) { - ret = copy_info_records_to_user(event, info, info_mode, pidfd, - buf, count); - if (ret < 0) - goto out_close_fd; - } + ret = copy_info_records_to_user(event, info, info_mode, pidfd, + buf, count); + if (ret < 0) + goto out_close_fd; if (f) fd_install(fd, f); From patchwork Wed Aug 14 21:25:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764156 Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 55C981C8231 for ; Wed, 14 Aug 2024 21:26:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670769; cv=none; b=HADb0uq/Y30g45DcR8wrrycOoLfJ6nsyzqj0kckJFVPuqskJxCF6pkOPRz9p94xkbaBJ14jIt5Ms7uteV2WAv8OOxVc6MAoSn8GnHym/8L/MJJJtd2FF3Ry3F8RUNRpz2gu4AdFBXZy+8QkLQlbyaAMuxN1+T6hziVLVy5zPp2Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670769; c=relaxed/simple; bh=Xn48LHyloGlt4wSB8hOBR2UrfV1ArFm69qZkTTQGfyU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XNRmKJ6GtX+XTzdxlIfoGNveKN5gQGoSHXRkN12WBWs2cn63DZoiqmA1MPXObb+3ZkGM0UJpj4lsb+qW7gS419WTZFKKC/oZZSyPGaIbQVVHrvbBKKtUHNV/yNy0IPmyGEnr58vA350bFlMpqr5Pr4LHuzlyQVpISuN3SzOzDlc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=En3n5z8I; arc=none smtp.client-ip=209.85.219.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="En3n5z8I" Received: by mail-qv1-f52.google.com with SMTP id 6a1803df08f44-6b79293a858so1523506d6.3 for ; Wed, 14 Aug 2024 14:26:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670766; x=1724275566; darn=vger.kernel.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=G5YjJnCjztcen/Z+X5lHX0n7xhNQ/QripYlvqYqvblA=; b=En3n5z8I/v4+3hgSGzRIxA92hiAMAdP1sNFxjipj71+sP/7he2nH+uSYmm77Rwwu2J JewkBvVTlCAsBMO2WipkCD9rtjYbiFqsy0R9fC3EVA17bB96kiJvw4jZNVvWSXaE7Vtd X2YwYyGP/VYTfJsuJoKwWhpIuSomrcXAXioFTZpkXJzH13UqjsQhoZRSNffv/ZpvAqja GsArWLu9B1eTG8b5MSOKedQplrDnnP12P43Ihtyc5pTaXS+Y9j7/mbMqSb0t2L2crA1e LALVbU5Jc9gQ+ylcKJBfCPL0Zy6JSDaklSN5VpF47f+cbiKgWt7FEpmWNW1E4+g9Qhyy N+ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670766; x=1724275566; 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=G5YjJnCjztcen/Z+X5lHX0n7xhNQ/QripYlvqYqvblA=; b=ILZZb+m4gJUzjd1cDdZP0/7fvP6jhH8NixT69U+cB9Col0MyRWAd/KYjPhviW87+CR 7q75TiUZOhbgjj8cs/xZW6z6LTJUpnuQj5JI6ODUcbUotsfsVIYP5Tm3vYZU8TmMs0TD VReXCe/x1xb386WziMP6mSUOeWOnTTEAjos4WobDcmZltLy6Fh5BR9BjWke+NGZbBtv/ VbJ1Pu6f54mBHYSC8VYp8u5SIFr79VOD+wWxOXWgw0AhkZ9LwPGI4C42SJAmZMX4Mga3 5nIz1m4erYT2o9vMB1Ar0xXFsQH5LjCUN7zpn0eWWWPZxnxnmJlpSVLW3h9DE/tyIGc5 axRg== X-Forwarded-Encrypted: i=1; AJvYcCXIy8d+RRm3kmrE9pNpCtPhnEsJJNkp8Te5Bam/whtNkpuhUlGTU2SvGYTTvQoHnhvrzzaPc//X9FWHkwYRLmmObkmUsUwW4GxE X-Gm-Message-State: AOJu0YzE6icvziUZBDzYDRjQ5ZKtBYLaRXhcslhG0XwC/EG+0MSyUWJ4 7o96Ko/Nheb9vTFCxAZLMO7Ok/ybqorcUl9yCNnnf7gBUEI+IitZXVGOzsIvmyA= X-Google-Smtp-Source: AGHT+IEJ45kMxMd9GAYKpZ0U1aW3kAHT2F3+5BvHJo7NIEX4yhQ21Vvr3YILDZWxA0MQ1vkSqNpnkg== X-Received: by 2002:ad4:53cb:0:b0:6bf:6604:c876 with SMTP id 6a1803df08f44-6bf6604cca1mr24849216d6.32.1723670766260; Wed, 14 Aug 2024 14:26:06 -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-6bf6fe25cf5sm632276d6.58.2024.08.14.14.26.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:05 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 02/16] fsnotify: introduce pre-content permission event Date: Wed, 14 Aug 2024 17:25:20 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein The new FS_PRE_ACCESS permission event is similar to FS_ACCESS_PERM, but it meant for a different use case of filling file content before access to a file range, so it has slightly different semantics. Generate FS_PRE_ACCESS/FS_ACCESS_PERM as two seperate events, same as we did for FS_OPEN_PERM/FS_OPEN_EXEC_PERM. FS_PRE_MODIFY is a new permission event, with similar semantics as FS_PRE_ACCESS, which is called before a file is modified. FS_ACCESS_PERM is reported also on blockdev and pipes, but the new pre-content events are only reported for regular files and dirs. The pre-content events are meant to be used by hierarchical storage managers that want to fill the content of files on first access. Signed-off-by: Amir Goldstein --- fs/notify/fsnotify.c | 2 +- include/linux/fsnotify.h | 27 ++++++++++++++++++++++++--- include/linux/fsnotify_backend.h | 13 +++++++++++-- security/selinux/hooks.c | 3 ++- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 272c8a1dab3c..1ca4a8da7f29 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -621,7 +621,7 @@ static __init int fsnotify_init(void) { int ret; - BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 23); + BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25); ret = init_srcu_struct(&fsnotify_mark_srcu); if (ret) diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index 278620e063ab..7600a0c045ba 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -133,12 +133,13 @@ static inline int fsnotify_file(struct file *file, __u32 mask) #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS /* - * fsnotify_file_area_perm - permission hook before access to file range + * fsnotify_file_area_perm - permission hook before access/modify of file range */ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, const loff_t *ppos, size_t count) { - __u32 fsnotify_mask = FS_ACCESS_PERM; + struct inode *inode = file_inode(file); + __u32 fsnotify_mask; /* * filesystem may be modified in the context of permission events @@ -147,7 +148,27 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, */ lockdep_assert_once(file_write_not_started(file)); - if (!(perm_mask & MAY_READ)) + /* + * Generate FS_PRE_ACCESS/FS_ACCESS_PERM as two seperate events. + */ + if (perm_mask & MAY_READ) { + int ret = fsnotify_file(file, FS_ACCESS_PERM); + + if (ret) + return ret; + } + + /* + * Pre-content events are only reported for regular files and dirs. + */ + if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)) + return 0; + + if (perm_mask & MAY_WRITE) + fsnotify_mask = FS_PRE_MODIFY; + else if (perm_mask & (MAY_READ | MAY_ACCESS)) + fsnotify_mask = FS_PRE_ACCESS; + else return 0; return fsnotify_file(file, fsnotify_mask); diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 8be029bc50b1..200a5e3b1cd4 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -56,6 +56,9 @@ #define FS_ACCESS_PERM 0x00020000 /* access event in a permissions hook */ #define FS_OPEN_EXEC_PERM 0x00040000 /* open/exec event in a permission hook */ +#define FS_PRE_ACCESS 0x00080000 /* Pre-content access hook */ +#define FS_PRE_MODIFY 0x00100000 /* Pre-content modify hook */ + /* * Set on inode mark that cares about things that happen to its children. * Always set for dnotify and inotify. @@ -77,8 +80,14 @@ */ #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME) -#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \ - FS_OPEN_EXEC_PERM) +/* Content events can be used to inspect file content */ +#define FSNOTIFY_CONTENT_PERM_EVENTS (FS_OPEN_PERM | FS_OPEN_EXEC_PERM | \ + FS_ACCESS_PERM) +/* Pre-content events can be used to fill file content */ +#define FSNOTIFY_PRE_CONTENT_EVENTS (FS_PRE_ACCESS | FS_PRE_MODIFY) + +#define ALL_FSNOTIFY_PERM_EVENTS (FSNOTIFY_CONTENT_PERM_EVENTS | \ + FSNOTIFY_PRE_CONTENT_EVENTS) /* * This is a list of all events that may get sent to a parent that is watching diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 55c78c318ccd..2997edf3e7cd 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -3406,7 +3406,8 @@ static int selinux_path_notify(const struct path *path, u64 mask, perm |= FILE__WATCH_WITH_PERM; /* watches on read-like events need the file:watch_reads permission */ - if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE)) + if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_PRE_ACCESS | + FS_CLOSE_NOWRITE)) perm |= FILE__WATCH_READS; return path_has_perm(current_cred(), path, perm); From patchwork Wed Aug 14 21:25:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764158 Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C2FB1C8248 for ; Wed, 14 Aug 2024 21:26:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670771; cv=none; b=fZkrwnFiLf8UxdAzp0M4Mf9EK0r7g+G7BX0DgoJf4CQmmrsotneaqs4uNJb/GtTQ5e1nqthySCcj39JJgy+VwtnWaS++HeWB9ePEfoFspnm4c09bPAyot7/en8Ea57gptLh2W8dmmEb0l9C0uABk9xefaPFrp+fLzLF2w8KoyzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670771; c=relaxed/simple; bh=KQKrHptqPFx5UnDyLAwlPOx0/0+4MigGJ4Sd0QKK9Tg=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=M2/tF2ecH7xRwZ6yHvuPdW9i1ggZH9zrAYP4JEDivROweGHyh5tUWswFAI4gfaupBBpasnqqUZSyFXEenUY6lFcgdddwSysj5Rc/IIRAra5rvWpDV6/De1nSfkrTPBTCjWESFnfZfVCSuGobdBopcFit4I1NivvofHUT+5q+aPc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=Z0HWOMMg; arc=none smtp.client-ip=209.85.222.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="Z0HWOMMg" Received: by mail-qk1-f169.google.com with SMTP id af79cd13be357-7a1d024f775so19549185a.2 for ; Wed, 14 Aug 2024 14:26:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670767; x=1724275567; darn=vger.kernel.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=jN13mVwJhuqjX4PmJ7GJ6BtR6lc23krv9oFHMk0R8ZY=; b=Z0HWOMMgOrSgPVSZOBi/9uBHz4Tfa36G4QNcdn+g7fc3uhcaQPXtqfAUMOFmQKI+hK SKdkrH4sln7u/TQnIHVz3/n8RNUk9wbfZvp6pU0O8jjTC0qvNPiYAR1Hm4ixpDLyPsPr v9RzOXNN31RxWtrAEWx0x1yZWXucvQV9bxQHhSjCdhBlkc3+ic+XC8xJBl1VMt67AHUX ZyP2ey0a0j/U6vhR971S9Y8/DgXoXiVbSN5DlOgxc+0v/Imu4o2xIxxhklU8Yh/pZ/bH d6+bvFko1cME3EFyr7CqLu7AIWttktPhY/UhWVjt5OwKQ8XE6HhDOgb41rUN+Hs3B7EW tLHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670767; x=1724275567; 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=jN13mVwJhuqjX4PmJ7GJ6BtR6lc23krv9oFHMk0R8ZY=; b=MpcS7qsHNdTm2Oeqk1SFmPdyEgPoBWNvWMZeK7Eyr2zYonjGhmRxccyXxRt9uRMJ4i VkOtLJ33ZIS9Ny6ZvKiGe6wUu5j9UsMzoFUf8W9gqwTDXnG2lnNIAMOXNnmnfDDwhP3l wWbduQyTkrQTgrycfESFU70i2QgmvsokI+dAv3kBLvVOIVRu8YRBiDDWC62+l5O4/SYJ oYmRdqywbCiMNd3e/k1M86jqV2NX5V/MT90B+MigVme548tvPmGcYkBV7UYMVshgGuUh /MRwZ0p7+vEatO2Vz5Elf/Rtvtxz95K6XCUok85prE6QDI5nRAgvf7XQSlvtqlV/dRFp ka/g== X-Forwarded-Encrypted: i=1; AJvYcCWLYKN1JKgueEH0TSLjGRcTGZx5WdCmF0ePiKFBHh1Z0Sn1PmBGM907kLb45tGXnzPmUE+CMUHygVBiIGhlMQnzNontthWN1Nc5 X-Gm-Message-State: AOJu0Yw1ttF+dvo/g6g3EJ8R/4y94ifhaStdEKGNWbhAcWIzLBWpcUs5 WJLTUpk7D5XyJpHknwDU4sL7Mgg7/3+9tjdxlaAoUyFOIIA1wMJLDHQ7d2WAYp0= X-Google-Smtp-Source: AGHT+IEOAxo/YLBU2zsyh9BoruPZGvcW7AnOYidoJyHePzbDbTVUapd9HA/DiVKweRuuQtAwKoLbLg== X-Received: by 2002:a05:620a:4506:b0:7a3:4fb7:5c77 with SMTP id af79cd13be357-7a4ee3187dcmr432377385a.11.1723670767459; Wed, 14 Aug 2024 14:26:07 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a4ff04830fsm8695885a.30.2024.08.14.14.26.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:07 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 03/16] fsnotify: generate pre-content permission event on open Date: Wed, 14 Aug 2024 17:25:21 -0400 Message-ID: <4b235bf62c99f1f1196edc9da4258167314dc3c3.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein FS_PRE_ACCESS or FS_PRE_MODIFY will be generated on open depending on file open mode. The pre-content event will be generated in addition to FS_OPEN_PERM, but without sb_writers held and after file was truncated in case file was opened with O_CREAT and/or O_TRUNC. The event will have a range info of (0..0) to provide an opportunity to fill entire file content on open. Signed-off-by: Amir Goldstein Reviewed-by: Christian Brauner --- fs/namei.c | 9 +++++++++ include/linux/fsnotify.h | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/fs/namei.c b/fs/namei.c index 3a4c40e12f78..c16487e3742d 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3735,6 +3735,15 @@ static int do_open(struct nameidata *nd, } if (do_truncate) mnt_drop_write(nd->path.mnt); + + /* + * This permission hook is different than fsnotify_open_perm() hook. + * This is a pre-content hook that is called without sb_writers held + * and after the file was truncated. + */ + if (!error) + error = fsnotify_file_perm(file, MAY_OPEN); + return error; } diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index 7600a0c045ba..fb3837b8de4c 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -168,6 +168,10 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, fsnotify_mask = FS_PRE_MODIFY; else if (perm_mask & (MAY_READ | MAY_ACCESS)) fsnotify_mask = FS_PRE_ACCESS; + else if (perm_mask & MAY_OPEN && file->f_mode & FMODE_WRITER) + fsnotify_mask = FS_PRE_MODIFY; + else if (perm_mask & MAY_OPEN) + fsnotify_mask = FS_PRE_ACCESS; else return 0; @@ -176,10 +180,14 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, /* * fsnotify_file_perm - permission hook before file access + * + * Called from read()/write() with perm_mask MAY_READ/MAY_WRITE. + * Called from open() with MAY_OPEN without sb_writers held and after the file + * was truncated. Note that this is a different event from fsnotify_open_perm(). */ static inline int fsnotify_file_perm(struct file *file, int perm_mask) { - return fsnotify_file_area_perm(file, perm_mask, NULL, 0); + return fsnotify_file_area_perm(file, perm_mask, &file->f_pos, 0); } /* From patchwork Wed Aug 14 21:25:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764157 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D9C61C8FA9 for ; Wed, 14 Aug 2024 21:26:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670771; cv=none; b=oDKZtLj1FWP87HgIx1csvcW1kUPQYEgcIjSbSQWv5VgwHThAUVm8jwmL2LZxewEtE9s1XQ/fhONQWt6R3KqYpCoUen/H20tSRpvn3H0zqR03M5u1UJpO/fE6nKxMd3eDPM3jmwcdZQMuMY9WH2pXkPbysF1ScPragKOqB3lKT08= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670771; c=relaxed/simple; bh=205ZgkeQOGfC9lat+UQrPYYg3l/LDds2VjsCRxYzGRM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QhelaJWObWcRCZFvhuIXA5l72fLN6djRCeyeD85jbe0H4lyZPZso+OHinhCwPWVh5uGaZrJSy5zf5vYxE/uJfhQWMROXq+mToDMOzoC0lQ52t818/YxWmZ/RhP5fVe1XiiHYGKMgCOaOeOGAdd55v2WAgJ/TvRizPW5YLcfvMuo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=Uq3xbV2h; arc=none smtp.client-ip=209.85.222.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="Uq3xbV2h" Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-7a1d6f47112so19426585a.0 for ; Wed, 14 Aug 2024 14:26:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670768; x=1724275568; darn=vger.kernel.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=wY5GQe96iyZHYx9I8f9pL6l7vs2y5u4HNtMRHVuMKgs=; b=Uq3xbV2hb6jw1I8O6NtxYaOSnK9/CtYMM+DeUVCcEE2Lg1ZGRmfmNDNaMcboXZ0ZSo /I8SBn4sCdJwA8WVja3wb/Km+28OKQs2rvn1bQ5FOfH9qd6gAU1t/uvfL67MVwPtMfxx y7uZ52Lrtm9IzAbCrd09wSt/dWiT9n9UaKPiM/kTLHOzeTJd/L8mXsfQwaHmpr1ZtD0I e8whAFg6vFn4UojIl64T6O49YtDgCATUmhVecfV6+dbgFKVxw5aaFjLPJeHXGLgTXFcx DZ6sHPYlljozDrltG9/XSwN7OFMm+BbvinEJJl8+qxqBuCFJnH4xEJ+HPYQ3VN4td7Au K57g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670768; x=1724275568; 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=wY5GQe96iyZHYx9I8f9pL6l7vs2y5u4HNtMRHVuMKgs=; b=nABQB9lGzTa4DdioUUyIJCUsCgKQmh5GnQyNAX9e01/aBcIyVwgKeNLzX599y8pm0L vKtGJDkUbgkr6IE67+N6nBv30SfkSgZdhUOXaIK+S+oweVPbehTgRhpNTvt9PMIfqV+f yfANC2lyCNnAPNddDiQCGNlk2me17qyqwqErtSUVolLkpvMeEluG/Qh59c2r5kpcoSGY bqpzpmAALpavmEEv0GSh8SU1TXXxR5ENvmzFyOUj9pomi76z6MUF/mmGuq0kH/9F7okO n26rcrlCP7RalHAWczjlJgHclTVQ37akNK6aXFZHgAKQSrYwlgbVFbgYbWPK2T05Ve4W anLg== X-Forwarded-Encrypted: i=1; AJvYcCU7V8IuBJh+FHfbEenLpDGJUk0wU9e2fSSezkhPcwXgxQ6IGihouz6cL2tt3mKRm5/d8TBaFCQIzjbgkUE1vdQjjRXOKApkA1G/ X-Gm-Message-State: AOJu0YyrI1WPwjTK+4DSi69g5yIVslggJx99i5uGhw4yA8QCVyBJDlGZ BQqvtHWWBMhgmgoxrna0UUFpHX6YxDtK191O4bm9JghTxiH6d05zQBICpQIo8CDq7xoDpP+4WGn 4 X-Google-Smtp-Source: AGHT+IFb4WPvIe9b0H0MB5ZFX64Xi4uEtDDjQyEK/diGPUoztnRQsa44qKf+dfrF4BFbTEpCYFi7wg== X-Received: by 2002:ac8:698e:0:b0:453:5f05:2ba3 with SMTP id d75a77b69052e-4535f052d3fmr26573931cf.8.1723670768575; Wed, 14 Aug 2024 14:26:08 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4536a07916bsm473631cf.94.2024.08.14.14.26.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:08 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 04/16] fanotify: introduce FAN_PRE_ACCESS permission event Date: Wed, 14 Aug 2024 17:25:22 -0400 Message-ID: <19cb8b3b5b93760bc6c6d47a8cbcd277952d7006.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein Similar to FAN_ACCESS_PERM permission event, but it is only allowed with class FAN_CLASS_PRE_CONTENT and only allowed on regular files and dirs. Unlike FAN_ACCESS_PERM, it is safe to write to the file being accessed in the context of the event handler. This pre-content event is meant to be used by hierarchical storage managers that want to fill the content of files on first read access. Signed-off-by: Amir Goldstein Reviewed-by: Christian Brauner --- fs/notify/fanotify/fanotify.c | 3 ++- fs/notify/fanotify/fanotify_user.c | 17 ++++++++++++++--- include/linux/fanotify.h | 14 ++++++++++---- include/uapi/linux/fanotify.h | 2 ++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 224bccaab4cc..7dac8e4486df 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -910,8 +910,9 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask, BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM); BUILD_BUG_ON(FAN_FS_ERROR != FS_ERROR); BUILD_BUG_ON(FAN_RENAME != FS_RENAME); + BUILD_BUG_ON(FAN_PRE_ACCESS != FS_PRE_ACCESS); - BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 21); + BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 22); mask = fanotify_group_event_mask(group, iter_info, &match_mask, mask, data, data_type, dir); diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 2e2fba8a9d20..c294849e474f 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1628,6 +1628,7 @@ static int fanotify_events_supported(struct fsnotify_group *group, unsigned int flags) { unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; + bool is_dir = d_is_dir(path->dentry); /* Strict validation of events in non-dir inode mask with v5.17+ APIs */ bool strict_dir_events = FAN_GROUP_FLAG(group, FAN_REPORT_TARGET_FID) || (mask & FAN_RENAME) || @@ -1665,9 +1666,15 @@ static int fanotify_events_supported(struct fsnotify_group *group, * but because we always allowed it, error only when using new APIs. */ if (strict_dir_events && mark_type == FAN_MARK_INODE && - !d_is_dir(path->dentry) && (mask & FANOTIFY_DIRONLY_EVENT_BITS)) + !is_dir && (mask & FANOTIFY_DIRONLY_EVENT_BITS)) return -ENOTDIR; + /* Pre-content events are only supported on regular files and dirs */ + if (mask & FANOTIFY_PRE_CONTENT_EVENTS) { + if (!is_dir && !d_is_reg(path->dentry)) + return -EINVAL; + } + return 0; } @@ -1769,11 +1776,15 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, goto fput_and_out; /* - * Permission events require minimum priority FAN_CLASS_CONTENT. + * Permission events are not allowed for FAN_CLASS_NOTIF. + * Pre-content permission events are not allowed for FAN_CLASS_CONTENT. */ ret = -EINVAL; if (mask & FANOTIFY_PERM_EVENTS && - group->priority < FSNOTIFY_PRIO_CONTENT) + group->priority == FSNOTIFY_PRIO_NORMAL) + goto fput_and_out; + else if (mask & FANOTIFY_PRE_CONTENT_EVENTS && + group->priority == FSNOTIFY_PRIO_CONTENT) goto fput_and_out; if (mask & FAN_FS_ERROR && diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index 4f1c4f603118..5c811baf44d2 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -88,6 +88,16 @@ #define FANOTIFY_DIRENT_EVENTS (FAN_MOVE | FAN_CREATE | FAN_DELETE | \ FAN_RENAME) +/* Content events can be used to inspect file content */ +#define FANOTIFY_CONTENT_PERM_EVENTS (FAN_OPEN_PERM | FAN_OPEN_EXEC_PERM | \ + FAN_ACCESS_PERM) +/* Pre-content events can be used to fill file content */ +#define FANOTIFY_PRE_CONTENT_EVENTS (FAN_PRE_ACCESS) + +/* Events that require a permission response from user */ +#define FANOTIFY_PERM_EVENTS (FANOTIFY_CONTENT_PERM_EVENTS | \ + FANOTIFY_PRE_CONTENT_EVENTS) + /* Events that can be reported with event->fd */ #define FANOTIFY_FD_EVENTS (FANOTIFY_PATH_EVENTS | FANOTIFY_PERM_EVENTS) @@ -103,10 +113,6 @@ FANOTIFY_INODE_EVENTS | \ FANOTIFY_ERROR_EVENTS) -/* Events that require a permission response from user */ -#define FANOTIFY_PERM_EVENTS (FAN_OPEN_PERM | FAN_ACCESS_PERM | \ - FAN_OPEN_EXEC_PERM) - /* Extra flags that may be reported with event or control handling of events */ #define FANOTIFY_EVENT_FLAGS (FAN_EVENT_ON_CHILD | FAN_ONDIR) diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index a37de58ca571..bcada21a3a2e 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -26,6 +26,8 @@ #define FAN_ACCESS_PERM 0x00020000 /* File accessed in perm check */ #define FAN_OPEN_EXEC_PERM 0x00040000 /* File open/exec in perm check */ +#define FAN_PRE_ACCESS 0x00080000 /* Pre-content access hook */ + #define FAN_EVENT_ON_CHILD 0x08000000 /* Interested in child events */ #define FAN_RENAME 0x10000000 /* File was renamed */ From patchwork Wed Aug 14 21:25:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764159 Received: from mail-oo1-f46.google.com (mail-oo1-f46.google.com [209.85.161.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BB691C8FC3 for ; Wed, 14 Aug 2024 21:26:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670772; cv=none; b=e5R27j7/ilo9kjTjOzwm59rctgm4rsTEHVjoEURcX/XhG/yN7UJZvA4u2pYrLevURpRLx1moAv7basnqKIsAG/DVeqM1+gKECby9Hf05UTG25XSUHO0Vj+oXYbmzp6bW1By7kRCQ3FxiOud7FhFEHdq1+1WOq7J8IWn4FPmjHao= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670772; c=relaxed/simple; bh=pD+LVjuiFGqxiYXNcE7SFfWih4a6NzbE/J64a1HUTSE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ztbqii1T0yN/qdDbzlIYjAGU+aFh5rke/FEED5OGRCDouy3OD7b3RHuAXYpdcwUvZM71kDY5w0o2sq5/dMe9hHMgAc3GuXKvDwueRaNM8NHrwuf1LnueSWA2lURt6ADJRqwhkVrMant2fNf1eje5ohtz4rdvMojiRek4stnNinA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=dwhmid1F; arc=none smtp.client-ip=209.85.161.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="dwhmid1F" Received: by mail-oo1-f46.google.com with SMTP id 006d021491bc7-5d5d0535396so187560eaf.1 for ; Wed, 14 Aug 2024 14:26:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670769; x=1724275569; darn=vger.kernel.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=03GJ1k40z8a5Psi/RqpgoqHfW5s1FJwp4eQtJBRMqtc=; b=dwhmid1FiaJqaBZR/k5Rr3PyIAu+zVraohw0tWiYQwxLa5ILvudOESc5h8YYkc1d0w hqZVyMXa9ShT9cTtpFg9u3OR8k5PmSwC3SSrrgRRbixjo+2suCr71dh7Wa22+zO+N6Hy 5uQw0oq1zGzcpv9QhO+I7KzxhfZ+AIlPiiIOCH6aTP/YoCksC3Z02IdEFv5SOYYcTpOa QdLbn/7cOuaqlq5Pow47NfzzcTGWtE5fPHYI/sL5/JZAw3P8uF7kPMij2/qDWdDH85Rk 0IZRYTcG7vlasQn8S5b0swv/qKTIxN+cedz+I+RMoi+Xa2VFQF+GNMaBgtauniDsu3U/ bIkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670769; x=1724275569; 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=03GJ1k40z8a5Psi/RqpgoqHfW5s1FJwp4eQtJBRMqtc=; b=ohhFqrC0wKcDVdUb/I3B/fzVNbuxGEU7nXwNqnd31kF6d+0bM3cPjVL0RkaeFFBWjO YeP8pLWh96pdSczWO/OAN+PtrVRiEfeHQZP54n+tjwSmJOBvJRQ8QEoWpPkDHPazM0TX 4Iuulcs9pXDqZlnV1C3129dc4PqAlE4TV5AWrIYkX/74EJXXH7/ehmMPLLrADdI3QmJe fU7jf9/0Hh0H5HEq2qV42dpcwRlB3ioJ5V2vWit2kYA2qTTdklQz+5VxQS2GrDpHSCUh M8nhn+RKpXuKVyDPP6GgzBrwCKdbn1n55jD1emGWtxOhJyz+G8NEc8iUWo0bTGUcdKGu RwAw== X-Forwarded-Encrypted: i=1; AJvYcCVDorcWxSK0QLZbyrU82+h88EHg3dBZlAT6Nn+sEZUYGxDvVnpqLiJsSRl0Z8g5LsCa6SaKI9EWjdg4kSNXtbHjMK9y0bXapvsy X-Gm-Message-State: AOJu0YzTdZ0k3x91XavqyANpzEIzWiOwe/31KJkhZO8jAd7w2MIYiJ2+ VbN6OHt8T9eEZVcQRO7XiM7AUh6SPDLjSNiANUrcmTzAgC1+rMLUusJKuWQI70Q= X-Google-Smtp-Source: AGHT+IHTmjx53oOy4ZaSkJClqvqNoIclprOHFm1rhknp6QMuI7g4a2JjrNNw/WvyavtTdJbWVmGPzw== X-Received: by 2002:a05:6218:2612:b0:1af:7ea6:c95e with SMTP id e5c5f4694b2df-1b1aab1fe35mr483092455d.4.1723670769562; Wed, 14 Aug 2024 14:26:09 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a4ff02b136sm8804385a.25.2024.08.14.14.26.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:09 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 05/16] fanotify: introduce FAN_PRE_MODIFY permission event Date: Wed, 14 Aug 2024 17:25:23 -0400 Message-ID: <5142d1715dfecf58bc0a77eb410ca21d95e71cfc.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein Generate FAN_PRE_MODIFY permission event from fsnotify_file_perm() pre-write hook to notify fanotify listeners on an intent to make modification to a file. Like FAN_PRE_ACCESS, it is only allowed with FAN_CLASS_PRE_CONTENT and unlike FAN_MODIFY, it is only allowed on regular files. Like FAN_PRE_ACCESS, it is generated without sb_start_write() held, so it is safe to perform filesystem modifications in the context of event handler. This pre-content event is meant to be used by hierarchical storage managers that want to fill the content of files on first write access. Signed-off-by: Amir Goldstein Reviewed-by: Christian Brauner --- fs/notify/fanotify/fanotify.c | 3 ++- fs/notify/fanotify/fanotify_user.c | 2 ++ include/linux/fanotify.h | 3 ++- include/uapi/linux/fanotify.h | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 7dac8e4486df..b163594843f5 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -911,8 +911,9 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask, BUILD_BUG_ON(FAN_FS_ERROR != FS_ERROR); BUILD_BUG_ON(FAN_RENAME != FS_RENAME); BUILD_BUG_ON(FAN_PRE_ACCESS != FS_PRE_ACCESS); + BUILD_BUG_ON(FAN_PRE_MODIFY != FS_PRE_MODIFY); - BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 22); + BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 23); mask = fanotify_group_event_mask(group, iter_info, &match_mask, mask, data, data_type, dir); diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index c294849e474f..3a7101544f30 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1673,6 +1673,8 @@ static int fanotify_events_supported(struct fsnotify_group *group, if (mask & FANOTIFY_PRE_CONTENT_EVENTS) { if (!is_dir && !d_is_reg(path->dentry)) return -EINVAL; + if (is_dir && mask & FAN_PRE_MODIFY) + return -EISDIR; } return 0; diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index 5c811baf44d2..ae6cb2688d52 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -92,7 +92,8 @@ #define FANOTIFY_CONTENT_PERM_EVENTS (FAN_OPEN_PERM | FAN_OPEN_EXEC_PERM | \ FAN_ACCESS_PERM) /* Pre-content events can be used to fill file content */ -#define FANOTIFY_PRE_CONTENT_EVENTS (FAN_PRE_ACCESS) +#define FANOTIFY_PRE_CONTENT_EVENTS (FAN_PRE_ACCESS | FAN_PRE_MODIFY) +#define FANOTIFY_PRE_MODIFY_EVENTS (FAN_PRE_MODIFY) /* Events that require a permission response from user */ #define FANOTIFY_PERM_EVENTS (FANOTIFY_CONTENT_PERM_EVENTS | \ diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index bcada21a3a2e..ac00fad66416 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -27,6 +27,7 @@ #define FAN_OPEN_EXEC_PERM 0x00040000 /* File open/exec in perm check */ #define FAN_PRE_ACCESS 0x00080000 /* Pre-content access hook */ +#define FAN_PRE_MODIFY 0x00100000 /* Pre-content modify hook */ #define FAN_EVENT_ON_CHILD 0x08000000 /* Interested in child events */ From patchwork Wed Aug 14 21:25:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764160 Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A2A3C1C9DDE for ; Wed, 14 Aug 2024 21:26:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670773; cv=none; b=mq+Hcyklh941+LDWGN2Ml10Z7nOzCBAZPA1S90xPeqhs3qvJPmaOO+IhFXUKPLcovf3VVIGOFK8KuLLaH29o96MfnaIupKlm+O3Lbf2Q1GzLmufZSLQs91muQRtmLsn4HVuQnR82iAyS+b6Wr7aXFYG3eWk0Jqxv5V+fmgEHqX8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670773; c=relaxed/simple; bh=EUC6xHSolCsXCfEGgJe/uC9iBPwO+UdprLu222bnv+E=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uLfVlVNquRyo44puqVPWPJz8+6njdDi3PKZimxDqiD3HbNG59Q1b7G6Gk/wkIMUNllLYzWaWKKgGRNQAJ8Ah+/4HPfVsSyYKae1TiLfatOjbe/g97p0A1W9VAFB1AHu3eaJiyslPNTL2fAIEEM9O1E3OdwtxJJygc5LImnCMxb8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=zYyn6SrU; arc=none smtp.client-ip=209.85.222.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="zYyn6SrU" Received: by mail-qk1-f176.google.com with SMTP id af79cd13be357-7a1dea79e1aso18241585a.1 for ; Wed, 14 Aug 2024 14:26:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670771; x=1724275571; darn=vger.kernel.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=WSgX58UYc0emFHK3lK4cO5xgsT7V4KpoEDCer0WZNY4=; b=zYyn6SrUtko3MgjsBGZwDxVw95Pl/g7WW4OMaLO3HMwt53ipuH4DWP8HytLiEyQ/W/ ob2DfYilrz+AU3v1dTR43VluuYyg3nogXd8qMBakzQl9JuZIFSlDQidAZJlhaKdhuUtf V9qHSHZDYs6a8jP96wHWxMclO015InqaK5CkfgnLeQS/y1lA08DULcEdgDOK5OimDRXw PDitTJo+MM02pYfn86XMMQCMYQB95Q8drLkWiUP4wkVSRXsZD82N11AOsqPpG1kibfgO dVLkeoygI8WM7EBjVOWuUPOKBttuSNBz3KISoo0Vk3uf/SCTsnVfTXKpaaeWkNtBgTfw +L7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670771; x=1724275571; 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=WSgX58UYc0emFHK3lK4cO5xgsT7V4KpoEDCer0WZNY4=; b=HcHW6eSBJGUzdnsnomrPyYIuvn5MZIdMIT33Qvny7LiSXpGPDEZjMP62e0SX2R9C8J LBGZH8Pr3Gcu/OFK62ado9DWztVdGoIt9c9qtDmvozGWzu9MO8N/MXLod9VlKMhU4C7L +jydJaiSujEDHzcgSJsWEAkmeC7FhzWMhGr0WpNs30qz/+e3JIvA/5lmRedcI5MSxj5D 1ZXtr6+gBTwNwAYFhdX5j7ayys5xZuYWgFAwhmVADpBj1BE0l6Vs+kRetleGi5O7Jyqy q8SagynfBflACmHpoc2+ZKM9WMGqQUxUwdyr6ZE1FAbfX7oTrViKWFhZ6UU6UTvl4N0Q bGmQ== X-Forwarded-Encrypted: i=1; AJvYcCXqOANS1DrhSj+IBbuf4uDqC2EbZQloZBNiybxAm4r13UHOHiHFQmTzW//VBhs1himXC42tmL3D4CyPy1WeUE6VX0Bmhg95krW6 X-Gm-Message-State: AOJu0YyEBuyNKEq0/6sRFhXOfOi9cDy7dZ3FNI5T3MDYMthG8D6ogoFz tQSoFBdEtW7dDIpc3+KAjiac4YdZT4F7/S7/aCjsGDkQSl2SefxjJV5A0o3MpSJKgmgGNvz3rKj m X-Google-Smtp-Source: AGHT+IHzVqdx5PPzNWH057t0P/FT/P3ME1f3o5KEdS47TJSPsPt6bym6sXk9f4FG53pkQP7Q2xC5YA== X-Received: by 2002:a05:620a:294d:b0:7a1:d6e4:d83a with SMTP id af79cd13be357-7a4ee3e82d9mr507957885a.69.1723670770577; Wed, 14 Aug 2024 14:26:10 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a4ff02f89bsm8812485a.23.2024.08.14.14.26.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:10 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 06/16] fanotify: pass optional file access range in pre-content event Date: Wed, 14 Aug 2024 17:25:24 -0400 Message-ID: <87325af81514d7bd0b2236e14c613b7160651bda.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein We would like to add file range information to pre-content events. Pass a struct file_range with optional offset and length to event handler along with pre-content permission event. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 10 ++++++++-- fs/notify/fanotify/fanotify.h | 2 ++ include/linux/fsnotify.h | 17 ++++++++++++++++- include/linux/fsnotify_backend.h | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index b163594843f5..4e8dce39fa8f 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -549,9 +549,13 @@ static struct fanotify_event *fanotify_alloc_path_event(const struct path *path, return &pevent->fae; } -static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path, +static struct fanotify_event *fanotify_alloc_perm_event(const void *data, + int data_type, gfp_t gfp) { + const struct path *path = fsnotify_data_path(data, data_type); + const struct file_range *range = + fsnotify_data_file_range(data, data_type); struct fanotify_perm_event *pevent; pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp); @@ -565,6 +569,8 @@ static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path, pevent->hdr.len = 0; pevent->state = FAN_EVENT_INIT; pevent->path = *path; + pevent->ppos = range ? range->ppos : NULL; + pevent->count = range ? range->count : 0; path_get(path); return &pevent->fae; @@ -802,7 +808,7 @@ static struct fanotify_event *fanotify_alloc_event( old_memcg = set_active_memcg(group->memcg); if (fanotify_is_perm_event(mask)) { - event = fanotify_alloc_perm_event(path, gfp); + event = fanotify_alloc_perm_event(data, data_type, gfp); } else if (fanotify_is_error_event(mask)) { event = fanotify_alloc_error_event(group, fsid, data, data_type, &hash); diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index e5ab33cae6a7..93598b7d5952 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -425,6 +425,8 @@ FANOTIFY_PE(struct fanotify_event *event) struct fanotify_perm_event { struct fanotify_event fae; struct path path; + const loff_t *ppos; /* optional file range info */ + size_t count; u32 response; /* userspace answer to the event */ unsigned short state; /* state of the event */ int fd; /* fd we passed to userspace for this event */ diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index fb3837b8de4c..9d001d328619 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -132,6 +132,21 @@ static inline int fsnotify_file(struct file *file, __u32 mask) } #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS +static inline int fsnotify_file_range(struct file *file, __u32 mask, + const loff_t *ppos, size_t count) +{ + struct file_range range; + + if (file->f_mode & FMODE_NONOTIFY) + return 0; + + range.path = &file->f_path; + range.ppos = ppos; + range.count = count; + return fsnotify_parent(range.path->dentry, mask, &range, + FSNOTIFY_EVENT_FILE_RANGE); +} + /* * fsnotify_file_area_perm - permission hook before access/modify of file range */ @@ -175,7 +190,7 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, else return 0; - return fsnotify_file(file, fsnotify_mask); + return fsnotify_file_range(file, fsnotify_mask, ppos, count); } /* diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 200a5e3b1cd4..276320846bfd 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -298,6 +298,7 @@ static inline void fsnotify_group_assert_locked(struct fsnotify_group *group) /* When calling fsnotify tell it if the data is a path or inode */ enum fsnotify_data_type { FSNOTIFY_EVENT_NONE, + FSNOTIFY_EVENT_FILE_RANGE, FSNOTIFY_EVENT_PATH, FSNOTIFY_EVENT_INODE, FSNOTIFY_EVENT_DENTRY, @@ -310,6 +311,17 @@ struct fs_error_report { struct super_block *sb; }; +struct file_range { + const struct path *path; + const loff_t *ppos; + size_t count; +}; + +static inline const struct path *file_range_path(const struct file_range *range) +{ + return range->path; +} + static inline struct inode *fsnotify_data_inode(const void *data, int data_type) { switch (data_type) { @@ -319,6 +331,8 @@ static inline struct inode *fsnotify_data_inode(const void *data, int data_type) return d_inode(data); case FSNOTIFY_EVENT_PATH: return d_inode(((const struct path *)data)->dentry); + case FSNOTIFY_EVENT_FILE_RANGE: + return d_inode(file_range_path(data)->dentry); case FSNOTIFY_EVENT_ERROR: return ((struct fs_error_report *)data)->inode; default: @@ -334,6 +348,8 @@ static inline struct dentry *fsnotify_data_dentry(const void *data, int data_typ return (struct dentry *)data; case FSNOTIFY_EVENT_PATH: return ((const struct path *)data)->dentry; + case FSNOTIFY_EVENT_FILE_RANGE: + return file_range_path(data)->dentry; default: return NULL; } @@ -345,6 +361,8 @@ static inline const struct path *fsnotify_data_path(const void *data, switch (data_type) { case FSNOTIFY_EVENT_PATH: return data; + case FSNOTIFY_EVENT_FILE_RANGE: + return file_range_path(data); default: return NULL; } @@ -360,6 +378,8 @@ static inline struct super_block *fsnotify_data_sb(const void *data, return ((struct dentry *)data)->d_sb; case FSNOTIFY_EVENT_PATH: return ((const struct path *)data)->dentry->d_sb; + case FSNOTIFY_EVENT_FILE_RANGE: + return file_range_path(data)->dentry->d_sb; case FSNOTIFY_EVENT_ERROR: return ((struct fs_error_report *) data)->sb; default: @@ -379,6 +399,18 @@ static inline struct fs_error_report *fsnotify_data_error_report( } } +static inline const struct file_range *fsnotify_data_file_range( + const void *data, + int data_type) +{ + switch (data_type) { + case FSNOTIFY_EVENT_FILE_RANGE: + return (struct file_range *)data; + default: + return NULL; + } +} + /* * Index to merged marks iterator array that correlates to a type of watch. * The type of watched object can be deduced from the iterator type, but not From patchwork Wed Aug 14 21:25:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764161 Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE3B51C9EA5 for ; Wed, 14 Aug 2024 21:26:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670775; cv=none; b=cM3r3SHLcn8dlh84OUOp8Bu6mbUKN1AQb6n+pDf2zT5YiMIWFwfB+234cZwTMkzMCB4yHaACkeVHJNZicOS8GpdZD5x+Ar2oXablAoR89KtXknFe3DyuEeg6SzgsF8CgHXgX3EQUC7qXDn6+Vdb81wMt5J5wK4+7gHdT4LbeOuk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670775; c=relaxed/simple; bh=qausY9NaTn1Dmt/1fWvpjV6gSoo0TkpL7RjFx1j8ing=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=a1mxHHii3yhm5GJTr8bdWphLw/58tjgNuXayAtDd/G/aFt9fUCLxPRBpjQq3+xqOWlys17JJn7WV1tnaGV4nT9+nor2EzmeebeHqXMzhsL9O93GRgJxPZoW0O5Vn+3Ep5y57ZkWzLH97QksIZ0yVqXlQ/XVwQvl6Tqza3eycdKY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=YiS9UnPP; arc=none smtp.client-ip=209.85.222.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="YiS9UnPP" Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-7a1e4c75488so17569385a.0 for ; Wed, 14 Aug 2024 14:26:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670772; x=1724275572; darn=vger.kernel.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=JmFE9zypkxWwtjgDkL/vbyJWxHrdDWRoGs2W5KUFOnA=; b=YiS9UnPPKE8jnWGziqmRqSMD8JR+3F8Cfq1puGYTeOHW8vtnGMZh7+LpNVqkT3O3HA AsMFdntx5HcquC0awu+cRKOKeaKigl7HyiaKvkwYzaGRoxzY+jtkWmbW1Ge0URFCnc/o QaePfa6pgG3N+L+ebChrbGBQiKkris0BoJbzp69EGmiZAlUZSptaCTDn9jxktBcQmhVM 27ze9fSNZlIGxN6YSLeSmiwQvNKDQ3gER5yzTDgVXbHXtuPoyEPFtQzwEbWhJj7Ponuc qqYJvjOEVMX5z7y3hy4Trhc5ViI78cDAi2zv5wRguWCvy/qJXekxs/YG9Xc9ZraTZp0r TCPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670772; x=1724275572; 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=JmFE9zypkxWwtjgDkL/vbyJWxHrdDWRoGs2W5KUFOnA=; b=XYalZ2JYBUumxjffeByuq5VUGuhw3e7BxBlvmsAprgGhlobvynd8fBYe6x0tkcNec5 q2gt3IYS3BcDfgzlx2HwE6YWnUl39DiMF/OkLyghtDVWgq/0jP5LfAGGKTBTrUxbPz4a CGJyH0d9YDAT2d1kelIylFxk2l1Hho95sGQDizxKh/ro8+x3N9caPoYq+jCIpkXtu33r 31gxSpNincvbSFylVG3PoPU4IOpgdm4a/5Q+5kOS/oWOPRZneZE5jivDIq70mjw6zmlp jYnOG/FMggHjWPwqAJGHjh1Gt8uVEeNM0TJdafXIjOymBhWAhab0wslZCbTrFVhLdZYZ OUWw== X-Forwarded-Encrypted: i=1; AJvYcCWVOpz67MaO5kMslp3pshC1e5YO0n4BHHsosT93F0aXm7de2EXdm5wPKNHTHShrFOZ8mDjY0TM0BRl9fvqO6NI7R4aNZyzS2GB9 X-Gm-Message-State: AOJu0YxBJikSO5FmVyB8gAFroTDrNjo+e6DTGwTKn5fIZgisoZ72ielo fonRQktrZhzJQMdBRMN9agoS+Yv2O/gP2zQO8HhtK1tLbjdEQB/7CgmiX5ySLwQ= X-Google-Smtp-Source: AGHT+IHIPCQoJFD/t0/lClNrUMrlZFIcM/ZiNaxk2Ar4OdyVmABqqGlEDSnusGifSdOunrg99gpoDQ== X-Received: by 2002:a05:620a:1907:b0:79d:76c3:5309 with SMTP id af79cd13be357-7a4ee323d52mr531446985a.4.1723670771708; Wed, 14 Aug 2024 14:26:11 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a4ff1073e5sm7925585a.121.2024.08.14.14.26.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:11 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 07/16] fanotify: rename a misnamed constant Date: Wed, 14 Aug 2024 17:25:25 -0400 Message-ID: <13c1df955c0e8af0aee2afce78b1ea1f2e3f8f66.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein FANOTIFY_PIDFD_INFO_HDR_LEN is not the length of the header. Signed-off-by: Amir Goldstein Reviewed-by: Christian Brauner --- fs/notify/fanotify/fanotify_user.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 3a7101544f30..5ece186d5c50 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -119,7 +119,7 @@ struct kmem_cache *fanotify_perm_event_cachep __ro_after_init; #define FANOTIFY_EVENT_ALIGN 4 #define FANOTIFY_FID_INFO_HDR_LEN \ (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle)) -#define FANOTIFY_PIDFD_INFO_HDR_LEN \ +#define FANOTIFY_PIDFD_INFO_LEN \ sizeof(struct fanotify_event_info_pidfd) #define FANOTIFY_ERROR_INFO_LEN \ (sizeof(struct fanotify_event_info_error)) @@ -174,14 +174,14 @@ static size_t fanotify_event_len(unsigned int info_mode, dot_len = 1; } - if (info_mode & FAN_REPORT_PIDFD) - event_len += FANOTIFY_PIDFD_INFO_HDR_LEN; - if (fanotify_event_has_object_fh(event)) { fh_len = fanotify_event_object_fh_len(event); event_len += fanotify_fid_info_len(fh_len, dot_len); } + if (info_mode & FAN_REPORT_PIDFD) + event_len += FANOTIFY_PIDFD_INFO_LEN; + return event_len; } @@ -511,7 +511,7 @@ static int copy_pidfd_info_to_user(int pidfd, size_t count) { struct fanotify_event_info_pidfd info = { }; - size_t info_len = FANOTIFY_PIDFD_INFO_HDR_LEN; + size_t info_len = FANOTIFY_PIDFD_INFO_LEN; if (WARN_ON_ONCE(info_len > count)) return -EFAULT; From patchwork Wed Aug 14 21:25:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764162 Received: from mail-vk1-f178.google.com (mail-vk1-f178.google.com [209.85.221.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 071CB1C8248 for ; Wed, 14 Aug 2024 21:26:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670775; cv=none; b=RtnUeth5xiIsjLvq653hZIQCQN+Doy3EO3FYZ8d59C+vdnbZ1m0FOqsMnjKEPHoY1ojM3ntFd4r3FJTaaSz/VHZAd+67e1onlsgTdJQYm93s7iVO4lk9tjMb1RrX1+CwKXofzpM7A3S7lETUBq31I9PHQzpmwTBYuZ4C8lL99s0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670775; c=relaxed/simple; bh=x/zxNttmCT+y0dOsXy0tvMYfEvnIhvz+MK7V5Zr+cWQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iY/vVDuXsQCTgSmX+q3+qVoBm9Eni+eOO8MPN9ixEwC4bzaeNRc3W2e/d0ex4XsqyOTGCDGVXgQ02JHdblcwbHm2E4RgJ8y9PPOpA4PwXRaUD4X5IKatgpvU2bgpp6+TQtLwZj5fw3AssKanKrUJ/DEo9QujGa7UVwoJh2y3p3U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=en+qEQiO; arc=none smtp.client-ip=209.85.221.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="en+qEQiO" Received: by mail-vk1-f178.google.com with SMTP id 71dfb90a1353d-4f6b612fad4so122109e0c.0 for ; Wed, 14 Aug 2024 14:26:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670773; x=1724275573; darn=vger.kernel.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=c/Hz6ZhLh0CWwWHWSWJMj1KvT9KtOfMKuwzMM3GJFXM=; b=en+qEQiOETr3SYLmlzGaTW8td+fupQSJfA5ALa/ZpPCgoL/KymuDIrrW4ZL3ZM8RKs Y6BZf90ZpT9z4gUXfQEisEoMZOp8xeMLNYM7WDZAyo2HJ/2H5ZA25T20uH5yQhyCJoxL E22rBWBqqUpE7U565sQuVT6sL7GWEuIFmS4pdJinGK4zbHJci8OR9L+0/9SujHvHXjrU V7oJlPqe/pZspkoGYu1Bofrskkb1ZFoxJP34D7rqrB32Li32R1N/SuIa3/1PCwxeayil l186EMeikpBxDEcF3zrXa2J8pxBIOcK6nygtCvCEa9KJ0KyrJruQjesS/Gh1/Rgg5MjQ FE4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670773; x=1724275573; 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=c/Hz6ZhLh0CWwWHWSWJMj1KvT9KtOfMKuwzMM3GJFXM=; b=KWU0Pz0B2pe1LoeSmU0tzP5/TxWZNer3Iqp0/Fzj9PdMXlbUl0qVSbxhmOZca9GNSP 3hsgm3o5GAFdTxjHrNIyiwhgQkK5XJUUkTk8KF+jTyut/7LmU2BI/HVAen0KYa7RIoZ8 U4qPf7HHZvPQNvcJ2kqQV1qidrP0vURk6RGrAtXzxmolEOSsTqKMN0nw/SXsLUTe6zRX CA3b9HI/sOlNoAsPwjYJvWvPmIN+Pv5ibOjMJX5C5t0V6u1Rh4tuzTam+neAJGNIjNDI gaANM7ajwBARHuQpI/IcJMZZ72/SPiNWpklICeCHodaIRsq1G984xp4cfLU236dx0s0s kG4g== X-Forwarded-Encrypted: i=1; AJvYcCWaBL8R/oFJaEFheDwDypwFqDV2vaXOO+Vz8PaE0Zs99CP+fnn8CgiItBqyQvhLC854tmK56qVpHrDDDKdN2l8z69Aj5hz8wpY6 X-Gm-Message-State: AOJu0YzjOMvYQ7oQwgsA4Qk9tPOQIFn584oQNGctqzqXPEJJ8MdbqPdV Yh+28h4VDUHgHM2ATY5ub3I1+aHKw43W8HbcPPojqirICD0jRi4zx5geqgttv3o= X-Google-Smtp-Source: AGHT+IEgvzETar+HRop8OxZM9BBYNdvV/dcg/BqJjJoXGsdJI9+RkgFTa2pKDMrLYqGGS3MRLjx9Ew== X-Received: by 2002:a05:6122:4699:b0:4f2:f2e1:5f04 with SMTP id 71dfb90a1353d-4fad1ea4056mr4448238e0c.8.1723670772920; Wed, 14 Aug 2024 14:26:12 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45369ff11d9sm557421cf.21.2024.08.14.14.26.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:12 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 08/16] fanotify: report file range info with pre-content events Date: Wed, 14 Aug 2024 17:25:26 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein With group class FAN_CLASS_PRE_CONTENT, report offset and length info along with FAN_PRE_ACCESS and FAN_PRE_MODIFY permission events. This information is meant to be used by hierarchical storage managers that want to fill partial content of files on first access to range. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.h | 8 +++++++ fs/notify/fanotify/fanotify_user.c | 38 ++++++++++++++++++++++++++++++ include/uapi/linux/fanotify.h | 7 ++++++ 3 files changed, 53 insertions(+) diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index 93598b7d5952..7f06355afa1f 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -448,6 +448,14 @@ static inline bool fanotify_is_perm_event(u32 mask) mask & FANOTIFY_PERM_EVENTS; } +static inline bool fanotify_event_has_access_range(struct fanotify_event *event) +{ + if (!(event->mask & FANOTIFY_PRE_CONTENT_EVENTS)) + return false; + + return FANOTIFY_PERM(event)->ppos; +} + static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse) { return container_of(fse, struct fanotify_event, fse); diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 5ece186d5c50..ed56fe6f5ec7 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -123,6 +123,8 @@ struct kmem_cache *fanotify_perm_event_cachep __ro_after_init; sizeof(struct fanotify_event_info_pidfd) #define FANOTIFY_ERROR_INFO_LEN \ (sizeof(struct fanotify_event_info_error)) +#define FANOTIFY_RANGE_INFO_LEN \ + (sizeof(struct fanotify_event_info_range)) static int fanotify_fid_info_len(int fh_len, int name_len) { @@ -182,6 +184,9 @@ static size_t fanotify_event_len(unsigned int info_mode, if (info_mode & FAN_REPORT_PIDFD) event_len += FANOTIFY_PIDFD_INFO_LEN; + if (fanotify_event_has_access_range(event)) + event_len += FANOTIFY_RANGE_INFO_LEN; + return event_len; } @@ -526,6 +531,30 @@ static int copy_pidfd_info_to_user(int pidfd, return info_len; } +static size_t copy_range_info_to_user(struct fanotify_event *event, + char __user *buf, int count) +{ + struct fanotify_perm_event *pevent = FANOTIFY_PERM(event); + struct fanotify_event_info_range info = { }; + size_t info_len = FANOTIFY_RANGE_INFO_LEN; + + if (WARN_ON_ONCE(info_len > count)) + return -EFAULT; + + if (WARN_ON_ONCE(!pevent->ppos)) + return -EINVAL; + + info.hdr.info_type = FAN_EVENT_INFO_TYPE_RANGE; + info.hdr.len = info_len; + info.offset = *(pevent->ppos); + info.count = pevent->count; + + if (copy_to_user(buf, &info, info_len)) + return -EFAULT; + + return info_len; +} + static int copy_info_records_to_user(struct fanotify_event *event, struct fanotify_info *info, unsigned int info_mode, int pidfd, @@ -647,6 +676,15 @@ static int copy_info_records_to_user(struct fanotify_event *event, total_bytes += ret; } + if (fanotify_event_has_access_range(event)) { + ret = copy_range_info_to_user(event, buf, count); + if (ret < 0) + return ret; + buf += ret; + count -= ret; + total_bytes += ret; + } + return total_bytes; } diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index ac00fad66416..cc28dce5f744 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -145,6 +145,7 @@ struct fanotify_event_metadata { #define FAN_EVENT_INFO_TYPE_DFID 3 #define FAN_EVENT_INFO_TYPE_PIDFD 4 #define FAN_EVENT_INFO_TYPE_ERROR 5 +#define FAN_EVENT_INFO_TYPE_RANGE 6 /* Special info types for FAN_RENAME */ #define FAN_EVENT_INFO_TYPE_OLD_DFID_NAME 10 @@ -191,6 +192,12 @@ struct fanotify_event_info_error { __u32 error_count; }; +struct fanotify_event_info_range { + struct fanotify_event_info_header hdr; + __u64 offset; + __u64 count; +}; + /* * User space may need to record additional information about its decision. * The extra information type records what kind of information is included. From patchwork Wed Aug 14 21:25:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764163 Received: from mail-qt1-f172.google.com (mail-qt1-f172.google.com [209.85.160.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 259361CB303 for ; Wed, 14 Aug 2024 21:26:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670777; cv=none; b=LTjuUZX5YjDJ+sGJMf9mSale0LDSpHAR/Bk4Ek5pdmDE3lQ0vpIxIZDMLs90UcEnKQLl0mlea6UbNMum7V4QJc1SrjfvQF4R6ep7MOvtMks1RQs/f/1KWQ5ZvgtlWu4SaWgB0sPb0msgvjdPU37vNi1iRL1xeHzzBER/jABhTkU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670777; c=relaxed/simple; bh=fHAWL+liJK7RUemLt/Svh2h8UMEWyBWVNZVgU9u6a6A=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GGOvQ07F61nUwzzkfm4MuwLp23ef53Nsm2qtNTp17xAGzcPuvLNOguRAd7zO4dgYNEerBg5vssiEFAG7KhM2HqRikn9k+DEzshyGqvrpIG/PWuKA4qj50jH1ve2oJBBdy3I6vjHSzUxIcB9Oo05qZ5AZCPbGwE2BofbxcxN+qCQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=ZwvCc7zy; arc=none smtp.client-ip=209.85.160.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="ZwvCc7zy" Received: by mail-qt1-f172.google.com with SMTP id d75a77b69052e-44fdde0c8dcso1813401cf.0 for ; Wed, 14 Aug 2024 14:26:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670774; x=1724275574; darn=vger.kernel.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=t2FDeRCcp9Y/rKgVhAuLqn/dDFHjuK0P8k/Tvh5Gfr4=; b=ZwvCc7zyys0wzJc1Jc5AaQTTv0d8t8T0o0Yg9PugsQvUYX2lsqesWY8QBjLerlZWbG 5/3ohG/m77zwr4SfK9migL7H+eQrGvw0IR+pvzOnSWI3cNuVPLvmpWylG2O3eDLU/Z6C 1khvks0JF6/6l5LPNxgJ6Z2YzVy/fhngbM+mkH7eANeK9gRQzVwYnKhbsOuuUxTcIArR HJEu2sbq47lXuXjmq6mp/Rt+nKX6OLWZr75YQUf2+y46bGTeCiZHx0FXg83ctAAA2E04 lB7ZRwOqzRriQ9KRJe+/oX10K0giOvsQgES2/QOklfvZvzxL/jzAMQL7vhUjPcnsbYmF pKKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670774; x=1724275574; 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=t2FDeRCcp9Y/rKgVhAuLqn/dDFHjuK0P8k/Tvh5Gfr4=; b=obMKCh0l6+zuhFzC5iUL8UcF37K2/7UVsH1Klip7mnB/mLa52qTrT6tMDOuOI0ByKL IXjKHpMPyWkOrXuzrKa/5jchLxmpkfsQhP0Fa8gxAXjxWJRo9GxH+EpoL7tPp8sC9tWO ksAqzPaBShP0GxMbUOmRsdMuZ99hoTdK4rHWJmpc5OhbvHoMdDNPPdgdtTtv9lntfOsT s6ilQwM3eu6W1SXnWNifraf5Qc8w2kLNLW6unvHJWls6yLcXx5FMfvYhOEz0Oz+Iikwl pmARKZ63jexIHtTcX8ANolR5RcvITbQgoJ0YqHNrJquHtFhiRVL7qi/1Za4/fwB0zuca DPtQ== X-Forwarded-Encrypted: i=1; AJvYcCXnEh8SLuXmYAuUCv1ix5i3QoCyY+vKN2jtG03+2TVnCcyZHCZNtX3eMdniJm6o4vlJ81LvWOHV/zRjeZgzbN/WVJigKiOTdR/J X-Gm-Message-State: AOJu0Ywm/qr7miBjDKtlUnEAcLtKITHs1iz7HlUc721h+MeHW0/4VSsN E3mq+cIqXXC5FbwwLbwdEXiyaJ45R5VZaFgryY0ELLf4vHfuKotuMWdrh53CcRE= X-Google-Smtp-Source: AGHT+IHliWHGRhze4pqf7VFGr7kBEPTv8Mb7Qz/sSgPlWWftPx1SWm1Y7AbPN4Kb5nWC//rHu2tx8A== X-Received: by 2002:ac8:70c:0:b0:453:5c20:90b7 with SMTP id d75a77b69052e-4535c2091eemr36300121cf.0.1723670774001; Wed, 14 Aug 2024 14:26:14 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4536a07837esm477701cf.91.2024.08.14.14.26.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:13 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 09/16] fanotify: allow to set errno in FAN_DENY permission response Date: Wed, 14 Aug 2024 17:25:27 -0400 Message-ID: <728fb849cd446e6b9a5b9fb9e9985c7d3bd9896a.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Amir Goldstein With FAN_DENY response, user trying to perform the filesystem operation gets an error with errno set to EPERM. It is useful for hierarchical storage management (HSM) service to be able to deny access for reasons more diverse than EPERM, for example EAGAIN, if HSM could retry the operation later. Allow fanotify groups with priority FAN_CLASSS_PRE_CONTENT to responsd to permission events with the response value FAN_DENY_ERRNO(errno), instead of FAN_DENY to return a custom error. Limit custom error values to errors expected on read(2)/write(2) and open(2) of regular files. This list could be extended in the future. Userspace can test for legitimate values of FAN_DENY_ERRNO(errno) by writing a response to an fanotify group fd with a value of FAN_NOFD in the fd field of the response. The change in fanotify_response is backward compatible, because errno is written in the high 8 bits of the 32bit response field and old kernels reject respose value with high bits set. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 18 ++++++++++----- fs/notify/fanotify/fanotify.h | 10 +++++++++ fs/notify/fanotify/fanotify_user.c | 36 +++++++++++++++++++++++++----- include/linux/fanotify.h | 5 ++++- include/uapi/linux/fanotify.h | 7 ++++++ 5 files changed, 65 insertions(+), 11 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 4e8dce39fa8f..1cbf41b34080 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -224,7 +224,8 @@ static int fanotify_get_response(struct fsnotify_group *group, struct fanotify_perm_event *event, struct fsnotify_iter_info *iter_info) { - int ret; + int ret, errno; + u32 decision; pr_debug("%s: group=%p event=%p\n", __func__, group, event); @@ -257,20 +258,27 @@ static int fanotify_get_response(struct fsnotify_group *group, goto out; } + decision = fanotify_get_response_decision(event->response); /* userspace responded, convert to something usable */ - switch (event->response & FANOTIFY_RESPONSE_ACCESS) { + switch (decision & FANOTIFY_RESPONSE_ACCESS) { case FAN_ALLOW: ret = 0; break; case FAN_DENY: + /* Check custom errno from pre-content events */ + errno = fanotify_get_response_errno(event->response); + if (errno) { + ret = -errno; + break; + } + fallthrough; default: ret = -EPERM; } /* Check if the response should be audited */ - if (event->response & FAN_AUDIT) - audit_fanotify(event->response & ~FAN_AUDIT, - &event->audit_rule); + if (decision & FAN_AUDIT) + audit_fanotify(decision & ~FAN_AUDIT, &event->audit_rule); pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, group, event, ret); diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index 7f06355afa1f..d0722ef13138 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -528,3 +528,13 @@ static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark) return mflags; } + +static inline u32 fanotify_get_response_decision(u32 res) +{ + return res & (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS); +} + +static inline int fanotify_get_response_errno(int res) +{ + return res >> FAN_ERRNO_SHIFT; +} diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index ed56fe6f5ec7..0a37f1c761aa 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -337,11 +337,13 @@ static int process_access_response(struct fsnotify_group *group, struct fanotify_perm_event *event; int fd = response_struct->fd; u32 response = response_struct->response; + u32 decision = fanotify_get_response_decision(response); + int errno = fanotify_get_response_errno(response); int ret = info_len; struct fanotify_response_info_audit_rule friar; - pr_debug("%s: group=%p fd=%d response=%u buf=%p size=%zu\n", __func__, - group, fd, response, info, info_len); + pr_debug("%s: group=%p fd=%d response=%x errno=%d buf=%p size=%zu\n", + __func__, group, fd, response, errno, info, info_len); /* * make sure the response is valid, if invalid we do nothing and either * userspace can send a valid response or we will clean it up after the @@ -350,18 +352,42 @@ static int process_access_response(struct fsnotify_group *group, if (response & ~FANOTIFY_RESPONSE_VALID_MASK) return -EINVAL; - switch (response & FANOTIFY_RESPONSE_ACCESS) { + switch (decision & FANOTIFY_RESPONSE_ACCESS) { case FAN_ALLOW: + if (errno) + return -EINVAL; + break; case FAN_DENY: + /* Custom errno is supported only for pre-content groups */ + if (errno && group->priority != FSNOTIFY_PRIO_PRE_CONTENT) + return -EINVAL; + + /* + * Limit errno to values expected on open(2)/read(2)/write(2) + * of regular files. + */ + switch (errno) { + case 0: + case EIO: + case EPERM: + case EBUSY: + case ETXTBSY: + case EAGAIN: + case ENOSPC: + case EDQUOT: + break; + default: + return -EINVAL; + } break; default: return -EINVAL; } - if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) + if ((decision & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) return -EINVAL; - if (response & FAN_INFO) { + if (decision & FAN_INFO) { ret = process_access_response_info(info, info_len, &friar); if (ret < 0) return ret; diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index ae6cb2688d52..547514542669 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -132,7 +132,10 @@ /* These masks check for invalid bits in permission responses. */ #define FANOTIFY_RESPONSE_ACCESS (FAN_ALLOW | FAN_DENY) #define FANOTIFY_RESPONSE_FLAGS (FAN_AUDIT | FAN_INFO) -#define FANOTIFY_RESPONSE_VALID_MASK (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS) +#define FANOTIFY_RESPONSE_ERRNO (FAN_ERRNO_MASK << FAN_ERRNO_SHIFT) +#define FANOTIFY_RESPONSE_VALID_MASK \ + (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS | \ + FANOTIFY_RESPONSE_ERRNO) /* Do not use these old uapi constants internally */ #undef FAN_ALL_CLASS_BITS diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index cc28dce5f744..7b746c5fcbd8 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -233,6 +233,13 @@ struct fanotify_response_info_audit_rule { /* Legit userspace responses to a _PERM event */ #define FAN_ALLOW 0x01 #define FAN_DENY 0x02 +/* errno other than EPERM can specified in upper byte of deny response */ +#define FAN_ERRNO_BITS 8 +#define FAN_ERRNO_SHIFT (32 - FAN_ERRNO_BITS) +#define FAN_ERRNO_MASK ((1 << FAN_ERRNO_BITS) - 1) +#define FAN_DENY_ERRNO(err) \ + (FAN_DENY | ((((__u32)(err)) & FAN_ERRNO_MASK) << FAN_ERRNO_SHIFT)) + #define FAN_AUDIT 0x10 /* Bitmask to create audit record for result */ #define FAN_INFO 0x20 /* Bitmask to indicate additional information */ From patchwork Wed Aug 14 21:25: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: 13764164 Received: from mail-qt1-f179.google.com (mail-qt1-f179.google.com [209.85.160.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1749A1CB336 for ; Wed, 14 Aug 2024 21:26:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670779; cv=none; b=f4sMdyT+ZuMlyw7AxprLmaSG32gRZb4goyC4Bk5hnYtJOf7rlyaVTvOJP2dQd302d5ZqQZh7Mu7g1wHOP2sclRffFyY/39D8wLeqHe2H7HZ6oBgorVx7tLAILeof0aZm1IyDiTSe6PoPRVnvQZE3TisJ1va5VQpF7R4z3q1oKgw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670779; c=relaxed/simple; bh=oIFEloV+HJRwW7KKzTETqcSZwuMF6cvmESmc2+RUi2s=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g2IRuWnXtg+5Ss7Tsx2BfAIakwpP2Z7+WjR32uWLW+351lVr1T6KzbRFZ5uVyRadcEIWiJ7dRMu4F5y8MDlRa9A3pgkxF17Z6NhA/AbgVa+1CrQYefubtB55/qXM5uF+p7grOduViafQxjojAmMCBab8ZPaZB7lWf8ajAqWl/HA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=egfBT2So; arc=none smtp.client-ip=209.85.160.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="egfBT2So" Received: by mail-qt1-f179.google.com with SMTP id d75a77b69052e-44fea44f725so2517541cf.1 for ; Wed, 14 Aug 2024 14:26:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670775; x=1724275575; darn=vger.kernel.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=iqckBRUWRZ8lY2PUqxw8YY3RQIFbRSWL2L9gDiXv76o=; b=egfBT2SohJocthjXgqBRlCM24YB5SjKy3kaaHIq3PNauChakpxk0meEX4hTK7dnT48 NNtwjPwseLA2GV0D1mgWNHR9qpW/+bcvWHgdoIH4aFtn9CmogjEYdycwzW0P21NeOc1O CMZzPJLYPRqfNPnkwMBrYN4w6Nc/eui1PiwZ/MEjTaBu4nrQFgxUf6I9/DfYL4ILWMq9 F4wgdMXDeEROGYbzC9nOMQZqy/RhNOYN4zt7HDyP+CtY2ClW2ziw0h/BNyE/AUEKamW6 cw+DNLQBVQUwvKP7NVw0dW8ygUCvPCA6uQvqP9YpDdRO/Xiku2zsCnkorF/SdH5FedNC OvJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670775; x=1724275575; 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=iqckBRUWRZ8lY2PUqxw8YY3RQIFbRSWL2L9gDiXv76o=; b=rmPoC8x2zaE1AgZ34Gydj5lx3pjORxh0YcTxlc+lVCKVsgIVYAjBvlfyqEh0fMwJUW 5UBGVZxVLfWtQHtJtqXJ0o1X10IuiSain+rBDGN28UdHTGDjqs80d5AqTWpyC461UvGX WRdjxR50GgOgQFR0rPuXXYG0XUqfqEA3gbCH4xLhoI+8JoBmt0kAk8Amqi62ufTooyLp /zfGKadLibeS2CrbPiNiTtWklf295VSJUKs3iG6xfWHw7dhef0gNCHCbZ6uaFpA403bu NkJ+CAEIqA/DKy07Fe7RF6XTgVcYVH2V/0TG/F8uWrpZjz3mHBytJF1dQOTXAWHQOIRU nm2g== X-Forwarded-Encrypted: i=1; AJvYcCUeOb+WuRQ7ogAWIO0Pdhw8y1yfMFQUtIqCguxvuRoB7ohSs4ZesuLEMVe+AGYsBJRiaA2UtodMx7DmAza3bJsk325oGGSdpES8 X-Gm-Message-State: AOJu0YzDvHfUPJSfIF6tYfHpiDkrWNeMyhxWjBcLECa45OQXjJOrp8XQ F3Q3RhaeOIOApBYylLmGrUqVKHqSLSuajg7BfIOiRf5JpdoXyhJqnKHfpAZp5GM= X-Google-Smtp-Source: AGHT+IEd/MmUwaA1JVX+VfdhiUmDxdoXxYs9e30nr4Gux5C2du2HbwieZEhvSa2zBuHRp84nZOLlBA== X-Received: by 2002:a05:622a:4c8e:b0:453:57b0:8814 with SMTP id d75a77b69052e-453678456ebmr18969301cf.6.1723670775058; Wed, 14 Aug 2024 14:26:15 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4536a04e22esm502161cf.74.2024.08.14.14.26.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:14 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 10/16] fanotify: add a helper to check for pre content events Date: Wed, 14 Aug 2024 17:25:28 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 We want to emit events during page fault, and calling into fanotify could be expensive, so add a helper to allow us to skip calling into fanotify from page fault. This will also be used to disable readahead for content watched files which will be handled in a subsequent patch. Reviewed-by: Christian Brauner Signed-off-by: Josef Bacik --- fs/notify/fsnotify.c | 12 ++++++++++++ include/linux/fsnotify_backend.h | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 1ca4a8da7f29..cbfaa000f815 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -201,6 +201,18 @@ static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask, return mask & marks_mask & ALL_FSNOTIFY_EVENTS; } +#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS +bool fsnotify_file_has_pre_content_watches(struct file *file) +{ + struct inode *inode = file_inode(file); + __u32 mnt_mask = real_mount(file->f_path.mnt)->mnt_fsnotify_mask; + + return fsnotify_object_watched(inode, mnt_mask, + FSNOTIFY_PRE_CONTENT_EVENTS); +} +#endif + + /* * Notify this dentry's parent about a child's events with child name info * if parent is watching or if inode/sb/mount are interested in events with diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 276320846bfd..b495a0676dd3 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -900,6 +900,15 @@ static inline void fsnotify_init_event(struct fsnotify_event *event) INIT_LIST_HEAD(&event->list); } +#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS +bool fsnotify_file_has_pre_content_watches(struct file *file); +#else +static inline bool fsnotify_file_has_pre_content_watches(struct file *file) +{ + return false; +} +#endif /* CONFIG_FANOTIFY_ACCESS_PERMISSIONS */ + #else static inline int fsnotify(__u32 mask, const void *data, int data_type, @@ -938,6 +947,11 @@ static inline u32 fsnotify_get_cookie(void) static inline void fsnotify_unmount_inodes(struct super_block *sb) {} +static inline bool fsnotify_file_has_pre_content_watches(struct file *file) +{ + return false; +} + #endif /* CONFIG_FSNOTIFY */ #endif /* __KERNEL __ */ From patchwork Wed Aug 14 21:25: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: 13764165 Received: from mail-qv1-f43.google.com (mail-qv1-f43.google.com [209.85.219.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A8211C824C for ; Wed, 14 Aug 2024 21:26:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670779; cv=none; b=foVen/KhGrixWl9U5EdrVdxGqoks7FvHUYvkGKKdT196qWkNp1Kxn9xF6+DENKS2avzZYP+tbcZejAyA1utzhAzweffEhbwbOZr0uXkK4bx54HCf9SqUdw7ozORGU8KB8qGF6peWGZg9qc0eBzJ9yz/7UKwOnhFQnOdelRZq2S4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670779; c=relaxed/simple; bh=RuLIdPYLf2U7vdfwd0GY223a6ukGYtQumd4qAvMMF1s=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EAO/SpfMXoMJLW/Jzxkvt34XskvMZw0KIqI8ezK6qWmUmnt1381v9MQAfOQERSr8YwEGPbEZKI8Zs/AtHzpa9CBKTSzhgZ1mCrrIxtz9NmfjV7xK8UCs612jg5999T8nz2vHQeysYDVY1wBKdFeMtg6k4wOFFciETTsn518/DgU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=IPZSBVx3; arc=none smtp.client-ip=209.85.219.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="IPZSBVx3" Received: by mail-qv1-f43.google.com with SMTP id 6a1803df08f44-6bd6f2c9d52so1365646d6.3 for ; Wed, 14 Aug 2024 14:26:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670776; x=1724275576; darn=vger.kernel.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=NQ0y3/dQBSJxG2OyPRkhEh3Vj0wjByustgiYia+MZqs=; b=IPZSBVx3FvAg/wy6rDR0AVmgrMkTIpQdIqpFh6B2mN60MX5Cz5YOVcUsQyTw7eR4q1 oKcHesIjSo9/dJycGPV2Vl+n3frm4OhclApkYB2ODCb9JMCPbg3fdbQ1f3RPUtgV2cMo 807s7jHG1sAjH4vqauzhgyQtG0I0cWhJHOmSvKi98hed+Hk7KKto1yfS+uwc0eoySD09 xjDioDWCPhb79AbqF0zIwgBu6mshgq12cmAPwiRb4x2euQrO2q8rHeMyae9GA5ZfuEpi K1wsjqm1lQNfCbCnKONLdy37L8tuc+cdmqv8/Ft5b3GUKq+yC7jdn1yStgHULCzrWCuw f1Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670776; x=1724275576; 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=NQ0y3/dQBSJxG2OyPRkhEh3Vj0wjByustgiYia+MZqs=; b=cy/H3I/Wkj0OIC9Ls6b8Qd2hjnxa81F1o8TlstPdWZxp+kVaOt0tzuqZs8cuynPaFh qJ6LxbAtt9LlmGOFGAvxiR0MeSqu11YKxwuPZjNHnbOUQvUDA0H30olgNfzJTbOxBQSR npgIwVeS9/ex1bsE13AocvPNRbwwWjdoaXQj6+jm67HlCtJ+8xzZOqDlNJgRXcc+A4S4 qsjAUKHWNEkffll373Xmdx9ALQ2KmGxmJL7i02GgsZX4lOB6+njRMoNk1/Kz6b5KyXaX cfl88FCJIh/pMQdBheGUdVJ7p+pgD2QS8loTn9ijjMD06F3Or1jc4oRFeVrFF6z/O9sp iPFQ== X-Forwarded-Encrypted: i=1; AJvYcCWeXeSbg6VSnYxZ78cZnml2NxOfqpE4cf/E2JZtBPz8tv0GF7Nh+pDYLYTkUDKEYOxh5pZ5bCjpZ5cO/6PehGB/eR/4sAt7NEER X-Gm-Message-State: AOJu0YyNXjEeFy9Eb4DE8CIhgB/x/FvUVjygax9ycSPqranRC8QXLaaG dm9c6GLSdd78Lc+mV0UQyV4xB5o/KfHnbwAMpAn0ipsL5+pmFi+td8UUdforuJw= X-Google-Smtp-Source: AGHT+IExGAEQEamrim6ho8QAd+QcOVhJTtOVmgmL6OYA606guNdAfaz4yD+2UP67QG+9AhpysKNQAg== X-Received: by 2002:a05:6214:2e4a:b0:6b7:a9dd:ba58 with SMTP id 6a1803df08f44-6bf5d1fa365mr62306796d6.18.1723670776088; Wed, 14 Aug 2024 14:26:16 -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-6bf6fe26c71sm618696d6.61.2024.08.14.14.26.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:15 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 11/16] fanotify: disable readahead if we have pre-content watches Date: Wed, 14 Aug 2024 17:25:29 -0400 Message-ID: <9a458c9c553c6a8d5416c91650a9b152458459d0.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 ca8c8d889eef..8b1684b62177 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3122,6 +3122,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) { @@ -3190,6 +3198,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 817b2a352d78..bc068d9218e3 100644 --- a/mm/readahead.c +++ b/mm/readahead.c @@ -128,6 +128,7 @@ #include #include #include +#include #include "internal.h" @@ -674,6 +675,14 @@ void page_cache_sync_ra(struct readahead_control *ractl, { bool do_forced_ra = ractl->file && (ractl->file->f_mode & FMODE_RANDOM); + /* + * 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 @@ -704,6 +713,10 @@ void page_cache_async_ra(struct readahead_control *ractl, if (!ractl->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. */ From patchwork Wed Aug 14 21:25: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: 13764167 Received: from mail-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34BF71CB328 for ; Wed, 14 Aug 2024 21:26:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670782; cv=none; b=d/Bpa82zVJAksV56qDAV+p1tTaWAcqhHAlFhn2PBpAhPViESfdlaEOG06xYHIJK5F9qT9SyaosK/GU3OwoeykUU3KgSFLLZT4NngXboNJt4Ns+tAbzkCiRr84lcSbsPGPDm0eoJ9KTScgxr6GNb29Jpiy+KzhF0TW/nVsiqfa20= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670782; c=relaxed/simple; bh=Q3VYbwM4CKv9HZE1IBds6JfYLVoY9EDuZvS5XcFcfQs=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=McaEtXb58PVoItMEgqN6dAgKWB4SlnJgGrVeND79s+SXiptZHVI+N5KN/EBgH/SAZXtED19uF9x8++7plgykarhGRcSzeLq2zr607KJfBR2NjAzb8OdQTbvglWfFpictgWtjLrAHMskNhznJEdOyaCGqaRwGpQFUNKdvX/5BzRA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=mcy8xQ02; arc=none smtp.client-ip=209.85.219.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="mcy8xQ02" Received: by mail-qv1-f49.google.com with SMTP id 6a1803df08f44-6b796667348so2710336d6.0 for ; Wed, 14 Aug 2024 14:26:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670777; x=1724275577; darn=vger.kernel.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=IDO1PsR4jqBoDIDFjuXXy+JZDMdVbZ460dPfM2iRCeI=; b=mcy8xQ02p/JaqYsO93gXjfPGPL74527c3uuV4cPmWoCO/cYJQdbTJRv1csIjjNBKyf OysPrIfSYD24PRfBHdVcf9ZAMTUt2NqnBosilpsx2pbGyTOBE/hvIILeFuTR4Aw/2I61 d/VdJEa7oeNhOT27Rjk7K+kGBLEO4PX8j5xL2Sb3NZJpNyS3cpTg5/hiR5O1rEvZEqcJ 5HmOTyvEAsb7prXKqXe+3kY10hG62OELDCAufYtkHBiQUEYtsv3Bzz2jSq4/C3Jw3Cp6 10f4D82SVs+LWw1nr44omqwjQu16HxkSE28pIDDSZPfNz/hlHNTS+afxmo+r21et0IeD 8Ymg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670777; x=1724275577; 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=IDO1PsR4jqBoDIDFjuXXy+JZDMdVbZ460dPfM2iRCeI=; b=KcGCgNszLY/NSbcFzQVft1WsCT2gjkOvLwO4pZe+EjSSLmk+pARawuKBxfzC6LrZop MrCxtDRYLcvNqUt0ggxN4nRXslV3wzdGeu5+qBT0FgOf/jr/WLxx4oDZ25N2RHDzBDVH 4HAWoFwYq04M4vTKGx2mWWicAMzB5IzjUmzJ3afwF7/cSJyjYPNFMoh/2bwoVgqNza3w kRJh7l16wSgs+Bgjtl7A7wPz6NuMGeVJkG+HXzJMZ3JI5ZzTWQoJHXTF75Us4sTkMpyW eQqPC+okmd/yl1CboV5pcXeklehR9AQLfclwuve9Dllz+sWLXcaQ4eq+HMtdGJk3CLFs 8E2Q== X-Forwarded-Encrypted: i=1; AJvYcCW206f3s3WqRepO8dtGujeS4x3Ow5JpZaguB+6yNdjsuChY3EWO6JIL0IIYVYORduw21RlZ0VI8VCg=@vger.kernel.org X-Gm-Message-State: AOJu0Yx4iI1IyS1wQl87ovKQ09j9wBcxjlSXEq6xCvHUGNe3tHsyC86L j0TU/yqV/TSSFxHd8/2Oz1zP2tfD3o1FmNLh177fyZ8HKEHKOoUG/D/V1qftfNE= X-Google-Smtp-Source: AGHT+IGeO0B2k6JtijtGgt8cNjSSZOq15LBjH6uKw7eaOrAdoCnw1rPT9UcRCivz4wM3Sne6FWLZ5g== X-Received: by 2002:a05:6214:cc6:b0:6b7:9a0a:33db with SMTP id 6a1803df08f44-6bf6de4eadfmr15035046d6.23.1723670777161; Wed, 14 Aug 2024 14:26:17 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45369fd7943sm572041cf.20.2024.08.14.14.26.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:16 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 12/16] mm: don't allow huge faults for files with pre content watches Date: Wed, 14 Aug 2024 17:25:30 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There's nothing stopping us from supporting this, we could simply pass the order into the helper and emit the proper length. However currently there's no tests to validate this works properly, so disable it until there's a desire to support this along with the appropriate tests. Reviewed-by: Christian Brauner Signed-off-by: Josef Bacik --- mm/memory.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mm/memory.c b/mm/memory.c index d10e616d7389..3010bcc5e4f9 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -78,6 +78,7 @@ #include #include #include +#include #include @@ -5252,8 +5253,17 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; if (vma_is_anonymous(vma)) return do_huge_pmd_anonymous_page(vmf); + /* + * Currently we just emit PAGE_SIZE for our fault events, so don't allow + * a huge fault if we have a pre content watch on this file. This would + * be trivial to support, but there would need to be tests to ensure + * this works properly and those don't exist currently. + */ + if (file && fsnotify_file_has_pre_content_watches(file)) + return VM_FAULT_FALLBACK; if (vma->vm_ops->huge_fault) return vma->vm_ops->huge_fault(vmf, PMD_ORDER); return VM_FAULT_FALLBACK; @@ -5263,6 +5273,7 @@ static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; vm_fault_t ret; @@ -5277,6 +5288,9 @@ static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf) } if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { + /* See comment in create_huge_pmd. */ + if (file && fsnotify_file_has_pre_content_watches(file)) + goto split; if (vma->vm_ops->huge_fault) { ret = vma->vm_ops->huge_fault(vmf, PMD_ORDER); if (!(ret & VM_FAULT_FALLBACK)) @@ -5296,9 +5310,13 @@ static vm_fault_t create_huge_pud(struct vm_fault *vmf) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; /* No support for anonymous transparent PUD pages yet */ if (vma_is_anonymous(vma)) return VM_FAULT_FALLBACK; + /* See comment in create_huge_pmd. */ + if (file && fsnotify_file_has_pre_content_watches(file)) + return VM_FAULT_FALLBACK; if (vma->vm_ops->huge_fault) return vma->vm_ops->huge_fault(vmf, PUD_ORDER); #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ @@ -5310,12 +5328,16 @@ static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; vm_fault_t ret; /* No support for anonymous transparent PUD pages yet */ if (vma_is_anonymous(vma)) goto split; if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { + /* See comment in create_huge_pmd. */ + if (file && fsnotify_file_has_pre_content_watches(file)) + goto split; if (vma->vm_ops->huge_fault) { ret = vma->vm_ops->huge_fault(vmf, PUD_ORDER); if (!(ret & VM_FAULT_FALLBACK)) From patchwork Wed Aug 14 21:25:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764166 Received: from mail-ua1-f47.google.com (mail-ua1-f47.google.com [209.85.222.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E26A1D0DF0 for ; Wed, 14 Aug 2024 21:26:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670781; cv=none; b=DDOx0lZHyjviWCD6jM0mIHD9eHvZbvVN2P+NscRnOKjzKt/IL/Hcw9PQCoqU+w+sdASKmx46OYcI+5cEZgWabJxp2XDKXjYULFav2wamTEkPXFH8zQ+/rHCSHBVQV+dP/JBut+Gf037658zIr1g9ddFbhJO/veGYLIstZH5gX9Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670781; c=relaxed/simple; bh=+R4CMbAnxjbapDmjS8smH4nL5obwlroO+mHsLO3YBIk=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iZBfAAI41kWC64u+KEEooHbvsrI9OdxP+bkIARYjqzp3hTobaA4fZJteI6JmU4gxwaTKhwV9xgOPr4ZsFUi3+JJApLLLirediJ0ObNNLfCmsCPgbyKIKT5w3IrKvaQaOTK3BpooQ362bkjGGLpaQtKvnbq9sbsQZVK2cTJoepTE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=Fb2tGS3b; arc=none smtp.client-ip=209.85.222.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="Fb2tGS3b" Received: by mail-ua1-f47.google.com with SMTP id a1e0cc1a2514c-83446a5601bso116565241.0 for ; Wed, 14 Aug 2024 14:26:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670778; x=1724275578; darn=vger.kernel.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=mR8PwrMz1WFxV/Gdk97UmpGfKPpAV+y+F/NYCz3BQ1A=; b=Fb2tGS3bqqRg8lcXdcIWJ9JSfodqdy4PShJOEB4op8fMoV1gcDN5ZrNSNs9rBg4nx9 BY1ciW1oiEXMfXpDmTh5KAo/BI1OHO28nUBjQnRvVGvOXt89pDYhP425AScfMmYo3VAy UHvpZIQmOEsnmiToayu8NDGw1vHHCBkEiXHlZiVVKPCQhdEWcP4OS7Yq63OGbdQSJTzp Pdmhr5gXW/y+5p9d1l0MvxyM/AKTO1iiSeuW8On3Z2gaZ4N20IP4IezYsNPsySopRf3P 5tdfRUKL3RGbmuLKeEleYBN0daCF+DIrL9f7qsBMOhjxkjx2CJut6rM96XMFB+pNZ2TZ WYRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670778; x=1724275578; 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=mR8PwrMz1WFxV/Gdk97UmpGfKPpAV+y+F/NYCz3BQ1A=; b=tpJD61olpDSJAU9pF/GVtn715rBBoroSMigNLn9TF5nBYnrnHROuYEod3eRw6KRsc0 Lpz6jaGWR8Y81tGI/0Fyf21E26hgSHzLe+2MlEUmJz3o6aoPOgG7t2TO2pDOZnbsw/Vx Y/ISGpi3NumtTT1nMCl+gpnXqBfuCrq2oVGY6fPWnyr6IusTb6W8JlAmvcAOvGym2oT+ 7F+v0LX8B6PB/uuRQoAU+tax5J026FlwqMR2E8G8685JwkzkQ/K8JXkQGEzaEQaAOklJ LqSnXOKruBL6ob1dZG2TeBZCOkfU0ITxXXmB9bMqx19W0V9n75+MIwyGG1G3V8nu56Ku KprQ== X-Forwarded-Encrypted: i=1; AJvYcCWcGsAYXM+s8sdHUuvQtNYr30l5WvufWVx0Tv0vjX2YovX6rMYDZlhmHHNhF1qA6xoomxXLtXfyojjSCDxaD+ufVO/JjU+uAg7B X-Gm-Message-State: AOJu0Yzd3+QI+XNgHrcr694fjRaAGUIl9JDgsN3eVZcPxdEOJoitfWf7 ux8pmNPszE6tjq3aGtW0gIRaWIwq8MhfAyKz7OeHbgShjxSRRafl2S7puz8egRk= X-Google-Smtp-Source: AGHT+IEKTDSPzYSxfB/JvZm9ph6NSf/1isbcglZoYV6GC6d5OM43yiDNeY1W0wMICYic6PdHx3h1lQ== X-Received: by 2002:a05:6102:4193:b0:493:b2b4:3708 with SMTP id ada2fe7eead31-497599e5b1emr5106732137.27.1723670778251; Wed, 14 Aug 2024 14:26:18 -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-6bf6fe06fc9sm605916d6.44.2024.08.14.14.26.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:17 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 13/16] fsnotify: generate pre-content permission event on page fault Date: Wed, 14 Aug 2024 17:25:31 -0400 Message-ID: <4be573448ff9f15e6fb55e41fa6453b655d8a467.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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..3e190f0a0997 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_maybe_emit_fsnotify_event(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..50e88e47dff3 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_maybe_emit_fsnotify_event(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_maybe_emit_fsnotify_event - 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_maybe_emit_fsnotify_event(struct vm_fault *vmf) +{ + struct file *fpin = NULL; + vm_fault_t ret; + + ret = __filemap_maybe_emit_fsnotify_event(vmf, &fpin); + if (fpin) { + fput(fpin); + if (!ret) + ret = VM_FAULT_RETRY; + } + return ret; +} +EXPORT_SYMBOL_GPL(filemap_maybe_emit_fsnotify_event); + /** * 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_maybe_emit_fsnotify_event(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 From patchwork Wed Aug 14 21:25:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764168 Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 513A21CB336 for ; Wed, 14 Aug 2024 21:26:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670783; cv=none; b=pl3CnT0OEObCOp+rgBg3sy7G0vAPeW86IbfxPFpWVVX1jOAfjeAwJ/Fo8WF7yMme8zfIr7vvH0i9BQ7B2cEGquMSwumd0iecqcGee8fup24xlvkE+utBvQ6gcbpP4XtAL5TB3uRxbKy8noeYGIkT7kSJJibIcg7F8iV1IFhZSLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670783; c=relaxed/simple; bh=/ZdKHR4pHjntJYSoUdVRDKcFzv/LrElzLuY8Xsnn2js=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KNRInlY3DsHf0bpT8h2OVeOC/yDpM/R7AiaCH12k3cnOT3nVMXnigtmQ4F1P/+CqqDM00KJq2sAEiYEReT0E06YRZnanb5F8Ll5sYQhc99ZKCL+wldv/pQv0GWoN1ClWRn4GnAaiXH/sX+dXvAv6vKMZMSQA8ixpDakfiAhWJ7A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=XJsENMnb; arc=none smtp.client-ip=209.85.222.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="XJsENMnb" Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-7a1d42da3e9so17763585a.1 for ; Wed, 14 Aug 2024 14:26:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670779; x=1724275579; darn=vger.kernel.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=AN6bShe37d+I+GAe4SQfQZex9z+pcNLPbf8dU2iG5Y0=; b=XJsENMnbwhH3Pwzq9pD/aXXais5rsjHl9cQJ781xKJ/Va8J//Sv9tEerVBsx3Eud4y 8jO4iJRTVS8qigq9ZBhNoT1x0RXtuYEJTcYoINjeGnZVSyLu88MugguCPXVd+q/xLizN cSM8+nIXlbK4rb3zXbtjSieepmtd2q18kUV/Kl2x71j4PvwDTTVtuFiS4MEQz3E+as8p 2aNCxCLWADe3IkNtmPhAtiXbuR49yWxKHLVSKmzGhlsVW72qh3K3UFQre8Ck8PDmgbZN jr4OaZ5qjNqiZbmcLLTWA8uhTs1WEqhraa/X/128IQSgz7ZoNVAGrvRE/P6h0C4588fF a3tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670779; x=1724275579; 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=AN6bShe37d+I+GAe4SQfQZex9z+pcNLPbf8dU2iG5Y0=; b=bLkFJWyB86fszAqi5AsyxknmHAKAbpwNCig9HBe2MFV4P2ZjVmRNmQw9fLf/pwXmDi z2I/l68iLqEOLPD4XFs/8WCtR8E5+ScKSZP0Ec9L/b3xcIQpAf6bHqUSNdJAUjJYo/sO la+v43WmTXgTOm7J2b79iPr0c0uG481kSpHl0RFwR8VecSuLBg3tq734xYskgpLdkp6j QIxsqfjI5NbeGhSdOUH2tua2bgXW6m9SCxO0E0CDoWJpqX8V7izIksx82ljW5QrYQ7YB JZxTAA67pQpBA/7eCpTnj2fopmNJaw3wtiDSissGa+d+qPSkkx3QlMq5hzg0FXCa3aIo 23wQ== X-Forwarded-Encrypted: i=1; AJvYcCV6meH+cLhWTVqT1lGdKz1Jke8gvUBo/gvtGOUL0y4mJ3ygKqpq4Xz+qZ7yOBzPRD2yEfrAMhdy4+RiJ9jCFBIMrRGTACNmmJ2x X-Gm-Message-State: AOJu0YxvFT+gb0mgo2GdAjnbGiLD5d/xywqElHyvu0DIh4/wAdVS4cXX g5ywWMda2J0CYq1N0lvZCgm2yMOQPEuJGcBOp/vK+WTd+IuK8v3deaiKJXsaS7g= X-Google-Smtp-Source: AGHT+IFQOOdc7hk322ZC4VYavDskQmkGcpA4gUJL30TWlxeg1xLDQtxnIo9ze+bETsdddSmDQjnk1g== X-Received: by 2002:a05:620a:c47:b0:79f:1cf:551e with SMTP id af79cd13be357-7a4ee323d9emr460557185a.5.1723670779329; Wed, 14 Aug 2024 14:26:19 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a4ff120085sm7708485a.131.2024.08.14.14.26.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:19 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 14/16] bcachefs: add pre-content fsnotify hook to fault Date: Wed, 14 Aug 2024 17:25:32 -0400 Message-ID: <9627e80117638745c2f4341eb8ac94f63ea9acee.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 bcachefs has its own locking around filemap_fault, so we have to make sure we do the fsnotify hook before the locking. Add the check to emit the event before the locking and return VM_FAULT_RETRY to retrigger the fault once the event has been emitted. Signed-off-by: Josef Bacik Acked-by: Kent Overstreet --- fs/bcachefs/fs-io-pagecache.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/bcachefs/fs-io-pagecache.c b/fs/bcachefs/fs-io-pagecache.c index a9cc5cad9cc9..1fa1f1ac48c8 100644 --- a/fs/bcachefs/fs-io-pagecache.c +++ b/fs/bcachefs/fs-io-pagecache.c @@ -570,6 +570,10 @@ vm_fault_t bch2_page_fault(struct vm_fault *vmf) if (fdm == mapping) return VM_FAULT_SIGBUS; + ret = filemap_maybe_emit_fsnotify_event(vmf); + if (unlikely(ret)) + return ret; + /* Lock ordering: */ if (fdm > mapping) { struct bch_inode_info *fdm_host = to_bch_ei(fdm->host); From patchwork Wed Aug 14 21:25:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764169 Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65AB11D1F43 for ; Wed, 14 Aug 2024 21:26:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670784; cv=none; b=p/oEtDmexFDNSXR672VbPnmq8QJOpitkE7ujZkpoxOMxXRFIxs8sTujwzld97yr5cmpI7vZWpiWt4S4KuoMBN+v5yeSB29T4puAAxi3Pg5SV0XoBxtXWb+7FWXbAOZPrkIAxLSw1cUgi5IWZG9hb0hW4Z6UqE2vl6JXA0BTzlrA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670784; c=relaxed/simple; bh=NSHxxZRLFbsIgAXbtW5uSuQCtRZde7l6XJ/+Bc0hmhU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lju0iVikV8HyWFjF5tArJPmmOCvfznlEbGJ36EgY51P021Tai2enyp+khHni+qperZNWUy6/8EbTJLWBY4ITSz+iQoNBj8ABWKsFm39hGEo+gFA3LGcGnVUWBtzK9bWPgnQsXAgoqOSEymUYnSHLJe5SSvhQNX8Up/b4ARp6Ujo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=JkBpJjdH; arc=none smtp.client-ip=209.85.161.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="JkBpJjdH" Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-5d8060662a1so235209eaf.2 for ; Wed, 14 Aug 2024 14:26:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670780; x=1724275580; darn=vger.kernel.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=nOXg5Kh3dRzVUCha6XNrLJPIFEv9dDgQMOq3uyVXRLk=; b=JkBpJjdHT0nr8y7rL8HeY2I866l2ae0wbXcN22jjs8VfDIDzAU7TwCjd0SowQT0Pp9 cH16jFw9xISJc3kn6MjOn+w/Bzkm1V970jOmuzvCTbD9vacuRiG0a9gtwpcnRwMz24Du MTP6wcx8anMOCNqYbIJWOo7bgNjY54IniLbtrBGePpdTAaV9p97fG60F9cApSCn8gofZ Ea8GDSl/14hEMp23KrnI874k+3gig+mrKcC98nTUmh0waf8jLw57j2B0N92xGbr59B7f twbESphI5/jr7Lkxx1tuWVF9g5HAMaLJYD6W51K8PHahW2K+w+9pfVEDo87m76zvNb++ SeRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670780; x=1724275580; 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=nOXg5Kh3dRzVUCha6XNrLJPIFEv9dDgQMOq3uyVXRLk=; b=wJTA8kw2r71xae5xVV4E7OrBMpc2D+BWTFftHD776F/xQmfct5U14Y/pnyeEJsUV95 cNFnEmYKE2gzVMWRwCawMZgSQxPvW0EOIaMc3gxQn//hn+k/jNrYV5w4+UMokc33aJLK le9YfU26Mt7JHA01cDZ2FCCQwM92yAM58f4yFcxIJpA3IQshjHKsrHfdNPS+C0lhwN03 rOSol2URluuSQiQJ7dVB0fnA8Yyc94sEW9lbovIWZSfTiisUAUZMYZ4h0tVFms4GcouW 8+kqCSHHiH4XyTWBsA7ZzNvm8OldB1CzYhsSqfJL0kA45dSNtxhwR/zAdZhbJky95ZIb 2q2g== X-Forwarded-Encrypted: i=1; AJvYcCVIUxOkYAgobP/cste1nWWl6ukFgpjmU8Hr2HQu2NamlXv87EjAj4Ok1yf1Su8ci35Q/PLfCztAFEEya+dnNyuSMx+oC2ylMnbN X-Gm-Message-State: AOJu0YxsNtX2ttN9qLy1tZhkWQSgXl2w2wpTgst0L3V6UD2aJAOzhQ/Y /fkQ0rg60ApZJOkcFLorzboYCJn16iUIqRcjXHCoubBSZiuw8qC/ftgyFINL8lE= X-Google-Smtp-Source: AGHT+IH8MPr5SHX8NbOpfre4Dyt2H2WS5ph4VEJ8agevnX7RI8saF6DZ/UBE7sdnWVD/jUXsHLxmXQ== X-Received: by 2002:a05:6358:6486:b0:1ac:ec74:a00a with SMTP id e5c5f4694b2df-1b1aab9017cmr527013655d.17.1723670780544; Wed, 14 Aug 2024 14:26:20 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a4ff0e5b09sm8115785a.73.2024.08.14.14.26.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:20 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 15/16] gfs2: add pre-content fsnotify hook to fault Date: Wed, 14 Aug 2024 17:25:33 -0400 Message-ID: <2bd333be8352f31163eac7528fdcb8b47a1f97b4.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 gfs2 takes the glock before calling into filemap fault, so add the fsnotify hook for ->fault before we take the glock in order to avoid any possible deadlock with the HSM. Signed-off-by: Josef Bacik --- fs/gfs2/file.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c index 08982937b5df..d4af70d765e0 100644 --- a/fs/gfs2/file.c +++ b/fs/gfs2/file.c @@ -556,6 +556,10 @@ static vm_fault_t gfs2_fault(struct vm_fault *vmf) vm_fault_t ret; int err; + ret = filemap_maybe_emit_fsnotify_event(vmf); + if (unlikely(ret)) + return ret; + gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh); err = gfs2_glock_nq(&gh); if (err) { From patchwork Wed Aug 14 21:25:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13764170 Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 967E71D1F62 for ; Wed, 14 Aug 2024 21:26:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670785; cv=none; b=Vk8IiUnmyJlUdYF1jutMFyQOzxxawdmri/RKTknNZK9Z33G7uOuiqHNaOfW6lE5CpJ8XPqji5nS6YyHeYY8OZ5yn078UWauvAa8xirNwMcvS99xVeSz/5pFXD3khgcUWyFzaFcmV/xIDH/tlYZl/XuiD2RuZtdiB1yzqiboHR3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723670785; c=relaxed/simple; bh=S0mezk7lokrHR+8fcU6LAupASbj5zKj9YDRU//oGrc4=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R6OKoB842fqrMiFwYPAVWoY6S0P/k+HwYE8GmeD4ObrwhIPjUmNH4ugEgesPFaesyzM61Vkqao8ldWApddxHNDUJT4zElwqKZZRAntuHoEnnLT6MP1C+LeLpZHk+j4jttdh8piMboDH9P80nDCrpxZkvcFFA9hMfwiyfu1GllTQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=yKYxjzMI; arc=none smtp.client-ip=209.85.222.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="yKYxjzMI" Received: by mail-qk1-f175.google.com with SMTP id af79cd13be357-7a1d7a544e7so17699085a.3 for ; Wed, 14 Aug 2024 14:26:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723670781; x=1724275581; darn=vger.kernel.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=/g0betjFPlSWGNNLN2rg+et8Qa/vCpguwAUALfab+wk=; b=yKYxjzMIHOKVD7hCfqqDbQ3M9MRTo7yLBD72u9lQJ+mFdhbDV088MXtG8urZrAPysj ybweUtaUr1xYRQjBHW3DQNwKBR6ilAPSyNAAgy4azinX3OB/tADp5lcp+O2Ao76NSl5W s3+SUN7NHq4J3Q3nBYeySTQ3XDRRkzuYWgokHzhhzEyFsSMOrU6fHRmzrADK2UmaS+sw sD5zjeXI7O0VSwDIQ8mbQfa/R0kBM46kV9F1c3m3bkfJgpk3+6b8oinyn11+cihhjfSq p80SBcXJJR6YPvjyS2+u5jPAGMxJqc5jGBkFwGpGNotkYebUJM1fS8IVHF/EZt02H1GS ngOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723670781; x=1724275581; 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=/g0betjFPlSWGNNLN2rg+et8Qa/vCpguwAUALfab+wk=; b=FAmCAtyICJWwx1bEwXuWJlCn/ytXCPEbLcrLquexW0Qj01mQIK9jebO3IvUjfA1SmV Vt9YYWQ1+cnjntx8zmrZFuwJVfzJwf5H3iRFLvjbxJbC+jROJg+ZvZ/9pQEWnqFD06gX F+KzXHJDepiynsG2oiMoL2on5hXPo3jGxdDwCn+FpOzyC+5zz6UZW/WgNAtrKOrDaUkk EWFxdc1bF/l+L+7GB92Dfe62rFvNx85CbPNU8x6yWnF8Msf7riPc/kqpsdUhOyrf0TJW v+KoujVuj+MxxH/Mhzpu0VP0aJrPy7x3H0Y/OWaSM69mgaNzoV5icsgBvSUwaIrflh+T IOfA== X-Forwarded-Encrypted: i=1; AJvYcCWEKCyFfvP3svtBUmea4Csw9AFA+spjoka5mW//RxQ75A5EvTg10RLqbwaXpRFTEqeh45c/+d37TnQjDADVWu9N8np9bdBdgGeO X-Gm-Message-State: AOJu0YxXMkyFJqWdjLTnWlxjmzxPq9Iea6GeXNT2xrfo+WUTKSN/dOq+ W2+zcjXX+zKhZ/BeoEHJThdN/9lPy9MaOWpBbqEWh28I2lyD/6+6hwqWj4QrEEo= X-Google-Smtp-Source: AGHT+IEwLYSHYYTuAZJvUuE3hfd+/zuyGZIMkGIICNZLTcaMoU6Cqa81XGFABSQNoiEu298KSeXnZA== X-Received: by 2002:a05:620a:288a:b0:79f:17d9:d86b with SMTP id af79cd13be357-7a4ee318773mr448400785a.12.1723670781667; Wed, 14 Aug 2024 14:26:21 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4536a072683sm480751cf.85.2024.08.14.14.26.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 14:26:21 -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, gfs2@lists.linux.dev, linux-bcachefs@vger.kernel.org Subject: [PATCH v4 16/16] xfs: add pre-content fsnotify hook for write faults Date: Wed, 14 Aug 2024 17:25:34 -0400 Message-ID: <631039816bbac737db351e3067520e85a8774ba1.1723670362.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 xfs has it's own handling for write faults, so we need to add the pre-content fsnotify hook for this case. Reads go through filemap_fault so they're handled properly there. Signed-off-by: Josef Bacik --- fs/xfs/xfs_file.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index 4cdc54dc9686..e61c4c389d7d 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1283,6 +1283,10 @@ xfs_write_fault( unsigned int lock_mode = XFS_MMAPLOCK_SHARED; vm_fault_t ret; + ret = filemap_maybe_emit_fsnotify_event(vmf); + if (unlikely(ret)) + return ret; + sb_start_pagefault(inode->i_sb); file_update_time(vmf->vma->vm_file);