Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / lnet / lnet / lib-me.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4  * Use is subject to license terms.
5  *
6  * Copyright (c) 2012, 2013, Intel Corporation.
7  */
8
9 /* This file is part of Lustre, http://www.lustre.org/
10  *
11  * Match Entry management routines
12  */
13
14 #define DEBUG_SUBSYSTEM S_LNET
15
16 #include <lnet/lib-lnet.h>
17
18 /**
19  * Create and attach a match entry to the match list of \a portal. The new
20  * ME is empty, i.e. not associated with a memory descriptor. LNetMDAttach()
21  * can be used to attach a MD to an empty ME.
22  *
23  * \param portal The portal table index where the ME should be attached.
24  * \param match_id Specifies the match criteria for the process ID of
25  * the requester. The constants LNET_PID_ANY and LNET_NID_ANY can be
26  * used to wildcard either of the identifiers in the struct lnet_process_id
27  * structure.
28  * \param match_bits,ignore_bits Specify the match criteria to apply
29  * to the match bits in the incoming request. The ignore bits are used
30  * to mask out insignificant bits in the incoming match bits. The resulting
31  * bits are then compared to the ME's match bits to determine if the
32  * incoming request meets the match criteria.
33  * \param unlink Indicates whether the ME should be unlinked when the memory
34  * descriptor associated with it is unlinked (Note that the check for
35  * unlinking a ME only occurs when the memory descriptor is unlinked.).
36  * Valid values are LNET_RETAIN and LNET_UNLINK.
37  * \param pos Indicates whether the new ME should be prepended or
38  * appended to the match list. Allowed constants: LNET_INS_BEFORE,
39  * LNET_INS_AFTER.
40  *
41  * \retval A handle to the newly created ME is returned on success
42  * \retval ERR_PTR(-EINVAL) If \a portal is invalid.
43  * \retval ERR_PTR(-ENOMEM) If new ME object cannot be allocated.
44  */
45 struct lnet_me *
46 LNetMEAttach(unsigned int portal,
47              struct lnet_processid *match_id,
48              __u64 match_bits, __u64 ignore_bits,
49              enum lnet_unlink unlink, enum lnet_ins_pos pos)
50 {
51         struct lnet_match_table *mtable;
52         struct lnet_me          *me;
53         struct list_head        *head;
54
55         LASSERT(the_lnet.ln_refcount > 0);
56
57         if ((int)portal >= the_lnet.ln_nportals)
58                 return ERR_PTR(-EINVAL);
59
60         mtable = lnet_mt_of_attach(portal, match_id,
61                                    match_bits, ignore_bits, pos);
62         if (mtable == NULL) /* can't match portal type */
63                 return ERR_PTR(-EPERM);
64
65         me = kmem_cache_zalloc(lnet_mes_cachep, GFP_NOFS);
66         if (me == NULL) {
67                 CDEBUG(D_MALLOC, "failed to allocate 'me'\n");
68                 return ERR_PTR(-ENOMEM);
69         }
70         LIBCFS_ALLOC_POST(me, sizeof(*me), "slab-alloced");
71
72         lnet_res_lock(mtable->mt_cpt);
73
74         me->me_portal = portal;
75         me->me_match_id = *match_id;
76         me->me_match_bits = match_bits;
77         me->me_ignore_bits = ignore_bits;
78         me->me_unlink = unlink;
79         me->me_md = NULL;
80
81         me->me_cpt = mtable->mt_cpt;
82
83         if (ignore_bits != 0)
84                 head = &mtable->mt_mhash[LNET_MT_HASH_IGNORE];
85         else
86                 head = lnet_mt_match_head(mtable, match_id, match_bits);
87
88         me->me_pos = head - &mtable->mt_mhash[0];
89         if (pos == LNET_INS_AFTER || pos == LNET_INS_LOCAL)
90                 list_add_tail(&me->me_list, head);
91         else
92                 list_add(&me->me_list, head);
93
94         lnet_res_unlock(mtable->mt_cpt);
95         return me;
96 }
97 EXPORT_SYMBOL(LNetMEAttach);
98
99 /* call with lnet_res_lock please */
100 void
101 lnet_me_unlink(struct lnet_me *me)
102 {
103         list_del(&me->me_list);
104
105         if (me->me_md != NULL) {
106                 struct lnet_libmd *md = me->me_md;
107
108                 /* detach MD from portal of this ME */
109                 lnet_ptl_detach_md(me, md);
110                 lnet_md_unlink(md);
111         }
112
113         LIBCFS_FREE_PRE(me, sizeof(*me), "slab-freed");
114         kmem_cache_free(lnet_mes_cachep, me);
115 }
116
117 #if 0
118 static void
119 lib_me_dump(struct lnet_me *me)
120 {
121         CWARN("Match Entry %p (%#llx)\n", me,
122               me->me_lh.lh_cookie);
123
124         CWARN("\tMatch/Ignore\t= %016lx / %016lx\n",
125               me->me_match_bits, me->me_ignore_bits);
126
127         CWARN("\tMD\t= %p\n", me->md);
128         CWARN("\tprev\t= %p\n",
129               list_entry(me->me_list.prev, struct lnet_me, me_list));
130         CWARN("\tnext\t= %p\n",
131               list_entry(me->me_list.next, struct lnet_me, me_list));
132 }
133 #endif