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

JDK-8320892: AArch64: Restore FPU control state after JNI #16851

Closed
wants to merge 4 commits into from

Conversation

theRealAph
Copy link
Contributor

@theRealAph theRealAph commented Nov 28, 2023

Some buggy libraries corrupt the floating-point control register. Provide something similar to the x86 RestoreMXCSROnJNICalls.

I realize that using the x86ish name "RestoreMXCSROnJNICalls" might be a little controversial, but it is a global flag, not a CPU-specific one. And it's clearly intended for this purpose. It might have been better if that flag had been given a better name twentyish years ago, but we can't change it now.


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-8320892: AArch64: Restore FPU control state after JNI (Bug - P3)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 16851

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 28, 2023

👋 Welcome back aph! 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 Nov 28, 2023
@openjdk
Copy link

openjdk bot commented Nov 28, 2023

@theRealAph 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 Nov 28, 2023
@mlbridge
Copy link

mlbridge bot commented Nov 28, 2023

Webrevs

// don't want non-IEEE rounding modes or floating-point traps.
bfi(tmp1, zr, 22, 4); // Clear DN, FZ, and Rmode
bfi(tmp1, zr, 8, 5); // Clear exception-control bits (8-12)
eor(tmp1, tmp1, tmp2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm? So . . .

  1. We ensure tmp1 has the bits we want by clearing DN FZ Rmode and Exception bits
  2. we XOR tmp1 with the original bits (saved in tmp2) and put the 'difference' in tmp1 (?)
  3. If the difference is zero we skip
  4. Otherwise we write tmp1 (i.e. the 'difference' bits) to fpcr (???)

Should this not be

    get_fpcr(tmp1);
    mov(tmp2, tmp1);
    bfi(tmp1, zr, 22, 4);
    bfi(tmp1, zr, 8, 5);
    eor(tmp2, tmp1, tmp2)
    cbz(tmp2, OK);
    set_fpcr(tmp1);
    bind(OK);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Argh, yes. The curse of the last-minute change...

Good catch.

Copy link
Contributor

@adinn adinn left a comment

Choose a reason for hiding this comment

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

Looks good.

@openjdk
Copy link

openjdk bot commented Nov 28, 2023

@theRealAph 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:

8320892: AArch64: Restore FPU control state after JNI

Reviewed-by: adinn, stuefe

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 173 new commits pushed to the master branch:

  • 78d0958: 8321406: Null IDs should be resolved as before catalogs are added
  • aaaae3e: 8321207: javac is not accepting correct code
  • 86b27b7: 8317831: compiler/codecache/CheckLargePages.java fails on OL 8.8 with unexpected memory string
  • 3cd65ce: 8321325: Remove unused Java_java_awt_MenuComponent_initIDs function
  • 905137d: 8321219: runtime/jni/FastGetField: assert(is_interpreted_frame()) failed: interpreted frame expected
  • 430564c: 8308715: Create a mechanism for Implicitly Declared Class javadoc
  • c8fa758: 8320860: add-opens/add-exports require '=' in JAVA_TOOL_OPTIONS
  • 9e57010: 8315149: Add hsperf counters for CPU time of internal GC threads
  • b0d1450: 8321053: Use ByteArrayInputStream.buf directly when parameter of transferTo() is trusted
  • acaf2c8: 8318590: JButton ignores margin when painting HTML text
  • ... and 163 more: https://git.openjdk.org/jdk/compare/0c9a61c18545c7bd48e54e6b4e523b9ad8d0507d...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.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 28, 2023
bfi(tmp1, zr, 22, 4); // Clear DN, FZ, and Rmode
bfi(tmp1, zr, 8, 5); // Clear exception-control bits (8-12)
eor(tmp2, tmp1, tmp2);
cbz(tmp2, OK); // Only reset FPCR if it's wrong
Copy link
Contributor

Choose a reason for hiding this comment

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

should we maybe do the same in generate_call_stub, too (likely faster)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, it would be. I didn't think it was worthwhile, but we have the code now.

@tkrodriguez
Copy link
Contributor

x86 also has a restore call in DowncallLinker::StubGenerator::generate so you might consider adding there as well.

@tkrodriguez
Copy link
Contributor

Regarding the flag name you could introduce a better name and alias to it RestoreMXCSROnJNICalls. Reusing the same name it definitely confusing. At a minimum you need to update the documentation for RestoreMXCSROnJNICalls to indicate that it also does something on aarch64 or generalize the language to something like Restore floating point control word when returning from JNI calls.

@theRealAph
Copy link
Contributor Author

Regarding the flag name you could introduce a better name and alias to it RestoreMXCSROnJNICalls.

Ah, I did not know that flag aliases were possible. I'll have a look at something generic.

Reusing the same name it definitely confusing. At a minimum you need to update the documentation for RestoreMXCSROnJNICalls to indicate that it also does something on aarch64 or generalize the language to something like Restore floating point control word when returning from JNI calls.

I'll go digging.

@theRealAph
Copy link
Contributor Author

x86 also has a restore call in DowncallLinker::StubGenerator::generate so you might consider adding there as well.

I think it's there.

@tkrodriguez
Copy link
Contributor

Yes it's there. Sorry I guess I was scanning too quickly.

@dholmes-ora
Copy link
Member

I was also going to suggest adding a new flag and creating an alias. The new flag will need a CSR request of course.

@theRealAph
Copy link
Contributor Author

I was also going to suggest adding a new flag and creating an alias. The new flag will need a CSR request of course.

Given that it's new and it's diagnostic flag I'm a bit surprised at that. I was trying for a quick fix.

Anyway, how do you create an alias? I can't see any examples, and I haven't found a way through the maze of twisty #define passages.

@dholmes-ora
Copy link
Member

@theRealAph the RestoreMXCSROnJNICalls flag is a product flag not diagnostic.

Aliased flags are setup in arguments.cpp by editing this:

static AliasedFlag const aliased_jvm_flags[] = {
  { "DefaultMaxRAMFraction",    "MaxRAMFraction"    },
  { "CreateMinidumpOnCrash",    "CreateCoredumpOnCrash" },
  { nullptr, nullptr}
};

@dholmes-ora
Copy link
Member

Also from arguments.cpp

 *      ALIASED: An option that is simply another name for another option. This is often
 *               part of the process of deprecating a flag, but not all aliases need
 *               to be deprecated.
 *
 *               Create an alias for an option by adding the old and new option names to the
 *               "aliased_jvm_flags" table. Delete the old variable from globals.hpp (etc).

@theRealAph
Copy link
Contributor Author

@theRealAph the RestoreMXCSROnJNICalls flag is a product flag not diagnostic.

Ah, thanks,

Aliased flags are setup in arguments.cpp by editing this:

OK. How about we split this into two, this first part without a CSR, and the second part, which creates the generic alias, with one? That way we can mitigate a live problem in this release.

@theRealAph
Copy link
Contributor Author

@theRealAph the RestoreMXCSROnJNICalls flag is a product flag not diagnostic.

Ah, thanks,

Aliased flags are setup in arguments.cpp by editing this:

OK. How about we split this into two, this first part without a CSR, and the second part, which creates the generic alias, with one? That way we can mitigate a live problem in this release.

Please? One day left.

Copy link
Member

@tstuefe tstuefe left a comment

Choose a reason for hiding this comment

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

Looks good.

I looked at https://help.totalview.io/previous_releases/2019/HTML/index.html#page/Reference_Guide%2FPowerFPSCRRegister_2.html%23, and there seems to be another exception bit, bit 15, the "Input denormal exception enable". That one is okay to be modified?

// Set FPCR to the state we need. We do want Round to Nearest. We
// don't want non-IEEE rounding modes or floating-point traps.
bfi(tmp1, zr, 22, 4); // Clear DN, FZ, and Rmode
bfi(tmp1, zr, 8, 5); // Clear exception-control bits (8-12)
Copy link

Choose a reason for hiding this comment

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

(Related to both this PR and #16637)

Shouldn't also explicit flushing inputs to zero, i.e. when AH:FIZ is (1:1), be protected against?

Also, is it necessary to clear DN? When looking at the spec, I think this should be allowed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(Related to both this PR and #16637)

Shouldn't also explicit flushing inputs to zero, i.e. when AH:FIZ is (1:1), be protected against?

I've avoided touching ArmV8.7 ALT_FP, but you might be right. AH:FIZ are both RES0, so it is safe to do so.

Also, is it necessary to clear DN? When looking at the spec, I think this should be allowed.

I think it's wise to clear DN. We save and restore all of the FPCR at entry to Java, clearing DN, and it's not unreasonable to expect a JNI call not to mess with FPCR. Also, I think replacing NaN payload bits with the default NaN is pointless, and violates the principle of least surprise if not the spec of longBitsToDouble.

@dholmes-ora
Copy link
Member

OK. How about we split this into two, this first part without a CSR, and the second part, which creates the generic alias, with one? That way we can mitigate a live problem in this release.

I'm fine with splitting it if needed, but I also re-evaluated this as a P3 so it can still go into 22 after RDP 1.

@theRealAph
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Dec 6, 2023

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

  • 0217b5a: 8321248: ClassFile API ClassModel::verify is inconsistent with the rest of the API
  • 7fbfb3b: 8321369: Unproblemlist gc/cslocker/TestCSLocker.java
  • 2678e4c: 8319111: Mismatched MemorySegment heap access is not consistently intrinsified
  • a0920aa: 8321159: SymbolLookup.libraryLookup(Path, Arena) Assumes default Filesystem
  • 9d77677: 8321124: java/util/stream/GatherersTest.java times out
  • 4c96aac: 8320935: Move CDS config initialization code to cdsConfig.cpp
  • 78d0958: 8321406: Null IDs should be resolved as before catalogs are added
  • aaaae3e: 8321207: javac is not accepting correct code
  • 86b27b7: 8317831: compiler/codecache/CheckLargePages.java fails on OL 8.8 with unexpected memory string
  • 3cd65ce: 8321325: Remove unused Java_java_awt_MenuComponent_initIDs function
  • ... and 169 more: https://git.openjdk.org/jdk/compare/0c9a61c18545c7bd48e54e6b4e523b9ad8d0507d...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Dec 6, 2023
@openjdk openjdk bot closed this Dec 6, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Dec 6, 2023
@openjdk
Copy link

openjdk bot commented Dec 6, 2023

@theRealAph Pushed as commit 50f3124.

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

@robehn
Copy link
Contributor

robehn commented Apr 19, 2024

@theRealAph IMHO we should add a new flag aliasing the old. Then deprecate MXCSR flag.

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
Development

Successfully merging this pull request may close these issues.

8 participants