Freeing Objects

When an allocated object is no longer required, it must be returned to the slab allocator using kmem_cache_free. Figure 3-52 shows the code flow diagram of this function.

kmem_cache_free immediately invokes_cache_free and forwards its arguments unchanged.

(Again the reason is to prevent code duplication in the implementation of kfree, as discussed in Section 3.6.5.)

As with allocation, there are two alternative courses of action depending on the state of the per-CPU cache. If the number of objects held is below the permitted limit, a pointer to the object in the cache is stored.

Figure 3-52: Code flow diagram for kmem_cache_free.

mm/slab.c static inline void _cache_free(kmem_cache_t *cachep, void *objp)

cache_flusharray(cachep, ac); ac->entry[ac->avail++] = objp;

If not, some objects (the exact number is given by array_cache->batchcount) must be moved from the cache back into the slabs starting with the array elements with the lowest numbers — because the cache implementation applies the LIFO principle, these are objects that have been in the array longest and whose data are therefore least likely still to be held in the CPU cache.

Implementation is delegated to cache_flusharray. In turn, this function invokes free_block to move the objects from the cache to their original slabs and shifts the remaining objects in the cache to the start of the array. For example, if there is space for 30 objects in the cache and the batchcount is 15, the objects at positions 0 to 14 are moved back into the slabs. The remaining objects numbered 15 to 29 are shifted upward in the cache so that they now occupy positions 0 to 14.

Moving objects from the cache back onto the slabs is instructive, so it's well worth taking a closer look at free_block. The arguments required by this function are the kmem_cache_t instance of the cache, a pointer to an array consisting of pointers to the objects in the cache, an integer to indicate the number of objects in the array, and the node whose memory is just being processed.

The function iterates over all objects in objpp after the number of unused objects in the cache data structure has been updated.

mm/slab.c static void free_block(kmem_cache_t *cachep, void **objpp, int nr_objects, int node)

struct kmem_list3 *l3;

for (i = 0; i < nr_objects; i++) { void *objp = objpp[i]; struct slab *slabp;

The following operations must be performed for each object: mm/slab.c slabp = virt_to_slab(objp) l3 = cachep->nodelists[node]; list_del(&slabp->list);

slab_put_obj(cachep, slabp, objp, node);

Before it can be established to which slab an object belongs, it is first necessary to invoke virt_to_page to find the page in which the object is located. The association with the slab is determined using page_get_slab as discussed above.

The slab is (temporarily) removed from the lists of the cache. slab_put_obj is used to reflect this action in the freelist: The first object to be used for allocation purposes is the one just removed, and the next object in the list is the one that was previously first.

Thereafter, the slab is reinserted in the linked lists of the cache: mm/slab.c

if (l3->free_objects > l3->free_limit) { l3->free_objects -= cachep->num; slab_destroy(cachep, slabp);

list_add(&slabp->list, &l3->slabs_free);

list_add(&slabp->list, &l3->slabs_partial);

The slab is normally placed on the slabs_free list if, after deletion, all objects in the slab are unused (slab->inuse == 0).

Exception: The number of free objects in the cache is above the predefined limit cachep->free_limit. In this case, the complete slab is returned to the buddy system using slab_destroy.

The slab is inserted into the slabs_partial list of the cache if it contains both used and unused objects.

Destroying Caches

The kmem_cache_destroy function must be invoked to destroy a complete cache in which there are only unused objects. This function is needed primarily when removing modules that want to return all memory space allocated to them.36

Since the implementation itself reveals nothing new, we will confine ourselves to outlining the main steps needed to remove a cache:

□ The slabs on the slabs_free list are scanned successively. The destructor is first invoked for each object on each slab, and then the slab memory space is returned to the buddy system.

□ The memory space for the per-CPU caches is freed.

□ The data are removed from the cache_cache list.

Continue reading here: General Caches

Was this article helpful?

0 0