Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/dmover Modernize simple_lock() and ltsleep() usage i...



details:   https://anonhg.NetBSD.org/src/rev/ef2b1ca0af0d
branches:  trunk
changeset: 764960:ef2b1ca0af0d
user:      jakllsch <jakllsch%NetBSD.org@localhost>
date:      Sat May 14 13:52:00 2011 +0000

description:
Modernize simple_lock() and ltsleep() usage in dmover(4).
(Thanks to rmind for providing hints on what was necessary.)

diffstat:

 sys/dev/dmover/dmover_io.c |  197 +++++++++++++++++++++-----------------------
 1 files changed, 95 insertions(+), 102 deletions(-)

diffs (truncated from 477 to 300 lines):

diff -r 45b0bb96fcc4 -r ef2b1ca0af0d sys/dev/dmover/dmover_io.c
--- a/sys/dev/dmover/dmover_io.c        Sat May 14 12:44:15 2011 +0000
+++ b/sys/dev/dmover/dmover_io.c        Sat May 14 13:52:00 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dmover_io.c,v 1.39 2010/11/13 14:08:20 uebayasi Exp $  */
+/*     $NetBSD: dmover_io.c,v 1.40 2011/05/14 13:52:00 jakllsch Exp $  */
 
 /*
  * Copyright (c) 2002, 2003 Wasabi Systems, Inc.
@@ -55,7 +55,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dmover_io.c,v 1.39 2010/11/13 14:08:20 uebayasi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dmover_io.c,v 1.40 2011/05/14 13:52:00 jakllsch Exp $");
 
 #include <sys/param.h>
 #include <sys/queue.h>
@@ -64,7 +64,6 @@
 #include <sys/proc.h>
 #include <sys/poll.h>
 #include <sys/malloc.h>
-#include <sys/simplelock.h>
 #include <sys/file.h>
 #include <sys/filedesc.h>
 #include <sys/filio.h>
@@ -74,6 +73,8 @@
 #include <sys/once.h>
 #include <sys/stat.h>
 #include <sys/kauth.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
 
 #include <uvm/uvm_extern.h>
 
@@ -101,7 +102,9 @@
        struct selinfo ds_selq;
        volatile int ds_flags;
        u_int ds_nreqs;
-       struct simplelock ds_slock;
+       kmutex_t ds_lock;
+       kcondvar_t ds_complete_cv;
+       kcondvar_t ds_nreqs_cv;
        struct timespec ds_atime;
        struct timespec ds_mtime;
        struct timespec ds_btime;
@@ -110,6 +113,8 @@
 static ONCE_DECL(dmio_cleaner_control);
 static struct workqueue *dmio_cleaner;
 static int dmio_cleaner_init(void);
+static struct dmio_state *dmio_state_get(void);
+static void dmio_state_put(struct dmio_state *);
 static void dmio_usrreq_fini1(struct work *wk, void *);
 
 #define        DMIO_STATE_SEL          0x0001
@@ -161,6 +166,40 @@
            NULL, PWAIT, IPL_SOFTCLOCK, 0);
 }
 
+static struct dmio_state *
+dmio_state_get(void)
+{
+       struct dmio_state *ds;
+
+       ds = pool_get(&dmio_state_pool, PR_WAITOK);
+
+       memset(ds, 0, sizeof(*ds));
+
+       getnanotime(&ds->ds_btime);
+       ds->ds_atime = ds->ds_mtime = ds->ds_btime;
+
+       mutex_init(&ds->ds_lock, MUTEX_DEFAULT, IPL_SOFTCLOCK);
+       cv_init(&ds->ds_complete_cv, "dmvrrd");
+       cv_init(&ds->ds_nreqs_cv, "dmiowr");
+       TAILQ_INIT(&ds->ds_pending);
+       TAILQ_INIT(&ds->ds_complete);
+       selinit(&ds->ds_selq);
+
+       return ds;
+}
+
+static void
+dmio_state_put(struct dmio_state *ds)
+{
+
+       seldestroy(&ds->ds_selq);
+       cv_destroy(&ds->ds_nreqs_cv);
+       cv_destroy(&ds->ds_complete_cv);
+       mutex_destroy(&ds->ds_lock);
+
+       pool_put(&dmio_state_pool, ds);
+}
+
 /*
  * dmio_usrreq_init:
  *
@@ -332,14 +371,11 @@
 dmio_usrreq_fini1(struct work *wk, void *dummy)
 {
        struct dmio_usrreq_state *dus = (void *)wk;
-       int s;
 
        KASSERT(wk == &dus->dus_work);
 
        uvmspace_free(dus->dus_vmspace);
-       s = splsoftclock();
        pool_put(&dmio_usrreq_state_pool, dus);
-       splx(s);
 }
 
 /*
@@ -355,7 +391,7 @@
        struct dmio_usrreq_state *dus;
        struct dmover_request *dreq;
        struct dmio_usrresp resp;
-       int s, error = 0, progress = 0;
+       int error = 0, progress = 0;
 
        if ((uio->uio_resid % sizeof(resp)) != 0)
                return (EINVAL);
@@ -364,8 +400,7 @@
                return (ENXIO);
 
        getnanotime(&ds->ds_atime);
-       s = splsoftclock();
-       simple_lock(&ds->ds_slock);
+       mutex_enter(&ds->ds_lock);
 
        while (uio->uio_resid != 0) {
 
@@ -377,9 +412,7 @@
                                        goto out;
                                }
                                ds->ds_flags |= DMIO_STATE_READ_WAIT;
-                               error = ltsleep(&ds->ds_complete,
-                                   PRIBIO | PCATCH, "dmvrrd", 0,
-                                   &ds->ds_slock);
+                               error = cv_wait_sig(&ds->ds_complete_cv, &ds->ds_lock);
                                if (error)
                                        goto out;
                                continue;
@@ -389,7 +422,7 @@
                        ds->ds_nreqs--;
                        if (ds->ds_flags & DMIO_STATE_WRITE_WAIT) {
                                ds->ds_flags &= ~DMIO_STATE_WRITE_WAIT;
-                               wakeup(&ds->ds_nreqs);
+                               cv_broadcast(&ds->ds_nreqs_cv);
                        }
                        if (ds->ds_flags & DMIO_STATE_SEL) {
                                ds->ds_flags &= ~DMIO_STATE_SEL;
@@ -398,8 +431,6 @@
                        break;
                }
 
-               simple_unlock(&ds->ds_slock);
-
                dreq = dus->dus_req;
                resp.resp_id = dus->dus_id;
                if (dreq->dreq_flags & DMOVER_REQ_ERROR)
@@ -412,7 +443,7 @@
 
                dmio_usrreq_fini(ds, dus);
 
-               splx(s);
+               mutex_exit(&ds->ds_lock);
 
                progress = 1;
 
@@ -422,13 +453,11 @@
                if (error)
                        return (error);
 
-               s = splsoftclock();
-               simple_lock(&ds->ds_slock);
+               mutex_enter(&ds->ds_lock);
        }
 
  out:
-       simple_unlock(&ds->ds_slock);
-       splx(s);
+       mutex_exit(&ds->ds_lock);
 
        return (error);
 }
@@ -446,30 +475,29 @@
 
        /* We're already at splsoftclock(). */
 
-       simple_lock(&ds->ds_slock);
+       mutex_enter(&ds->ds_lock);
        TAILQ_REMOVE(&ds->ds_pending, dus, dus_q);
        if (ds->ds_flags & DMIO_STATE_DEAD) {
-               ds->ds_nreqs--;
+               int nreqs = --ds->ds_nreqs;
+               mutex_exit(&ds->ds_lock);
                dmio_usrreq_fini(ds, dus);
                dmover_request_free(dreq);
-               if (ds->ds_nreqs == 0) {
-                       simple_unlock(&ds->ds_slock);
-                       seldestroy(&ds->ds_selq);
-                       pool_put(&dmio_state_pool, ds);
-                       return;
+               if (nreqs == 0) {
+                       dmio_state_put(ds);
                }
-       } else {
-               TAILQ_INSERT_TAIL(&ds->ds_complete, dus, dus_q);
-               if (ds->ds_flags & DMIO_STATE_READ_WAIT) {
-                       ds->ds_flags &= ~DMIO_STATE_READ_WAIT;
-                       wakeup(&ds->ds_complete);
-               }
-               if (ds->ds_flags & DMIO_STATE_SEL) {
-                       ds->ds_flags &= ~DMIO_STATE_SEL;
-                       selnotify(&ds->ds_selq, POLLOUT | POLLWRNORM, 0);
-               }
+               return;
        }
-       simple_unlock(&ds->ds_slock);
+
+       TAILQ_INSERT_TAIL(&ds->ds_complete, dus, dus_q);
+       if (ds->ds_flags & DMIO_STATE_READ_WAIT) {
+               ds->ds_flags &= ~DMIO_STATE_READ_WAIT;
+               cv_broadcast(&ds->ds_complete_cv);
+       }
+       if (ds->ds_flags & DMIO_STATE_SEL) {
+               ds->ds_flags &= ~DMIO_STATE_SEL;
+               selnotify(&ds->ds_selq, POLLOUT | POLLWRNORM, 0);
+       }
+       mutex_exit(&ds->ds_lock);
 }
 
 /*
@@ -485,7 +513,7 @@
        struct dmio_usrreq_state *dus;
        struct dmover_request *dreq;
        struct dmio_usrreq req;
-       int error = 0, s, progress = 0;
+       int error = 0, progress = 0;
 
        if ((uio->uio_resid % sizeof(req)) != 0)
                return (EINVAL);
@@ -494,8 +522,7 @@
                return (ENXIO);
 
        getnanotime(&ds->ds_mtime);
-       s = splsoftclock();
-       simple_lock(&ds->ds_slock);
+       mutex_enter(&ds->ds_lock);
 
        while (uio->uio_resid != 0) {
 
@@ -505,8 +532,7 @@
                                break;
                        }
                        ds->ds_flags |= DMIO_STATE_WRITE_WAIT;
-                       error = ltsleep(&ds->ds_nreqs, PRIBIO | PCATCH,
-                           "dmiowr", 0, &ds->ds_slock);
+                       error = cv_wait_sig(&ds->ds_complete_cv, &ds->ds_lock);
                        if (error)
                                break;
                        continue;
@@ -514,15 +540,13 @@
 
                ds->ds_nreqs++;
 
-               simple_unlock(&ds->ds_slock);
-               splx(s);
+               mutex_exit(&ds->ds_lock);
 
                progress = 1;
 
                error = uiomove(&req, sizeof(req), uio);
                if (error) {
-                       s = splsoftclock();
-                       simple_lock(&ds->ds_slock);
+                       mutex_enter(&ds->ds_lock);
                        ds->ds_nreqs--;
                        break;
                }
@@ -531,23 +555,17 @@
                dreq = dmover_request_alloc(ds->ds_session, NULL);
                if (dreq == NULL) {
                        /* XXX */
-                       s = splsoftclock();
-                       simple_lock(&ds->ds_slock);
                        ds->ds_nreqs--;
                        error = ENOMEM;
-                       break;
+                       return error;
                }
-               s = splsoftclock();
                dus = pool_get(&dmio_usrreq_state_pool, PR_WAITOK);
-               splx(s);
 
                error = dmio_usrreq_init(fp, dus, &req, dreq);
                if (error) {
                        dmover_request_free(dreq);
-                       s = splsoftclock();
                        pool_put(&dmio_usrreq_state_pool, dus);
-                       simple_lock(&ds->ds_slock);



Home | Main Index | Thread Index | Old Index