Skip to contentSkip to conversation

timens: Simplify some resource management through cleanup.h helpers

PATCH Applied Thomas Weißschuh 2 participants 9 messages Mar 30 – Apr 1 Posix clocks and timers

lore
Share & tools
lore
#0 lore raw

cover timens: Simplify some resource management through cleanup.h helpers

Based on tip/timers/vdso.

---
Thomas Weißschuh (4):
timens: Add a __free() wrapper for put_time_ns()
timens: Simplify some calls to put_time_ns()
timens: Use mutex guard in proc_timens_set_offset()
timens: Use task_lock guard in timens_get*()

 include/linux/time_namespace.h |  3 ++
 kernel/time/namespace.c        | 67 ++++++++++++++++--------------------------
 2 files changed, 29 insertions(+), 41 deletions(-)

---
base-commit: 8e936460de513407846993b8409bff5cebdf10bf
change-id: 20260325-timens-cleanup-4294bbffdfa3
prerequisite-change-id: 20250908-vdso-timens-decoupling-24c0eee0c6c4:v2
prerequisite-patch-id: 69020428722a28a11c698ef8439f266bbacf1a26
prerequisite-patch-id: df6df9340399a47bef2b29b5073c7603cbb1b3f3

Best regards,

Signature · 1 line
Thomas Weißschuh <[email protected]>
#1 lore raw

PATCH 1/4 timens: Add a __free() wrapper for put_time_ns()

The wrapper will be used to simplify cleanups of 'struct time_namespace'.

---
 include/linux/time_namespace.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/time_namespace.h +3 -0
index c1de21a27c34..58bd9728df58 100644
@@ -8,6 +8,7 @@
 #include <linux/ns_common.h>
 #include <linux/err.h>
 #include <linux/time64.h>
+#include <linux/cleanup.h>
 
 struct user_namespace;
 extern struct user_namespace init_user_ns;
@@ -171,4 +172,6 @@ static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma)
 }
 #endif /* CONFIG_TIME_NS_VDSO */
 
+DEFINE_FREE(time_ns, struct time_namespace *, if (_T) put_time_ns(_T))
+
 #endif /* _LINUX_TIMENS_H */
Signature · 1 line
2.53.0
#2 lore raw

[tip: timers/vdso] timens: Add a __free() wrapper for put_time_ns()

The following commit has been merged into the timers/vdso branch of tip:

The following commit has been merged into the timers/vdso branch of tip:

Commit-ID:     c2de5a5be4d60af5f928a2dd2b0f73e17358e346
Gitweb:        https://git.kernel.org/tip/c2de5a5be4d60af5f928a2dd2b0f73e17358e346
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Mon, 30 Mar 2026 09:07:55 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 01 Apr 2026 17:13:35 +02:00

timens: Add a __free() wrapper for put_time_ns()

The wrapper will be used to simplify cleanups of 'struct time_namespace'.

---
 include/linux/time_namespace.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/time_namespace.h +3 -0
index c1de21a..58bd972 100644
@@ -8,6 +8,7 @@
 #include <linux/ns_common.h>
 #include <linux/err.h>
 #include <linux/time64.h>
+#include <linux/cleanup.h>
 
 struct user_namespace;
 extern struct user_namespace init_user_ns;
@@ -171,4 +172,6 @@ static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma)
 }
 #endif /* CONFIG_TIME_NS_VDSO */
 
+DEFINE_FREE(time_ns, struct time_namespace *, if (_T) put_time_ns(_T))
+
 #endif /* _LINUX_TIMENS_H */
#3 lore raw

PATCH 2/4 timens: Simplify some calls to put_time_ns()

In reply to Thomas Weißschuh

Use the new __free() based cleanup helpers to simplify some functions.

---
 kernel/time/namespace.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)
kernel/time/namespace.c +7 -14
index 42302cc3f3fb..ae9a3a5d53ab 100644
@@ -18,6 +18,7 @@
 #include <linux/cred.h>
 #include <linux/err.h>
 #include <linux/mm.h>
+#include <linux/cleanup.h>
 
 #include "namespace_internal.h"
 
@@ -252,7 +253,7 @@ static void show_offset(struct seq_file *m, int clockid, struct timespec64 *ts)
 void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m)
 {
 	struct ns_common *ns;
-	struct time_namespace *time_ns;
+	struct time_namespace *time_ns __free(time_ns) = NULL;
 
 	ns = timens_for_children_get(p);
 	if (!ns)
@@ -261,14 +262,13 @@ void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m)
 
 	show_offset(m, CLOCK_MONOTONIC, &time_ns->offsets.monotonic);
 	show_offset(m, CLOCK_BOOTTIME, &time_ns->offsets.boottime);
-	put_time_ns(time_ns);
 }
 
 int proc_timens_set_offset(struct file *file, struct task_struct *p,
 			   struct proc_timens_offset *offsets, int noffsets)
 {
 	struct ns_common *ns;
-	struct time_namespace *time_ns;
+	struct time_namespace *time_ns __free(time_ns) = NULL;
 	struct timespec64 tp;
 	int i, err;
 
@@ -277,10 +277,8 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 		return -ESRCH;
 	time_ns = to_time_ns(ns);
 
-	if (!file_ns_capable(file, time_ns->user_ns, CAP_SYS_TIME)) {
-		put_time_ns(time_ns);
+	if (!file_ns_capable(file, time_ns->user_ns, CAP_SYS_TIME))
 		return -EPERM;
-	}
 
 	for (i = 0; i < noffsets; i++) {
 		struct proc_timens_offset *off = &offsets[i];
@@ -293,15 +291,12 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 			ktime_get_boottime_ts64(&tp);
 			break;
 		default:
-			err = -EINVAL;
-			goto out;
+			return -EINVAL;
 		}
 
-		err = -ERANGE;
-
 		if (off->val.tv_sec > KTIME_SEC_MAX ||
 		    off->val.tv_sec < -KTIME_SEC_MAX)
-			goto out;
+			return -ERANGE;
 
 		tp = timespec64_add(tp, off->val);
 		/*
@@ -309,7 +304,7 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 		 * still unreachable.
 		 */
 		if (tp.tv_sec < 0 || tp.tv_sec > KTIME_SEC_MAX / 2)
-			goto out;
+			return -ERANGE;
 	}
 
 	mutex_lock(&timens_offset_lock);
@@ -338,8 +333,6 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 
 out_unlock:
 	mutex_unlock(&timens_offset_lock);
-out:
-	put_time_ns(time_ns);
 
 	return err;
 }
Signature · 1 line
2.53.0
#4 lore raw

[tip: timers/vdso] timens: Simplify some calls to put_time_ns()

The following commit has been merged into the timers/vdso branch of tip:

The following commit has been merged into the timers/vdso branch of tip:

Commit-ID:     3fa3aeb4a5cb19e372680ef8860a0381cd5409e9
Gitweb:        https://git.kernel.org/tip/3fa3aeb4a5cb19e372680ef8860a0381cd5409e9
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Mon, 30 Mar 2026 09:07:56 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 01 Apr 2026 17:13:35 +02:00

timens: Simplify some calls to put_time_ns()

Use the new __free() based cleanup helpers to simplify some functions.

---
 kernel/time/namespace.c | 29 +++++++++++------------------
 1 file changed, 11 insertions(+), 18 deletions(-)
kernel/time/namespace.c +11 -18
index 42302cc..37495bd 100644
@@ -18,6 +18,7 @@
 #include <linux/cred.h>
 #include <linux/err.h>
 #include <linux/mm.h>
+#include <linux/cleanup.h>
 
 #include "namespace_internal.h"
 
@@ -251,36 +252,33 @@ static void show_offset(struct seq_file *m, int clockid, struct timespec64 *ts)
 
 void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m)
 {
-	struct ns_common *ns;
-	struct time_namespace *time_ns;
+	struct time_namespace *time_ns __free(time_ns) = NULL;
+	struct ns_common *ns = timens_for_children_get(p);
 
-	ns = timens_for_children_get(p);
 	if (!ns)
 		return;
+
 	time_ns = to_time_ns(ns);
 
 	show_offset(m, CLOCK_MONOTONIC, &time_ns->offsets.monotonic);
 	show_offset(m, CLOCK_BOOTTIME, &time_ns->offsets.boottime);
-	put_time_ns(time_ns);
 }
 
 int proc_timens_set_offset(struct file *file, struct task_struct *p,
 			   struct proc_timens_offset *offsets, int noffsets)
 {
-	struct ns_common *ns;
-	struct time_namespace *time_ns;
+	struct time_namespace *time_ns __free(time_ns) = NULL;
+	struct ns_common *ns = timens_for_children_get(p);
 	struct timespec64 tp;
 	int i, err;
 
-	ns = timens_for_children_get(p);
 	if (!ns)
 		return -ESRCH;
+
 	time_ns = to_time_ns(ns);
 
-	if (!file_ns_capable(file, time_ns->user_ns, CAP_SYS_TIME)) {
-		put_time_ns(time_ns);
+	if (!file_ns_capable(file, time_ns->user_ns, CAP_SYS_TIME))
 		return -EPERM;
-	}
 
 	for (i = 0; i < noffsets; i++) {
 		struct proc_timens_offset *off = &offsets[i];
@@ -293,15 +291,12 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 			ktime_get_boottime_ts64(&tp);
 			break;
 		default:
-			err = -EINVAL;
-			goto out;
+			return -EINVAL;
 		}
 
-		err = -ERANGE;
-
 		if (off->val.tv_sec > KTIME_SEC_MAX ||
 		    off->val.tv_sec < -KTIME_SEC_MAX)
-			goto out;
+			return -ERANGE;
 
 		tp = timespec64_add(tp, off->val);
 		/*
@@ -309,7 +304,7 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 		 * still unreachable.
 		 */
 		if (tp.tv_sec < 0 || tp.tv_sec > KTIME_SEC_MAX / 2)
-			goto out;
+			return -ERANGE;
 	}
 
 	mutex_lock(&timens_offset_lock);
@@ -338,8 +333,6 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 
 out_unlock:
 	mutex_unlock(&timens_offset_lock);
-out:
-	put_time_ns(time_ns);
 
 	return err;
 }
#5 lore raw

PATCH 3/4 timens: Use mutex guard in proc_timens_set_offset()

In reply to Thomas Weißschuh

Simplify the logic in proc_timens_set_offset() by converting the mutex usage to a guard().

---
 kernel/time/namespace.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
kernel/time/namespace.c +5 -11
index ae9a3a5d53ab..fbf026e63104 100644
@@ -270,7 +270,7 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 	struct ns_common *ns;
 	struct time_namespace *time_ns __free(time_ns) = NULL;
 	struct timespec64 tp;
-	int i, err;
+	int i;
 
 	ns = timens_for_children_get(p);
 	if (!ns)
@@ -307,13 +307,10 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 			return -ERANGE;
 	}
 
-	mutex_lock(&timens_offset_lock);
-	if (time_ns->frozen_offsets) {
-		err = -EACCES;
-		goto out_unlock;
-	}
+	guard(mutex)(&timens_offset_lock);
+	if (time_ns->frozen_offsets)
+		return -EACCES;
 
-	err = 0;
 	/* Don't report errors after this line */
 	for (i = 0; i < noffsets; i++) {
 		struct proc_timens_offset *off = &offsets[i];
@@ -331,10 +328,7 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 		*offset = off->val;
 	}
 
-out_unlock:
-	mutex_unlock(&timens_offset_lock);
-
-	return err;
+	return 0;
 }
 
 const struct proc_ns_operations timens_operations = {
Signature · 1 line
2.53.0
#6 lore raw

[tip: timers/vdso] timens: Use mutex guard in proc_timens_set_offset()

The following commit has been merged into the timers/vdso branch of tip:

The following commit has been merged into the timers/vdso branch of tip:

Commit-ID:     6d89dc8b1c559ee2662db86f7490ac52cab3030c
Gitweb:        https://git.kernel.org/tip/6d89dc8b1c559ee2662db86f7490ac52cab3030c
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Mon, 30 Mar 2026 09:07:57 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 01 Apr 2026 17:13:35 +02:00

timens: Use mutex guard in proc_timens_set_offset()

Simplify the logic in proc_timens_set_offset() by converting the mutex usage to a guard().

---
 kernel/time/namespace.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
kernel/time/namespace.c +5 -11
index 37495bd..084ceec 100644
@@ -270,7 +270,7 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 	struct time_namespace *time_ns __free(time_ns) = NULL;
 	struct ns_common *ns = timens_for_children_get(p);
 	struct timespec64 tp;
-	int i, err;
+	int i;
 
 	if (!ns)
 		return -ESRCH;
@@ -307,13 +307,10 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 			return -ERANGE;
 	}
 
-	mutex_lock(&timens_offset_lock);
-	if (time_ns->frozen_offsets) {
-		err = -EACCES;
-		goto out_unlock;
-	}
+	guard(mutex)(&timens_offset_lock);
+	if (time_ns->frozen_offsets)
+		return -EACCES;
 
-	err = 0;
 	/* Don't report errors after this line */
 	for (i = 0; i < noffsets; i++) {
 		struct proc_timens_offset *off = &offsets[i];
@@ -331,10 +328,7 @@ int proc_timens_set_offset(struct file *file, struct task_struct *p,
 		*offset = off->val;
 	}
 
-out_unlock:
-	mutex_unlock(&timens_offset_lock);
-
-	return err;
+	return 0;
 }
 
 const struct proc_ns_operations timens_operations = {
#7 lore raw

PATCH 4/4 timens: Use task_lock guard in timens_get*()

In reply to Thomas Weißschuh

Simplify the logic in timens_get*() by converting the task_lock usage to a guard().

---
 kernel/time/namespace.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
kernel/time/namespace.c +14 -16
index fbf026e63104..9d2e4870a2c5 100644
@@ -154,34 +154,32 @@ void free_time_ns(struct time_namespace *ns)
 
 static struct ns_common *timens_get(struct task_struct *task)
 {
-	struct time_namespace *ns = NULL;
+	struct time_namespace *ns;
 	struct nsproxy *nsproxy;
 
-	task_lock(task);
+	guard(task_lock)(task);
 	nsproxy = task->nsproxy;
-	if (nsproxy) {
-		ns = nsproxy->time_ns;
-		get_time_ns(ns);
-	}
-	task_unlock(task);
+	if (!nsproxy)
+		return NULL;
 
-	return ns ? &ns->ns : NULL;
+	ns = nsproxy->time_ns;
+	get_time_ns(ns);
+	return &ns->ns;
 }
 
 static struct ns_common *timens_for_children_get(struct task_struct *task)
 {
-	struct time_namespace *ns = NULL;
+	struct time_namespace *ns;
 	struct nsproxy *nsproxy;
 
-	task_lock(task);
+	guard(task_lock)(task);
 	nsproxy = task->nsproxy;
-	if (nsproxy) {
-		ns = nsproxy->time_ns_for_children;
-		get_time_ns(ns);
-	}
-	task_unlock(task);
+	if (!nsproxy)
+		return NULL;
 
-	return ns ? &ns->ns : NULL;
+	ns = nsproxy->time_ns_for_children;
+	get_time_ns(ns);
+	return &ns->ns;
 }
 
 static void timens_put(struct ns_common *ns)
Signature · 1 line
2.53.0
#8 lore raw

[tip: timers/vdso] timens: Use task_lock guard in timens_get*()

The following commit has been merged into the timers/vdso branch of tip:

The following commit has been merged into the timers/vdso branch of tip:

Commit-ID:     7138a8698a39e81eb153e05500823fff76d5b3bd
Gitweb:        https://git.kernel.org/tip/7138a8698a39e81eb153e05500823fff76d5b3bd
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Mon, 30 Mar 2026 09:07:58 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 01 Apr 2026 17:13:36 +02:00

timens: Use task_lock guard in timens_get*()

Simplify the logic in timens_get*() by converting the task_lock usage to a guard().

---
 kernel/time/namespace.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
kernel/time/namespace.c +14 -16
index 084ceec..4bca3f7 100644
@@ -154,34 +154,32 @@ void free_time_ns(struct time_namespace *ns)
 
 static struct ns_common *timens_get(struct task_struct *task)
 {
-	struct time_namespace *ns = NULL;
+	struct time_namespace *ns;
 	struct nsproxy *nsproxy;
 
-	task_lock(task);
+	guard(task_lock)(task);
 	nsproxy = task->nsproxy;
-	if (nsproxy) {
-		ns = nsproxy->time_ns;
-		get_time_ns(ns);
-	}
-	task_unlock(task);
+	if (!nsproxy)
+		return NULL;
 
-	return ns ? &ns->ns : NULL;
+	ns = nsproxy->time_ns;
+	get_time_ns(ns);
+	return &ns->ns;
 }
 
 static struct ns_common *timens_for_children_get(struct task_struct *task)
 {
-	struct time_namespace *ns = NULL;
+	struct time_namespace *ns;
 	struct nsproxy *nsproxy;
 
-	task_lock(task);
+	guard(task_lock)(task);
 	nsproxy = task->nsproxy;
-	if (nsproxy) {
-		ns = nsproxy->time_ns_for_children;
-		get_time_ns(ns);
-	}
-	task_unlock(task);
+	if (!nsproxy)
+		return NULL;
 
-	return ns ? &ns->ns : NULL;
+	ns = nsproxy->time_ns_for_children;
+	get_time_ns(ns);
+	return &ns->ns;
 }
 
 static void timens_put(struct ns_common *ns)