Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Improve performance of SystemNative_GetNonCryptographicallySecureRandomBytes - #27601

Merged
stephentoub merged 5 commits into
dotnet:masterfrom
janvorli:improve-random-perf
Mar 5, 2018
Merged

stephentoub merged 5 commits into
dotnet:masterfrom
janvorli:improve-random-perf

Conversation

@janvorli

@janvorli janvorli commented Mar 1, 2018

Copy link
Copy Markdown
Member

Based on the same change in coreclr. Improves the performance 6 times.
Main improvement comes from using just one read per the whole buffer
instead of one per byte. Smaller part of the win comes from using only
the /dev/urandom.

@janvorli janvorli added this to the 2.1.0 milestone Mar 1, 2018
@janvorli janvorli self-assigned this Mar 1, 2018
@janvorli
janvorli requested review from jkotas and stephentoub March 1, 2018 10:54
…omBytes

Based on the same change in coreclr. Improves the performance 6 times.
Main improvement comes from using just one read per the whole buffer
instead of one per byte. Smaller part of the win comes from using only
the /dev/urandom.

int rand_des;
char buf;
uint8_t* buf = (uint8_t*)alloca(bufferLength);

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.

Why do we need this? This seems problematic: if you pass in a large buffer, you could stack overflow. Why can't we just read into the original buffer and then update it in place with the xor'ing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was the semantics of the original code which I haven't changed and in coreclr, it was like that even before it was open sourced. But I agree there is not much benefit in doing it and removing it would have the nice consequence of not having to allocate the buffer.
I was thinking about the potential of stack overflow before too, but since this function is ever used only for tiny buffers in our code base and it is internal, it seemed ok.
I'll change it based on your suggestion.

}

*(buffer + i) ^= num;
*(buffer + i) ^= buf[i] ^ num;

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.

Is this xor'ing really necessary?

@janvorli

janvorli commented Mar 1, 2018

Copy link
Copy Markdown
Member Author

Actually, I've just got another idea - keeping the FD for the /dev/urandom open all the time. This further improves the performance twice.

// in case /dev/urandom is not really random

for(i = 0; i < bufferLength; i++)
for (int i = 0; i < bufferLength; i++)

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.

I'm still not clear why this xor'ing is necessary, why the data we get back from urandom is insufficient.

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.

This was done back in the Rotor/Silveright days. At that time, we have seen cases where the random device was just returning non-random data on some systems under certain circumstances due to OS bugs. This was mitigating them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@stephentoub, @jkotas what do you think about removing it then?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I would need to add fail fast if the read failed for some obscure reason in that case.

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.

If the function is changed to return error and all places are changed to do something about the error, I would be ok with removing it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll keep it in. The perf difference between having and not having it is just 2%

By keeping the file descriptor for /dev/urandom open permanently,
another 2x perf improvement was achieved. The total perf improvement
measured on my physical machine is now 13.75x.
@janvorli
janvorli force-pushed the improve-random-perf branch from 9388708 to 78203ca Compare March 1, 2018 12:55
if (rand_des == -1)
{
if (errno == ENOENT)
int fd = open("/dev/urandom", O_RDONLY, O_CLOEXEC);

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.

Is it thread safe to read concurrently from this now shared fd?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

See the "Bugs" section in http://man7.org/linux/man-pages/man2/read.2.html. It says that from Linux >= 3.14, it is thread safe.
On Linux < 3.14, the doc says that multiple threads can possibly obtain blocks that overlap.

But looking at the kernel sources, the random data extraction has a spinlock around it, so it should be safe even on the older Linux kernels.

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.

What about on macOS / BSD?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As for the read functions, according to their man pages, they should both conform to POSIX 1 standard, like on Linux. That means that the operation should thread safe there too. However, POSIX talks only about regular files, not pseudo files like /dev/urandom.

Looking at FreeBSD sources, there is a lock around reading from the random generator in the /dev/urandom handler.

And the same is true for macOS source code (checked in the 10.12 version).

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.

Ok. Thanks.

while (offset != bufferLength);

close(rand_des);
assert(offset == bufferLength);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit, this is asserting the line above, does not seem worth it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The loop has a break in it, so in case of an error in the read which should not happen, the assert would fire.

@stephentoub stephentoub Mar 2, 2018 •

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.

Should we just move the assert (with a different condition) to where the break is instead then?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done, it makes more sense.

for( ; i < bufferLength; i++)
{
if (read(rand_des, &buf, 1) < 1)
if (!__sync_bool_compare_and_swap(&rand_des, -1, fd))

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.

How many handles does have a small .NET process opened (e.g. Console.ReadLine)? Just want to understand the relative cost of one more permanently open handle.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There are 21 handles open without my change, so that would be 23 with it (one in corefx, one in coreclr)

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.

Ok, it does not sound that bad.

@janvorli

janvorli commented Mar 1, 2018

Copy link
Copy Markdown
Member Author

@dotnet-bot test OSX x64 Debug Build please

1 similar comment
@janvorli

janvorli commented Mar 1, 2018

Copy link
Copy Markdown
Member Author

@dotnet-bot test OSX x64 Debug Build please

@janvorli

janvorli commented Mar 2, 2018

Copy link
Copy Markdown
Member Author

@dotnet-bot test OSX x64 Debug Build please

@stephentoub
stephentoub merged commit 52b6280 into dotnet:master Mar 5, 2018
@stephentoub

Copy link
Copy Markdown
Member

Thanks, Jan.

@janvorli
janvorli deleted the improve-random-perf branch March 5, 2018 10:08
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…omBytes (dotnet/corefx#27601)

* Improve performance of SystemNative_GetNonCryptographicallySecureRandomBytes

Based on the same change in coreclr. Improves the performance 6 times.
Main improvement comes from using just one read per the whole buffer
instead of one per byte. Smaller part of the win comes from using only
the /dev/urandom.

* Remove alloca, generate right to the buffer

* Further 2x perf improvement

By keeping the file descriptor for /dev/urandom open permanently,
another 2x perf improvement was achieved. The total perf improvement
measured on my physical machine is now 13.75x.

* Handle EINTR for the open right away

* Move assert to the read error location


Commit migrated from dotnet/corefx@52b6280
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.

5 participants