Skip to content

firmware: fix race conditions in control out transfers (-3 bytes XRAM).#703

Merged
whitequark merged 1 commit intoGlasgowEmbedded:mainfrom
purdeaandrei:f_fix_control_out_transfer_race_conditions
Oct 8, 2025
Merged

firmware: fix race conditions in control out transfers (-3 bytes XRAM).#703
whitequark merged 1 commit intoGlasgowEmbedded:mainfrom
purdeaandrei:f_fix_control_out_transfer_race_conditions

Conversation

@purdeaandrei
Copy link
Copy Markdown
Contributor

This PR depends on GlasgowEmbedded/libfx2#18
This PR is ready for review but it should only be merged after the libfx2 PR is merged,
and then this PR should also include an update to the submodule reference.

Before this change, the EP0BUF buffer used by control out transfers could be overwritten by new control transfers before the firmware is finished processing the previous control transfer.

The easiest way to illustrate this problem would be to run in a different terminal the following:

while true; do lsusb -v -d 20b7:9db1 > /dev/null; done

While this is running, glasglow is completely unusable. Presumably even a single lsusb run could cause corruption, if it happens to be issued at the wrong time.

With this change glasgow is now usable, even if the above loop is running.

@purdeaandrei purdeaandrei force-pushed the f_fix_control_out_transfer_race_conditions branch from a628e16 to b1e7539 Compare September 27, 2024 04:26
@purdeaandrei purdeaandrei changed the title firmware: fix race conditions in control out transfers (+0 bytes XRAM). firmware: fix race conditions in control out transfers (-2 bytes XRAM). Sep 27, 2024
@whitequark whitequark added the waiting-on-review Status: Waiting for review label Jul 6, 2025
@purdeaandrei purdeaandrei force-pushed the f_fix_control_out_transfer_race_conditions branch from b1e7539 to cf672e2 Compare October 6, 2025 03:46
@whitequark
Copy link
Copy Markdown
Member

I think you might need to update the submodule.

@purdeaandrei
Copy link
Copy Markdown
Contributor Author

I think you might need to update the submodule.

Yes, but PR18 in libfx2 isn't merged yet, only approved. I'll do that as soon as it's merged.

@purdeaandrei purdeaandrei force-pushed the f_fix_control_out_transfer_race_conditions branch from cf672e2 to 8591432 Compare October 8, 2025 08:33
@purdeaandrei purdeaandrei changed the title firmware: fix race conditions in control out transfers (-2 bytes XRAM). firmware: fix race conditions in control out transfers (-3 bytes XRAM). Oct 8, 2025
@purdeaandrei purdeaandrei force-pushed the f_fix_control_out_transfer_race_conditions branch from 8591432 to 1091d3d Compare October 8, 2025 08:39
@purdeaandrei
Copy link
Copy Markdown
Contributor Author

Alright,

  • I have updated the submodule
  • I re-measured the XRAM change, it's now -3 bytes
  • I have deployed the firmware in a separate commit

Should be ready to merge.

@whitequark
Copy link
Copy Markdown
Member

Thanks!

@whitequark whitequark added this pull request to the merge queue Oct 8, 2025
@whitequark
Copy link
Copy Markdown
Member

(I think deploying the firmware may have to happen in a separate PR.)

@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 8, 2025
@purdeaandrei purdeaandrei force-pushed the f_fix_control_out_transfer_race_conditions branch from 1091d3d to be634a5 Compare October 8, 2025 17:07
@purdeaandrei
Copy link
Copy Markdown
Contributor Author

Okay, re-pushed without the deploy

Before this change, the EP0BUF buffer used by control out transfers
could be overwritten by new control transfers before the firmware is
finished processing the previous control transfer.

The easiest way to illustrate this problem would be to run in a
different terminal the following:

```bash
while true; do lsusb -v -d 20b7:9db1 > /dev/null; done
```

While this is running, glasglow is completely unusable.
Presumably even a single lsusb run could cause corruption, if it
happens to be issued at the wrong time.

With this change glasgow is now usable, even if the above loop is
running.

Please see GlasgowEmbedded/libfx2#18 for more
details.
@whitequark whitequark added this pull request to the merge queue Oct 8, 2025
@purdeaandrei purdeaandrei force-pushed the f_fix_control_out_transfer_race_conditions branch from be634a5 to 7bbbd25 Compare October 8, 2025 17:11
@purdeaandrei
Copy link
Copy Markdown
Contributor Author

Aand pushed again, rebased

@whitequark
Copy link
Copy Markdown
Member

(You have write access now, so I think it automatically replaces your PR in the queue if you push to it, or something like that?)

Merged via the queue into GlasgowEmbedded:main with commit d935ee7 Oct 8, 2025
1 check failed
@purdeaandrei
Copy link
Copy Markdown
Contributor Author

(You have write access now, so I think it automatically replaces your PR in the queue if you push to it, or something like that?)

Not sure, I definitely didn't add it to the queue

@whitequark
Copy link
Copy Markdown
Member

I did. But if you don't have write access, then it kicks you out of the queue after a (force-/normal-)push.

@purdeaandrei purdeaandrei deleted the f_fix_control_out_transfer_race_conditions branch October 8, 2025 17:21
purdeaandrei added a commit to purdeaandrei/glasgow that referenced this pull request Nov 1, 2025
…tes XRAM)

These were missed by PR GlasgowEmbedded#703, because they used `SETUP_EP0_BUF(2)`
with a non-zero argument for Control OUT transfers. Even though
these didn't follow libfx2's (old) rule, of setting length to 0 when
using Control OUT transfers, they did mostly work, because the FX2
actually ignores the length on OUT transfers.

PR GlasgowEmbedded#703 assumed that all non-zero `SETUP_EP0_BUF` calls were IN
transfer, which was wrong, so these instances remained vulnerable to
the race condition. This change fixes those too.
purdeaandrei added a commit to purdeaandrei/glasgow that referenced this pull request Nov 1, 2025
…tes XRAM).

These were missed by PR GlasgowEmbedded#703, because they used `SETUP_EP0_BUF(2)`
with a non-zero argument for Control OUT transfers. Even though
these didn't follow libfx2's (old) rule, of setting length to 0 when
using Control OUT transfers, they did mostly work, because the FX2
actually ignores the length on OUT transfers.

PR GlasgowEmbedded#703 assumed that all non-zero `SETUP_EP0_BUF` calls were IN
transfer, which was wrong, so these instances remained vulnerable to
the race condition. This change fixes those too.
github-merge-queue Bot pushed a commit that referenced this pull request Nov 2, 2025
These were missed by PR #703, because they used `SETUP_EP0_BUF(2)`
with a non-zero argument for Control OUT transfers. Even though
these didn't follow libfx2's (old) rule, of setting length to 0 when
using Control OUT transfers, they did mostly work, because the FX2
actually ignores the length on OUT transfers.

PR #703 assumed that all non-zero `SETUP_EP0_BUF` calls were IN
transfer, which was wrong, so these instances remained vulnerable to
the race condition. This change fixes those too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-on-review Status: Waiting for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants