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