1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
9 #include <rte_string_fns.h>
11 #include "eal_memalloc.h"
12 #include "eal_memcfg.h"
13 #include "eal_private.h"
15 #include "malloc_elem.h"
16 #include "malloc_mp.h"
18 #define MP_ACTION_SYNC "mp_malloc_sync"
19 /**< request sent by primary process to notify of changes in memory map */
20 #define MP_ACTION_ROLLBACK "mp_malloc_rollback"
21 /**< request sent by primary process to notify of changes in memory map. this is
22 * essentially a regular sync request, but we cannot send sync requests while
23 * another one is in progress, and we might have to - therefore, we do this as
24 * a separate callback.
26 #define MP_ACTION_REQUEST "mp_malloc_request"
27 /**< request sent by secondary process to ask for allocation/deallocation */
28 #define MP_ACTION_RESPONSE "mp_malloc_response"
29 /**< response sent to secondary process to indicate result of request */
31 /* forward declarations */
33 handle_sync_response(const struct rte_mp_msg *request,
34 const struct rte_mp_reply *reply);
36 handle_rollback_response(const struct rte_mp_msg *request,
37 const struct rte_mp_reply *reply);
39 #define MP_TIMEOUT_S 5 /**< 5 seconds timeouts */
41 /* when we're allocating, we need to store some state to ensure that we can
44 struct primary_alloc_req_state {
45 struct malloc_heap *heap;
46 struct rte_memseg **ms;
48 struct malloc_elem *elem;
54 REQ_STATE_INACTIVE = 0,
60 TAILQ_ENTRY(mp_request) next;
61 struct malloc_mp_req user_req; /**< contents of request */
62 pthread_cond_t cond; /**< variable we use to time out on this request */
63 enum req_state state; /**< indicate status of this request */
64 struct primary_alloc_req_state alloc_state;
68 * We could've used just a single request, but it may be possible for
69 * secondaries to timeout earlier than the primary, and send a new request while
70 * primary is still expecting replies to the old one. Therefore, each new
71 * request will get assigned a new ID, which is how we will distinguish between
72 * expected and unexpected messages.
74 TAILQ_HEAD(mp_request_list, mp_request);
76 struct mp_request_list list;
79 .list = TAILQ_HEAD_INITIALIZER(mp_request_list.list),
80 .lock = PTHREAD_MUTEX_INITIALIZER
84 * General workflow is the following:
87 * S: send request to primary
88 * P: attempt to allocate memory
89 * if failed, sendmsg failure
90 * if success, send sync request
91 * S: if received msg of failure, quit
92 * if received sync request, synchronize memory map and reply with result
93 * P: if received sync request result
94 * if success, sendmsg success
95 * if failure, roll back allocation and send a rollback request
96 * S: if received msg of success, quit
97 * if received rollback request, synchronize memory map and reply with result
98 * P: if received sync request result
99 * sendmsg sync request result
100 * S: if received msg, quit
102 * Aside from timeouts, there are three points where we can quit:
103 * - if allocation failed straight away
104 * - if allocation and sync request succeeded
105 * - if allocation succeeded, sync request failed, allocation rolled back and
106 * rollback request received (irrespective of whether it succeeded or failed)
109 * S: send request to primary
110 * P: attempt to deallocate memory
111 * if failed, sendmsg failure
112 * if success, send sync request
113 * S: if received msg of failure, quit
114 * if received sync request, synchronize memory map and reply with result
115 * P: if received sync request result
116 * sendmsg sync request result
117 * S: if received msg, quit
119 * There is no "rollback" from deallocation, as it's safe to have some memory
120 * mapped in some processes - it's absent from the heap, so it won't get used.
123 static struct mp_request *
124 find_request_by_id(uint64_t id)
126 struct mp_request *req;
127 TAILQ_FOREACH(req, &mp_request_list.list, next) {
128 if (req->user_req.id == id)
134 /* this ID is, like, totally guaranteed to be absolutely unique. pinky swear. */
141 } while (find_request_by_id(id) != NULL);
145 /* secondary will respond to sync requests thusly */
147 handle_sync(const struct rte_mp_msg *msg, const void *peer)
149 struct rte_mp_msg reply;
150 const struct malloc_mp_req *req =
151 (const struct malloc_mp_req *)msg->param;
152 struct malloc_mp_req *resp =
153 (struct malloc_mp_req *)reply.param;
156 if (req->t != REQ_TYPE_SYNC) {
157 RTE_LOG(ERR, EAL, "Unexpected request from primary\n");
161 memset(&reply, 0, sizeof(reply));
164 strlcpy(reply.name, msg->name, sizeof(reply.name));
165 reply.len_param = sizeof(*resp);
167 ret = eal_memalloc_sync_with_primary();
169 resp->t = REQ_TYPE_SYNC;
171 resp->result = ret == 0 ? REQ_RESULT_SUCCESS : REQ_RESULT_FAIL;
173 return rte_mp_reply(&reply, peer);
177 handle_free_request(const struct malloc_mp_req *m)
179 const struct rte_memseg_list *msl;
183 len = m->free_req.len;
184 start = m->free_req.addr;
185 end = RTE_PTR_ADD(start, len - 1);
187 /* check if the requested memory actually exists */
188 msl = rte_mem_virt2memseg_list(start);
190 RTE_LOG(ERR, EAL, "Requested to free unknown memory\n");
194 /* check if end is within the same memory region */
195 if (rte_mem_virt2memseg_list(end) != msl) {
196 RTE_LOG(ERR, EAL, "Requested to free memory spanning multiple regions\n");
200 /* we're supposed to only free memory that's not external */
202 RTE_LOG(ERR, EAL, "Requested to free external memory\n");
206 /* now that we've validated the request, announce it */
207 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
208 m->free_req.addr, m->free_req.len);
210 /* now, do the actual freeing */
211 return malloc_heap_free_pages(m->free_req.addr, m->free_req.len);
215 handle_alloc_request(const struct malloc_mp_req *m,
216 struct mp_request *req)
218 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
219 const struct malloc_req_alloc *ar = &m->alloc_req;
220 struct malloc_heap *heap;
221 struct malloc_elem *elem;
222 struct rte_memseg **ms;
227 /* this is checked by the API, but we need to prevent divide by zero */
228 if (ar->page_sz == 0 || !rte_is_power_of_2(ar->page_sz)) {
229 RTE_LOG(ERR, EAL, "Attempting to allocate with invalid page size\n");
233 /* heap idx is index into the heap array, not socket ID */
234 if (ar->malloc_heap_idx >= RTE_MAX_HEAPS) {
235 RTE_LOG(ERR, EAL, "Attempting to allocate from invalid heap\n");
239 heap = &mcfg->malloc_heaps[ar->malloc_heap_idx];
242 * for allocations, we must only use internal heaps, but since the
243 * rte_malloc_heap_socket_is_external() is thread-safe and we're already
244 * read-locked, we'll have to take advantage of the fact that internal
245 * socket ID's are always lower than RTE_MAX_NUMA_NODES.
247 if (heap->socket_id >= RTE_MAX_NUMA_NODES) {
248 RTE_LOG(ERR, EAL, "Attempting to allocate from external heap\n");
252 alloc_sz = RTE_ALIGN_CEIL(ar->align + ar->elt_size +
253 MALLOC_ELEM_OVERHEAD, ar->page_sz);
254 n_segs = alloc_sz / ar->page_sz;
256 /* we can't know in advance how many pages we'll need, so we malloc */
257 ms = malloc(sizeof(*ms) * n_segs);
259 RTE_LOG(ERR, EAL, "Couldn't allocate memory for request state\n");
262 memset(ms, 0, sizeof(*ms) * n_segs);
264 elem = alloc_pages_on_heap(heap, ar->page_sz, ar->elt_size, ar->socket,
265 ar->flags, ar->align, ar->bound, ar->contig, ms,
271 map_addr = ms[0]->addr;
273 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_ALLOC, map_addr, alloc_sz);
275 /* we have succeeded in allocating memory, but we still need to sync
276 * with other processes. however, since DPDK IPC is single-threaded, we
277 * send an asynchronous request and exit this callback.
280 req->alloc_state.ms = ms;
281 req->alloc_state.ms_len = n_segs;
282 req->alloc_state.map_addr = map_addr;
283 req->alloc_state.map_len = alloc_sz;
284 req->alloc_state.elem = elem;
285 req->alloc_state.heap = heap;
293 /* first stage of primary handling requests from secondary */
295 handle_request(const struct rte_mp_msg *msg, const void *peer __rte_unused)
297 const struct malloc_mp_req *m =
298 (const struct malloc_mp_req *)msg->param;
299 struct mp_request *entry;
302 /* lock access to request */
303 pthread_mutex_lock(&mp_request_list.lock);
305 /* make sure it's not a dupe */
306 entry = find_request_by_id(m->id);
308 RTE_LOG(ERR, EAL, "Duplicate request id\n");
312 entry = malloc(sizeof(*entry));
314 RTE_LOG(ERR, EAL, "Unable to allocate memory for request\n");
319 memset(entry, 0, sizeof(*entry));
321 if (m->t == REQ_TYPE_ALLOC) {
322 ret = handle_alloc_request(m, entry);
323 } else if (m->t == REQ_TYPE_FREE) {
324 ret = handle_free_request(m);
326 RTE_LOG(ERR, EAL, "Unexpected request from secondary\n");
331 struct rte_mp_msg resp_msg;
332 struct malloc_mp_req *resp =
333 (struct malloc_mp_req *)resp_msg.param;
335 /* send failure message straight away */
336 resp_msg.num_fds = 0;
337 resp_msg.len_param = sizeof(*resp);
338 strlcpy(resp_msg.name, MP_ACTION_RESPONSE,
339 sizeof(resp_msg.name));
342 resp->result = REQ_RESULT_FAIL;
345 if (rte_mp_sendmsg(&resp_msg)) {
346 RTE_LOG(ERR, EAL, "Couldn't send response\n");
349 /* we did not modify the request */
352 struct rte_mp_msg sr_msg;
353 struct malloc_mp_req *sr =
354 (struct malloc_mp_req *)sr_msg.param;
357 memset(&sr_msg, 0, sizeof(sr_msg));
359 /* we can do something, so send sync request asynchronously */
361 sr_msg.len_param = sizeof(*sr);
362 strlcpy(sr_msg.name, MP_ACTION_SYNC, sizeof(sr_msg.name));
365 ts.tv_sec = MP_TIMEOUT_S;
367 /* sync requests carry no data */
368 sr->t = REQ_TYPE_SYNC;
371 /* there may be stray timeout still waiting */
373 ret = rte_mp_request_async(&sr_msg, &ts,
374 handle_sync_response);
375 } while (ret != 0 && rte_errno == EEXIST);
377 RTE_LOG(ERR, EAL, "Couldn't send sync request\n");
378 if (m->t == REQ_TYPE_ALLOC)
379 free(entry->alloc_state.ms);
383 /* mark request as in progress */
384 memcpy(&entry->user_req, m, sizeof(*m));
385 entry->state = REQ_STATE_ACTIVE;
387 TAILQ_INSERT_TAIL(&mp_request_list.list, entry, next);
389 pthread_mutex_unlock(&mp_request_list.lock);
392 pthread_mutex_unlock(&mp_request_list.lock);
397 /* callback for asynchronous sync requests for primary. this will either do a
398 * sendmsg with results, or trigger rollback request.
401 handle_sync_response(const struct rte_mp_msg *request,
402 const struct rte_mp_reply *reply)
404 enum malloc_req_result result;
405 struct mp_request *entry;
406 const struct malloc_mp_req *mpreq =
407 (const struct malloc_mp_req *)request->param;
410 /* lock the request */
411 pthread_mutex_lock(&mp_request_list.lock);
413 entry = find_request_by_id(mpreq->id);
415 RTE_LOG(ERR, EAL, "Wrong request ID\n");
419 result = REQ_RESULT_SUCCESS;
421 if (reply->nb_received != reply->nb_sent)
422 result = REQ_RESULT_FAIL;
424 for (i = 0; i < reply->nb_received; i++) {
425 struct malloc_mp_req *resp =
426 (struct malloc_mp_req *)reply->msgs[i].param;
428 if (resp->t != REQ_TYPE_SYNC) {
429 RTE_LOG(ERR, EAL, "Unexpected response to sync request\n");
430 result = REQ_RESULT_FAIL;
433 if (resp->id != entry->user_req.id) {
434 RTE_LOG(ERR, EAL, "Response to wrong sync request\n");
435 result = REQ_RESULT_FAIL;
438 if (resp->result == REQ_RESULT_FAIL) {
439 result = REQ_RESULT_FAIL;
444 if (entry->user_req.t == REQ_TYPE_FREE) {
445 struct rte_mp_msg msg;
446 struct malloc_mp_req *resp = (struct malloc_mp_req *)msg.param;
448 memset(&msg, 0, sizeof(msg));
450 /* this is a free request, just sendmsg result */
451 resp->t = REQ_TYPE_FREE;
452 resp->result = result;
453 resp->id = entry->user_req.id;
455 msg.len_param = sizeof(*resp);
456 strlcpy(msg.name, MP_ACTION_RESPONSE, sizeof(msg.name));
458 if (rte_mp_sendmsg(&msg))
459 RTE_LOG(ERR, EAL, "Could not send message to secondary process\n");
461 TAILQ_REMOVE(&mp_request_list.list, entry, next);
463 } else if (entry->user_req.t == REQ_TYPE_ALLOC &&
464 result == REQ_RESULT_SUCCESS) {
465 struct malloc_heap *heap = entry->alloc_state.heap;
466 struct rte_mp_msg msg;
467 struct malloc_mp_req *resp =
468 (struct malloc_mp_req *)msg.param;
470 memset(&msg, 0, sizeof(msg));
472 heap->total_size += entry->alloc_state.map_len;
474 /* result is success, so just notify secondary about this */
475 resp->t = REQ_TYPE_ALLOC;
476 resp->result = result;
477 resp->id = entry->user_req.id;
479 msg.len_param = sizeof(*resp);
480 strlcpy(msg.name, MP_ACTION_RESPONSE, sizeof(msg.name));
482 if (rte_mp_sendmsg(&msg))
483 RTE_LOG(ERR, EAL, "Could not send message to secondary process\n");
485 TAILQ_REMOVE(&mp_request_list.list, entry, next);
486 free(entry->alloc_state.ms);
488 } else if (entry->user_req.t == REQ_TYPE_ALLOC &&
489 result == REQ_RESULT_FAIL) {
490 struct rte_mp_msg rb_msg;
491 struct malloc_mp_req *rb =
492 (struct malloc_mp_req *)rb_msg.param;
494 struct primary_alloc_req_state *state =
498 memset(&rb_msg, 0, sizeof(rb_msg));
500 /* we've failed to sync, so do a rollback */
501 eal_memalloc_mem_event_notify(RTE_MEM_EVENT_FREE,
502 state->map_addr, state->map_len);
504 rollback_expand_heap(state->ms, state->ms_len, state->elem,
505 state->map_addr, state->map_len);
507 /* send rollback request */
509 rb_msg.len_param = sizeof(*rb);
510 strlcpy(rb_msg.name, MP_ACTION_ROLLBACK, sizeof(rb_msg.name));
513 ts.tv_sec = MP_TIMEOUT_S;
515 /* sync requests carry no data */
516 rb->t = REQ_TYPE_SYNC;
517 rb->id = entry->user_req.id;
519 /* there may be stray timeout still waiting */
521 ret = rte_mp_request_async(&rb_msg, &ts,
522 handle_rollback_response);
523 } while (ret != 0 && rte_errno == EEXIST);
525 RTE_LOG(ERR, EAL, "Could not send rollback request to secondary process\n");
527 /* we couldn't send rollback request, but that's OK -
528 * secondary will time out, and memory has been removed
531 TAILQ_REMOVE(&mp_request_list.list, entry, next);
537 RTE_LOG(ERR, EAL, " to sync request of unknown type\n");
541 pthread_mutex_unlock(&mp_request_list.lock);
544 pthread_mutex_unlock(&mp_request_list.lock);
549 handle_rollback_response(const struct rte_mp_msg *request,
550 const struct rte_mp_reply *reply __rte_unused)
552 struct rte_mp_msg msg;
553 struct malloc_mp_req *resp = (struct malloc_mp_req *)msg.param;
554 const struct malloc_mp_req *mpreq =
555 (const struct malloc_mp_req *)request->param;
556 struct mp_request *entry;
558 /* lock the request */
559 pthread_mutex_lock(&mp_request_list.lock);
561 memset(&msg, 0, sizeof(msg));
563 entry = find_request_by_id(mpreq->id);
565 RTE_LOG(ERR, EAL, "Wrong request ID\n");
569 if (entry->user_req.t != REQ_TYPE_ALLOC) {
570 RTE_LOG(ERR, EAL, "Unexpected active request\n");
574 /* we don't care if rollback succeeded, request still failed */
575 resp->t = REQ_TYPE_ALLOC;
576 resp->result = REQ_RESULT_FAIL;
577 resp->id = mpreq->id;
579 msg.len_param = sizeof(*resp);
580 strlcpy(msg.name, MP_ACTION_RESPONSE, sizeof(msg.name));
582 if (rte_mp_sendmsg(&msg))
583 RTE_LOG(ERR, EAL, "Could not send message to secondary process\n");
586 TAILQ_REMOVE(&mp_request_list.list, entry, next);
587 free(entry->alloc_state.ms);
590 pthread_mutex_unlock(&mp_request_list.lock);
593 pthread_mutex_unlock(&mp_request_list.lock);
597 /* final stage of the request from secondary */
599 handle_response(const struct rte_mp_msg *msg, const void *peer __rte_unused)
601 const struct malloc_mp_req *m =
602 (const struct malloc_mp_req *)msg->param;
603 struct mp_request *entry;
605 pthread_mutex_lock(&mp_request_list.lock);
607 entry = find_request_by_id(m->id);
609 /* update request status */
610 entry->user_req.result = m->result;
612 entry->state = REQ_STATE_COMPLETE;
614 /* trigger thread wakeup */
615 pthread_cond_signal(&entry->cond);
618 pthread_mutex_unlock(&mp_request_list.lock);
623 /* synchronously request memory map sync, this is only called whenever primary
624 * process initiates the allocation.
629 struct rte_mp_msg msg;
630 struct rte_mp_reply reply;
631 struct malloc_mp_req *req = (struct malloc_mp_req *)msg.param;
635 memset(&msg, 0, sizeof(msg));
636 memset(&reply, 0, sizeof(reply));
638 /* no need to create tailq entries as this is entirely synchronous */
641 msg.len_param = sizeof(*req);
642 strlcpy(msg.name, MP_ACTION_SYNC, sizeof(msg.name));
644 /* sync request carries no data */
645 req->t = REQ_TYPE_SYNC;
646 req->id = get_unique_id();
649 ts.tv_sec = MP_TIMEOUT_S;
651 /* there may be stray timeout still waiting */
653 ret = rte_mp_request_sync(&msg, &reply, &ts);
654 } while (ret != 0 && rte_errno == EEXIST);
656 /* if IPC is unsupported, behave as if the call succeeded */
657 if (rte_errno != ENOTSUP)
658 RTE_LOG(ERR, EAL, "Could not send sync request to secondary process\n");
664 if (reply.nb_received != reply.nb_sent) {
665 RTE_LOG(ERR, EAL, "Not all secondaries have responded\n");
669 for (i = 0; i < reply.nb_received; i++) {
670 struct malloc_mp_req *resp =
671 (struct malloc_mp_req *)reply.msgs[i].param;
672 if (resp->t != REQ_TYPE_SYNC) {
673 RTE_LOG(ERR, EAL, "Unexpected response from secondary\n");
676 if (resp->id != req->id) {
677 RTE_LOG(ERR, EAL, "Wrong request ID\n");
680 if (resp->result != REQ_RESULT_SUCCESS) {
681 RTE_LOG(ERR, EAL, "Secondary process failed to synchronize\n");
692 /* this is a synchronous wrapper around a bunch of asynchronous requests to
693 * primary process. this will initiate a request and wait until responses come.
696 request_to_primary(struct malloc_mp_req *user_req)
698 struct rte_mp_msg msg;
699 struct malloc_mp_req *msg_req = (struct malloc_mp_req *)msg.param;
700 struct mp_request *entry;
705 memset(&msg, 0, sizeof(msg));
706 memset(&ts, 0, sizeof(ts));
708 pthread_mutex_lock(&mp_request_list.lock);
710 entry = malloc(sizeof(*entry));
712 RTE_LOG(ERR, EAL, "Cannot allocate memory for request\n");
716 memset(entry, 0, sizeof(*entry));
718 if (gettimeofday(&now, NULL) < 0) {
719 RTE_LOG(ERR, EAL, "Cannot get current time\n");
723 ts.tv_nsec = (now.tv_usec * 1000) % 1000000000;
724 ts.tv_sec = now.tv_sec + MP_TIMEOUT_S +
725 (now.tv_usec * 1000) / 1000000000;
727 /* initialize the request */
728 pthread_cond_init(&entry->cond, NULL);
731 msg.len_param = sizeof(*msg_req);
732 strlcpy(msg.name, MP_ACTION_REQUEST, sizeof(msg.name));
734 /* (attempt to) get a unique id */
735 user_req->id = get_unique_id();
737 /* copy contents of user request into the message */
738 memcpy(msg_req, user_req, sizeof(*msg_req));
740 if (rte_mp_sendmsg(&msg)) {
741 RTE_LOG(ERR, EAL, "Cannot send message to primary\n");
745 /* copy contents of user request into active request */
746 memcpy(&entry->user_req, user_req, sizeof(*user_req));
748 /* mark request as in progress */
749 entry->state = REQ_STATE_ACTIVE;
751 TAILQ_INSERT_TAIL(&mp_request_list.list, entry, next);
753 /* finally, wait on timeout */
755 ret = pthread_cond_timedwait(&entry->cond,
756 &mp_request_list.lock, &ts);
757 } while (ret != 0 && ret != ETIMEDOUT);
759 if (entry->state != REQ_STATE_COMPLETE) {
760 RTE_LOG(ERR, EAL, "Request timed out\n");
764 user_req->result = entry->user_req.result;
766 TAILQ_REMOVE(&mp_request_list.list, entry, next);
769 pthread_mutex_unlock(&mp_request_list.lock);
772 pthread_mutex_unlock(&mp_request_list.lock);
778 register_mp_requests(void)
780 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
781 /* it's OK for primary to not support IPC */
782 if (rte_mp_action_register(MP_ACTION_REQUEST, handle_request) &&
783 rte_errno != ENOTSUP) {
784 RTE_LOG(ERR, EAL, "Couldn't register '%s' action\n",
789 if (rte_mp_action_register(MP_ACTION_SYNC, handle_sync)) {
790 RTE_LOG(ERR, EAL, "Couldn't register '%s' action\n",
794 if (rte_mp_action_register(MP_ACTION_ROLLBACK, handle_sync)) {
795 RTE_LOG(ERR, EAL, "Couldn't register '%s' action\n",
799 if (rte_mp_action_register(MP_ACTION_RESPONSE,
801 RTE_LOG(ERR, EAL, "Couldn't register '%s' action\n",
810 unregister_mp_requests(void)
812 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
813 rte_mp_action_unregister(MP_ACTION_REQUEST);
815 rte_mp_action_unregister(MP_ACTION_SYNC);
816 rte_mp_action_unregister(MP_ACTION_ROLLBACK);
817 rte_mp_action_unregister(MP_ACTION_RESPONSE);