From patchwork Thu Aug 8 19:27:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758130 Received: from mail-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) (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 8F229158D63 for ; Thu, 8 Aug 2024 19:28:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145302; cv=none; b=YfZpSSzQZKGDju/g/Ad244TTnQFJfOjlAZnN0NuQ+9nzGKzgjP8TuvwLdC10geQqa1T76RADWa9INKCFJjRnO7PN8LQP2I/qRXU6HGKexydMHCXGdl7bNTRolrcj42RtxTN3PLmLlmc62BRKCGhs57tRKsaWrOND2NJBh9vuN70= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145302; c=relaxed/simple; bh=2T/bTyebmmWzgd79hiDdf0J2vl5OhNotJm/x2K/voWI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VSLXvGCSFRaoZmROs76iWP3HiaOE1Ouazrir0t6GLuJRXALYYK04j1yxtjAK+I6QY1zxk/u/b8GRSW6Xyivl32z+OYhzVrKHXlRhBmIwgrh9fBTsAjWd3lBTP+ZT/P4oc0i36QhyPXsCURhr0gDsYOc5EQsDrbIsoMMODqtkW8k= 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=3LpYCTl+; arc=none smtp.client-ip=209.85.222.182 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="3LpYCTl+" Received: by mail-qk1-f182.google.com with SMTP id af79cd13be357-79ef72bb8c8so56940185a.2 for ; Thu, 08 Aug 2024 12:28:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145299; x=1723750099; 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=3LpYCTl+TTMHPOVTFUjxbAP5FdreSyhgMqNnVG8AMrrjShiMrZMhLtgidkLT/MJPDI WiATpKWSxlMkGnVpxIWnB4E/kOAzfn8XJY2Sid4QEFbARfIW8/znKlX90QQtEudEr7VC WHUwWDfSyathT4saIRCmEnaugWGKfY9PnN1M+WxnVPvFGrv2YdoakfB2RgvWbqEVC2Mi jJIYqt/17FcXGuQRxAmD/5iNfixztyyZ8b1dKqn+oeaOE2/MCEtp1G5JIbAcrSOYvATO 4gadLdC8utv4Jl3KgNDdoT0eRdZWf2iST2QeGK/URMvSXAdRnZIN6nRR+4fLV0dz7B5x G5Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145299; x=1723750099; 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=UT5Hm9baOlFe7oxpr4ecLCCg0TIPAKuYI0ikz8iLHU6cnXx2JQJhHaFLiFwGx0XPlw sa0yw4Xsca9svKodeiXiHkYe5fubHJycfjwHfOivRrpWw1Cg+aaEuMEPZBuy/mugjR2Z HwzgMCGKjCy9opJm0sasGyPeehU/m81bCpYo/SCDDbWxmn9VdDXv6CGyuv6LYftpA2RN OrSQiT2F9vBeKEGBoFuhx3KTVSVybaCpw5dqpVzZpXGk2Kyv+YvPWJSTEp2WDdYOk3hn vnqarckRhQMvx84m8PmV7AmK7U5lqHD9rsld9nJmsABtj492bJ4cNAiwfJCAkwnSdCvI bvSg== X-Forwarded-Encrypted: i=1; AJvYcCX5O4otcSaqQoS4zCWJKPjwgpZSDF9i55JussgDvG8+6FCSjclAadtwzRhe0KEbci7gH5VVwNS5d7ruNmLcCJsKKIrYwewaWlSh59zppQ== X-Gm-Message-State: AOJu0YybV2XTEfPh1XZuFVec2+uWMur7CKYc9t4HNsVpLAHLPcY4lGIA 9xy7+ksS0bSVavL13t0jjhGQnLOvvO1XOSmtE8Ojyd1biqJw7S20SFpjeu3S+ow= X-Google-Smtp-Source: AGHT+IHIPxOimm17lsK2mRhrmoBs8JQ3/lrD4oPi09uZKL/T2UFWVQatxODNJLMYL/PxGf3Q64nPmg== X-Received: by 2002:a05:620a:2596:b0:7a3:49dc:e4c8 with SMTP id af79cd13be357-7a38183e77dmr296678685a.31.1723145299430; Thu, 08 Aug 2024 12:28: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 d75a77b69052e-451c86ff90fsm15610521cf.5.2024.08.08.12.28.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28: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 v2 01/16] fanotify: don't skip extra event info if no info_mode is set Date: Thu, 8 Aug 2024 15:27:03 -0400 Message-ID: <6a659625a0d08fae894cc47352453a6be2579788.1723144881.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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 Thu Aug 8 19:27:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758131 Received: from mail-ot1-f43.google.com (mail-ot1-f43.google.com [209.85.210.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 79450158DA3 for ; Thu, 8 Aug 2024 19:28:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145303; cv=none; b=hIx2oNiEcEbIn53yfkC3V32+n5f9GG3H3qYS4rQIBOnHpeM0D0bpJmYAuMGpytp41/kghvJcFALiP48XKKdiVLVYedWt3Q1oUoGTtQM3MvXQ6/RABcwpA0YF3kkt6eDGDF8cEfl4YYP63RgmJCsvzoggqJYqUtOB6xqgy0QiYA8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145303; c=relaxed/simple; bh=AjMEINZmgDlFiuIMbDg0DxklpTafNFhb5Kj9+SN+Xhg=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Nb1+zZU1os5jBbAdynpPQ5CZ6YyQJsHMCwRvu0kG5hKhkz5lbKppdzNG/astdLkhjD+H0Suv+1pbjqaG3XUS7j0VpOzt9/9tagc+Pkiuy9SZjSERMG5M0JBd8zX1AuRSaG5+8JZdUJ+V4iMcapuO5cKJ9dRPo/QwpjCZ7U1uQc0= 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=VmZuSm0W; arc=none smtp.client-ip=209.85.210.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="VmZuSm0W" Received: by mail-ot1-f43.google.com with SMTP id 46e09a7af769-7093472356dso736661a34.0 for ; Thu, 08 Aug 2024 12:28:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145300; x=1723750100; 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=RfzczfZaBcEy/mBP9fMeuoTBUjG4OtJdJggqfPPwrUM=; b=VmZuSm0WsRD7m1XbH8Hsgb+/+gcZ/7QMskVskALgsuwDe/4zz8M4OP1RPEeRDRNJ1h 5vo+x6Bal8d8RDcS3A58ML6ZqZoJSrFsGennFj2GiqBglwZc4SazictEe1tHoeoP3oW3 j0rcejq5unt2oHjIhjRryNc0bx6eV8AjmJZ1uryJhiIF8aY/OHItRl9er3RAj+8zCX+j jAFa0dav9KlLhcDqEds30LorzVaw9KOMsT8zPa07Br9M69qYfgl1zYjJZGZAQheORnkK xHo+mceRW2SCaRbDVl4hGT22bTyDx/f/zPsB1UoROrI52gg5sst6C0Y4h5/1l0xYoij2 n2cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145300; x=1723750100; 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=RfzczfZaBcEy/mBP9fMeuoTBUjG4OtJdJggqfPPwrUM=; b=DAOfCNESd5k78kQvKTgHiSU7lwhrUJJy6UZebwEV/O3ARH99yBOuKlTR2FISgldbom 0p/SJ4dhr2B319Q/2yJU9gl09DMligO7srKR+rDU8Rn63G1/19F+UeB2Bnzm5qLzTluM qZgN8BGA9C3hzDpcyEQindhfcCxpqOc8DhI7qigKtjstOoZZAmeH4cYcEfk2CJbJfOCO dd1zX3SzPawNZsvmp1AkcViUMCqsX+kSKmGY/QXqM3oLSjl4siJ/0zyY6KxeWOWmz9FJ NlzuUlFmInW+HSNJFXuGAILi437lk0h3q6qajzEGf+EUXHsFftK/FMY7fufzUvXSof/h 3Xyw== X-Forwarded-Encrypted: i=1; AJvYcCW8TVYULz3NGI+EWLODuh93BxOwKVoDWgn/2sHkdKZtdLK4lgCs0mikbKLrviiVOxh1x1UXewyXj5DPq2h7G4rBCSc++/oPP7/sQvobyQ== X-Gm-Message-State: AOJu0YyCNBI9pNn4Z3SW9Ie8ee3Li0Bu7Ho6+lrIil9Z5P9PkfT8e4xx B5qRq543JBTj+5hgWchhC56sbBq4JvdK9DVD+pxovc62yrA6b4eGTVAEqmZHUrU= X-Google-Smtp-Source: AGHT+IFLYp5VTPJUoVcL4vvQ9CTbr2LLDuMn9IUx4rBw9P/IpMywByjToDcaLuZIzEi2P8UI2UosKA== X-Received: by 2002:a05:6830:61cd:b0:703:5ba3:581b with SMTP id 46e09a7af769-70b4fc2f6bamr4011410a34.5.1723145300564; Thu, 08 Aug 2024 12:28: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 d75a77b69052e-451c8717bbcsm15607261cf.35.2024.08.08.12.28.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28: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 v2 02/16] fsnotify: introduce pre-content permission event Date: Thu, 8 Aug 2024 15:27:04 -0400 Message-ID: <83e50a373eb541de178bfbbc518a4be2bacb2372.1723144881.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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..028ce807805a 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) + 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 Thu Aug 8 19:27:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758132 Received: from mail-qv1-f53.google.com (mail-qv1-f53.google.com [209.85.219.53]) (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 C3AA8155A26 for ; Thu, 8 Aug 2024 19:28:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145304; cv=none; b=Z41Szs/c4ETGBO4nSudY/Jq/ktGyEIrSRUQDlvsjgqlSYA58MwpMVwx/FbkThzhXasl3FyJwCzL4lsHe2CUppxwjOKyTPjYmZsAVe6Iht1jBe0WQI1InULlDEjYPyTPMZr69D3OyYocBRmQBse4v5xXcf09PV16PdCFK4MSWXtg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145304; c=relaxed/simple; bh=VnNy26VUpNnims6UXBrv9Si64DHRDQUU01RSugTpPdI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tFTg5xA0oo/9266lDEjdP9xW8SL5YDHW6Kyjcm4HI9n52xipfoktUy0czvXWIEEvLecCY/NoCbDQw/2VwtT2xoM7iIHV5Xhcd0vkh5jc5g1rBt5Y6V9m8asuqd+q8isTMlCegKe3pdbk5r1LUNETgvEKDT4w2WQFZH55y4glOgE= 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=LY6aieM2; arc=none smtp.client-ip=209.85.219.53 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="LY6aieM2" Received: by mail-qv1-f53.google.com with SMTP id 6a1803df08f44-6b795574f9dso7650646d6.0 for ; Thu, 08 Aug 2024 12:28:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145302; x=1723750102; 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=36ugo1TfvovnnnUK9ncR3kaa9VL4nnnXrq63bAYq2UE=; b=LY6aieM2h5DXZLs/yTmgyZ3H9TlyjJfNtG12iXwnZNTfTJv4CeClXC/NC2T1LX4JEJ t9Mz+XyATvrg5lYuKXe2VuXudcJRM/ztz6pD4qweRJgWnbMKB0txQtJMAX/Yu9uOM/LK YqAfuOZCjl3hMAHBVniJr6LZWM8S0Rj60yvtTrlbYAaxyH6ilJR+YcsdEjGvvvvmqHMV zcmp7dHgDZ6WAuKqnukK2IVZj/qJk3O6cq7G65nohXn8c5/werhpTj4zpBawkX+yqIkK BLzVLPV3mfyjF7iaAgdSvf1tmnbBj5KFeB4uh+Md8houqeK3DU5udlX4RRZ5gVsX/qb7 5sJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145302; x=1723750102; 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=36ugo1TfvovnnnUK9ncR3kaa9VL4nnnXrq63bAYq2UE=; b=uKsK/wXvyWwyok5nTwB7A4yob7yw3a8NStpXLjAZBwCR+78bBeP5DRTcqNpUchUCPF leHy61qvS0VrVSrGaQn3Cy29rt9OmW2VJW4oB/DeDrmzffqbfcsjpdlHeFv/dnHM5rRK FRx0S77mL+jKEctimmiKPd/Q4U+LXIgWllAJ2ssWmRhnGTfa61XWB1rq9EGyjD+/vdEo O3jGgiz0V7SGC/73kU5pGhFpNpHRdTNFskNdNwmZ75W28f2dzJ/tISD98jSD0yaTzva3 CMZog+GSvp0veTAB4rfOhzrEHCxNytqbG4J/VW5xROV9Y/msXzt75ycbUeaj8rqyy4x/ ayBg== X-Forwarded-Encrypted: i=1; AJvYcCXSLgQT4q+ttE+HJAWr7YZJPIbgnVIyc/69iONq0pijAtNSHcHE982eIhRRV/+OyU0+uptsrgTu4qXbuYj4TxHOnVtHTuAKieexLdOhcw== X-Gm-Message-State: AOJu0Yxbm+ftlYWis00eGgeGgGDvbYO+ra5BgED1xt7hnZVBg+4tvckc aNhIEbBysyq/yLUjZNcbnoJyKuwrr2d5xLsnQ8/9d3l1I5OO6yABph/mwiYRinw= X-Google-Smtp-Source: AGHT+IEHt4QHrJ4mSzrw5WD3LjHgxaV0kWINjLNd9fffAZjVvdMOxmyVWHGrqTt/kBpNz8Get2nkdQ== X-Received: by 2002:a0c:f207:0:b0:6b9:60ad:a9e2 with SMTP id 6a1803df08f44-6bd6bcae45bmr36797866d6.11.1723145301692; Thu, 08 Aug 2024 12:28: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 6a1803df08f44-6bb9c777e01sm69809966d6.1.2024.08.08.12.28.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28: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 v2 03/16] fsnotify: generate pre-content permission event on open Date: Thu, 8 Aug 2024 15:27:05 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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 028ce807805a..a28daf136fea 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) 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 Thu Aug 8 19:27:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758133 Received: from mail-yb1-f173.google.com (mail-yb1-f173.google.com [209.85.219.173]) (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 D49DC15A4B7 for ; Thu, 8 Aug 2024 19:28:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145306; cv=none; b=B1EwCps2XPlV+Lw+QR+WFsDTtui6h+LDUGQ8rXsqgsSHgu3eg8OE9ha2zbgU0J2fuzTSAh3rzPGrHRIw/uV3TNXSBXavmDr+Mq1GTY+Hlu5TYOxqES8XobbNQ1alQmf25pl9w85SMq278E1ky1J1H9dyqGxWYny/04wbsHKC08w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145306; c=relaxed/simple; bh=zFcM2soOTMk6hOg67wC1ZTXuQ05EaZXZUYhL4yxgprU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hACg5h17L6gzBmyqL6Nch/oBHGwY+H15cR8wV0QLjKtCjAGwn2q9IxggcZq9n9Jn1W4/ibMgLY5xxu6zsneSOcaihz5zIOZ2drp8jFN0hiGISWyPlO/TaBCgBtFeQBAPbaiiwRbwKHBrxyxbgzJmZnVxLZLQoEHAGZTbncgfZ/U= 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=1zuBMGyG; arc=none smtp.client-ip=209.85.219.173 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="1zuBMGyG" Received: by mail-yb1-f173.google.com with SMTP id 3f1490d57ef6-e04196b7603so1404156276.0 for ; Thu, 08 Aug 2024 12:28:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145303; x=1723750103; 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=WIzimyxswBclQJjUxcfERSoum/Rvft2jVU7lz6kF1FQ=; b=1zuBMGyGfIABkUSBaTnSPuaCYotL+ClPGy+gh0rZb+ba/TnYUNJ3/leSd08rSB5l4q 7QIJ0XowP0GKcOT28pMguxm1lpdkjnB/0vURHwuk4nbd3R85tbuDJaNKH0mqt/7WGZbz eo3S8enDy/aW/h8B1guKAFXuZbUG1WFY2VDxfz4uAEWYWT8bmkq9B+t7YXeQuSBC4dsn +GVjkGzRikvfAhhYyWuhhsRfEy68s+nA4AUqAsDhrxlCVuJgwxG3qdBLKbRNIQTJK9sO TrBjCDwmEn6mpoxEKlfYn0oZ3a9Whvfm01ykQ5d0uZbYIqFQsH72+VAbIqp/CKODXYaX Z0XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145303; x=1723750103; 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=WIzimyxswBclQJjUxcfERSoum/Rvft2jVU7lz6kF1FQ=; b=RepPjOojEJNHMgcW11dsOBDVwF3i24LbkgZS9jT2Xf1GaHat4BXuLplz1WAF+fz5OK SEVOWQ4RDamwt1/5wCHTY2mRSUZ4Xg5JXnfCsvbLoiIkW+d6N2zO6JKO9bvUv2HNd+8H Qgk6ynVNVDgJjZdOLNYFHHQ1a67KkCWpZaLUjK4hGR3kRRjBD5UcSwt4rs2mmpvChcLW qP7IpstT+EZ9l7yaZeuzKxxwDQMXG/zkoWAt2iqrKGb4CiPD+sg9oDlq3axAWLvFjYlt uD+fBjsWAgf8kzeQpAHGJZBOmILp7r6Qg5QBnjiW/P2lhHdUUh+M8J1uGNrfcWDVjo6L JBXw== X-Forwarded-Encrypted: i=1; AJvYcCVDoKti7thx82gCmf0PqhB4TtXdt5We8XotBwezC8OpbUaLikdbNQebRdpnahmM0srdb0RPfw2rvsp2+pLc5d94U4zDRgOLF1lV0Fh8pg== X-Gm-Message-State: AOJu0Ywhpc7yGnRKResv/0DD54Fx1ZRatsgp6C+Wf8ZfWPWMKF2dghl/ u3WqEltZAq9HvGa7WYQgrFKbX/Gv3i31jnLE8VprvYIwcB7+wW8cM8l9ntb6azY= X-Google-Smtp-Source: AGHT+IFN2uY+aatZYl9pzjPxRfjRO57s+88T7gzgtnSck91WLFz/T2NMwe1+6pQSCfmTodKYGud9Sg== X-Received: by 2002:a05:6902:2085:b0:e0b:f6de:2ce2 with SMTP id 3f1490d57ef6-e0e9dae2c02mr3161822276.27.1723145302883; Thu, 08 Aug 2024 12:28:22 -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-7a3785e27fasm188463385a.30.2024.08.08.12.28.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:22 -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 v2 04/16] fanotify: introduce FAN_PRE_ACCESS permission event Date: Thu, 8 Aug 2024 15:27:06 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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 Thu Aug 8 19:27:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758134 Received: from mail-ua1-f43.google.com (mail-ua1-f43.google.com [209.85.222.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 56FDD15A876 for ; Thu, 8 Aug 2024 19:28:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145307; cv=none; b=oxWtkwemnlmvraFPtu4cV9G7TozH5Fe7h5g75QK7MN47/GJQ+aEZYy+VAjHQhY8NQQaoBGoO83xCqYiCv3TSHr/1kj9nM3WeWqlameI8AFDktqT7KNCuxc/dqxmp21hCmrlkfD4GRtqu0L5C41cYZMa8omiDcy9ez5x53L4E5x8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145307; c=relaxed/simple; bh=iunaa8k8tOjCE0fjxciqNBkoFDQCNOOCmaZxaYuGWPk=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WPZ79/6r8zde9c4PmOjgJAmJyQj7cJGomLrtFraQHhL5F8XQ7AtFjTFBeR1CGrtD1oKbbR/yqwN6n3ttscWHr5+vCWHkR1wWEOCo4VS7BsM3CD779G6aOFRHfK3QKRxKrsRAmJiIafxHoF9rN2dtN3KYtHz1DUQGb1mn5EsHoEg= 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=202xW/HY; arc=none smtp.client-ip=209.85.222.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="202xW/HY" Received: by mail-ua1-f43.google.com with SMTP id a1e0cc1a2514c-84119164f2cso355066241.3 for ; Thu, 08 Aug 2024 12:28:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145304; x=1723750104; 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=sw6Pmt3QfBKQRU9ccrV2L9s4E/3XEH78F2jlIvWLGp4=; b=202xW/HYgel/bPyV7njYVtSLFibzSq4whf28Yefobx9bry8t4ALHMgF05YIZt3zApu 3zZOetbE8ymSX2IrjGebHOSJacU3U+XSKOchxZ7Tjc3ntWuDr69+Z/yuWY1smFZfULZ4 GIcU2Of3uBsrKH1XrBz6L9FGTylUeRHQJLEUTthNUvAn/mpTp+zTT62zp8Z2DlV8RATM C9XOs9foF7tEhbSOGZ9BD2EDS6C2ayxdj55eAmfVHawWp90iKQN8tn5bDuI///N/ks1P uR3jyoDd+KbMmfoGX6xed6EmyavKNxvJ249ywfLfCWWeRy106cZjqJhjsJdggkWUc1JC EkHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145304; x=1723750104; 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=sw6Pmt3QfBKQRU9ccrV2L9s4E/3XEH78F2jlIvWLGp4=; b=XBWwgoQ/xg1a924QzhHv1gG0VvWMZetb6BjcDKzrDzYDZNmswhIYIqz+U/AJcO+M6t gMKohIs80JvcJ4eGX372aeOJ46fcZkfDt4BsC6I7pcu7KxTXRYXj23TVYyJNCOqW8VWU z94IzCRcUZNkVUQTvBZhVvqhaI/qcsbrqUsc5P/cXCGd+qqDGRWMtk0QGSTJpGIlo2+K +JYOo2OSktVwkWJKUHqJNceJI2LIhaC1CXfDusu3YX4phNI2NqnHqj83obtATt2fqd6k IVVkKfTjF7+rtIKwafZwUgMibf9St6k5mh4RUBwFCegDA6xlwt4vpJgUBCxoKKUdCEwk p+fw== X-Forwarded-Encrypted: i=1; AJvYcCXTq3Fci9ihR9ZXNNFvnI/eIk4BgJvF0mKcUKZTD+Kd/Q20Kp5nkzYHq3rcBCWisMZL+A+LjHhFUsa2nKficDQ5YvqIUroNxoLbKVjGrQ== X-Gm-Message-State: AOJu0Yy09QJdA20lLGsXf6GepmVXfEDKXwOg/kD/TO7AtBSebYq69myN ve8KXZDV8sjDOJdklbzBudGLs50apxh5+rxb4f1KXrySSLX+47K7nsbk/2T+OmNQp0MK5KRMSRa b X-Google-Smtp-Source: AGHT+IFTIoze4bXD1QlmBwlkzkFqqrnl/wiGJPb337BvGqoyHHOZ144D1AqNG7b2IMKB7qka0a1Izw== X-Received: by 2002:a05:6102:e0b:b0:493:e587:3251 with SMTP id ada2fe7eead31-495c5bd0636mr3571031137.20.1723145304111; Thu, 08 Aug 2024 12:28:24 -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-6bb9c83cbf0sm69344576d6.83.2024.08.08.12.28.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:23 -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 v2 05/16] fanotify: introduce FAN_PRE_MODIFY permission event Date: Thu, 8 Aug 2024 15:27:07 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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 Thu Aug 8 19:27:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758135 Received: from mail-oa1-f42.google.com (mail-oa1-f42.google.com [209.85.160.42]) (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 7254C158DA3 for ; Thu, 8 Aug 2024 19:28:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145308; cv=none; b=CNEpS3dEyujTz7nvy2oluJs7WTEW6VBP1UR9pe+AIKlndjhIDuhtAQTF1+md2s6G8Vyj5QT4pQmtei9n+SxqzTeRaxIp7J3iSfTBFjarTEz0t6FCfL1Lu/2mtdwbHCFl3986oprdR+HH2X0ajVYBWv/G74XKUpAENAJNNwujF0A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145308; c=relaxed/simple; bh=WsmgXkCKE6WJb4VZozDBI+nYHL1Hhl1EEmXlR/6WlbA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bNd1eCdPjoCRv2CdvJYTaKAbgRfmLjg9VjzBkMwRntC1maHPz4itd17VRGomGhA2SoGmRaazUC/1TnqkZyZMPWWxNfV+1xh3PJ7guZppbrxrOMLY9MPoQLd5pA8HtH68vB0BIDAeO+7MjidUqIEZ3+FuwDsCpq7RM1z2v4F/4as= 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=Ms+v8c+K; arc=none smtp.client-ip=209.85.160.42 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="Ms+v8c+K" Received: by mail-oa1-f42.google.com with SMTP id 586e51a60fabf-25dfb580d1fso640254fac.2 for ; Thu, 08 Aug 2024 12:28:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145305; x=1723750105; 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=xzenZXFB+zERTZqh582MYbJosgZh3nXgIyzY6rCrvew=; b=Ms+v8c+KenwX5k0A+qya2ksMSePu4yfnloYy/2GwMs7amRBtTXP4TB41iyxz0e+0KK R1vNCNZccOmveTKD7PTMzgNKUAthdE3ZKN+CrpueANhfKvMGDVVrhx5ICkgp75cHBq94 QTFVNm/QKhThO9O0Eaoe23UMacqcmRWzTQviLlKdMdTVCRDXc8evm42IdkA7n9OijaCj iHBObEKKJRBx8aXFz70wRsJ/F6pV7IAe2VlJ1GgjDrQBcCJSuZKD4wsz8OPpJa/ib/ST 0jFO2QkeQkZJQ6fSW8QNyD5pweUKE5aks+P3bC9qisQMNhXOa3ObzAy6d9OtVVSGnnja mByQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145305; x=1723750105; 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=xzenZXFB+zERTZqh582MYbJosgZh3nXgIyzY6rCrvew=; b=xGRTuRh6dkrPdkPMvP5c35jktNPrcwconxD2E5O/B/LIMdpBpH8K3ct44ENqt+gW3N ADqWDVTMvKNZ8BQlTizoIDohhAghjPgoNVpq3Xfz0+sDWZnQ1+R36TonFtYPQIKTwB9A I34BSSkeovBeAzo410fQqCLE1lEIsMwN4YmooGbc/DjtQC8w0TBPfE13lfkxHskcfb9d BvY3U2LNTYcmGRF897UZ45Y+c70EfbYcbdKce4RHTsaDX1AihyPuXprWIg/2c2qWV6Fo twG4KlncaSPzH0jbDpGEsQF+JamFfpaXGIg3uA/jNx9a/PIo89oG8VX5XJe5n+OapYIh H6Eg== X-Forwarded-Encrypted: i=1; AJvYcCWbWqkA6/HIL4Ku4T29wD//p/RrL0W29/hAijhoS/EZoRx4gRZ5FOz/8TGfljSWmkPBq1u6nY0QOCVSOuG0Py3pn7WqmBKk9ZwwodejFA== X-Gm-Message-State: AOJu0YxIWYfhbmuTB6BXHBdYDMSMH4eAFcz/IUsPJljJPndfdRZ+bK6a MIsk1m5rdBxPKviLOhDInF9XovnJVPtfj9gRZoFUrJHwn332/L/y+E9eWhlZXaE= X-Google-Smtp-Source: AGHT+IGkNG2N1guMTFmAJrYc1Yq09hr5aUo8VHvIDmVB7iXoZ+EZU+kk+PU4rcOuv/UUDPzrVE22cA== X-Received: by 2002:a05:6870:7247:b0:260:fd4c:6506 with SMTP id 586e51a60fabf-2692b63ea86mr3647958fac.19.1723145305309; Thu, 08 Aug 2024 12:28:25 -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-7a3785f254csm188758885a.68.2024.08.08.12.28.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:24 -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 v2 06/16] fanotify: pass optional file access range in pre-content event Date: Thu, 8 Aug 2024 15:27:08 -0400 Message-ID: <4b45f1d898fdb67c8e493b90d99ca85ce45fd8d9.1723144881.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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 a28daf136fea..4609d9b6b087 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 Thu Aug 8 19:27:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758136 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.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 5A37815AD9B for ; Thu, 8 Aug 2024 19:28:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145308; cv=none; b=NfcMC/66roK8NO5sdLRcASIYQtxypRVyCcgz82B6UwQMDyTmUvrO0a7SS9dA/Jgbns2qyfHgk718qPIyfLoSRpor97SGevOC7k0k9HxyQmxw1KCWqFtCZLkK8YZH/gCLAckvDMlUkccvGmfh9yNsXolJcK9z/6d/0Ro9ZZX2Q3U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145308; c=relaxed/simple; bh=lM4E+0QDfWeiHiQEc8/d7xk75Z3XLq+R4ATU188GEVg=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jWQVg48Jzm270ybBRVfYDUS3oem7N+acQ/WJMyVbfCbZDG7ZiAZqHCXv20mGWYK1ug7BaJSb/xFN1gy05aeU0QsTPfOxWbPD/AwHBDKzLIH+r6WqE/C7bfTM4TZPOikfunfoBvTjBKR9cjekzco6DqVxZ0X/Lze/TL5eCkJfjck= 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=o0I0Kw42; arc=none smtp.client-ip=209.85.160.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="o0I0Kw42" Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-45007373217so20049041cf.0 for ; Thu, 08 Aug 2024 12:28:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145306; x=1723750106; 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=EjfT3NMblUo7sZTNjknqk3yM6nPmAl4UxLqtdr8jd/U=; b=o0I0Kw42ZFGX0/H6DLAvadPYxR59ucVUzdlSx2wx16xVACVdVeDa5j5CdDImTDno2N Cg5yhKqb0LYHFDs0Ibrd27lsHEVfzN4B800gsFd/XTXfoMQDxtT45UmJnNwPVsMEBsYf Nvwki25hOYz/Gcd5LSJpCFZHLeAVA1/YdYXsmBe0zl8+XPvaafrlfw6Cfs+L9MZPV/2+ c6mXbLViGCbHFpO+GpTZmO+DZfhQl/mP3XQ1++zYstvG+3jgiDrPcgMnlDZlnRlRCiyu EKVE8ucWmsVc8Zepq3E2sxhik/5rrM+6oeXTG3KL9XGPDkegjt9v2LSUy0oehFtsQjny GJ9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145306; x=1723750106; 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=EjfT3NMblUo7sZTNjknqk3yM6nPmAl4UxLqtdr8jd/U=; b=EOGTpXcHg8vPfzzylKJ4OCUsz3DDu9LJndl3Uf6naLDVmGSUS0YEDXwokgA4RwYDff 7P+Y8r70zeoPzNKQ4GC88Vx6wDieibLKxHzvG25mbvTbQFVLlsoJAlSJhlLHBdZiDXM5 I/zAXMyPVVB2+hdsWfvPYbAHZQAHIgNwv7/HuwlyyzQSQoIFiM+2oVbkGwYpmLhdPj3E E1X1tHPNRzFbc1+TPBFX8LKtemwb0OvAFFwyzwXa+Zmf+NbUoLzzxO8zaKiORpOz2Gxy 71651h1N2ZYWJsy2BoxRI/onRLEz/fxvJXuVSJGqT/OclUKGDQZpQyHLAPinE25kKdq4 mGHA== X-Forwarded-Encrypted: i=1; AJvYcCW4W9V7wU8r1x7Cf70XAfrQlT+Apzrw4MB/qKKPmNF2zh442Gx9f450tLiXJG8WmyOv0F1M95F1h7Z8y7B6tuKI6+8Z+5IRAiy8y/oa/g== X-Gm-Message-State: AOJu0Yw62Burm4b1GzYpF+dfLbBS2dHXIdYqzMGUB14BhPhHyq/JLVOc BP34t4zZ1ScL0uPhpLBoqHg3CvBmo2I+Qzcn4lWlrdJ9YR2qiOlY2pjVQBcofj8= X-Google-Smtp-Source: AGHT+IGSh+lZaiCIMZedN+ZNZCZXZjbXsLY7KjtrhuBLT/ySiKQHjkhCsGdxbJMDj7u/JeFTXRzKag== X-Received: by 2002:ad4:5ca8:0:b0:6b5:e761:73ce with SMTP id 6a1803df08f44-6bd6cb0fcb5mr41503466d6.16.1723145306354; Thu, 08 Aug 2024 12:28:26 -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-6bb9c76aceasm69502006d6.6.2024.08.08.12.28.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:26 -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 v2 07/16] fanotify: rename a misnamed constant Date: Thu, 8 Aug 2024 15:27:09 -0400 Message-ID: <5d8efd2bf048544e9dcc7bb00cb9013837e3db6c.1723144881.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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 Thu Aug 8 19:27:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758137 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 8D73415A876 for ; Thu, 8 Aug 2024 19:28:28 +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=1723145310; cv=none; b=e+XOrjZdIs9Jdbu9nV16vfkUZL2a3ADD/jbwyHguqTCOc9kA83vUYjvj8bsJxFkp5RI1FnH8jW8I+/34wh/kygxM97caLPHc+lpZfNbKVHDMxXKd5IY0CUsjyQ4OE7uGrlVOYwNWxJxzlXvI1RnTcSErqY9WTjxFaYxLTKJsJNk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145310; c=relaxed/simple; bh=x/zxNttmCT+y0dOsXy0tvMYfEvnIhvz+MK7V5Zr+cWQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sPxnCtD+YthdiHpTpPgWmLW7zeQxvJG7VuIErLfXqyCSbMjdMST+B8l5OJUYXU16tRJt3A/ZD5RNsf+67HwLk+TONuCYqHB+isgwRZiocrvcXTtIiKrg5j/moNlYQ7z5q3DbSbCm5HWw3qIMguqkS3JQUM7r4P+9FmX98D9QSSM= 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=kEHDaZ0c; 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="kEHDaZ0c" Received: by mail-qk1-f175.google.com with SMTP id af79cd13be357-7a1d3959ad5so118297685a.0 for ; Thu, 08 Aug 2024 12:28:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145307; x=1723750107; 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=kEHDaZ0cqQhrlEz/TWdEvjdXA1jictnrSbSJj9gyZk9fdKC9pQ84ihJUpjkzWO3YfQ BbvmSuSwNiUT0rmOk9SB75Y6YK3qmMnRqIn+kx9g3KIApG0Vm8p2UMeMhdXt8VD5QjUD rP2FcCE1IDSWicZIHgOD3tNQq27kzNwxKcuhnXrv/hI2/xXgE5dw34c5HZH3Uixjtmd8 uXCHNhOQgAUZHzhNqqR2nCa0GSSCgggxFZ0sqZeEGEyB0V6h2aEwpZNIG9CBhIRY8Twv +3QLPN9aqi2TVCrB9SA4+GEIIziqS+dROtY/Cl5rog5oWE+Z1szuLg+TjC6sruZcpwYY qGcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145307; x=1723750107; 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=hZwogZ0eVERdp2g+e9aVFJU3ELZpeIEMxRaXuQnbH2EE7EYVbSxnmR3uMBvOkRzriy M/h6szKfYVMr3x1Zequ4YX/P+ZAJV0DP18O11NCz8CLdq2RhqeXdUpdFAAzw6K8ZAIeR h4xvKmLs+4hFg2Dwc5wn+dD04MbGZnDfL108Xg08RvXAxB3NyQd2hVZPAeBLSOFdmEqb 0nNsJxq1Nn7iJr2sLkSCvSgnLPOlrrqkCqCaB4nUccERcBUypzgi6tb9B6+2km8encvA lJTVPivrxuRvav6CCUGS7xVlNC0JoFx8XWJd2Z3y4hJEyRwMI6fzqMh1as8CW66mUCqo RfdA== X-Forwarded-Encrypted: i=1; AJvYcCW2kHZCnN9jfpUa01PeaaKHwzPiWuoXMEiFUPxij9EPxIvsoUYKljI8tDaBHSpfAVCiyJiI4DlbnAd13iHKyD2nJIt60JRfluOB2450sw== X-Gm-Message-State: AOJu0Yxc2jccvn38KDu2ZXtt81IDtJAS6Afc7FfKpfzSVEJ7lH6sNOW9 ENmYnMx2bXqXayw7F5bXFvXVS6apkw47iD60JKyP/9ahr7r2zGnzs0+Ot6eaRoE= X-Google-Smtp-Source: AGHT+IHevrYqSvfIR9PpV+71TzE1Gr09+Eeauvh798vi19CUyPqYAs9+KzCuXWncrkXC8qcitERfHg== X-Received: by 2002:a05:620a:2443:b0:79b:a8df:7829 with SMTP id af79cd13be357-7a38247641bmr454452485a.14.1723145307425; Thu, 08 Aug 2024 12:28:27 -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-7a3785cfbebsm189000985a.14.2024.08.08.12.28.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:27 -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 v2 08/16] fanotify: report file range info with pre-content events Date: Thu, 8 Aug 2024 15:27:10 -0400 Message-ID: <3c49d7ebc450ceae5433fc28535274fee23b2c01.1723144881.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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 Thu Aug 8 19:27:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758138 Received: from mail-qv1-f45.google.com (mail-qv1-f45.google.com [209.85.219.45]) (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 8B95215ECC3 for ; Thu, 8 Aug 2024 19:28:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145311; cv=none; b=Wn6+EyWFou1UWIAAHCq5wScosef29qeTrDHj2GD32xHffFqKvAgkaXLE0Jzf2JHoYbIqcEitz6bs4vpbXzRyNXoF4nfS8nvbxVl006P9Bz/9gMFEXMToWwwJgAS3jMSlzYuTqtloC+wGwz3FfBAg9WVLFO5n+HffU/YHJ5kCgMg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145311; c=relaxed/simple; bh=fHAWL+liJK7RUemLt/Svh2h8UMEWyBWVNZVgU9u6a6A=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gfUD+bDkn3KDHmGTYkrQ+xu8doJOYGcEq2H4oZpQpVTJQt+hhia3hxwK3PC3NdNMC8HS3GBMROBW20gEnr5wrxhJzqOlS0o9T1v9KtlV3js0w3r4H9rApY/A8f9FwewXRyvH8JyACe8BlD62Cdrk+e1xoEYDpexZJHC8IjcZB64= 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=YRiiPO2S; arc=none smtp.client-ip=209.85.219.45 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="YRiiPO2S" Received: by mail-qv1-f45.google.com with SMTP id 6a1803df08f44-6b7aed340daso8026946d6.3 for ; Thu, 08 Aug 2024 12:28:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145308; x=1723750108; 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=YRiiPO2S/LfKt7amV2xx1zCZyUicvZq+ASk1SKOp23bnFp2YIdDoocy7LR/wzlK6ed u+MP6htIZPvjQ0mPiwSc/AzoMI3NhTU8HmsMXoUDRLl92ZcwQJFrTIhN5sKeXoW7hvZG s7WhDW9ny4ZjRp9cFDCvQUwPtuXK5OGIADF6HbqoYYHgAwWKGqVI1CGOfMSavh+ZZu+S fwxXpw59WqS/mtpq/UK8Aoyvc5T4h5gOcBEb2ggOlyYvp24DV7ZiC90O3cBGYIPlKWn9 hDkSIGUtG0hookXjRBKHmKT3H9Fef3c1dFVkbHSuE/MYK0Riiig9d8kIRYYKXssUggB9 JL6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145308; x=1723750108; 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=wFmHPToPpPyJmSpGbXqibv3stCIRL2sUGqmn6fJojCGezBlsnEt7X8fvKgIWbl6JT8 rZh1c3ZRqmdbvnebnfB1MjMwsSjy/qrih5jIUJQpXOPKlqB89irEQfof+MHtU2VSk7Go yejh8a6etFG7YdJcWkhMWC0xfmbINfMfvECQ5NiK+/MHQTnbvjW8r+9iFkz/FeQvgmca 92nmHW4pW2MSXPbmvtba4A+CaZg+zuCdvtgKabMxlhndQh12WFkDGqoz5bptCr0hFB3E T6dXQ2TUozoTyUbSz+SvPr5iuDZ8dNmrIqoqY7V44hLOyIla8gm1Uz30pgVICPh7KGlK v9YA== X-Forwarded-Encrypted: i=1; AJvYcCVb6KZ3VKADxGkhEHb0ZYn1VBmiuEB8r1ZyuQoAvjQh9s9kbfZftLaXtvQHXXXhWw6zAW3aLfxwZnZtJYBCOS7BVV25IfEjdpvWNgJyaA== X-Gm-Message-State: AOJu0YxlsdxbEJaBwOxysrvUPDL1SJkGo1IadKx0IsQPZguU7IOd0k+h zL5XUQeyh3z2oed2VznGpjdOyAELcG16n47oOdjE2bChnVA6ixJTBVvoR1JW7eg= X-Google-Smtp-Source: AGHT+IEt65g5+uygOwKtL2lYj+QBCC0ALQODQD+EfR5n9rijzdpvGKarJDG4JHdn+2nYq035wMKfew== X-Received: by 2002:a05:6214:3203:b0:6b7:a182:4130 with SMTP id 6a1803df08f44-6bd6bda50d4mr42997116d6.49.1723145308451; Thu, 08 Aug 2024 12:28:28 -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-6bb9c83c52esm69183136d6.88.2024.08.08.12.28.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:28 -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 v2 09/16] fanotify: allow to set errno in FAN_DENY permission response Date: Thu, 8 Aug 2024 15:27:11 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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 Thu Aug 8 19:27:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758139 Received: from mail-qt1-f171.google.com (mail-qt1-f171.google.com [209.85.160.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 6F68815EFA1 for ; Thu, 8 Aug 2024 19:28:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145312; cv=none; b=Cwajc2OTI7HJ3IMo9p9YImA5NlFZKRSxZLw/FhC2yM+vDPL0dubNfA6m10QldILppbwpawCBXGrkgsPYprlfVfg7rI5FC7gPwdowYAkPqWRdKQFIzoSk0l+wHCN3Qu6GRRGSpeJBGBOHuwiglbHgbnkVa+e1HbDdP6qD4kxVHk8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145312; c=relaxed/simple; bh=GuL24mTj9mXM70tRp2J+zNN/WSRlPMuaehdquMxMOMM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E1kcX3wtkbvfFqCL0qc0mE7zMPYBRYynIsSHynE4P3mtHbRf6dObpKXee67MLqgcEsMPncxY5a+SnHzjKfkkLPI/zmMsHUAS5S6ZJP6TM+iPvdPjlKvUlKPBSfo3KGsaNlgxIeKx0TmJhsBeVrv1oTLPB5PEj/YoQ4lW0RjFdC4= 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=YuIjDizw; arc=none smtp.client-ip=209.85.160.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="YuIjDizw" Received: by mail-qt1-f171.google.com with SMTP id d75a77b69052e-44fea2d40adso7503731cf.0 for ; Thu, 08 Aug 2024 12:28:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145309; x=1723750109; 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=c2MplehQscA1+uvrWS0C9V1o5GwHxjl+BfkNu3cTe0I=; b=YuIjDizwBSBPP6YTB4We5JJP1CXSwKk6cmjWPtWgUS+SqPJl5OEigR8GifxH3HYjhu qCFp+vzbu7LlIRp+y6BZPhUJVyM/IEJnvke1iXb3nPXL/EuPVyIbsn4eBepUiIAMMl/g mEnNVdGANOSxCTRL6Deiq3DoHDieAaH9igHfZ12bUp/N8HMF5SUOjHxsqExHsNjs3DIz Boux0Ad41RNNSknEfzuVwlEy/We9lQg7VfTRWrgXmbHXyPoPtIqo4/HhyOq/5f5eDTR+ ZzpyEVQLxPM49a6wWMm/mGaHP2i8HCiUoEYmlvO0cuiULtxHFq9ixHJLV6qzsQY2Z+NJ FdxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145309; x=1723750109; 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=c2MplehQscA1+uvrWS0C9V1o5GwHxjl+BfkNu3cTe0I=; b=vP9SAQe7SPxxv//+Jyu1bSb0y2fmad5s9Snqi2a3HMaNbD4GCvOH00eswAaTq+Wvl2 khHxEm5UQyv42bLKiywIyRLmnNE9AE033aRI8K1CzGRFgSw4ofQS35X7tXkzJMlRZppl HOL0FDipuYtRx33k+IqfK6s4mG7IcHNwzuPbEdZfb2J6Pg10R4tTvouBJlyQtxjLzZD3 mn3isem75XumOpnOfdeg3x2GB8iNbZq5IRW+3Upejoz6wDSHLmNHcfalWxoWtUoj/cQQ 8LfbAxMIg9uKlspL3AWm46+rvatApfYwywmq+geFA3Oeow2c/Xp3odtHPF4yBU3jD1HX YKFQ== X-Forwarded-Encrypted: i=1; AJvYcCW9SugWEGgj8GupDDjqs88eS0FRCS2K4VVNgryGDdBZlt/JYTMETAFVf4ZR4r1UwH0zOOIIaSA8BDxaKhd6jnkURVFHxRiN503KM1X5JA== X-Gm-Message-State: AOJu0YxEtzxefyRNH6d19kPy0XCUnArwz2nX5cZ0DmkqMJdhjsjZO2Vx uRcIsgzen37XoNsGc7+KaPubej+HcqC/UHaioKpY09lpX54Ie1zUO3FeC27D4Mw= X-Google-Smtp-Source: AGHT+IFDsbaV/n8kDCGttZbX/69iKd/pkdvlPw0dlBa5DAGrReE31B6U9TrZPgrw5N0QpefXodV8nA== X-Received: by 2002:ac8:6f0a:0:b0:440:6345:257f with SMTP id d75a77b69052e-451d42ff7c2mr40818221cf.60.1723145309461; Thu, 08 Aug 2024 12:28:29 -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-451c87db202sm15626721cf.72.2024.08.08.12.28.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:29 -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 v2 10/16] fanotify: add a helper to check for pre content events Date: Thu, 8 Aug 2024 15:27:12 -0400 Message-ID: <531d057087b9430839ddd6082022e29a9066ef1f.1723144881.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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. Signed-off-by: Josef Bacik Reviewed-by: Christian Brauner --- 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 Thu Aug 8 19:27:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758140 Received: from mail-yb1-f182.google.com (mail-yb1-f182.google.com [209.85.219.182]) (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 8F83A15EFBC for ; Thu, 8 Aug 2024 19:28:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145313; cv=none; b=e+TtoxTC7O/DNe+RCM5an7TUK3VprVFtpvYvaZLjCOnTL0DZMIKOpOx4U5arhl0BIAlTeiVEaZ6W+7lwTkIOMX5w3Ee8c6ZL+JZNjJJgEapTG2Z5i4DLjMuw626/hNQQlcrWtHUyysmiYNaQcOcJr/uTgSY7Uiy7nT2t/HXT8i0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145313; c=relaxed/simple; bh=4MNVpsyfCXtbAesIGplXT094r1e//DqP/WIiy3n6590=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dS2UrE3SyuXuHLNj7aDRTfFLaeMZgLzPttmxfML5NCCJQ+HH2nva8fGEtbD1ABWBY4QMzCIBLdw4CwVlNJWRviZfE7/chV90hSmvNHbFJCDefBbAbdei0CnlUIGT5zgKAClzIdc1DceTNVXHOxz5JuFZh8C0oewCnUgMV/W6/vg= 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=2we1+z4p; arc=none smtp.client-ip=209.85.219.182 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="2we1+z4p" Received: by mail-yb1-f182.google.com with SMTP id 3f1490d57ef6-e0bf677e0feso1309861276.1 for ; Thu, 08 Aug 2024 12:28:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145311; x=1723750111; 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=Vg7JhZ5Svel94ZByv/XfsZ7gyzFgAjs3SwDkqZ+V3Ck=; b=2we1+z4pY6sX0WySI8wotX4SlU3xhM5evV/Oys700n8Hhtk0EbQtDjDwvwuTSpzEZi VytvfXcBIDY0Br2ZOgzuwbIF3XuHmY8sP7N6gqEiegj+L/mIQzZx0cL7CXjVpy+5D9OS 6tp9m1KfJuqtIs3zhL1e41LmR6wkc1+g7PwuRRllKkYtD/hv22sHtADqTdkdXw7TVTnN h8034jKOPyovn2tea2po7n7/zQ4y7T9aNqYcXuUlVuA2a4NnToQeAKo2XdM1v+X9hquE eP7ib43ufLYoLUlFtHQYCoxfRgICRkvbUNZPfYNuXjdytZfSYLFscYk+9e0bMO+ucv+d JQKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145311; x=1723750111; 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=Vg7JhZ5Svel94ZByv/XfsZ7gyzFgAjs3SwDkqZ+V3Ck=; b=L5wysPRwkrTmP4Dvl9dilR8ZQl4qAcwEBDhMckll3HbWhMSgwZO1q6YU4ZCHehE9qS FgkvfcXWGx8iqBwFXedR5fH+E6XwFeY3xyZWLMOyBm8A5n1+QzduruwgpNh6eYpOqM/h Tc+kvEWAn808wmwa02x4as4HE095jri/H9iCyACX97HYvzeIsIJ3lQNzY4IgefAyUJcW UOIFug/IrdltQSgth6052Iyj9GtBDKCMZAiFZh5EQF5CyeUj83f446Cu3N+x6bqaImkL xQexTTQ4hCQB3V8w1ti2UtC6bL5YxFVoBvV/HSmSPN/4vLDj37+kYKwgu76nIZPXBaaK K5+Q== X-Forwarded-Encrypted: i=1; AJvYcCVFERhNIntTJQX3jCZeXZSPFmu9nNcQguVIMCO0xnynkSDMsH0gTZavPfRxX3xcthD3N4kgFEMFHkFbh5aq83S3e2vrNnTeLCxzCSn5zA== X-Gm-Message-State: AOJu0Yxik/uoOM2uD536gXkcDhuCH4rUW2thOOm6yLDxrqO08mNCbmnh DpXntPvmFpeOIwQXlXRMJtdhtox8yqpcuFXtLdzos1csLM2PMQuO2n26L5+YilY= X-Google-Smtp-Source: AGHT+IFFeTx74Ds9CwAg9CiYnTBosAK5oHp1F8D1LB3amJF/JdGVkj9ODxJsKT8POKKxshfGvAs+zQ== X-Received: by 2002:a05:6902:10c1:b0:e0b:b2a7:d145 with SMTP id 3f1490d57ef6-e0e9dbcc39amr2607498276.55.1723145310633; Thu, 08 Aug 2024 12:28:30 -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-7a3784ca5fasm190091385a.0.2024.08.08.12.28.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:30 -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 v2 11/16] fanotify: disable readahead if we have pre-content watches Date: Thu, 8 Aug 2024 15:27:13 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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. Signed-off-by: Josef Bacik Reviewed-by: Christian Brauner --- 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 Thu Aug 8 19:27:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758141 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) (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 B83C215ECFD for ; Thu, 8 Aug 2024 19:28:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145314; cv=none; b=eJu2ScofT92HQswKxdVf3d06tTUJGuivJ755vstWSY/1v0lJNwZadTYhqISXnmgL1ssJVqZ3qExzR4i6x42gHX6NJwzjtaWMQJm7Hk0DhN5Q79Y23eD5pBhEUH+4k7fnObwKSJa1aLK9pv6mqAAIy4Ye3Q4xj8XoNJ7AtsJY9/c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145314; c=relaxed/simple; bh=SRLbOqJ8zAl5qD12oNCtm7iiNr2HczkPZnZesiep1gI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ESzuapz/g0JKpJZJgxUKadfXw+mN2R/TuXRCUu/+dTH7+llYfg1gxxug9dVw/u5L/x2IqCzXBbQ64UDZY6b04UXpT0eEVXPZnJgX2ZafdoM4L3xVasZz4hA9c07Uapo7574L8ZfGHmto0AMJZDHTWQrVekPXUyK7qoc2Nd+z4D4= 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=f8azxr/1; arc=none smtp.client-ip=209.85.219.42 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="f8azxr/1" Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-6b7af49e815so7867696d6.0 for ; Thu, 08 Aug 2024 12:28:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145312; x=1723750112; 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=CZ0oLpoVqilzi+VChanZFK4WtdGvxjCPwQLhvVUYV3Q=; b=f8azxr/13vGJspiin4z3X62CCrN8u+PfWvvu8j5ETd/tbAHMcZxzgqMAsS+jkqYCHD Hds6Lj6IIQHPfBlW09CPtfF+PknQeNQSSp4U+NaVe8hn3yZPOVcqcwXyK9o1PUaiXShs s20nZOeRBLvUD3OQD6CYyEH5v6EdtTk2wJRMx28BWGtr395/zSxP5pcfQOizfO3uPK84 UocQJvifAaWTCvvtUiBLT9owoGR3ThKc0mf9zPe9imKse9KIyfXqnyu75a1Gvqi7Ft4l rGy8iAH+6yjXFWitwKLeWkQvvxI0BIHrO0xXVMHRzmoKz3uMS/xKadbIyvge25Opmg4B KKaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145312; x=1723750112; 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=CZ0oLpoVqilzi+VChanZFK4WtdGvxjCPwQLhvVUYV3Q=; b=AF5MFRSo0GIjUCw1AXVZVIzk0gjXw3HJr8jjekXs1LXermtSp87EcKONSuIqX5xkOs 4t3WISUDlScr1Y+JTO9mtpr88Lmqc/pDd9Y4EXhPNfqWNOe/oLA7ugOnK4uJv5ZceC1s 1S0jiaohkfhsLW9tgUL8lpdKQaUc4eM+CEvwDF9HQyZ7KqLXNPvTLVHDbX02q+u3CvY4 rzXUUfn9mK1B5t6s7VrNCwwzG7C7VJIdI+gEgojfawqgwI8cXwOCRIvuusZ4uVtZRYF3 fho/nPxiP/MEehH64MGlRIzVlu2cc3qqLkJKN6i1HCydErIOn7QvEJKvU5873wydZ6UU 3x3g== X-Forwarded-Encrypted: i=1; AJvYcCXl6JfZMEPDl5kyFfW+m677BjVBuT9d6ztcfsfpIc0y2ZJ3x/nfRldxk5cI3MQWIH972zuTmHwJVwC7gwJJsSkhaCOPKKHu4POmgEL0Bw== X-Gm-Message-State: AOJu0YxWwSlH7ZvVibfzd3XmL8uhGLuIjiHWEep5WXP+Uj6hBcSj3oK4 DI5/Ow2Qs8uJ/you4RP6blrZLbl/0VqVVLX3U6y63kBtrvCo59nztbu/eqsY+O43OnZszEKOtyz i X-Google-Smtp-Source: AGHT+IFCnvBLMQYwV4xdiQDtXkQ4ye2f9PGHfgns1lK+Hj5Gxr/FET7hfoWmkMYm+Nx3ymsaFXyQDA== X-Received: by 2002:a05:6214:5687:b0:6bb:b1b7:a690 with SMTP id 6a1803df08f44-6bd6bce1ea6mr31418406d6.22.1723145311777; Thu, 08 Aug 2024 12:28:31 -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-6bb9c839ee1sm69475856d6.89.2024.08.08.12.28.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:31 -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 v2 12/16] mm: don't allow huge faults for files with pre content watches Date: Thu, 8 Aug 2024 15:27:14 -0400 Message-ID: <8b4c1abeff52322da354a4df2881ec13b7493fdd.1723144881.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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. Signed-off-by: Josef Bacik Reviewed-by: Christian Brauner --- 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 Thu Aug 8 19:27:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758142 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.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 2A9D515EFBC for ; Thu, 8 Aug 2024 19:28:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145315; cv=none; b=tl3BMRhXhGUcL1GReeNz6Nhqm/bUDRtIrn8BfIdqK5HJJpJgMA5z88xhIoX3mZxbqEMTWwlNhz9YGx1jU/zKdp7jVlU/7djHOknMPSCXoSyP1hRMtqEiGeDnUkENKhYcuPLWfq3xtWwzi5lsZRiIUv+gycsQsshGMO6tGuhMOcA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145315; c=relaxed/simple; bh=QxwPx/y8cQKYt0urtviHOcv6Dke8kHBSjkLFA9JU5oY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mQCS2gB4WpeGjR9E4XTvqk63b6FL27uX1Wo+5HDK1PRkZfE0vWP78bjxATMB3bVR1EVNC/v327o5mqwqzT9BphLED2Af2xC4hiLfOPbVokgkgq9IgoSux3KL2aaD0hKHYLeW+FtFqLYEa+3qSU5fwwYZBHrz1JrV9IK7xA/tQEA= 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=xePCLMjo; arc=none smtp.client-ip=209.85.222.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="xePCLMjo" Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-7a1d3e93cceso223723985a.1 for ; Thu, 08 Aug 2024 12:28:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145313; x=1723750113; 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=bvi8Rvhrw8hau65mccNHsPcN1GGNLITREHvwNWenIGo=; b=xePCLMjo9bKQOYBS3zYObj/6DOZYMYgWGOXKI+QVjvzLFzt39OM/varGzge5Vwt2Oj Jo1YQmHyxAAeDuSocdjg8ZUyS7rCqV1AXIdy3CucTy0Ad5u6hIcouNNhX7u5T++DhRvG wATXu02Bp/zggHdaqOunW+/ssKhDneZhyESiQWr/tiq04k/XcycJG2jOMVUE8oa+dnNR yjDSlpgRXBEnjqcd4XWbXluVbv2gXAJ7YmqhvsPQz29eai64urb5pQlhfKQNGHiEmzCU rzOirmnPNdxvDZbv8LDYmxJQ+gDxlQljjsZjh4XMdkUo0k35ZT289vhLv6/DG3dkUcPa ybKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145313; x=1723750113; 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=bvi8Rvhrw8hau65mccNHsPcN1GGNLITREHvwNWenIGo=; b=e514Jbzdst3C/AZAPhYUTBl4cPqibcILQ6HATUtjMDRUaYc9bgJohsJA5pu73cnQC7 udaT4HnfjhybvHFFhQ/e/PBfCWsBXjMsABkLZC4kEEmK5Vi8h2SnBild7XlnRWSTffnh vzpTDN3iv57sAE8BL91iiuTkTded7EgHS52VwqfY3ll6Bl8FPM1nvmkIH13OoX5CO9Hr 0O/zRr3uVGOcarANqzHC3f/uRxB37tGEO1dYr46SkUi1hq+9ITfXPkl8lxRiw4nnYCxJ /shaKlrgUaWlOmZDKQCIrN7vrluEg4uI1kX5iec7BhjbXbc+yW+grZyNYDfbBUmpJrtU bRtQ== X-Forwarded-Encrypted: i=1; AJvYcCXZHiowY4Q4Wgwr60OnYe4DCfJfx9W1XjqOSDF1yw+BMSpYO4Es8bcmShjKT7DBx5BdvjmDcxGoacXrGXfYybRN6w1exFoKo9BPq4QAug== X-Gm-Message-State: AOJu0YzX5VuIdp/nwy/Doc6kYJ2wLUFoBVtG71kMe1UyY/5aiZsUszwJ 3pkl8kzeLjZff/rL3CHXJS+y5h5rFJbEe+mdE31YOpPTOKrld+muL1sF7hw7k2s= X-Google-Smtp-Source: AGHT+IHg5LiBxHcCbfRpZx/vfgLSV1DWqamdnXjHqSd3YJqdZjtNT3V/jkh6Hvbl6Y9gLBaUnNrkIQ== X-Received: by 2002:a05:620a:1917:b0:7a1:dc64:59db with SMTP id af79cd13be357-7a3824762c9mr447120385a.8.1723145312983; Thu, 08 Aug 2024 12:28:32 -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-7a378693d23sm188152785a.87.2024.08.08.12.28.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:32 -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 v2 13/16] fsnotify: generate pre-content permission event on page fault Date: Thu, 8 Aug 2024 15:27:15 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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. Signed-off-by: Josef Bacik --- include/linux/mm.h | 2 + mm/filemap.c | 97 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 92 insertions(+), 7 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index ab3d78116043..c33f3b7f7261 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3503,6 +3503,8 @@ 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, + struct file **fpin); 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..3d232166b051 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,72 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf) return ret; } +/** + * filemap_maybe_emit_fsnotify_event - maybe emit a pre-content event. + * @vmf: struct vm_fault containing details of the fault. + * @fpin: pointer to the struct file pointer that may be pinned. + * + * 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. + */ +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_READ; + 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 | VM_FAULT_RETRY; + + /* + * 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; +} +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 +3366,19 @@ 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); + ret |= VM_FAULT_RETRY; + } + return ret; + } + /* * Do we have something in the page cache already? */ @@ -3309,21 +3389,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 Thu Aug 8 19:27:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758143 Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) (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 3802215F3EE for ; Thu, 8 Aug 2024 19:28:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145316; cv=none; b=HGz7YfobRe8Zyba975VLp0NcXE6N5kTBbyQMvwxKWgIhRnBXmeU403a7tuhkj1KCMqoCKtZbL6F5V9pWOCV8qZfZOAnemf+FjJXGAzCOB/u4jJ94T7UStaCo5+r/07MzEdkmvRY7+QZGAhA5R1cFGJ22L4IW2pRK6PzQXNUifeY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145316; c=relaxed/simple; bh=z/3MEM5R7oglWCH0X6//tWx2TfoPHx5BV2T8P5cTJQk=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T02nN6kHh4Lo/B3Vk6a1ik4GHAusRsygy0J4JmwPyTm/Kzj4H35FWFoBkYDgAPe3KPBIdylvBea1sIFCJkSzy2nGi1wHtxB0AOqOernIfk+uB40Rfp3E+ccpMoICZunwlqI7reLUY3rZrWmsR0Et3QfzfgVNcjgEu9dolBoh5mc= 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=VG7mshXD; arc=none smtp.client-ip=209.85.222.181 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="VG7mshXD" Received: by mail-qk1-f181.google.com with SMTP id af79cd13be357-7a1d7bc07b7so84520285a.0 for ; Thu, 08 Aug 2024 12:28:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145314; x=1723750114; 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=6YLadnK5fpBDv1t3bY/aSRwCYBZfpbYWzdBOoO1tjFE=; b=VG7mshXDEFU4+vK5516DIMQYe0XsvDKJD7MFTIxYGRv+tzYX6CQMXqs/r1bkr9vJq9 2p543T/5Vaci1JV8vh5X7UTVUpVT9JhLOPgZ/UgcIasyMsjdExVmZQfEI6iFfbhHZ8F0 FD+zY2NJL4zgTv1ors0OrtblaTOiTKq2g2TB9F/ibrDU2zhPnp2qAawJ3+wxy7GZ6bkA ijCF1b1Lp7G7aaZwhCRfwtSqBw0bjdC/n0EXAoJ4iyuDL4EcFVLp6fX8Kg9JqeVgc41e iwtpwpV/EvUb46JpID2RfSNRmtWPC4Xizz85abzZI8xxoJhBjMLZ0OJMezNA9noZcG0H dzHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145314; x=1723750114; 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=6YLadnK5fpBDv1t3bY/aSRwCYBZfpbYWzdBOoO1tjFE=; b=mlWwt0EUlTy8XsPiorflXyQmeLjLU/cLAuo8CPsiuXnWM67lUtRHAhkBmTcQaBvnDJ ybObRYFF3flQahkctqBOcxwM25OBf4b52u3miZEc136JpbiDV99J5LOs4m/407ic9TqQ HornNJXoXMqyF+IfXzmHuH3HlSIpr42LatNJJ6/57thqBKUUHYQhEeeFXYW2JU62fH+x 4Z9Li2qogoIbVUzSkDQVF71aPMxg4pnbyq0Mu+ymd5zkpSt6L/AWZfzoAt1CY9jcweUt R7VQSfMwIl8xzLmC0tTLvqO21znOCW7C6nY+iQMPH5PHAB59ZfH6vAJwuBwcqD9cc67l gUwA== X-Forwarded-Encrypted: i=1; AJvYcCWNlKxsNMYQZCmQUqAwXQ+iAljX0qCYvLAR7cf/fNIPNfFbYEOTxNXI8CNCBD1ps0L0UsEJxImvck89b7Y1FftvMgGfm7cFnwdT5iMwxg== X-Gm-Message-State: AOJu0YzsPHvg1GY9Mum5od1JEeZIK1sUYhgptm4e9SvuLYkc7Fg+k/KX gKgz+v96ExK5ASnoqBeM+nZd3dTNgiAygUdWSxvrKVhatX60KML3mDl0cmDxlds= X-Google-Smtp-Source: AGHT+IGnhyiOZnALMvi26eVvjGx6xQxis4L0ZCZvKEWqHg1OHyJN3CwJxtYZ6LVYWXuIgBD+VdomqA== X-Received: by 2002:a05:620a:17a1:b0:79f:8f3:6ad4 with SMTP id af79cd13be357-7a3817eeaf6mr419940585a.1.1723145314246; Thu, 08 Aug 2024 12:28:34 -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-7a3786c1cc5sm187457885a.104.2024.08.08.12.28.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:33 -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 v2 14/16] bcachefs: add pre-content fsnotify hook to fault Date: Thu, 8 Aug 2024 15:27:16 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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 --- fs/bcachefs/fs-io-pagecache.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fs/bcachefs/fs-io-pagecache.c b/fs/bcachefs/fs-io-pagecache.c index a9cc5cad9cc9..359856df52d4 100644 --- a/fs/bcachefs/fs-io-pagecache.c +++ b/fs/bcachefs/fs-io-pagecache.c @@ -562,6 +562,7 @@ void bch2_set_folio_dirty(struct bch_fs *c, vm_fault_t bch2_page_fault(struct vm_fault *vmf) { struct file *file = vmf->vma->vm_file; + struct file *fpin = NULL; struct address_space *mapping = file->f_mapping; struct address_space *fdm = faults_disabled_mapping(); struct bch_inode_info *inode = file_bch_inode(file); @@ -570,6 +571,18 @@ vm_fault_t bch2_page_fault(struct vm_fault *vmf) if (fdm == mapping) return VM_FAULT_SIGBUS; + ret = filemap_maybe_emit_fsnotify_event(vmf, &fpin); + if (unlikely(ret)) { + if (fpin) { + fput(fpin); + ret |= VM_FAULT_RETRy; + } + return ret; + } else if (fpin) { + fput(fpin); + return VM_FAULT_RETRY; + } + /* Lock ordering: */ if (fdm > mapping) { struct bch_inode_info *fdm_host = to_bch_ei(fdm->host); From patchwork Thu Aug 8 19:27:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758144 Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) (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 86C73156864 for ; Thu, 8 Aug 2024 19:28:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145317; cv=none; b=o9VOloJ5cYnu2FNssXbd5Ci6T/r75cH88sdeUPWKiF7y2TpkdXSeriTTZJ2eP0GB9AK5IG+KNFIAjAfCe9MaRfS1LEAV7XI/UH+WcmJr0R3COH6HpYe/itqUq37vaWyzKkVDqLWeI+2QRny7v15j1I0/s+Z0w1ygYYLeHzbWJq8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145317; c=relaxed/simple; bh=qFTMKWIL/yGnaEo4ST947CpdEdpSPVdDj9nlHMZ5dyM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Jo1vzKEkYJrJdv/i8bpTuNOEj2Q5wNDYdeg3rc8MS71CMZE/WfxlVs/qDfeHWqbUtalBwFZTuw1JbnhL0oz/+c67DlPaLwAiY+eK57tQJq+HJHQzkF/timNBgEve8V9Vwl+0r9i4GRXCzh1xZpkTx2LtZ/HxSdG/9f5jUIH5ysU= 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=VI/YI4c6; arc=none smtp.client-ip=209.85.222.174 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="VI/YI4c6" Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-7a1d3e93cceso223727485a.1 for ; Thu, 08 Aug 2024 12:28:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145315; x=1723750115; 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=7CNYL3NI8L4DrPragHH0rXidYBkqgWEZukxmJe1amgo=; b=VI/YI4c6ripGhuKHMF8RqdxVDQmEZQzIPpz+nW4Wcq9A6qqSvC3s57O4jYDBCdz53l wwzG+5vtDfkVTDjezxwkf5FTxtyULOY7POiaDoXgJA2lNEtyrI8UYe5QnIM13tI5ZtDd d1ma3BpNtF4iS2W1cAyj54vCM2VyXnClScKMBKOKkn1g51JXPyTuhW27jmrRn1Y0MKZT PGYvsljHAxOhdn7UoDt0q00Nxspvlc2eENOmLYe7y2QIhsyo/MI0g/vj2B0TkBZW/hYz jxZ9GhLCMXzQDRn237dX/E1ppI7669jMmPgV537IS9lRqA+MPNUAfG/GDwKq7MzxrBBn 0L/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145315; x=1723750115; 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=7CNYL3NI8L4DrPragHH0rXidYBkqgWEZukxmJe1amgo=; b=LZXd9PtCNSWOCm4cFcnRxW5dDejIpLHtSrAOtyWKAzppyPDJZY++wS3rZLbv2cN3sx XYUeqw2INtD6m6A8cnWSgkclV8KiSJdjDio/IRG/Hgct/hxXwTX36vpo9ceAf91pzAVA N0S3d0AD9GPYuWTZVkBIEBnOXsXg9xzrVU/9ChWtE4YgycHeGaiMhUtsfxh57dy/7OI4 by9F2D0/WCX0vLQbg9/h05ucVCQ4J1fT2Axo64u/nWr3EPVNTppghr9kUDDoV3lMkvKI q9NGzi1OS/AxxqAffverJ7tEyUmVcYU/U/XXhNx6CnSELdIXWr6zy2UNr//EjyPrTK+o e+Ow== X-Forwarded-Encrypted: i=1; AJvYcCVptBpVHz6PoIsOkqsvG1epKmMVqPJQiceDVo6hx4UENPRIgIEGxafGwUckKSaeZA4W0uZxPZmaqUwCrcRCyW4vZ3AkLL8fQHi/44VxeQ== X-Gm-Message-State: AOJu0Yy98gA/8dyPrxqLs5RLwkX1MnIN74YK7AexyL1ipoItzqyPTyCO gHUErIp8MM2Xr1FT68B3DVAB7GC/5mWkgUDz/ApW4nxC76fQG7zX1l92+MkDe+Q= X-Google-Smtp-Source: AGHT+IELzedQB6qkhz/0uMlxd5H8eyyS/8cuLteD4Tx6/D3Ehd4NvybXJW2wN9ROVLZVzC4b0efbpg== X-Received: by 2002:a05:620a:1aa0:b0:7a1:62d3:b9a3 with SMTP id af79cd13be357-7a382498ec0mr519333185a.17.1723145315534; Thu, 08 Aug 2024 12:28:35 -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-7a3785f18fesm187453785a.60.2024.08.08.12.28.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:35 -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 v2 15/16] gfs2: add pre-content fsnotify hook to fault Date: Thu, 8 Aug 2024 15:27:17 -0400 Message-ID: <5d4babb06516f5288a999eefb1a4dec09e775195.1723144881.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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 | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c index 08982937b5df..b841b1720b5c 100644 --- a/fs/gfs2/file.c +++ b/fs/gfs2/file.c @@ -553,9 +553,22 @@ static vm_fault_t gfs2_fault(struct vm_fault *vmf) struct inode *inode = file_inode(vmf->vma->vm_file); struct gfs2_inode *ip = GFS2_I(inode); struct gfs2_holder gh; + struct file *fpin = NULL; vm_fault_t ret; int err; + ret = filemap_maybe_emit_fsnotify_event(vmf, &fpin); + if (unlikely(ret)) { + if (fpin) { + fput(fpin); + ret |= VM_FAULT_RETRY; + } + return ret; + } else if (fpin) { + fput(fpin); + return VM_FAULT_RETRY; + } + gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh); err = gfs2_glock_nq(&gh); if (err) { From patchwork Thu Aug 8 19:27:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13758145 Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) (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 D293A16190B for ; Thu, 8 Aug 2024 19:28:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145319; cv=none; b=t+0fGxJ53TwNHcRcaN1Gww2WMcTnc6rilUJ1tsUk/81kBRF2dblVOFM089K3oHRVUxeXc3Kc6aJta2xpMnE9NivvFtQ1ifg8WzGp3oEXjlQ3y9YK0moXo1hGJZ1gpabD8BLDe3ivgNdjQNuap4aqaO4WyBPbbPIOyViD7OsQW+o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723145319; c=relaxed/simple; bh=NX0d0m9ReKcqhm2PuSfHrLPH/9MXqCB++0q0ouz1III=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N5aVF3yqca5lXNFIWEa71bQqOGvutD+bu+qdMQpaC05h5EHm+9UZjBBG/bXOlKcuPWYhXgdHSzt+Gi8xjxgCuGDj32nWBaZJtWDnFkeh8xdKFE39FEE1uQdk4YGSHySNP5AB3DlsP+92P87YEccCP1SoqfgB8DWH7kT2Vd2IC7k= 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=29sC+ltb; arc=none smtp.client-ip=209.85.222.173 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="29sC+ltb" Received: by mail-qk1-f173.google.com with SMTP id af79cd13be357-7a3574acafeso64713085a.1 for ; Thu, 08 Aug 2024 12:28:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1723145317; x=1723750117; 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=PCXucMs8l0Ng5ZAiC85xGGR8b9zvUxCeXymlHlDJx8U=; b=29sC+ltbU5iX2JBtHMLxA26nSnqffvsQSu/RiKgpp8dohYTjNSFMvLexjzp32dAxwf pN4x/JGhe2I32IkUIpJa8cWW5laN4dFagjdmZA26JVAbJocrw2QY4j3vB4LEjO7dFHct VmWvo5sRbdY3FGIHBX/HQbrdIS9n9MA5xusFIS+XB5jvOdwAX82M1w0/RpCwcjj678KL j/0L8k90Sqbh72gbjtPrIlVMu2Ajelnmy8TMx2RaAd8plHIW7NdzJmd7wytyDApKFkFs h+UteWF72XcQRUSln0/NvOFGEMSxUzao6HH7EDfEAGmtDYuuOByZK5nQRCS8nsv53zat sRkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723145317; x=1723750117; 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=PCXucMs8l0Ng5ZAiC85xGGR8b9zvUxCeXymlHlDJx8U=; b=aRX810/cVaPRI/YDZkYvF/zFAQvBOgqNDe52kLv22I9QqQmk5n4v178m1zQPaouF4W w//0Y/2EwHsW1/sS+M4G74HzGYLl6lJftdoOmYnx+/VM6BKZVlUIZc0IscYeqEecdoUE 38BE4FOVjU9amOJfZ00qN/xvQu2FSx/NSWUqFm1RB2eJrGJieR4R8+eJ+Bh+PNulQnEN RZ3Bz/vtsEoys6OwULx8Tf8YQ9UA2/BtzGTb0B/mMomL2Ikh1HezZpM6X1oqpG4qsZ4w zWnowjIj1NnDFMZx+l+rdfaJ3+oiLhWsgNsKx2S05RCKnTBBS3uDvRbX6w0wX2AcJ883 UFjA== X-Forwarded-Encrypted: i=1; AJvYcCUK4XcoQ7ocAQx20uJ9w2FUIOpz6+nXFKB70sTB3vZbgUuBte4eXxuPv3umMJajm7iXleTgAGnU4ktmCVLPM4AksabXCKB9IBzsgwmSSQ== X-Gm-Message-State: AOJu0YyzYqFdtSWMHu1wtZZH+EDvSGUerIS1W+7E66WodESG3g5CP2bi a8Tv/NWrj7/KviwyuK5IKlri4dhSh2DaD5/J6ZHmcFxJu8bUAxokuV28Yw002Pjvg7MYAjFYGjd c X-Google-Smtp-Source: AGHT+IFqTokrODzDW+F7aRkm8RWDe1d2EvLJDXo8zz/Qi7NPVBOa+VCzv+goifozXKnE/sORJ8j7uA== X-Received: by 2002:a05:620a:3943:b0:79d:7246:ea67 with SMTP id af79cd13be357-7a3817f799emr380397285a.33.1723145316678; Thu, 08 Aug 2024 12:28:36 -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-7a3786b5a15sm187770785a.101.2024.08.08.12.28.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:28:36 -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 v2 16/16] xfs: add pre-content fsnotify hook for write faults Date: Thu, 8 Aug 2024 15:27:18 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@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 | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index 4cdc54dc9686..585a8c2eea0f 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1325,14 +1325,28 @@ __xfs_filemap_fault( bool write_fault) { struct inode *inode = file_inode(vmf->vma->vm_file); + struct file *fpin = NULL; + vm_fault_t ret; trace_xfs_filemap_fault(XFS_I(inode), order, write_fault); - if (write_fault) - return xfs_write_fault(vmf, order); if (IS_DAX(inode)) return xfs_dax_read_fault(vmf, order); - return filemap_fault(vmf); + + if (!write_fault) + return filemap_fault(vmf); + + ret = filemap_maybe_emit_fsnotify_event(vmf, &fpin); + if (unlikely(ret)) { + if (fpin) + fput(fpin); + return ret; + } else if (fpin) { + fput(fpin); + return VM_FAULT_RETRY; + } + + return xfs_write_fault(vmf, order); } static inline bool