Whamcloud - gitweb
LU-6142 lnet: use list_first_entry() in lnet/lnet subdirectory.
[fs/lustre-release.git] / lnet / lnet / lib-ptl.c
index c8378d8..4196424 100644 (file)
  * GPL HEADER END
  */
 /*
- * Copyright (c) 2011, 2012, Whamcloud, Inc.
+ * Copyright (c) 2012, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * lnet/lnet/lib-ptl.c
  *
 #include <lnet/lib-lnet.h>
 
 /* NB: add /proc interfaces in upcoming patches */
-int    portal_rotor    = LNET_PTL_ROTOR_HASH_RT;
-CFS_MODULE_PARM(portal_rotor, "i", int, 0644,
-               "redirect PUTs to different cpu-partitions");
+int portal_rotor = LNET_PTL_ROTOR_HASH_RT;
+module_param(portal_rotor, int, 0644);
+MODULE_PARM_DESC(portal_rotor, "redirect PUTs to different cpu-partitions");
 
 static int
-lnet_ptl_match_type(unsigned int index, lnet_process_id_t match_id,
+lnet_ptl_match_type(unsigned int index, struct lnet_processid *match_id,
                    __u64 mbits, __u64 ignore_bits)
 {
        struct lnet_portal      *ptl = the_lnet.ln_portals[index];
        int                     unique;
 
-       unique = ignore_bits == 0 &&
-                match_id.nid != LNET_NID_ANY &&
-                match_id.pid != LNET_PID_ANY;
+       unique = (ignore_bits == 0 &&
+                 !LNET_NID_IS_ANY(&match_id->nid) &&
+                 match_id->pid != LNET_PID_ANY);
 
        LASSERT(!lnet_ptl_is_unique(ptl) || !lnet_ptl_is_wildcard(ptl));
 
@@ -136,14 +135,14 @@ lnet_ptl_disable_mt(struct lnet_portal *ptl, int cpt)
 }
 
 static int
-lnet_try_match_md(lnet_libmd_t *md,
+lnet_try_match_md(struct lnet_libmd *md,
                  struct lnet_match_info *info, struct lnet_msg *msg)
 {
        /* ALWAYS called holding the lnet_res_lock, and can't lnet_res_unlock;
         * lnet_match_blocked_msg() relies on this to avoid races */
        unsigned int    offset;
        unsigned int    mlength;
-       lnet_me_t       *me = md->md_me;
+       struct lnet_me  *me = md->md_me;
 
        /* MD exhausted */
        if (lnet_md_exhausted(md))
@@ -154,8 +153,8 @@ lnet_try_match_md(lnet_libmd_t *md,
                return LNET_MATCHMD_NONE;
 
        /* mismatched ME nid/pid? */
-       if (me->me_match_id.nid != LNET_NID_ANY &&
-           me->me_match_id.nid != info->mi_id.nid)
+       if (!LNET_NID_IS_ANY(&me->me_match_id.nid) &&
+           !nid_same(&me->me_match_id.nid, &info->mi_id.nid))
                return LNET_MATCHMD_NONE;
 
        if (me->me_match_id.pid != LNET_PID_ANY &&
@@ -184,9 +183,9 @@ lnet_try_match_md(lnet_libmd_t *md,
                mlength = info->mi_rlength;
        } else if ((md->md_options & LNET_MD_TRUNCATE) == 0) {
                /* this packet _really_ is too big */
-               CERROR("Matching packet from %s, match "LPU64
+               CERROR("Matching packet from %s, match %llu"
                       " length %d too big: %d left, %d allowed\n",
-                      libcfs_id2str(info->mi_id), info->mi_mbits,
+                      libcfs_idstr(&info->mi_id), info->mi_mbits,
                       info->mi_rlength, md->md_length - offset, mlength);
 
                return LNET_MATCHMD_DROP;
@@ -194,9 +193,9 @@ lnet_try_match_md(lnet_libmd_t *md,
 
        /* Commit to this ME/MD */
        CDEBUG(D_NET, "Incoming %s index %x from %s of "
-              "length %d/%d into md "LPX64" [%d] + %d\n",
+              "length %d/%d into md %#llx [%d] + %d\n",
               (info->mi_opc == LNET_MD_OP_PUT) ? "put" : "get",
-              info->mi_portal, libcfs_id2str(info->mi_id), mlength,
+              info->mi_portal, libcfs_idstr(&info->mi_id), mlength,
               info->mi_rlength, md->md_lh.lh_cookie, md->md_niov, offset);
 
        lnet_msg_attach_md(msg, md, offset, mlength);
@@ -215,19 +214,19 @@ lnet_try_match_md(lnet_libmd_t *md,
 }
 
 static struct lnet_match_table *
-lnet_match2mt(struct lnet_portal *ptl, lnet_process_id_t id, __u64 mbits)
+lnet_match2mt(struct lnet_portal *ptl, struct lnet_processid *id, __u64 mbits)
 {
        if (LNET_CPT_NUMBER == 1)
                return ptl->ptl_mtables[0]; /* the only one */
 
        /* if it's a unique portal, return match-table hashed by NID */
        return lnet_ptl_is_unique(ptl) ?
-              ptl->ptl_mtables[lnet_cpt_of_nid(id.nid)] : NULL;
+              ptl->ptl_mtables[lnet_nid2cpt(&id->nid, NULL)] : NULL;
 }
 
 struct lnet_match_table *
-lnet_mt_of_attach(unsigned int index, lnet_process_id_t id,
-                 __u64 mbits, __u64 ignore_bits, lnet_ins_pos_t pos)
+lnet_mt_of_attach(unsigned int index, struct lnet_processid *id,
+                 __u64 mbits, __u64 ignore_bits, enum lnet_ins_pos pos)
 {
        struct lnet_portal      *ptl;
        struct lnet_match_table *mtable;
@@ -264,10 +263,10 @@ lnet_mt_of_match(struct lnet_match_info *info, struct lnet_msg *msg)
 {
        struct lnet_match_table *mtable;
        struct lnet_portal      *ptl;
-       int                     nmaps;
-       int                     rotor;
-       int                     routed;
-       int                     cpt;
+       unsigned int            nmaps;
+       unsigned int            rotor;
+       unsigned int            cpt;
+       bool                    routed;
 
        /* NB: called w/o lock */
        LASSERT(info->mi_portal < the_lnet.ln_nportals);
@@ -275,13 +274,13 @@ lnet_mt_of_match(struct lnet_match_info *info, struct lnet_msg *msg)
 
        LASSERT(lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl));
 
-       mtable = lnet_match2mt(ptl, info->mi_id, info->mi_mbits);
+       mtable = lnet_match2mt(ptl, &info->mi_id, info->mi_mbits);
        if (mtable != NULL)
                return mtable;
 
        /* it's a wildcard portal */
-       routed = LNET_NIDNET(msg->msg_hdr.src_nid) !=
-                LNET_NIDNET(msg->msg_hdr.dest_nid);
+       routed = LNET_NID_NET(&msg->msg_hdr.src_nid) !=
+                LNET_NID_NET(&msg->msg_hdr.dest_nid);
 
        if (portal_rotor == LNET_PTL_ROTOR_OFF ||
            (portal_rotor != LNET_PTL_ROTOR_ON && !routed)) {
@@ -292,7 +291,7 @@ lnet_mt_of_match(struct lnet_match_info *info, struct lnet_msg *msg)
 
        rotor = ptl->ptl_rotor++; /* get round-robin factor */
        if (portal_rotor == LNET_PTL_ROTOR_HASH_RT && routed)
-               cpt = lnet_cpt_of_nid(msg->msg_hdr.src_nid);
+               cpt = info->mi_cpt;
        else
                cpt = rotor % LNET_CPT_NUMBER;
 
@@ -354,20 +353,20 @@ lnet_mt_set_exhausted(struct lnet_match_table *mtable, int pos, int exhausted)
                *bmap |= 1ULL << pos;
 }
 
-cfs_list_t *
+struct list_head *
 lnet_mt_match_head(struct lnet_match_table *mtable,
-                  lnet_process_id_t id, __u64 mbits)
+                  struct lnet_processid *id, __u64 mbits)
 {
        struct lnet_portal *ptl = the_lnet.ln_portals[mtable->mt_portal];
 
        if (lnet_ptl_is_wildcard(ptl)) {
                return &mtable->mt_mhash[mbits & LNET_MT_HASH_MASK];
        } else {
-               unsigned long hash = mbits + id.nid + id.pid;
+               unsigned long hash = mbits + nidhash(&id->nid) + id->pid;
 
                LASSERT(lnet_ptl_is_unique(ptl));
-               hash = cfs_hash_long(hash, LNET_MT_HASH_BITS);
-               return &mtable->mt_mhash[hash];
+               hash = hash_long(hash, LNET_MT_HASH_BITS);
+               return &mtable->mt_mhash[hash & LNET_MT_HASH_MASK];
        }
 }
 
@@ -375,23 +374,24 @@ int
 lnet_mt_match_md(struct lnet_match_table *mtable,
                 struct lnet_match_info *info, struct lnet_msg *msg)
 {
-       cfs_list_t              *head;
-       lnet_me_t               *me;
-       lnet_me_t               *tmp;
+       struct list_head        *head;
+       struct lnet_me          *me;
+       struct lnet_me          *tmp;
        int                     exhausted = 0;
        int                     rc;
 
        /* any ME with ignore bits? */
-       if (!cfs_list_empty(&mtable->mt_mhash[LNET_MT_HASH_IGNORE]))
+       if (!list_empty(&mtable->mt_mhash[LNET_MT_HASH_IGNORE]))
                head = &mtable->mt_mhash[LNET_MT_HASH_IGNORE];
        else
-               head = lnet_mt_match_head(mtable, info->mi_id, info->mi_mbits);
+               head = lnet_mt_match_head(mtable, &info->mi_id,
+                                         info->mi_mbits);
  again:
        /* NB: only wildcard portal needs to return LNET_MATCHMD_EXHAUSTED */
        if (lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]))
                exhausted = LNET_MATCHMD_EXHAUSTED;
 
-       cfs_list_for_each_entry_safe(me, tmp, head, me_list) {
+       list_for_each_entry_safe(me, tmp, head, me_list) {
                /* ME attached but MD not attached yet */
                if (me->me_md == NULL)
                        continue;
@@ -416,7 +416,8 @@ lnet_mt_match_md(struct lnet_match_table *mtable,
        }
 
        if (exhausted == 0 && head == &mtable->mt_mhash[LNET_MT_HASH_IGNORE]) {
-               head = lnet_mt_match_head(mtable, info->mi_id, info->mi_mbits);
+               head = lnet_mt_match_head(mtable, &info->mi_id,
+                                         info->mi_mbits);
                goto again; /* re-check MEs w/o ignore-bits */
        }
 
@@ -447,8 +448,8 @@ lnet_ptl_match_early(struct lnet_portal *ptl, struct lnet_msg *msg)
        if (lnet_ptl_is_lazy(ptl)) {
                if (msg->msg_rx_ready_delay) {
                        msg->msg_rx_delayed = 1;
-                       cfs_list_add_tail(&msg->msg_list,
-                                         &ptl->ptl_msg_delayed);
+                       list_add_tail(&msg->msg_list,
+                                     &ptl->ptl_msg_delayed);
                }
                rc = LNET_MATCHMD_NONE;
        } else {
@@ -467,9 +468,13 @@ lnet_ptl_match_delay(struct lnet_portal *ptl,
        int     rc = 0;
        int     i;
 
-       /* steal buffer from other CPTs, and delay it if nothing to steal,
-        * this function is more expensive than a regular match, but we
-        * don't expect it can happen a lot */
+       /*
+        * Steal buffer from other CPTs, and delay msg if nothing to
+        * steal.  This function is more expensive than a regular
+        * match, but we don't expect it can happen a lot. The return
+        * code contains one of LNET_MATCHMD_OK, LNET_MATCHMD_DROP, or
+        * LNET_MATCHMD_NONE.
+        */
        LASSERT(lnet_ptl_is_wildcard(ptl));
 
        for (i = 0; i < LNET_CPT_NUMBER; i++) {
@@ -484,50 +489,71 @@ lnet_ptl_match_delay(struct lnet_portal *ptl,
                lnet_res_lock(cpt);
                lnet_ptl_lock(ptl);
 
-               if (i == 0) { /* the first try, attach on stealing list */
-                       cfs_list_add_tail(&msg->msg_list,
-                                         &ptl->ptl_msg_stealing);
+               if (i == 0) {
+                       /* The first try, add to stealing list. */
+                       list_add_tail(&msg->msg_list,
+                                     &ptl->ptl_msg_stealing);
                }
 
-               if (!cfs_list_empty(&msg->msg_list)) { /* on stealing list */
+               if (!list_empty(&msg->msg_list)) {
+                       /* On stealing list. */
                        rc = lnet_mt_match_md(mtable, info, msg);
 
                        if ((rc & LNET_MATCHMD_EXHAUSTED) != 0 &&
                            mtable->mt_enabled)
                                lnet_ptl_disable_mt(ptl, cpt);
 
-                       if ((rc & LNET_MATCHMD_FINISH) != 0)
-                               cfs_list_del_init(&msg->msg_list);
-
-               } else {
-                       /* could be matched by lnet_ptl_attach_md()
-                        * which is called by another thread */
-                       rc = msg->msg_md == NULL ?
-                            LNET_MATCHMD_DROP : LNET_MATCHMD_OK;
-               }
-
-               if (!cfs_list_empty(&msg->msg_list) && /* not matched yet */
-                   (i == LNET_CPT_NUMBER - 1 || /* the last CPT */
-                    ptl->ptl_mt_nmaps == 0 ||   /* no active CPT */
-                    (ptl->ptl_mt_nmaps == 1 &&  /* the only active CPT */
-                     ptl->ptl_mt_maps[0] == cpt))) {
-                       /* nothing to steal, delay or drop */
-                       cfs_list_del_init(&msg->msg_list);
-
-                       if (lnet_ptl_is_lazy(ptl)) {
-                               msg->msg_rx_delayed = 1;
-                               cfs_list_add_tail(&msg->msg_list,
-                                                 &ptl->ptl_msg_delayed);
-                               rc = LNET_MATCHMD_NONE;
+                       if ((rc & LNET_MATCHMD_FINISH) != 0) {
+                               /* Match found, remove from stealing list. */
+                               list_del_init(&msg->msg_list);
+                       } else if (i == LNET_CPT_NUMBER - 1 || /* (1) */
+                                  ptl->ptl_mt_nmaps == 0 ||   /* (2) */
+                                  (ptl->ptl_mt_nmaps == 1 &&  /* (3) */
+                                   ptl->ptl_mt_maps[0] == cpt)) {
+                               /*
+                                * No match found, and this is either
+                                * (1) the last cpt to check, or
+                                * (2) there is no active cpt, or
+                                * (3) this is the only active cpt.
+                                * There is nothing to steal: delay or
+                                * drop the message.
+                                */
+                               list_del_init(&msg->msg_list);
+
+                               if (lnet_ptl_is_lazy(ptl)) {
+                                       msg->msg_rx_delayed = 1;
+                                       list_add_tail(&msg->msg_list,
+                                                     &ptl->ptl_msg_delayed);
+                                       rc = LNET_MATCHMD_NONE;
+                               } else {
+                                       rc = LNET_MATCHMD_DROP;
+                               }
                        } else {
-                               rc = LNET_MATCHMD_DROP;
+                               /* Do another iteration. */
+                               rc = 0;
                        }
+               } else {
+                       /*
+                        * No longer on stealing list: another thread
+                        * matched the message in lnet_ptl_attach_md().
+                        * We are now expected to handle the message.
+                        */
+                       rc = msg->msg_md == NULL ?
+                               LNET_MATCHMD_DROP : LNET_MATCHMD_OK;
                }
 
                lnet_ptl_unlock(ptl);
                lnet_res_unlock(cpt);
 
-               if ((rc & LNET_MATCHMD_FINISH) != 0 || msg->msg_rx_delayed)
+               /*
+                * Note that test (1) above ensures that we always
+                * exit the loop through this break statement.
+                *
+                * LNET_MATCHMD_NONE means msg was added to the
+                * delayed queue, and we may no longer reference it
+                * after lnet_ptl_unlock() and lnet_res_unlock().
+                */
+               if (rc & (LNET_MATCHMD_FINISH | LNET_MATCHMD_NONE))
                        break;
        }
 
@@ -541,8 +567,9 @@ lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg)
        struct lnet_portal      *ptl;
        int                     rc;
 
-       CDEBUG(D_NET, "Request from %s of length %d into portal %d "
-              "MB="LPX64"\n", libcfs_id2str(info->mi_id),
+       CDEBUG(D_NET,
+              "Request from %s of length %d into portal %d MB=%#llx\n",
+              libcfs_idstr(&info->mi_id),
               info->mi_rlength, info->mi_portal, info->mi_mbits);
 
        if (info->mi_portal >= the_lnet.ln_nportals) {
@@ -559,7 +586,7 @@ lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg)
        mtable = lnet_mt_of_match(info, msg);
        lnet_res_lock(mtable->mt_cpt);
 
-       if (the_lnet.ln_shutdown) {
+       if (the_lnet.ln_state != LNET_STATE_RUNNING) {
                rc = LNET_MATCHMD_DROP;
                goto out1;
        }
@@ -585,21 +612,22 @@ lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg)
                lnet_ptl_lock(ptl);
 
                msg->msg_rx_delayed = 1;
-               cfs_list_add_tail(&msg->msg_list, &ptl->ptl_msg_delayed);
+               list_add_tail(&msg->msg_list, &ptl->ptl_msg_delayed);
 
                lnet_ptl_unlock(ptl);
                lnet_res_unlock(mtable->mt_cpt);
-
-       } else  {
+               rc = LNET_MATCHMD_NONE;
+       } else  {
                lnet_res_unlock(mtable->mt_cpt);
                rc = lnet_ptl_match_delay(ptl, info, msg);
        }
 
-       if (msg->msg_rx_delayed) {
+       /* LNET_MATCHMD_NONE means msg was added to the delay queue */
+       if (rc & LNET_MATCHMD_NONE) {
                CDEBUG(D_NET,
-                      "Delaying %s from %s ptl %d MB "LPX64" off %d len %d\n",
+                      "Delaying %s from %s ptl %d MB %#llx off %d len %d\n",
                       info->mi_opc == LNET_MD_OP_PUT ? "PUT" : "GET",
-                      libcfs_id2str(info->mi_id), info->mi_portal,
+                      libcfs_idstr(&info->mi_id), info->mi_portal,
                       info->mi_mbits, info->mi_roffset, info->mi_rlength);
        }
        goto out0;
@@ -611,7 +639,7 @@ lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg)
 }
 
 void
-lnet_ptl_detach_md(lnet_me_t *me, lnet_libmd_t *md)
+lnet_ptl_detach_md(struct lnet_me *me, struct lnet_libmd *md)
 {
        LASSERT(me->me_md == md && md->md_me == me);
 
@@ -621,16 +649,16 @@ lnet_ptl_detach_md(lnet_me_t *me, lnet_libmd_t *md)
 
 /* called with lnet_res_lock held */
 void
-lnet_ptl_attach_md(lnet_me_t *me, lnet_libmd_t *md,
-                  cfs_list_t *matches, cfs_list_t *drops)
+lnet_ptl_attach_md(struct lnet_me *me, struct lnet_libmd *md,
+                  struct list_head *matches, struct list_head *drops)
 {
-       struct lnet_portal      *ptl = the_lnet.ln_portals[me->me_portal];
+       struct lnet_portal *ptl = the_lnet.ln_portals[me->me_portal];
        struct lnet_match_table *mtable;
-       cfs_list_t              *head;
-       lnet_msg_t              *tmp;
-       lnet_msg_t              *msg;
-       int                     exhausted = 0;
-       int                     cpt;
+       struct list_head *head;
+       struct lnet_msg *tmp;
+       struct lnet_msg *msg;
+       int exhausted = 0;
+       int cpt;
 
        LASSERT(md->md_refcount == 0); /* a brand new MD */
 
@@ -640,23 +668,24 @@ lnet_ptl_attach_md(lnet_me_t *me, lnet_libmd_t *md,
        cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
        mtable = ptl->ptl_mtables[cpt];
 
-       if (cfs_list_empty(&ptl->ptl_msg_stealing) &&
-           cfs_list_empty(&ptl->ptl_msg_delayed) &&
+       if (list_empty(&ptl->ptl_msg_stealing) &&
+           list_empty(&ptl->ptl_msg_delayed) &&
            !lnet_mt_test_exhausted(mtable, me->me_pos))
                return;
 
        lnet_ptl_lock(ptl);
        head = &ptl->ptl_msg_stealing;
  again:
-       cfs_list_for_each_entry_safe(msg, tmp, head, msg_list) {
+       list_for_each_entry_safe(msg, tmp, head, msg_list) {
                struct lnet_match_info  info;
-               lnet_hdr_t              *hdr;
+               struct lnet_hdr         *hdr;
                int                     rc;
 
                LASSERT(msg->msg_rx_delayed || head == &ptl->ptl_msg_stealing);
 
                hdr   = &msg->msg_hdr;
-               info.mi_id.nid  = hdr->src_nid;
+               /* Multi-Rail: Primary peer NID */
+               info.mi_id.nid  = msg->msg_initiator;
                info.mi_id.pid  = hdr->src_pid;
                info.mi_opc     = LNET_MD_OP_PUT;
                info.mi_portal  = hdr->msg.put.ptl_index;
@@ -675,7 +704,7 @@ lnet_ptl_attach_md(lnet_me_t *me, lnet_libmd_t *md,
 
                /* Hurrah! This _is_ a match */
                LASSERT((rc & LNET_MATCHMD_FINISH) != 0);
-               cfs_list_del_init(&msg->msg_list);
+               list_del_init(&msg->msg_list);
 
                if (head == &ptl->ptl_msg_stealing) {
                        if (exhausted)
@@ -685,15 +714,15 @@ lnet_ptl_attach_md(lnet_me_t *me, lnet_libmd_t *md,
                }
 
                if ((rc & LNET_MATCHMD_OK) != 0) {
-                       cfs_list_add_tail(&msg->msg_list, matches);
+                       list_add_tail(&msg->msg_list, matches);
 
                        CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
-                              "match "LPU64" offset %d length %d.\n",
-                              libcfs_id2str(info.mi_id),
+                              "match %llu offset %d length %d.\n",
+                              libcfs_idstr(&info.mi_id),
                               info.mi_portal, info.mi_mbits,
                               info.mi_roffset, info.mi_rlength);
                } else {
-                       cfs_list_add_tail(&msg->msg_list, drops);
+                       list_add_tail(&msg->msg_list, drops);
                }
 
                if (exhausted)
@@ -714,7 +743,7 @@ lnet_ptl_attach_md(lnet_me_t *me, lnet_libmd_t *md,
        lnet_ptl_unlock(ptl);
 }
 
-void
+static void
 lnet_ptl_cleanup(struct lnet_portal *ptl)
 {
        struct lnet_match_table *mtable;
@@ -723,17 +752,12 @@ lnet_ptl_cleanup(struct lnet_portal *ptl)
        if (ptl->ptl_mtables == NULL) /* uninitialized portal */
                return;
 
-       LASSERT(cfs_list_empty(&ptl->ptl_msg_delayed));
-       LASSERT(cfs_list_empty(&ptl->ptl_msg_stealing));
-#ifndef __KERNEL__
-# ifdef HAVE_LIBPTHREAD
-       pthread_mutex_destroy(&ptl->ptl_lock);
-# endif
-#endif
+       LASSERT(list_empty(&ptl->ptl_msg_delayed));
+       LASSERT(list_empty(&ptl->ptl_msg_stealing));
        cfs_percpt_for_each(mtable, i, ptl->ptl_mtables) {
-               cfs_list_t      *mhash;
-               lnet_me_t       *me;
-               int             j;
+               struct list_head *mhash;
+               struct lnet_me   *me;
+               int               j;
 
                if (mtable->mt_mhash == NULL) /* uninitialized match-table */
                        continue;
@@ -741,27 +765,30 @@ lnet_ptl_cleanup(struct lnet_portal *ptl)
                mhash = mtable->mt_mhash;
                /* cleanup ME */
                for (j = 0; j < LNET_MT_HASH_SIZE + 1; j++) {
-                       while (!cfs_list_empty(&mhash[j])) {
-                               me = cfs_list_entry(mhash[j].next,
-                                                   lnet_me_t, me_list);
+                       while ((me = list_first_entry_or_null(&mhash[j],
+                                                             struct lnet_me,
+                                                             me_list)) != NULL) {
                                CERROR("Active ME %p on exit\n", me);
-                               cfs_list_del(&me->me_list);
-                               lnet_me_free(me);
+                               list_del(&me->me_list);
+                               CDEBUG(D_MALLOC,
+                                      "slab-freed 'me' at %p in cleanup.\n",
+                                      me);
+                               kmem_cache_free(lnet_mes_cachep, me);
                        }
                }
                /* the extra entry is for MEs with ignore bits */
-               LIBCFS_FREE(mhash, sizeof(*mhash) * (LNET_MT_HASH_SIZE + 1));
+               CFS_FREE_PTR_ARRAY(mhash, LNET_MT_HASH_SIZE + 1);
        }
 
        cfs_percpt_free(ptl->ptl_mtables);
        ptl->ptl_mtables = NULL;
 }
 
-int
+static int
 lnet_ptl_setup(struct lnet_portal *ptl, int index)
 {
        struct lnet_match_table *mtable;
-       cfs_list_t              *mhash;
+       struct list_head        *mhash;
        int                     i;
        int                     j;
 
@@ -773,15 +800,9 @@ lnet_ptl_setup(struct lnet_portal *ptl, int index)
        }
 
        ptl->ptl_index = index;
-       CFS_INIT_LIST_HEAD(&ptl->ptl_msg_delayed);
-       CFS_INIT_LIST_HEAD(&ptl->ptl_msg_stealing);
-#ifdef __KERNEL__
+       INIT_LIST_HEAD(&ptl->ptl_msg_delayed);
+       INIT_LIST_HEAD(&ptl->ptl_msg_stealing);
        spin_lock_init(&ptl->ptl_lock);
-#else
-# ifdef HAVE_LIBPTHREAD
-       pthread_mutex_init(&ptl->ptl_lock, NULL);
-# endif
-#endif
        cfs_percpt_for_each(mtable, i, ptl->ptl_mtables) {
                /* the extra entry is for MEs with ignore bits */
                LIBCFS_CPT_ALLOC(mhash, lnet_cpt_table(), i,
@@ -797,7 +818,7 @@ lnet_ptl_setup(struct lnet_portal *ptl, int index)
                       LNET_MT_EXHAUSTED_BMAP);
                mtable->mt_mhash = mhash;
                for (j = 0; j < LNET_MT_HASH_SIZE + 1; j++)
-                       CFS_INIT_LIST_HEAD(&mhash[j]);
+                       INIT_LIST_HEAD(&mhash[j]);
 
                mtable->mt_portal = index;
                mtable->mt_cpt = i;
@@ -809,6 +830,7 @@ lnet_ptl_setup(struct lnet_portal *ptl, int index)
        return -ENOMEM;
 }
 
+#define PORTAL_SIZE (offsetof(struct lnet_portal, ptl_mt_maps[LNET_CPT_NUMBER]))
 void
 lnet_portals_destroy(void)
 {
@@ -818,29 +840,31 @@ lnet_portals_destroy(void)
                return;
 
        for (i = 0; i < the_lnet.ln_nportals; i++)
-               lnet_ptl_cleanup(the_lnet.ln_portals[i]);
+               if (the_lnet.ln_portals[i]) {
+                       lnet_ptl_cleanup(the_lnet.ln_portals[i]);
+                       LIBCFS_FREE(the_lnet.ln_portals[i], PORTAL_SIZE);
+               }
 
-       cfs_array_free(the_lnet.ln_portals);
+       CFS_FREE_PTR_ARRAY(the_lnet.ln_portals, the_lnet.ln_nportals);
        the_lnet.ln_portals = NULL;
 }
 
 int
 lnet_portals_create(void)
 {
-       int     size;
        int     i;
 
-       size = offsetof(struct lnet_portal, ptl_mt_maps[LNET_CPT_NUMBER]);
-
        the_lnet.ln_nportals = MAX_PORTALS;
-       the_lnet.ln_portals = cfs_array_alloc(the_lnet.ln_nportals, size);
+       CFS_ALLOC_PTR_ARRAY(the_lnet.ln_portals, the_lnet.ln_nportals);
        if (the_lnet.ln_portals == NULL) {
                CERROR("Failed to allocate portals table\n");
                return -ENOMEM;
        }
 
        for (i = 0; i < the_lnet.ln_nportals; i++) {
-               if (lnet_ptl_setup(the_lnet.ln_portals[i], i)) {
+               LIBCFS_ALLOC(the_lnet.ln_portals[i], PORTAL_SIZE);
+               if (!the_lnet.ln_portals[i] ||
+                   lnet_ptl_setup(the_lnet.ln_portals[i], i)) {
                        lnet_portals_destroy();
                        return -ENOMEM;
                }
@@ -874,7 +898,7 @@ lnet_portals_create(void)
  *
  * \param portal Index of the portal to enable the lazy attribute on.
  *
- * \retval 0       On success.
+ * \retval 0      On success.
  * \retval -EINVAL If \a portal is not a valid index.
  */
 int
@@ -900,20 +924,11 @@ LNetSetLazyPortal(int portal)
 }
 EXPORT_SYMBOL(LNetSetLazyPortal);
 
-/**
- * Turn off the lazy portal attribute. Delayed requests on the portal,
- * if any, will be all dropped when this function returns.
- *
- * \param portal Index of the portal to disable the lazy attribute on.
- *
- * \retval 0       On success.
- * \retval -EINVAL If \a portal is not a valid index.
- */
 int
-LNetClearLazyPortal(int portal)
+lnet_clear_lazy_portal(struct lnet_ni *ni, int portal, char *reason)
 {
        struct lnet_portal      *ptl;
-       CFS_LIST_HEAD           (zombies);
+       LIST_HEAD(zombies);
 
        if (portal < 0 || portal >= the_lnet.ln_nportals)
                return -EINVAL;
@@ -929,21 +944,48 @@ LNetClearLazyPortal(int portal)
                return 0;
        }
 
-       if (the_lnet.ln_shutdown)
-               CWARN("Active lazy portal %d on exit\n", portal);
-       else
-               CDEBUG(D_NET, "clearing portal %d lazy\n", portal);
+       if (ni != NULL) {
+               struct lnet_msg *msg, *tmp;
 
-       /* grab all the blocked messages atomically */
-       cfs_list_splice_init(&ptl->ptl_msg_delayed, &zombies);
+               /* grab all messages which are on the NI passed in */
+               list_for_each_entry_safe(msg, tmp, &ptl->ptl_msg_delayed,
+                                        msg_list) {
+                       if (msg->msg_txni == ni || msg->msg_rxni == ni)
+                               list_move(&msg->msg_list, &zombies);
+               }
+       } else {
+               if (the_lnet.ln_state != LNET_STATE_RUNNING)
+                       CWARN("Active lazy portal %d on exit\n", portal);
+               else
+                       CDEBUG(D_NET, "clearing portal %d lazy\n", portal);
 
-       lnet_ptl_unsetopt(ptl, LNET_PTL_LAZY);
+               /* grab all the blocked messages atomically */
+               list_splice_init(&ptl->ptl_msg_delayed, &zombies);
+
+               lnet_ptl_unsetopt(ptl, LNET_PTL_LAZY);
+       }
 
        lnet_ptl_unlock(ptl);
        lnet_res_unlock(LNET_LOCK_EX);
 
-       lnet_drop_delayed_msg_list(&zombies, "Clearing lazy portal attr");
+       lnet_drop_delayed_msg_list(&zombies, reason);
 
        return 0;
 }
+
+/**
+ * Turn off the lazy portal attribute. Delayed requests on the portal,
+ * if any, will be all dropped when this function returns.
+ *
+ * \param portal Index of the portal to disable the lazy attribute on.
+ *
+ * \retval 0      On success.
+ * \retval -EINVAL If \a portal is not a valid index.
+ */
+int
+LNetClearLazyPortal(int portal)
+{
+       return lnet_clear_lazy_portal(NULL, portal,
+                                     "Clearing lazy portal attr");
+}
 EXPORT_SYMBOL(LNetClearLazyPortal);