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