1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Use is subject to license terms.
6 * Copyright (c) 2012, 2017, Intel Corporation.
9 /* This file is part of Lustre, http://www.lustre.org/
11 * Memory Descriptor management routines
14 #define DEBUG_SUBSYSTEM S_LNET
16 #include <lnet/lib-lnet.h>
18 /* must be called with lnet_res_lock held */
20 lnet_md_unlink(struct lnet_libmd *md)
22 if ((md->md_flags & LNET_MD_FLAG_ZOMBIE) == 0) {
23 /* first unlink attempt... */
24 struct lnet_me *me = md->md_me;
26 md->md_flags |= LNET_MD_FLAG_ZOMBIE;
28 /* Disassociate from ME (if any), and unlink it if it was created
31 /* detach MD from portal */
32 lnet_ptl_detach_md(me, md);
33 if (me->me_unlink == LNET_UNLINK)
37 /* ensure all future handle lookups fail */
38 lnet_res_lh_invalidate(&md->md_lh);
41 if (md->md_refcount != 0) {
42 CDEBUG(D_NET, "Queueing unlink of md %p\n", md);
46 CDEBUG(D_NET, "Unlinking md %p\n", md);
48 LASSERT(!list_empty(&md->md_list));
49 list_del_init(&md->md_list);
50 LASSERT(!(md->md_flags & LNET_MD_FLAG_HANDLING));
55 lnet_get_first_page(struct lnet_libmd *md, unsigned int offset)
61 * if the md_options has a bulk handle then we want to look at the
62 * bulk md because that's the data which we will be DMAing
64 if (md && (md->md_options & LNET_MD_BULK_HANDLE) != 0 &&
65 !LNetMDHandleIsInvalid(md->md_bulk_handle))
66 md = lnet_handle2md(&md->md_bulk_handle);
68 if (!md || md->md_niov == 0)
74 while (offset >= kiov->bv_len) {
75 offset -= kiov->bv_len;
79 CERROR("offset %d goes beyond kiov\n", offset);
88 lnet_cpt_of_md(struct lnet_libmd *md, unsigned int offset)
91 int cpt = CFS_CPT_ANY;
93 page = lnet_get_first_page(md, offset);
95 CDEBUG(D_NET, "Couldn't resolve first page of md %p with offset %u\n",
100 cpt = cfs_cpt_of_node(lnet_cpt_table(), page_to_nid(page));
106 static int lnet_md_validate(const struct lnet_md *umd);
108 static struct lnet_libmd *
109 lnet_md_build(const struct lnet_md *umd, int unlink)
113 int total_length = 0;
114 struct lnet_libmd *lmd;
117 if (lnet_md_validate(umd) != 0)
118 return ERR_PTR(-EINVAL);
120 if (umd->options & LNET_MD_KIOV)
123 niov = DIV_ROUND_UP(offset_in_page(umd->start) + umd->length,
125 size = offsetof(struct lnet_libmd, md_kiov[niov]);
127 if (size <= LNET_SMALL_MD_SIZE) {
128 lmd = kmem_cache_zalloc(lnet_small_mds_cachep, GFP_NOFS);
130 LIBCFS_MEM_MSG(lmd, size, "slab-alloced");
132 CDEBUG(D_MALLOC, "failed to allocate 'md' of size %u\n",
136 LIBCFS_ALLOC(lmd, size);
140 return ERR_PTR(-ENOMEM);
143 INIT_LIST_HEAD(&lmd->md_list);
145 lmd->md_start = umd->start;
147 lmd->md_max_size = umd->max_size;
148 lmd->md_options = umd->options;
149 lmd->md_user_ptr = umd->user_ptr;
150 lmd->md_handler = NULL;
151 lmd->md_threshold = umd->threshold;
152 lmd->md_refcount = 0;
153 lmd->md_flags = (unlink == LNET_UNLINK) ? LNET_MD_FLAG_AUTO_UNLINK : 0;
154 lmd->md_bulk_handle = umd->bulk_handle;
156 if (umd->options & LNET_MD_GPU_ADDR)
157 lmd->md_flags |= LNET_MD_FLAG_GPU;
159 if (umd->options & LNET_MD_KIOV) {
160 memcpy(lmd->md_kiov, umd->start,
161 niov * sizeof(lmd->md_kiov[0]));
163 for (i = 0; i < (int)niov; i++) {
164 /* We take the page pointer on trust */
165 if (lmd->md_kiov[i].bv_offset +
166 lmd->md_kiov[i].bv_len > PAGE_SIZE) {
168 return ERR_PTR(-EINVAL); /* invalid length */
171 total_length += lmd->md_kiov[i].bv_len;
174 lmd->md_length = total_length;
176 if ((umd->options & LNET_MD_MAX_SIZE) && /* max size used */
177 (umd->max_size < 0 ||
178 umd->max_size > total_length)) { /* illegal max_size */
180 return ERR_PTR(-EINVAL);
182 } else { /* contiguous - split into pages */
183 void *pa = umd->start;
184 int len = umd->length;
186 lmd->md_length = len;
192 if (is_vmalloc_addr(pa))
193 p = vmalloc_to_page(pa);
195 p = virt_to_page(pa);
196 plen = min_t(int, len, PAGE_SIZE - offset_in_page(pa));
198 lmd->md_kiov[i].bv_page = p;
199 lmd->md_kiov[i].bv_offset = offset_in_page(pa);
200 lmd->md_kiov[i].bv_len = plen;
207 WARN(!(lmd->md_options & LNET_MD_GNILND) && i > LNET_MAX_IOV,
208 "Max IOV exceeded: %d should be < %d\n",
210 if ((umd->options & LNET_MD_MAX_SIZE) && /* max size used */
211 (umd->max_size < 0 ||
212 umd->max_size > (int)umd->length)) { /* illegal max_size */
214 return ERR_PTR(-EINVAL);
216 lmd->md_options |= LNET_MD_KIOV;
222 /* must be called with resource lock held */
224 lnet_md_link(struct lnet_libmd *md, lnet_handler_t handler, int cpt)
226 struct lnet_res_container *container = the_lnet.ln_md_containers[cpt];
228 /* NB we are passed an allocated, but inactive md.
229 * Caller may lnet_md_unlink() it, or may lnet_md_free() it.
231 /* This implementation doesn't know how to create START events or
232 * disable END events. Best to LASSERT our caller is compliant so
233 * we find out quickly... */
234 /* TODO - reevaluate what should be here in light of
235 * the removal of the start and end events
236 * maybe there we shouldn't even allow LNET_EQ_NONE!)
237 * LASSERT (handler != NULL);
239 md->md_handler = handler;
241 lnet_res_lh_initialize(container, &md->md_lh);
243 LASSERT(list_empty(&md->md_list));
244 list_add(&md->md_list, &container->rec_active);
247 void lnet_assert_handler_unused(lnet_handler_t handler)
249 struct lnet_res_container *container;
254 cfs_percpt_for_each(container, cpt, the_lnet.ln_md_containers) {
255 struct lnet_libmd *md;
258 list_for_each_entry(md, &container->rec_active, md_list) {
259 LASSERT(md->md_handler != handler);
261 lnet_res_unlock(cpt);
264 EXPORT_SYMBOL(lnet_assert_handler_unused);
266 /* must be called with lnet_res_lock held */
268 lnet_md_deconstruct(struct lnet_libmd *lmd, struct lnet_event *ev)
270 ev->md_start = lmd->md_start;
271 ev->md_options = lmd->md_options;
272 ev->md_user_ptr = lmd->md_user_ptr;
276 lnet_md_validate(const struct lnet_md *umd)
278 if (umd->start == NULL && umd->length != 0) {
279 CERROR("MD start pointer can not be NULL with length %u\n",
284 if ((umd->options & LNET_MD_KIOV) &&
285 umd->length > LNET_MAX_IOV) {
286 CERROR("Invalid option: too many fragments %u, %d max\n",
287 umd->length, LNET_MAX_IOV);
295 * Create a memory descriptor and attach it to a ME
297 * \param me An ME to associate the new MD with.
298 * \param umd Provides initial values for the user-visible parts of a MD.
299 * Other than its use for initialization, there is no linkage between this
300 * structure and the MD maintained by the LNet.
301 * \param unlink A flag to indicate whether the MD is automatically unlinked
302 * when it becomes inactive, either because the operation threshold drops to
303 * zero or because the available memory becomes less than \a umd.max_size.
304 * (Note that the check for unlinking a MD only occurs after the completion
305 * of a successful operation on the MD.) The value LNET_UNLINK enables auto
306 * unlinking; the value LNET_RETAIN disables it.
307 * \param handle On successful returns, a handle to the newly created MD is
308 * saved here. This handle can be used later in LNetMDUnlink().
310 * The ME will either be linked to the new MD, or it will be freed.
312 * \retval 0 On success.
313 * \retval -EINVAL If \a umd is not valid.
314 * \retval -ENOMEM If new MD cannot be allocated.
317 LNetMDAttach(struct lnet_me *me, const struct lnet_md *umd,
318 enum lnet_unlink unlink, struct lnet_handle_md *handle)
322 struct lnet_libmd *md;
325 LASSERT(the_lnet.ln_refcount > 0);
328 if ((umd->options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) == 0) {
329 CERROR("Invalid option: no MD_OP set\n");
330 md = ERR_PTR(-EINVAL);
332 md = lnet_md_build(umd, unlink);
339 lnet_res_unlock(cpt);
343 lnet_md_link(md, umd->handler, cpt);
345 /* attach this MD to portal of ME and check if it matches any
346 * blocked msgs on this portal */
347 lnet_ptl_attach_md(me, md, &matches, &drops);
349 lnet_md2handle(handle, md);
351 lnet_res_unlock(cpt);
353 lnet_drop_delayed_msg_list(&drops, "Bad match");
354 lnet_recv_delayed_msg_list(&matches);
358 EXPORT_SYMBOL(LNetMDAttach);
361 * Create a "free floating" memory descriptor - a MD that is not associated
362 * with a ME. Such MDs are usually used in LNetPut() and LNetGet() operations.
364 * \param umd,unlink See the discussion for LNetMDAttach().
365 * \param handle On successful returns, a handle to the newly created MD is
366 * saved here. This handle can be used later in LNetMDUnlink(), LNetPut(),
367 * and LNetGet() operations.
369 * \retval 0 On success.
370 * \retval -EINVAL If \a umd is not valid.
371 * \retval -ENOMEM If new MD cannot be allocated.
374 LNetMDBind(const struct lnet_md *umd, enum lnet_unlink unlink,
375 struct lnet_handle_md *handle)
377 struct lnet_libmd *md;
381 LASSERT(the_lnet.ln_refcount > 0);
383 if ((umd->options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) != 0) {
384 CERROR("Invalid option: GET|PUT illegal on active MDs\n");
388 md = lnet_md_build(umd, unlink);
392 if (md->md_length > LNET_MTU) {
393 CERROR("Invalid length: too big transfer size %u, %d max\n",
394 md->md_length, LNET_MTU);
399 cpt = lnet_res_lock_current();
401 lnet_md_link(md, umd->handler, cpt);
403 lnet_md2handle(handle, md);
405 lnet_res_unlock(cpt);
412 EXPORT_SYMBOL(LNetMDBind);
415 * Unlink the memory descriptor from any ME it may be linked to and release
416 * the internal resources associated with it. As a result, active messages
417 * associated with the MD may get aborted.
419 * This function does not free the memory region associated with the MD;
420 * i.e., the memory the user allocated for this MD. If the ME associated with
421 * this MD is not NULL and was created with auto unlink enabled, the ME is
422 * unlinked as well (see LNetMEAttach()).
424 * Explicitly unlinking a MD via this function call has the same behavior as
425 * a MD that has been automatically unlinked, except that no LNET_EVENT_UNLINK
426 * is generated in the latter case.
428 * An unlinked event can be reported in two ways:
429 * - If there's no pending operations on the MD, it's unlinked immediately
430 * and an LNET_EVENT_UNLINK event is logged before this function returns.
431 * - Otherwise, the MD is only marked for deletion when this function
432 * returns, and the unlinked event will be piggybacked on the event of
433 * the completion of the last operation by setting the unlinked field of
434 * the event. No dedicated LNET_EVENT_UNLINK event is generated.
436 * Note that in both cases the unlinked field of the event is always set; no
437 * more event will happen on the MD after such an event is logged.
439 * \param mdh A handle for the MD to be unlinked.
441 * \retval 0 On success.
442 * \retval -ENOENT If \a mdh does not point to a valid MD object.
445 __LNetMDUnlink(struct lnet_handle_md mdh, bool discard)
447 struct lnet_event ev;
448 struct lnet_libmd *md = NULL;
449 lnet_handler_t handler = NULL;
452 LASSERT(the_lnet.ln_refcount > 0);
454 cpt = lnet_cpt_of_cookie(mdh.cookie);
457 md = lnet_handle2md(&mdh);
459 lnet_res_unlock(cpt);
462 if (md->md_refcount == 0 &&
463 md->md_flags & LNET_MD_FLAG_HANDLING) {
464 /* Race with unlocked call to ->md_handler. */
465 lnet_md_wait_handling(md, cpt);
470 md->md_flags |= LNET_MD_FLAG_ABORTED;
471 /* If the MD is busy, lnet_md_unlink just marks it for deletion, and
472 * when the LND is done, the completion event flags that the MD was
473 * unlinked. Otherwise, we enqueue an event now... */
474 if (md->md_handler && md->md_refcount == 0) {
475 lnet_build_unlink_event(md, &ev);
476 handler = md->md_handler;
480 md->md_flags |= LNET_MD_FLAG_DISCARD;
482 if (md->md_rspt_ptr != NULL)
483 lnet_detach_rsp_tracker(md, cpt);
487 lnet_res_unlock(cpt);
494 EXPORT_SYMBOL(__LNetMDUnlink);
497 lnet_md_discarded(struct lnet_libmd *md)
505 cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
507 rc = md->md_flags & LNET_MD_FLAG_DISCARD;
508 lnet_res_unlock(cpt);
512 EXPORT_SYMBOL(lnet_md_discarded);