[RFC PATCH 1/5] sched, fork: Wire BLOG contexts into task lifecycle

Alex Markuze posted 5 patches 3 months, 2 weeks ago
[RFC PATCH 1/5] sched, fork: Wire BLOG contexts into task lifecycle
Posted by Alex Markuze 3 months, 2 weeks ago
Extend struct task_struct with a blog_contexts array to hold per-module
BLOG TLS pointers. Each task may have up to BLOG_MAX_MODULES (currently 8)
distinct logging contexts, allowing multiple subsystems to attach binary
loggers without interference.

The fork path (copy_process) initializes blog_contexts to NULL for new
tasks, ensuring clean initial state. The exit path (do_exit) calls
blog_tls_clear_task() to release any active contexts before task teardown,
ensuring contexts are properly recycled to the magazine pool and preventing
use-after-free scenarios.

These changes are conditional on CONFIG_BLOG. Kernels built without BLOG
support incur no storage or runtime overhead in task_struct.

This commit establishes the foundation for per-task binary logging contexts
but does not activate any logging functionality. The BLOG subsystem itself
is introduced in subsequent commits.

Signed-off-by: Alex Markuze <amarkuze@redhat.com>
---
 include/linux/sched.h |  7 +++++++
 kernel/fork.c         | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 07576479c0ed..e381f8421a11 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1278,6 +1278,13 @@ struct task_struct {
 	/* Journalling filesystem info: */
 	void				*journal_info;
 
+/* BLOG support - max modules defined here for use by other headers */
+#define BLOG_MAX_MODULES 8
+
+#ifdef CONFIG_BLOG
+	struct blog_tls_ctx		*blog_contexts[BLOG_MAX_MODULES];
+#endif
+
 	/* Stacked block device info: */
 	struct bio_list			*bio_list;
 
diff --git a/kernel/fork.c b/kernel/fork.c
index 3da0f08615a9..b06843af05a9 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -24,6 +24,9 @@
 #include <linux/sched/cputime.h>
 #include <linux/sched/ext.h>
 #include <linux/seq_file.h>
+#ifdef CONFIG_BLOG
+#include <linux/blog/blog.h>
+#endif
 #include <linux/rtmutex.h>
 #include <linux/init.h>
 #include <linux/unistd.h>
@@ -186,6 +189,29 @@ static inline struct task_struct *alloc_task_struct_node(int node)
 
 static inline void free_task_struct(struct task_struct *tsk)
 {
+#ifdef CONFIG_BLOG
+	/* Clean up any BLOG contexts */
+	{
+		struct blog_tls_ctx *contexts[BLOG_MAX_MODULES];
+		int i;
+
+		/* Step 1: Atomically detach all contexts while holding lock */
+		task_lock(tsk);
+		for (i = 0; i < BLOG_MAX_MODULES; i++) {
+			contexts[i] = tsk->blog_contexts[i];
+			tsk->blog_contexts[i] = NULL;
+		}
+		task_unlock(tsk);
+
+		/* Step 2: Release contexts outside the lock */
+		for (i = 0; i < BLOG_MAX_MODULES; i++) {
+			struct blog_tls_ctx *ctx = contexts[i];
+
+			if (ctx && ctx->release)
+				ctx->release(ctx);
+		}
+	}
+#endif
 	kmem_cache_free(task_struct_cachep, tsk);
 }
 
@@ -2012,6 +2038,17 @@ __latent_entropy struct task_struct *copy_process(
 	p = dup_task_struct(current, node);
 	if (!p)
 		goto fork_out;
+
+#ifdef CONFIG_BLOG
+	/* Initialize BLOG contexts */
+	{
+		int i;
+
+		for (i = 0; i < BLOG_MAX_MODULES; i++)
+			p->blog_contexts[i] = NULL;
+	}
+#endif
+
 	p->flags &= ~PF_KTHREAD;
 	if (args->kthread)
 		p->flags |= PF_KTHREAD;
-- 
2.34.1
Re: [RFC PATCH 1/5] sched, fork: Wire BLOG contexts into task lifecycle
Posted by Viacheslav Dubeyko 3 months, 1 week ago
On Fri, 2025-10-24 at 08:42 +0000, Alex Markuze wrote:
> Extend struct task_struct with a blog_contexts array to hold per-module
> BLOG TLS pointers. Each task may have up to BLOG_MAX_MODULES (currently 8)
> distinct logging contexts, allowing multiple subsystems to attach binary
> loggers without interference.
> 
> The fork path (copy_process) initializes blog_contexts to NULL for new
> tasks, ensuring clean initial state. The exit path (do_exit) calls
> blog_tls_clear_task() to release any active contexts before task teardown,
> ensuring contexts are properly recycled to the magazine pool and preventing
> use-after-free scenarios.
> 
> These changes are conditional on CONFIG_BLOG. Kernels built without BLOG
> support incur no storage or runtime overhead in task_struct.
> 
> This commit establishes the foundation for per-task binary logging contexts
> but does not activate any logging functionality. The BLOG subsystem itself
> is introduced in subsequent commits.
> 
> Signed-off-by: Alex Markuze <amarkuze@redhat.com>
> ---
>  include/linux/sched.h |  7 +++++++
>  kernel/fork.c         | 37 +++++++++++++++++++++++++++++++++++++
>  2 files changed, 44 insertions(+)
> 
> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index 07576479c0ed..e381f8421a11 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -1278,6 +1278,13 @@ struct task_struct {
>  	/* Journalling filesystem info: */
>  	void				*journal_info;
>  
> +/* BLOG support - max modules defined here for use by other headers */
> +#define BLOG_MAX_MODULES 8
> +
> +#ifdef CONFIG_BLOG
> +	struct blog_tls_ctx		*blog_contexts[BLOG_MAX_MODULES];
> +#endif
> +
>  	/* Stacked block device info: */
>  	struct bio_list			*bio_list;
>  
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 3da0f08615a9..b06843af05a9 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -24,6 +24,9 @@
>  #include <linux/sched/cputime.h>
>  #include <linux/sched/ext.h>
>  #include <linux/seq_file.h>
> +#ifdef CONFIG_BLOG
> +#include <linux/blog/blog.h>
> +#endif
>  #include <linux/rtmutex.h>
>  #include <linux/init.h>
>  #include <linux/unistd.h>
> @@ -186,6 +189,29 @@ static inline struct task_struct *alloc_task_struct_node(int node)
>  
>  static inline void free_task_struct(struct task_struct *tsk)
>  {
> +#ifdef CONFIG_BLOG
> +	/* Clean up any BLOG contexts */
> +	{
> +		struct blog_tls_ctx *contexts[BLOG_MAX_MODULES];
> +		int i;
> +
> +		/* Step 1: Atomically detach all contexts while holding lock */
> +		task_lock(tsk);
> +		for (i = 0; i < BLOG_MAX_MODULES; i++) {
> +			contexts[i] = tsk->blog_contexts[i];
> +			tsk->blog_contexts[i] = NULL;
> +		}
> +		task_unlock(tsk);
> +
> +		/* Step 2: Release contexts outside the lock */
> +		for (i = 0; i < BLOG_MAX_MODULES; i++) {
> +			struct blog_tls_ctx *ctx = contexts[i];
> +
> +			if (ctx && ctx->release)
> +				ctx->release(ctx);
> +		}
> +	}
> +#endif

It looks like a function that can hide this CONFIG_BLOG declarations. What's
about to introduce the function for this logic?

#ifdef CONFIG_BLOG
static inline void free_blog_context()
{
   <logic is here>
}
#else
static inline void free_blog_context() {}
#endif


>  	kmem_cache_free(task_struct_cachep, tsk);
>  }
>  
> @@ -2012,6 +2038,17 @@ __latent_entropy struct task_struct *copy_process(
>  	p = dup_task_struct(current, node);
>  	if (!p)
>  		goto fork_out;
> +
> +#ifdef CONFIG_BLOG
> +	/* Initialize BLOG contexts */
> +	{
> +		int i;
> +
> +		for (i = 0; i < BLOG_MAX_MODULES; i++)
> +			p->blog_contexts[i] = NULL;
> +	}
> +#endif
> +

The same here. What's about to introduce the function for this logic?

By the way, could memset() be used here instead of loop?

Thanks,
Slava.

>  	p->flags &= ~PF_KTHREAD;
>  	if (args->kthread)
>  		p->flags |= PF_KTHREAD;
Re: [RFC PATCH 1/5] sched, fork: Wire BLOG contexts into task lifecycle
Posted by Steven Rostedt 3 months, 2 weeks ago

On Fri, 24 Oct 2025 08:42:55 +0000
Alex Markuze <amarkuze@redhat.com> wrote:

> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index 07576479c0ed..e381f8421a11 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -1278,6 +1278,13 @@ struct task_struct {
>  	/* Journalling filesystem info: */
>  	void				*journal_info;
>  
> +/* BLOG support - max modules defined here for use by other headers */
> +#define BLOG_MAX_MODULES 8
> +
> +#ifdef CONFIG_BLOG
> +	struct blog_tls_ctx		*blog_contexts[BLOG_MAX_MODULES];
> +#endif
> +
>  	/* Stacked block device info: */
>  	struct bio_list			*bio_list;
>  
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 3da0f08615a9..b06843af05a9 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -24,6 +24,9 @@
>  #include <linux/sched/cputime.h>
>  #include <linux/sched/ext.h>
>  #include <linux/seq_file.h>
> +#ifdef CONFIG_BLOG
> +#include <linux/blog/blog.h>
> +#endif

The proper way to do this is to have the #ifdef in the header file and not
in the C file.

>  #include <linux/rtmutex.h>
>  #include <linux/init.h>
>  #include <linux/unistd.h>
> @@ -186,6 +189,29 @@ static inline struct task_struct *alloc_task_struct_node(int node)
>  
>  static inline void free_task_struct(struct task_struct *tsk)
>  {
> +#ifdef CONFIG_BLOG
> +	/* Clean up any BLOG contexts */
> +	{

There should be a function that gets called here that frees up the context.
This does not belong in the fork.c file.

	blog_free(task);

In the header file have:

#ifdef CONFIG_BLOG
[..]
void blog_free(struct task_struct *task);
#else
static inline blog_free(struct task_struct *task)
{
}
#endif /* CONFIG_BLOG */



> +		struct blog_tls_ctx *contexts[BLOG_MAX_MODULES];
> +		int i;
> +
> +		/* Step 1: Atomically detach all contexts while holding lock */
> +		task_lock(tsk);
> +		for (i = 0; i < BLOG_MAX_MODULES; i++) {
> +			contexts[i] = tsk->blog_contexts[i];
> +			tsk->blog_contexts[i] = NULL;
> +		}
> +		task_unlock(tsk);
> +
> +		/* Step 2: Release contexts outside the lock */
> +		for (i = 0; i < BLOG_MAX_MODULES; i++) {
> +			struct blog_tls_ctx *ctx = contexts[i];
> +
> +			if (ctx && ctx->release)
> +				ctx->release(ctx);
> +		}
> +	}
> +#endif
>  	kmem_cache_free(task_struct_cachep, tsk);
>  }
>  
> @@ -2012,6 +2038,17 @@ __latent_entropy struct task_struct *copy_process(
>  	p = dup_task_struct(current, node);
>  	if (!p)
>  		goto fork_out;
> +
> +#ifdef CONFIG_BLOG
> +	/* Initialize BLOG contexts */
> +	{
> +		int i;
> +
> +		for (i = 0; i < BLOG_MAX_MODULES; i++)
> +			p->blog_contexts[i] = NULL;
> +	}
> +#endif

Same here.

> +
>  	p->flags &= ~PF_KTHREAD;
>  	if (args->kthread)
>  		p->flags |= PF_KTHREAD;

-- Steve