1 /*
  2  * Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2019, 2021, 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 
 27 #include "classfile/classLoaderDataGraph.hpp"
 28 #include "classfile/systemDictionary.hpp"
 29 #include "code/codeBehaviours.hpp"
 30 #include "code/codeCache.hpp"
 31 #include "code/dependencyContext.hpp"
 32 #include "gc/shared/classUnloadingContext.hpp"
 33 #include "gc/shared/gcBehaviours.hpp"
 34 #include "gc/shared/suspendibleThreadSet.hpp"
 35 #include "gc/shenandoah/shenandoahLock.hpp"
 36 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
 37 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
 38 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
 39 #include "gc/shenandoah/shenandoahUnload.hpp"
 40 #include "gc/shenandoah/shenandoahVerifier.hpp"
 41 #include "memory/iterator.hpp"
 42 #include "memory/metaspaceUtils.hpp"
 43 #include "memory/resourceArea.hpp"
 44 #include "oops/access.inline.hpp"
 45 
 46 class ShenandoahIsUnloadingOopClosure : public OopClosure {
 47 private:
 48   ShenandoahMarkingContext* const _marking_context;
 49   bool                            _is_unloading;
 50 
 51 public:
 52   ShenandoahIsUnloadingOopClosure() :
 53     _marking_context(ShenandoahHeap::heap()->global_generation()->complete_marking_context()),
 54     _is_unloading(false) {
 55   }
 56 
 57   virtual void do_oop(oop* p) {
 58     if (_is_unloading) {
 59       return;
 60     }
 61 
 62     const oop o = RawAccess<>::oop_load(p);
 63     if (!CompressedOops::is_null(o) &&
 64         !_marking_context->is_marked(o)) {
 65       _is_unloading = true;
 66     }
 67   }
 68 
 69   virtual void do_oop(narrowOop* p) {
 70     ShouldNotReachHere();
 71   }
 72 
 73   bool is_unloading() const {
 74     return _is_unloading;
 75   }
 76 };
 77 
 78 class ShenandoahIsUnloadingBehaviour : public IsUnloadingBehaviour {
 79 public:
 80   virtual bool has_dead_oop(nmethod* nm) const {
 81     assert(ShenandoahHeap::heap()->is_concurrent_weak_root_in_progress(), "Only for this phase");
 82     ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
 83     ShenandoahNMethodLocker locker(data->lock());
 84     ShenandoahIsUnloadingOopClosure cl;
 85     data->oops_do(&cl, /* fix_relocations = */ false, /* icic = */ nullptr);
 86     return  cl.is_unloading();
 87   }
 88 };
 89 
 90 class ShenandoahCompiledICProtectionBehaviour : public CompiledICProtectionBehaviour {
 91 public:
 92   virtual bool lock(nmethod* nm) {
 93     ShenandoahNMethodLock* const lock = ShenandoahNMethod::ic_lock_for_nmethod(nm);
 94     assert(lock != nullptr, "Not yet registered?");
 95     lock->lock();
 96     return true;
 97   }
 98 
 99   virtual void unlock(nmethod* nm) {
100     ShenandoahNMethodLock* const lock = ShenandoahNMethod::ic_lock_for_nmethod(nm);
101     assert(lock != nullptr, "Not yet registered?");
102     lock->unlock();
103   }
104 
105   virtual bool is_safe(nmethod* nm) {
106     if (SafepointSynchronize::is_at_safepoint() || nm->is_unloading() || (NMethodState_lock->owned_by_self() && nm->is_not_installed())) {
107       return true;
108     }
109 
110     ShenandoahNMethodLock* const lock = ShenandoahNMethod::ic_lock_for_nmethod(nm);
111     assert(lock != nullptr, "Not yet registered?");
112     return lock->owned_by_self();
113   }
114 };
115 
116 ShenandoahUnload::ShenandoahUnload() {
117   if (ClassUnloading) {
118     static ShenandoahIsUnloadingBehaviour is_unloading_behaviour;
119     IsUnloadingBehaviour::set_current(&is_unloading_behaviour);
120 
121     static ShenandoahCompiledICProtectionBehaviour ic_protection_behaviour;
122     CompiledICProtectionBehaviour::set_current(&ic_protection_behaviour);
123   }
124 }
125 
126 void ShenandoahUnload::prepare() {
127   assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint");
128   assert(ClassUnloading, "Sanity");
129   CodeCache::increment_unloading_cycle();
130   DependencyContext::cleaning_start();
131 }
132 
133 void ShenandoahUnload::unload() {
134   ShenandoahHeap* heap = ShenandoahHeap::heap();
135   assert(ClassUnloading, "Filtered by caller");
136   assert(heap->is_concurrent_weak_root_in_progress(), "Filtered by caller");
137 
138   ClassUnloadingContext ctx(heap->workers()->active_workers(),
139                             true /* unregister_nmethods_during_purge */,
140                             true /* lock_nmethod_free_separately */);
141 
142   // Unlink stale metadata and nmethods
143   {
144     ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink);
145 
146     SuspendibleThreadSetJoiner sts;
147     bool unloadingOccurred;
148     {
149       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_sd);
150       MutexLocker cldgMl(ClassLoaderDataGraph_lock);
151       unloadingOccurred = SystemDictionary::do_unloading(heap->gc_timer());
152     }
153 
154     {
155       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_weak_klass);
156       Klass::clean_weak_klass_links(unloadingOccurred);
157     }
158 
159     {
160       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_code_roots);
161       ShenandoahCodeRoots::unlink(heap->workers(), unloadingOccurred);
162     }
163 
164     DependencyContext::cleaning_end();
165   }
166 
167   // Make sure stale metadata and nmethods are no longer observable
168   {
169     ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_rendezvous);
170     heap->rendezvous_threads("Shenandoah Class Unloading");
171   }
172 
173   // Purge stale metadata and nmethods that were unlinked
174   {
175     ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge);
176 
177     {
178       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_coderoots);
179       SuspendibleThreadSetJoiner sts;
180       ShenandoahCodeRoots::purge();
181     }
182 
183     {
184       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_cldg);
185       ClassLoaderDataGraph::purge(false /* at_safepoint */);
186     }
187 
188     {
189       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_ec);
190       CodeCache::purge_exception_caches();
191     }
192   }
193 }
194 
195 void ShenandoahUnload::finish() {
196   MetaspaceGC::compute_new_size();
197   DEBUG_ONLY(MetaspaceUtils::verify();)
198 }