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"
20 #include "afs/sysincludes.h"
21 #include "afsincludes.h"
25 #include "afs_atomlist.h"
26 #include "afs_lhash.h"
28 #define MAX_KMALLOC_SIZE PAGE_SIZE /* Max we should alloc with kmalloc */
29 #define MAX_BUCKET_LEN 30 /* max. no. of entries per buckets we expect to see */
30 #define STAT_INTERVAL 8192 /* we collect stats once every STAT_INTERVAL allocs */
33 #define KM_TYPE 1 /* kmalloc */
34 #define VM_TYPE 2 /* vmalloc */
36 struct osi_linux_mem {
40 /* These assume 32-bit pointers */
41 #define MEMTYPE(A) (((unsigned long)A) & 0x3)
42 #define MEMADDR(A) (void *)((unsigned long)(A) & (~0x3))
45 afs_atomlist *al_mem_pool; /* pool of osi_linux_mem structures */
46 afs_lhash *lh_mem_htab; /* mem hash table */
47 unsigned int allocator_init = 0; /* has the allocator been initialized? */
48 unsigned int afs_linux_cur_allocs = 0;
49 unsigned int afs_linux_total_allocs = 0;
50 unsigned int afs_linux_hash_verify_count = 0; /* used by hash_verify */
51 struct afs_lhash_stat afs_linux_lsb; /* hash table statistics */
52 unsigned int afs_linux_hash_bucket_dist[MAX_BUCKET_LEN]; /* bucket population distribution in our hash table */
54 #if defined(AFS_LINUX24_ENV)
55 #include "h/vmalloc.h"
57 /* externs : can we do this in a better way. Including vmalloc.h causes other
59 extern void vfree(void *addr);
60 extern void *vmalloc(unsigned long size);
63 /* Allocator support functions (static) */
66 hash_equal(const void *a, const void *b)
68 return (MEMADDR(((struct osi_linux_mem *)a)->chunk) ==
69 MEMADDR(((struct osi_linux_mem *)b)->chunk));
73 /* linux_alloc : Allocates memory from the linux kernel. It uses
74 * kmalloc if possible. Otherwise, we use vmalloc.
76 * asize - size of memory required in bytes
78 * returns NULL if we failed to allocate memory.
79 * or pointer to memory if we succeeded.
82 linux_alloc(unsigned int asize, int drop_glock)
86 int haveGlock = ISAFS_GLOCK();
88 /* if we can use kmalloc use it to allocate the required memory. */
89 while (!new && max_retry) {
90 if (asize <= MAX_KMALLOC_SIZE) {
91 new = (void *)(unsigned long)kmalloc(asize,
98 if (new) /* piggy back alloc type */
99 (unsigned long)new |= KM_TYPE;
101 new = (void *)vmalloc(asize);
102 if (new) /* piggy back alloc type */
103 (unsigned long)new |= VM_TYPE;
107 #ifdef set_current_state
108 set_current_state(TASK_INTERRUPTIBLE);
110 current->state = TASK_INTERRUPTIBLE;
112 if (drop_glock && haveGlock)
114 schedule_timeout(HZ);
115 if (drop_glock && haveGlock)
117 #ifdef set_current_state
118 set_current_state(TASK_RUNNING);
120 current->state = TASK_RUNNING;
126 memset(MEMADDR(new), 0, asize);
135 /* mask out the type information from the pointer and
136 * use the appropriate free routine to free the chunk.
138 switch (MEMTYPE(p)) {
146 printf("afs_osi_Free: Asked to free unknown type %d at 0x%x\n",
147 MEMTYPE(p), MEMADDR(p));
153 /* hash_chunk() receives a pointer to a chunk and hashes it to produce a
154 * key that the hashtable can use. The key is obtained by
155 * right shifting out the 2 LSBs and then multiplying the
156 * result by a constant no. and dividing it with a large prime.
158 #define HASH_CONST 32786
159 #define HASH_PRIME 79367
165 key = (unsigned int)(long)p >> 2;
166 key = (key * HASH_CONST) % HASH_PRIME;
171 /* hash_free() : Invoked by osi_linux_free_afs_memory(), thru
172 * afs_lhash_iter(), this function is called by the lhash
173 * module for every entry in the hash table. hash_free
174 * frees the memory associated with the entry as well
175 * as returning the osi_linux_mem struct to its pool.
178 hash_free(size_t index, unsigned key, void *data)
180 linux_free(((struct osi_linux_mem *)data)->chunk);
181 afs_atomlist_put(al_mem_pool, data);
184 /* hash_verify() is invoked by osi_linux_verify_alloced_memory() thru
185 * afs_lhash_iter() and is called by the lhash module for every element
187 * hash_verify() verifies (within limits) that the memory passed to it is
191 hash_verify(size_t index, unsigned key, void *data)
193 struct osi_linux_mem *lmp = (struct osi_linux_mem *)data;
196 memtype = MEMTYPE(lmp->chunk);
197 if (memtype != KM_TYPE && memtype != VM_TYPE) {
199 ("osi_linux_verify_alloced_memory: unknown type %d at 0x%x, index=%d\n",
200 memtype, lmp->chunk, index);
202 afs_linux_hash_verify_count++;
206 /* local_free() : wrapper for vfree(), to deal with incompatible protoypes */
208 local_free(void *p, size_t n)
213 /* linux_alloc_init(): Initializes the kernel memory allocator. As part
214 * of this process, it also initializes a pool of osi_linux_mem
215 * structures as well as the hash table itself.
223 /* initiate our pool of osi_linux_mem structs */
225 afs_atomlist_create(sizeof(struct osi_linux_mem), sizeof(long) * 1024,
226 (void *)vmalloc, local_free);
228 printf("afs_osi_Alloc: Error in initialization(atomlist_create)\n");
232 /* initialize the hash table to hold references to alloc'ed chunks */
233 lh_mem_htab = afs_lhash_create(hash_equal, (void *)vmalloc, local_free);
235 printf("afs_osi_Alloc: Error in initialization(lhash_create)\n");
243 /* hash_bucket_stat() : Counts the no. of elements in each bucket and
244 * stores results in our bucket stats vector.
246 static unsigned int cur_bucket, cur_bucket_len;
248 hash_bucket_stat(size_t index, unsigned key, void *data)
250 if (index == cur_bucket) {
251 /* while still on the same bucket, inc len & return */
254 } else { /* if we're on the next bucket, store the distribution */
255 if (cur_bucket_len < MAX_BUCKET_LEN)
256 afs_linux_hash_bucket_dist[cur_bucket_len]++;
259 ("afs_get_hash_stats: Warning! exceeded max bucket len %d\n",
266 /* get_hash_stats() : get hash table statistics */
272 afs_lhash_stat(lh_mem_htab, &afs_linux_lsb);
274 /* clear out the bucket stat vector */
275 for (i = 0; i < MAX_BUCKET_LEN; i++, afs_linux_hash_bucket_dist[i] = 0);
276 cur_bucket = cur_bucket_len = 00;
278 /* populate the bucket stat vector */
279 afs_lhash_iter(lh_mem_htab, hash_bucket_stat);
282 /************** Linux memory allocator interface functions **********/
284 #if defined(AFS_LINUX24_ENV)
285 DECLARE_MUTEX(afs_linux_alloc_sem);
287 struct semaphore afs_linux_alloc_sem = MUTEX;
291 osi_linux_alloc(unsigned int asize, int drop_glock)
294 struct osi_linux_mem *lmem;
296 new = linux_alloc(asize, drop_glock); /* get a chunk of memory of size asize */
299 printf("afs_osi_Alloc: Can't vmalloc %d bytes.\n", asize);
303 down(&afs_linux_alloc_sem);
305 /* allocator hasn't been initialized yet */
306 if (allocator_init == 0) {
307 if (linux_alloc_init() == 0) {
310 allocator_init = 1; /* initialization complete */
313 /* get an atom to store the pointer to the chunk */
314 lmem = (struct osi_linux_mem *)afs_atomlist_get(al_mem_pool);
316 printf("afs_osi_Alloc: atomlist_get() failed.");
319 /* store the chunk reference */
322 /* hash in the chunk */
323 if (afs_lhash_enter(lh_mem_htab, hash_chunk(new), lmem) != 0) {
324 printf("afs_osi_Alloc: lhash_enter failed\n");
327 afs_linux_cur_allocs++; /* no. of current allocations */
328 afs_linux_total_allocs++; /* total no. of allocations done so far */
329 if ((afs_linux_cur_allocs % STAT_INTERVAL) == 0) {
333 up(&afs_linux_alloc_sem);
338 up(&afs_linux_alloc_sem);
340 down(&afs_linux_alloc_sem);
348 /* osi_linux_free() - free chunk of memory passed to us.
351 osi_linux_free(void *addr)
353 struct osi_linux_mem lmem, *lmp;
355 down(&afs_linux_alloc_sem);
358 /* remove this chunk from our hash table */
360 (struct osi_linux_mem *)afs_lhash_remove(lh_mem_htab,
361 hash_chunk(addr), &lmem))) {
362 linux_free(lmp->chunk); /* this contains the piggybacked type info */
363 afs_atomlist_put(al_mem_pool, lmp); /* return osi_linux_mem struct to pool */
364 afs_linux_cur_allocs--;
366 printf("osi_linux_free: failed to remove chunk from hashtable\n");
369 up(&afs_linux_alloc_sem);
372 /* osi_linux_free_afs_memory() - free all chunks of memory allocated.
375 osi_linux_free_afs_memory(void)
377 down(&afs_linux_alloc_sem);
379 if (allocator_init) {
380 /* iterate through all elements in the hash table and free both
381 * the chunk and the atom associated with it.
383 afs_lhash_iter(lh_mem_htab, hash_free);
385 /* free the atomlist. */
386 afs_atomlist_destroy(al_mem_pool);
388 /* free the hashlist. */
389 afs_lhash_destroy(lh_mem_htab);
391 /* change the state so that the allocator is now uninitialized. */
394 up(&afs_linux_alloc_sem);
397 /* osi_linux_verify_alloced_memory(): verify all chunks of alloced memory in
401 osi_linux_verify_alloced_memory()
403 down(&afs_linux_alloc_sem);
405 /* count of times hash_verify was called. reset it to 0 before iteration */
406 afs_linux_hash_verify_count = 0;
408 /* iterate thru elements in the hash table */
409 afs_lhash_iter(lh_mem_htab, hash_verify);
411 if (afs_linux_hash_verify_count != afs_linux_cur_allocs) {
412 /* hmm, some pieces of memory are missing. */
414 ("osi_linux_verify_alloced_memory: %d chunks of memory are not accounted for during verify!\n",
415 afs_linux_hash_verify_count - afs_linux_cur_allocs);
418 up(&afs_linux_alloc_sem);