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