i386-user-mode-linux-support-20030513
[openafs.git] / src / afs / LINUX / osi_alloc.c
index f076d7e..0e30d67 100644 (file)
  * osi_alloc.c - Linux memory allocation routines.
  *
  */
-#include "../afs/param.h"
-#include "../afs/sysincludes.h"
-#include "../afs/afsincludes.h"
-#include "../h/mm.h"
+#include <afsconfig.h>
+#include "afs/param.h"
 
-#include "../afs/afs_atomlist.h"
-#include "../afs/afs_lhash.h"
+RCSID("$Header$");
 
-#define MAX_KMALLOC_SIZE (131072-16) /* Max we can alloc in physmem */
+#include "afs/sysincludes.h"
+#include "afsincludes.h"
+#include "h/mm.h"
+#include "h/slab.h"
+
+#include "afs_atomlist.h"
+#include "afs_lhash.h"
+
+#define MAX_KMALLOC_SIZE PAGE_SIZE /* Max we should alloc with kmalloc */
 #define MAX_BUCKET_LEN 30 /* max. no. of entries per buckets we expect to see */
 #define STAT_INTERVAL 8192 /* we collect stats once every STAT_INTERVAL allocs*/
 
@@ -46,7 +51,7 @@ struct afs_lhash_stat afs_linux_lsb; /* hash table statistics */
 unsigned int afs_linux_hash_bucket_dist[MAX_BUCKET_LEN]; /* bucket population distribution in our hash table */
 
 #if defined(AFS_LINUX24_ENV)
-#include "../h/vmalloc.h"
+#include "h/vmalloc.h"
 #else
 /* externs : can we do this in a better way. Including vmalloc.h causes other
  * problems.*/
@@ -71,39 +76,51 @@ static int hash_equal(const void *a, const void *b)
  *  returns NULL if we failed to allocate memory.
  *  or pointer to memory if we succeeded.
  */
-static void *linux_alloc(unsigned int asize)
+static void *linux_alloc(unsigned int asize, int drop_glock)
 {
     void *new = NULL;
-    int has_afs_glock = ISAFS_GLOCK();
-
-    /* if global lock has been held save this info and unlock it. */
-    if (has_afs_glock)
-        AFS_GUNLOCK();
+    int max_retry = 10;
+    int haveGlock = ISAFS_GLOCK();
 
     /*  if we can use kmalloc use it to allocate the required memory. */
-    if (asize <  MAX_KMALLOC_SIZE) {
-        new = (void *)(long)kmalloc(asize, GFP_KERNEL);
+    while(!new && max_retry)
+    {
+        if (asize <=  MAX_KMALLOC_SIZE) {
+        new = (void *)(unsigned long)kmalloc(asize, 
+#ifdef GFP_NOFS
+                                            GFP_NOFS
+#else
+                                            GFP_KERNEL
+#endif
+                                            );
         if (new) /* piggy back alloc type */
             (unsigned long)new |= KM_TYPE;
-    }
-    if (!new) { /* otherwise use vmalloc  */
-       int max_wait = 10;
-        while (!(new = (void *)vmalloc(asize))) {
-            if (--max_wait <=0) {
-               break;
+        } else {
+            new = (void *)vmalloc(asize);
+           if (new) /* piggy back alloc type */
+               (unsigned long)new |= VM_TYPE;
             }
-           schedule();
+
+       if (!new) {
+#ifdef set_current_state
+           set_current_state(TASK_INTERRUPTIBLE);
+#else
+           current->state = TASK_INTERRUPTIBLE;
+#endif
+           if (drop_glock && haveGlock) AFS_GUNLOCK();
+           schedule_timeout(HZ);
+           if (drop_glock && haveGlock) AFS_GLOCK();
+#ifdef set_current_state
+            set_current_state(TASK_RUNNING);
+#else
+            current->state = TASK_RUNNING;
+#endif
+            --max_retry;
         }
-       if (new) /* piggy back alloc type */
-           (unsigned long)new |= VM_TYPE;
     }
     if (new)
        memset(MEMADDR(new), 0, asize);
 
-    /* if the global lock had been held, lock it again. */
-    if (has_afs_glock)
-        AFS_GLOCK();
-
     return new;
 }
 
@@ -171,9 +188,15 @@ hash_verify(size_t index, unsigned key, void *data)
     int memtype;
 
     memtype = MEMTYPE(lmp->chunk);
+#if defined(AFS_SPARC64_LINUX24_ENV) || defined(AFS_I386_UMLINUX20_ENV)
+    if ((memtype == KM_TYPE) && (!VALID_PAGE(virt_to_page(lmp->chunk)))) {
+       printf("osi_linux_verify_alloced_memory: address 0x%x outside range, index=%d, key=%d\n", lmp->chunk, index, key);
+    }
+#else
     if ((memtype == KM_TYPE) && (AFS_LINUX_MAP_NR(lmp->chunk) > max_mapnr)) {
        printf("osi_linux_verify_alloced_memory: address 0x%x outside range, index=%d, key=%d\n", lmp->chunk, index, key);
     }
+#endif
     
     if (memtype != KM_TYPE && memtype != VM_TYPE) {
        printf("osi_linux_verify_alloced_memory: unknown type %d at 0x%x, index=%d\n",    memtype, lmp->chunk, index);
@@ -260,25 +283,27 @@ DECLARE_MUTEX(afs_linux_alloc_sem);
 struct semaphore afs_linux_alloc_sem = MUTEX;
 #endif
 
-void *osi_linux_alloc(unsigned int asize)
+void *osi_linux_alloc(unsigned int asize, int drop_glock)
 {
     void *new = NULL;
     struct osi_linux_mem *lmem;
 
+    new = linux_alloc(asize, drop_glock); /* get a chunk of memory of size asize */
+
+    if (!new) {
+       printf("afs_osi_Alloc: Can't vmalloc %d bytes.\n", asize);
+       return new;
+    }
+
     down(&afs_linux_alloc_sem);
 
-    if (allocator_init == 0) { /* allocator hasn't been initialized yet */
+    /* allocator hasn't been initialized yet */
+    if (allocator_init == 0) {
        if (linux_alloc_init() == 0) {
            goto error;
         }
        allocator_init = 1; /* initialization complete */
     }
-
-    new = linux_alloc(asize); /* get a chunk of memory of size asize */
-    if (!new) {
-       printf("afs_osi_Alloc: Can't vmalloc %d bytes.\n", asize);
-       goto error;
-    }
     
     /* get an atom to store the pointer to the chunk */
     lmem = (struct osi_linux_mem *)afs_atomlist_get(al_mem_pool);
@@ -304,8 +329,11 @@ void *osi_linux_alloc(unsigned int asize)
     return MEMADDR(new);
 
   free_error:
-    if (new)
-        linux_free(new);
+    if (new) {
+       up(&afs_linux_alloc_sem);
+       linux_free(new);
+       down(&afs_linux_alloc_sem);
+    }
     new = NULL;
     goto error;
 
@@ -322,7 +350,7 @@ void osi_linux_free(void *addr)
     
     lmem.chunk = addr;
     /* remove this chunk from our hash table */
-    if ( lmp = (struct osi_linux_mem *)afs_lhash_remove(lh_mem_htab, hash_chunk(addr), &lmem)) {
+    if ((lmp = (struct osi_linux_mem *)afs_lhash_remove(lh_mem_htab, hash_chunk(addr), &lmem))) {
         linux_free(lmp->chunk); /* this contains the piggybacked type info*/
         afs_atomlist_put(al_mem_pool, lmp); /* return osi_linux_mem struct to pool*/
         afs_linux_cur_allocs--;