Skip to content

libhns, util: Fix the aarch64 mmio_memcpy_x64() length check - #1851

Merged
rleon merged 2 commits into
linux-rdma:masterfrom
0z5a:fix/hns-dwqe-mmio-memcpy
Nov 9, 2026
Merged

rleon merged 2 commits into
linux-rdma:masterfrom
0z5a:fix/hns-dwqe-mmio-memcpy

Conversation

@0z5a

@0z5a 0z5a commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

The aarch64 mmio_memcpy_x64() macro writes only 64 bytes for any length the compiler can reason about, because __builtin_constant_p((bytecount) == 64) tests whether the comparison's result is known. hns_roce_write512() passes 32 and relies on this to write its 64-byte direct WQE, so the libhns caller is fixed first and the macro second. Each commit is safe on its own.

Reported to the linux-rdma mailing list on 2026-11-07; Message-ID: <20261107231102.Horde.VTLAyGut_WGch71PWzwoxG_@webmail.uni-tuebingen.de>. Junxian Huang replied on the list that the proposed fix looks good and offered to test it on hns hardware.

Validation on aarch64 (Neoverse V2, gcc 13.3), against a70151dfe:

  • The full build has no new warnings.
  • The libhns commit does not change .text in any library.
  • After both commits, .text is unchanged in libhns, libmlx4 and libmlx5, also for those translation units at -O0 to -Os.
  • libefa changes in register allocation and block layout. libionic also gains one compare and branch in ionic_post_cmb_common(). The copy loops are still there in both.
  • A user-space copy of the corrected macro copies the requested length for literals of 64 and 128 and for lengths known only to differ from 64.

On x86_64 (gcc 13.3, -DENABLE_WERROR=1) all three trees build without warnings. The libhns commit changes .text only in libhns, and the macro commit changes no library. Before the libhns commit, the generic mmio_memcpy_x64() compiles hns_roce_write_dwqe() into a loop with no exit, because 32 minus 64 in a size_t never reaches 0. After it, the function is eight 8-byte stores.

No hns hardware was available, so this has not run against a device. buildlib/cbuild was not run, and clang was only used on macOS with stub headers.

I used Claude, an AI coding assistant, to find this, write the tests and draft the commits; both commits carry an Assisted-by tag.

Copilot AI balanced review requested due to automatic review settings November 9, 2026 09:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@0z5a
0z5a force-pushed the fix/hns-dwqe-mmio-memcpy branch from 9fdec78 to 65c07fb Compare November 9, 2026 09:22
Comment thread providers/hns/hns_roce_u_hw_v2.c Outdated
static void hns_roce_write512(uint64_t *dest, uint64_t *val)
{
mmio_memcpy_x64(dest, val, sizeof(struct hns_roce_rc_sq_wqe));
mmio_memcpy_x64(dest, val, 64);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would prefer 1 << HNS_ROCE_SQWQE_SHIFT

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, and added your Reviewed-by and Tested-by to both patches. Thanks for testing!

@hginjgerx

Copy link
Copy Markdown

I tested this PR on an hns hardware and it works well.
Apart from the minor comment above, this PR LGTM.

Feel free to add:
Reviewed-by: Junxian Huang [email protected]
Tested-by: Junxian Huang [email protected]

0z5a added 2 commits November 9, 2026 13:00
hns_roce_write512() passes sizeof(struct hns_roce_rc_sq_wqe), which is
32, to mmio_memcpy_x64(). It writes a 64-byte direct WQE only because
the aarch64 macro takes its 64-byte path for any constant length. On
the generic implementation a length of 32 makes the size_t counter
wrap, and the copy does not stop.

Pass the size of a direct WQE, 1 << HNS_ROCE_SQWQE_SHIFT. On aarch64
the generated code does not change.

Fixes: 159933c ("libhns: Add support for direct wqe")
Reviewed-by: Junxian Huang <[email protected]>
Tested-by: Junxian Huang <[email protected]>
Assisted-by: Claude:claude-opus-5-5 <[email protected]>
Signed-off-by: Dezhen Lu <[email protected]>
__builtin_constant_p((bytecount) == 64) is true when the comparison has
a known result, whatever that result is, so the macro writes only 64
bytes for any length the compiler can reason about. A literal 128 is
truncated, and so is a variable after "if (n <= 64) return;".

Take the 64-byte path only when the length is known to be 64. libhns
was the one in-tree caller that relied on the old behaviour, and the
previous patch removes that.

Fixes: 159933c ("libhns: Add support for direct wqe")
Reviewed-by: Junxian Huang <[email protected]>
Tested-by: Junxian Huang <[email protected]>
Assisted-by: Claude:claude-opus-5-5 <[email protected]>
Signed-off-by: Dezhen Lu <[email protected]>
@0z5a
0z5a force-pushed the fix/hns-dwqe-mmio-memcpy branch from 65c07fb to b9c0359 Compare November 9, 2026 12:00
Copilot AI balanced review requested due to automatic review settings November 9, 2026 12:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@rleon
rleon merged commit 3e79f02 into linux-rdma:master Nov 9, 2026
14 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.

4 participants