From patchwork Fri Mar 26 18:30:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Pfaff X-Patchwork-Id: 1458921 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4F6VsJ71ppz9sTD for ; Sat, 27 Mar 2021 05:30:44 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 8490840622; Fri, 26 Mar 2021 18:30:41 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ikClxDDfCSze; Fri, 26 Mar 2021 18:30:40 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp2.osuosl.org (Postfix) with ESMTP id 67B234026E; Fri, 26 Mar 2021 18:30:39 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 94C9DC0015; Fri, 26 Mar 2021 18:30:37 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 34387C000A for ; Fri, 26 Mar 2021 18:30:35 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 2329184C50 for ; Fri, 26 Mar 2021 18:30:35 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 2lrNNFxdF4Rs for ; Fri, 26 Mar 2021 18:30:34 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 Received: from relay12.mail.gandi.net (relay12.mail.gandi.net [217.70.178.232]) by smtp1.osuosl.org (Postfix) with ESMTPS id 09A4B84C4F for ; Fri, 26 Mar 2021 18:30:33 +0000 (UTC) Received: from sigfpe.attlocal.net (75-54-222-30.lightspeed.rdcyca.sbcglobal.net [75.54.222.30]) (Authenticated sender: blp@ovn.org) by relay12.mail.gandi.net (Postfix) with ESMTPSA id 98F0320000C; Fri, 26 Mar 2021 18:30:31 +0000 (UTC) From: Ben Pfaff To: dev@openvswitch.org Date: Fri, 26 Mar 2021 11:30:23 -0700 Message-Id: <20210326183024.3214527-2-blp@ovn.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210326183024.3214527-1-blp@ovn.org> References: <20210326183024.3214527-1-blp@ovn.org> MIME-Version: 1.0 Cc: Ben Pfaff Subject: [ovs-dev] [PATCH 2/3] util: Add allocation wrappers that don't increment coverage counters. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" The thread-local data allocators can't increment coverage counters because this can cause reentrancy. Until now, this code has used explicit calls to malloc(). This code replaces them by calls to the new functions. This will make it easier in an upcoming patch to update all the code that can run out of memory. Signed-off-by: Ben Pfaff Acked-by: Ilya Maximets --- lib/ovs-thread.h | 12 ++---------- lib/util.c | 41 +++++++++++++++++++++++++++++++++-------- lib/util.h | 10 +++++++++- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/lib/ovs-thread.h b/lib/ovs-thread.h index 1050fc29af7c..7552a4e4b215 100644 --- a/lib/ovs-thread.h +++ b/lib/ovs-thread.h @@ -294,11 +294,7 @@ void xpthread_join(pthread_t, void **); value = NAME##_get_unsafe(); \ if (!value) { \ static const NAME##_type initial_value = __VA_ARGS__; \ - \ - value = malloc(sizeof *value); \ - if (value == NULL) { \ - out_of_memory(); \ - } \ + value = xmalloc__(sizeof *value); \ *value = initial_value; \ xpthread_setspecific(NAME##_key, value); \ } \ @@ -334,11 +330,7 @@ void xpthread_join(pthread_t, void **); value = NAME##_get_unsafe(); \ if (!value) { \ static const NAME##_type initial_value = __VA_ARGS__; \ - \ - value = malloc(sizeof *value); \ - if (value == NULL) { \ - out_of_memory(); \ - } \ + value = xmalloc__(sizeof *value); \ *value = initial_value; \ xpthread_setspecific(NAME##_key, value); \ } \ diff --git a/lib/util.c b/lib/util.c index 25635b27ff00..1195c7982118 100644 --- a/lib/util.c +++ b/lib/util.c @@ -116,10 +116,9 @@ out_of_memory(void) } void * -xcalloc(size_t count, size_t size) +xcalloc__(size_t count, size_t size) { void *p = count && size ? calloc(count, size) : malloc(1); - COVERAGE_INC(util_xalloc); if (p == NULL) { out_of_memory(); } @@ -127,16 +126,15 @@ xcalloc(size_t count, size_t size) } void * -xzalloc(size_t size) +xzalloc__(size_t size) { - return xcalloc(1, size); + return xcalloc__(1, size); } void * -xmalloc(size_t size) +xmalloc__(size_t size) { void *p = malloc(size ? size : 1); - COVERAGE_INC(util_xalloc); if (p == NULL) { out_of_memory(); } @@ -144,16 +142,43 @@ xmalloc(size_t size) } void * -xrealloc(void *p, size_t size) +xrealloc__(void *p, size_t size) { p = realloc(p, size ? size : 1); - COVERAGE_INC(util_xalloc); if (p == NULL) { out_of_memory(); } return p; } +void * +xcalloc(size_t count, size_t size) +{ + COVERAGE_INC(util_xalloc); + return xcalloc__(count, size); +} + +void * +xzalloc(size_t size) +{ + COVERAGE_INC(util_xalloc); + return xzalloc__(size); +} + +void * +xmalloc(size_t size) +{ + COVERAGE_INC(util_xalloc); + return xmalloc__(size); +} + +void * +xrealloc(void *p, size_t size) +{ + COVERAGE_INC(util_xalloc); + return xrealloc__(p, size); +} + void * xmemdup(const void *p_, size_t size) { diff --git a/lib/util.h b/lib/util.h index 067dcad15786..9c2b14fae304 100644 --- a/lib/util.h +++ b/lib/util.h @@ -145,8 +145,9 @@ void ovs_print_version(uint8_t min_ofp, uint8_t max_ofp); void set_memory_locked(void); bool memory_locked(void); - OVS_NO_RETURN void out_of_memory(void); + +/* Allocation wrappers that abort if memory is exhausted. */ void *xmalloc(size_t) MALLOC_LIKE; void *xcalloc(size_t, size_t) MALLOC_LIKE; void *xzalloc(size_t) MALLOC_LIKE; @@ -160,6 +161,13 @@ char *xasprintf(const char *format, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE; char *xvasprintf(const char *format, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE; void *x2nrealloc(void *p, size_t *n, size_t s); +/* Allocation wrappers for specialized situations where coverage counters + * cannot be used. */ +void *xmalloc__(size_t) MALLOC_LIKE; +void *xcalloc__(size_t, size_t) MALLOC_LIKE; +void *xzalloc__(size_t) MALLOC_LIKE; +void *xrealloc__(void *, size_t); + void *xmalloc_cacheline(size_t) MALLOC_LIKE; void *xzalloc_cacheline(size_t) MALLOC_LIKE; void free_cacheline(void *);