[LTP] [PATCH v2 1/2] memcontrol: Add bind_to_current_cpu() helper

Wei Gao wegao@suse.com
Thu Oct 22 08:20:50 CEST 2026


On Wed, Sep 30, 2026 at 03:53:14AM +0000, Wei Gao wrote:
> Add bind_to_current_cpu() to memcontrol_common.h to allow memcg tests
> to bind the test process to a single CPU.
> 
> Signed-off-by: Wei Gao <wegao@suse.com>
> ---
>  .../controllers/memcg/memcontrol_common.h     | 27 +++++++++++++++++++
>  1 file changed, 27 insertions(+)
> 
> diff --git a/testcases/kernel/controllers/memcg/memcontrol_common.h b/testcases/kernel/controllers/memcg/memcontrol_common.h
> index 495f924a0..0f5021c0d 100644
> --- a/testcases/kernel/controllers/memcg/memcontrol_common.h
> +++ b/testcases/kernel/controllers/memcg/memcontrol_common.h
> @@ -52,4 +52,31 @@ static inline void alloc_anon(const size_t size)
>  	free(buf);
>  }
>  
> +static inline void bind_to_current_cpu(void)
> +{
> +	int cpu = sched_getcpu();
> +	size_t mask_size;
> +	cpu_set_t *mask;
> +
> +	if (cpu < 0)
> +		tst_brk(TBROK | TERRNO, "sched_getcpu() failed");
> +
> +	tst_res(TINFO, "Binding test process to CPU %d", cpu);
> +
> +	mask = CPU_ALLOC(cpu + 1);
> +	if (!mask)
> +		tst_brk(TBROK | TERRNO, "CPU_ALLOC() failed");
> +
> +	mask_size = CPU_ALLOC_SIZE(cpu + 1);
> +	CPU_ZERO_S(mask_size, mask);
> +	CPU_SET_S(cpu, mask_size, mask);
> +
> +	if (sched_setaffinity(0, mask_size, mask) < 0) {
> +		CPU_FREE(mask);
> +		tst_brk(TBROK | TERRNO, "sched_setaffinity() failed");
> +	}
> +
> +	CPU_FREE(mask);
> +}
> +
>  #endif /* MEMCONTROL_COMMON_H__ */
> -- 
> 2.55.0
> 
Hi Michal

Could you help check this patch? I already change to pinning to the
single CPU for all arch.

Thanks
Wei Gao


More information about the ltp mailing list