1 /*
  2  * Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2017, 2022, Red Hat, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "code/codeCache.hpp"
 27 #include "code/nmethod.hpp"
 28 #include "gc/shared/classUnloadingContext.hpp"
 29 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
 30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 31 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
 32 #include "gc/shenandoah/shenandoahStackWatermark.hpp"
 33 #include "gc/shenandoah/shenandoahUtils.hpp"
 34 #include "memory/resourceArea.hpp"
 35 #include "memory/universe.hpp"
 36 #include "runtime/atomicAccess.hpp"
 37 #include "utilities/powerOfTwo.hpp"
 38 
 39 
 40 ShenandoahNMethodTable* ShenandoahCodeRoots::_nmethod_table;
 41 int ShenandoahCodeRoots::_disarmed_value = 1;
 42 
 43 void ShenandoahCodeRoots::initialize() {
 44   _nmethod_table = new ShenandoahNMethodTable();
 45 }
 46 
 47 void ShenandoahCodeRoots::register_nmethod(nmethod* nm) {
 48   assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
 49   _nmethod_table->register_nmethod(nm);
 50 }
 51 
 52 void ShenandoahCodeRoots::unregister_nmethod(nmethod* nm) {
 53   assert_locked_or_safepoint(CodeCache_lock);
 54   _nmethod_table->unregister_nmethod(nm);
 55 }
 56 
 57 class ShenandoahNMethodUnlinkClosure : public NMethodClosure {
 58 private:
 59   bool                      _unloading_occurred;
 60   ShenandoahHeap* const     _heap;
 61   BarrierSetNMethod* const  _bs;
 62 
 63 public:
 64   ShenandoahNMethodUnlinkClosure(bool unloading_occurred) :
 65       _unloading_occurred(unloading_occurred),
 66       _heap(ShenandoahHeap::heap()),
 67       _bs(ShenandoahBarrierSet::barrier_set()->barrier_set_nmethod()) {}
 68 
 69   virtual void do_nmethod(nmethod* nm) {
 70     assert(_heap->is_concurrent_weak_root_in_progress(), "Only this phase");
 71 
 72     ShenandoahNMethod* nm_data = ShenandoahNMethod::gc_data(nm);
 73     assert(!nm_data->is_unregistered(), "Should not see unregistered entry");
 74 
 75     if (nm->is_unloading()) {
 76       ShenandoahNMethodLocker locker(nm_data->lock());
 77       nm->unlink();
 78       return;
 79     }
 80 
 81     if (_heap->is_evacuation_in_progress()) {
 82       ShenandoahNMethodLocker locker(nm_data->lock());
 83 
 84       // Heal oops
 85       if (_bs->is_armed(nm)) {
 86         ShenandoahNMethod::heal_nmethod_metadata(nm_data);
 87         // Must remain armed to complete remaining work in nmethod entry barrier
 88         assert(_bs->is_armed(nm), "Should remain armed");
 89       }
 90     }
 91 
 92     // Clear compiled ICs and exception caches
 93     ShenandoahNMethodLocker locker(nm_data->ic_lock());
 94     nm->unload_nmethod_caches(_unloading_occurred);
 95   }
 96 };
 97 
 98 class ShenandoahUnlinkTask : public WorkerTask {
 99 private:
100   ShenandoahNMethodUnlinkClosure      _cl;
101   ShenandoahConcurrentNMethodIterator _iterator;
102 
103 public:
104   ShenandoahUnlinkTask(bool unloading_occurred) :
105     WorkerTask("Shenandoah Unlink NMethods"),
106     _cl(unloading_occurred),
107     _iterator(ShenandoahCodeRoots::table()) {}
108 
109   virtual void work(uint worker_id) {
110     _iterator.nmethods_do(&_cl);
111   }
112 };
113 
114 void ShenandoahCodeRoots::unlink(WorkerThreads* workers, bool unloading_occurred) {
115   assert(ShenandoahHeap::heap()->unload_classes(), "Only when running concurrent class unloading");
116 
117   ShenandoahUnlinkTask task(unloading_occurred);
118   workers->run_task(&task);
119 }
120 
121 void ShenandoahCodeRoots::purge() {
122   assert(ShenandoahHeap::heap()->unload_classes(), "Only when running concurrent class unloading");
123 
124   ClassUnloadingContext::context()->purge_and_free_nmethods();
125 }
126 
127 ShenandoahCodeRootsIterator::ShenandoahCodeRootsIterator() :
128         _table_snapshot(nullptr) {
129   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
130   MutexLocker locker(CodeCache_lock, Mutex::_no_safepoint_check_flag);
131   _table_snapshot = ShenandoahCodeRoots::table()->snapshot_for_iteration();
132 }
133 
134 ShenandoahCodeRootsIterator::~ShenandoahCodeRootsIterator() {
135   MonitorLocker locker(CodeCache_lock, Mutex::_no_safepoint_check_flag);
136   ShenandoahCodeRoots::table()->finish_iteration(_table_snapshot);
137   _table_snapshot = nullptr;
138   locker.notify_all();
139 }
140 
141 void ShenandoahCodeRootsIterator::possibly_parallel_nmethods_do(NMethodClosure *f) {
142   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
143   assert(_table_snapshot != nullptr, "Sanity");
144   _table_snapshot->parallel_nmethods_do(f);
145 }