1 /*
   2  * Copyright (c) 2015, 2020, Red Hat, Inc. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
  26 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
  27 
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "gc/shenandoah/shenandoahAsserts.hpp"
  30 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  31 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
  32 #include "gc/shenandoah/shenandoahEvacOOMHandler.inline.hpp"
  33 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
  34 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  35 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  36 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  37 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
  38 #include "memory/iterator.inline.hpp"
  39 #include "oops/oop.inline.hpp"
  40 
  41 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null(oop p) {
  42   return ShenandoahForwarding::get_forwardee(p);
  43 }
  44 
  45 inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) {
  46   if (p != NULL) {
  47     return resolve_forwarded_not_null(p);
  48   } else {
  49     return p;
  50   }
  51 }
  52 
  53 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null_mutator(oop p) {
  54   return ShenandoahForwarding::get_forwardee_mutator(p);
  55 }
  56 
  57 template <class T>
  58 inline oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, T* load_addr) {
  59   assert(ShenandoahLoadRefBarrier, "should be enabled");
  60   shenandoah_assert_in_cset(load_addr, obj);
  61 
  62   oop fwd = resolve_forwarded_not_null_mutator(obj);
  63   if (obj == fwd) {
  64     assert(_heap->is_evacuation_in_progress(),
  65            "evac should be in progress");
  66     Thread* const t = Thread::current();
  67     ShenandoahEvacOOMScope scope(t);
  68     fwd = _heap->evacuate_object(obj, t);
  69   }
  70 
  71   if (load_addr != NULL && fwd != obj) {
  72     // Since we are here and we know the load address, update the reference.
  73     ShenandoahHeap::cas_oop(fwd, load_addr, obj);
  74   }
  75 
  76   return fwd;
  77 }
  78 
  79 inline void ShenandoahBarrierSet::enqueue(oop obj) {
  80   assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");
  81 
  82   // Filter marked objects before hitting the SATB queues. The same predicate would
  83   // be used by SATBMQ::filter to eliminate already marked objects downstream, but
  84   // filtering here helps to avoid wasteful SATB queueing work to begin with.
  85   if (!_heap->requires_marking<false>(obj)) return;
  86 
  87   ShenandoahThreadLocalData::satb_mark_queue(Thread::current()).enqueue_known_active(obj);
  88 }
  89 
  90 template <DecoratorSet decorators, typename T>
  91 inline void ShenandoahBarrierSet::satb_barrier(T *field) {
  92   if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||
  93       HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
  94     return;
  95   }
  96   if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
  97     T heap_oop = RawAccess<>::oop_load(field);
  98     if (!CompressedOops::is_null(heap_oop)) {
  99       enqueue(CompressedOops::decode(heap_oop));
 100     }
 101   }
 102 }
 103 
 104 inline void ShenandoahBarrierSet::satb_enqueue(oop value) {
 105   assert(value != NULL, "checked before");
 106   if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
 107     enqueue(value);
 108   }
 109 }
 110 
 111 inline void ShenandoahBarrierSet::storeval_barrier(oop obj) {
 112   if (ShenandoahStoreValEnqueueBarrier && obj != NULL && _heap->is_concurrent_mark_in_progress()) {
 113     enqueue(obj);
 114   }
 115 }
 116 
 117 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {
 118   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
 119   const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
 120   const bool peek              = (decorators & AS_NO_KEEPALIVE) != 0;
 121   if (!peek && !on_strong_oop_ref) {
 122     satb_enqueue(value);
 123   }
 124 }
 125 
 126 template <DecoratorSet decorators>
 127 inline void ShenandoahBarrierSet::keep_alive_if_weak(oop value) {
 128   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
 129   if (!HasDecorator<decorators, ON_STRONG_OOP_REF>::value &&
 130       !HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
 131     satb_enqueue(value);
 132   }
 133 }
 134 
 135 template <DecoratorSet decorators, typename BarrierSetT>
 136 template <typename T>
 137 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
 138   oop value = Raw::oop_load_not_in_heap(addr);
 139   if (value != NULL) {
 140     ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();
 141     value = bs->load_reference_barrier_native(value, addr);
 142     if (value != NULL) {
 143       bs->keep_alive_if_weak<decorators>(value);
 144     }
 145   }
 146   return value;
 147 }
 148 
 149 template <DecoratorSet decorators, typename BarrierSetT>
 150 template <typename T>
 151 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
 152   oop value = Raw::oop_load_in_heap(addr);
 153   if (value != NULL) {
 154     ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();
 155     value = bs->load_reference_barrier_not_null(value);
 156     bs->keep_alive_if_weak<decorators>(value);
 157   }
 158   return value;
 159 }
 160 
 161 template <DecoratorSet decorators, typename BarrierSetT>
 162 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
 163   oop value = Raw::oop_load_in_heap_at(base, offset);
 164   if (value != NULL) {
 165     ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();
 166     value = bs->load_reference_barrier_not_null(value);
 167     bs->keep_alive_if_weak(AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset),
 168                            value);
 169   }
 170   return value;
 171 }
 172 
 173 template <DecoratorSet decorators, typename BarrierSetT>
 174 template <typename T>
 175 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
 176   shenandoah_assert_marked_if(NULL, value, !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress());
 177   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
 178   bs->storeval_barrier(value);
 179   bs->satb_barrier<decorators>(addr);
 180   Raw::oop_store(addr, value);
 181 }
 182 
 183 template <DecoratorSet decorators, typename BarrierSetT>
 184 template <typename T>
 185 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
 186   shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
 187   shenandoah_assert_not_forwarded_except  (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
 188   shenandoah_assert_not_in_cset_except    (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
 189 
 190   oop_store_not_in_heap(addr, value);
 191 }
 192 
 193 template <DecoratorSet decorators, typename BarrierSetT>
 194 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
 195   oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
 196 }
 197 
 198 template <DecoratorSet decorators, typename BarrierSetT>
 199 template <typename T>
 200 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
 201   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
 202   bs->storeval_barrier(new_value);
 203 
 204   oop res;
 205   oop expected = compare_value;
 206   do {
 207     compare_value = expected;
 208     res = Raw::oop_atomic_cmpxchg(addr, compare_value, new_value);
 209     expected = res;
 210   } while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected)));
 211 
 212   // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
 213   // because it must be the previous value.
 214   if (res != NULL) {
 215     res = ShenandoahBarrierSet::barrier_set()->load_reference_barrier_not_null(res);
 216     bs->satb_enqueue(res);
 217   }
 218   return res;
 219 }
 220 
 221 template <DecoratorSet decorators, typename BarrierSetT>
 222 template <typename T>
 223 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
 224   return oop_atomic_cmpxchg_not_in_heap(addr, compare_value, new_value);
 225 }
 226 
 227 template <DecoratorSet decorators, typename BarrierSetT>
 228 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
 229   return oop_atomic_cmpxchg_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), compare_value, new_value);
 230 }
 231 
 232 template <DecoratorSet decorators, typename BarrierSetT>
 233 template <typename T>
 234 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
 235   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
 236   bs->storeval_barrier(new_value);
 237 
 238   oop previous = Raw::oop_atomic_xchg(addr, new_value);
 239 
 240   // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
 241   // because it must be the previous value.
 242   if (previous != NULL) {
 243     previous = ShenandoahBarrierSet::barrier_set()->load_reference_barrier_not_null(previous);
 244     bs->satb_enqueue(previous);
 245   }
 246   return previous;
 247 }
 248 
 249 template <DecoratorSet decorators, typename BarrierSetT>
 250 template <typename T>
 251 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
 252   return oop_atomic_xchg_not_in_heap(addr, new_value);
 253 }
 254 
 255 template <DecoratorSet decorators, typename BarrierSetT>
 256 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
 257   return oop_atomic_xchg_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), new_value);
 258 }
 259 
 260 // Clone barrier support
 261 template <DecoratorSet decorators, typename BarrierSetT>
 262 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
 263   if (ShenandoahCloneBarrier) {
 264     ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src);
 265   }
 266   Raw::clone(src, dst, size);
 267 }
 268 
 269 template <DecoratorSet decorators, typename BarrierSetT>
 270 template <typename T>
 271 bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 272                                                                                          arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 273                                                                                          size_t length) {
 274   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
 275   bs->arraycopy_barrier(arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw),
 276                         arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw),
 277                         length);
 278   return Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
 279 }
 280 
 281 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
 282 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
 283   assert(HAS_FWD == _heap->has_forwarded_objects(), "Forwarded object status is sane");
 284 
 285   Thread* thread = Thread::current();
 286   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 287   ShenandoahMarkingContext* ctx = _heap->marking_context();
 288   const ShenandoahCollectionSet* const cset = _heap->collection_set();
 289   T* end = src + count;
 290   for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
 291     T o = RawAccess<>::oop_load(elem_ptr);
 292     if (!CompressedOops::is_null(o)) {
 293       oop obj = CompressedOops::decode_not_null(o);
 294       if (HAS_FWD && cset->is_in(obj)) {
 295         oop fwd = resolve_forwarded_not_null(obj);
 296         if (EVAC && obj == fwd) {
 297           fwd = _heap->evacuate_object(obj, thread);
 298         }
 299         assert(obj != fwd || _heap->cancelled_gc(), "must be forwarded");
 300         oop witness = ShenandoahHeap::cas_oop(fwd, elem_ptr, o);
 301         obj = fwd;
 302       }
 303       if (ENQUEUE && !ctx->is_marked(obj)) {
 304         queue.enqueue_known_active(obj);
 305       }
 306     }
 307   }
 308 }
 309 
 310 template <class T>
 311 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {
 312   if (count == 0) {
 313     return;
 314   }
 315   int gc_state = _heap->gc_state();
 316   if ((gc_state & ShenandoahHeap::MARKING) != 0) {
 317     arraycopy_marking(src, dst, count);
 318   } else if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
 319     arraycopy_evacuation(src, count);
 320   } else if ((gc_state & ShenandoahHeap::UPDATEREFS) != 0) {
 321     arraycopy_update(src, count);
 322   }
 323 }
 324 
 325 template <class T>
 326 void ShenandoahBarrierSet::arraycopy_marking(T* src, T* dst, size_t count) {
 327   assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
 328   T* array = ShenandoahSATBBarrier ? dst : src;
 329   if (!_heap->marking_context()->allocated_after_mark_start(reinterpret_cast<HeapWord*>(array))) {
 330     arraycopy_work<T, false, false, true>(array, count);
 331   }
 332 }
 333 
 334 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {
 335   return ary < _heap->heap_region_containing(ary)->get_update_watermark();
 336 }
 337 
 338 template <class T>
 339 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
 340   assert(_heap->is_evacuation_in_progress(), "only during evacuation");
 341   if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
 342     ShenandoahEvacOOMScope oom_evac;
 343     arraycopy_work<T, true, true, false>(src, count);
 344   }
 345 }
 346 
 347 template <class T>
 348 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
 349   assert(_heap->is_update_refs_in_progress(), "only during update-refs");
 350   if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
 351     arraycopy_work<T, true, false, false>(src, count);
 352   }
 353 }
 354 
 355 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP