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"
22 #include "../h/slab.h"
24 #include "../afs/afs_atomlist.h"
25 #include "../afs/afs_lhash.h"
27 #define MAX_KMALLOC_SIZE AFS_SMALLOCSIZ /* Max we should alloc with kmalloc */
28 #define MAX_BUCKET_LEN 30 /* max. no. of entries per buckets we expect to see */
29 #define STAT_INTERVAL 8192 /* we collect stats once every STAT_INTERVAL allocs*/
32 #define KM_TYPE 1 /* kmalloc */
33 #define VM_TYPE 2 /* vmalloc */
35 struct osi_linux_mem {
39 /* These assume 32-bit pointers */
40 #define MEMTYPE(A) (((unsigned long)A) & 0x3)
41 #define MEMADDR(A) (void *)((unsigned long)(A) & (~0x3))
44 afs_atomlist *al_mem_pool; /* pool of osi_linux_mem structures */
45 afs_lhash *lh_mem_htab; /* mem hash table */
46 unsigned int allocator_init = 0; /* has the allocator been initialized? */
47 unsigned int afs_linux_cur_allocs = 0;
48 unsigned int afs_linux_total_allocs = 0;
49 unsigned int afs_linux_hash_verify_count = 0; /* used by hash_verify */
50 struct afs_lhash_stat afs_linux_lsb; /* hash table statistics */
51 unsigned int afs_linux_hash_bucket_dist[MAX_BUCKET_LEN]; /* bucket population distribution in our hash table */
53 #if defined(AFS_LINUX24_ENV)
54 #include "../h/vmalloc.h"
56 /* externs : can we do this in a better way. Including vmalloc.h causes other
58 extern void vfree(void * addr);
59 extern void *vmalloc(unsigned long size);
62 /* Allocator support functions (static) */
64 static int hash_equal(const void *a, const void *b)
66 return ( MEMADDR(((struct osi_linux_mem *)a)->chunk) ==
67 MEMADDR(((struct osi_linux_mem *)b)->chunk) );
71 /* linux_alloc : Allocates memory from the linux kernel. It uses
72 * kmalloc if possible. Otherwise, we use vmalloc.
74 * asize - size of memory required in bytes
76 * returns NULL if we failed to allocate memory.
77 * or pointer to memory if we succeeded.
79 static void *linux_alloc(unsigned int asize)
84 /* if we can use kmalloc use it to allocate the required memory. */
85 while(!new && max_retry)
87 if (asize <= MAX_KMALLOC_SIZE) {
88 new = (void *)(unsigned long)kmalloc(asize,
95 if (new) /* piggy back alloc type */
96 (unsigned long)new |= KM_TYPE;
98 new = (void *)vmalloc(asize);
99 if (new) /* piggy back alloc type */
100 (unsigned long)new |= VM_TYPE;
104 #ifdef set_current_state
105 set_current_state(TASK_INTERRUPTIBLE);
107 current->state = TASK_INTERRUPTIBLE;
109 schedule_timeout(HZ);
110 #ifdef set_current_state
111 set_current_state(TASK_RUNNING);
113 current->state = TASK_RUNNING;
119 memset(MEMADDR(new), 0, asize);
124 static void linux_free(void *p)
127 /* mask out the type information from the pointer and
128 * use the appropriate free routine to free the chunk.
138 printf("afs_osi_Free: Asked to free unknown type %d at 0x%x\n",
139 MEMTYPE(p), MEMADDR(p));
145 /* hash_chunk() receives a pointer to a chunk and hashes it to produce a
146 * key that the hashtable can use. The key is obtained by
147 * right shifting out the 2 LSBs and then multiplying the
148 * result by a constant no. and dividing it with a large prime.
150 #define HASH_CONST 32786
151 #define HASH_PRIME 79367
152 static unsigned hash_chunk(void *p)
156 key = (unsigned int)(long)p >> 2;
157 key = (key * HASH_CONST)%HASH_PRIME;
162 /* hash_free() : Invoked by osi_linux_free_afs_memory(), thru
163 * afs_lhash_iter(), this function is called by the lhash
164 * module for every entry in the hash table. hash_free
165 * frees the memory associated with the entry as well
166 * as returning the osi_linux_mem struct to its pool.
169 hash_free(size_t index, unsigned key, void *data)
171 linux_free(((struct osi_linux_mem *)data)->chunk);
172 afs_atomlist_put(al_mem_pool, data);
175 /* hash_verify() is invoked by osi_linux_verify_alloced_memory() thru
176 * afs_lhash_iter() and is called by the lhash module for every element
178 * hash_verify() verifies (within limits) that the memory passed to it is
182 hash_verify(size_t index, unsigned key, void *data)
184 struct osi_linux_mem *lmp = (struct osi_linux_mem *)data;
187 memtype = MEMTYPE(lmp->chunk);
188 #ifdef AFS_SPARC64_LINUX24_ENV
189 if ((memtype == KM_TYPE) && (!VALID_PAGE(virt_to_page(lmp->chunk)))) {
190 printf("osi_linux_verify_alloced_memory: address 0x%x outside range, index=%d, key=%d\n", lmp->chunk, index, key);
193 if ((memtype == KM_TYPE) && (AFS_LINUX_MAP_NR(lmp->chunk) > max_mapnr)) {
194 printf("osi_linux_verify_alloced_memory: address 0x%x outside range, index=%d, key=%d\n", lmp->chunk, index, key);
198 if (memtype != KM_TYPE && memtype != VM_TYPE) {
199 printf("osi_linux_verify_alloced_memory: unknown type %d at 0x%x, index=%d\n", memtype, lmp->chunk, index);
201 afs_linux_hash_verify_count++;
205 /* local_free() : wrapper for vfree(), to deal with incompatible protoypes */
206 static void local_free(void *p, size_t n)
211 /* linux_alloc_init(): Initializes the kernel memory allocator. As part
212 * of this process, it also initializes a pool of osi_linux_mem
213 * structures as well as the hash table itself.
218 static int linux_alloc_init()
220 /* initiate our pool of osi_linux_mem structs */
221 al_mem_pool = afs_atomlist_create(sizeof(struct osi_linux_mem),
222 sizeof(long)*1024, (void *)vmalloc,
225 printf("afs_osi_Alloc: Error in initialization(atomlist_create)\n");
229 /* initialize the hash table to hold references to alloc'ed chunks */
230 lh_mem_htab = afs_lhash_create(hash_equal, (void *)vmalloc, local_free);
232 printf("afs_osi_Alloc: Error in initialization(lhash_create)\n");
240 /* hash_bucket_stat() : Counts the no. of elements in each bucket and
241 * stores results in our bucket stats vector.
243 static unsigned int cur_bucket, cur_bucket_len;
244 static void 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 */
251 else { /* if we're on the next bucket, store the distribution */
252 if (cur_bucket_len < MAX_BUCKET_LEN)
253 afs_linux_hash_bucket_dist[cur_bucket_len]++;
255 printf("afs_get_hash_stats: Warning! exceeded max bucket len %d\n", cur_bucket_len);
260 /* get_hash_stats() : get hash table statistics */
261 static void get_hash_stats()
265 afs_lhash_stat(lh_mem_htab, &afs_linux_lsb);
267 /* clear out the bucket stat vector */
268 for(i=0;i<MAX_BUCKET_LEN;i++, afs_linux_hash_bucket_dist[i]=0);
269 cur_bucket = cur_bucket_len = 00;
271 /* populate the bucket stat vector */
272 afs_lhash_iter(lh_mem_htab, hash_bucket_stat);
275 /************** Linux memory allocator interface functions **********/
277 #if defined(AFS_LINUX24_ENV)
278 DECLARE_MUTEX(afs_linux_alloc_sem);
280 struct semaphore afs_linux_alloc_sem = MUTEX;
283 void *osi_linux_alloc(unsigned int asize)
286 struct osi_linux_mem *lmem;
288 new = linux_alloc(asize); /* 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);
330 up(&afs_linux_alloc_sem);
332 down(&afs_linux_alloc_sem);
340 /* osi_linux_free() - free chunk of memory passed to us.
342 void osi_linux_free(void *addr)
344 struct osi_linux_mem lmem, *lmp;
346 down(&afs_linux_alloc_sem);
349 /* remove this chunk from our hash table */
350 if ( lmp = (struct osi_linux_mem *)afs_lhash_remove(lh_mem_htab, hash_chunk(addr), &lmem)) {
351 linux_free(lmp->chunk); /* this contains the piggybacked type info*/
352 afs_atomlist_put(al_mem_pool, lmp); /* return osi_linux_mem struct to pool*/
353 afs_linux_cur_allocs--;
356 printf("osi_linux_free: failed to remove chunk from hashtable\n");
359 up(&afs_linux_alloc_sem);
362 /* osi_linux_free_afs_memory() - free all chunks of memory allocated.
364 void osi_linux_free_afs_memory(void)
366 down(&afs_linux_alloc_sem);
368 if (allocator_init) {
369 /* iterate through all elements in the hash table and free both
370 * the chunk and the atom associated with it.
372 afs_lhash_iter(lh_mem_htab, hash_free);
374 /* free the atomlist. */
375 afs_atomlist_destroy(al_mem_pool);
377 /* free the hashlist. */
378 afs_lhash_destroy(lh_mem_htab);
380 /* change the state so that the allocator is now uninitialized. */
383 up(&afs_linux_alloc_sem);
386 /* osi_linux_verify_alloced_memory(): verify all chunks of alloced memory in
389 void osi_linux_verify_alloced_memory()
391 down(&afs_linux_alloc_sem);
393 /* count of times hash_verify was called. reset it to 0 before iteration */
394 afs_linux_hash_verify_count = 0;
396 /* iterate thru elements in the hash table */
397 afs_lhash_iter(lh_mem_htab, hash_verify);
399 if (afs_linux_hash_verify_count != afs_linux_cur_allocs) {
400 /* hmm, some pieces of memory are missing. */
401 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);
404 up(&afs_linux_alloc_sem);