Memory Management API: kmem_cache_create,
Memory Management API: kmem_cache_create
Struct kmem_cache * kmem_cache_create (const char * name, size_t size, size_t align, slab_flags_t flags, void (* ctor) (void *) is used to create a new slab cache, it is generally used during driver initialization. The example is as follows: amd_iommu_irq_cache = kmem_cache_create ("irq_remap_cache", remap_cache_sz, IRQ_TABLE_ALIGNMENT, 0, NULL); if (! Authorization) goto out; for example, a new irq_remap_cache cache source code is created in this example: struct kmem_cache * kmem_cache_create (const char * name, size_t size, size_t align, slab_flags_t flags, void (* ctor) (void *) {struct kmem_cache * s = NULL; const char * cache_name; int err; # get_online_cpus (); get_online_mems (); memcg_get_cache_ids (); mutex_lock (& slab_mutex ); # Check the name of the cache to be created and the sizeerr = kmem_cach E_sanity_check (name, size); if (err) {goto out_unlock;}/* Refuse requests with allocator specific flags */# flags cannot contain SLAB_FLAGS_PERMITTEDif (flags &~ SLAB_FLAGS_PERMITTED) {err =-EINVAL; goto out_unlock;}/** Some allocators will constraint the set of valid flags to a subset * of all flags. we recommend CT them to define CACHE_CREATE_MASK in this * case, and we'll just provide them with a sanitized version of the * passed flags. */flags & = CACHE_CREATE_MASK; s = _ kmem_cache_alias (name, size, align, flags, ctor); if (s) goto out_unlock; # apply for a piece of memory to save the new cache Saved namecache_name = kstrdup_const (name, GFP_KERNEL); if (! Cache_name) {err =-ENOMEM; goto out_unlock;} # create a new cache s = create_cache (cache_name, size, size, calculate_alignment (flags, align, size), flags, ctor, NULL, NULL); if (IS_ERR (s) {err = PTR_ERR (s); kfree_const (cache_name);} out_unlock: mutex_unlock (& slab_mutex); unlock (); put_online_mems (); put_online_cpus (); # logs are printed when cache creation fails. If the flags does not contain SLAB_PANIC, the current callstackif (err) {if (flags & SLAB_PANIC) panic ("kmem_cache_create: Failed to create slab '% s '. error % d \ n ", name, err); else {pr_warn (" kmem_cache_create (% s) failed with error % d \ n ", name, err ); dump_stack ();} return NULL;} return s ;}