Read the "real_parent" field of task_struct

John Wood john.wood at gmx.com
Thu Oct 8 13:05:02 EDT 2020


Hi,

On Fri, Oct 02, 2020 at 06:59:22PM +0200, John Wood wrote:
>
> static void brute_share_stats(struct brute_stats **src,
> 			      struct brute_stats **dst)
> {
> 	spin_lock(&(*src)->lock);
> 	refcount_inc(&(*src)->refc);
> 	*dst = *src;
> 	spin_unlock(&(*src)->lock);
> }
>
> static int brute_task_alloc(struct task_struct *task, unsigned long clone_flags)
> {
> 	struct task_struct *p_task;
> 	struct brute_stats **stats, **p_stats;
>
> 	p_task = task->real_parent;		/////////// <----
> 	if (unlikely(!p_task))			/////////// <----
> 		return -ESRCH;
>
> 	stats = brute_stats_ptr(task);
> 	p_stats = brute_stats_ptr(p_task);	/////////// <----
>
> 	if (likely(*p_stats)) {
> 		brute_share_stats(p_stats, stats);
> 		return 0;
> 	}
>
> 	*stats = brute_new_stats();
> 	if (!*stats)
> 		return -ENOMEM;
>
> 	brute_share_stats(stats, p_stats);
> 	return 0;
> }
>
> This code is very untested.

Now the code is tested.

> And now my first question: how can I read the
> real_parent field in a secure way. Do I need to use an rcu_read_lock()/
> rcu_read_unlock() block? Do I need to use rcu_dereference? Do I need to
> use a read_lock(&task_list_lock)/read_unlock(&task_list_lock) block?
>
> The lines with the mark are not clear to me. Sorry.

Any help would be greatly appreciated. Thanks in advance.

Regards,
John Wood



More information about the Kernelnewbies mailing list