gru: allocation may fail in quicktest1()
[linux-2.6.git] / drivers / misc / sgi-gru / grumain.c
index 1ce32bc..3bc643d 100644 (file)
@@ -3,11 +3,21 @@
  *
  *            DRIVER TABLE MANAGER + GRU CONTEXT LOAD/UNLOAD
  *
- * This file is subject to the terms and conditions of the GNU General Public
- * License.  See the file "COPYING" in the main directory of this archive
- * for more details.
+ *  Copyright (c) 2008 Silicon Graphics, Inc.  All Rights Reserved.
  *
- * Copyright (c) 2008 Silicon Graphics, Inc.  All Rights Reserved.
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */
 
 #include <linux/kernel.h>
@@ -76,10 +86,9 @@ int gru_cpu_fault_map_id(void)
 /* Hit the asid limit. Start over */
 static int gru_wrap_asid(struct gru_state *gru)
 {
-       gru_dbg(grudev, "gru %p\n", gru);
+       gru_dbg(grudev, "gid %d\n", gru->gs_gid);
        STAT(asid_wrap);
        gru->gs_asid_gen++;
-       gru_flush_all_tlb(gru);
        return MIN_ASID;
 }
 
@@ -88,19 +97,21 @@ static int gru_reset_asid_limit(struct gru_state *gru, int asid)
 {
        int i, gid, inuse_asid, limit;
 
-       gru_dbg(grudev, "gru %p, asid 0x%x\n", gru, asid);
+       gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid);
        STAT(asid_next);
        limit = MAX_ASID;
        if (asid >= limit)
                asid = gru_wrap_asid(gru);
+       gru_flush_all_tlb(gru);
        gid = gru->gs_gid;
 again:
        for (i = 0; i < GRU_NUM_CCH; i++) {
-               if (!gru->gs_gts[i])
+               if (!gru->gs_gts[i] || is_kernel_context(gru->gs_gts[i]))
                        continue;
                inuse_asid = gru->gs_gts[i]->ts_gms->ms_asids[gid].mt_asid;
-               gru_dbg(grudev, "gru %p, inuse_asid 0x%x, cxtnum %d, gts %p\n",
-                       gru, inuse_asid, i, gru->gs_gts[i]);
+               gru_dbg(grudev, "gid %d, gts %p, gms %p, inuse 0x%x, cxt %d\n",
+                       gru->gs_gid, gru->gs_gts[i], gru->gs_gts[i]->ts_gms,
+                       inuse_asid, i);
                if (inuse_asid == asid) {
                        asid += ASID_INC;
                        if (asid >= limit) {
@@ -120,8 +131,8 @@ again:
        }
        gru->gs_asid_limit = limit;
        gru->gs_asid = asid;
-       gru_dbg(grudev, "gru %p, new asid 0x%x, new_limit 0x%x\n", gru, asid,
-               limit);
+       gru_dbg(grudev, "gid %d, new asid 0x%x, new_limit 0x%x\n", gru->gs_gid,
+                                       asid, limit);
        return asid;
 }
 
@@ -130,14 +141,12 @@ static int gru_assign_asid(struct gru_state *gru)
 {
        int asid;
 
-       spin_lock(&gru->gs_asid_lock);
        gru->gs_asid += ASID_INC;
        asid = gru->gs_asid;
        if (asid >= gru->gs_asid_limit)
                asid = gru_reset_asid_limit(gru, asid);
-       spin_unlock(&gru->gs_asid_lock);
 
-       gru_dbg(grudev, "gru %p, asid 0x%x\n", gru, asid);
+       gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid);
        return asid;
 }
 
@@ -151,7 +160,7 @@ static unsigned long reserve_resources(unsigned long *p, int n, int mmax,
        unsigned long bits = 0;
        int i;
 
-       do {
+       while (n--) {
                i = find_first_bit(p, mmax);
                if (i == mmax)
                        BUG();
@@ -159,7 +168,7 @@ static unsigned long reserve_resources(unsigned long *p, int n, int mmax,
                __set_bit(i, &bits);
                if (idx)
                        *idx++ = i;
-       } while (--n);
+       }
        return bits;
 }
 
@@ -215,17 +224,20 @@ static int check_gru_resources(struct gru_state *gru, int cbr_au_count,
  * TLB manangment requires tracking all GRU chiplets that have loaded a GSEG
  * context.
  */
-static int gru_load_mm_tracker(struct gru_state *gru, struct gru_mm_struct *gms,
-                              int ctxnum)
+static int gru_load_mm_tracker(struct gru_state *gru,
+                                       struct gru_thread_state *gts)
 {
+       struct gru_mm_struct *gms = gts->ts_gms;
        struct gru_mm_tracker *asids = &gms->ms_asids[gru->gs_gid];
-       unsigned short ctxbitmap = (1 << ctxnum);
+       unsigned short ctxbitmap = (1 << gts->ts_ctxnum);
        int asid;
 
        spin_lock(&gms->ms_asid_lock);
        asid = asids->mt_asid;
 
-       if (asid == 0 || asids->mt_asid_gen != gru->gs_asid_gen) {
+       spin_lock(&gru->gs_asid_lock);
+       if (asid == 0 || (asids->mt_ctxbitmap == 0 && asids->mt_asid_gen !=
+                         gru->gs_asid_gen)) {
                asid = gru_assign_asid(gru);
                asids->mt_asid = asid;
                asids->mt_asid_gen = gru->gs_asid_gen;
@@ -233,6 +245,7 @@ static int gru_load_mm_tracker(struct gru_state *gru, struct gru_mm_struct *gms,
        } else {
                STAT(asid_reuse);
        }
+       spin_unlock(&gru->gs_asid_lock);
 
        BUG_ON(asids->mt_ctxbitmap & ctxbitmap);
        asids->mt_ctxbitmap |= ctxbitmap;
@@ -241,24 +254,28 @@ static int gru_load_mm_tracker(struct gru_state *gru, struct gru_mm_struct *gms,
        spin_unlock(&gms->ms_asid_lock);
 
        gru_dbg(grudev,
-               "gru %x, gms %p, ctxnum 0x%d, asid 0x%x, asidmap 0x%lx\n",
-               gru->gs_gid, gms, ctxnum, asid, gms->ms_asidmap[0]);
+               "gid %d, gts %p, gms %p, ctxnum %d, asid 0x%x, asidmap 0x%lx\n",
+               gru->gs_gid, gts, gms, gts->ts_ctxnum, asid,
+               gms->ms_asidmap[0]);
        return asid;
 }
 
 static void gru_unload_mm_tracker(struct gru_state *gru,
-                                 struct gru_mm_struct *gms, int ctxnum)
+                                       struct gru_thread_state *gts)
 {
+       struct gru_mm_struct *gms = gts->ts_gms;
        struct gru_mm_tracker *asids;
        unsigned short ctxbitmap;
 
        asids = &gms->ms_asids[gru->gs_gid];
-       ctxbitmap = (1 << ctxnum);
+       ctxbitmap = (1 << gts->ts_ctxnum);
        spin_lock(&gms->ms_asid_lock);
+       spin_lock(&gru->gs_asid_lock);
        BUG_ON((asids->mt_ctxbitmap & ctxbitmap) != ctxbitmap);
        asids->mt_ctxbitmap ^= ctxbitmap;
-       gru_dbg(grudev, "gru %x, gms %p, ctxnum 0x%d, asidmap 0x%lx\n",
-               gru->gs_gid, gms, ctxnum, gms->ms_asidmap[0]);
+       gru_dbg(grudev, "gid %d, gts %p, gms %p, ctxnum 0x%d, asidmap 0x%lx\n",
+               gru->gs_gid, gts, gms, gts->ts_ctxnum, gms->ms_asidmap[0]);
+       spin_unlock(&gru->gs_asid_lock);
        spin_unlock(&gms->ms_asid_lock);
 }
 
@@ -292,37 +309,39 @@ static struct gru_thread_state *gru_find_current_gts_nolock(struct gru_vma_data
 /*
  * Allocate a thread state structure.
  */
-static struct gru_thread_state *gru_alloc_gts(struct vm_area_struct *vma,
-                                             struct gru_vma_data *vdata,
-                                             int tsid)
+struct gru_thread_state *gru_alloc_gts(struct vm_area_struct *vma,
+               int cbr_au_count, int dsr_au_count, int options, int tsid)
 {
        struct gru_thread_state *gts;
        int bytes;
 
-       bytes = DSR_BYTES(vdata->vd_dsr_au_count) +
-                               CBR_BYTES(vdata->vd_cbr_au_count);
+       bytes = DSR_BYTES(dsr_au_count) + CBR_BYTES(cbr_au_count);
        bytes += sizeof(struct gru_thread_state);
-       gts = kzalloc(bytes, GFP_KERNEL);
+       gts = kmalloc(bytes, GFP_KERNEL);
        if (!gts)
                return NULL;
 
        STAT(gts_alloc);
+       memset(gts, 0, sizeof(struct gru_thread_state)); /* zero out header */
        atomic_set(&gts->ts_refcnt, 1);
        mutex_init(&gts->ts_ctxlock);
-       gts->ts_cbr_au_count = vdata->vd_cbr_au_count;
-       gts->ts_dsr_au_count = vdata->vd_dsr_au_count;
-       gts->ts_user_options = vdata->vd_user_options;
+       gts->ts_cbr_au_count = cbr_au_count;
+       gts->ts_dsr_au_count = dsr_au_count;
+       gts->ts_user_options = options;
        gts->ts_tsid = tsid;
-       gts->ts_user_options = vdata->vd_user_options;
        gts->ts_ctxnum = NULLCTX;
-       gts->ts_mm = current->mm;
-       gts->ts_vma = vma;
        gts->ts_tlb_int_select = -1;
-       gts->ts_gms = gru_register_mmu_notifier();
-       if (!gts->ts_gms)
-               goto err;
+       gts->ts_cch_req_slice = -1;
+       gts->ts_sizeavail = GRU_SIZEAVAIL(PAGE_SHIFT);
+       if (vma) {
+               gts->ts_mm = current->mm;
+               gts->ts_vma = vma;
+               gts->ts_gms = gru_register_mmu_notifier();
+               if (!gts->ts_gms)
+                       goto err;
+       }
 
-       gru_dbg(grudev, "alloc vdata %p, new gts %p\n", vdata, gts);
+       gru_dbg(grudev, "alloc gts %p\n", gts);
        return gts;
 
 err:
@@ -373,7 +392,8 @@ struct gru_thread_state *gru_alloc_thread_state(struct vm_area_struct *vma,
        struct gru_vma_data *vdata = vma->vm_private_data;
        struct gru_thread_state *gts, *ngts;
 
-       gts = gru_alloc_gts(vma, vdata, tsid);
+       gts = gru_alloc_gts(vma, vdata->vd_cbr_au_count, vdata->vd_dsr_au_count,
+                           vdata->vd_user_options, tsid);
        if (!gts)
                return NULL;
 
@@ -399,7 +419,7 @@ static void gru_free_gru_context(struct gru_thread_state *gts)
        struct gru_state *gru;
 
        gru = gts->ts_gru;
-       gru_dbg(grudev, "gts %p, gru %p\n", gts, gru);
+       gru_dbg(grudev, "gts %p, gid %d\n", gts, gru->gs_gid);
 
        spin_lock(&gru->gs_lock);
        gru->gs_gts[gts->ts_ctxnum] = NULL;
@@ -408,6 +428,7 @@ static void gru_free_gru_context(struct gru_thread_state *gts)
        __clear_bit(gts->ts_ctxnum, &gru->gs_context_map);
        gts->ts_ctxnum = NULLCTX;
        gts->ts_gru = NULL;
+       gts->ts_blade = -1;
        spin_unlock(&gru->gs_lock);
 
        gts_drop(gts);
@@ -449,7 +470,8 @@ static void gru_prefetch_context(void *gseg, void *cb, void *cbe,
 }
 
 static void gru_load_context_data(void *save, void *grubase, int ctxnum,
-                                 unsigned long cbrmap, unsigned long dsrmap)
+                                 unsigned long cbrmap, unsigned long dsrmap,
+                                 int data_valid)
 {
        void *gseg, *cb, *cbe;
        unsigned long length;
@@ -462,12 +484,22 @@ static void gru_load_context_data(void *save, void *grubase, int ctxnum,
        gru_prefetch_context(gseg, cb, cbe, cbrmap, length);
 
        for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
-               save += gru_copy_handle(cb, save);
-               save += gru_copy_handle(cbe + i * GRU_HANDLE_STRIDE, save);
+               if (data_valid) {
+                       save += gru_copy_handle(cb, save);
+                       save += gru_copy_handle(cbe + i * GRU_HANDLE_STRIDE,
+                                               save);
+               } else {
+                       memset(cb, 0, GRU_CACHE_LINE_BYTES);
+                       memset(cbe + i * GRU_HANDLE_STRIDE, 0,
+                                               GRU_CACHE_LINE_BYTES);
+               }
                cb += GRU_HANDLE_STRIDE;
        }
 
-       memcpy(gseg + GRU_DS_BASE, save, length);
+       if (data_valid)
+               memcpy(gseg + GRU_DS_BASE, save, length);
+       else
+               memset(gseg + GRU_DS_BASE, 0, length);
 }
 
 static void gru_unload_context_data(void *save, void *grubase, int ctxnum,
@@ -497,19 +529,23 @@ void gru_unload_context(struct gru_thread_state *gts, int savestate)
        struct gru_context_configuration_handle *cch;
        int ctxnum = gts->ts_ctxnum;
 
-       zap_vma_ptes(gts->ts_vma, UGRUADDR(gts), GRU_GSEG_PAGESIZE);
+       if (!is_kernel_context(gts))
+               zap_vma_ptes(gts->ts_vma, UGRUADDR(gts), GRU_GSEG_PAGESIZE);
        cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
 
+       gru_dbg(grudev, "gts %p\n", gts);
        lock_cch_handle(cch);
        if (cch_interrupt_sync(cch))
                BUG();
-       gru_dbg(grudev, "gts %p\n", gts);
 
-       gru_unload_mm_tracker(gru, gts->ts_gms, gts->ts_ctxnum);
-       if (savestate)
+       if (!is_kernel_context(gts))
+               gru_unload_mm_tracker(gru, gts);
+       if (savestate) {
                gru_unload_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr,
                                        ctxnum, gts->ts_cbr_map,
                                        gts->ts_dsr_map);
+               gts->ts_data_valid = 1;
+       }
 
        if (cch_deallocate(cch))
                BUG();
@@ -517,24 +553,22 @@ void gru_unload_context(struct gru_thread_state *gts, int savestate)
        unlock_cch_handle(cch);
 
        gru_free_gru_context(gts);
-       STAT(unload_context);
 }
 
 /*
  * Load a GRU context by copying it from the thread data structure in memory
  * to the GRU.
  */
-static void gru_load_context(struct gru_thread_state *gts)
+void gru_load_context(struct gru_thread_state *gts)
 {
        struct gru_state *gru = gts->ts_gru;
        struct gru_context_configuration_handle *cch;
-       int err, asid, ctxnum = gts->ts_ctxnum;
+       int i, err, asid, ctxnum = gts->ts_ctxnum;
 
        gru_dbg(grudev, "gts %p\n", gts);
        cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
 
        lock_cch_handle(cch);
-       asid = gru_load_mm_tracker(gru, gts->ts_gms, gts->ts_ctxnum);
        cch->tfm_fault_bit_enable =
            (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
             || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
@@ -543,8 +577,32 @@ static void gru_load_context(struct gru_thread_state *gts)
                gts->ts_tlb_int_select = gru_cpu_fault_map_id();
                cch->tlb_int_select = gts->ts_tlb_int_select;
        }
+       if (gts->ts_cch_req_slice >= 0) {
+               cch->req_slice_set_enable = 1;
+               cch->req_slice = gts->ts_cch_req_slice;
+       } else {
+               cch->req_slice_set_enable =0;
+       }
        cch->tfm_done_bit_enable = 0;
-       err = cch_allocate(cch, asid, gts->ts_cbr_map, gts->ts_dsr_map);
+       cch->dsr_allocation_map = gts->ts_dsr_map;
+       cch->cbr_allocation_map = gts->ts_cbr_map;
+
+       if (is_kernel_context(gts)) {
+               cch->unmap_enable = 1;
+               cch->tfm_done_bit_enable = 1;
+               cch->cb_int_enable = 1;
+       } else {
+               cch->unmap_enable = 0;
+               cch->tfm_done_bit_enable = 0;
+               cch->cb_int_enable = 0;
+               asid = gru_load_mm_tracker(gru, gts);
+               for (i = 0; i < 8; i++) {
+                       cch->asid[i] = asid + i;
+                       cch->sizeavail[i] = gts->ts_sizeavail;
+               }
+       }
+
+       err = cch_allocate(cch);
        if (err) {
                gru_dbg(grudev,
                        "err %d: cch %p, gts %p, cbr 0x%lx, dsr 0x%lx\n",
@@ -553,23 +611,22 @@ static void gru_load_context(struct gru_thread_state *gts)
        }
 
        gru_load_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, ctxnum,
-                             gts->ts_cbr_map, gts->ts_dsr_map);
+                       gts->ts_cbr_map, gts->ts_dsr_map, gts->ts_data_valid);
 
        if (cch_start(cch))
                BUG();
        unlock_cch_handle(cch);
-
-       STAT(load_context);
 }
 
 /*
  * Update fields in an active CCH:
  *     - retarget interrupts on local blade
+ *     - update sizeavail mask
  *     - force a delayed context unload by clearing the CCH asids. This
  *       forces TLB misses for new GRU instructions. The context is unloaded
  *       when the next TLB miss occurs.
  */
-static int gru_update_cch(struct gru_thread_state *gts, int int_select)
+int gru_update_cch(struct gru_thread_state *gts, int force_unload)
 {
        struct gru_context_configuration_handle *cch;
        struct gru_state *gru = gts->ts_gru;
@@ -583,9 +640,14 @@ static int gru_update_cch(struct gru_thread_state *gts, int int_select)
                        goto exit;
                if (cch_interrupt(cch))
                        BUG();
-               if (int_select >= 0) {
-                       gts->ts_tlb_int_select = int_select;
-                       cch->tlb_int_select = int_select;
+               if (!force_unload) {
+                       for (i = 0; i < 8; i++)
+                               cch->sizeavail[i] = gts->ts_sizeavail;
+                       gts->ts_tlb_int_select = gru_cpu_fault_map_id();
+                       cch->tlb_int_select = gru_cpu_fault_map_id();
+                       cch->tfm_fault_bit_enable =
+                         (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
+                           || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
                } else {
                        for (i = 0; i < 8; i++)
                                cch->asid[i] = 0;
@@ -617,7 +679,7 @@ static int gru_retarget_intr(struct gru_thread_state *gts)
 
        gru_dbg(grudev, "retarget from %d to %d\n", gts->ts_tlb_int_select,
                gru_cpu_fault_map_id());
-       return gru_update_cch(gts, gru_cpu_fault_map_id());
+       return gru_update_cch(gts, 0);
 }
 
 
@@ -629,7 +691,28 @@ static int gru_retarget_intr(struct gru_thread_state *gts)
 #define next_gru(b, g) (((g) < &(b)->bs_grus[GRU_CHIPLETS_PER_BLADE - 1]) ?  \
                                 ((g)+1) : &(b)->bs_grus[0])
 
-static void gru_steal_context(struct gru_thread_state *gts)
+static int is_gts_stealable(struct gru_thread_state *gts,
+               struct gru_blade_state *bs)
+{
+       if (is_kernel_context(gts))
+               return down_write_trylock(&bs->bs_kgts_sema);
+       else
+               return mutex_trylock(&gts->ts_ctxlock);
+}
+
+static void gts_stolen(struct gru_thread_state *gts,
+               struct gru_blade_state *bs)
+{
+       if (is_kernel_context(gts)) {
+               up_write(&bs->bs_kgts_sema);
+               STAT(steal_kernel_context);
+       } else {
+               mutex_unlock(&gts->ts_ctxlock);
+               STAT(steal_user_context);
+       }
+}
+
+void gru_steal_context(struct gru_thread_state *gts, int blade_id)
 {
        struct gru_blade_state *blade;
        struct gru_state *gru, *gru0;
@@ -639,8 +722,7 @@ static void gru_steal_context(struct gru_thread_state *gts)
        cbr = gts->ts_cbr_au_count;
        dsr = gts->ts_dsr_au_count;
 
-       preempt_disable();
-       blade = gru_base[uv_numa_blade_id()];
+       blade = gru_base[blade_id];
        spin_lock(&blade->bs_lock);
 
        ctxnum = next_ctxnum(blade->bs_lru_ctxnum);
@@ -663,7 +745,7 @@ static void gru_steal_context(struct gru_thread_state *gts)
                         * success are high. If trylock fails, try to steal a
                         * different GSEG.
                         */
-                       if (ngts && mutex_trylock(&ngts->ts_ctxlock))
+                       if (ngts && is_gts_stealable(ngts, blade))
                                break;
                        ngts = NULL;
                        flag = 1;
@@ -677,18 +759,17 @@ static void gru_steal_context(struct gru_thread_state *gts)
        blade->bs_lru_gru = gru;
        blade->bs_lru_ctxnum = ctxnum;
        spin_unlock(&blade->bs_lock);
-       preempt_enable();
 
        if (ngts) {
-               STAT(steal_context);
+               gts->ustats.context_stolen++;
                ngts->ts_steal_jiffies = jiffies;
-               gru_unload_context(ngts, 1);
-               mutex_unlock(&ngts->ts_ctxlock);
+               gru_unload_context(ngts, is_kernel_context(ngts) ? 0 : 1);
+               gts_stolen(ngts, blade);
        } else {
                STAT(steal_context_failed);
        }
        gru_dbg(grudev,
-               "stole gru %x, ctxnum %d from gts %p. Need cb %d, ds %d;"
+               "stole gid %d, ctxnum %d from gts %p. Need cb %d, ds %d;"
                " avail cb %ld, ds %ld\n",
                gru->gs_gid, ctxnum, ngts, cbr, dsr, hweight64(gru->gs_cbr_map),
                hweight64(gru->gs_dsr_map));
@@ -697,17 +778,17 @@ static void gru_steal_context(struct gru_thread_state *gts)
 /*
  * Scan the GRUs on the local blade & assign a GRU context.
  */
-static struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts)
+struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts,
+                                               int blade)
 {
        struct gru_state *gru, *grux;
        int i, max_active_contexts;
 
-       preempt_disable();
 
 again:
        gru = NULL;
        max_active_contexts = GRU_NUM_CCH;
-       for_each_gru_on_blade(grux, uv_numa_blade_id(), i) {
+       for_each_gru_on_blade(grux, blade, i) {
                if (check_gru_resources(grux, gts->ts_cbr_au_count,
                                        gts->ts_dsr_au_count,
                                        max_active_contexts)) {
@@ -727,6 +808,7 @@ again:
                }
                reserve_gru_resources(gru, gts);
                gts->ts_gru = gru;
+               gts->ts_blade = gru->gs_blade_id;
                gts->ts_ctxnum =
                    find_first_zero_bit(&gru->gs_context_map, GRU_NUM_CCH);
                BUG_ON(gts->ts_ctxnum == GRU_NUM_CCH);
@@ -737,7 +819,7 @@ again:
 
                STAT(assign_context);
                gru_dbg(grudev,
-                       "gseg %p, gts %p, gru %x, ctx %d, cbr %d, dsr %d\n",
+                       "gseg %p, gts %p, gid %d, ctx %d, cbr %d, dsr %d\n",
                        gseg_virtual_address(gts->ts_gru, gts->ts_ctxnum), gts,
                        gts->ts_gru->gs_gid, gts->ts_ctxnum,
                        gts->ts_cbr_au_count, gts->ts_dsr_au_count);
@@ -746,7 +828,6 @@ again:
                STAT(assign_context_failed);
        }
 
-       preempt_enable();
        return gru;
 }
 
@@ -761,6 +842,7 @@ int gru_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 {
        struct gru_thread_state *gts;
        unsigned long paddr, vaddr;
+       int blade_id;
 
        vaddr = (unsigned long)vmf->virtual_address;
        gru_dbg(grudev, "vma %p, vaddr 0x%lx (0x%lx)\n",
@@ -775,8 +857,10 @@ int gru_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 again:
        mutex_lock(&gts->ts_ctxlock);
        preempt_disable();
+       blade_id = uv_numa_blade_id();
+
        if (gts->ts_gru) {
-               if (gts->ts_gru->gs_blade_id != uv_numa_blade_id()) {
+               if (gts->ts_gru->gs_blade_id != blade_id) {
                        STAT(migrated_nopfn_unload);
                        gru_unload_context(gts, 1);
                } else {
@@ -786,12 +870,15 @@ again:
        }
 
        if (!gts->ts_gru) {
-               if (!gru_assign_gru_context(gts)) {
-                       mutex_unlock(&gts->ts_ctxlock);
+               STAT(load_user_context);
+               if (!gru_assign_gru_context(gts, blade_id)) {
                        preempt_enable();
+                       mutex_unlock(&gts->ts_ctxlock);
+                       set_current_state(TASK_INTERRUPTIBLE);
                        schedule_timeout(GRU_ASSIGN_DELAY);  /* true hack ZZZ */
+                       blade_id = uv_numa_blade_id();
                        if (gts->ts_steal_jiffies + GRU_STEAL_DELAY < jiffies)
-                               gru_steal_context(gts);
+                               gru_steal_context(gts, blade_id);
                        goto again;
                }
                gru_load_context(gts);
@@ -801,8 +888,8 @@ again:
                                vma->vm_page_prot);
        }
 
-       mutex_unlock(&gts->ts_ctxlock);
        preempt_enable();
+       mutex_unlock(&gts->ts_ctxlock);
 
        return VM_FAULT_NOPAGE;
 }