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