From nobody Thu Apr 2 20:25:27 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 7E18D35DD13 for ; Thu, 12 Feb 2026 21:58:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770933513; cv=none; b=KEUEhgAxm4J9BGx3pNkJmPxnTd3E0AAfCdwFnq2lt8h/ZkkK5BnHDofSMkm/VJTIuG6D7wgBmN5h7g9BCkkJU6eVHPc8OhLLCugw3siIR2ya7KpBud2wEKVps/vgyF65SmvqkZiZEnQkIs7ZCb9X3M8lxxVTOEkvHJmr4bXnBk4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770933513; c=relaxed/simple; bh=cddz7yiNNtZcRMbECFzY2RrBpOd+pdNNI+Dx9a521G0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tcisBeAvH9luYOTU+sjmNXzL9HciVsULCUHYANZxT6dp5UfyRX0Xiad6GTcBxrBAr97cNqg1Oc9gd0OKR9BA0O5y2QUk9EKYZ8DBav5g55SjaWJjx8oJ/9sm8JVv8fHvVtaBmrRhKDiiEpYiIz9OLhC3uDPAStkzgF1ZBucq1Y0= 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=OgOaRaf3; arc=none smtp.client-ip=209.85.216.73 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="OgOaRaf3" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3562171b56dso249775a91.2 for ; Thu, 12 Feb 2026 13:58:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770933511; x=1771538311; 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=F9QIJ7/JNUJlnhiwP3aDc1NcdR+8wgC780wGHRVk+o8=; b=OgOaRaf37vhBtHyCrP3d6E4o6GDVE174CiT1tFdoq35UW+RZumKLiGLZhVNorLvyLH VK009YlDwcnMrFrOVNfprc01roOhwIUuFiVRy1gq9etX/MnTIOVu9xfxgOzhXpc288gu MvwJDLqHydjFUmUuSBL6OvVf0c25o/7Bfqgv130ieL4UZPRAHTV1GbGmAbM8f8f26muW oGhHN6yNqqC5iNATmJG32MmJegxnETit56bLO/QTq0zIlFXKdNmtFaAQuSjeAlb2kzT7 wDBxPc1y6Hso9hIkc8PDGMx+ChE1/MJ1kXvSa34JIgWra1TXhMQr/yneGpuWUZNvoe7/ WgEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770933511; x=1771538311; 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=F9QIJ7/JNUJlnhiwP3aDc1NcdR+8wgC780wGHRVk+o8=; b=wnrNdEwYxdyLWe7jkJWfGKuVomzlSq1+at2hKYXe9/TzFWTp/NDk6sz1PW/6TpN2Si COAaDVnQlGueWQrI4obTvfba4lmCXWkqmwxMvjYtLXAJuKW96YWWHqXJib34woHdj8OY R9wGalTTd6WKmD88rC8sV/VAYtrr2XB37VtLTOCqRAcSaAjCRSGYQ3/slH4ZOJMZlPCB TMTbTHusd0OcxQ5Ve1U+TUh0eu408gPrPhsP03rTZv2sXGkKfpAqcnbqwvXxe/6x6pce jZgU8P135g2BS6WNP84ic2Io2gPbtcg1pubZD6xJbnxvShpxsOwNb1o2LU+peQBuV5PC 761Q== X-Forwarded-Encrypted: i=1; AJvYcCWfTER4H8Pozdx6kEBUKIS3kfA7U5WXgBrq5nP1/S9SyYClW9KkgMKgp1x3KYeMRc4hLda4nmxaSl3IiDM=@vger.kernel.org X-Gm-Message-State: AOJu0YxBVkNzykx6HWFNRMDtZ90fPt6KIOC6KZ9lTlNfjfD2eYUzrfnh o2FCpImPcUNw7UXJLzgvp1aQW1mkHCu8uAXL5Vpad5oo5q6ZlCulgSINjxzznajUoWkB8nBvNut Biwe9q/VmJyXqS7H0nw== X-Received: from pjob16.prod.google.com ([2002:a17:90a:8c90:b0:34b:e29d:f74c]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2d90:b0:356:22ef:57b9 with SMTP id 98e67ed59e1d1-356a7641a8emr399373a91.3.1770933510727; Thu, 12 Feb 2026 13:58:30 -0800 (PST) Date: Thu, 12 Feb 2026 13:58:12 -0800 In-Reply-To: <20260212215814.629709-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: <20260212215814.629709-1-tjmercier@google.com> X-Mailer: git-send-email 2.53.0.273.g2a3d683680-goog Message-ID: <20260212215814.629709-2-tjmercier@google.com> Subject: [PATCH v2 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 Acked-by: Amir Goldstein Acked-by: Tejun Heo --- 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.273.g2a3d683680-goog From nobody Thu Apr 2 20:25:27 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 4EF4C35E553 for ; Thu, 12 Feb 2026 21:58:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770933516; cv=none; b=oYet77keBl4lgcekvcVdYKZtHeEdJ6P6brUsa16JgvTr1AQpwVFNSroQwY9+skkAVPG6nkppgDpScXaPQbfiAtMin1NkZaE60/UuWgk5RvKgVl5+b+rydVD+d1ObrL/GqW8TUVg4frZ/rvcX34suHWmIVXSrwnDF7K/u9LzgY0Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770933516; c=relaxed/simple; bh=JZj4LpEl8D/f9PumooC8+GYc0Dg8nxxVo/kQgge2600=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fuPfhfaIf0/7jyyNGO3ZxbxVTeQP9Xzcnh50qbv4wNRpJyqkW8+IpmE0sajvY6rbjDBPFCR5E+ipG35eXmkY3Dqx1z13D3OWCqRYuVTKIxd5FmPUlhP87aAbibNmg3B8dJy7RuHfzY9kGGjDQ/dLNEsFKq88uctBBh4VxM4Bae4= 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=WXnY8bkK; arc=none smtp.client-ip=209.85.214.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="WXnY8bkK" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2aad3380076so14116855ad.1 for ; Thu, 12 Feb 2026 13:58:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770933515; x=1771538315; 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=ajT1DeXeAjXwu6HaXbFv5+sD4L7P9mYZ5NNigjqhjfQ=; b=WXnY8bkKdBf5dKEscJFH2TB+k5QETGokMg0QvFEedO2G5/puiVWwC6Hy5Ld1zCDKrw Iu+5G1LG8Pi6fVO0FRyoNwWonRTzzFsL0PLEIEC7uOZ9MJrG1L0fKgHEJ1KVkRNXDLue lrcQGHd/IsSmFSA33wiYTWFZMTh4e1FyAztXCJDVLD6mAA6IBtx6QHgK9RpTlcxdaIi1 ESCAjOp66gX2Yml+fDvOOM7JG2Y36+TSeqHxDDu6sreMYn3l2/hZyw28iv0NsF+wn5nL EX+4cTWC39dp/LCm4fEJLVUQXevKc85fErseWVGkEkYoWXSianGd5qu519EGQRvb+vDI 2P0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770933515; x=1771538315; 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=ajT1DeXeAjXwu6HaXbFv5+sD4L7P9mYZ5NNigjqhjfQ=; b=o66JtBna/TvmSZWE0NznfJKsClym2vBVLxu+QYuvpqgdvrn2e9GIkvCetKmjH/WZB0 P7UOntRMu3Vf5tbcTPaNnIuVLswYznJqeipLur0Q2SGUJEyYyi6BA/kEQPWLiHw7Fb2b K9yRGmv3J0Vj7wqlNWVOSZ4S5/A+HJ2rK4tH1zcDUo29NUzEoC21NbWQ5C2zsO24ViL0 fUjjTUtabRwgONA21yp8dwKF39L7GN7JPyNmEpP05nOgYqs2DqnujSSxUhc86jbFd0E+ q6hLs5sfdrj9GIMmU/mXmO/gaNjSx4+cQOoKHg/0+H0SJzNEgKNjEDuqKNLgF6XVuIRT mahw== X-Forwarded-Encrypted: i=1; AJvYcCWGFdioonQugT2lxAsKnFaRTuWfEQSAogNB5goSDM+sgE2EsxMBY9hPHrP6+p8qUYi2heO8SNItLm3jt3E=@vger.kernel.org X-Gm-Message-State: AOJu0YyxYY0sIr1J/TJIjQNPJA3Ddfc0AGTGK6/EvFc/2h0zphnN0yWP 7a69ydIpz9Yb2XwS0ua9D3ZJl1DorUvDzPBiUFG7K9fRyFYUN/BKqG+0Wad4jtvqH9VGL6IZlcR 7CNnUDBw5dvJLHYtcCQ== X-Received: from ploe4.prod.google.com ([2002:a17:903:2404:b0:2a0:f5f5:419d]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:b598:b0:394:4d99:ff56 with SMTP id adf61e73a8af0-394669e12eamr341305637.11.1770933514447; Thu, 12 Feb 2026 13:58:34 -0800 (PST) Date: Thu, 12 Feb 2026 13:58:13 -0800 In-Reply-To: <20260212215814.629709-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: <20260212215814.629709-1-tjmercier@google.com> X-Mailer: git-send-email 2.53.0.273.g2a3d683680-goog Message-ID: <20260212215814.629709-3-tjmercier@google.com> Subject: [PATCH v2 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 Acked-by: Tejun Heo --- fs/kernfs/dir.c | 21 +++++++++++++++++++++ fs/kernfs/file.c | 16 ++++++++++------ fs/kernfs/kernfs-internal.h | 3 +++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 29baeeb97871..e5bda829fcb8 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(pos) =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..2d21af3cfcad 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,7 +909,7 @@ static loff_t kernfs_fop_llseek(struct file *file, loff= _t offset, int whence) return ret; } =20 -static void kernfs_notify_workfn(struct work_struct *work) +void kernfs_notify_workfn(struct work_struct *work) { struct kernfs_node *kn; struct kernfs_super_info *info; @@ -959,15 +959,19 @@ 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 FS_MODIFY) ? + inode : NULL, 0); iput(p_inode); } =20 kernfs_put(parent); } =20 - if (!p_inode) - fsnotify_inode(inode, notify_event); + if (notify_event =3D=3D FS_DELETE) + fsnotify_inoderemove(inode); + else if (!p_inode) + fsnotify_inode(inode, FS_MODIFY); =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.273.g2a3d683680-goog From nobody Thu Apr 2 20:25:27 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 C36B535E556 for ; Thu, 12 Feb 2026 21:58:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770933519; cv=none; b=oi2WDNBzb3AwfYC7lD7KlnnU71Tz2Hs8BKgzbek82+HRWuirB4oO4Z5X3zVF4p6yXYrUVHUYKmoyLHzM1hwe6Pau9mPhYEqtcL33zU7FcG54VB6ewAR8IqPJUq8MHWi/FV8yR8PBoKVg0AWxTrOFbDc0DTZkzNaTtTbTD7Cdpzw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770933519; c=relaxed/simple; bh=ezbBm/Tq1NbOZNYchfM5BrbiYJXHM46rp75jJpESgIM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YdnO2A4ETM0LMbQiy+djLJx/woZa8FoLWcdwEtxvgrK4gVi1Ymx7R5/FNOibnU3q1l8JLHqSBFWU0fS1HGEhVK1rPLIQaNvvEpNZGd2DLYSMTp1pMssW/eeeVPdpKAZb+gs06uUW/GUwrzxVL+A+Jxrn1EbnyVEr5Cd6TxkSU4k= 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=cwnua99f; arc=none smtp.client-ip=209.85.214.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="cwnua99f" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a13cd9a784so2387385ad.2 for ; Thu, 12 Feb 2026 13:58:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770933518; x=1771538318; 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=/49beE9xrDbjk2PTkq3bWwQqpKtfvuvBtaf8t9tZ8yg=; b=cwnua99fB0+vvZARrjQFt2miVg+prGDbN9oZs8jB2HxK+GLIItGcsZ8RhBu0tPl5Ks FwIjMBKXn6hfVVGCPeSN4XD4fBLobGDf+RXFLP9RyiD6hH+eQWCAZXWWPJYge0aCUHN0 mVwaUY/03pV5wEs7VH7a4him0RbU2HiSifwQOszre/RrgNqXXsI0Hvu0b8wLkQRvON9a jE1v3Ar8l2WQdbFahdVVRJwuiSR3quYjEz/GgfxXoB0vcpz6tA1M/3B6Oq4bp1BMK4I9 4yjZZxZc1tsLwWEZLNtv4o553Wa6kjv3pXWWXb8uzmKPKoGQpN+MBaT5/9vdrofO4EpN yrIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770933518; x=1771538318; 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=/49beE9xrDbjk2PTkq3bWwQqpKtfvuvBtaf8t9tZ8yg=; b=QcgfIz8/Vz4w/lp2uNm1Ir4lRIG5DhoNTi2TqgHVe2lxsZjyBUqBXkWWgTHzlL9ZZl zlr4/xHFlbafiA196kxSx3HkPPpwKfLkSqyfrwXimCAPCFFmR5LWQ/NUnS3knGVfk9V5 FjcmKlFtSERvD1DOOkZbrNpAJ9obA0zZQ3eGof17eqPvMd6vKY30+B1+i5ZGLtX30fFv y8f78LPLnSD7ZAPz33gVilN0hf67armznwgymNU8yDp0GECsdNszDKktbbPFcBHA3v9i fPBsH+OrwQbYkECk8WcPtnjkSOtW1vBxBfsUpPsKhi63TnMjvt6pK5KxZipH849dxCLL DZUA== X-Forwarded-Encrypted: i=1; AJvYcCWH9zCwdSdK+n+wjwIrWa9nFCuj8YMN7M2DG56P2VUsgiIFYjsdjS9b+VhFUczAi9D/gsgRrri1c4gKgDw=@vger.kernel.org X-Gm-Message-State: AOJu0YxkHFyYe6dbuPZJwx+9/sX5EbzEwlnWN+Z8XNErDDqKxMgWs5t1 bndDyboNseZKlNIHdHzPjca3mxeRbk1WJThjyI08Ep8qnYIadK+hCN8qJcJoOyOHIUCWWKj0qsd M5hCMyMVuoAdz6yj/ig== X-Received: from pjbqx6.prod.google.com ([2002:a17:90b:3e46:b0:354:c41d:dd42]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:544e:b0:356:268e:ff97 with SMTP id 98e67ed59e1d1-356a792ab7fmr512870a91.20.1770933517978; Thu, 12 Feb 2026 13:58:37 -0800 (PST) Date: Thu, 12 Feb 2026 13:58:14 -0800 In-Reply-To: <20260212215814.629709-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: <20260212215814.629709-1-tjmercier@google.com> X-Mailer: git-send-email 2.53.0.273.g2a3d683680-goog Message-ID: <20260212215814.629709-4-tjmercier@google.com> Subject: [PATCH v2 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 Acked-by: Tejun Heo --- .../selftests/cgroup/test_memcontrol.c | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testi= ng/selftests/cgroup/test_memcontrol.c index 4e1647568c5b..be0e78809494 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,125 @@ 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; + + 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; + + 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 +1764,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.273.g2a3d683680-goog