[Mini-OS] Make gnttab allocation/free safe
authorKeir Fraser <keir.fraser@citrix.com>
Wed, 28 Nov 2007 12:40:57 +0000 (12:40 +0000)
committerKeir Fraser <keir.fraser@citrix.com>
Wed, 28 Nov 2007 12:40:57 +0000 (12:40 +0000)
Add a semaphore to protect gnttab_list from exhaustion, and disable
callbacks during allocation/free.  Fix the network frontend accordingly.

Signed-off-by: Samuel Thibault <samuel.thibault@citrix.com>
extras/mini-os/gnttab.c
extras/mini-os/netfront.c

index b22f51ceaefdde6e7cfe5f95f053063b3a2027ab..5c2dcea86386919927572f206770200a22f074d1 100644 (file)
@@ -18,6 +18,7 @@
 #include <os.h>
 #include <mm.h>
 #include <gnttab.h>
+#include <semaphore.h>
 
 #define NR_RESERVED_ENTRIES 8
 
 
 static grant_entry_t *gnttab_table;
 static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
+static __DECLARE_SEMAPHORE_GENERIC(gnttab_sem, NR_GRANT_ENTRIES);
 
 static void
 put_free_entry(grant_ref_t ref)
 {
+    unsigned long flags;
+    local_irq_save(flags);
     gnttab_list[ref] = gnttab_list[0];
     gnttab_list[0]  = ref;
-
+    local_irq_restore(flags);
+    up(&gnttab_sem);
 }
 
 static grant_ref_t
 get_free_entry(void)
 {
-    unsigned int ref = gnttab_list[0];
+    unsigned int ref;
+    unsigned long flags;
+    down(&gnttab_sem);
+    local_irq_save(flags);
+    ref = gnttab_list[0];
     gnttab_list[0] = gnttab_list[ref];
+    local_irq_restore(flags);
     return ref;
 }
 
index fc1e9ab357aa0f27534a62e63fd5e717760d8e2f..43411686521e0e2a7408877f27fca174dd8fd6e7 100644 (file)
@@ -147,6 +147,7 @@ moretodo:
         struct net_buffer* buf = &rx_buffers[id];
         void* page = buf->page;
 
+        /* We are sure to have free gnttab entries since they got released above */
         buf->gref = req->gref = 
             gnttab_grant_access(0,virt_to_mfn(page),0);
 
@@ -436,8 +437,9 @@ void netfront_xmit(unsigned char* data,int len)
     down(&tx_sem);
 
     local_irq_save(flags);
-
     id = get_id_from_freelist(tx_freelist);
+    local_irq_restore(flags);
+
     buf = &tx_buffers[id];
     page = buf->page;
 
@@ -461,7 +463,7 @@ void netfront_xmit(unsigned char* data,int len)
 
     if(notify) notify_remote_via_evtchn(info->evtchn);
 
+    local_irq_save(flags);
     network_tx_buf_gc();
-
     local_irq_restore(flags);
 }