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"
19 #include "../afs/sysincludes.h"
20 #include "../afs/afsincludes.h"
23 #include "../afs/afs_atomlist.h"
24 #include "../afs/afs_lhash.h"
26 #define MAX_KMALLOC_SIZE (131072-16) /* Max we can alloc in physmem */
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 #if defined(AFS_LINUX24_ENV)
53 #include "../h/vmalloc.h"
55 /* externs : can we do this in a better way. Including vmalloc.h causes other
57 extern void vfree(void * addr);
58 extern void *vmalloc(unsigned long size);
61 /* Allocator support functions (static) */
63 static int hash_equal(const void *a, const void *b)
65 return ( MEMADDR(((struct osi_linux_mem *)a)->chunk) ==
66 MEMADDR(((struct osi_linux_mem *)b)->chunk) );
70 /* linux_alloc : Allocates memory from the linux kernel. It uses
71 * kmalloc if possible. Otherwise, we use vmalloc.
73 * asize - size of memory required in bytes
75 * returns NULL if we failed to allocate memory.
76 * or pointer to memory if we succeeded.
78 static void *linux_alloc(unsigned int asize)
81 int has_afs_glock = ISAFS_GLOCK();
83 /* if global lock has been held save this info and unlock it. */
87 /* if we can use kmalloc use it to allocate the required memory. */
88 if (asize < MAX_KMALLOC_SIZE) {
89 new = (void *)(long)kmalloc(asize, GFP_KERNEL);
90 if (new) /* piggy back alloc type */
91 (unsigned long)new |= KM_TYPE;
93 if (!new) { /* otherwise use vmalloc */
95 while (!(new = (void *)vmalloc(asize))) {
101 if (new) /* piggy back alloc type */
102 (unsigned long)new |= VM_TYPE;
105 memset(MEMADDR(new), 0, asize);
107 /* if the global lock had been held, lock it again. */
114 static void linux_free(void *p)
117 /* mask out the type information from the pointer and
118 * use the appropriate free routine to free the chunk.
128 printf("afs_osi_Free: Asked to free unknown type %d at 0x%x\n",
129 MEMTYPE(p), MEMADDR(p));
135 /* hash_chunk() receives a pointer to a chunk and hashes it to produce a
136 * key that the hashtable can use. The key is obtained by
137 * right shifting out the 2 LSBs and then multiplying the
138 * result by a constant no. and dividing it with a large prime.
140 #define HASH_CONST 32786
141 #define HASH_PRIME 79367
142 static unsigned hash_chunk(void *p)
146 key = (unsigned int)(long)p >> 2;
147 key = (key * HASH_CONST)%HASH_PRIME;
152 /* hash_free() : Invoked by osi_linux_free_afs_memory(), thru
153 * afs_lhash_iter(), this function is called by the lhash
154 * module for every entry in the hash table. hash_free
155 * frees the memory associated with the entry as well
156 * as returning the osi_linux_mem struct to its pool.
159 hash_free(size_t index, unsigned key, void *data)
161 linux_free(((struct osi_linux_mem *)data)->chunk);
162 afs_atomlist_put(al_mem_pool, data);
165 /* hash_verify() is invoked by osi_linux_verify_alloced_memory() thru
166 * afs_lhash_iter() and is called by the lhash module for every element
168 * hash_verify() verifies (within limits) that the memory passed to it is
172 hash_verify(size_t index, unsigned key, void *data)
174 struct osi_linux_mem *lmp = (struct osi_linux_mem *)data;
177 memtype = MEMTYPE(lmp->chunk);
178 if ((memtype == KM_TYPE) && (AFS_LINUX_MAP_NR(lmp->chunk) > max_mapnr)) {
179 printf("osi_linux_verify_alloced_memory: address 0x%x outside range, index=%d, key=%d\n", lmp->chunk, index, key);
182 if (memtype != KM_TYPE && memtype != VM_TYPE) {
183 printf("osi_linux_verify_alloced_memory: unknown type %d at 0x%x, index=%d\n", memtype, lmp->chunk, index);
185 afs_linux_hash_verify_count++;
189 /* local_free() : wrapper for vfree(), to deal with incompatible protoypes */
190 static void local_free(void *p, size_t n)
195 /* linux_alloc_init(): Initializes the kernel memory allocator. As part
196 * of this process, it also initializes a pool of osi_linux_mem
197 * structures as well as the hash table itself.
202 static int linux_alloc_init()
204 /* initiate our pool of osi_linux_mem structs */
205 al_mem_pool = afs_atomlist_create(sizeof(struct osi_linux_mem),
206 sizeof(long)*1024, (void *)vmalloc,
209 printf("afs_osi_Alloc: Error in initialization(atomlist_create)\n");
213 /* initialize the hash table to hold references to alloc'ed chunks */
214 lh_mem_htab = afs_lhash_create(hash_equal, (void *)vmalloc, local_free);
216 printf("afs_osi_Alloc: Error in initialization(lhash_create)\n");
224 /* hash_bucket_stat() : Counts the no. of elements in each bucket and
225 * stores results in our bucket stats vector.
227 static unsigned int cur_bucket, cur_bucket_len;
228 static void hash_bucket_stat(size_t index, unsigned key, void *data)
230 if (index == cur_bucket) {
231 /* while still on the same bucket, inc len & return */
235 else { /* if we're on the next bucket, store the distribution */
236 if (cur_bucket_len < MAX_BUCKET_LEN)
237 afs_linux_hash_bucket_dist[cur_bucket_len]++;
239 printf("afs_get_hash_stats: Warning! exceeded max bucket len %d\n", cur_bucket_len);
244 /* get_hash_stats() : get hash table statistics */
245 static void get_hash_stats()
249 afs_lhash_stat(lh_mem_htab, &afs_linux_lsb);
251 /* clear out the bucket stat vector */
252 for(i=0;i<MAX_BUCKET_LEN;i++, afs_linux_hash_bucket_dist[i]=0);
253 cur_bucket = cur_bucket_len = 00;
255 /* populate the bucket stat vector */
256 afs_lhash_iter(lh_mem_htab, hash_bucket_stat);
259 /************** Linux memory allocator interface functions **********/
261 #if defined(AFS_LINUX24_ENV)
262 DECLARE_MUTEX(afs_linux_alloc_sem);
264 struct semaphore afs_linux_alloc_sem = MUTEX;
267 void *osi_linux_alloc(unsigned int asize)
270 struct osi_linux_mem *lmem;
272 down(&afs_linux_alloc_sem);
274 if (allocator_init == 0) { /* allocator hasn't been initialized yet */
275 if (linux_alloc_init() == 0) {
278 allocator_init = 1; /* initialization complete */
281 new = linux_alloc(asize); /* get a chunk of memory of size asize */
283 printf("afs_osi_Alloc: Can't vmalloc %d bytes.\n", asize);
287 /* get an atom to store the pointer to the chunk */
288 lmem = (struct osi_linux_mem *)afs_atomlist_get(al_mem_pool);
290 printf("afs_osi_Alloc: atomlist_get() failed.");
293 /* store the chunk reference */
296 /* hash in the chunk */
297 if (afs_lhash_enter(lh_mem_htab, hash_chunk(new), lmem) != 0) {
298 printf("afs_osi_Alloc: lhash_enter failed\n");
301 afs_linux_cur_allocs++; /* no. of current allocations */
302 afs_linux_total_allocs++; /* total no. of allocations done so far */
303 if ((afs_linux_cur_allocs % STAT_INTERVAL) == 0) {
307 up(&afs_linux_alloc_sem);
319 /* osi_linux_free() - free chunk of memory passed to us.
321 void osi_linux_free(void *addr)
323 struct osi_linux_mem lmem, *lmp;
325 down(&afs_linux_alloc_sem);
328 /* remove this chunk from our hash table */
329 if ( lmp = (struct osi_linux_mem *)afs_lhash_remove(lh_mem_htab, hash_chunk(addr), &lmem)) {
330 linux_free(lmp->chunk); /* this contains the piggybacked type info*/
331 afs_atomlist_put(al_mem_pool, lmp); /* return osi_linux_mem struct to pool*/
332 afs_linux_cur_allocs--;
335 printf("osi_linux_free: failed to remove chunk from hashtable\n");
338 up(&afs_linux_alloc_sem);
341 /* osi_linux_free_afs_memory() - free all chunks of memory allocated.
343 void osi_linux_free_afs_memory(void)
345 down(&afs_linux_alloc_sem);
347 if (allocator_init) {
348 /* iterate through all elements in the hash table and free both
349 * the chunk and the atom associated with it.
351 afs_lhash_iter(lh_mem_htab, hash_free);
353 /* free the atomlist. */
354 afs_atomlist_destroy(al_mem_pool);
356 /* free the hashlist. */
357 afs_lhash_destroy(lh_mem_htab);
359 /* change the state so that the allocator is now uninitialized. */
362 up(&afs_linux_alloc_sem);
365 /* osi_linux_verify_alloced_memory(): verify all chunks of alloced memory in
368 void osi_linux_verify_alloced_memory()
370 down(&afs_linux_alloc_sem);
372 /* count of times hash_verify was called. reset it to 0 before iteration */
373 afs_linux_hash_verify_count = 0;
375 /* iterate thru elements in the hash table */
376 afs_lhash_iter(lh_mem_htab, hash_verify);
378 if (afs_linux_hash_verify_count != afs_linux_cur_allocs) {
379 /* hmm, some pieces of memory are missing. */
380 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);
383 up(&afs_linux_alloc_sem);