Skip to content

Commit a7d7b9e

Browse files
mgeisertgithub-cygwin
authored andcommitted
Cygwin: Carry process affinity through to result
Due to deficient testing, the current code doesn't return a valid result to users of sched_getaffinity(). The updated code carries the determined procmask through to the generation of result cpu mask. Recognize Windows' limitation that if the process is multi-group (i.e., has threads in multiple cpu groups) there is no visibility to which processors in other groups are being used. One could remedy this by looping through all the process' threads, but that could be expensive so is left for future contemplation. In addition, we'd have to maintain our own copy of each thread's current group and mask in internal overhead. (By the way, multi-group processes are only possible on Windows systems with more than 64 hardware threads.) Reported-by: Christian Franke <[email protected]> Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257616.html Signed-off-by: Mark Geisert <[email protected]> Fixes: 641ecb0 ("Cygwin: Implement sched_[gs]etaffinity()")
1 parent f74dc93 commit a7d7b9e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

winsup/cygwin/release/3.6.0

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,7 @@ Fixes:
132132
- Fix starving of auxiliary signal return address stack in case signal
133133
handlers bail out (longjmp/setcontext).
134134
Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257648.html
135+
136+
- Fix sched_getaffinity(2) to carry determined process affinity all the way
137+
through to the returned result.
138+
Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257616.html

winsup/cygwin/sched.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,16 @@ __sched_getaffinity_sys (pid_t pid, size_t sizeof_set, cpu_set_t *set)
587587
goto done;
588588
}
589589

590-
KAFFINITY miscmask = groupmask (__get_cpus_per_group ());
590+
KAFFINITY fullmask = groupmask (__get_cpus_per_group ());
591+
/* if process is multi-group, we don't have processor visibility. */
592+
/*TODO We could provide the missing Windows visibility by book-keeping
593+
each thread's current group and mask in our thread overhead, updating
594+
them on sched_set_thread_affinity() calls. We could then assemble the
595+
total mask here by looping through all threads. */
596+
if (groupcount > 1)
597+
procmask = fullmask;
591598
for (int i = 0; i < groupcount; i++)
592-
setgroup (sizeof_set, set, grouparray[i], miscmask);
599+
setgroup (sizeof_set, set, grouparray[i], fullmask & procmask);
593600
}
594601
else
595602
status = ESRCH;

0 commit comments

Comments
 (0)