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