Whamcloud - gitweb
LU-13004 lnet: discard kvec option from lnet_libmd.
[fs/lustre-release.git] / lnet / lnet / lib-md.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lnet/lnet/lib-md.c
33  *
34  * Memory Descriptor management routines
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38
39 #include <lnet/lib-lnet.h>
40
41 /* must be called with lnet_res_lock held */
42 void
43 lnet_md_unlink(struct lnet_libmd *md)
44 {
45         if ((md->md_flags & LNET_MD_FLAG_ZOMBIE) == 0) {
46                 /* first unlink attempt... */
47                 struct lnet_me *me = md->md_me;
48
49                 md->md_flags |= LNET_MD_FLAG_ZOMBIE;
50
51                 /* Disassociate from ME (if any), and unlink it if it was created
52                  * with LNET_UNLINK */
53                 if (me != NULL) {
54                         /* detach MD from portal */
55                         lnet_ptl_detach_md(me, md);
56                         if (me->me_unlink == LNET_UNLINK)
57                                 lnet_me_unlink(me);
58                 }
59
60                 /* ensure all future handle lookups fail */
61                 lnet_res_lh_invalidate(&md->md_lh);
62         }
63
64         if (md->md_refcount != 0) {
65                 CDEBUG(D_NET, "Queueing unlink of md %p\n", md);
66                 return;
67         }
68
69         CDEBUG(D_NET, "Unlinking md %p\n", md);
70
71         LASSERT(!list_empty(&md->md_list));
72         list_del_init(&md->md_list);
73         lnet_md_free(md);
74 }
75
76 struct page *
77 lnet_kvaddr_to_page(unsigned long vaddr)
78 {
79         if (is_vmalloc_addr((void *)vaddr))
80                 return vmalloc_to_page((void *)vaddr);
81
82 #ifdef CONFIG_HIGHMEM
83
84 #ifdef HAVE_KMAP_TO_PAGE
85         /*
86          * This ifdef is added to handle the kernel versions
87          * which have kmap_to_page() function exported. If so,
88          * we should use it. Otherwise, remain with the legacy check.
89          */
90         return kmap_to_page((void *)vaddr);
91 #else
92
93         if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
94                 /* No highmem pages only used for bulk (kiov) I/O */
95                 CERROR("find page for address in highmem\n");
96                 LBUG();
97         }
98         return virt_to_page(vaddr);
99 #endif /* HAVE_KMAP_TO_PAGE */
100 #else
101
102         return virt_to_page(vaddr);
103 #endif /* CONFIG_HIGHMEM */
104 }
105 EXPORT_SYMBOL(lnet_kvaddr_to_page);
106
107 int
108 lnet_cpt_of_md(struct lnet_libmd *md, unsigned int offset)
109 {
110         int cpt = CFS_CPT_ANY;
111         unsigned int niov;
112         struct bio_vec *kiov;
113
114         /*
115          * if the md_options has a bulk handle then we want to look at the
116          * bulk md because that's the data which we will be DMAing
117          */
118         if (md && (md->md_options & LNET_MD_BULK_HANDLE) != 0 &&
119             !LNetMDHandleIsInvalid(md->md_bulk_handle))
120                 md = lnet_handle2md(&md->md_bulk_handle);
121
122         if (!md || md->md_niov == 0)
123                 return CFS_CPT_ANY;
124
125         kiov = md->md_kiov;
126         niov = md->md_niov;
127
128         while (offset >= kiov->bv_len) {
129                 offset -= kiov->bv_len;
130                 niov--;
131                 kiov++;
132                 if (niov == 0) {
133                         CERROR("offset %d goes beyond kiov\n", offset);
134                         goto out;
135                 }
136         }
137
138         cpt = cfs_cpt_of_node(lnet_cpt_table(),
139                               page_to_nid(kiov->bv_page));
140 out:
141         return cpt;
142 }
143
144 static struct lnet_libmd *
145 lnet_md_build(struct lnet_md *umd, int unlink)
146 {
147         int i;
148         unsigned int niov;
149         int total_length = 0;
150         struct lnet_libmd *lmd;
151         unsigned int size;
152
153         if (umd->options & LNET_MD_KIOV)
154                 niov = umd->length;
155         else
156                 niov = DIV_ROUND_UP(offset_in_page(umd->start) + umd->length,
157                                     PAGE_SIZE);
158         size = offsetof(struct lnet_libmd, md_kiov[niov]);
159
160         if (size <= LNET_SMALL_MD_SIZE) {
161                 lmd = kmem_cache_zalloc(lnet_small_mds_cachep, GFP_NOFS);
162                 if (lmd) {
163                         CDEBUG(D_MALLOC,
164                                "slab-alloced 'md' of size %u at %p.\n",
165                                size, lmd);
166                 } else {
167                         CDEBUG(D_MALLOC, "failed to allocate 'md' of size %u\n",
168                                size);
169                 }
170         } else {
171                 LIBCFS_ALLOC(lmd, size);
172         }
173
174         if (!lmd)
175                 return ERR_PTR(-ENOMEM);
176
177         lmd->md_niov = niov;
178         INIT_LIST_HEAD(&lmd->md_list);
179         lmd->md_me = NULL;
180         lmd->md_start = umd->start;
181         lmd->md_offset = 0;
182         lmd->md_max_size = umd->max_size;
183         lmd->md_options = umd->options;
184         lmd->md_user_ptr = umd->user_ptr;
185         lmd->md_handler = NULL;
186         lmd->md_threshold = umd->threshold;
187         lmd->md_refcount = 0;
188         lmd->md_flags = (unlink == LNET_UNLINK) ? LNET_MD_FLAG_AUTO_UNLINK : 0;
189         lmd->md_bulk_handle = umd->bulk_handle;
190
191         if (umd->options & LNET_MD_KIOV) {
192                 memcpy(lmd->md_kiov, umd->start,
193                        niov * sizeof(lmd->md_kiov[0]));
194
195                 for (i = 0; i < (int)niov; i++) {
196                         /* We take the page pointer on trust */
197                         if (lmd->md_kiov[i].bv_offset +
198                             lmd->md_kiov[i].bv_len > PAGE_SIZE) {
199                                 lnet_md_free(lmd);
200                                 return ERR_PTR(-EINVAL); /* invalid length */
201                         }
202
203                         total_length += lmd->md_kiov[i].bv_len;
204                 }
205
206                 lmd->md_length = total_length;
207
208                 if ((umd->options & LNET_MD_MAX_SIZE) && /* max size used */
209                     (umd->max_size < 0 ||
210                      umd->max_size > total_length)) { /* illegal max_size */
211                         lnet_md_free(lmd);
212                         return ERR_PTR(-EINVAL);
213                 }
214         } else {   /* contiguous - split into pages */
215                 void *pa = umd->start;
216                 int len = umd->length;
217
218                 lmd->md_length = len;
219                 i = 0;
220                 while (len) {
221                         int plen;
222
223                         plen = min_t(int, len, PAGE_SIZE - offset_in_page(pa));
224
225                         lmd->md_kiov[i].bv_page =
226                                 lnet_kvaddr_to_page((unsigned long) pa);
227                         lmd->md_kiov[i].bv_offset = offset_in_page(pa);
228                         lmd->md_kiov[i].bv_len = plen;
229
230                         len -= plen;
231                         pa += plen;
232                         i += 1;
233                 }
234                 if ((umd->options & LNET_MD_MAX_SIZE) && /* max size used */
235                     (umd->max_size < 0 ||
236                      umd->max_size > (int)umd->length)) { /* illegal max_size */
237                         lnet_md_free(lmd);
238                         return ERR_PTR(-EINVAL);
239                 }
240                 lmd->md_options |= LNET_MD_KIOV;
241         }
242
243         return lmd;
244 }
245
246 /* must be called with resource lock held */
247 static int
248 lnet_md_link(struct lnet_libmd *md, lnet_handler_t handler, int cpt)
249 {
250         struct lnet_res_container *container = the_lnet.ln_md_containers[cpt];
251
252         /* NB we are passed an allocated, but inactive md.
253          * if we return success, caller may lnet_md_unlink() it.
254          * otherwise caller may only lnet_md_free() it.
255          */
256         /* This implementation doesn't know how to create START events or
257          * disable END events.  Best to LASSERT our caller is compliant so
258          * we find out quickly...  */
259         /*  TODO - reevaluate what should be here in light of
260          * the removal of the start and end events
261          * maybe there we shouldn't even allow LNET_EQ_NONE!)
262          * LASSERT (handler != NULL);
263          */
264         md->md_handler = handler;
265
266         lnet_res_lh_initialize(container, &md->md_lh);
267
268         LASSERT(list_empty(&md->md_list));
269         list_add(&md->md_list, &container->rec_active);
270
271         return 0;
272 }
273
274 void lnet_assert_handler_unused(lnet_handler_t handler)
275 {
276         struct lnet_res_container *container;
277         int cpt;
278
279         if (!handler)
280                 return;
281         cfs_percpt_for_each(container, cpt, the_lnet.ln_md_containers) {
282                 struct lnet_libmd *md;
283
284                 lnet_res_lock(cpt);
285                 list_for_each_entry(md, &container->rec_active, md_list)
286                         LASSERT(md->md_handler != handler);
287                 lnet_res_unlock(cpt);
288         }
289 }
290 EXPORT_SYMBOL(lnet_assert_handler_unused);
291
292 /* must be called with lnet_res_lock held */
293 void
294 lnet_md_deconstruct(struct lnet_libmd *lmd, struct lnet_event *ev)
295 {
296         ev->md_start = lmd->md_start;
297         ev->md_options = lmd->md_options;
298         ev->md_user_ptr = lmd->md_user_ptr;
299 }
300
301 static int
302 lnet_md_validate(struct lnet_md *umd)
303 {
304         if (umd->start == NULL && umd->length != 0) {
305                 CERROR("MD start pointer can not be NULL with length %u\n",
306                        umd->length);
307                 return -EINVAL;
308         }
309
310         if ((umd->options & LNET_MD_KIOV) &&
311             umd->length > LNET_MAX_IOV) {
312                 CERROR("Invalid option: too many fragments %u, %d max\n",
313                        umd->length, LNET_MAX_IOV);
314                 return -EINVAL;
315         }
316
317         return 0;
318 }
319
320 /**
321  * Create a memory descriptor and attach it to a ME
322  *
323  * \param me An ME to associate the new MD with.
324  * \param umd Provides initial values for the user-visible parts of a MD.
325  * Other than its use for initialization, there is no linkage between this
326  * structure and the MD maintained by the LNet.
327  * \param unlink A flag to indicate whether the MD is automatically unlinked
328  * when it becomes inactive, either because the operation threshold drops to
329  * zero or because the available memory becomes less than \a umd.max_size.
330  * (Note that the check for unlinking a MD only occurs after the completion
331  * of a successful operation on the MD.) The value LNET_UNLINK enables auto
332  * unlinking; the value LNET_RETAIN disables it.
333  * \param handle On successful returns, a handle to the newly created MD is
334  * saved here. This handle can be used later in LNetMDUnlink().
335  *
336  * \retval 0       On success.
337  * \retval -EINVAL If \a umd is not valid.
338  * \retval -ENOMEM If new MD cannot be allocated.
339  * \retval -ENOENT Either \a meh or \a umd.eq_handle does not point to a
340  * valid object. Note that it's OK to supply a NULL \a umd.eq_handle by
341  * calling LNetInvalidateHandle() on it.
342  * \retval -EBUSY  If the ME pointed to by \a meh is already associated with
343  * a MD.
344  */
345 int
346 LNetMDAttach(struct lnet_me *me, struct lnet_md umd,
347              enum lnet_unlink unlink, struct lnet_handle_md *handle)
348 {
349         LIST_HEAD(matches);
350         LIST_HEAD(drops);
351         struct lnet_libmd       *md;
352         int                     cpt;
353         int                     rc;
354
355         LASSERT(the_lnet.ln_refcount > 0);
356
357         if (lnet_md_validate(&umd) != 0)
358                 return -EINVAL;
359
360         if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) == 0) {
361                 CERROR("Invalid option: no MD_OP set\n");
362                 return -EINVAL;
363         }
364
365         md = lnet_md_build(&umd, unlink);
366         if (IS_ERR(md))
367                 return PTR_ERR(md);
368
369         cpt = me->me_cpt;
370
371         lnet_res_lock(cpt);
372
373         if (me->me_md)
374                 rc = -EBUSY;
375         else
376                 rc = lnet_md_link(md, umd.handler, cpt);
377
378         if (rc != 0)
379                 goto out_unlock;
380
381         /* attach this MD to portal of ME and check if it matches any
382          * blocked msgs on this portal */
383         lnet_ptl_attach_md(me, md, &matches, &drops);
384
385         lnet_md2handle(handle, md);
386
387         lnet_res_unlock(cpt);
388
389         lnet_drop_delayed_msg_list(&drops, "Bad match");
390         lnet_recv_delayed_msg_list(&matches);
391
392         return 0;
393
394 out_unlock:
395         lnet_res_unlock(cpt);
396         lnet_md_free(md);
397         return rc;
398 }
399 EXPORT_SYMBOL(LNetMDAttach);
400
401 /**
402  * Create a "free floating" memory descriptor - a MD that is not associated
403  * with a ME. Such MDs are usually used in LNetPut() and LNetGet() operations.
404  *
405  * \param umd,unlink See the discussion for LNetMDAttach().
406  * \param handle On successful returns, a handle to the newly created MD is
407  * saved here. This handle can be used later in LNetMDUnlink(), LNetPut(),
408  * and LNetGet() operations.
409  *
410  * \retval 0       On success.
411  * \retval -EINVAL If \a umd is not valid.
412  * \retval -ENOMEM If new MD cannot be allocated.
413  * \retval -ENOENT \a umd.eq_handle does not point to a valid EQ. Note that
414  * it's OK to supply a NULL \a umd.eq_handle by calling
415  * LNetInvalidateHandle() on it.
416  */
417 int
418 LNetMDBind(struct lnet_md umd, enum lnet_unlink unlink,
419            struct lnet_handle_md *handle)
420 {
421         struct lnet_libmd       *md;
422         int             cpt;
423         int             rc;
424
425         LASSERT(the_lnet.ln_refcount > 0);
426
427         if (lnet_md_validate(&umd) != 0)
428                 return -EINVAL;
429
430         if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) != 0) {
431                 CERROR("Invalid option: GET|PUT illegal on active MDs\n");
432                 return -EINVAL;
433         }
434
435         md = lnet_md_build(&umd, unlink);
436         if (IS_ERR(md))
437                 return PTR_ERR(md);
438
439         if (md->md_length > LNET_MTU) {
440                 CERROR("Invalid length: too big transfer size %u, %d max\n",
441                        md->md_length, LNET_MTU);
442                 rc = -EINVAL;
443                 goto out_free;
444         }
445
446         cpt = lnet_res_lock_current();
447
448         rc = lnet_md_link(md, umd.handler, cpt);
449         if (rc != 0)
450                 goto out_unlock;
451
452         lnet_md2handle(handle, md);
453
454         lnet_res_unlock(cpt);
455         return 0;
456
457  out_unlock:
458         lnet_res_unlock(cpt);
459
460  out_free:
461         lnet_md_free(md);
462         return rc;
463 }
464 EXPORT_SYMBOL(LNetMDBind);
465
466 /**
467  * Unlink the memory descriptor from any ME it may be linked to and release
468  * the internal resources associated with it. As a result, active messages
469  * associated with the MD may get aborted.
470  *
471  * This function does not free the memory region associated with the MD;
472  * i.e., the memory the user allocated for this MD. If the ME associated with
473  * this MD is not NULL and was created with auto unlink enabled, the ME is
474  * unlinked as well (see LNetMEAttach()).
475  *
476  * Explicitly unlinking a MD via this function call has the same behavior as
477  * a MD that has been automatically unlinked, except that no LNET_EVENT_UNLINK
478  * is generated in the latter case.
479  *
480  * An unlinked event can be reported in two ways:
481  * - If there's no pending operations on the MD, it's unlinked immediately
482  *   and an LNET_EVENT_UNLINK event is logged before this function returns.
483  * - Otherwise, the MD is only marked for deletion when this function
484  *   returns, and the unlinked event will be piggybacked on the event of
485  *   the completion of the last operation by setting the unlinked field of
486  *   the event. No dedicated LNET_EVENT_UNLINK event is generated.
487  *
488  * Note that in both cases the unlinked field of the event is always set; no
489  * more event will happen on the MD after such an event is logged.
490  *
491  * \param mdh A handle for the MD to be unlinked.
492  *
493  * \retval 0       On success.
494  * \retval -ENOENT If \a mdh does not point to a valid MD object.
495  */
496 int
497 LNetMDUnlink(struct lnet_handle_md mdh)
498 {
499         struct lnet_event ev;
500         struct lnet_libmd *md;
501         int cpt;
502
503         LASSERT(the_lnet.ln_refcount > 0);
504
505         cpt = lnet_cpt_of_cookie(mdh.cookie);
506         lnet_res_lock(cpt);
507
508         md = lnet_handle2md(&mdh);
509         if (md == NULL) {
510                 lnet_res_unlock(cpt);
511                 return -ENOENT;
512         }
513
514         md->md_flags |= LNET_MD_FLAG_ABORTED;
515         /* If the MD is busy, lnet_md_unlink just marks it for deletion, and
516          * when the LND is done, the completion event flags that the MD was
517          * unlinked. Otherwise, we enqueue an event now... */
518         if (md->md_handler && md->md_refcount == 0) {
519                 lnet_build_unlink_event(md, &ev);
520                 md->md_handler(&ev);
521         }
522
523         if (md->md_rspt_ptr != NULL)
524                 lnet_detach_rsp_tracker(md, cpt);
525
526         lnet_md_unlink(md);
527
528         lnet_res_unlock(cpt);
529         return 0;
530 }
531 EXPORT_SYMBOL(LNetMDUnlink);