@@ -775,15 +775,33 @@ void InterpreterMacroAssembler::lock_object(Register lock_reg)
775775 cmpxchg_obj_header (swap_reg, lock_reg, obj_reg, rscratch1, done, /* fallthrough*/ NULL );
776776 }
777777
778- // Test if the oopMark is an obvious stack pointer, i.e.,
778+ // Fast check for recursive lock.
779+ //
780+ // Can apply the optimization only if this is a stack lock
781+ // allocated in this thread. For efficiency, we can focus on
782+ // recently allocated stack locks (instead of reading the stack
783+ // base and checking whether 'mark' points inside the current
784+ // thread stack):
779785 // 1) (mark & 7) == 0, and
780- // 2) rsp <= mark < mark + os::pagesize()
786+ // 2) sp <= mark < mark + os::pagesize()
787+ //
788+ // Warning: sp + os::pagesize can overflow the stack base. We must
789+ // neither apply the optimization for an inflated lock allocated
790+ // just above the thread stack (this is why condition 1 matters)
791+ // nor apply the optimization if the stack lock is inside the stack
792+ // of another thread. The latter is avoided even in case of overflow
793+ // because we have guard pages at the end of all stacks. Hence, if
794+ // we go over the stack base and hit the stack of another thread,
795+ // this should not be in a writeable area that could contain a
796+ // stack lock allocated by that thread. As a consequence, a stack
797+ // lock less than page size away from sp is guaranteed to be
798+ // owned by the current thread.
781799 //
782800 // These 3 tests can be done by evaluating the following
783- // expression: ((mark - rsp ) & (7 - os::vm_page_size())),
801+ // expression: ((mark - sp ) & (7 - os::vm_page_size())),
784802 // assuming both stack pointer and pagesize have their
785803 // least significant 3 bits clear.
786- // NOTE: the oopMark is in swap_reg %r0 as the result of cmpxchg
804+ // NOTE: the mark is in swap_reg %r0 as the result of cmpxchg
787805 // NOTE2: aarch64 does not like to subtract sp from rn so take a
788806 // copy
789807 mov (rscratch1, sp);
0 commit comments