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

8294087: RISC-V: RVC: Fix a potential alignment issue and add more alignment assertions for the patchable calls/nops #10370

Conversation

zhengxiaolinX
Copy link
Contributor

@zhengxiaolinX zhengxiaolinX commented Sep 21, 2022

With RVC turning on, we must carefully keep all runtime-patchable instructions aligned. Code is running at full speed, when patching unaligned instructions spanning cache lines, concurrency issues may occur. This patch fixes a potential alignment issue of the patchable nop after MachUEPNode, with adding some strong alignment assertions as well. (In fact, currently the nop in the Verified Entry Point is fortunately aligned to 4 under RVC even without this patch, so this patch doesn't change program behaviors.)

Tested hotspot tier1 and tier2 together with other patches on QEMU.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8294087: RISC-V: RVC: Fix a potential alignment issue and add more alignment assertions for the patchable calls/nops

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/10370/head:pull/10370
$ git checkout pull/10370

Update a local copy of the PR:
$ git checkout pull/10370
$ git pull https://git.openjdk.org/jdk pull/10370/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 10370

View PR using the GUI difftool:
$ git pr show -t 10370

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/10370.diff

@zhengxiaolinX zhengxiaolinX marked this pull request as ready for review September 21, 2022 03:54
@bridgekeeper
Copy link

bridgekeeper bot commented Sep 21, 2022

👋 Welcome back xlinzheng! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Sep 21, 2022
@openjdk
Copy link

openjdk bot commented Sep 21, 2022

@zhengxiaolinX The following label will be automatically applied to this pull request:

  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot hotspot-dev@openjdk.org label Sep 21, 2022
@mlbridge
Copy link

mlbridge bot commented Sep 21, 2022

@RealFYang
Copy link
Member

Can you add in the code alignment assertions introduced by your new RVC proposal [1]?

[1] zhengxiaolinX@2687452

@@ -91,6 +91,14 @@ int MacroAssembler::align(int modulus, int extra_offset) {
return (int)(offset() - before);
}

void MacroAssembler::assert_alignment() {
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit weird that assert_alignment only checks the alignment of instruction size. Can you extend this method with a parameter like align_size to check any alignment? (instruction size can be the default size of the check)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, that is reasonable and I have modified it; but due to the introduction of NativeInstruction::instruction_size in the macroassembler_riscv.hpp, it seems we also need to introduce nativeinst_riscv.hpp in it. I was wondering is this okay, or I can explicitly add NativeInstruction::instruction_size arguments to the call sites.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated a revertible patch to see which code style is better: adding NativeInstruction::instruction_size to the default argument would introduce nativeInst_riscv.hpp to one hpp file; adding it to every call site would introduce the nativeInst_riscv.hpp to two cpp files.

@zhengxiaolinX zhengxiaolinX changed the title 8294087: RISC-V: Refactor instruction alignment assertions 8294087: RISC-V: Fix a potential alignment issue and add more alignment assertions for the patchable calls/nops Sep 21, 2022
@zhengxiaolinX zhengxiaolinX changed the title 8294087: RISC-V: Fix a potential alignment issue and add more alignment assertions for the patchable calls/nops 8294087: RISC-V: RVC: Fix a potential alignment issue and add more alignment assertions for the patchable calls/nops Sep 21, 2022
@zhengxiaolinX
Copy link
Contributor Author

Can you add in the code alignment assertions introduced by your new RVC proposal [1]?

[1] zhengxiaolinX@2687452

Of course, I have added that and modified the title and descriptions of this PR.

Copy link
Member

@shipilev shipilev 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 static void assert_alignment(address pc, int alignment = NativeInstruction::instruction_size); is cleaner, even though it requires a new include in macroAssembler_riscv.hpp.

@@ -2839,6 +2847,7 @@ address MacroAssembler::trampoline_call(Address entry) {
}

address call_pc = pc();
assert(entry.rspec().type() == relocInfo::runtime_call_type || is_aligned(call_pc, 4), "bad alignment for patchable calls");
Copy link
Member

Choose a reason for hiding this comment

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

"4" is still here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, modified.

@zhengxiaolinX
Copy link
Contributor Author

I think static void assert_alignment(address pc, int alignment = NativeInstruction::instruction_size); is cleaner, even though it requires a new include in macroAssembler_riscv.hpp.

Thanks for the determination. I have reverted the revertible one.

Comment on lines 273 to 274
// Must be 4 byte aligned
MacroAssembler::assert_alignment(verified_entry);
Copy link
Member

Choose a reason for hiding this comment

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

So, if the intent to pass explicit NativeInsn::insn_size everywhere we say "Must be 4 bytes aligned", then we should probably pass it here too. Or, drop the explicit NativeInsn::insn_size everywhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, if I understand correctly, as we want to make it clean, we won't explicitly pass NativeInstruction::instruction_size as an argument of __ assert_alignment(). And it seems currently all the call sites of __ assert_alignment() have already dropped the explicit argument, including this position. So... haven't we already succeeded in dropping them?

The __ align() API needs an explicit argument as alignment, so when calling that one, the NativeInstruction::instruction_size is passed explicitly.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I misread the patch, and confused align and assert_alignment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I misread the patch, and confused align and assert_alignment.

That's nothing. Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

I think this comment should be: "// Must be 4 bytes aligned"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this comment should be: "// Must be 4 bytes aligned"

Oh thanks for catching that. With hindsight, I found Aleksey had already hinted at it by indicating ...everywhere we say "Must be 4 bytes aligned"..., but I failed to catch that :-) Thank you both!

Comment on lines 273 to 274
// Must be 4 byte aligned
MacroAssembler::assert_alignment(verified_entry);
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I misread the patch, and confused align and assert_alignment.

@openjdk
Copy link

openjdk bot commented Sep 21, 2022

@zhengxiaolinX This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8294087: RISC-V: RVC: Fix a potential alignment issue and add more alignment assertions for the patchable calls/nops

Reviewed-by: shade, fjiang, fyang

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 40 new commits pushed to the master branch:

  • 800e68d: 8292044: HttpClient doesn't handle 102 or 103 properly
  • 83abfa5: 8255670: Improve C2's detection of modified nodes
  • 5652030: 8292376: A few Swing methods use inheritDoc on exceptions which are not inherited
  • 03f287d: 8293995: Problem list sun/tools/jstatd/TestJstatdRmiPort.java on all platforms because of 8293577
  • d5bee4a: 8294086: RISC-V: Cleanup InstructionMark usages in the backend
  • 47f233a: 8292202: modules_do is called without Module_lock
  • 742bc04: 8294100: RISC-V: Move rt_call and xxx_move from SharedRuntime to MacroAssembler
  • 2283c32: 8294149: JMH 1.34 and later requires jopt-simple 5.0.4
  • 9f90eb0: 8294062: Improve parsing performance of j.l.c.MethodTypeDesc
  • c6be2cd: 8293156: Dcmd VM.classloaders fails to print the full hierarchy
  • ... and 30 more: https://git.openjdk.org/jdk/compare/84ee1a291cb38e1500dc1529c2aa79e7a195502c...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@shipilev, @RealFYang) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 21, 2022
void MacroAssembler::assert_alignment(address pc, int alignment) {
assert(is_aligned(pc, alignment), "bad alignment");
}

Copy link
Member

Choose a reason for hiding this comment

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

Would you mind a few more tweaks?
I don't think we need two versions here. Maybe we can only keep the second one and remove the first one here?
Then the call sites will look more consistent passing an explict 'pc' argument.
Also we should move the function definition to macroAssembler_riscv.hpp.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course, revised and add static inline void for it in the hpp file.

@@ -1372,7 +1372,7 @@ void LIR_Assembler::ic_call(LIR_OpJavaCall* op) {

void LIR_Assembler::emit_static_call_stub() {
address call_pc = __ pc();
assert((__ offset() % 4) == 0, "bad alignment");
__ assert_alignment(call_pc);
Copy link
Member

Choose a reason for hiding this comment

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

As assert_alignment is defined as a static method, there is no need for __. MacroAssembler::assert_alignment(call_pc) would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reasonable; all done.

@@ -262,7 +262,7 @@ void BarrierSetAssembler::nmethod_entry_barrier(MacroAssembler* masm, Label* slo

__ bind(local_guard);

assert(__ offset() % 4 == 0, "bad alignment");
__ assert_alignment(__ pc());
Copy link
Member

Choose a reason for hiding this comment

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

ditto

@@ -1318,6 +1318,7 @@ void MachPrologNode::emit(CodeBuffer &cbuf, PhaseRegAlloc *ra_) const {

// insert a nop at the start of the prolog so we can patch in a
// branch if we need to invalidate the method later
__ assert_alignment(__ pc());
Copy link
Member

Choose a reason for hiding this comment

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

ditto

@@ -1140,6 +1140,7 @@ nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm,
int vep_offset = ((intptr_t)__ pc()) - start;

// First instruction must be a nop as it may need to be patched on deoptimisation
__ assert_alignment(__ pc());
Copy link
Member

Choose a reason for hiding this comment

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

ditto

@@ -1291,6 +1292,7 @@ nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm,

// If we have to make this method not-entrant we'll overwrite its
// first instruction with a jump.
__ assert_alignment(__ pc());
Copy link
Member

Choose a reason for hiding this comment

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

ditto

@zhengxiaolinX
Copy link
Contributor Author

zhengxiaolinX commented Sep 22, 2022

Hotspot tier1 on QEMU with fastdebug build and RVC turning on has no new errors again after the changes.

Thank you all for taking the time to review this!

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Sep 22, 2022
@openjdk
Copy link

openjdk bot commented Sep 22, 2022

@zhengxiaolinX
Your change (at version 83bdcd2) is now ready to be sponsored by a Committer.

@RealFYang
Copy link
Member

/sponsor

@openjdk
Copy link

openjdk bot commented Sep 22, 2022

Going to push as commit a216960.
Since your change was applied there have been 41 commits pushed to the master branch:

  • 3fa6778: 8292296: Use multiple threads to process ParallelGC deferred updates
  • 800e68d: 8292044: HttpClient doesn't handle 102 or 103 properly
  • 83abfa5: 8255670: Improve C2's detection of modified nodes
  • 5652030: 8292376: A few Swing methods use inheritDoc on exceptions which are not inherited
  • 03f287d: 8293995: Problem list sun/tools/jstatd/TestJstatdRmiPort.java on all platforms because of 8293577
  • d5bee4a: 8294086: RISC-V: Cleanup InstructionMark usages in the backend
  • 47f233a: 8292202: modules_do is called without Module_lock
  • 742bc04: 8294100: RISC-V: Move rt_call and xxx_move from SharedRuntime to MacroAssembler
  • 2283c32: 8294149: JMH 1.34 and later requires jopt-simple 5.0.4
  • 9f90eb0: 8294062: Improve parsing performance of j.l.c.MethodTypeDesc
  • ... and 31 more: https://git.openjdk.org/jdk/compare/84ee1a291cb38e1500dc1529c2aa79e7a195502c...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Sep 22, 2022
@openjdk openjdk bot closed this Sep 22, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Sep 22, 2022
@openjdk
Copy link

openjdk bot commented Sep 22, 2022

@RealFYang @zhengxiaolinX Pushed as commit a216960.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot hotspot-dev@openjdk.org integrated Pull request has been integrated
4 participants