GPU: Indirect execution budget can overflow and allow excessive GPU workloads #863

Closed
opened 2026-11-26 04:14:05 +00:00 by github-bot · 4 comments
Owner

@⁠FunWithFlagsDev on GitHub · 2026-11-24 12:54 UTC · original

Bug description

While reviewing the indirect execution implementation in victory-gpu/src/indirect.rs, I identified a potential integer overflow in the GPU workload budget mechanism.

The current implementation uses a 32-bit atomic addition to accumulate workload costs. However, rejected records continue increasing the counter, eventually allowing it to wrap around and reopen the workload budget.

Affected code

https://github.com/Marvisak/VIctory/blob/main/crates/victory-gpu/src/indirect.rs

uint before;
budget.InterlockedAdd(0, cost, before);

if (before >= BUDGET || before + cost > BUDGET) {
    args.Store(at, 0);
}

Reproduction

Using 66 consecutive records, each with a workload cost of 2^26:

  • Configured budget: 134,217,728
  • Accepted workload with the original implementation: 268,435,456
  • Accepted workload with the proposed implementation: 134,217,728

The original implementation can therefore accept twice the intended workload in this synthetic example.

Proposed fix

Replace the unconditional atomic addition with a saturating atomic counter using compare-and-exchange.

uint before;
budget.InterlockedAdd(0, 0u, before);

[allow_uav_condition]
while (true) {
    if (before >= BUDGET) {
        args.Store(at, 0);
        return;
    }

    uint remaining = BUDGET - before;
    uint next = before + min(cost, remaining);

    uint observed;
    budget.InterlockedCompareExchange(
        0, before, next, observed
    );

    if (observed == before) {
        if (cost > remaining) {
            args.Store(at, 0);
        }
        return;
    }

    before = observed;
}

Expected benefits

  • Prevents workload budget wraparound.
  • Preserves the intended upper workload limit.
  • May reduce GPU hangs caused by excessive indirect workloads.
  • Does not require changes to game assets.

Validation

Five CPU-side regression tests passed, including overflow reproduction, boundary conditions, zero-cost operations, and 51,200 randomized operations.

These tests validate the arithmetic model, not actual GPU execution.

HLSL compilation, GPU execution, and contention-related performance still require validation. I have not established whether this overflow occurs during normal gameplay or whether it contributes to the currently reported GPU hangs.

I am attaching the proposed patch and regression tests for review.

Thank you for your work on this project.

test_victory_budget.py
victory_indirect_budget.patch

**@⁠FunWithFlagsDev** on GitHub · 2026-11-24 12:54 UTC · [original](https://github.com/Marvisak/VIctory/issues/863) ## Bug description While reviewing the indirect execution implementation in `victory-gpu/src/indirect.rs`, I identified a potential integer overflow in the GPU workload budget mechanism. The current implementation uses a 32-bit atomic addition to accumulate workload costs. However, rejected records continue increasing the counter, eventually allowing it to wrap around and reopen the workload budget. ## Affected code https://github.com/Marvisak/VIctory/blob/main/crates/victory-gpu/src/indirect.rs ```hlsl uint before; budget.InterlockedAdd(0, cost, before); if (before >= BUDGET || before + cost > BUDGET) { args.Store(at, 0); } ``` ## Reproduction Using 66 consecutive records, each with a workload cost of `2^26`: - Configured budget: 134,217,728 - Accepted workload with the original implementation: 268,435,456 - Accepted workload with the proposed implementation: 134,217,728 The original implementation can therefore accept twice the intended workload in this synthetic example. ## Proposed fix Replace the unconditional atomic addition with a saturating atomic counter using compare-and-exchange. ```hlsl uint before; budget.InterlockedAdd(0, 0u, before); [allow_uav_condition] while (true) { if (before >= BUDGET) { args.Store(at, 0); return; } uint remaining = BUDGET - before; uint next = before + min(cost, remaining); uint observed; budget.InterlockedCompareExchange( 0, before, next, observed ); if (observed == before) { if (cost > remaining) { args.Store(at, 0); } return; } before = observed; } ``` ## Expected benefits - Prevents workload budget wraparound. - Preserves the intended upper workload limit. - May reduce GPU hangs caused by excessive indirect workloads. - Does not require changes to game assets. ## Validation Five CPU-side regression tests passed, including overflow reproduction, boundary conditions, zero-cost operations, and 51,200 randomized operations. These tests validate the arithmetic model, not actual GPU execution. HLSL compilation, GPU execution, and contention-related performance still require validation. I have not established whether this overflow occurs during normal gameplay or whether it contributes to the currently reported GPU hangs. I am attaching the proposed patch and regression tests for review. Thank you for your work on this project. [test_victory_budget.py](https://github.com/user-attachments/files/35331803/test_victory_budget.py) [victory_indirect_budget.patch](https://github.com/user-attachments/files/35331802/victory_indirect_budget.patch)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:05 +00:00
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 13:01 UTC · original · edited 2026-11-24 13:02 UTC

Melissa checked the open HLSL compilation question using Microsoft's D3DCompiler_47 with the cs_5_1 target. The attached patch hit an internal compiler error on our Wine setup and the normal argument-clamp pipeline creation test failed. Keeping your saturating compare-and-exchange logic and giving rejected records one shared exit allowed the same compiler to build the shader. The host created the pipeline on RTX 4090 under Wine/vkd3d-proton 3.2. Tested change on ffe310e

On the separate wave-per-group branch, the port also passed all three existing indirect tests, including ordinary scaled-dispatch output and preservation of the guest argument records when the host declines scaled work. No overflow workload was submitted. These checks establish compiler compatibility and ordinary dispatch behavior; contention costs and any connection to in-game hangs remain unmeasured.

Melissa also ported the saturation fix to #190’s compiler-free DXIL emitter. Microsoft DXV accepted the emitted shader, the production pipeline was created on the RTX 4090, and both clamp readback tests passed under Wine/vkd3d-proton 3.2. With eight concurrent one-vertex records and room for three, exactly three were admitted and the counter finished at the budget limit. The test ran the clamp and read back its records without executing the resulting draws. Compiler-free implementation and test

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 13:01 UTC · [original](https://github.com/Marvisak/VIctory/issues/863#issuecomment-7132240109) · edited 2026-11-24 13:02 UTC Melissa checked the open HLSL compilation question using Microsoft's D3DCompiler_47 with the cs_5_1 target. The attached patch hit an internal compiler error on our Wine setup and the normal argument-clamp pipeline creation test failed. Keeping your saturating compare-and-exchange logic and giving rejected records one shared exit allowed the same compiler to build the shader. The host created the pipeline on RTX 4090 under Wine/vkd3d-proton 3.2. [Tested change on ffe310e](https://github.com/MichaelSmithAI/VIctory/commit/27f7e570d15216f59c5027bdd4ee5e40a237d3bc) On the separate wave-per-group branch, the port also passed all three existing indirect tests, including ordinary scaled-dispatch output and preservation of the guest argument records when the host declines scaled work. No overflow workload was submitted. These checks establish compiler compatibility and ordinary dispatch behavior; contention costs and any connection to in-game hangs remain unmeasured. Melissa also ported the saturation fix to #190’s compiler-free DXIL emitter. Microsoft DXV accepted the emitted shader, the production pipeline was created on the RTX 4090, and both clamp readback tests passed under Wine/vkd3d-proton 3.2. With eight concurrent one-vertex records and room for three, exactly three were admitted and the counter finished at the budget limit. The test ran the clamp and read back its records without executing the resulting draws. [Compiler-free implementation and test](https://github.com/MichaelSmithAI/VIctory/commit/e467697d89eac9dd045cc162bf004af24df12e5a)
Author
Owner

@⁠FunWithFlagsDev on GitHub · 2026-11-24 13:03 UTC · original

Thank you, Michael and Melissa, for testing the proposed fix and resolving the HLSL compiler issue.

The shared rejection exit is a useful improvement. It preserves the saturating compare-and-exchange logic while addressing the compiler compatibility problem.

I reviewed commit 27f7e570 and the revised control flow appears consistent with the intended budget invariant.

A useful next step would be a GPU-side regression test using synthetic indirect argument records that would overflow the original 32-bit counter. The test could execute only the clamp shader and read back the resulting counter and argument records, without submitting the excessive indirect workloads themselves.

This would allow us to verify the overflow protection independently of game assets and without executing the potentially excessive workloads.

I agree that contention costs and any relationship to in-game GPU hangs remain open questions.

Thanks again for extending the validation!

**@⁠FunWithFlagsDev** on GitHub · 2026-11-24 13:03 UTC · [original](https://github.com/Marvisak/VIctory/issues/863#issuecomment-7132298257) Thank you, Michael and Melissa, for testing the proposed fix and resolving the HLSL compiler issue. The shared rejection exit is a useful improvement. It preserves the saturating compare-and-exchange logic while addressing the compiler compatibility problem. I reviewed commit `27f7e570` and the revised control flow appears consistent with the intended budget invariant. A useful next step would be a GPU-side regression test using synthetic indirect argument records that would overflow the original 32-bit counter. The test could execute only the clamp shader and read back the resulting counter and argument records, without submitting the excessive indirect workloads themselves. This would allow us to verify the overflow protection independently of game assets and without executing the potentially excessive workloads. I agree that contention costs and any relationship to in-game GPU hangs remain open questions. Thanks again for extending the validation!
Author
Owner

@⁠Sunkiller-qc on GitHub · 2026-11-24 13:09 UTC · original

This is already covered in two places:

  • #583 (open) says it "stops the uint32 budget accumulator from wrapping". It also makes the clamp read the count buffer, so garbage records past the count don't use up the budget.
  • On #597, rouges78 found the same wrap and reproduced it on hardware (64 records of 2^26 bring the word back to zero). acleofe confirmed it on ffe310e.

It would help to compare your saturating CAS with #583's change there, rather than carry a third version.

**@⁠Sunkiller-qc** on GitHub · 2026-11-24 13:09 UTC · [original](https://github.com/Marvisak/VIctory/issues/863#issuecomment-7132421400) This is already covered in two places: - #583 (open) says it "stops the uint32 budget accumulator from wrapping". It also makes the clamp read the count buffer, so garbage records past the count don't use up the budget. - On #597, rouges78 found the same wrap and reproduced it on hardware (64 records of 2^26 bring the word back to zero). acleofe confirmed it on `ffe310e`. It would help to compare your saturating CAS with #583's change there, rather than carry a third version.
Author
Owner

@⁠FunWithFlagsDev on GitHub · 2026-11-24 13:11 UTC · original

Thank you for pointing out the existing work. I missed #583 and the earlier hardware reproduction in #597 when checking for duplicates.

After reviewing #583 and #242, I found that:

  • #583 rejects oversized reservations without consuming the remaining budget, allowing subsequent smaller records to fit.
  • My proposed CAS implementation saturates the counter when a reservation exceeds the remaining budget.
  • #242 already implements a very similar saturation strategy, while also respecting the count buffer and preserving the original argument buffer.

More importantly, rouges78's hardware results in #583 demonstrate severe contention in the CAS reservation loop when many small records fit within the budget. My proposal shares that potential limitation, and the available tests do not establish its scalability.

I agree that maintaining another independent implementation is unnecessary. Consolidating the existing work and addressing the contention issue would be more valuable.

Thank you for the references and technical feedback.

**@⁠FunWithFlagsDev** on GitHub · 2026-11-24 13:11 UTC · [original](https://github.com/Marvisak/VIctory/issues/863#issuecomment-7132475743) Thank you for pointing out the existing work. I missed #583 and the earlier hardware reproduction in #597 when checking for duplicates. After reviewing #583 and #242, I found that: - #583 rejects oversized reservations without consuming the remaining budget, allowing subsequent smaller records to fit. - My proposed CAS implementation saturates the counter when a reservation exceeds the remaining budget. - #242 already implements a very similar saturation strategy, while also respecting the count buffer and preserving the original argument buffer. More importantly, rouges78's hardware results in #583 demonstrate severe contention in the CAS reservation loop when many small records fit within the budget. My proposal shares that potential limitation, and the available tests do not establish its scalability. I agree that maintaining another independent implementation is unnecessary. Consolidating the existing work and addressing the contention issue would be more valuable. Thank you for the references and technical feedback.
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#863
No description provided.