From patchwork Tue May 5 13:55:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 38915 X-Patchwork-Delegate: carlos@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5B631388F063; Tue, 5 May 2020 13:55:34 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from albireo.enyo.de (albireo.enyo.de [37.24.231.21]) by sourceware.org (Postfix) with ESMTPS id 59ACB388F060 for ; Tue, 5 May 2020 13:55:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 59ACB388F060 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=deneb.enyo.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=fw@deneb.enyo.de Received: from [172.17.203.2] (helo=deneb.enyo.de) by albireo.enyo.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) id 1jVy2h-0000oJ-6R for libc-alpha@sourceware.org; Tue, 05 May 2020 13:55:27 +0000 Received: from fw by deneb.enyo.de with local (Exim 4.92) (envelope-from ) id 1jVy2Z-0007i2-NG for libc-alpha@sourceware.org; Tue, 05 May 2020 15:55:19 +0200 From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH] support: Add support_blob_repeat_allocate_shared Date: Tue, 05 May 2020 15:55:19 +0200 Message-ID: <87ftce1o6w.fsf@mid.deneb.enyo.de> MIME-Version: 1.0 X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" ----- support/blob_repeat.c | 31 +++++++++++++++++++++------ support/blob_repeat.h | 12 ++++++++++- support/tst-support_blob_repeat.c | 45 ++++++++++++++++++++++++++++----------- 3 files changed, 68 insertions(+), 20 deletions(-) Reviewed-by: Carlos O'Donell. diff --git a/support/blob_repeat.c b/support/blob_repeat.c index a7aa9bf4c7..cd6297e026 100644 --- a/support/blob_repeat.c +++ b/support/blob_repeat.c @@ -125,10 +125,11 @@ minimum_stride_size (size_t page_size, size_t element_size) } /* Allocations larger than maximum_small_size potentially use mmap - with alias mappings. */ + with alias mappings. If SHARED, the alias mappings are created + using MAP_SHARED instead of MAP_PRIVATE. */ static struct support_blob_repeat allocate_big (size_t total_size, const void *element, size_t element_size, - size_t count) + size_t count, bool shared) { unsigned long page_size = xsysconf (_SC_PAGESIZE); size_t stride_size = minimum_stride_size (page_size, element_size); @@ -213,7 +214,11 @@ allocate_big (size_t total_size, const void *element, size_t element_size, { size_t remaining_size = total_size; char *current = target; - int flags = MAP_FIXED | MAP_FILE | MAP_PRIVATE; + int flags = MAP_FIXED | MAP_FILE; + if (shared) + flags |= MAP_SHARED; + else + flags |= MAP_PRIVATE; #ifdef MAP_NORESERVE flags |= MAP_NORESERVE; #endif @@ -251,8 +256,8 @@ allocate_big (size_t total_size, const void *element, size_t element_size, } struct support_blob_repeat -support_blob_repeat_allocate (const void *element, size_t element_size, - size_t count) +repeat_allocate (const void *element, size_t element_size, + size_t count, bool shared) { size_t total_size; if (__builtin_mul_overflow (element_size, count, &total_size)) @@ -263,7 +268,21 @@ support_blob_repeat_allocate (const void *element, size_t element_size, if (total_size <= maximum_small_size) return allocate_malloc (total_size, element, element_size, count); else - return allocate_big (total_size, element, element_size, count); + return allocate_big (total_size, element, element_size, count, shared); +} + +struct support_blob_repeat +support_blob_repeat_allocate (const void *element, size_t element_size, + size_t count) +{ + return repeat_allocate (element, element_size, count, false); +} + +struct support_blob_repeat +support_blob_repeat_allocate_shared (const void *element, size_t element_size, + size_t count) +{ + return repeat_allocate (element, element_size, count, true); } void diff --git a/support/blob_repeat.h b/support/blob_repeat.h index 12f33bcd02..519458cf50 100644 --- a/support/blob_repeat.h +++ b/support/blob_repeat.h @@ -38,7 +38,17 @@ struct support_blob_repeat support_blob_repeat_allocate (const void *element, size_t element_size, size_t count); -/* Deallocate the blob created by support_blob_repeat_allocate. */ +/* Like support_blob_repeat_allocate, except that copy-on-write + semantics are disabled. This means writing to one part of the blob + can affect other parts. It is possible to map non-shared memory + over parts of the resulting blob using MAP_ANONYMOUS | MAP_FIXED + | MAP_PRIVATE, so that writes to these parts do not affect + others. */ +struct support_blob_repeat support_blob_repeat_allocate_shared + (const void *element, size_t element_size, size_t count); + +/* Deallocate the blob created by support_blob_repeat_allocate or + support_blob_repeat_allocate_shared. */ void support_blob_repeat_free (struct support_blob_repeat *); #endif /* SUPPORT_BLOB_REPEAT_H */ diff --git a/support/tst-support_blob_repeat.c b/support/tst-support_blob_repeat.c index a0eb9d2b89..b61d6b249a 100644 --- a/support/tst-support_blob_repeat.c +++ b/support/tst-support_blob_repeat.c @@ -17,6 +17,7 @@ . */ #include +#include #include #include @@ -63,21 +64,39 @@ do_test (void) } support_blob_repeat_free (&repeat); - repeat = support_blob_repeat_allocate ("012345678", 9, 10 * 1000 * 1000); - if (repeat.start == NULL) - puts ("warning: not enough memory for large mapping"); - else + for (int do_shared = 0; do_shared < 2; ++do_shared) { - unsigned char *p = repeat.start; - for (int i = 0; i < 10 * 1000 * 1000; ++i) - for (int j = 0; j <= 8; ++j) - if (p[i * 9 + j] != '0' + j) - { - printf ("error: element %d index %d\n", i, j); - TEST_COMPARE (p[i * 9 + j], '0' + j); - } + if (do_shared) + repeat = support_blob_repeat_allocate_shared ("012345678", 9, + 10 * 1000 * 1000); + else + repeat = support_blob_repeat_allocate ("012345678", 9, + 10 * 1000 * 1000); + if (repeat.start == NULL) + puts ("warning: not enough memory for large mapping"); + else + { + unsigned char *p = repeat.start; + for (int i = 0; i < 10 * 1000 * 1000; ++i) + for (int j = 0; j <= 8; ++j) + if (p[i * 9 + j] != '0' + j) + { + printf ("error: element %d index %d\n", i, j); + TEST_COMPARE (p[i * 9 + j], '0' + j); + } + + enum { total_size = 9 * 10 * 1000 * 1000 }; + p[total_size - 1] = '\0'; + asm ("" ::: "memory"); + if (do_shared) + /* The write is repeated in multiple places earlier in the + string due to page sharing. */ + TEST_VERIFY (strlen (repeat.start) < total_size - 1); + else + TEST_COMPARE (strlen (repeat.start), total_size - 1); + } + support_blob_repeat_free (&repeat); } - support_blob_repeat_free (&repeat); return 0; }