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 "../afs/param.h"
15 #include "../afs/sysincludes.h"
16 #include "../afs/afsincludes.h"
19 #include "../afs/afs_atomlist.h"
20 #include "../afs/afs_lhash.h"
22 #define MAX_KMALLOC_SIZE (131072-16) /* Max we can alloc in physmem */
23 #define MAX_BUCKET_LEN 30 /* max. no. of entries per buckets we expect to see */
24 #define STAT_INTERVAL 8192 /* we collect stats once every STAT_INTERVAL allocs*/
27 #define KM_TYPE 1 /* kmalloc */
28 #define VM_TYPE 2 /* vmalloc */
30 struct osi_linux_mem {
34 /* These assume 32-bit pointers */
35 #define MEMTYPE(A) (((unsigned int)A) & 0x3)
36 #define MEMADDR(A) (void *)((unsigned int)(A) & (~0x3))
39 afs_atomlist *al_mem_pool; /* pool of osi_linux_mem structures */
40 afs_lhash *lh_mem_htab; /* mem hash table */
41 unsigned int allocator_init = 0; /* has the allocator been initialized? */
42 unsigned int afs_linux_cur_allocs = 0;
43 unsigned int afs_linux_total_allocs = 0;
44 unsigned int afs_linux_hash_verify_count = 0; /* used by hash_verify */
45 struct afs_lhash_stat afs_linux_lsb; /* hash table statistics */
46 unsigned int afs_linux_hash_bucket_dist[MAX_BUCKET_LEN]; /* bucket population distribution in our hash table */
48 #if defined(AFS_LINUX24_ENV)
49 #include "../h/vmalloc.h"
51 /* externs : can we do this in a better way. Including vmalloc.h causes other
53 extern void vfree(void * addr);
54 extern void *vmalloc(unsigned long size);
57 /* Allocator support functions (static) */
59 static int hash_equal(const void *a, const void *b)
61 return ( MEMADDR(((struct osi_linux_mem *)a)->chunk) ==
62 MEMADDR(((struct osi_linux_mem *)b)->chunk) );
66 /* linux_alloc : Allocates memory from the linux kernel. It uses
67 * kmalloc if possible. Otherwise, we use vmalloc.
69 * asize - size of memory required in bytes
71 * returns NULL if we failed to allocate memory.
72 * or pointer to memory if we succeeded.
74 static void *linux_alloc(unsigned int asize)
77 int has_afs_glock = ISAFS_GLOCK();
79 /* if global lock has been held save this info and unlock it. */
83 /* if we can use kmalloc use it to allocate the required memory. */
84 if (asize < MAX_KMALLOC_SIZE) {
85 new = (void *)kmalloc(asize, GFP_KERNEL);
86 if (new) /* piggy back alloc type */
87 (unsigned int)new |= KM_TYPE;
89 if (!new) { /* otherwise use vmalloc */
91 while (!(new = (void *)vmalloc(asize))) {
97 if (new) /* piggy back alloc type */
98 (unsigned int)new |= VM_TYPE;
101 memset(MEMADDR(new), 0, asize);
103 /* if the global lock had been held, lock it again. */
110 static void linux_free(void *p)
113 /* mask out the type information from the pointer and
114 * use the appropriate free routine to free the chunk.
124 printf("afs_osi_Free: Asked to free unknown type %d at 0x%x\n",
125 MEMTYPE(p), MEMADDR(p));
131 /* hash_chunk() receives a pointer to a chunk and hashes it to produce a
132 * key that the hashtable can use. The key is obtained by
133 * right shifting out the 2 LSBs and then multiplying the
134 * result by a constant no. and dividing it with a large prime.
136 #define HASH_CONST 32786
137 #define HASH_PRIME 79367
138 static unsigned hash_chunk(void *p)
142 key = (unsigned int)p >> 2;
143 key = (key * HASH_CONST)%HASH_PRIME;
148 /* hash_free() : Invoked by osi_linux_free_afs_memory(), thru
149 * afs_lhash_iter(), this function is called by the lhash
150 * module for every entry in the hash table. hash_free
151 * frees the memory associated with the entry as well
152 * as returning the osi_linux_mem struct to its pool.
155 hash_free(size_t index, unsigned key, void *data)
157 linux_free(((struct osi_linux_mem *)data)->chunk);
158 afs_atomlist_put(al_mem_pool, data);
161 /* hash_verify() is invoked by osi_linux_verify_alloced_memory() thru
162 * afs_lhash_iter() and is called by the lhash module for every element
164 * hash_verify() verifies (within limits) that the memory passed to it is
168 hash_verify(size_t index, unsigned key, void *data)
170 struct osi_linux_mem *lmp = (struct osi_linux_mem *)data;
173 memtype = MEMTYPE(lmp->chunk);
174 if ((memtype == KM_TYPE) && (AFS_LINUX_MAP_NR(lmp->chunk) > max_mapnr)) {
175 printf("osi_linux_verify_alloced_memory: address 0x%x outside range, index=%d, key=%d\n", lmp->chunk, index, key);
178 if (memtype != KM_TYPE && memtype != VM_TYPE) {
179 printf("osi_linux_verify_alloced_memory: unknown type %d at 0x%x, index=%d\n", memtype, lmp->chunk, index);
181 afs_linux_hash_verify_count++;
185 /* local_free() : wrapper for vfree(), to deal with incompatible protoypes */
186 static void local_free(void *p, size_t n)
191 /* linux_alloc_init(): Initializes the kernel memory allocator. As part
192 * of this process, it also initializes a pool of osi_linux_mem
193 * structures as well as the hash table itself.
198 static int linux_alloc_init()
200 /* initiate our pool of osi_linux_mem structs */
201 al_mem_pool = afs_atomlist_create(sizeof(struct osi_linux_mem),
202 sizeof(long)*1024, vmalloc, local_free);
204 printf("afs_osi_Alloc: Error in initialization(atomlist_create)\n");
208 /* initialize the hash table to hold references to alloc'ed chunks */
209 lh_mem_htab = afs_lhash_create(hash_equal, vmalloc, local_free);
211 printf("afs_osi_Alloc: Error in initialization(lhash_create)\n");
219 /* hash_bucket_stat() : Counts the no. of elements in each bucket and
220 * stores results in our bucket stats vector.
222 static unsigned int cur_bucket, cur_bucket_len;
223 static void hash_bucket_stat(size_t index, unsigned key, void *data)
225 if (index == cur_bucket) {
226 /* while still on the same bucket, inc len & return */
230 else { /* if we're on the next bucket, store the distribution */
231 if (cur_bucket_len < MAX_BUCKET_LEN)
232 afs_linux_hash_bucket_dist[cur_bucket_len]++;
234 printf("afs_get_hash_stats: Warning! exceeded max bucket len %d\n", cur_bucket_len);
239 /* get_hash_stats() : get hash table statistics */
240 static void get_hash_stats()
244 afs_lhash_stat(lh_mem_htab, &afs_linux_lsb);
246 /* clear out the bucket stat vector */
247 for(i=0;i<MAX_BUCKET_LEN;i++, afs_linux_hash_bucket_dist[i]=0);
248 cur_bucket = cur_bucket_len = 00;
250 /* populate the bucket stat vector */
251 afs_lhash_iter(lh_mem_htab, hash_bucket_stat);
254 /************** Linux memory allocator interface functions **********/
256 #if defined(AFS_LINUX24_ENV)
257 DECLARE_MUTEX(afs_linux_alloc_sem);
259 struct semaphore afs_linux_alloc_sem = MUTEX;
262 void *osi_linux_alloc(unsigned int asize)
265 struct osi_linux_mem *lmem;
267 down(&afs_linux_alloc_sem);
269 if (allocator_init == 0) { /* allocator hasn't been initialized yet */
270 if (linux_alloc_init() == 0) {
273 allocator_init = 1; /* initialization complete */
276 new = linux_alloc(asize); /* get a chunk of memory of size asize */
278 printf("afs_osi_Alloc: Can't vmalloc %d bytes.\n", asize);
282 /* get an atom to store the pointer to the chunk */
283 lmem = (struct osi_linux_mem *)afs_atomlist_get(al_mem_pool);
285 printf("afs_osi_Alloc: atomlist_get() failed.");
288 /* store the chunk reference */
291 /* hash in the chunk */
292 if (afs_lhash_enter(lh_mem_htab, hash_chunk(new), lmem) != 0) {
293 printf("afs_osi_Alloc: lhash_enter failed\n");
296 afs_linux_cur_allocs++; /* no. of current allocations */
297 afs_linux_total_allocs++; /* total no. of allocations done so far */
298 if ((afs_linux_cur_allocs % STAT_INTERVAL) == 0) {
302 up(&afs_linux_alloc_sem);
314 /* osi_linux_free() - free chunk of memory passed to us.
316 void osi_linux_free(void *addr)
318 struct osi_linux_mem lmem, *lmp;
320 down(&afs_linux_alloc_sem);
323 /* remove this chunk from our hash table */
324 if ( lmp = (struct osi_linux_mem *)afs_lhash_remove(lh_mem_htab, hash_chunk(addr), &lmem)) {
325 linux_free(lmp->chunk); /* this contains the piggybacked type info*/
326 afs_atomlist_put(al_mem_pool, lmp); /* return osi_linux_mem struct to pool*/
327 afs_linux_cur_allocs--;
330 printf("osi_linux_free: failed to remove chunk from hashtable\n");
333 up(&afs_linux_alloc_sem);
336 /* osi_linux_free_afs_memory() - free all chunks of memory allocated.
338 void osi_linux_free_afs_memory(void)
340 down(&afs_linux_alloc_sem);
342 if (allocator_init) {
343 /* iterate through all elements in the hash table and free both
344 * the chunk and the atom associated with it.
346 afs_lhash_iter(lh_mem_htab, hash_free);
348 /* free the atomlist. */
349 afs_atomlist_destroy(al_mem_pool);
351 /* free the hashlist. */
352 afs_lhash_destroy(lh_mem_htab);
354 /* change the state so that the allocator is now uninitialized. */
357 up(&afs_linux_alloc_sem);
360 /* osi_linux_verify_alloced_memory(): verify all chunks of alloced memory in
363 void osi_linux_verify_alloced_memory()
365 down(&afs_linux_alloc_sem);
367 /* count of times hash_verify was called. reset it to 0 before iteration */
368 afs_linux_hash_verify_count = 0;
370 /* iterate thru elements in the hash table */
371 afs_lhash_iter(lh_mem_htab, hash_verify);
373 if (afs_linux_hash_verify_count != afs_linux_cur_allocs) {
374 /* hmm, some pieces of memory are missing. */
375 printf("osi_linux_verify_alloced_memory: %d chunks of memory are not accounted for during verify!\n", afs_linux_hash_verify_count - afs_linux_cur_allocs);
378 up(&afs_linux_alloc_sem);