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 bool ShenandoahCodeRoots::use_nmethod_barriers_for_mark() {
 44   // Continuations need nmethod barriers for scanning stack chunk nmethods.
 45   if (Continuations::enabled()) return true;
 46 
 47   // Concurrent class unloading needs nmethod barriers.
 48   // When a nmethod is about to be executed, we need to make sure that all its
 49   // metadata are marked. The alternative is to remark thread roots at final mark
 50   // pause, which would cause latency issues.
 51   if (ShenandoahHeap::heap()->unload_classes()) return true;
 52 
 53   // Otherwise, we can go without nmethod barriers.
 54   return false;
 55 }
 56 
 57 void ShenandoahCodeRoots::initialize() {
 58   _nmethod_table = new ShenandoahNMethodTable();
 59 }
 60 
 61 void ShenandoahCodeRoots::register_nmethod(nmethod* nm) {
 62   assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
 63   _nmethod_table->register_nmethod(nm);
 64 }
 65 
 66 void ShenandoahCodeRoots::unregister_nmethod(nmethod* nm) {
 67   assert_locked_or_safepoint(CodeCache_lock);
 68   _nmethod_table->unregister_nmethod(nm);
 69 }
 70 
 71 void ShenandoahCodeRoots::arm_nmethods_for_mark() {
 72   if (use_nmethod_barriers_for_mark()) {
 73     BarrierSet::barrier_set()->barrier_set_nmethod()->arm_all_nmethods();
 74   }
 75 }
 76 
 77 void ShenandoahCodeRoots::arm_nmethods_for_evac() {
 78   BarrierSet::barrier_set()->barrier_set_nmethod()->arm_all_nmethods();
 79 }
 80 
 81 class ShenandoahDisarmNMethodClosure : public NMethodClosure {
 82 private:
 83   BarrierSetNMethod* const _bs;
 84 
 85 public:
 86   ShenandoahDisarmNMethodClosure() :
 87     _bs(BarrierSet::barrier_set()->barrier_set_nmethod()) {
 88   }
 89 
 90   virtual void do_nmethod(nmethod* nm) {
 91     _bs->disarm(nm);
 92   }
 93 };
 94 
 95 class ShenandoahDisarmNMethodsTask : public WorkerTask {
 96 private:
 97   ShenandoahDisarmNMethodClosure      _cl;
 98   ShenandoahConcurrentNMethodIterator _iterator;
 99 
100 public:
101   ShenandoahDisarmNMethodsTask() :
102     WorkerTask("Shenandoah Disarm NMethods"),
103     _iterator(ShenandoahCodeRoots::table()) {
104     assert(SafepointSynchronize::is_at_safepoint(), "Only at a safepoint");
105   }
106 
107   virtual void work(uint worker_id) {
108     ShenandoahParallelWorkerSession worker_session(worker_id);
109     _iterator.nmethods_do(&_cl);
110   }
111 };
112 
113 void ShenandoahCodeRoots::disarm_nmethods() {
114   if (use_nmethod_barriers_for_mark()) {
115     ShenandoahDisarmNMethodsTask task;
116     ShenandoahHeap::heap()->workers()->run_task(&task);
117   }
118 }
119 
120 class ShenandoahNMethodUnlinkClosure : public NMethodClosure {
121 private:
122   bool                      _unloading_occurred;
123   ShenandoahHeap* const     _heap;
124   BarrierSetNMethod* const  _bs;
125 
126 public:
127   ShenandoahNMethodUnlinkClosure(bool unloading_occurred) :
128       _unloading_occurred(unloading_occurred),
129       _heap(ShenandoahHeap::heap()),
130       _bs(ShenandoahBarrierSet::barrier_set()->barrier_set_nmethod()) {}
131 
132   virtual void do_nmethod(nmethod* nm) {
133     assert(_heap->is_concurrent_weak_root_in_progress(), "Only this phase");
134 
135     ShenandoahNMethod* nm_data = ShenandoahNMethod::gc_data(nm);
136     assert(!nm_data->is_unregistered(), "Should not see unregistered entry");
137 
138     if (nm->is_unloading()) {
139       ShenandoahNMethodLocker locker(nm_data->lock());
140       nm->unlink();
141       return;
142     }
143 
144     {
145       ShenandoahNMethodLocker locker(nm_data->lock());
146 
147       // Heal oops
148       if (_bs->is_armed(nm)) {
149         ShenandoahEvacOOMScope oom_evac_scope;
150         ShenandoahNMethod::heal_nmethod_metadata(nm_data);
151         // Must remain armed to complete remaining work in nmethod entry barrier
152         assert(_bs->is_armed(nm), "Should remain armed");
153       }
154     }
155 
156     // Clear compiled ICs and exception caches
157     ShenandoahNMethodLocker locker(nm_data->ic_lock());
158     nm->unload_nmethod_caches(_unloading_occurred);
159   }
160 };
161 
162 class ShenandoahUnlinkTask : public WorkerTask {
163 private:
164   ShenandoahNMethodUnlinkClosure      _cl;
165   ShenandoahConcurrentNMethodIterator _iterator;
166 
167 public:
168   ShenandoahUnlinkTask(bool unloading_occurred) :
169     WorkerTask("Shenandoah Unlink NMethods"),
170     _cl(unloading_occurred),
171     _iterator(ShenandoahCodeRoots::table()) {}
172 
173   virtual void work(uint worker_id) {
174     _iterator.nmethods_do(&_cl);
175   }
176 };
177 
178 void ShenandoahCodeRoots::unlink(WorkerThreads* workers, bool unloading_occurred) {
179   assert(ShenandoahHeap::heap()->unload_classes(), "Only when running concurrent class unloading");
180 
181   ShenandoahUnlinkTask task(unloading_occurred);
182   workers->run_task(&task);
183 }
184 
185 void ShenandoahCodeRoots::purge() {
186   assert(ShenandoahHeap::heap()->unload_classes(), "Only when running concurrent class unloading");
187 
188   ClassUnloadingContext::context()->purge_and_free_nmethods();
189 }
190 
191 ShenandoahCodeRootsIterator::ShenandoahCodeRootsIterator() :
192         _table_snapshot(nullptr) {
193   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
194   MutexLocker locker(CodeCache_lock, Mutex::_no_safepoint_check_flag);
195   _table_snapshot = ShenandoahCodeRoots::table()->snapshot_for_iteration();
196 }
197 
198 ShenandoahCodeRootsIterator::~ShenandoahCodeRootsIterator() {
199   MonitorLocker locker(CodeCache_lock, Mutex::_no_safepoint_check_flag);
200   ShenandoahCodeRoots::table()->finish_iteration(_table_snapshot);
201   _table_snapshot = nullptr;
202   locker.notify_all();
203 }
204 
205 void ShenandoahCodeRootsIterator::possibly_parallel_nmethods_do(NMethodClosure *f) {
206   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
207   assert(_table_snapshot != nullptr, "Sanity");
208   _table_snapshot->parallel_nmethods_do(f);
209 }