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

[PATCH v5] linux-gen: random: make random data independent of destination buffer alignment #2107

Merged

Conversation

JereLeppanen
Copy link
Collaborator

@JereLeppanen JereLeppanen commented Aug 27, 2024

No description provided.

@odpbuild odpbuild changed the title linux-gen: random: make random data independent of destination buffer alignment [PATCH v1] linux-gen: random: make random data independent of destination buffer alignment Aug 27, 2024
@odpbuild odpbuild changed the title [PATCH v1] linux-gen: random: make random data independent of destination buffer alignment [PATCH v2] linux-gen: random: make random data independent of destination buffer alignment Aug 27, 2024
@odpbuild odpbuild changed the title [PATCH v2] linux-gen: random: make random data independent of destination buffer alignment [PATCH v3] linux-gen: random: make random data independent of destination buffer alignment Aug 28, 2024
@@ -40,38 +41,35 @@ static int32_t _random_data(uint8_t *buf, uint32_t len, uint64_t *seed)
{
const uint32_t ret = len;

if (!_ODP_UNALIGNED) {
uint32_t r = xorshift64s32(seed);
if (!_ODP_UNALIGNED && (uintptr_t)buf & 3) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if it is worth the effort and code complecity to keep optimizing the !_ODP_UNALIGNED case here. All relevant environments seem to have _ODP_UNALIGNED set 1. In my test systems I get _ODP_UNALIGNED set 0 only if compiling for 32-bit Arm with -mno-unaligned-access.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed in v4.

r >>= 8;
buf += 1;
len -= 1;
memcpy(buf, &r, 4);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This memcpy does not appear to get optimized away when compiling with GCC 12 for armv6+fp with -mno-unaligned-access. So if we do want to optimize for the _ODP_UNALIGNED == 0 case, the memcpy should perhaps be replaced by direct assignments to buf.

}

for (uint32_t i = 0; i < len / 4; i++) {
*(odp_una_u32_t *)buf = xorshift64s32(seed);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be memcpy (at least if we do not care too much about _ODP_UNALIGNED 0) to avoid alignment and aliasing problems and the need to use weird type attributes. The memcpy would get optimized away when compiling for x86_64 or A64. And same comments for the other occurences of this sort of assignments.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not in favor of replacing every unaligned integer assignment with memcpy. But in any case that's outside the scope of this PR, so I left this as-is.

@@ -149,6 +149,26 @@ static void random_test_align_and_overflow_true(void)
random_test_align_and_overflow(ODP_RANDOM_TRUE);
}

static void random_test_align_and_len_test(void)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks good. I just wonder if it would be worthwhile to add checks that the function is not writing more than len bytes in the buffer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That and the destination buffer alignment are already tested in random_test_align_and_overflow. So if random test data is allowed to differ for different len values, even if seed is the same, then there's no need for this new test. Removed in v4.

@odpbuild odpbuild changed the title [PATCH v3] linux-gen: random: make random data independent of destination buffer alignment [PATCH v4] linux-gen: random: make random data independent of destination buffer alignment Sep 3, 2024
@JereLeppanen
Copy link
Collaborator Author

v4:

  • Janne's comments.
  • Rebase.
  • Add review tags.

… alignment

Make random test data independent of destination buffer alignment when
efficient unaligned access is not available.

Signed-off-by: Jere Leppänen <jere.leppanen@nokia.com>
Reviewed-by: Janne Peltonen <janne.peltonen@nokia.com>
@odpbuild odpbuild changed the title [PATCH v4] linux-gen: random: make random data independent of destination buffer alignment [PATCH v5] linux-gen: random: make random data independent of destination buffer alignment Sep 4, 2024
@MatiasElo MatiasElo merged commit 35d96f0 into OpenDataPlane:master Sep 5, 2024
170 checks passed
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