xref: /unit/src/nxt_port_memory.c (revision 564:762f8c976ead)
1 
2 /*
3  * Copyright (C) Max Romanov
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 
9 #if (NXT_HAVE_MEMFD_CREATE)
10 
11 #include <linux/memfd.h>
12 #include <unistd.h>
13 #include <sys/syscall.h>
14 
15 #endif
16 
17 #include <nxt_port_memory_int.h>
18 
19 
20 nxt_inline void
21 nxt_port_mmap_handler_use(nxt_port_mmap_handler_t *mmap_handler, int i)
22 {
23     int c;
24 
25     c = nxt_atomic_fetch_add(&mmap_handler->use_count, i);
26 
27     if (i < 0 && c == -i) {
28         if (mmap_handler->hdr != NULL) {
29             nxt_mem_munmap(mmap_handler->hdr, PORT_MMAP_SIZE);
30             mmap_handler->hdr = NULL;
31         }
32 
33         nxt_free(mmap_handler);
34     }
35 }
36 
37 
38 static nxt_port_mmap_t *
39 nxt_port_mmap_at(nxt_port_mmaps_t *port_mmaps, uint32_t i)
40 {
41     uint32_t  cap;
42 
43     cap = port_mmaps->cap;
44 
45     if (cap == 0) {
46         cap = i + 1;
47     }
48 
49     while (i + 1 > cap) {
50 
51         if (cap < 16) {
52           cap = cap * 2;
53 
54         } else {
55           cap = cap + cap / 2;
56         }
57     }
58 
59     if (cap != port_mmaps->cap) {
60 
61         port_mmaps->elts = nxt_realloc(port_mmaps->elts,
62                                        cap * sizeof(nxt_port_mmap_t));
63         if (nxt_slow_path(port_mmaps->elts == NULL)) {
64             return NULL;
65         }
66 
67         nxt_memzero(port_mmaps->elts + port_mmaps->cap,
68                     sizeof(nxt_port_mmap_t) * (cap - port_mmaps->cap));
69 
70         port_mmaps->cap = cap;
71     }
72 
73     if (i + 1 > port_mmaps->size) {
74         port_mmaps->size = i + 1;
75     }
76 
77     return port_mmaps->elts + i;
78 }
79 
80 
81 void
82 nxt_port_mmaps_destroy(nxt_port_mmaps_t *port_mmaps, nxt_bool_t free_elts)
83 {
84     uint32_t         i;
85     nxt_port_mmap_t  *port_mmap;
86 
87     if (port_mmaps == NULL) {
88         return;
89     }
90 
91     port_mmap = port_mmaps->elts;
92 
93     for (i = 0; i < port_mmaps->size; i++) {
94         nxt_port_mmap_handler_use(port_mmap[i].mmap_handler, -1);
95     }
96 
97     port_mmaps->size = 0;
98 
99     if (free_elts != 0) {
100         nxt_free(port_mmaps->elts);
101     }
102 }
103 
104 
105 #define nxt_port_mmap_free_junk(p, size)                                      \
106     memset((p), 0xA5, size)
107 
108 
109 static void
110 nxt_port_mmap_buf_completion(nxt_task_t *task, void *obj, void *data)
111 {
112     u_char                   *p;
113     nxt_mp_t                 *mp;
114     nxt_buf_t                *b;
115     nxt_chunk_id_t           c;
116     nxt_port_mmap_header_t   *hdr;
117     nxt_port_mmap_handler_t  *mmap_handler;
118 
119     if (nxt_buf_ts_handle(task, obj, data)) {
120         return;
121     }
122 
123     b = obj;
124 
125     mp = b->data;
126 
127     nxt_assert(data == b->parent);
128 
129     mmap_handler = data;
130     hdr = mmap_handler->hdr;
131 
132     if (nxt_slow_path(hdr->src_pid != nxt_pid && hdr->dst_pid != nxt_pid)) {
133         nxt_debug(task, "mmap buf completion: mmap for other process pair "
134                   "%PI->%PI", hdr->src_pid, hdr->dst_pid);
135 
136         goto release_buf;
137     }
138 
139     if (b->is_port_mmap_sent && b->mem.pos > b->mem.start) {
140         /*
141          * Chunks until b->mem.pos has been sent to other side,
142          * let's release rest (if any).
143          */
144         p = b->mem.pos - 1;
145         c = nxt_port_mmap_chunk_id(hdr, p) + 1;
146         p = nxt_port_mmap_chunk_start(hdr, c);
147 
148     } else {
149         p = b->mem.start;
150         c = nxt_port_mmap_chunk_id(hdr, p);
151     }
152 
153     nxt_port_mmap_free_junk(p, b->mem.end - p);
154 
155     nxt_debug(task, "mmap buf completion: %p [%p,%uz] (sent=%d), "
156               "%PI->%PI,%d,%d", b, b->mem.start, b->mem.end - b->mem.start,
157               b->is_port_mmap_sent, hdr->src_pid, hdr->dst_pid, hdr->id, c);
158 
159     while (p < b->mem.end) {
160         nxt_port_mmap_set_chunk_free(hdr->free_map, c);
161 
162         p += PORT_MMAP_CHUNK_SIZE;
163         c++;
164     }
165 
166 release_buf:
167 
168     nxt_port_mmap_handler_use(mmap_handler, -1);
169 
170     nxt_mp_free(mp, b);
171     nxt_mp_release(mp);
172 }
173 
174 
175 nxt_port_mmap_handler_t *
176 nxt_port_incoming_port_mmap(nxt_task_t *task, nxt_process_t *process,
177     nxt_fd_t fd)
178 {
179     void                     *mem;
180     struct stat              mmap_stat;
181     nxt_port_mmap_t          *port_mmap;
182     nxt_port_mmap_header_t   *hdr;
183     nxt_port_mmap_handler_t  *mmap_handler;
184 
185     nxt_debug(task, "got new mmap fd #%FD from process %PI",
186               fd, process->pid);
187 
188     port_mmap = NULL;
189 
190     if (fstat(fd, &mmap_stat) == -1) {
191         nxt_log(task, NXT_LOG_WARN, "fstat(%FD) failed %E", fd, nxt_errno);
192 
193         return NULL;
194     }
195 
196     mem = nxt_mem_mmap(NULL, mmap_stat.st_size,
197                        PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
198 
199     if (nxt_slow_path(mem == MAP_FAILED)) {
200         nxt_log(task, NXT_LOG_WARN, "mmap() failed %E", nxt_errno);
201 
202         return NULL;
203     }
204 
205     hdr = mem;
206 
207     mmap_handler = nxt_zalloc(sizeof(nxt_port_mmap_handler_t));
208     if (nxt_slow_path(mmap_handler == NULL)) {
209         nxt_log(task, NXT_LOG_WARN, "failed to allocate mmap_handler");
210 
211         nxt_mem_munmap(mem, PORT_MMAP_SIZE);
212 
213         return NULL;
214     }
215 
216     mmap_handler->hdr = hdr;
217 
218     if (nxt_slow_path(hdr->src_pid != process->pid
219                       || hdr->dst_pid != nxt_pid))
220     {
221         nxt_log(task, NXT_LOG_WARN, "unexpected pid in mmap header detected: "
222                 "%PI != %PI or %PI != %PI", hdr->src_pid, process->pid,
223                 hdr->dst_pid, nxt_pid);
224 
225         return NULL;
226     }
227 
228     nxt_thread_mutex_lock(&process->incoming.mutex);
229 
230     port_mmap = nxt_port_mmap_at(&process->incoming, hdr->id);
231     if (nxt_slow_path(port_mmap == NULL)) {
232         nxt_log(task, NXT_LOG_WARN, "failed to add mmap to incoming array");
233 
234         nxt_mem_munmap(mem, PORT_MMAP_SIZE);
235         hdr = NULL;
236 
237         nxt_free(mmap_handler);
238         mmap_handler = NULL;
239 
240         goto fail;
241     }
242 
243     port_mmap->mmap_handler = mmap_handler;
244     nxt_port_mmap_handler_use(mmap_handler, 1);
245 
246     hdr->sent_over = 0xFFFFu;
247 
248 fail:
249 
250     nxt_thread_mutex_unlock(&process->incoming.mutex);
251 
252     return mmap_handler;
253 }
254 
255 
256 static nxt_port_mmap_handler_t *
257 nxt_port_new_port_mmap(nxt_task_t *task, nxt_process_t *process,
258     nxt_port_t *port, nxt_bool_t tracking)
259 {
260     void                     *mem;
261     u_char                   *p, name[64];
262     nxt_fd_t                 fd;
263     nxt_free_map_t           *free_map;
264     nxt_port_mmap_t          *port_mmap;
265     nxt_port_mmap_header_t   *hdr;
266     nxt_port_mmap_handler_t  *mmap_handler;
267 
268     mmap_handler = nxt_zalloc(sizeof(nxt_port_mmap_handler_t));
269     if (nxt_slow_path(mmap_handler == NULL)) {
270         nxt_log(task, NXT_LOG_WARN, "failed to allocate mmap_handler");
271 
272         return NULL;
273     }
274 
275     port_mmap = nxt_port_mmap_at(&process->outgoing, process->outgoing.size);
276     if (nxt_slow_path(port_mmap == NULL)) {
277         nxt_log(task, NXT_LOG_WARN,
278                 "failed to add port mmap to outgoing array");
279 
280         nxt_free(mmap_handler);
281         return NULL;
282     }
283 
284     p = nxt_sprintf(name, name + sizeof(name), NXT_SHM_PREFIX "unit.%PI.%uxD",
285                     nxt_pid, nxt_random(&task->thread->random));
286     *p = '\0';
287 
288 #if (NXT_HAVE_MEMFD_CREATE)
289 
290     fd = syscall(SYS_memfd_create, name, MFD_CLOEXEC);
291 
292     if (nxt_slow_path(fd == -1)) {
293         nxt_alert(task, "memfd_create(%s) failed %E", name, nxt_errno);
294 
295         goto remove_fail;
296     }
297 
298     nxt_debug(task, "memfd_create(%s): %FD", name, fd);
299 
300 #elif (NXT_HAVE_SHM_OPEN)
301 
302     /* Just in case. */
303     shm_unlink((char *) name);
304 
305     fd = shm_open((char *) name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
306 
307     nxt_debug(task, "shm_open(%s): %FD", name, fd);
308 
309     if (nxt_slow_path(fd == -1)) {
310         nxt_alert(task, "shm_open(%s) failed %E", name, nxt_errno);
311 
312         goto remove_fail;
313     }
314 
315     if (nxt_slow_path(shm_unlink((char *) name) == -1)) {
316         nxt_log(task, NXT_LOG_WARN, "shm_unlink(%s) failed %E", name,
317                 nxt_errno);
318     }
319 
320 #else
321 
322 #error No working shared memory implementation.
323 
324 #endif
325 
326     if (nxt_slow_path(ftruncate(fd, PORT_MMAP_SIZE) == -1)) {
327         nxt_log(task, NXT_LOG_WARN, "ftruncate() failed %E", nxt_errno);
328 
329         goto remove_fail;
330     }
331 
332     mem = nxt_mem_mmap(NULL, PORT_MMAP_SIZE, PROT_READ | PROT_WRITE,
333                        MAP_SHARED, fd, 0);
334 
335     if (nxt_slow_path(mem == MAP_FAILED)) {
336         goto remove_fail;
337     }
338 
339     mmap_handler->hdr = mem;
340     port_mmap->mmap_handler = mmap_handler;
341     nxt_port_mmap_handler_use(mmap_handler, 1);
342 
343     /* Init segment header. */
344     hdr = mmap_handler->hdr;
345 
346     nxt_memset(hdr->free_map, 0xFFU, sizeof(hdr->free_map));
347     nxt_memset(hdr->free_tracking_map, 0xFFU, sizeof(hdr->free_tracking_map));
348 
349     hdr->id = process->outgoing.size - 1;
350     hdr->src_pid = nxt_pid;
351     hdr->dst_pid = process->pid;
352     hdr->sent_over = port->id;
353 
354     /* Mark first chunk as busy */
355     free_map = tracking ? hdr->free_tracking_map : hdr->free_map;
356 
357     nxt_port_mmap_set_chunk_busy(free_map, 0);
358 
359     /* Mark as busy chunk followed the last available chunk. */
360     nxt_port_mmap_set_chunk_busy(hdr->free_map, PORT_MMAP_CHUNK_COUNT);
361     nxt_port_mmap_set_chunk_busy(hdr->free_tracking_map, PORT_MMAP_CHUNK_COUNT);
362 
363     nxt_debug(task, "send mmap fd %FD to process %PI", fd, port->pid);
364 
365     /* TODO handle error */
366     (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_MMAP, fd, 0, 0, NULL);
367 
368     nxt_log(task, NXT_LOG_DEBUG, "new mmap #%D created for %PI -> %PI",
369             hdr->id, nxt_pid, process->pid);
370 
371     return mmap_handler;
372 
373 remove_fail:
374 
375     nxt_free(mmap_handler);
376 
377     process->outgoing.size--;
378 
379     return NULL;
380 }
381 
382 
383 static nxt_port_mmap_handler_t *
384 nxt_port_mmap_get(nxt_task_t *task, nxt_port_t *port, nxt_chunk_id_t *c,
385     nxt_bool_t tracking)
386 {
387     nxt_process_t            *process;
388     nxt_free_map_t           *free_map;
389     nxt_port_mmap_t          *port_mmap;
390     nxt_port_mmap_t          *end_port_mmap;
391     nxt_port_mmap_header_t   *hdr;
392     nxt_port_mmap_handler_t  *mmap_handler;
393 
394     process = port->process;
395     if (nxt_slow_path(process == NULL)) {
396         return NULL;
397     }
398 
399     *c = 0;
400 
401     nxt_thread_mutex_lock(&process->outgoing.mutex);
402 
403     end_port_mmap = process->outgoing.elts + process->outgoing.size;
404 
405     for (port_mmap = process->outgoing.elts;
406          port_mmap < end_port_mmap;
407          port_mmap++)
408     {
409         mmap_handler = port_mmap->mmap_handler;
410         hdr = mmap_handler->hdr;
411 
412         if (hdr->sent_over != 0xFFFFu && hdr->sent_over != port->id) {
413             continue;
414         }
415 
416         free_map = tracking ? hdr->free_tracking_map : hdr->free_map;
417 
418         if (nxt_port_mmap_get_free_chunk(free_map, c)) {
419             goto unlock_return;
420         }
421     }
422 
423     /* TODO introduce port_mmap limit and release wait. */
424 
425     mmap_handler = nxt_port_new_port_mmap(task, process, port, tracking);
426 
427 unlock_return:
428 
429     nxt_thread_mutex_unlock(&process->outgoing.mutex);
430 
431     return mmap_handler;
432 }
433 
434 
435 static nxt_port_mmap_handler_t *
436 nxt_port_get_port_incoming_mmap(nxt_task_t *task, nxt_pid_t spid, uint32_t id)
437 {
438     nxt_process_t            *process;
439     nxt_port_mmap_handler_t  *mmap_handler;
440 
441     process = nxt_runtime_process_find(task->thread->runtime, spid);
442     if (nxt_slow_path(process == NULL)) {
443         return NULL;
444     }
445 
446     nxt_thread_mutex_lock(&process->incoming.mutex);
447 
448     if (nxt_fast_path(process->incoming.size > id)) {
449         mmap_handler = process->incoming.elts[id].mmap_handler;
450 
451     } else {
452         mmap_handler = NULL;
453 
454         nxt_debug(task, "invalid incoming mmap id %uD for pid %PI", id, spid);
455     }
456 
457     nxt_thread_mutex_unlock(&process->incoming.mutex);
458 
459     return mmap_handler;
460 }
461 
462 
463 nxt_int_t
464 nxt_port_mmap_get_tracking(nxt_task_t *task, nxt_port_t *port,
465     nxt_port_mmap_tracking_t *tracking, uint32_t stream)
466 {
467     nxt_chunk_id_t           c;
468     nxt_port_mmap_header_t   *hdr;
469     nxt_port_mmap_handler_t  *mmap_handler;
470 
471     nxt_debug(task, "request tracking for stream #%uD", stream);
472 
473     mmap_handler = nxt_port_mmap_get(task, port, &c, 1);
474     if (nxt_slow_path(mmap_handler == NULL)) {
475         return NXT_ERROR;
476     }
477 
478     nxt_port_mmap_handler_use(mmap_handler, 1);
479 
480     hdr = mmap_handler->hdr;
481 
482     tracking->mmap_handler = mmap_handler;
483     tracking->tracking = hdr->tracking + c;
484 
485     *tracking->tracking = stream;
486 
487     nxt_debug(task, "outgoing tracking allocation: %PI->%PI,%d,%d",
488               hdr->src_pid, hdr->dst_pid, hdr->id, c);
489 
490     return NXT_OK;
491 }
492 
493 
494 nxt_bool_t
495 nxt_port_mmap_tracking_cancel(nxt_task_t *task,
496     nxt_port_mmap_tracking_t *tracking, uint32_t stream)
497 {
498     nxt_bool_t               res;
499     nxt_chunk_id_t           c;
500     nxt_port_mmap_header_t   *hdr;
501     nxt_port_mmap_handler_t  *mmap_handler;
502 
503     mmap_handler = tracking->mmap_handler;
504 
505     if (nxt_slow_path(mmap_handler == NULL)) {
506         return 0;
507     }
508 
509     hdr = mmap_handler->hdr;
510 
511     res = nxt_atomic_cmp_set(tracking->tracking, stream, 0);
512 
513     nxt_debug(task, "%s tracking for stream #%uD",
514               (res ? "cancelled" : "failed to cancel"), stream);
515 
516     if (!res) {
517         c = tracking->tracking - hdr->tracking;
518         nxt_port_mmap_set_chunk_free(hdr->free_tracking_map, c);
519     }
520 
521     nxt_port_mmap_handler_use(mmap_handler, -1);
522 
523     return res;
524 }
525 
526 
527 nxt_int_t
528 nxt_port_mmap_tracking_write(uint32_t *buf, nxt_port_mmap_tracking_t *t)
529 {
530     nxt_port_mmap_handler_t  *mmap_handler;
531 
532     mmap_handler = t->mmap_handler;
533 
534 #if (NXT_DEBUG)
535     {
536     nxt_atomic_t  *tracking;
537 
538     tracking = mmap_handler->hdr->tracking;
539 
540     nxt_assert(t->tracking >= tracking);
541     nxt_assert(t->tracking < tracking + PORT_MMAP_CHUNK_COUNT);
542     }
543 #endif
544 
545     buf[0] = mmap_handler->hdr->id;
546     buf[1] = t->tracking - mmap_handler->hdr->tracking;
547 
548     return NXT_OK;
549 }
550 
551 nxt_bool_t
552 nxt_port_mmap_tracking_read(nxt_task_t *task, nxt_port_recv_msg_t *msg)
553 {
554     nxt_buf_t                     *b;
555     nxt_bool_t                    res;
556     nxt_chunk_id_t                c;
557     nxt_port_mmap_header_t        *hdr;
558     nxt_port_mmap_handler_t       *mmap_handler;
559     nxt_port_mmap_tracking_msg_t  *tracking_msg;
560 
561     b = msg->buf;
562 
563     if (nxt_buf_used_size(b) < (int) sizeof(nxt_port_mmap_tracking_msg_t)) {
564         nxt_debug(task, "too small message %O", nxt_buf_used_size(b));
565         return 0;
566     }
567 
568     tracking_msg = (nxt_port_mmap_tracking_msg_t *) b->mem.pos;
569 
570     b->mem.pos += sizeof(nxt_port_mmap_tracking_msg_t);
571     mmap_handler = nxt_port_get_port_incoming_mmap(task, msg->port_msg.pid,
572                                                    tracking_msg->mmap_id);
573 
574     if (nxt_slow_path(mmap_handler == NULL)) {
575         return 0;
576     }
577 
578     hdr = mmap_handler->hdr;
579 
580     c = tracking_msg->tracking_id;
581     res = nxt_atomic_cmp_set(hdr->tracking + c, msg->port_msg.stream, 0);
582 
583     nxt_debug(task, "tracking for stream #%uD %s", msg->port_msg.stream,
584               (res ? "received" : "already cancelled"));
585 
586     if (!res) {
587         nxt_port_mmap_set_chunk_free(hdr->free_tracking_map, c);
588     }
589 
590     return res;
591 }
592 
593 
594 nxt_buf_t *
595 nxt_port_mmap_get_buf(nxt_task_t *task, nxt_port_t *port, size_t size)
596 {
597     size_t                   nchunks;
598     nxt_mp_t                 *mp;
599     nxt_buf_t                *b;
600     nxt_chunk_id_t           c;
601     nxt_port_mmap_header_t   *hdr;
602     nxt_port_mmap_handler_t  *mmap_handler;
603 
604     nxt_debug(task, "request %z bytes shm buffer", size);
605 
606     b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, 0);
607     if (nxt_slow_path(b == NULL)) {
608         return NULL;
609     }
610 
611     b->completion_handler = nxt_port_mmap_buf_completion;
612     nxt_buf_set_port_mmap(b);
613 
614     mmap_handler = nxt_port_mmap_get(task, port, &c, 0);
615     if (nxt_slow_path(mmap_handler == NULL)) {
616         mp = task->thread->engine->mem_pool;
617         nxt_mp_free(mp, b);
618         nxt_mp_release(mp);
619         return NULL;
620     }
621 
622     b->parent = mmap_handler;
623 
624     nxt_port_mmap_handler_use(mmap_handler, 1);
625 
626     hdr = mmap_handler->hdr;
627 
628     b->mem.start = nxt_port_mmap_chunk_start(hdr, c);
629     b->mem.pos = b->mem.start;
630     b->mem.free = b->mem.start;
631     b->mem.end = b->mem.start + PORT_MMAP_CHUNK_SIZE;
632 
633     nchunks = size / PORT_MMAP_CHUNK_SIZE;
634     if ((size % PORT_MMAP_CHUNK_SIZE) != 0 || nchunks == 0) {
635         nchunks++;
636     }
637 
638     nxt_debug(task, "outgoing mmap buf allocation: %p [%p,%uz] %PI->%PI,%d,%d",
639               b, b->mem.start, b->mem.end - b->mem.start,
640               hdr->src_pid, hdr->dst_pid, hdr->id, c);
641 
642     c++;
643     nchunks--;
644 
645     /* Try to acquire as much chunks as required. */
646     while (nchunks > 0) {
647 
648         if (nxt_port_mmap_chk_set_chunk_busy(hdr->free_map, c) == 0) {
649             break;
650         }
651 
652         b->mem.end += PORT_MMAP_CHUNK_SIZE;
653         c++;
654         nchunks--;
655     }
656 
657     return b;
658 }
659 
660 
661 nxt_int_t
662 nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, size_t size,
663     size_t min_size)
664 {
665     size_t                   nchunks, free_size;
666     nxt_chunk_id_t           c, start;
667     nxt_port_mmap_header_t   *hdr;
668     nxt_port_mmap_handler_t  *mmap_handler;
669 
670     nxt_debug(task, "request increase %z bytes shm buffer", size);
671 
672     if (nxt_slow_path(nxt_buf_is_port_mmap(b) == 0)) {
673         nxt_log(task, NXT_LOG_WARN,
674                 "failed to increase, not a mmap buffer");
675         return NXT_ERROR;
676     }
677 
678     free_size = nxt_buf_mem_free_size(&b->mem);
679 
680     if (nxt_slow_path(size <= free_size)) {
681         return NXT_OK;
682     }
683 
684     mmap_handler = b->parent;
685     hdr = mmap_handler->hdr;
686 
687     start = nxt_port_mmap_chunk_id(hdr, b->mem.end);
688 
689     size -= free_size;
690 
691     nchunks = size / PORT_MMAP_CHUNK_SIZE;
692     if ((size % PORT_MMAP_CHUNK_SIZE) != 0 || nchunks == 0) {
693         nchunks++;
694     }
695 
696     c = start;
697 
698     /* Try to acquire as much chunks as required. */
699     while (nchunks > 0) {
700 
701         if (nxt_port_mmap_chk_set_chunk_busy(hdr->free_map, c) == 0) {
702             break;
703         }
704 
705         c++;
706         nchunks--;
707     }
708 
709     if (nchunks != 0
710         && min_size > free_size + PORT_MMAP_CHUNK_SIZE * (c - start))
711     {
712         c--;
713         while (c >= start) {
714             nxt_port_mmap_set_chunk_free(hdr->free_map, c);
715             c--;
716         }
717 
718         nxt_debug(task, "failed to increase, %uz chunks busy", nchunks);
719 
720         return NXT_ERROR;
721 
722     } else {
723         b->mem.end += PORT_MMAP_CHUNK_SIZE * (c - start);
724 
725         return NXT_OK;
726     }
727 }
728 
729 
730 static nxt_buf_t *
731 nxt_port_mmap_get_incoming_buf(nxt_task_t *task, nxt_port_t *port,
732     nxt_pid_t spid, nxt_port_mmap_msg_t *mmap_msg)
733 {
734     size_t                   nchunks;
735     nxt_buf_t                *b;
736     nxt_port_mmap_header_t   *hdr;
737     nxt_port_mmap_handler_t  *mmap_handler;
738 
739     mmap_handler = nxt_port_get_port_incoming_mmap(task, spid,
740                                                    mmap_msg->mmap_id);
741     if (nxt_slow_path(mmap_handler == NULL)) {
742         return NULL;
743     }
744 
745     b = nxt_buf_mem_ts_alloc(task, port->mem_pool, 0);
746     if (nxt_slow_path(b == NULL)) {
747         return NULL;
748     }
749 
750     b->completion_handler = nxt_port_mmap_buf_completion;
751 
752     nxt_buf_set_port_mmap(b);
753 
754     nchunks = mmap_msg->size / PORT_MMAP_CHUNK_SIZE;
755     if ((mmap_msg->size % PORT_MMAP_CHUNK_SIZE) != 0) {
756         nchunks++;
757     }
758 
759     hdr = mmap_handler->hdr;
760 
761     b->mem.start = nxt_port_mmap_chunk_start(hdr, mmap_msg->chunk_id);
762     b->mem.pos = b->mem.start;
763     b->mem.free = b->mem.start + mmap_msg->size;
764     b->mem.end = b->mem.start + nchunks * PORT_MMAP_CHUNK_SIZE;
765 
766     b->parent = mmap_handler;
767     nxt_port_mmap_handler_use(mmap_handler, 1);
768 
769     nxt_debug(task, "incoming mmap buf allocation: %p [%p,%uz] %PI->%PI,%d,%d",
770               b, b->mem.start, b->mem.end - b->mem.start,
771               hdr->src_pid, hdr->dst_pid, hdr->id, mmap_msg->chunk_id);
772 
773     return b;
774 }
775 
776 
777 void
778 nxt_port_mmap_write(nxt_task_t *task, nxt_port_t *port,
779     nxt_port_send_msg_t *msg, nxt_sendbuf_coalesce_t *sb)
780 {
781     size_t                   bsize;
782     nxt_buf_t                *bmem;
783     nxt_uint_t               i;
784     nxt_port_mmap_msg_t      *mmap_msg;
785     nxt_port_mmap_header_t   *hdr;
786     nxt_port_mmap_handler_t  *mmap_handler;
787 
788     nxt_debug(task, "prepare %z bytes message for transfer to process %PI "
789                     "via shared memory", sb->size, port->pid);
790 
791     bsize = sb->niov * sizeof(nxt_port_mmap_msg_t);
792     mmap_msg = port->mmsg_buf;
793 
794     bmem = msg->buf;
795 
796     for (i = 0; i < sb->niov; i++, mmap_msg++) {
797 
798         /* Lookup buffer which starts current iov_base. */
799         while (bmem && sb->iobuf[i].iov_base != bmem->mem.pos) {
800             bmem = bmem->next;
801         }
802 
803         if (nxt_slow_path(bmem == NULL)) {
804             nxt_log_error(NXT_LOG_ERR, task->log,
805                           "failed to find buf for iobuf[%d]", i);
806             return;
807             /* TODO clear b and exit */
808         }
809 
810         mmap_handler = bmem->parent;
811         hdr = mmap_handler->hdr;
812 
813         mmap_msg->mmap_id = hdr->id;
814         mmap_msg->chunk_id = nxt_port_mmap_chunk_id(hdr, bmem->mem.pos);
815         mmap_msg->size = sb->iobuf[i].iov_len;
816 
817         nxt_debug(task, "mmap_msg={%D, %D, %D} to %PI",
818                   mmap_msg->mmap_id, mmap_msg->chunk_id, mmap_msg->size,
819                   port->pid);
820     }
821 
822     sb->iobuf[0].iov_base = port->mmsg_buf;
823     sb->iobuf[0].iov_len = bsize;
824     sb->niov = 1;
825     sb->size = bsize;
826 
827     msg->port_msg.mmap = 1;
828 }
829 
830 
831 void
832 nxt_port_mmap_read(nxt_task_t *task, nxt_port_recv_msg_t *msg)
833 {
834     nxt_buf_t            *b, **pb;
835     nxt_port_mmap_msg_t  *end, *mmap_msg;
836 
837     pb = &msg->buf;
838     msg->size = 0;
839 
840     for (b = msg->buf; b != NULL; b = b->next) {
841 
842         mmap_msg = (nxt_port_mmap_msg_t *) b->mem.pos;
843         end = (nxt_port_mmap_msg_t *) b->mem.free;
844 
845         while (mmap_msg < end) {
846             nxt_debug(task, "mmap_msg={%D, %D, %D} from %PI",
847                       mmap_msg->mmap_id, mmap_msg->chunk_id, mmap_msg->size,
848                       msg->port_msg.pid);
849 
850             *pb = nxt_port_mmap_get_incoming_buf(task, msg->port,
851                                                  msg->port_msg.pid, mmap_msg);
852             if (nxt_slow_path(*pb == NULL)) {
853                 nxt_log_error(NXT_LOG_ERR, task->log,
854                               "failed to get mmap buffer");
855 
856                 break;
857             }
858 
859             msg->size += mmap_msg->size;
860             pb = &(*pb)->next;
861             mmap_msg++;
862 
863             /* Mark original buf as complete. */
864             b->mem.pos += sizeof(nxt_port_mmap_msg_t);
865         }
866     }
867 }
868 
869 
870 nxt_port_method_t
871 nxt_port_mmap_get_method(nxt_task_t *task, nxt_port_t *port, nxt_buf_t *b)
872 {
873     nxt_port_method_t        m;
874     nxt_port_mmap_header_t   *hdr;
875     nxt_port_mmap_handler_t  *mmap_handler;
876 
877     m = NXT_PORT_METHOD_ANY;
878 
879     for (; b != NULL; b = b->next) {
880         if (nxt_buf_used_size(b) == 0) {
881             /* empty buffers does not affect method */
882             continue;
883         }
884 
885         if (nxt_buf_is_port_mmap(b)) {
886             mmap_handler = b->parent;
887             hdr = mmap_handler->hdr;
888 
889             if (m == NXT_PORT_METHOD_PLAIN) {
890                 nxt_log_error(NXT_LOG_ERR, task->log,
891                               "mixing plain and mmap buffers, "
892                               "using plain mode");
893 
894                 break;
895             }
896 
897             if (port->pid != hdr->dst_pid) {
898                 nxt_log_error(NXT_LOG_ERR, task->log,
899                               "send mmap buffer for %PI to %PI, "
900                               "using plain mode", hdr->dst_pid, port->pid);
901 
902                 m = NXT_PORT_METHOD_PLAIN;
903 
904                 break;
905             }
906 
907             if (m == NXT_PORT_METHOD_ANY) {
908                 nxt_debug(task, "using mmap mode");
909 
910                 m = NXT_PORT_METHOD_MMAP;
911             }
912         } else {
913             if (m == NXT_PORT_METHOD_MMAP) {
914                 nxt_log_error(NXT_LOG_ERR, task->log,
915                               "mixing mmap and plain buffers, "
916                               "switching to plain mode");
917 
918                 m = NXT_PORT_METHOD_PLAIN;
919 
920                 break;
921             }
922 
923             if (m == NXT_PORT_METHOD_ANY) {
924                 nxt_debug(task, "using plain mode");
925 
926                 m = NXT_PORT_METHOD_PLAIN;
927             }
928         }
929     }
930 
931     return m;
932 }
933