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/shenandoahEvacOOMHandler.inline.hpp"
 31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 32 #include "gc/shenandoah/shenandoahNMethod.inline.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 void ShenandoahCodeRoots::arm_nmethods() {
 58   if (ShenandoahGCStateCheckHotpatch) {
 59     char gc_state = ShenandoahHeap::heap()->gc_state();
 60     log_info(gc)("Arming nmethods with GC state: %d [%s%s%s%s%s%s%s]",
 61          gc_state,
 62          ((gc_state & ShenandoahHeap::HAS_FORWARDED) > 0) ? "HAS_FORWARDED "  : "",
 63          ((gc_state & ShenandoahHeap::MARKING) > 0)       ? "MARKING "        : "",
 64          ((gc_state & ShenandoahHeap::EVACUATION) > 0)    ? "EVACUATION "     : "",
 65          ((gc_state & ShenandoahHeap::UPDATE_REFS) > 0)   ? "UPDATE_REFS "    : "",
 66          ((gc_state & ShenandoahHeap::WEAK_ROOTS) > 0)    ? "WEAK_ROOTS "     : "",
 67          ((gc_state & ShenandoahHeap::YOUNG_MARKING) > 0) ? "YOUNG_MARKING "  : "",
 68          ((gc_state & ShenandoahHeap::OLD_MARKING) > 0)   ? "OLD_MARKING "    : ""
 69     );
 70   }
 71   BarrierSet::barrier_set()->barrier_set_nmethod()->arm_all_nmethods();
 72 }
 73 
 74 class ShenandoahDisarmNMethodClosure : public NMethodClosure {
 75 public:
 76   virtual void do_nmethod(nmethod* nm) {
 77     ShenandoahNMethod::disarm_nmethod(nm);
 78   }
 79 };
 80 
 81 class ShenandoahDisarmNMethodsTask : public WorkerTask {
 82 private:
 83   ShenandoahDisarmNMethodClosure      _cl;
 84   ShenandoahConcurrentNMethodIterator _iterator;
 85 
 86 public:
 87   ShenandoahDisarmNMethodsTask() :
 88     WorkerTask("Shenandoah Disarm NMethods"),
 89     _iterator(ShenandoahCodeRoots::table()) {}
 90 
 91   virtual void work(uint worker_id) {
 92     ShenandoahParallelWorkerSession worker_session(worker_id);
 93     _iterator.nmethods_do(&_cl);
 94   }
 95 };
 96 
 97 void ShenandoahCodeRoots::disarm_nmethods() {
 98   ShenandoahDisarmNMethodsTask task;
 99   ShenandoahHeap::heap()->workers()->run_task(&task);
100 }
101 
102 class ShenandoahNMethodUnlinkClosure : public NMethodClosure {
103 private:
104   bool                      _unloading_occurred;
105   ShenandoahHeap* const     _heap;
106   BarrierSetNMethod* const  _bs;
107 
108 public:
109   ShenandoahNMethodUnlinkClosure(bool unloading_occurred) :
110       _unloading_occurred(unloading_occurred),
111       _heap(ShenandoahHeap::heap()),
112       _bs(ShenandoahBarrierSet::barrier_set()->barrier_set_nmethod()) {}
113 
114   virtual void do_nmethod(nmethod* nm) {
115     assert(_heap->is_concurrent_weak_root_in_progress(), "Only this phase");
116 
117     ShenandoahNMethod* nm_data = ShenandoahNMethod::gc_data(nm);
118     assert(!nm_data->is_unregistered(), "Should not see unregistered entry");
119 
120     if (nm->is_unloading()) {
121       ShenandoahNMethodLocker locker(nm_data->lock());
122       nm->unlink();
123       return;
124     }
125 
126     {
127       ShenandoahNMethodLocker locker(nm_data->lock());
128 
129       // Heal oops
130       if (_bs->is_armed(nm)) {
131         ShenandoahEvacOOMScope oom_evac_scope;
132         ShenandoahNMethod::heal_nmethod_metadata(nm_data);
133         // Must remain armed to complete remaining work in nmethod entry barrier
134         assert(_bs->is_armed(nm), "Should remain armed");
135       }
136     }
137 
138     // Clear compiled ICs and exception caches
139     ShenandoahNMethodLocker locker(nm_data->ic_lock());
140     nm->unload_nmethod_caches(_unloading_occurred);
141   }
142 };
143 
144 class ShenandoahUnlinkTask : public WorkerTask {
145 private:
146   ShenandoahNMethodUnlinkClosure      _cl;
147   ShenandoahConcurrentNMethodIterator _iterator;
148 
149 public:
150   ShenandoahUnlinkTask(bool unloading_occurred) :
151     WorkerTask("Shenandoah Unlink NMethods"),
152     _cl(unloading_occurred),
153     _iterator(ShenandoahCodeRoots::table()) {}
154 
155   virtual void work(uint worker_id) {
156     _iterator.nmethods_do(&_cl);
157   }
158 };
159 
160 void ShenandoahCodeRoots::unlink(WorkerThreads* workers, bool unloading_occurred) {
161   assert(ShenandoahHeap::heap()->unload_classes(), "Only when running concurrent class unloading");
162 
163   ShenandoahUnlinkTask task(unloading_occurred);
164   workers->run_task(&task);
165 }
166 
167 void ShenandoahCodeRoots::purge() {
168   assert(ShenandoahHeap::heap()->unload_classes(), "Only when running concurrent class unloading");
169 
170   ClassUnloadingContext::context()->purge_and_free_nmethods();
171 }
172 
173 ShenandoahCodeRootsIterator::ShenandoahCodeRootsIterator() :
174         _table_snapshot(nullptr) {
175   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
176   MutexLocker locker(CodeCache_lock, Mutex::_no_safepoint_check_flag);
177   _table_snapshot = ShenandoahCodeRoots::table()->snapshot_for_iteration();
178 }
179 
180 ShenandoahCodeRootsIterator::~ShenandoahCodeRootsIterator() {
181   MonitorLocker locker(CodeCache_lock, Mutex::_no_safepoint_check_flag);
182   ShenandoahCodeRoots::table()->finish_iteration(_table_snapshot);
183   _table_snapshot = nullptr;
184   locker.notify_all();
185 }
186 
187 void ShenandoahCodeRootsIterator::possibly_parallel_nmethods_do(NMethodClosure *f) {
188   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
189   assert(_table_snapshot != nullptr, "Sanity");
190   _table_snapshot->parallel_nmethods_do(f);
191 }