From nobody Tue Feb 10 06:04:37 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 E7E4C24503B for ; Tue, 10 Feb 2026 00:38:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770683911; cv=none; b=FwkcCNaYAVdVU/+TyTlfBT25KPMQsfAx+kN6B7ond0epkDHHEVNNRB03VD8Pkkdc6mVK+oH7hlPfe+uS1jZhlvGqwwxVZJU4iRfULabv8zFAZmMLZE+LVS8d3KnhEhZtB46rYZ0wlD1BqpBPSom2luvOai0overCvTEIZe9qOG8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770683911; c=relaxed/simple; bh=xUZ6aO6Vonw0GmRwLJmSzeGDgxD5B5jYUwBGyhliZYQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=m2mXDh4rOPEy2l4CNFB4pJJTsD2GVwn1Q7dKxvXMWcqOkET6NNX24C3KzK6d629OaQXapJTL+A2qswiaXQ1RJLLfY+7MEySJjtGZX7v4AfdpvGCD3FuG0zXswCZP95NXf0uzqzhNdoMs+AbwuPwfZg0e2X3+CLRnxDV/Hq2miFQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RWcy9Fsr; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RWcy9Fsr" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-82438027416so151800b3a.1 for ; Mon, 09 Feb 2026 16:38:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770683909; x=1771288709; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tip76l+g47OdHjbnqUq9WcMpsS2PZvPoe5KfSjLBVzA=; b=RWcy9FsrN2bax19CnztshhDysUsqmrhqmi0k6shl208zzWKSDywAMQERfNmyU7nleo XpZ3ldY0rK4GxtXzmZQis9Qn5wWQFTdoc4r04IPIqGXEmJzDwJQFSVlQAa3wet3DKJ5N jO+iwQNPIB0+SaSmOqfck9sk5dsk8wlGAUO5Bpif2synIclkS/ytxycKK+L9/bsKDbgT OjisM9QdKqa4Rp81cEDmX3c4Auhsq0yFCZw6V24lzngK3ZJXT9Dwjy4NDvRupvDB3mrP gvxhhf7ntwn+8OCdRbOJFntwPTTN20AEL69bAGhpgGPrN4NJyZFeBxyguiQszaQjU2T/ C7Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770683909; x=1771288709; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tip76l+g47OdHjbnqUq9WcMpsS2PZvPoe5KfSjLBVzA=; b=VP+55b42yely65X+AMzAtS76/g3jM8YvLsB/w0rhwcS4S1Hux5D3UyIG1pOZmwzzx/ XCYu3T5HyNg5SRGGwjd/YSb3Y8pPby/LTd3z8dL45/hU7GuN3QhzRJmB+QIK0eRgYojS k6Oubl72igFuRJJY8Ni3iraDb8ISs/Z+RHp7GgUVhFro5O6COpde3M3X0iCZ+7zcGicq /+he3e3C1w43liuo7qjFYnVAZKRR6aKuZj5E7Me/eZ+7WgEzi9NrItd0HDzxwpeBWoqQ BbEilft93csvXy7/hKlk5PfxmqAmE6p5LQgRWvFucaHwgz519nFqPOgheEOapuOiEDkR rayw== X-Forwarded-Encrypted: i=1; AJvYcCUneNjGlMam4W8EYRlKiDKgtB8UNVcLPIXwEaDCfoEl8fkhjogFL7hPSMNZ4DBAr4K1j5jlB74kStmh9+M=@vger.kernel.org X-Gm-Message-State: AOJu0YzG0BUQfedcc1Kywm9hpVASPm0nn1afZy4FonBRiDfz9qy5X/KP isBLJbC1DpoWONL7nHQnGs3DGBWdKQ2hj41ckhQC1Cq2C4FX78I9p2wbKx5ex3C6epaWIFDJpNy kSVHh+RrTDSgFJbXQsw== X-Received: from pfbls21.prod.google.com ([2002:a05:6a00:7415:b0:822:4e8c:2c9e]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3914:b0:823:9e5:855e with SMTP id d2e1a72fcca58-824413c8e09mr12457964b3a.0.1770683909153; Mon, 09 Feb 2026 16:38:29 -0800 (PST) Date: Mon, 9 Feb 2026 16:37:59 -0800 In-Reply-To: <20260210003801.2834976-1-tjmercier@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260210003801.2834976-1-tjmercier@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260210003801.2834976-2-tjmercier@google.com> Subject: [PATCH 1/3] kernfs: allow passing fsnotify event types From: "T.J. Mercier" To: gregkh@linuxfoundation.org, tj@kernel.org, driver-core@lists.linux.dev, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, shuah@kernel.org, linux-kselftest@vger.kernel.org Cc: "T.J. Mercier" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The kernfs_notify function is hardcoded to only issue FS_MODIFY events since that is the only current use case. Allow for supporting other events by adding a notify_event field to kernfs_elem_attr. The limitation of only one queued event per kernfs_node continues to exist as a consequence of the design of the kernfs_notify_list. The new notify_event field is protected by the same kernfs_notify_lock as the existing notify_next field. Signed-off-by: T.J. Mercier --- fs/kernfs/file.c | 8 ++++++-- include/linux/kernfs.h | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 9adf36e6364b..e978284ff983 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -914,6 +914,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) struct kernfs_node *kn; struct kernfs_super_info *info; struct kernfs_root *root; + u32 notify_event; repeat: /* pop one off the notify_list */ spin_lock_irq(&kernfs_notify_lock); @@ -924,6 +925,8 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) } kernfs_notify_list =3D kn->attr.notify_next; kn->attr.notify_next =3D NULL; + notify_event =3D kn->attr.notify_event; + kn->attr.notify_event =3D 0; spin_unlock_irq(&kernfs_notify_lock); =20 root =3D kernfs_root(kn); @@ -954,7 +957,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) if (parent) { p_inode =3D ilookup(info->sb, kernfs_ino(parent)); if (p_inode) { - fsnotify(FS_MODIFY | FS_EVENT_ON_CHILD, + fsnotify(notify_event | FS_EVENT_ON_CHILD, inode, FSNOTIFY_EVENT_INODE, p_inode, &name, inode, 0); iput(p_inode); @@ -964,7 +967,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) } =20 if (!p_inode) - fsnotify_inode(inode, FS_MODIFY); + fsnotify_inode(inode, notify_event); =20 iput(inode); } @@ -1005,6 +1008,7 @@ void kernfs_notify(struct kernfs_node *kn) if (!kn->attr.notify_next) { kernfs_get(kn); kn->attr.notify_next =3D kernfs_notify_list; + kn->attr.notify_event =3D FS_MODIFY; kernfs_notify_list =3D kn; schedule_work(&kernfs_notify_work); } diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index b5a5f32fdfd1..1762b32c1a8e 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -181,6 +181,7 @@ struct kernfs_elem_attr { struct kernfs_open_node __rcu *open; loff_t size; struct kernfs_node *notify_next; /* for kernfs_notify() */ + u32 notify_event; /* for kernfs_notify() */ }; =20 /* --=20 2.53.0.rc2.204.g2597b5adb4-goog From nobody Tue Feb 10 06:04:37 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 B22AD257849 for ; Tue, 10 Feb 2026 00:38:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770683913; cv=none; b=s3njU4oHENL+ip/LjNnJzuer/FS5Gt3BxupfJh9gbvgPwXsD8nTMGTPF9phL3OTW/Y9erY9ZJakdUT2qSoN8rLOlHb2ra6nH21+YVUrnHqb0+u+BFCw4x/6NYuo1KjuYuSV2LcXuPB5tWbz0P2zhxrvmZypclePgqFZE+ofAfUU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770683913; c=relaxed/simple; bh=teigoZqI95reTfkorAH782wxQYo6YePSlVBSeRHPhoo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ncxmUmNrTcwXfaaLFhgK3Gjl2eH7UfOhjNlVlQ4QFOuWwPSc1ccrTx1oYySHn/5WA+PhxWOPr5nyMOEhUr4pr9IUZXCSkQE2RZfJbLhEwMmAV0j8/Im+1cFkW3fhnnTyMV9TBTScT09laF058xG0F4wK8gTVMuqNgGqAT6AjiZ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=IA+IiOz0; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IA+IiOz0" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-8244932b122so418485b3a.0 for ; Mon, 09 Feb 2026 16:38:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770683912; x=1771288712; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=VKFhdZVLSybbb15qv/W0122KHQjxioqzTfb665HUCmk=; b=IA+IiOz046lBTzloYQ70v8VLS+rvP34AZ8k2MOTtoelZjufTSF5vPom5MfIsp6aNHP bd3F1S9gElEU77uSnHdfhasOGwyPnWBUlt72auOnaJV9aJWtBtiTp8ZcUrqMz1ZsGh8b wC4AptnNIVL8fPjfr2+jl5FmaV76i1RPciKCBDdtZ9YzH67jK2o0GONCHDwptc5i42jm 6YIyMf8Wkm+k/is05jT1GOww3NAxegXQKMvmj5faF5C1i2T7YO3Q7g3yXAgJqXHiJF4m cjZFMWVFC0j8VyS8rL4GWidB3TFb6yjcE4BfTPXORDAaXgQw9nabFbZ18hMRTgrdtnIq T3bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770683912; x=1771288712; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VKFhdZVLSybbb15qv/W0122KHQjxioqzTfb665HUCmk=; b=g+pOX58FpbIAUJhFK4XBu3nob5gix7MwP3SKK1hiqGB1kgpqAtYsXn6TBVWfiyZ3pQ OW2/4Kn6R30wiMDrolGBh/3Mjm6nkw/1uo/Mnb5T2Vn87cJNrr4LR1LqnLOG+ODy/WrI Sl/+yzz2rSNqITxsJdlbp8Qxpsbcv8f/71iLyjzyOH3XdTo6yJ1ZXm3tQL2ieFbYjYRP P8Zzpy60e+ntSCfebNCsGATNaXG9VSZnZEtmL5eWcVryFfFh/8j+Yb8h8uQakl4fuqfk 3o8tjG3hBcIZ/0zW8FfnwBpzuJ0rCdl3z/W67tG0/0KymF1wawQ7p3wbutR2ErtCQRvW XMZA== X-Forwarded-Encrypted: i=1; AJvYcCVF2zMj/kffyLSqLyuB2jMRwyePIKp8odSx82L56tZS/ABkCVlHZ7IxlE+PQT+58QifZqZz3yGQkYLAlVg=@vger.kernel.org X-Gm-Message-State: AOJu0YwjJ/+kN8UmYoe+47HGhmzXDRGd0v+UdlVBhiz27/cSEl7MlwtI unl3otWI/N9/WdUsTrqpstTlrKDUeC7h91mtxhdx4lbpPeABmwu6QhHxLEiKBpYjzdtHLcIA68n DcPki9B6JYTsLQTqBgw== X-Received: from pfbna12.prod.google.com ([2002:a05:6a00:3e0c:b0:81f:b161:df0e]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1a1a:b0:823:1392:ba5d with SMTP id d2e1a72fcca58-824416f8de9mr9591003b3a.38.1770683911999; Mon, 09 Feb 2026 16:38:31 -0800 (PST) Date: Mon, 9 Feb 2026 16:38:00 -0800 In-Reply-To: <20260210003801.2834976-1-tjmercier@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260210003801.2834976-1-tjmercier@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260210003801.2834976-3-tjmercier@google.com> Subject: [PATCH 2/3] kernfs: send IN_DELETE_SELF and IN_IGNORED on file deletion From: "T.J. Mercier" To: gregkh@linuxfoundation.org, tj@kernel.org, driver-core@lists.linux.dev, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, shuah@kernel.org, linux-kselftest@vger.kernel.org Cc: "T.J. Mercier" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently some kernfs files (e.g. cgroup.events, memory.events) support inotify watches for IN_MODIFY, but unlike with regular filesystems, they do not receive IN_DELETE_SELF or IN_IGNORED events when they are removed. This creates a problem for processes monitoring cgroups. For example, a service monitoring memory.events for memory.high breaches needs to know when a cgroup is removed to clean up its state. Where it's known that a cgroup is removed when all processes die, without IN_DELETE_SELF the service must resort to inefficient workarounds such as: 1. Periodically scanning procfs to detect process death (wastes CPU and is susceptible to PID reuse). 2. Placing an additional IN_DELETE watch on the parent directory (wastes resources managing double the watches). 3. Holding a pidfd for every monitored cgroup (can exhaust file descriptors). This patch enables kernfs to send IN_DELETE_SELF and IN_IGNORED events. This allows applications to rely on a single existing watch on the file of interest (e.g. memory.events) to receive notifications for both modifications and the eventual removal of the file, as well as automatic watch descriptor cleanup, simplifying userspace logic and improving resource efficiency. Implementation details: The kernfs notification worker is updated to handle file deletion. fsnotify handles sending MODIFY events to both a watched file and its parent, but it does not handle sending a DELETE event to the parent and a DELETE_SELF event to the watched file in a single call. Therefore, separate fsnotify calls are made: one for the parent (DELETE) and one for the child (DELETE_SELF), while retaining the optimized single call for MODIFY events. Signed-off-by: T.J. Mercier --- fs/kernfs/dir.c | 21 +++++++++++++++++++++ fs/kernfs/file.c | 29 +++++++++++++++++++++++------ fs/kernfs/kernfs-internal.h | 3 +++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 29baeeb97871..74a4c347b78a 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -9,6 +9,7 @@ =20 #include #include +#include #include #include #include @@ -1471,6 +1472,23 @@ void kernfs_show(struct kernfs_node *kn, bool show) up_write(&root->kernfs_rwsem); } =20 +static void kernfs_notify_file_deleted(struct kernfs_node *kn) +{ + static DECLARE_WORK(kernfs_notify_deleted_work, + kernfs_notify_workfn); + + guard(spinlock_irqsave)(&kernfs_notify_lock); + /* may overwite already pending FS_MODIFY events */ + kn->attr.notify_event =3D FS_DELETE; + + if (!kn->attr.notify_next) { + kernfs_get(kn); + kn->attr.notify_next =3D kernfs_notify_list; + kernfs_notify_list =3D kn; + schedule_work(&kernfs_notify_deleted_work); + } +} + static void __kernfs_remove(struct kernfs_node *kn) { struct kernfs_node *pos, *parent; @@ -1520,6 +1538,9 @@ static void __kernfs_remove(struct kernfs_node *kn) struct kernfs_iattrs *ps_iattr =3D parent ? parent->iattr : NULL; =20 + if (kernfs_type(kn) =3D=3D KERNFS_FILE) + kernfs_notify_file_deleted(pos); + /* update timestamps on the parent */ down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); =20 diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index e978284ff983..3e813b09ab05 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -37,8 +37,8 @@ struct kernfs_open_node { */ #define KERNFS_NOTIFY_EOL ((void *)&kernfs_notify_list) =20 -static DEFINE_SPINLOCK(kernfs_notify_lock); -static struct kernfs_node *kernfs_notify_list =3D KERNFS_NOTIFY_EOL; +DEFINE_SPINLOCK(kernfs_notify_lock); +struct kernfs_node *kernfs_notify_list =3D KERNFS_NOTIFY_EOL; =20 static inline struct mutex *kernfs_open_file_mutex_ptr(struct kernfs_node = *kn) { @@ -909,12 +909,21 @@ static loff_t kernfs_fop_llseek(struct file *file, lo= ff_t offset, int whence) return ret; } =20 -static void kernfs_notify_workfn(struct work_struct *work) +static int fsnotify_self_event(int event) +{ + if (event =3D=3D FS_DELETE) + return FS_DELETE_SELF; + + return event; +} + +void kernfs_notify_workfn(struct work_struct *work) { struct kernfs_node *kn; struct kernfs_super_info *info; struct kernfs_root *root; u32 notify_event; + u32 self_event; repeat: /* pop one off the notify_list */ spin_lock_irq(&kernfs_notify_lock); @@ -929,6 +938,8 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) kn->attr.notify_event =3D 0; spin_unlock_irq(&kernfs_notify_lock); =20 + self_event =3D fsnotify_self_event(notify_event); + root =3D kernfs_root(kn); /* kick fsnotify */ =20 @@ -959,15 +970,21 @@ static void kernfs_notify_workfn(struct work_struct *= work) if (p_inode) { fsnotify(notify_event | FS_EVENT_ON_CHILD, inode, FSNOTIFY_EVENT_INODE, - p_inode, &name, inode, 0); + p_inode, &name, + (notify_event =3D=3D self_event) ? + inode : NULL, 0); iput(p_inode); } =20 kernfs_put(parent); } =20 - if (!p_inode) - fsnotify_inode(inode, notify_event); + if (!p_inode || self_event !=3D notify_event) + fsnotify_inode(inode, self_event); + + /* For IN_IGNORED, and automatic watch descriptor removal */ + if (self_event =3D=3D FS_DELETE_SELF) + fsnotify_inode_delete(inode); =20 iput(inode); } diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index 6061b6f70d2a..cf4b21f4f3b6 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -199,6 +199,8 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node = *parent, * file.c */ extern const struct file_operations kernfs_file_fops; +extern struct kernfs_node *kernfs_notify_list; +extern void kernfs_notify_workfn(struct work_struct *work); =20 bool kernfs_should_drain_open_files(struct kernfs_node *kn); void kernfs_drain_open_files(struct kernfs_node *kn); @@ -212,4 +214,5 @@ extern const struct inode_operations kernfs_symlink_iop= s; * kernfs locks */ extern struct kernfs_global_locks *kernfs_locks; +extern spinlock_t kernfs_notify_lock; #endif /* __KERNFS_INTERNAL_H */ --=20 2.53.0.rc2.204.g2597b5adb4-goog From nobody Tue Feb 10 06:04:37 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 61D5E23BF9B for ; Tue, 10 Feb 2026 00:38:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770683918; cv=none; b=OuOB4pkSHY3jNEsuM+kbwYuCOO8LObPat/L8i8tJE7llAnutpYyhrOa7JZML6cvp6MPLVGDFfevnovdnS/yeoq3b/21oIsijWqEKTKIsNMbhOvxotfUUZ/d/HsprT8E/vkxL8lTJ5OhR4wNkdBqogDPWC950qb3bJS0PLt0Y8WY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770683918; c=relaxed/simple; bh=1Qt94GB4G3IwGLclm8KcPaUi5sZ/Z6HPS+sk0iKFMzE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Vf7fQGqs4C86YS3n5jfATwe0JAZMcSttufWK7K52ICYrxlbnORPIyfnxS3NMKfNTx5WRJZtssKfk61yzrW1jsbDiuSY0F6OOALGGyKOfHyCbvHQgvryMemGON/G3W6GG2WIcDObMl8Qm+A90zCjiU3AT2RSYtHcDKm10fRhSrg8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3Wbbg7Tp; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3Wbbg7Tp" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2a784b2234dso7649955ad.1 for ; Mon, 09 Feb 2026 16:38:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770683917; x=1771288717; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=D0Pi0e7p+zApj87fVsFVxZlxRDj171oPkvxq1iRYRTY=; b=3Wbbg7TpK9Xu4R+smXq77Pzb8HFn3IhHQdo3ZV6vAs6likXrwBjBK41dNcCVDkh58z cP1nOCEVVxGa/wYoKfmOSEq024dYBTfezNYay4tVcZAHaq13u+ZrB+93HuMInfMiPBff Pga/Xacrj6YtpL5AjD0qzbrNHWz6wiMYD66jURzTKaON83LLmk4a23K5U60NMqXMBya0 6LFGIovsI9sWyL2Vnt3P4UFDEWbDIxMBAV+vnxMPHrgfMEpsH1tpXvPLkb1d2wwXtnGo ZxkP/glCnhawW9n7i9rDxB2H4IBQ/AITXwts2NCj7cb3rAfmMWmmiYsS1I0ulnt0tPH7 090A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770683917; x=1771288717; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=D0Pi0e7p+zApj87fVsFVxZlxRDj171oPkvxq1iRYRTY=; b=ID6xev7OVod27o8R8JzGzrnDc3uNFEsw7UNiZHwoNw9XMuUKp2WAGx+Tdw3SNvMOO4 kQaO+Yzd7IOW48SYGd06T4h/BPPTUC0EltMUhX6vwnUjGod2TCbXPI6gwI7RS3jwonEB pQ7F6XGBZh6DQnyRx9YUGpXqsTspxq9FKkG5F/gIYlFwCKA+jmK626rTq+lIcAMq6Q5F JcjJ6Ii5LbmBt/8815/4I2Ih0pBqPSpqRpKJDJF2twy0XeP/Qu6Xbb7MqnE3b1y/l0j4 Uga09Kq2Qdhhd214wxrtWhV/4wpjzAxXohj9pPtqI1LsEh/20g2RjKrni8RA3QYecaQ9 nAww== X-Forwarded-Encrypted: i=1; AJvYcCW57KpTLXaVNvs3hFuKCCEgv8sWCtgYNxwoKQvizyvm85ug761pOHeZ8Ot4Nj5IYKDajDQnd2q3mfM2Pyk=@vger.kernel.org X-Gm-Message-State: AOJu0YzngFU5BUPiH2eUW7pMoAWiZ0cM1Ir7ALPCmSwGT0103+AxSIEm xIvPR1t2mgYgITrAZYqPrzBiGJSh0kPH+3e+pHwl48yabYu73EhSPErQ9ZoP2ERF8UjoAD/j9RM DCntvC97uHtmPs8+7ww== X-Received: from plzv5.prod.google.com ([2002:a17:902:b7c5:b0:2a8:759b:173d]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e744:b0:2a1:3cd9:a737 with SMTP id d9443c01a7336-2a95194698fmr165463975ad.36.1770683916818; Mon, 09 Feb 2026 16:38:36 -0800 (PST) Date: Mon, 9 Feb 2026 16:38:01 -0800 In-Reply-To: <20260210003801.2834976-1-tjmercier@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260210003801.2834976-1-tjmercier@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260210003801.2834976-4-tjmercier@google.com> Subject: [PATCH 3/3] selftests: memcg: Add tests IN_DELETE_SELF and IN_IGNORED on memory.events From: "T.J. Mercier" To: gregkh@linuxfoundation.org, tj@kernel.org, driver-core@lists.linux.dev, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, shuah@kernel.org, linux-kselftest@vger.kernel.org Cc: "T.J. Mercier" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add two new tests that verify inotify events are sent when memcg files are removed. Signed-off-by: T.J. Mercier --- .../selftests/cgroup/test_memcontrol.c | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testi= ng/selftests/cgroup/test_memcontrol.c index 4e1647568c5b..25a495347f7c 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -1625,6 +1626,129 @@ static int test_memcg_oom_group_score_events(const = char *root) return ret; } =20 +static int read_event(int inotify_fd, int expected_event, int expected_wd) +{ + struct inotify_event event; + ssize_t len =3D 0; + + len =3D read(inotify_fd, &event, sizeof(event)); + if (len < (ssize_t)sizeof(event)) + return -1; + + if (event.mask !=3D expected_event || event.wd !=3D expected_wd) { + fprintf(stderr, + "event does not match expected values: mask %d (expected %d) wd %d (exp= ected %d)\n", + event.mask, expected_event, event.wd, expected_wd); + return -1; + } + + return 0; +} + +static int test_memcg_inotify_delete_file(const char *root) +{ + int ret =3D KSFT_FAIL; + char *memcg, *child_memcg; + int fd, wd; + struct inotify_event event; + ssize_t len =3D 0; + + memcg =3D cg_name(root, "memcg_test_0"); + + if (!memcg) + goto cleanup; + + if (cg_create(memcg)) + goto cleanup; + + if (cg_write(memcg, "cgroup.subtree_control", "+memory")) + goto cleanup; + + child_memcg =3D cg_name(memcg, "child"); + if (!child_memcg) + goto cleanup; + + if (cg_create(child_memcg)) + goto cleanup; + + fd =3D inotify_init1(0); + if (fd =3D=3D -1) + goto cleanup; + + wd =3D inotify_add_watch(fd, cg_control(child_memcg, "memory.events"), IN= _DELETE_SELF); + if (wd =3D=3D -1) + goto cleanup; + + cg_write(memcg, "cgroup.subtree_control", "-memory"); + + if (read_event(fd, IN_DELETE_SELF, wd)) + goto cleanup; + + if (read_event(fd, IN_IGNORED, wd)) + goto cleanup; + + ret =3D KSFT_PASS; + +cleanup: + if (fd >=3D 0) + close(fd); + if (child_memcg) + cg_destroy(child_memcg); + free(child_memcg); + if (memcg) + cg_destroy(memcg); + free(memcg); + + return ret; +} + +static int test_memcg_inotify_delete_rmdir(const char *root) +{ + int ret =3D KSFT_FAIL; + char *memcg; + int fd, wd; + struct inotify_event event; + ssize_t len =3D 0; + + memcg =3D cg_name(root, "memcg_test_0"); + + if (!memcg) + goto cleanup; + + if (cg_create(memcg)) + goto cleanup; + + fd =3D inotify_init1(0); + if (fd =3D=3D -1) + goto cleanup; + + wd =3D inotify_add_watch(fd, cg_control(memcg, "memory.events"), IN_DELET= E_SELF); + if (wd =3D=3D -1) + goto cleanup; + + if (cg_destroy(memcg)) + goto cleanup; + free(memcg); + memcg =3D NULL; + + if (read_event(fd, IN_DELETE_SELF, wd)) + goto cleanup; + + if (read_event(fd, IN_IGNORED, wd)) + goto cleanup; + + ret =3D KSFT_PASS; + +cleanup: + if (fd >=3D 0) + close(fd); + if (memcg) + cg_destroy(memcg); + free(memcg); + + return ret; +} + #define T(x) { x, #x } struct memcg_test { int (*fn)(const char *root); @@ -1644,6 +1768,8 @@ struct memcg_test { T(test_memcg_oom_group_leaf_events), T(test_memcg_oom_group_parent_events), T(test_memcg_oom_group_score_events), + T(test_memcg_inotify_delete_file), + T(test_memcg_inotify_delete_rmdir), }; #undef T =20 --=20 2.53.0.rc2.204.g2597b5adb4-goog