Memory Management API: kmem_cache_destroy,
Memory Management API: kmem_cache_destroy
Void destroy (struct kmem_cache * s) is used to destroy the cache created through kmem_cache_create. Its source code analysis is as follows: void kmem_cache_destroy (struct kmem_cache * s) {int err; # The parameter s is null, exit directly, indicating that the parameter kmem_cache_destroy can be nullif (unlikely (! S) return; # Use get_online_mems and put_online_cpus together with put_online_cpus (); get_online_mems (); mutex_lock (& slab_mutex); # The cached application count is reduced by 1, if the reference count after the value of 1 is not zero, it indicates that someone else is using this cache and will exit directly. s-> refcount --; if (s-> refcount) goto out_unlock; # if the reference count is already zero, destroy memcg first. if yes, err is equal to zero, finally, use shutdown_cache to destroy the cache err = shutdown_memcg_caches (s); if (! Err) err = shutdown_cache (s); if (err) {pr_err ("kmem_cache_destroy % s: Slab cache still has objects \ n", s-> name ); dump_stack ();} out_unlock: mutex_unlock (& slab_mutex); put_online_mems (); put_online_cpus () ;}# destroy static int shutdown_cache (struct kmem_cache * s) {/* free export quarantined objects */kasan_cache_shutdown (s); # release all resources occupied by slab if (_ kmem_cache_shutdown (s )! = 0) return-EBUSY; # unlink and delete the listmemcg_unlink_cache (s); list_del (& s-> list); # If rcu exists, release if (s-> flags & SLAB_TYPESAFE_BY_RCU) {list_add_tail (& s-> list, & slab_caches_to_rcu_destroy); schedule_work (& activities );} else {# ifdef SLAB_SUPPORTS_SYSFS # reference count of kobject minus 1sysfs_slab_release (s); # elseslab_kmem_cache_release (s); # endif} return 0 ;} # Release the void Merge (struct kmem_cache * s) {__ kmem_cache_release (s); merge (s); kfree_const (s-> name); kmem_cache_free (kmem_cache, s );}