From nobody Fri Apr 17 11:54:07 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 56D15315D35 for ; Fri, 20 Feb 2026 05:55:15 +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=1771566916; cv=none; b=GfAsfAnCVPRRgEB+0BeyxjQ/yE/q8jFxYlDCayyX/bqY+0ymaSpKWM8CCwsMS6gO1UaMAJ+rz1PwF1eHf2kRz9SlcKcqia3eUBvrWpGZbwjXcYaGE5Wvzr1GR5AbYSR7lReQeFrktV0d4t40mWtlX5dhTumXd/mKjYfPPMDjwmQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771566916; c=relaxed/simple; bh=d/Njxo6LsxfyrmnJNgQrbf4Jrkxu+B4X3AZ0DsjVEVo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jI2BPBtnxfv27S/ArK8ygHYPtaw5/ASQPlNaZ32ci5EPOpcVbgVUCORfFRQrPMQodozU4aNYFBI6htBz1HYEZIbVy5rZ8AZdyTt0iPA95IlM02gNTw3uh7+9NV2RRkFRgGbbVOBdsTXbP3N64ozm9qukXMckK4kFrF9zqHPe/pQ= 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=SZ9kflJm; 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="SZ9kflJm" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2ab0b2e804cso21005695ad.3 for ; Thu, 19 Feb 2026 21:55:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771566915; x=1772171715; 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=Ej8LZF1b2o/Ks4R9g/3WwRmZsBfUCiZmdJJkOcGo2tM=; b=SZ9kflJmOLf6mfxQZrtOSRooBSwUjxlYGdxn0wXR68/2lgpl9UvUKfnKAVue4uRi7O zlf+ld9GMTgb9fO4szslpS75J1Avk2LvZbLEDiOzCVafZteADj2sfHhQXpEyjzE0kWa6 ZLzDQt0+6QrA1JpfIwn2Wux7dZtwwDsfRD2NeGxKYY8fVCmDa8ktXY4RanZ3vw0RmBK/ j1AG1U8Ib3IXyE7y1W46/8AC1wgiJzDCSBZrEq2U0vM3+UhwShGaaDFGdOZ5wIwmZiz9 vuhHTJtiPKXZWH/T3P1TxV14OiIR+T3fEcUpgsPEcSWaqG7hx+S0KMc21HemBrwAFbTK 6fNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771566915; x=1772171715; 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=Ej8LZF1b2o/Ks4R9g/3WwRmZsBfUCiZmdJJkOcGo2tM=; b=oass3rF+9rxpCXv2o3m6IVjZgCtqKmJw7pgoyWxYsjLAEK30K+pOx/wBCVfJPvovnG PP7l++Ru09m5ti8yDxyk9HKlwFuMZAy7qZHrvlAkqPfh6zDKzDOzdObrCxt06RjX2Wsk /wE/983nLU1zmRiG4iEiUViqrVRI33DDEEvHHP1neNB153Kr5H1GNDp2nTxzjqcJeNTr LJAoyvqPzuAJgAK44VJepICVMjHD3FgJyYy3/X6r35TSzaEIgEEAtUFXcA3b9eq3r6j9 5lSEuYA20eF0489gvMAq4gGHrfiTw6h/S3Xw/r6pAi3015j4hKmhMgf/KPMT7HD6SiT+ o+fw== X-Forwarded-Encrypted: i=1; AJvYcCVtOxqA0dZMZdedPfCdSKbrAm4UamRzPhn50Z0cz4scCLSwKwapN3Fn9xrASsnMP9G8S+JNDD3O9EUvBVI=@vger.kernel.org X-Gm-Message-State: AOJu0YzyCtFgJhIpHnJuXP2SFGQsQ9YwmiMHCjWvDNurGcSuw3OwsCOt LtvUcg7cg2S/c02ZKuML0MYNCNgdwjr4rpHCihchepZf9SygTXFEMMxmwwYpDGiJInaOFcbTj5F 7GYgUImQvnTQNdOwO8g== X-Received: from plma8.prod.google.com ([2002:a17:902:7d88:b0:2a0:92b7:79c1]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:3bd0:b0:2aa:dc84:251f with SMTP id d9443c01a7336-2ad5aec750dmr47251955ad.2.1771566914488; Thu, 19 Feb 2026 21:55:14 -0800 (PST) Date: Thu, 19 Feb 2026 21:54:46 -0800 In-Reply-To: <20260220055449.3073-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: <20260220055449.3073-1-tjmercier@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260220055449.3073-2-tjmercier@google.com> Subject: [PATCH v4 1/3] kernfs: Don't set_nlink for directories being removed 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, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, shuah@kernel.org, linux-kselftest@vger.kernel.org Cc: "T.J. Mercier" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If a directory is already in the process of removal its i_nlink count becomes irrelevant because its contents are also about to be removed and any pending filesystem operations on it or its contents will soon start to fail. So we can avoid setting it for directories already flagged for removal. This avoids a race in the next patch, which adds clearing of the i_nlink count for kernfs nodes being removed to support inotify delete events. Use protection from the kernfs_iattr_rwsem to avoid adding more contention to the kernfs_rwsem for calls to kernfs_refresh_inode. Signed-off-by: T.J. Mercier --- fs/kernfs/dir.c | 2 ++ fs/kernfs/inode.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 29baeeb97871..5b6ce2351a53 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -1491,12 +1491,14 @@ static void __kernfs_remove(struct kernfs_node *kn) pr_debug("kernfs %s: removing\n", kernfs_rcu_name(kn)); =20 /* prevent new usage by marking all nodes removing and deactivating */ + down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); pos =3D NULL; while ((pos =3D kernfs_next_descendant_post(pos, kn))) { pos->flags |=3D KERNFS_REMOVING; if (kernfs_active(pos)) atomic_add(KN_DEACTIVATED_BIAS, &pos->active); } + up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); =20 /* deactivate and unlink the subtree node-by-node */ do { diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c index a36aaee98dce..afdc4021e81a 100644 --- a/fs/kernfs/inode.c +++ b/fs/kernfs/inode.c @@ -178,7 +178,7 @@ static void kernfs_refresh_inode(struct kernfs_node *kn= , struct inode *inode) */ set_inode_attr(inode, attrs); =20 - if (kernfs_type(kn) =3D=3D KERNFS_DIR) + if (kernfs_type(kn) =3D=3D KERNFS_DIR && !(kn->flags & KERNFS_REMOVING)) set_nlink(inode, kn->dir.subdirs + 2); } =20 --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 11:54:07 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 AA10532FA20 for ; Fri, 20 Feb 2026 05:55:18 +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=1771566920; cv=none; b=akEwf5whfqppbnFQJANjs1F/oB2SD4Vhn9VYZdqLjA5fGUah2jVTmvMs4oV6XFQ+IX3bFWkIwpLrQ+GuKNPvxABTHZ0E+0jsBTR45fqzR0b12SInE6hPRPl59rC+CK+ScnAGnxPKPvtfRDyGuCZKPfTCtUF3zc1P4okAlj+wXsU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771566920; c=relaxed/simple; bh=yUmonH/CIzK5lTEiC0IVbnht7EAF3Vi0dnZPfCMsJ2Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WCyv6lfjj/ntfbAOJt0pl6GbUFfao/NNb4bBIziPftWTREtVT5cu8aGTC7o7KajRJYwrd60d/2oG65FQRqWypn4UeW23wtD/lUdtcyUSytbec4F9XHvGHc70J0Y9vRPmCElXEm41TnZ2hmAsP6FhBfZ3gLQDOHuYb6loEU1RpG4= 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=zR6xDTLP; 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="zR6xDTLP" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-354bc535546so1949588a91.3 for ; Thu, 19 Feb 2026 21:55:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771566918; x=1772171718; 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=gZMtE/y1E7pRM+BgrGCWh1tlQyBGUgnD2quexZvuA3I=; b=zR6xDTLPIPyJj2ALo9HaoWuJs6szIUR+nw7pPRGuF1WpLcPk8rr+gjBHszKvgOwDDS 5J2thD5hWM+ZTTf4OSQm9HHXsb0VlkjqmId4q83snqdN/xQRaaTwcZshS31qA4eRsrgi wU4B/xGC2OLSknGeix8ezWoR4h7rbKLFShQgQHLoM0T+Wha6tStVyWhXSXsrCvCFsLDi aNM0lHBpHipClhsx4YeeFho7V6/WKMy3V3S9BDX3zrDGarJe8SQXixmSGCQnxv64lJIr VboUZBfNEMKuZKR9spnhZk6zz847djmH3lmWDfxxwVl9qbIr9r/LGt/Q7WoTg8RBkjv2 JhQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771566918; x=1772171718; 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=gZMtE/y1E7pRM+BgrGCWh1tlQyBGUgnD2quexZvuA3I=; b=qurVCa4G1j33kzpP/htVYkICk7LrlHZUUTnPBfKYBFKKL6hHf4c3bqlxlQglQOKwlv i658xdB9l7WI7Ru1pK2y14YKJLZfTzSo1iQAQ2iqqnzaI2M/QI5+xZONJxLmjt+WO4Ee iWLD7T8JmynAci4zEaJ3UqCgA8rdLscuQ9K0JjY4/B1WEqOT2/X256a64uGF15Pgar+B 5CoHrZIdnhHnEIMqf6zMsAG4vMFhmWKYxIteWmYo9GIVwdclYYfr4BecCHpPqomfIxA/ LqLyWqexIUF7lc1nUcpajzx+pVk5nwRyastriyO7s1pwgOzeBZGawZ0/KQCc+FfItyNb f4iQ== X-Forwarded-Encrypted: i=1; AJvYcCWUEfjO4mU/h8xJ4/0T2crzoarv4IBqhn9F+O+k6fEEFhF4UpjqWe6bSlHdtGkDnNEBxZix9ta702YvqO4=@vger.kernel.org X-Gm-Message-State: AOJu0Yy7XR8AvXnqG6qgWzHP4G9Ctd3BQ2ujEa84qQks6yKMkn4/Mqnb oBg8WaQNGaVdgG/grAjRl4SZOk9C48cu8sO/LCLkUd315VeT64SlobZtPLAMpxZT3ZvZPYRXSb7 Fdip1WP8qYmt5f2wE8A== X-Received: from pgac4.prod.google.com ([2002:a05:6a02:2944:b0:c66:4148:f3f6]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:918e:b0:38c:792:56b0 with SMTP id adf61e73a8af0-394839d9911mr18666582637.38.1771566917867; Thu, 19 Feb 2026 21:55:17 -0800 (PST) Date: Thu, 19 Feb 2026 21:54:47 -0800 In-Reply-To: <20260220055449.3073-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: <20260220055449.3073-1-tjmercier@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260220055449.3073-3-tjmercier@google.com> Subject: [PATCH v4 2/3] kernfs: Send IN_DELETE_SELF and IN_IGNORED 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, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, 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 means inotify watches persist after file deletion until the process exits and the inotify file descriptor is cleaned up, or until inotify_rm_watch is called manually. 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) Holding a pidfd for every monitored cgroup (can exhaust file descriptors). This patch enables IN_DELETE_SELF and IN_IGNORED events for kernfs files and directories by clearing inode i_nlink values during removal. This allows VFS to make the necessary fsnotify calls so that userspace receives the inotify events. As a result, applications can rely on a single existing watch on a 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 efficiency. There is gap in this implementation for certain file removals due their unique nature in kernfs. Directory removals that trigger file removals occur through vfs_rmdir, which shrinks the dcache and emits fsnotify events after the rmdir operation; there is no issue here. However kernfs writes to particular files (e.g. cgroup.subtree_control) can also cause file removal, but vfs_write does not attempt to emit fsnotify events after the write operation, even if i_nlink counts are 0. As a usecase for monitoring this category of file removals is not known, they are left without having IN_DELETE or IN_DELETE_SELF events generated. Suggested-by: Jan Kara Signed-off-by: T.J. Mercier Acked-by: Tejun Heo --- fs/kernfs/dir.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 5b6ce2351a53..41541b969fb2 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -1471,6 +1471,23 @@ void kernfs_show(struct kernfs_node *kn, bool show) up_write(&root->kernfs_rwsem); } =20 +static void kernfs_clear_inode_nlink(struct kernfs_node *kn) +{ + struct kernfs_root *root =3D kernfs_root(kn); + struct kernfs_super_info *info; + + lockdep_assert_held_read(&root->kernfs_supers_rwsem); + + list_for_each_entry(info, &root->supers, node) { + struct inode *inode =3D ilookup(info->sb, kernfs_ino(kn)); + + if (inode) { + clear_nlink(inode); + iput(inode); + } + } +} + static void __kernfs_remove(struct kernfs_node *kn) { struct kernfs_node *pos, *parent; @@ -1479,6 +1496,7 @@ static void __kernfs_remove(struct kernfs_node *kn) if (!kn) return; =20 + lockdep_assert_held_read(&kernfs_root(kn)->kernfs_supers_rwsem); lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem); =20 /* @@ -1522,9 +1540,11 @@ static void __kernfs_remove(struct kernfs_node *kn) struct kernfs_iattrs *ps_iattr =3D parent ? parent->iattr : NULL; =20 - /* update timestamps on the parent */ down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); =20 + kernfs_clear_inode_nlink(pos); + + /* update timestamps on the parent */ if (ps_iattr) { ktime_get_real_ts64(&ps_iattr->ia_ctime); ps_iattr->ia_mtime =3D ps_iattr->ia_ctime; @@ -1553,9 +1573,11 @@ void kernfs_remove(struct kernfs_node *kn) =20 root =3D kernfs_root(kn); =20 + down_read(&root->kernfs_supers_rwsem); down_write(&root->kernfs_rwsem); __kernfs_remove(kn); up_write(&root->kernfs_rwsem); + up_read(&root->kernfs_supers_rwsem); } =20 /** @@ -1646,6 +1668,7 @@ bool kernfs_remove_self(struct kernfs_node *kn) bool ret; struct kernfs_root *root =3D kernfs_root(kn); =20 + down_read(&root->kernfs_supers_rwsem); down_write(&root->kernfs_rwsem); kernfs_break_active_protection(kn); =20 @@ -1675,7 +1698,9 @@ bool kernfs_remove_self(struct kernfs_node *kn) break; =20 up_write(&root->kernfs_rwsem); + up_read(&root->kernfs_supers_rwsem); schedule(); + down_read(&root->kernfs_supers_rwsem); down_write(&root->kernfs_rwsem); } finish_wait(waitq, &wait); @@ -1690,6 +1715,7 @@ bool kernfs_remove_self(struct kernfs_node *kn) kernfs_unbreak_active_protection(kn); =20 up_write(&root->kernfs_rwsem); + up_read(&root->kernfs_supers_rwsem); return ret; } =20 @@ -1716,6 +1742,7 @@ int kernfs_remove_by_name_ns(struct kernfs_node *pare= nt, const char *name, } =20 root =3D kernfs_root(parent); + down_read(&root->kernfs_supers_rwsem); down_write(&root->kernfs_rwsem); =20 kn =3D kernfs_find_ns(parent, name, ns); @@ -1726,6 +1753,7 @@ int kernfs_remove_by_name_ns(struct kernfs_node *pare= nt, const char *name, } =20 up_write(&root->kernfs_rwsem); + up_read(&root->kernfs_supers_rwsem); =20 if (kn) return 0; --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 11:54:07 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 5BFAA32E6A2 for ; Fri, 20 Feb 2026 05:55:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771566924; cv=none; b=Qv5//eHhL/u9BMAm/yprn+0YZDfAKLzgk2i3FVbYpMg1VhNcu36mMZm4eHeHMhLq/pnBXW2Syuv6MV4/nItg+u7IWi7R5ECDUwGfsxGBuxQxbhCwJPh47JklUIiyhAn3IaDzC1p2K069ujPXoZXEqn9+fuPqmNM+/Y6WQkGJdYc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771566924; c=relaxed/simple; bh=ymKrZvnn1wXoX3MO7tLDPSCDvegTjqhP4f2VwqTSGPs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kNq7pPVDZZ57JVqJO/PBm1kc4x0kGaVxp9PAZVPzzFujjuQ53ly5A06Y5Fx3MOLzIiYkEfZ/ktN6XMSmGBFB7vzqCsMMLIAvCkVS5/JJ6MO25UD9VsRFqKKKEzDfaW3dsz2frAtM26XJ5t5AOvYsu7e2WcMeelmfzXLqpAkQ0iE= 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=XvvK9z35; arc=none smtp.client-ip=209.85.216.74 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="XvvK9z35" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-354c44bf176so1699787a91.0 for ; Thu, 19 Feb 2026 21:55:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771566922; x=1772171722; 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=VLezOUj++5KtU+iy76kNFYSHOIOxW3z9n/oglLgyr0s=; b=XvvK9z3505mugn+OFk5vRK0PeXyyHS3SoBc1LZPu+8cqwFxy9MMW+kYvNulm3BBOJ6 DiNn37woVS2EsgwigDMLm2kWjN8FF96sEeRnpGRSxKfRX5cl3lH8FeLpcgCzrEx3vmys k5lQ4+OC3W+VO8b0eNBaLHkXB8v3gXR+ASkhQdFB4AXPcljgSr3U8wKo1551VH0kGWhJ 0riqvduSIVwYi7le9CrsKhf1mSVsNwUpZVcHLIo8jzqwCmcnSQmYidbwJWxA571zCaAL Ea4kqJag7fOCtinNADc+e45trP6k9bOYUtjJWfVqSbfh9BejsYdEGtJKauCOwlektk3e uLNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771566922; x=1772171722; 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=VLezOUj++5KtU+iy76kNFYSHOIOxW3z9n/oglLgyr0s=; b=Lvbl05SmyJ6olmdcw4j15tbnpc7GlFjHoSzV30swLUiUcUWhw+2AzSl2mazsao1DXf aLzWqxMw9iLtdlriqL3TKx2EW4SgqRPZBzth8xhw/yekMzUjgfhhm3NuFf9/Yf3pPn3y ErT6Pl5cPUQR4/Si9YeI6aBdMZZ+HIPSxrme4Fo5on2MAEeFytickyRxcjepqkTSpfHZ utTGKgITQyEK6XOiXXLzBoOrSFqX/PsVFpzwCKogCjb1TSirmDmHGGIez6D5FjDgwjvf DPphiMG1XLioFbhnaq1WB3OkxkMkjoWAXdwHHGfL/JnQirces27O2jwTPSmebK7MGU3P a3hQ== X-Forwarded-Encrypted: i=1; AJvYcCVSVaiLovDP9AYdf+cQKPENcXwb6oJOsbU1teOlMvEawJceXV3eQ+qWnuiYb2ceLYuT+F1PeUqWryNxvzg=@vger.kernel.org X-Gm-Message-State: AOJu0YyY7OqbPhIqeyFQYZ+rwgqXKIrL2f4FIlUD0Oiy3gtghKj9X2Vz 7OzO+vN9rPJ6dwzwNuilsg70XgfXn6hicweUosu4h3bBQ1RFc8/I6ml/7GOeL3iFnzzhiWCiflC OHLXdj1QPwsguUWLd2g== X-Received: from pjmm7.prod.google.com ([2002:a17:90b:5807:b0:349:8a6d:dfd1]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:274c:b0:343:c3d1:8b9b with SMTP id 98e67ed59e1d1-35844f85b15mr17487949a91.19.1771566921614; Thu, 19 Feb 2026 21:55:21 -0800 (PST) Date: Thu, 19 Feb 2026 21:54:48 -0800 In-Reply-To: <20260220055449.3073-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: <20260220055449.3073-1-tjmercier@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260220055449.3073-4-tjmercier@google.com> Subject: [PATCH v4 3/3] selftests: memcg: Add tests for IN_DELETE_SELF and IN_IGNORED 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, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, 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 or directories are removed with rmdir. Signed-off-by: T.J. Mercier Acked-by: Tejun Heo Acked-by: Amir Goldstein --- .../selftests/cgroup/test_memcontrol.c | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testi= ng/selftests/cgroup/test_memcontrol.c index 4e1647568c5b..57726bc82757 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,115 @@ 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 =3D NULL; + 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; +} + +static int test_memcg_inotify_delete_dir(const char *root) +{ + int ret =3D KSFT_FAIL; + char *memcg =3D NULL; + 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, memcg, IN_DELETE_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 +1754,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_dir), }; #undef T =20 --=20 2.53.0.414.gf7e9f6c205-goog