summarylogtreecommitdiffstats
path: root/0001-Apply-WIP-CL-from-upstream-wayland.patch
blob: a5e4ab60be44bc153e0166dc072a8b2e5a418bb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
From 66a24337ec401573418985515275f753c72272d6 Mon Sep 17 00:00:00 2001
From: Erik Chen <erikchen@chromium.org>
Date: Thu, 20 Oct 2022 00:06:01 +0000
Subject: Apply WIP CL from upstream wayland

This CL provides the capability of dynamically modifying the buffer size.
https://gitlab.freedesktop.org/wayland/wayland/-/commit/0bfcd4c4bef0ddb555c0e9f07cd3ceff81a33313

Note that I am not the author. This CL has been reviewed upstream.

Bug: 1342356
Change-Id: I24f90fb31248a52aa97f1813a5ff9b30157aa350
---
 src/connection.c          | 315 ++++++++++++++++++++++++++++----------
 src/wayland-client.c      |   2 +-
 src/wayland-private.h     |   8 +-
 src/wayland-server-core.h |   7 +
 src/wayland-server.c      |  63 +++++++-
 tests/connection-test.c   |  46 +++++-
 tests/display-test.c      |   5 +-
 tests/os-wrappers-test.c  |   6 +-
 8 files changed, 356 insertions(+), 96 deletions(-)

diff --git a/src/connection.c b/src/connection.c
index bf97676..98aefd6 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -55,12 +55,12 @@ div_roundup(uint32_t n, size_t a)
 }
 
 struct wl_ring_buffer {
-	char data[4096];
-	uint32_t head, tail;
+	char *data;
+	size_t head, tail;
+	uint32_t size_bits;
+	uint32_t max_size_bits;  /* 0 for unlimited */
 };
 
-#define MASK(i) ((i) & 4095)
-
 #define MAX_FDS_OUT	28
 #define CLEN		(CMSG_LEN(MAX_FDS_OUT * sizeof(int32_t)))
 
@@ -71,23 +71,27 @@ struct wl_connection {
 	int want_flush;
 };
 
+static size_t
+ring_buffer_space(const struct wl_ring_buffer *b) {
+	return ((size_t)1) << b->size_bits;
+}
+
+static size_t
+ring_buffer_mask(const struct wl_ring_buffer *b, size_t i) {
+	size_t m = (((size_t)1) << b->size_bits) - 1;
+	return i & m;
+}
+
 static int
 ring_buffer_put(struct wl_ring_buffer *b, const void *data, size_t count)
 {
-	uint32_t head, size;
+	size_t head, size;
 
-	if (count > sizeof(b->data)) {
-		wl_log("Data too big for buffer (%d > %d).\n",
-		       count, sizeof(b->data));
-		errno = E2BIG;
-		return -1;
-	}
-
-	head = MASK(b->head);
-	if (head + count <= sizeof b->data) {
+	head = ring_buffer_mask(b, b->head);
+	if (head + count <= ring_buffer_space(b)) {
 		memcpy(b->data + head, data, count);
 	} else {
-		size = sizeof b->data - head;
+		size = ring_buffer_space(b) - head;
 		memcpy(b->data + head, data, size);
 		memcpy(b->data, (const char *) data + size, count - size);
 	}
@@ -100,21 +104,21 @@ ring_buffer_put(struct wl_ring_buffer *b, const void *data, size_t count)
 static void
 ring_buffer_put_iov(struct wl_ring_buffer *b, struct iovec *iov, int *count)
 {
-	uint32_t head, tail;
+	size_t head, tail;
 
-	head = MASK(b->head);
-	tail = MASK(b->tail);
+	head = ring_buffer_mask(b, b->head);
+	tail = ring_buffer_mask(b, b->tail);
 	if (head < tail) {
 		iov[0].iov_base = b->data + head;
 		iov[0].iov_len = tail - head;
 		*count = 1;
 	} else if (tail == 0) {
 		iov[0].iov_base = b->data + head;
-		iov[0].iov_len = sizeof b->data - head;
+		iov[0].iov_len = ring_buffer_space(b) - head;
 		*count = 1;
 	} else {
 		iov[0].iov_base = b->data + head;
-		iov[0].iov_len = sizeof b->data - head;
+		iov[0].iov_len = ring_buffer_space(b) - head;
 		iov[1].iov_base = b->data;
 		iov[1].iov_len = tail;
 		*count = 2;
@@ -124,21 +128,21 @@ ring_buffer_put_iov(struct wl_ring_buffer *b, struct iovec *iov, int *count)
 static void
 ring_buffer_get_iov(struct wl_ring_buffer *b, struct iovec *iov, int *count)
 {
-	uint32_t head, tail;
+	size_t head, tail;
 
-	head = MASK(b->head);
-	tail = MASK(b->tail);
+	head = ring_buffer_mask(b, b->head);
+	tail = ring_buffer_mask(b, b->tail);
 	if (tail < head) {
 		iov[0].iov_base = b->data + tail;
 		iov[0].iov_len = head - tail;
 		*count = 1;
 	} else if (head == 0) {
 		iov[0].iov_base = b->data + tail;
-		iov[0].iov_len = sizeof b->data - tail;
+		iov[0].iov_len = ring_buffer_space(b) - tail;
 		*count = 1;
 	} else {
 		iov[0].iov_base = b->data + tail;
-		iov[0].iov_len = sizeof b->data - tail;
+		iov[0].iov_len = ring_buffer_space(b) - tail;
 		iov[1].iov_base = b->data;
 		iov[1].iov_len = head;
 		*count = 2;
@@ -148,26 +152,136 @@ ring_buffer_get_iov(struct wl_ring_buffer *b, struct iovec *iov, int *count)
 static void
 ring_buffer_copy(struct wl_ring_buffer *b, void *data, size_t count)
 {
-	uint32_t tail, size;
+	size_t tail, size;
 
-	tail = MASK(b->tail);
-	if (tail + count <= sizeof b->data) {
+	tail = ring_buffer_mask(b, b->tail);
+	if (tail + count <= ring_buffer_space(b)) {
 		memcpy(data, b->data + tail, count);
 	} else {
-		size = sizeof b->data - tail;
+		size = ring_buffer_space(b) - tail;
 		memcpy(data, b->data + tail, size);
 		memcpy((char *) data + size, b->data, count - size);
 	}
 }
 
-static uint32_t
+static size_t
 ring_buffer_size(struct wl_ring_buffer *b)
 {
 	return b->head - b->tail;
 }
 
+static uint32_t
+get_max_size_bits_for_size(size_t buffer_size)
+{
+	uint32_t max_size_bits = WL_BUFFER_DEFAULT_SIZE_POT;
+
+	/* buffer_size == 0 means unbound buffer size */
+	if (buffer_size == 0)
+		return 0;
+
+	do {
+		if (buffer_size <= (((size_t)1) << max_size_bits))
+			break;
+	} while (max_size_bits++ < (8 * sizeof(size_t) - 1));
+
+	return max_size_bits;
+}
+
+static int
+ring_buffer_allocate(struct wl_ring_buffer *b, size_t size_bits)
+{
+	char *new_data;
+
+	new_data = calloc(((size_t)1) << size_bits, 1);
+	if (!new_data) {
+		errno = ENOMEM;
+		return -1;
+	}
+
+	ring_buffer_copy(b, new_data, ring_buffer_size(b));
+	free(b->data);
+	b->data = new_data;
+	b->size_bits = size_bits;
+	b->head = ring_buffer_size(b);
+	b->tail = 0;
+
+	return 0;
+}
+
+static size_t
+ring_buffer_get_bits_for_size(struct wl_ring_buffer *b, size_t count)
+{
+	size_t net_size = ring_buffer_size(b) + count;
+	size_t max_size_bits = get_max_size_bits_for_size(net_size);
+
+	if (max_size_bits < WL_BUFFER_DEFAULT_SIZE_POT)
+		max_size_bits = WL_BUFFER_DEFAULT_SIZE_POT;
+
+	if (b->max_size_bits > 0 && max_size_bits > b->max_size_bits)
+		max_size_bits = b->max_size_bits;
+
+	return max_size_bits;
+}
+
+static int
+ring_buffer_check_space(struct wl_ring_buffer *b, size_t count)
+{
+	if (ring_buffer_size(b) + count > ring_buffer_space(b)) {
+		wl_log("Data too big for buffer (%d + %zd > %zd).\n",
+		       ring_buffer_size(b), count, ring_buffer_space(b));
+		errno = E2BIG;
+		return -1;
+	}
+
+	return 0;
+}
+
+static int
+ring_buffer_ensure_space(struct wl_ring_buffer *b, size_t count)
+{
+	size_t size_bits = ring_buffer_get_bits_for_size(b, count);
+	size_t net_size = ring_buffer_size(b) + count;
+
+	if (b->data == NULL)
+		return ring_buffer_allocate(b, size_bits);
+
+	/* Do not shrink (reallocate) if net size won't fit */
+	if (net_size >= (((size_t)1) << size_bits) || b->size_bits == size_bits)
+		return ring_buffer_check_space(b, count);
+
+	if (ring_buffer_allocate(b, size_bits) < 0)
+		return -1;
+
+	return ring_buffer_check_space(b, count);
+}
+
+void
+wl_connection_set_max_buffer_size(struct wl_connection *connection,
+				  size_t max_buffer_size)
+{
+	uint32_t max_size_bits;
+
+	max_size_bits = get_max_size_bits_for_size(max_buffer_size);
+
+	connection->fds_in.size_bits = WL_BUFFER_DEFAULT_SIZE_POT;
+	connection->fds_in.max_size_bits = max_size_bits;
+	ring_buffer_ensure_space(&connection->fds_in, 0);
+
+	connection->fds_out.size_bits = WL_BUFFER_DEFAULT_SIZE_POT;
+	connection->fds_out.max_size_bits = max_size_bits;
+	ring_buffer_ensure_space(&connection->fds_out, 0);
+
+	connection->in.size_bits = WL_BUFFER_DEFAULT_SIZE_POT;
+	connection->in.max_size_bits = max_size_bits;
+	ring_buffer_ensure_space(&connection->in, 0);
+
+	connection->out.size_bits = WL_BUFFER_DEFAULT_SIZE_POT;
+	connection->out.max_size_bits = max_size_bits;
+	ring_buffer_ensure_space(&connection->out, 0);
+}
+
 struct wl_connection *
-wl_connection_create(int fd)
+wl_connection_create(int fd, size_t max_buffer_size)
 {
 	struct wl_connection *connection;
 
@@ -175,6 +289,8 @@ wl_connection_create(int fd)
 	if (connection == NULL)
 		return NULL;
 
+	wl_connection_set_max_buffer_size(connection, max_buffer_size);
+
 	connection->fd = fd;
 
 	return connection;
@@ -183,20 +299,28 @@ wl_connection_create(int fd)
 static void
 close_fds(struct wl_ring_buffer *buffer, int max)
 {
-	int32_t fds[sizeof(buffer->data) / sizeof(int32_t)], i, count;
-	size_t size;
+	size_t size, tail, space;
+	int32_t i, count, *p;
 
 	size = ring_buffer_size(buffer);
 	if (size == 0)
 		return;
 
-	ring_buffer_copy(buffer, fds, size);
-	count = size / sizeof fds[0];
+	count = size / sizeof(int32_t);
 	if (max > 0 && max < count)
 		count = max;
-	size = count * sizeof fds[0];
-	for (i = 0; i < count; i++)
-		close(fds[i]);
+
+	tail = ring_buffer_mask(buffer, buffer->tail);
+	space = ring_buffer_space(buffer);
+
+	p = (int32_t *) (buffer->data + tail);
+	for (i = 0; i < count; i++) {
+		if (p >= (int32_t *) (buffer->data + space))
+			p = (int32_t *) buffer->data;
+		close(*p++);
+	}
+
+	size = count * sizeof(int32_t);
 	buffer->tail += size;
 }
 
@@ -213,6 +337,10 @@ wl_connection_destroy(struct wl_connection *connection)
 
 	close_fds(&connection->fds_out, -1);
 	close_fds(&connection->fds_in, -1);
+	free(connection->fds_in.data);
+	free(connection->fds_out.data);
+	free(connection->in.data);
+	free(connection->out.data);
 	free(connection);
 
 	return fd;
@@ -256,7 +384,7 @@ static int
 decode_cmsg(struct wl_ring_buffer *buffer, struct msghdr *msg)
 {
 	struct cmsghdr *cmsg;
-	size_t size, max, i;
+	size_t size, i;
 	int overflow = 0;
 
 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
@@ -266,8 +394,8 @@ decode_cmsg(struct wl_ring_buffer *buffer, struct msghdr *msg)
 			continue;
 
 		size = cmsg->cmsg_len - CMSG_LEN(0);
-		max = sizeof(buffer->data) - ring_buffer_size(buffer);
-		if (size > max || overflow) {
+
+		if (ring_buffer_ensure_space(buffer, size) < 0 || overflow) {
 			overflow = 1;
 			size /= sizeof(int32_t);
 			for (i = 0; i < size; i++)
@@ -293,17 +421,30 @@ wl_connection_flush(struct wl_connection *connection)
 	char cmsg[CLEN];
 	int len = 0, count;
 	size_t clen;
-	uint32_t tail;
+	size_t tail;
 
 	if (!connection->want_flush)
 		return 0;
 
 	tail = connection->out.tail;
 	while (connection->out.head - connection->out.tail > 0) {
-		ring_buffer_get_iov(&connection->out, iov, &count);
-
 		build_cmsg(&connection->fds_out, cmsg, &clen);
 
+		if (clen >= CMSG_LEN(MAX_FDS_OUT * sizeof(int32_t))) {
+			/* Send only a single byte, to ensure all FDs are sent
+			 * before the bytes are cleared out. This can fail to
+			 * clear the FDs first if individual messages are allowed to
+			 * have 8*28 = 224 fds. */
+			iov[0].iov_base = connection->out.data +
+				ring_buffer_mask(&connection->out, connection->out.tail);
+			iov[0].iov_len = 1;
+			count = 1;
+		} else {
+			ring_buffer_get_iov(&connection->out, iov, &count);
+		}
+
+		msg.msg_name = NULL;
+		msg.msg_namelen = 0;
 		msg.msg_iov = iov;
 		msg.msg_iovlen = count;
 		msg.msg_control = (clen > 0) ? cmsg : NULL;
@@ -341,49 +482,49 @@ wl_connection_read(struct wl_connection *connection)
 	char cmsg[CLEN];
 	int len, count, ret;
 
-	if (ring_buffer_size(&connection->in) >= sizeof(connection->in.data)) {
-		errno = EOVERFLOW;
-		return -1;
+	while (1) {
+		int data_size = ring_buffer_size(&connection->in);
+		size_t size_bits = ring_buffer_get_bits_for_size(&connection->in, 1);
+
+		// stop once we've read the max buffer size
+		if (data_size >= (1 << size_bits))
+			return data_size;
+		if (ring_buffer_ensure_space(&connection->in, 1) < 0)
+			return -1;
+
+		ring_buffer_put_iov(&connection->in, iov, &count);
+
+		msg.msg_name = NULL;
+		msg.msg_namelen = 0;
+		msg.msg_iov = iov;
+		msg.msg_iovlen = count;
+		msg.msg_control = cmsg;
+		msg.msg_controllen = sizeof cmsg;
+		msg.msg_flags = 0;
+
+		do {
+			len = wl_os_recvmsg_cloexec(connection->fd, &msg, MSG_DONTWAIT);
+		} while (len < 0 && errno == EINTR);
+
+		if (len < 0 && errno != EAGAIN) {
+			return -1;
+		} else if (len <= 0) {
+			return (data_size > 0) ? data_size : len;
+		}
+
+		ret = decode_cmsg(&connection->fds_in, &msg);
+		if (ret)
+			return -1;
+
+		connection->in.head += len;
 	}
-
-	ring_buffer_put_iov(&connection->in, iov, &count);
-
-	msg.msg_name = NULL;
-	msg.msg_namelen = 0;
-	msg.msg_iov = iov;
-	msg.msg_iovlen = count;
-	msg.msg_control = cmsg;
-	msg.msg_controllen = sizeof cmsg;
-	msg.msg_flags = 0;
-
-	do {
-		len = wl_os_recvmsg_cloexec(connection->fd, &msg, MSG_DONTWAIT);
-	} while (len < 0 && errno == EINTR);
-
-	if (len <= 0)
-		return len;
-
-	ret = decode_cmsg(&connection->fds_in, &msg);
-	if (ret)
-		return -1;
-
-	connection->in.head += len;
-
-	return wl_connection_pending_input(connection);
 }
 
 int
 wl_connection_write(struct wl_connection *connection,
 		    const void *data, size_t count)
 {
-	if (connection->out.head - connection->out.tail +
-	    count > ARRAY_LENGTH(connection->out.data)) {
-		connection->want_flush = 1;
-		if (wl_connection_flush(connection) < 0)
-			return -1;
-	}
-
-	if (ring_buffer_put(&connection->out, data, count) < 0)
+	if (wl_connection_queue(connection, data, count) < 0)
 		return -1;
 
 	connection->want_flush = 1;
@@ -396,12 +537,15 @@ wl_connection_queue(struct wl_connection *connection,
 		    const void *data, size_t count)
 {
 	if (connection->out.head - connection->out.tail +
-	    count > ARRAY_LENGTH(connection->out.data)) {
+	    count > WL_BUFFER_DEFAULT_MAX_SIZE) {
 		connection->want_flush = 1;
-		if (wl_connection_flush(connection) < 0)
+		if (wl_connection_flush(connection) < 0 && errno != EAGAIN)
 			return -1;
 	}
 
+	if (ring_buffer_ensure_space(&connection->out, count) < 0)
+		return -1;
+
 	return ring_buffer_put(&connection->out, data, count);
 }
 
@@ -427,12 +571,15 @@ wl_connection_get_fd(struct wl_connection *connection)
 static int
 wl_connection_put_fd(struct wl_connection *connection, int32_t fd)
 {
-	if (ring_buffer_size(&connection->fds_out) == MAX_FDS_OUT * sizeof fd) {
+	if (ring_buffer_size(&connection->fds_out) >= MAX_FDS_OUT * sizeof fd) {
 		connection->want_flush = 1;
-		if (wl_connection_flush(connection) < 0)
+		if (wl_connection_flush(connection) < 0 && errno != EAGAIN)
 			return -1;
 	}
 
+	if (ring_buffer_ensure_space(&connection->fds_out, sizeof fd) < 0)
+		return -1;
+
 	return ring_buffer_put(&connection->fds_out, &fd, sizeof fd);
 }
 
diff --git a/src/wayland-client.c b/src/wayland-client.c
index 90fb9c7..6f88aba 100644
--- a/src/wayland-client.c
+++ b/src/wayland-client.c
@@ -1198,7 +1198,7 @@ wl_display_connect_to_fd(int fd)
 	 */
 	display->proxy.version = 0;
 
-	display->connection = wl_connection_create(display->fd);
+	display->connection = wl_connection_create(display->fd, 0);
 	if (display->connection == NULL)
 		goto err_connection;
 
diff --git a/src/wayland-private.h b/src/wayland-private.h
index 9274f1b..a3a5032 100644
--- a/src/wayland-private.h
+++ b/src/wayland-private.h
@@ -47,6 +47,8 @@
 #define WL_SERVER_ID_START 0xff000000
 #define WL_MAP_MAX_OBJECTS 0x00f00000
 #define WL_CLOSURE_MAX_ARGS 20
+#define WL_BUFFER_DEFAULT_SIZE_POT 12
+#define WL_BUFFER_DEFAULT_MAX_SIZE (1 << WL_BUFFER_DEFAULT_SIZE_POT)
 
 struct wl_object {
 	const struct wl_interface *interface;
@@ -106,7 +108,7 @@ void
 wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data);
 
 struct wl_connection *
-wl_connection_create(int fd);
+wl_connection_create(int fd, size_t max_buffer_size);
 
 int
 wl_connection_destroy(struct wl_connection *connection);
@@ -237,4 +239,8 @@ zalloc(size_t s)
 void
 wl_connection_close_fds_in(struct wl_connection *connection, int max);
 
+void
+wl_connection_set_max_buffer_size(struct wl_connection *connection,
+				  size_t max_buffer_size);
+
 #endif
diff --git a/src/wayland-server-core.h b/src/wayland-server-core.h
index 7a1375f..37b7706 100644
--- a/src/wayland-server-core.h
+++ b/src/wayland-server-core.h
@@ -217,6 +217,10 @@ wl_display_flush_clients(struct wl_display *display);
 void
 wl_display_destroy_clients(struct wl_display *display);
 
+void
+wl_display_set_default_max_buffer_size(struct wl_display *display,
+				       size_t max_buffer_size);
+
 struct wl_client;
 
 typedef void (*wl_global_bind_func_t)(struct wl_client *client, void *data,
@@ -353,6 +357,9 @@ wl_client_for_each_resource(struct wl_client *client,
                             wl_client_for_each_resource_iterator_func_t iterator,
                             void *user_data);
 
+void
+wl_client_set_max_buffer_size(struct wl_client *client, size_t max_buffer_size);
+
 /** \class wl_listener
  *
  * \brief A single listener for Wayland signals
diff --git a/src/wayland-server.c b/src/wayland-server.c
index 8e24b41..b925810 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -109,6 +109,8 @@ struct wl_display {
 
 	int terminate_efd;
 	struct wl_event_source *term_source;
+
+	size_t max_buffer_size;
 };
 
 struct wl_global {
@@ -537,7 +539,8 @@ wl_client_create(struct wl_display *display, int fd)
 				  &client->pid) != 0)
 		goto err_source;
 
-	client->connection = wl_connection_create(fd);
+	client->connection = wl_connection_create(fd, display->max_buffer_size);
+
 	if (client->connection == NULL)
 		goto err_source;
 
@@ -1102,6 +1105,7 @@ wl_display_create(void)
 
 	display->global_filter = NULL;
 	display->global_filter_data = NULL;
+	display->max_buffer_size = WL_BUFFER_DEFAULT_MAX_SIZE;
 
 	wl_array_init(&display->additional_shm_formats);
 
@@ -1488,6 +1492,36 @@ wl_display_destroy_clients(struct wl_display *display)
 	}
 }
 
+/** Sets the default maximum size for connection buffers of new clients
+ *
+ * \param display The display object
+ * \param max_buffer_size The default maximum size of the connection buffers
+ *
+ * This function sets the default size of the internal connection buffers for
+ * new clients, it doesn't change the buffer size for existing wl_client.
+ *
+ * The connection buffer size of each existing wl_client can be adjusted using
+ * wl_client_set_max_buffer_size().
+ *
+ * The actual size of the connection buffers is a power of two, the requested
+ * \a max_buffer_size is therefore rounded up to the nearest power of two value.
+ *
+ * The minimum buffer size is 4096.
+ *
+ * \sa wl_client_set_max_buffer_size
+ *
+ * \memberof wl_display
+ */
+WL_EXPORT void
+wl_display_set_default_max_buffer_size(struct wl_display *display,
+			               size_t max_buffer_size)
+{
+	if (max_buffer_size < WL_BUFFER_DEFAULT_MAX_SIZE)
+		max_buffer_size = WL_BUFFER_DEFAULT_MAX_SIZE;
+
+	display->max_buffer_size = max_buffer_size;
+}
+
 static int
 socket_data(int fd, uint32_t mask, void *data)
 {
@@ -2183,6 +2217,33 @@ wl_signal_emit_mutable(struct wl_signal *signal, void *data)
 	wl_list_remove(&end.link);
 }
 
+/** Adjust the maximum size of the client connection buffers
+ *
+ * \param client The client object
+ * \param max_buffer_size The maximum size of the connection buffers
+ *
+ * The actual size of the connection buffers is a power of two, the requested
+ * \a max_buffer_size is therefore rounded up to the nearest power of two value.
+ *
+ * Lowering the maximum size may not take effect immediately if the current content
+ * of the buffer does not fit within the new size limit.
+ *
+ * The minimum buffer size is 4096. The default buffers size can be set using
+ * wl_display_set_default_max_buffer_size()
+ *
+ * \sa wl_display_set_default_max_buffer_size()
+ *
+ * \memberof wl_client
+ */
+WL_EXPORT void
+wl_client_set_max_buffer_size(struct wl_client *client, size_t max_buffer_size)
+{
+	if (max_buffer_size < WL_BUFFER_DEFAULT_MAX_SIZE)
+		max_buffer_size = WL_BUFFER_DEFAULT_MAX_SIZE;
+
+	wl_connection_set_max_buffer_size(client->connection, max_buffer_size);
+}
+
 /** \cond INTERNAL */
 
 /** Initialize a wl_priv_signal object
diff --git a/tests/connection-test.c b/tests/connection-test.c
index eea9287..f5d943c 100644
--- a/tests/connection-test.c
+++ b/tests/connection-test.c
@@ -50,7 +50,7 @@ setup(int *s)
 
 	assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
 
-	connection = wl_connection_create(s[0]);
+	connection = wl_connection_create(s[0], WL_BUFFER_DEFAULT_MAX_SIZE);
 	assert(connection);
 
 	return connection;
@@ -183,9 +183,11 @@ setup_marshal_data(struct marshal_data *data)
 {
 	assert(socketpair(AF_UNIX,
 			  SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0);
-	data->read_connection = wl_connection_create(data->s[0]);
+	data->read_connection = wl_connection_create(data->s[0],
+						     WL_BUFFER_DEFAULT_MAX_SIZE);
 	assert(data->read_connection);
-	data->write_connection = wl_connection_create(data->s[1]);
+	data->write_connection = wl_connection_create(data->s[1],
+						      WL_BUFFER_DEFAULT_MAX_SIZE);
 	assert(data->write_connection);
 }
 
@@ -280,6 +282,25 @@ expected_fail_marshal(int expected_error, const char *format, ...)
 	assert(errno == expected_error);
 }
 
+static void
+marshal_send(struct marshal_data *data, const char *format, ...)
+{
+	struct wl_closure *closure;
+	static const uint32_t opcode = 4444;
+	static struct wl_object sender = { NULL, NULL, 1234 };
+	struct wl_message message = { "test", format, NULL };
+	va_list ap;
+
+	va_start(ap, format);
+	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
+	va_end(ap);
+
+	assert(closure);
+	assert(wl_closure_send(closure, data->write_connection) == 0);
+
+	wl_closure_destroy(closure);
+}
+
 static void
 expected_fail_marshal_send(struct marshal_data *data, int expected_error,
 			   const char *format, ...)
@@ -657,6 +678,25 @@ TEST(connection_marshal_too_big)
 	free(big_string);
 }
 
+TEST(connection_marshal_big_enough)
+{
+	struct marshal_data data;
+	char *big_string = malloc(5000);
+
+	assert(big_string);
+
+	memset(big_string, ' ', 4999);
+	big_string[4999] = '\0';
+
+	setup_marshal_data(&data);
+	wl_connection_set_max_buffer_size(data.write_connection, 5120);
+
+	marshal_send(&data, "s", big_string);
+
+	release_marshal_data(&data);
+	free(big_string);
+}
+
 static void
 marshal_helper(const char *format, void *handler, ...)
 {
diff --git a/tests/display-test.c b/tests/display-test.c
index bcb3267..6fc136d 100644
--- a/tests/display-test.c
+++ b/tests/display-test.c
@@ -1513,10 +1513,7 @@ send_overflow_client(void *data)
 	/* Do not close the pipe file descriptors afterwards, because the leak
 	 * check verifies that the initial/final FD counts are the same */
 	assert(write(pipes[1], &tmp, sizeof(tmp)) == (ssize_t)sizeof(tmp));
-
-	/* Expect an error */
-	fprintf(stderr, "Send loop failed on try %d, err = %d, %s\n", i, err, strerror(err));
-	assert(err == EAGAIN);
+	assert(err == 0);
 
 	client_disconnect_nocheck(c);
 }
diff --git a/tests/os-wrappers-test.c b/tests/os-wrappers-test.c
index 8d8c3ab..e65a5d6 100644
--- a/tests/os-wrappers-test.c
+++ b/tests/os-wrappers-test.c
@@ -256,10 +256,12 @@ setup_marshal_data(struct marshal_data *data)
 	assert(socketpair(AF_UNIX,
 			  SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0);
 
-	data->read_connection = wl_connection_create(data->s[0]);
+	data->read_connection = wl_connection_create(data->s[0],
+						     WL_BUFFER_DEFAULT_MAX_SIZE);
 	assert(data->read_connection);
 
-	data->write_connection = wl_connection_create(data->s[1]);
+	data->write_connection = wl_connection_create(data->s[1],
+						      WL_BUFFER_DEFAULT_MAX_SIZE);
 	assert(data->write_connection);
 }
 
-- 
2.43.0