4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 * Memory Descriptor management routines
36 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
40 /* must be called with lnet_res_lock held */
42 lnet_md_unlink(struct lnet_libmd *md)
44 if ((md->md_flags & LNET_MD_FLAG_ZOMBIE) == 0) {
45 /* first unlink attempt... */
46 struct lnet_me *me = md->md_me;
48 md->md_flags |= LNET_MD_FLAG_ZOMBIE;
50 /* Disassociate from ME (if any), and unlink it if it was created
53 /* detach MD from portal */
54 lnet_ptl_detach_md(me, md);
55 if (me->me_unlink == LNET_UNLINK)
59 /* ensure all future handle lookups fail */
60 lnet_res_lh_invalidate(&md->md_lh);
63 if (md->md_refcount != 0) {
64 CDEBUG(D_NET, "Queueing unlink of md %p\n", md);
68 CDEBUG(D_NET, "Unlinking md %p\n", md);
70 LASSERT(!list_empty(&md->md_list));
71 list_del_init(&md->md_list);
72 LASSERT(!(md->md_flags & LNET_MD_FLAG_HANDLING));
77 lnet_kvaddr_to_page(unsigned long vaddr)
79 if (is_vmalloc_addr((void *)vaddr))
80 return vmalloc_to_page((void *)vaddr);
84 #ifdef HAVE_KMAP_TO_PAGE
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.
90 return kmap_to_page((void *)vaddr);
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");
98 return virt_to_page(vaddr);
99 #endif /* HAVE_KMAP_TO_PAGE */
102 return virt_to_page(vaddr);
103 #endif /* CONFIG_HIGHMEM */
105 EXPORT_SYMBOL(lnet_kvaddr_to_page);
108 lnet_get_first_page(struct lnet_libmd *md, unsigned int offset)
111 struct bio_vec *kiov;
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
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);
121 if (!md || md->md_niov == 0)
127 while (offset >= kiov->bv_len) {
128 offset -= kiov->bv_len;
132 CERROR("offset %d goes beyond kiov\n", offset);
137 return kiov->bv_page;
141 lnet_cpt_of_md(struct lnet_libmd *md, unsigned int offset)
144 int cpt = CFS_CPT_ANY;
146 page = lnet_get_first_page(md, offset);
148 CDEBUG(D_NET, "Couldn't resolve first page of md %p with offset %u\n",
153 cpt = cfs_cpt_of_node(lnet_cpt_table(), page_to_nid(page));
159 static int lnet_md_validate(const struct lnet_md *umd);
161 static struct lnet_libmd *
162 lnet_md_build(const struct lnet_md *umd, int unlink)
166 int total_length = 0;
167 struct lnet_libmd *lmd;
170 if (lnet_md_validate(umd) != 0)
171 return ERR_PTR(-EINVAL);
173 if (umd->options & LNET_MD_KIOV)
176 niov = DIV_ROUND_UP(offset_in_page(umd->start) + umd->length,
178 size = offsetof(struct lnet_libmd, md_kiov[niov]);
180 if (size <= LNET_SMALL_MD_SIZE) {
181 lmd = kmem_cache_zalloc(lnet_small_mds_cachep, GFP_NOFS);
184 "slab-alloced 'md' of size %u at %p.\n",
187 CDEBUG(D_MALLOC, "failed to allocate 'md' of size %u\n",
191 LIBCFS_ALLOC(lmd, size);
195 return ERR_PTR(-ENOMEM);
198 INIT_LIST_HEAD(&lmd->md_list);
200 lmd->md_start = umd->start;
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;
211 if (umd->options & LNET_MD_KIOV) {
212 memcpy(lmd->md_kiov, umd->start,
213 niov * sizeof(lmd->md_kiov[0]));
215 for (i = 0; i < (int)niov; i++) {
216 /* We take the page pointer on trust */
217 if (lmd->md_kiov[i].bv_offset +
218 lmd->md_kiov[i].bv_len > PAGE_SIZE) {
220 return ERR_PTR(-EINVAL); /* invalid length */
223 total_length += lmd->md_kiov[i].bv_len;
226 lmd->md_length = total_length;
228 if ((umd->options & LNET_MD_MAX_SIZE) && /* max size used */
229 (umd->max_size < 0 ||
230 umd->max_size > total_length)) { /* illegal max_size */
232 return ERR_PTR(-EINVAL);
234 } else { /* contiguous - split into pages */
235 void *pa = umd->start;
236 int len = umd->length;
238 lmd->md_length = len;
243 plen = min_t(int, len, PAGE_SIZE - offset_in_page(pa));
245 lmd->md_kiov[i].bv_page =
246 lnet_kvaddr_to_page((unsigned long) pa);
247 lmd->md_kiov[i].bv_offset = offset_in_page(pa);
248 lmd->md_kiov[i].bv_len = plen;
254 WARN(!(lmd->md_options & LNET_MD_GNILND) && i > LNET_MAX_IOV,
255 "Max IOV exceeded: %d should be < %d\n",
257 if ((umd->options & LNET_MD_MAX_SIZE) && /* max size used */
258 (umd->max_size < 0 ||
259 umd->max_size > (int)umd->length)) { /* illegal max_size */
261 return ERR_PTR(-EINVAL);
263 lmd->md_options |= LNET_MD_KIOV;
269 /* must be called with resource lock held */
271 lnet_md_link(struct lnet_libmd *md, lnet_handler_t handler, int cpt)
273 struct lnet_res_container *container = the_lnet.ln_md_containers[cpt];
275 /* NB we are passed an allocated, but inactive md.
276 * Caller may lnet_md_unlink() it, or may lnet_md_free() it.
278 /* This implementation doesn't know how to create START events or
279 * disable END events. Best to LASSERT our caller is compliant so
280 * we find out quickly... */
281 /* TODO - reevaluate what should be here in light of
282 * the removal of the start and end events
283 * maybe there we shouldn't even allow LNET_EQ_NONE!)
284 * LASSERT (handler != NULL);
286 md->md_handler = handler;
288 lnet_res_lh_initialize(container, &md->md_lh);
290 LASSERT(list_empty(&md->md_list));
291 list_add(&md->md_list, &container->rec_active);
294 void lnet_assert_handler_unused(lnet_handler_t handler)
296 struct lnet_res_container *container;
301 cfs_percpt_for_each(container, cpt, the_lnet.ln_md_containers) {
302 struct lnet_libmd *md;
305 list_for_each_entry(md, &container->rec_active, md_list)
306 LASSERT(md->md_handler != handler);
307 lnet_res_unlock(cpt);
310 EXPORT_SYMBOL(lnet_assert_handler_unused);
312 /* must be called with lnet_res_lock held */
314 lnet_md_deconstruct(struct lnet_libmd *lmd, struct lnet_event *ev)
316 ev->md_start = lmd->md_start;
317 ev->md_options = lmd->md_options;
318 ev->md_user_ptr = lmd->md_user_ptr;
322 lnet_md_validate(const struct lnet_md *umd)
324 if (umd->start == NULL && umd->length != 0) {
325 CERROR("MD start pointer can not be NULL with length %u\n",
330 if ((umd->options & LNET_MD_KIOV) &&
331 umd->length > LNET_MAX_IOV) {
332 CERROR("Invalid option: too many fragments %u, %d max\n",
333 umd->length, LNET_MAX_IOV);
341 * Create a memory descriptor and attach it to a ME
343 * \param me An ME to associate the new MD with.
344 * \param umd Provides initial values for the user-visible parts of a MD.
345 * Other than its use for initialization, there is no linkage between this
346 * structure and the MD maintained by the LNet.
347 * \param unlink A flag to indicate whether the MD is automatically unlinked
348 * when it becomes inactive, either because the operation threshold drops to
349 * zero or because the available memory becomes less than \a umd.max_size.
350 * (Note that the check for unlinking a MD only occurs after the completion
351 * of a successful operation on the MD.) The value LNET_UNLINK enables auto
352 * unlinking; the value LNET_RETAIN disables it.
353 * \param handle On successful returns, a handle to the newly created MD is
354 * saved here. This handle can be used later in LNetMDUnlink().
356 * The ME will either be linked to the new MD, or it will be freed.
358 * \retval 0 On success.
359 * \retval -EINVAL If \a umd is not valid.
360 * \retval -ENOMEM If new MD cannot be allocated.
363 LNetMDAttach(struct lnet_me *me, const struct lnet_md *umd,
364 enum lnet_unlink unlink, struct lnet_handle_md *handle)
368 struct lnet_libmd *md;
371 LASSERT(the_lnet.ln_refcount > 0);
374 if ((umd->options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) == 0) {
375 CERROR("Invalid option: no MD_OP set\n");
376 md = ERR_PTR(-EINVAL);
378 md = lnet_md_build(umd, unlink);
385 lnet_res_unlock(cpt);
389 lnet_md_link(md, umd->handler, cpt);
391 /* attach this MD to portal of ME and check if it matches any
392 * blocked msgs on this portal */
393 lnet_ptl_attach_md(me, md, &matches, &drops);
395 lnet_md2handle(handle, md);
397 lnet_res_unlock(cpt);
399 lnet_drop_delayed_msg_list(&drops, "Bad match");
400 lnet_recv_delayed_msg_list(&matches);
404 EXPORT_SYMBOL(LNetMDAttach);
407 * Create a "free floating" memory descriptor - a MD that is not associated
408 * with a ME. Such MDs are usually used in LNetPut() and LNetGet() operations.
410 * \param umd,unlink See the discussion for LNetMDAttach().
411 * \param handle On successful returns, a handle to the newly created MD is
412 * saved here. This handle can be used later in LNetMDUnlink(), LNetPut(),
413 * and LNetGet() operations.
415 * \retval 0 On success.
416 * \retval -EINVAL If \a umd is not valid.
417 * \retval -ENOMEM If new MD cannot be allocated.
420 LNetMDBind(const struct lnet_md *umd, enum lnet_unlink unlink,
421 struct lnet_handle_md *handle)
423 struct lnet_libmd *md;
427 LASSERT(the_lnet.ln_refcount > 0);
429 if ((umd->options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) != 0) {
430 CERROR("Invalid option: GET|PUT illegal on active MDs\n");
434 md = lnet_md_build(umd, unlink);
438 if (md->md_length > LNET_MTU) {
439 CERROR("Invalid length: too big transfer size %u, %d max\n",
440 md->md_length, LNET_MTU);
445 cpt = lnet_res_lock_current();
447 lnet_md_link(md, umd->handler, cpt);
449 lnet_md2handle(handle, md);
451 lnet_res_unlock(cpt);
458 EXPORT_SYMBOL(LNetMDBind);
461 * Unlink the memory descriptor from any ME it may be linked to and release
462 * the internal resources associated with it. As a result, active messages
463 * associated with the MD may get aborted.
465 * This function does not free the memory region associated with the MD;
466 * i.e., the memory the user allocated for this MD. If the ME associated with
467 * this MD is not NULL and was created with auto unlink enabled, the ME is
468 * unlinked as well (see LNetMEAttach()).
470 * Explicitly unlinking a MD via this function call has the same behavior as
471 * a MD that has been automatically unlinked, except that no LNET_EVENT_UNLINK
472 * is generated in the latter case.
474 * An unlinked event can be reported in two ways:
475 * - If there's no pending operations on the MD, it's unlinked immediately
476 * and an LNET_EVENT_UNLINK event is logged before this function returns.
477 * - Otherwise, the MD is only marked for deletion when this function
478 * returns, and the unlinked event will be piggybacked on the event of
479 * the completion of the last operation by setting the unlinked field of
480 * the event. No dedicated LNET_EVENT_UNLINK event is generated.
482 * Note that in both cases the unlinked field of the event is always set; no
483 * more event will happen on the MD after such an event is logged.
485 * \param mdh A handle for the MD to be unlinked.
487 * \retval 0 On success.
488 * \retval -ENOENT If \a mdh does not point to a valid MD object.
491 __LNetMDUnlink(struct lnet_handle_md mdh, bool discard)
493 struct lnet_event ev;
494 struct lnet_libmd *md = NULL;
495 lnet_handler_t handler = NULL;
498 LASSERT(the_lnet.ln_refcount > 0);
500 cpt = lnet_cpt_of_cookie(mdh.cookie);
503 md = lnet_handle2md(&mdh);
505 lnet_res_unlock(cpt);
508 if (md->md_refcount == 0 &&
509 md->md_flags & LNET_MD_FLAG_HANDLING) {
510 /* Race with unlocked call to ->md_handler. */
511 lnet_md_wait_handling(md, cpt);
516 md->md_flags |= LNET_MD_FLAG_ABORTED;
517 /* If the MD is busy, lnet_md_unlink just marks it for deletion, and
518 * when the LND is done, the completion event flags that the MD was
519 * unlinked. Otherwise, we enqueue an event now... */
520 if (md->md_handler && md->md_refcount == 0) {
521 lnet_build_unlink_event(md, &ev);
522 handler = md->md_handler;
526 md->md_flags |= LNET_MD_FLAG_DISCARD;
528 if (md->md_rspt_ptr != NULL)
529 lnet_detach_rsp_tracker(md, cpt);
533 lnet_res_unlock(cpt);
540 EXPORT_SYMBOL(__LNetMDUnlink);
543 lnet_md_discarded(struct lnet_libmd *md)
551 cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
553 rc = md->md_flags & LNET_MD_FLAG_DISCARD;
554 lnet_res_unlock(cpt);
558 EXPORT_SYMBOL(lnet_md_discarded);