2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * osi_alloc.c - Linux memory allocation routines.
14 #include <afsconfig.h>
15 #include "afs/param.h"
18 #include "afs/sysincludes.h"
19 #include "afsincludes.h"
23 #include "afs_atomlist.h"
24 #include "afs_lhash.h"
26 #define MAX_KMALLOC_SIZE PAGE_SIZE /* Max we should alloc with kmalloc */
27 #define MAX_BUCKET_LEN 30 /* max. no. of entries per buckets we expect to see */
28 #define STAT_INTERVAL 8192 /* we collect stats once every STAT_INTERVAL allocs */
31 #define KM_TYPE 1 /* kmalloc */
32 #define VM_TYPE 2 /* vmalloc */
34 struct osi_linux_mem {
38 /* These assume 32-bit pointers */
39 #define MEMTYPE(A) (((unsigned long)A) & 0x3)
40 #define MEMADDR(A) (void *)((unsigned long)(A) & (~0x3))
43 afs_atomlist *al_mem_pool; /* pool of osi_linux_mem structures */
44 afs_lhash *lh_mem_htab; /* mem hash table */
45 unsigned int allocator_init = 0; /* has the allocator been initialized? */
46 unsigned int afs_linux_cur_allocs = 0;
47 unsigned int afs_linux_total_allocs = 0;
48 unsigned int afs_linux_hash_verify_count = 0; /* used by hash_verify */
49 struct afs_lhash_stat afs_linux_lsb; /* hash table statistics */
50 unsigned int afs_linux_hash_bucket_dist[MAX_BUCKET_LEN]; /* bucket population distribution in our hash table */
52 #include "h/vmalloc.h"
54 /* Allocator support functions (static) */
57 hash_equal(const void *a, const void *b)
59 return (MEMADDR(((struct osi_linux_mem *)a)->chunk) ==
60 MEMADDR(((struct osi_linux_mem *)b)->chunk));
64 /* linux_alloc : Allocates memory from the linux kernel. It uses
65 * kmalloc if possible. Otherwise, we use vmalloc.
67 * asize - size of memory required in bytes
69 * returns NULL if we failed to allocate memory.
70 * or pointer to memory if we succeeded.
73 linux_alloc(unsigned int asize, int drop_glock)
77 int haveGlock = ISAFS_GLOCK();
79 /* if we can use kmalloc use it to allocate the required memory. */
80 while (!new && max_retry) {
81 if (asize <= MAX_KMALLOC_SIZE) {
82 new = (void *)(unsigned long)kmalloc(asize,
89 if (new) /* piggy back alloc type */
90 new = (void *)(KM_TYPE | (unsigned long)new);
92 osi_Assert(drop_glock || !haveGlock);
93 if (drop_glock && haveGlock)
95 new = (void *)vmalloc(asize);
96 if (drop_glock && haveGlock)
98 if (new) /* piggy back alloc type */
99 new = (void *)(VM_TYPE | (unsigned long)new);
103 #ifdef set_current_state
104 set_current_state(TASK_INTERRUPTIBLE);
106 current->state = TASK_INTERRUPTIBLE;
108 if (drop_glock && haveGlock)
110 schedule_timeout(HZ);
111 if (drop_glock && haveGlock)
113 #ifdef set_current_state
114 set_current_state(TASK_RUNNING);
116 current->state = TASK_RUNNING;
122 memset(MEMADDR(new), 0, asize);
131 /* mask out the type information from the pointer and
132 * use the appropriate free routine to free the chunk.
134 switch (MEMTYPE(p)) {
142 printf("afs_osi_Free: Asked to free unknown type %d at 0x%lx\n",
143 (int)MEMTYPE(p), (unsigned long)MEMADDR(p));
149 /* hash_chunk() receives a pointer to a chunk and hashes it to produce a
150 * key that the hashtable can use. The key is obtained by
151 * right shifting out the 2 LSBs and then multiplying the
152 * result by a constant no. and dividing it with a large prime.
154 #define HASH_CONST 32786
155 #define HASH_PRIME 79367
161 key = (unsigned int)(long)p >> 2;
162 key = (key * HASH_CONST) % HASH_PRIME;
167 /* hash_free() : Invoked by osi_linux_free_afs_memory(), thru
168 * afs_lhash_iter(), this function is called by the lhash
169 * module for every entry in the hash table. hash_free
170 * frees the memory associated with the entry as well
171 * as returning the osi_linux_mem struct to its pool.
174 hash_free(size_t index, unsigned key, void *data)
176 linux_free(((struct osi_linux_mem *)data)->chunk);
177 afs_atomlist_put(al_mem_pool, data);
180 /* hash_verify() is invoked by osi_linux_verify_alloced_memory() thru
181 * afs_lhash_iter() and is called by the lhash module for every element
183 * hash_verify() verifies (within limits) that the memory passed to it is
187 hash_verify(size_t index, unsigned key, void *data)
189 struct osi_linux_mem *lmp = (struct osi_linux_mem *)data;
192 memtype = MEMTYPE(lmp->chunk);
193 if (memtype != KM_TYPE && memtype != VM_TYPE) {
195 ("osi_linux_verify_alloced_memory: unknown type %d at 0x%lx, index=%lu\n",
196 (int)memtype, (unsigned long)lmp->chunk, (unsigned long)index);
198 afs_linux_hash_verify_count++;
202 /* local_free() : wrapper for vfree(), to deal with incompatible protoypes */
204 local_free(void *p, size_t n)
209 /* linux_alloc_init(): Initializes the kernel memory allocator. As part
210 * of this process, it also initializes a pool of osi_linux_mem
211 * structures as well as the hash table itself.
217 linux_alloc_init(void)
219 /* initiate our pool of osi_linux_mem structs */
221 afs_atomlist_create(sizeof(struct osi_linux_mem), sizeof(long) * 1024,
222 (void *)vmalloc, local_free);
224 printf("afs_osi_Alloc: Error in initialization(atomlist_create)\n");
228 /* initialize the hash table to hold references to alloc'ed chunks */
229 lh_mem_htab = afs_lhash_create(hash_equal, (void *)vmalloc, local_free);
231 printf("afs_osi_Alloc: Error in initialization(lhash_create)\n");
239 /* hash_bucket_stat() : Counts the no. of elements in each bucket and
240 * stores results in our bucket stats vector.
242 static unsigned int cur_bucket, cur_bucket_len;
244 hash_bucket_stat(size_t index, unsigned key, void *data)
246 if (index == cur_bucket) {
247 /* while still on the same bucket, inc len & return */
250 } else { /* if we're on the next bucket, store the distribution */
251 if (cur_bucket_len < MAX_BUCKET_LEN)
252 afs_linux_hash_bucket_dist[cur_bucket_len]++;
255 ("afs_get_hash_stats: Warning! exceeded max bucket len %d\n",
262 /* get_hash_stats() : get hash table statistics */
268 afs_lhash_stat(lh_mem_htab, &afs_linux_lsb);
270 /* clear out the bucket stat vector */
271 for (i = 0; i < MAX_BUCKET_LEN; i++, afs_linux_hash_bucket_dist[i] = 0);
272 cur_bucket = cur_bucket_len = 00;
274 /* populate the bucket stat vector */
275 afs_lhash_iter(lh_mem_htab, hash_bucket_stat);
278 /************** Linux memory allocator interface functions **********/
280 DECLARE_MUTEX(afs_linux_alloc_sem);
283 osi_linux_alloc(unsigned int asize, int drop_glock)
286 struct osi_linux_mem *lmem;
288 new = linux_alloc(asize, drop_glock); /* get a chunk of memory of size asize */
291 printf("afs_osi_Alloc: Can't vmalloc %d bytes.\n", asize);
295 down(&afs_linux_alloc_sem);
297 /* allocator hasn't been initialized yet */
298 if (allocator_init == 0) {
299 if (linux_alloc_init() == 0) {
302 allocator_init = 1; /* initialization complete */
305 /* get an atom to store the pointer to the chunk */
306 lmem = (struct osi_linux_mem *)afs_atomlist_get(al_mem_pool);
308 printf("afs_osi_Alloc: atomlist_get() failed.");
311 /* store the chunk reference */
314 /* hash in the chunk */
315 if (afs_lhash_enter(lh_mem_htab, hash_chunk(new), lmem) != 0) {
316 printf("afs_osi_Alloc: lhash_enter failed\n");
319 afs_linux_cur_allocs++; /* no. of current allocations */
320 afs_linux_total_allocs++; /* total no. of allocations done so far */
321 if ((afs_linux_cur_allocs % STAT_INTERVAL) == 0) {
325 up(&afs_linux_alloc_sem);
338 /* osi_linux_free() - free chunk of memory passed to us.
341 osi_linux_free(void *addr)
343 struct osi_linux_mem lmem, *lmp;
345 down(&afs_linux_alloc_sem);
348 /* remove this chunk from our hash table */
350 (struct osi_linux_mem *)afs_lhash_remove(lh_mem_htab,
351 hash_chunk(addr), &lmem))) {
352 linux_free(lmp->chunk); /* this contains the piggybacked type info */
353 afs_atomlist_put(al_mem_pool, lmp); /* return osi_linux_mem struct to pool */
354 afs_linux_cur_allocs--;
357 printf("osi_linux_free: failed to remove chunk from hashtable\n");
360 up(&afs_linux_alloc_sem);
363 /* osi_linux_free_afs_memory() - free all chunks of memory allocated.
366 osi_linux_free_afs_memory(void)
368 down(&afs_linux_alloc_sem);
370 if (allocator_init) {
371 /* iterate through all elements in the hash table and free both
372 * the chunk and the atom associated with it.
374 afs_lhash_iter(lh_mem_htab, hash_free);
376 /* free the atomlist. */
377 afs_atomlist_destroy(al_mem_pool);
379 /* free the hashlist. */
380 afs_lhash_destroy(lh_mem_htab);
382 /* change the state so that the allocator is now uninitialized. */
385 up(&afs_linux_alloc_sem);
388 /* osi_linux_verify_alloced_memory(): verify all chunks of alloced memory in
392 osi_linux_verify_alloced_memory()
394 down(&afs_linux_alloc_sem);
396 /* count of times hash_verify was called. reset it to 0 before iteration */
397 afs_linux_hash_verify_count = 0;
399 /* iterate thru elements in the hash table */
400 afs_lhash_iter(lh_mem_htab, hash_verify);
402 if (afs_linux_hash_verify_count != afs_linux_cur_allocs) {
403 /* hmm, some pieces of memory are missing. */
405 ("osi_linux_verify_alloced_memory: %d chunks of memory are not accounted for during verify!\n",
406 afs_linux_hash_verify_count - afs_linux_cur_allocs);
409 up(&afs_linux_alloc_sem);