Skip to contentSkip to conversation

sched_setaffinity() with > 1024 cpus on gcc-9

DISCUSSION David Laight 1 participant 1 message Jun 5, 2019

lore
Share & tools
lore
#0 lore raw

I suspect that if I compile the following using gcc-9 It'll bleat about going beyond the end of the array:

#define _GNU_SOURCE
#include <sched.h>

#define MAX_CPU 2048

int fubar(void)

{
        cpu_set_t *set = CPU_ALLOC(MAX_CPU);
        CPU_SET_S(MAX_CPU - 2, CPU_ALLOC_SIZE(MAX_CPU), set);
        return CPU_COUNT_S(CPU_ALLOC_SIZE(MAX_CPU), set);
}

I can't test since I don't have a copy of gcc-9.

The whole interface for > 1024 cpus looks horrid though. Reading version 5.01 on the man page.
I'd have thought that if the application provided a short mask to sched_setaffinity() the rest would be deemed to be zeros. And that if an overlong mask is passed to sched_getaffinity() the extra bytes/longs would either be zerod or unchanged.

Be aware that CPU_ALLOC(3) may allocate a slightly larger CPU set than requested (because CPU sets are implemented as bit masks allocated in units of sizeof(long)). Consequently, sched_getaffinity() can set bits beyond the requested allocation size, because the kernel sees a few additional bits. Therefore, the caller should iterate over the bits in the returned set, counting those which are set, and stop upon reaching the value returned by CPU_COUNT(3) (rather than iterating over the number of bits requested to be allocated).

A short mask to sched_getaffinity() should also do something more sensible than just returning an error.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)