Whamcloud - gitweb
LU-13004 lnet: merge lnet_md_alloc into lnet_md_build.
[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
113         /*
114          * if the md_options has a bulk handle then we want to look at the
115          * bulk md because that's the data which we will be DMAing
116          */
117         if (md && (md->md_options & LNET_MD_BULK_HANDLE) != 0 &&
118             !LNetMDHandleIsInvalid(md->md_bulk_handle))
119                 md = lnet_handle2md(&md->md_bulk_handle);
120
121         if (!md || md->md_niov == 0)
122                 return CFS_CPT_ANY;
123
124         niov = md->md_niov;
125
126         /*
127          * There are three cases to handle:
128          *  1. The MD is using struct bio_vec
129          *  2. The MD is using struct kvec
130          *  3. Contiguous buffer allocated via vmalloc
131          *
132          *  in case 2 we can use virt_to_page() macro to get the page
133          *  address of the memory kvec describes.
134          *
135          *  in case 3 use is_vmalloc_addr() and vmalloc_to_page()
136          *
137          * The offset provided can be within the first iov/kiov entry or
138          * it could go beyond it. In that case we need to make sure to
139          * look at the page which actually contains the data that will be
140          * DMAed.
141          */
142         if ((md->md_options & LNET_MD_KIOV) != 0) {
143                 struct bio_vec *kiov = md->md_iov.kiov;
144
145                 while (offset >= kiov->bv_len) {
146                         offset -= kiov->bv_len;
147                         niov--;
148                         kiov++;
149                         if (niov == 0) {
150                                 CERROR("offset %d goes beyond kiov\n", offset);
151                                 goto out;
152                         }
153                 }
154
155                 cpt = cfs_cpt_of_node(lnet_cpt_table(),
156                                 page_to_nid(kiov->bv_page));
157         } else {
158                 struct kvec *iov = md->md_iov.iov;
159                 unsigned long vaddr;
160                 struct page *page;
161
162                 while (offset >= iov->iov_len) {
163                         offset -= iov->iov_len;
164                         niov--;
165                         iov++;
166                         if (niov == 0) {
167                                 CERROR("offset %d goes beyond iov\n", offset);
168                                 goto out;
169                         }
170                 }
171
172                 vaddr = ((unsigned long)iov->iov_base) + offset;
173                 page = lnet_kvaddr_to_page(vaddr);
174                 if (!page) {
175                         CERROR("Couldn't resolve vaddr 0x%lx to page\n", vaddr);
176                         goto out;
177                 }
178                 cpt = cfs_cpt_of_node(lnet_cpt_table(), page_to_nid(page));
179         }
180
181 out:
182         return cpt;
183 }
184
185 static struct lnet_libmd *
186 lnet_md_build(struct lnet_md *umd, int unlink)
187 {
188         int i;
189         unsigned int niov;
190         int total_length = 0;
191         struct lnet_libmd *lmd;
192         unsigned int size;
193
194         if ((umd->options & LNET_MD_KIOV) != 0) {
195                 niov = umd->length;
196                 size = offsetof(struct lnet_libmd, md_iov.kiov[niov]);
197         } else {
198                 niov = 1;
199                 size = offsetof(struct lnet_libmd, md_iov.iov[niov]);
200         }
201
202         if (size <= LNET_SMALL_MD_SIZE) {
203                 lmd = kmem_cache_zalloc(lnet_small_mds_cachep, GFP_NOFS);
204                 if (lmd) {
205                         CDEBUG(D_MALLOC,
206                                "slab-alloced 'md' of size %u at %p.\n",
207                                size, lmd);
208                 } else {
209                         CDEBUG(D_MALLOC, "failed to allocate 'md' of size %u\n",
210                                size);
211                 }
212         } else {
213                 LIBCFS_ALLOC(lmd, size);
214         }
215
216         if (!lmd)
217                 return ERR_PTR(-ENOMEM);
218
219         lmd->md_niov = niov;
220         INIT_LIST_HEAD(&lmd->md_list);
221
222         lmd->md_me = NULL;
223         lmd->md_start = umd->start;
224         lmd->md_offset = 0;
225         lmd->md_max_size = umd->max_size;
226         lmd->md_options = umd->options;
227         lmd->md_user_ptr = umd->user_ptr;
228         lmd->md_handler = NULL;
229         lmd->md_threshold = umd->threshold;
230         lmd->md_refcount = 0;
231         lmd->md_flags = (unlink == LNET_UNLINK) ? LNET_MD_FLAG_AUTO_UNLINK : 0;
232         lmd->md_bulk_handle = umd->bulk_handle;
233
234         if ((umd->options & LNET_MD_KIOV) != 0) {
235                 lmd->md_niov = niov = umd->length;
236                 memcpy(lmd->md_iov.kiov, umd->start,
237                        niov * sizeof(lmd->md_iov.kiov[0]));
238
239                 for (i = 0; i < (int)niov; i++) {
240                         /* We take the page pointer on trust */
241                         if (lmd->md_iov.kiov[i].bv_offset +
242                             lmd->md_iov.kiov[i].bv_len > PAGE_SIZE) {
243                                 lnet_md_free(lmd);
244                                 return ERR_PTR(-EINVAL); /* invalid length */
245                         }
246
247                         total_length += lmd->md_iov.kiov[i].bv_len;
248                 }
249
250                 lmd->md_length = total_length;
251
252                 if ((umd->options & LNET_MD_MAX_SIZE) && /* max size used */
253                     (umd->max_size < 0 ||
254                      umd->max_size > total_length)) { /* illegal max_size */
255                         lnet_md_free(lmd);
256                         return ERR_PTR(-EINVAL);
257                 }
258         } else {   /* contiguous */
259                 lmd->md_length = umd->length;
260                 lmd->md_niov = niov = 1;
261                 lmd->md_iov.iov[0].iov_base = umd->start;
262                 lmd->md_iov.iov[0].iov_len = umd->length;
263
264                 if ((umd->options & LNET_MD_MAX_SIZE) && /* max size used */
265                     (umd->max_size < 0 ||
266                      umd->max_size > (int)umd->length)) { /* illegal max_size */
267                         lnet_md_free(lmd);
268                         return ERR_PTR(-EINVAL);
269                 }
270         }
271
272         return lmd;
273 }
274
275 /* must be called with resource lock held */
276 static int
277 lnet_md_link(struct lnet_libmd *md, lnet_handler_t handler, int cpt)
278 {
279         struct lnet_res_container *container = the_lnet.ln_md_containers[cpt];
280
281         /* NB we are passed an allocated, but inactive md.
282          * if we return success, caller may lnet_md_unlink() it.
283          * otherwise caller may only lnet_md_free() it.
284          */
285         /* This implementation doesn't know how to create START events or
286          * disable END events.  Best to LASSERT our caller is compliant so
287          * we find out quickly...  */
288         /*  TODO - reevaluate what should be here in light of
289          * the removal of the start and end events
290          * maybe there we shouldn't even allow LNET_EQ_NONE!)
291          * LASSERT (handler != NULL);
292          */
293         md->md_handler = handler;
294
295         lnet_res_lh_initialize(container, &md->md_lh);
296
297         LASSERT(list_empty(&md->md_list));
298         list_add(&md->md_list, &container->rec_active);
299
300         return 0;
301 }
302
303 void lnet_assert_handler_unused(lnet_handler_t handler)
304 {
305         struct lnet_res_container *container;
306         int cpt;
307
308         if (!handler)
309                 return;
310         cfs_percpt_for_each(container, cpt, the_lnet.ln_md_containers) {
311                 struct lnet_libmd *md;
312
313                 lnet_res_lock(cpt);
314                 list_for_each_entry(md, &container->rec_active, md_list)
315                         LASSERT(md->md_handler != handler);
316                 lnet_res_unlock(cpt);
317         }
318 }
319 EXPORT_SYMBOL(lnet_assert_handler_unused);
320
321 /* must be called with lnet_res_lock held */
322 void
323 lnet_md_deconstruct(struct lnet_libmd *lmd, struct lnet_event *ev)
324 {
325         ev->md_start = lmd->md_start;
326         ev->md_options = lmd->md_options;
327         ev->md_user_ptr = lmd->md_user_ptr;
328 }
329
330 static int
331 lnet_md_validate(struct lnet_md *umd)
332 {
333         if (umd->start == NULL && umd->length != 0) {
334                 CERROR("MD start pointer can not be NULL with length %u\n",
335                        umd->length);
336                 return -EINVAL;
337         }
338
339         if ((umd->options & LNET_MD_KIOV) &&
340             umd->length > LNET_MAX_IOV) {
341                 CERROR("Invalid option: too many fragments %u, %d max\n",
342                        umd->length, LNET_MAX_IOV);
343                 return -EINVAL;
344         }
345
346         return 0;
347 }
348
349 /**
350  * Create a memory descriptor and attach it to a ME
351  *
352  * \param me An ME to associate the new MD with.
353  * \param umd Provides initial values for the user-visible parts of a MD.
354  * Other than its use for initialization, there is no linkage between this
355  * structure and the MD maintained by the LNet.
356  * \param unlink A flag to indicate whether the MD is automatically unlinked
357  * when it becomes inactive, either because the operation threshold drops to
358  * zero or because the available memory becomes less than \a umd.max_size.
359  * (Note that the check for unlinking a MD only occurs after the completion
360  * of a successful operation on the MD.) The value LNET_UNLINK enables auto
361  * unlinking; the value LNET_RETAIN disables it.
362  * \param handle On successful returns, a handle to the newly created MD is
363  * saved here. This handle can be used later in LNetMDUnlink().
364  *
365  * \retval 0       On success.
366  * \retval -EINVAL If \a umd is not valid.
367  * \retval -ENOMEM If new MD cannot be allocated.
368  * \retval -ENOENT Either \a meh or \a umd.eq_handle does not point to a
369  * valid object. Note that it's OK to supply a NULL \a umd.eq_handle by
370  * calling LNetInvalidateHandle() on it.
371  * \retval -EBUSY  If the ME pointed to by \a meh is already associated with
372  * a MD.
373  */
374 int
375 LNetMDAttach(struct lnet_me *me, struct lnet_md umd,
376              enum lnet_unlink unlink, struct lnet_handle_md *handle)
377 {
378         LIST_HEAD(matches);
379         LIST_HEAD(drops);
380         struct lnet_libmd       *md;
381         int                     cpt;
382         int                     rc;
383
384         LASSERT(the_lnet.ln_refcount > 0);
385
386         if (lnet_md_validate(&umd) != 0)
387                 return -EINVAL;
388
389         if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) == 0) {
390                 CERROR("Invalid option: no MD_OP set\n");
391                 return -EINVAL;
392         }
393
394         md = lnet_md_build(&umd, unlink);
395         if (IS_ERR(md))
396                 return PTR_ERR(md);
397
398         cpt = me->me_cpt;
399
400         lnet_res_lock(cpt);
401
402         if (me->me_md)
403                 rc = -EBUSY;
404         else
405                 rc = lnet_md_link(md, umd.handler, cpt);
406
407         if (rc != 0)
408                 goto out_unlock;
409
410         /* attach this MD to portal of ME and check if it matches any
411          * blocked msgs on this portal */
412         lnet_ptl_attach_md(me, md, &matches, &drops);
413
414         lnet_md2handle(handle, md);
415
416         lnet_res_unlock(cpt);
417
418         lnet_drop_delayed_msg_list(&drops, "Bad match");
419         lnet_recv_delayed_msg_list(&matches);
420
421         return 0;
422
423 out_unlock:
424         lnet_res_unlock(cpt);
425         lnet_md_free(md);
426         return rc;
427 }
428 EXPORT_SYMBOL(LNetMDAttach);
429
430 /**
431  * Create a "free floating" memory descriptor - a MD that is not associated
432  * with a ME. Such MDs are usually used in LNetPut() and LNetGet() operations.
433  *
434  * \param umd,unlink See the discussion for LNetMDAttach().
435  * \param handle On successful returns, a handle to the newly created MD is
436  * saved here. This handle can be used later in LNetMDUnlink(), LNetPut(),
437  * and LNetGet() operations.
438  *
439  * \retval 0       On success.
440  * \retval -EINVAL If \a umd is not valid.
441  * \retval -ENOMEM If new MD cannot be allocated.
442  * \retval -ENOENT \a umd.eq_handle does not point to a valid EQ. Note that
443  * it's OK to supply a NULL \a umd.eq_handle by calling
444  * LNetInvalidateHandle() on it.
445  */
446 int
447 LNetMDBind(struct lnet_md umd, enum lnet_unlink unlink,
448            struct lnet_handle_md *handle)
449 {
450         struct lnet_libmd       *md;
451         int             cpt;
452         int             rc;
453
454         LASSERT(the_lnet.ln_refcount > 0);
455
456         if (lnet_md_validate(&umd) != 0)
457                 return -EINVAL;
458
459         if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) != 0) {
460                 CERROR("Invalid option: GET|PUT illegal on active MDs\n");
461                 return -EINVAL;
462         }
463
464         md = lnet_md_build(&umd, unlink);
465         if (IS_ERR(md))
466                 return PTR_ERR(md);
467
468         if (md->md_length > LNET_MTU) {
469                 CERROR("Invalid length: too big transfer size %u, %d max\n",
470                        md->md_length, LNET_MTU);
471                 rc = -EINVAL;
472                 goto out_free;
473         }
474
475         cpt = lnet_res_lock_current();
476
477         rc = lnet_md_link(md, umd.handler, cpt);
478         if (rc != 0)
479                 goto out_unlock;
480
481         lnet_md2handle(handle, md);
482
483         lnet_res_unlock(cpt);
484         return 0;
485
486  out_unlock:
487         lnet_res_unlock(cpt);
488
489  out_free:
490         lnet_md_free(md);
491         return rc;
492 }
493 EXPORT_SYMBOL(LNetMDBind);
494
495 /**
496  * Unlink the memory descriptor from any ME it may be linked to and release
497  * the internal resources associated with it. As a result, active messages
498  * associated with the MD may get aborted.
499  *
500  * This function does not free the memory region associated with the MD;
501  * i.e., the memory the user allocated for this MD. If the ME associated with
502  * this MD is not NULL and was created with auto unlink enabled, the ME is
503  * unlinked as well (see LNetMEAttach()).
504  *
505  * Explicitly unlinking a MD via this function call has the same behavior as
506  * a MD that has been automatically unlinked, except that no LNET_EVENT_UNLINK
507  * is generated in the latter case.
508  *
509  * An unlinked event can be reported in two ways:
510  * - If there's no pending operations on the MD, it's unlinked immediately
511  *   and an LNET_EVENT_UNLINK event is logged before this function returns.
512  * - Otherwise, the MD is only marked for deletion when this function
513  *   returns, and the unlinked event will be piggybacked on the event of
514  *   the completion of the last operation by setting the unlinked field of
515  *   the event. No dedicated LNET_EVENT_UNLINK event is generated.
516  *
517  * Note that in both cases the unlinked field of the event is always set; no
518  * more event will happen on the MD after such an event is logged.
519  *
520  * \param mdh A handle for the MD to be unlinked.
521  *
522  * \retval 0       On success.
523  * \retval -ENOENT If \a mdh does not point to a valid MD object.
524  */
525 int
526 LNetMDUnlink(struct lnet_handle_md mdh)
527 {
528         struct lnet_event ev;
529         struct lnet_libmd *md;
530         int cpt;
531
532         LASSERT(the_lnet.ln_refcount > 0);
533
534         cpt = lnet_cpt_of_cookie(mdh.cookie);
535         lnet_res_lock(cpt);
536
537         md = lnet_handle2md(&mdh);
538         if (md == NULL) {
539                 lnet_res_unlock(cpt);
540                 return -ENOENT;
541         }
542
543         md->md_flags |= LNET_MD_FLAG_ABORTED;
544         /* If the MD is busy, lnet_md_unlink just marks it for deletion, and
545          * when the LND is done, the completion event flags that the MD was
546          * unlinked. Otherwise, we enqueue an event now... */
547         if (md->md_handler && md->md_refcount == 0) {
548                 lnet_build_unlink_event(md, &ev);
549                 md->md_handler(&ev);
550         }
551
552         if (md->md_rspt_ptr != NULL)
553                 lnet_detach_rsp_tracker(md, cpt);
554
555         lnet_md_unlink(md);
556
557         lnet_res_unlock(cpt);
558         return 0;
559 }
560 EXPORT_SYMBOL(LNetMDUnlink);