Skip to content

Fix cpu affinity set size to have minimal size - #133497

Merged
janvorli merged 2 commits into
dotnet:mainfrom
janvorli:fix-cpu-affinity-issue
Sep 11, 2026
Merged

janvorli merged 2 commits into
dotnet:mainfrom
janvorli:fix-cpu-affinity-issue

Conversation

@janvorli

@janvorli janvorli commented Sep 9, 2026

Copy link
Copy Markdown
Member

There is a problem when the /sys/devices/system/cpu/possible reports less CPUs than the kernel keeps as nr_cpu_ids. My recent change has made the CPU set size dynamic based on the possible CPU count. In the problematic case, kernel returns failure from the sched_getaffinity syscall, since it requires the passed in CPU set size to be >= nr_cpu_ids.

This change fixes it by always using CPU_SETSIZE as the minimum size allocated, which is the default size when the CPU set is not allocated dynamically and which was used before my recent fix.

Close #133449

There is a problem when the /sys/devices/system/cpu/possible reports less
CPUs than the kernel keeps as `nr_cpu_ids`. My recent change has made the
CPU set size dynamic based on the possible CPU count. In the problematic
case, kernel returns failure from the sched_getaffinity syscall, since it
requires the passed in CPU set size to be >= `nr_cpu_ids`.

This change fixes it by always using CPU_SETSIZE as the minimum size allocated,
which is the default size when the CPU set is not allocated dynamically and
which was used before my recent fix.

Close dotnet#133449
@janvorli janvorli added this to the 10.0.x milestone Sep 9, 2026
@janvorli janvorli self-assigned this Sep 9, 2026
Copilot AI lite review requested due to automatic review settings September 9, 2026 13:28
@janvorli
janvorli requested a review from kkokosa September 9, 2026 13:28
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The behavioral change is narrowly scoped and consistent across affected Unix implementations, with only a minor whitespace nit noted.

Pull request overview

This PR adjusts several Unix CPU-affinity code paths to ensure the cpu_set_t buffer passed to sched_getaffinity/sched_setaffinity is never smaller than CPU_SETSIZE, avoiding failures when the kernel’s nr_cpu_ids exceeds the CPU count inferred from /sys/devices/system/cpu/possible.

Changes:

  • Allocate cpu_set_t with max(configuredCpuCount, CPU_SETSIZE) to guarantee a minimum affinity mask size.
  • Reuse the computed cpuSetSize consistently when calling sched_setaffinity / CPU_COUNT_S.
  • Apply the same sizing logic across CoreCLR PAL, CoreCLR GC Unix env, and NativeAOT PAL to keep behavior consistent.
File summaries
File Description
src/coreclr/pal/src/thread/thread.cpp Ensures affinity mask allocation is at least CPU_SETSIZE and reuses cpuSetSize for sched_setaffinity.
src/coreclr/pal/src/misc/sysinfo.cpp Ensures sched_getaffinity uses a mask sized to at least CPU_SETSIZE before counting CPUs.
src/coreclr/nativeaot/Runtime/unix/PalUnix.cpp Aligns NativeAOT CPU-count initialization with the minimum CPU_SETSIZE affinity mask allocation.
src/coreclr/gc/unix/gcenv.unix.cpp Ensures GC’s affinity probing and thread affinity setting allocate a mask sized to at least CPU_SETSIZE.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread src/coreclr/gc/unix/gcenv.unix.cpp Resolved
Copilot AI review requested due to automatic review settings September 9, 2026 20:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

GC Unix changes introduce a Debug-only abort that defeats the newly-added fallback, and one new call site uses a uint32_t cpu count with APIs that expect an int, risking conversion warnings/errors.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

src/coreclr/gc/unix/gcenv.unix.cpp:911

  • In SetThreadAffinity, cpusToAllocate is uint32_t, but the CPU_ALLOC/CPU_ALLOC_SIZE family takes an int cpu count on Linux. Keeping this as int (as done in the other call sites in this PR) avoids signed/unsigned conversions and potential -Wconversion/-Wsign-conversion build breaks.
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread src/coreclr/gc/unix/gcenv.unix.cpp Resolved

@kkokosa kkokosa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Maybe longer term, retrying sched_getaffinity with a larger mask on EINVAL would be more robust? Not sure if it is worth it.

@janvorli
janvorli merged commit b44cd90 into dotnet:main Sep 11, 2026
109 checks passed
@janvorli

Copy link
Copy Markdown
Member Author

/backport to 11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to 11.0 (link to workflow run)

@janvorli

Copy link
Copy Markdown
Member Author

/backport to 10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to 10.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

@janvorli an error occurred while backporting to 11.0. See the workflow output for details.

@github-actions

Copy link
Copy Markdown
Contributor

@janvorli an error occurred while backporting to 10.0. See the workflow output for details.

@lg2de

lg2de commented Sep 12, 2026

Copy link
Copy Markdown

/backport to 10.0

Did you mean release/10.0?

@janvorli

Copy link
Copy Markdown
Member Author

@lg2de yes, I've meant that. Anyways, I've created the backports manually afterwards.

@github-actions github-actions Bot locked and limited conversation to collaborators Oct 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server GC heap initialization fails with 0x8013200A ("affinitized to 0 CPUs") on legacy OpenVZ hosts due to mismatched CPU affinity reporting

4 participants