Memory Management API: kmem_cache_free, apikmem_cache_free
Memory Management API: kmem_cache_free
Void kmem_cache_free (struct kmem_cache * cache, void * objp) is used to release the memory applied for by kmem_cache_alloc. The source code analysis is as follows: void kmem_cache_free (struct kmem_cache * cache, void * objp) {unsigned long flags; # This function is mainly used to check whether the slab is valid cache_from_obj (caclap, objp); if (! Cache) return; # When mem is released, irqlocal_irq_save (flags) of the current cpu is disabled; debug_check_no_locks_freed (objp, cache-> object_size); if (! (Cache-> flags & SLAB_DEBUG_OBJECTS) release (objp, cache-> object_size); # Release mem _ cache_free (cache, objp, _ RET_IP _); local_irq_restore (flags ); trace_kmem_cache_free (_ RET_IP _, objp);} static inline struct kmem_cache * cache_from_obj (struct kmem_cache * s, void * x) {struct kmem_cache * caclap; struct page * page; /** When kmemcg is not being used, both assignments shoshould return the * same valu E. but we don't want to pay the assignment price in that * case. if it is not compiled in, the compiler shocould be smart enough * to not do even the assignment. in that case, slab_assist_or_root * will also be a constant. */# Here we will judge # if defined (CONFIG_MEMCG )&&! Defined (CONFIG_SLOB) is the condition true? # In my system, this condition is true, so the if condition below will not be true if (! Memcg_kmem_enabled ()&&! Unlikely (s-> flags & volumes) return s; # obtain the pagepage = pai_to_head_page (x) of the slab Based on the parameter x; # obtain slab_cachecachu = page-> slab_cache Based on the page; # Here, we will compare the slab pointer obtained from x with the slab represented by s. If not, return the slab of s to release it. # If they are equal, then, the slab pointer obtained from the form parameter x is released. It doesn't matter who is returned when they are equal. if (slab_1__or_root (cache, s) return cache; pr_err ("% s: Wrong slab cache. % s but object is from % s \ n ", _ func __, s-> name, cachu-> name); WARN_ON_ONCE (1); return s ;}