Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-2.6
[pandora-kernel.git] / arch / powerpc / platforms / cell / spufs / file.c
index 1018acd..c7df0a6 100644 (file)
@@ -32,7 +32,6 @@
 #include <linux/marker.h>
 
 #include <asm/io.h>
-#include <asm/semaphore.h>
 #include <asm/spu.h>
 #include <asm/spu_info.h>
 #include <asm/uaccess.h>
@@ -358,6 +357,7 @@ static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
 {
        struct spu_context *ctx = vma->vm_file->private_data;
        unsigned long area, offset = address - vma->vm_start;
+       int ret = 0;
 
        spu_context_nospu_trace(spufs_ps_nopfn__enter, ctx);
 
@@ -365,6 +365,13 @@ static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
        if (offset >= ps_size)
                return NOPFN_SIGBUS;
 
+       /*
+        * Because we release the mmap_sem, the context may be destroyed while
+        * we're in spu_wait. Grab an extra reference so it isn't destroyed
+        * in the meantime.
+        */
+       get_spu_context(ctx);
+
        /*
         * We have to wait for context to be loaded before we have
         * pages to hand out to the user, but we don't want to wait
@@ -374,12 +381,12 @@ static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
         * hanged.
         */
        if (spu_acquire(ctx))
-               return NOPFN_REFAULT;
+               goto refault;
 
        if (ctx->state == SPU_STATE_SAVED) {
                up_read(&current->mm->mmap_sem);
                spu_context_nospu_trace(spufs_ps_nopfn__sleep, ctx);
-               spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
+               ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
                spu_context_trace(spufs_ps_nopfn__wake, ctx, ctx->spu);
                down_read(&current->mm->mmap_sem);
        } else {
@@ -388,7 +395,11 @@ static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
                spu_context_trace(spufs_ps_nopfn__insert, ctx, ctx->spu);
        }
 
-       spu_release(ctx);
+       if (!ret)
+               spu_release(ctx);
+
+refault:
+       put_spu_context(ctx);
        return NOPFN_REFAULT;
 }
 
@@ -460,7 +471,7 @@ static int spufs_cntl_open(struct inode *inode, struct file *file)
        if (!i->i_openers++)
                ctx->cntl = inode->i_mapping;
        mutex_unlock(&ctx->mapping_lock);
-       return spufs_attr_open(inode, file, spufs_cntl_get,
+       return simple_attr_open(inode, file, spufs_cntl_get,
                                        spufs_cntl_set, "0x%08lx");
 }
 
@@ -470,7 +481,7 @@ spufs_cntl_release(struct inode *inode, struct file *file)
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spufs_attr_release(inode, file);
+       simple_attr_release(inode, file);
 
        mutex_lock(&ctx->mapping_lock);
        if (!--i->i_openers)
@@ -482,8 +493,8 @@ spufs_cntl_release(struct inode *inode, struct file *file)
 static const struct file_operations spufs_cntl_fops = {
        .open = spufs_cntl_open,
        .release = spufs_cntl_release,
-       .read = spufs_attr_read,
-       .write = spufs_attr_write,
+       .read = simple_attr_read,
+       .write = simple_attr_write,
        .mmap = spufs_cntl_mmap,
 };
 
@@ -755,23 +766,25 @@ static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
 
        count = spu_acquire(ctx);
        if (count)
-               return count;
+               goto out;
 
        /* wait only for the first element */
        count = 0;
        if (file->f_flags & O_NONBLOCK) {
-               if (!spu_ibox_read(ctx, &ibox_data))
+               if (!spu_ibox_read(ctx, &ibox_data)) {
                        count = -EAGAIN;
+                       goto out_unlock;
+               }
        } else {
                count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
+               if (count)
+                       goto out;
        }
-       if (count)
-               goto out;
 
        /* if we can't write at all, return -EFAULT */
        count = __put_user(ibox_data, udata);
        if (count)
-               goto out;
+               goto out_unlock;
 
        for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
                int ret;
@@ -788,9 +801,9 @@ static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
                        break;
        }
 
-out:
+out_unlock:
        spu_release(ctx);
-
+out:
        return count;
 }
 
@@ -905,7 +918,7 @@ static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
 
        count = spu_acquire(ctx);
        if (count)
-               return count;
+               goto out;
 
        /*
         * make sure we can at least write one element, by waiting
@@ -913,14 +926,16 @@ static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
         */
        count = 0;
        if (file->f_flags & O_NONBLOCK) {
-               if (!spu_wbox_write(ctx, wbox_data))
+               if (!spu_wbox_write(ctx, wbox_data)) {
                        count = -EAGAIN;
+                       goto out_unlock;
+               }
        } else {
                count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
+               if (count)
+                       goto out;
        }
 
-       if (count)
-               goto out;
 
        /* write as much as possible */
        for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
@@ -934,8 +949,9 @@ static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
                        break;
        }
 
-out:
+out_unlock:
        spu_release(ctx);
+out:
        return count;
 }
 
@@ -1598,12 +1614,11 @@ static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
        } else {
                ret = spufs_wait(ctx->mfc_wq,
                           spufs_read_mfc_tagstatus(ctx, &status));
+               if (ret)
+                       goto out;
        }
        spu_release(ctx);
 
-       if (ret)
-               goto out;
-
        ret = 4;
        if (copy_to_user(buffer, &status, 4))
                ret = -EFAULT;
@@ -1732,6 +1747,8 @@ static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
                int status;
                ret = spufs_wait(ctx->mfc_wq,
                                 spu_send_mfc_command(ctx, cmd, &status));
+               if (ret)
+                       goto out;
                if (status)
                        ret = status;
        }
@@ -1785,7 +1802,7 @@ static int spufs_mfc_flush(struct file *file, fl_owner_t id)
 
        ret = spu_acquire(ctx);
        if (ret)
-               return ret;
+               goto out;
 #if 0
 /* this currently hangs */
        ret = spufs_wait(ctx->mfc_wq,
@@ -1794,12 +1811,13 @@ static int spufs_mfc_flush(struct file *file, fl_owner_t id)
                goto out;
        ret = spufs_wait(ctx->mfc_wq,
                         ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
-out:
+       if (ret)
+               goto out;
 #else
        ret = 0;
 #endif
        spu_release(ctx);
-
+out:
        return ret;
 }