Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't block in getrandom() detection #26

Merged
merged 1 commit into from
Jun 10, 2019

Conversation

josephlr
Copy link
Member

On Linux, we detect if getrandom is present by calling getrandom with
an empty buffer and seeing if ENOSYS is returned. However, even with
an empty buffer, this call will block unless we explicitly pass a flag.

This can be seen in the source for getrandom:
https://elixir.bootlin.com/linux/v5.1.8/source/drivers/char/random.c#L2043

This change adds a boolean parameter to syscall_getrandom to control
the blocking behavior. We now don't block in initialization, but do
block when actually reading random data.

On Linux, we detect if `getrandom` is present by calling getrandom with
an empty buffer and seeing if `ENOSYS` is returned. However, even with
an empty buffer, this call will block unless we explicitly pass a flag.

This can be seen in the source for `getrandom`:
  https://elixir.bootlin.com/linux/v5.1.8/source/drivers/char/random.c#L2043

This change adds a boolean parameter to `syscall_getrandom` to control
the blocking behavior. We now don't block in initialization, but do
block when actually reading random data.
Copy link
Member

@dhardy dhardy left a comment

Choose a reason for hiding this comment

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

I think in practice it doesn't make much difference since detection happens on first use. Looks good to me however.

@newpavlov
Copy link
Member

I also think that blocking is not a big deal. If kernel does not have getrandom syscall we immediately get ENOSYS, and if call blocks, the next getrandom call will block either way. But I guess with this PR we will get a slightly better behavior in multi-threaded programs on a system launch, i.e. instead of blocking on user-land synchronization, threads will be blocked on syscall.

@newpavlov newpavlov merged commit db27e97 into rust-random:master Jun 10, 2019
@josephlr josephlr deleted the getrandom branch June 10, 2019 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants