xref: /unit/src/nxt_port_memory.c (revision 538:2d66e6b0e50f)
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 #if (NXT_DEBUG)
128     if (nxt_slow_path(data != b->parent)) {
129         nxt_log_alert(task->log, "completion data (%p) != b->parent (%p)",
130                       data, b->parent);
131         nxt_abort();
132     }
133 #endif
134 
135     mmap_handler = data;
136     hdr = mmap_handler->hdr;
137 
138     if (nxt_slow_path(hdr->src_pid != nxt_pid && hdr->dst_pid != nxt_pid)) {
139         nxt_debug(task, "mmap buf completion: mmap for other process pair "
140                   "%PI->%PI", hdr->src_pid, hdr->dst_pid);
141 
142         goto release_buf;
143     }
144 
145     if (b->is_port_mmap_sent && b->mem.pos > b->mem.start) {
146         /*
147          * Chunks until b->mem.pos has been sent to other side,
148          * let's release rest (if any).
149          */
150         p = b->mem.pos - 1;
151         c = nxt_port_mmap_chunk_id(hdr, p) + 1;
152         p = nxt_port_mmap_chunk_start(hdr, c);
153 
154     } else {
155         p = b->mem.start;
156         c = nxt_port_mmap_chunk_id(hdr, p);
157     }
158 
159     nxt_port_mmap_free_junk(p, b->mem.end - p);
160 
161     nxt_debug(task, "mmap buf completion: %p [%p,%uz] (sent=%d), "
162               "%PI->%PI,%d,%d", b, b->mem.start, b->mem.end - b->mem.start,
163               b->is_port_mmap_sent, hdr->src_pid, hdr->dst_pid, hdr->id, c);
164 
165     while (p < b->mem.end) {
166         nxt_port_mmap_set_chunk_free(hdr->free_map, c);
167 
168         p += PORT_MMAP_CHUNK_SIZE;
169         c++;
170     }
171 
172 release_buf:
173 
174     nxt_port_mmap_handler_use(mmap_handler, -1);
175 
176     nxt_mp_free(mp, b);
177     nxt_mp_release(mp);
178 }
179 
180 
181 nxt_port_mmap_handler_t *
182 nxt_port_incoming_port_mmap(nxt_task_t *task, nxt_process_t *process,
183     nxt_fd_t fd)
184 {
185     void                     *mem;
186     struct stat              mmap_stat;
187     nxt_port_mmap_t          *port_mmap;
188     nxt_port_mmap_header_t   *hdr;
189     nxt_port_mmap_handler_t  *mmap_handler;
190 
191     nxt_debug(task, "got new mmap fd #%FD from process %PI",
192               fd, process->pid);
193 
194     port_mmap = NULL;
195 
196     if (fstat(fd, &mmap_stat) == -1) {
197         nxt_log(task, NXT_LOG_WARN, "fstat(%FD) failed %E", fd, nxt_errno);
198 
199         return NULL;
200     }
201 
202     mem = nxt_mem_mmap(NULL, mmap_stat.st_size,
203                        PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
204 
205     if (nxt_slow_path(mem == MAP_FAILED)) {
206         nxt_log(task, NXT_LOG_WARN, "mmap() failed %E", nxt_errno);
207 
208         return NULL;
209     }
210 
211     hdr = mem;
212 
213     mmap_handler = nxt_zalloc(sizeof(nxt_port_mmap_handler_t));
214     if (nxt_slow_path(mmap_handler == NULL)) {
215         nxt_log(task, NXT_LOG_WARN, "failed to allocate mmap_handler");
216 
217         nxt_mem_munmap(mem, PORT_MMAP_SIZE);
218 
219         return NULL;
220     }
221 
222     mmap_handler->hdr = hdr;
223 
224     nxt_thread_mutex_lock(&process->incoming.mutex);
225 
226     port_mmap = nxt_port_mmap_at(&process->incoming, hdr->id);
227     if (nxt_slow_path(port_mmap == NULL)) {
228         nxt_log(task, NXT_LOG_WARN, "failed to add mmap to incoming array");
229 
230         nxt_mem_munmap(mem, PORT_MMAP_SIZE);
231         hdr = NULL;
232 
233         nxt_free(mmap_handler);
234         mmap_handler = NULL;
235 
236         goto fail;
237     }
238 
239     nxt_assert(hdr->src_pid == process->pid);
240     nxt_assert(hdr->dst_pid == nxt_pid);
241 
242     port_mmap->mmap_handler = mmap_handler;
243     nxt_port_mmap_handler_use(mmap_handler, 1);
244 
245     hdr->sent_over = 0xFFFFu;
246 
247 fail:
248 
249     nxt_thread_mutex_unlock(&process->incoming.mutex);
250 
251     return mmap_handler;
252 }
253 
254 
255 static nxt_port_mmap_handler_t *
256 nxt_port_new_port_mmap(nxt_task_t *task, nxt_process_t *process,
257     nxt_port_t *port, nxt_bool_t tracking)
258 {
259     void                     *mem;
260     u_char                   *p, name[64];
261     nxt_fd_t                 fd;
262     nxt_free_map_t           *free_map;
263     nxt_port_mmap_t          *port_mmap;
264     nxt_port_mmap_header_t   *hdr;
265     nxt_port_mmap_handler_t  *mmap_handler;
266 
267     mmap_handler = nxt_zalloc(sizeof(nxt_port_mmap_handler_t));
268     if (nxt_slow_path(mmap_handler == NULL)) {
269         nxt_log(task, NXT_LOG_WARN, "failed to allocate mmap_handler");
270 
271         return NULL;
272     }
273 
274     port_mmap = nxt_port_mmap_at(&process->outgoing, process->outgoing.size);
275     if (nxt_slow_path(port_mmap == NULL)) {
276         nxt_log(task, NXT_LOG_WARN,
277                 "failed to add port mmap to outgoing array");
278 
279         nxt_free(mmap_handler);
280         return NULL;
281     }
282 
283     p = nxt_sprintf(name, name + sizeof(name), NXT_SHM_PREFIX "unit.%PI.%uxD",
284                     nxt_pid, nxt_random(&task->thread->random));
285     *p = '\0';
286 
287 #if (NXT_HAVE_MEMFD_CREATE)
288 
289     fd = syscall(SYS_memfd_create, name, MFD_CLOEXEC);
290 
291     if (nxt_slow_path(fd == -1)) {
292         nxt_log(task, NXT_LOG_CRIT, "memfd_create(%s) failed %E",
293                 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_log(task, NXT_LOG_CRIT, "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_assert(mmap_msg + 1 <= end);
847 
848             nxt_debug(task, "mmap_msg={%D, %D, %D} from %PI",
849                       mmap_msg->mmap_id, mmap_msg->chunk_id, mmap_msg->size,
850                       msg->port_msg.pid);
851 
852             *pb = nxt_port_mmap_get_incoming_buf(task, msg->port,
853                                                  msg->port_msg.pid, mmap_msg);
854             if (nxt_slow_path(*pb == NULL)) {
855                 nxt_log_error(NXT_LOG_ERR, task->log,
856                               "failed to get mmap buffer");
857 
858                 break;
859             }
860 
861             msg->size += mmap_msg->size;
862             pb = &(*pb)->next;
863             mmap_msg++;
864 
865             /* Mark original buf as complete. */
866             b->mem.pos += sizeof(nxt_port_mmap_msg_t);
867         }
868     }
869 }
870 
871 
872 nxt_port_method_t
873 nxt_port_mmap_get_method(nxt_task_t *task, nxt_port_t *port, nxt_buf_t *b)
874 {
875     nxt_port_method_t        m;
876     nxt_port_mmap_header_t   *hdr;
877     nxt_port_mmap_handler_t  *mmap_handler;
878 
879     m = NXT_PORT_METHOD_ANY;
880 
881     for (; b != NULL; b = b->next) {
882         if (nxt_buf_used_size(b) == 0) {
883             /* empty buffers does not affect method */
884             continue;
885         }
886 
887         if (nxt_buf_is_port_mmap(b)) {
888             mmap_handler = b->parent;
889             hdr = mmap_handler->hdr;
890 
891             if (m == NXT_PORT_METHOD_PLAIN) {
892                 nxt_log_error(NXT_LOG_ERR, task->log,
893                               "mixing plain and mmap buffers, "
894                               "using plain mode");
895 
896                 break;
897             }
898 
899             if (port->pid != hdr->dst_pid) {
900                 nxt_log_error(NXT_LOG_ERR, task->log,
901                               "send mmap buffer for %PI to %PI, "
902                               "using plain mode", hdr->dst_pid, port->pid);
903 
904                 m = NXT_PORT_METHOD_PLAIN;
905 
906                 break;
907             }
908 
909             if (m == NXT_PORT_METHOD_ANY) {
910                 nxt_debug(task, "using mmap mode");
911 
912                 m = NXT_PORT_METHOD_MMAP;
913             }
914         } else {
915             if (m == NXT_PORT_METHOD_MMAP) {
916                 nxt_log_error(NXT_LOG_ERR, task->log,
917                               "mixing mmap and plain buffers, "
918                               "switching to plain mode");
919 
920                 m = NXT_PORT_METHOD_PLAIN;
921 
922                 break;
923             }
924 
925             if (m == NXT_PORT_METHOD_ANY) {
926                 nxt_debug(task, "using plain mode");
927 
928                 m = NXT_PORT_METHOD_PLAIN;
929             }
930         }
931     }
932 
933     return m;
934 }
935