From patchwork Tue Sep 12 13:15:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 721766 Delivered-To: patch@linaro.org Received: by 2002:adf:f64d:0:b0:31d:da82:a3b4 with SMTP id x13csp1635577wrp; Tue, 12 Sep 2023 06:15:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFud8+AeEKMXRCQ+5RfLWSrbB+lg/qCrnxUuc8Omy2vJ1szHcF2gukiSKWevFbg8Ayotv0z X-Received: by 2002:a2e:9b97:0:b0:2bc:ddf2:4fcb with SMTP id z23-20020a2e9b97000000b002bcddf24fcbmr10115794lji.1.1694524524717; Tue, 12 Sep 2023 06:15:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694524524; cv=none; d=google.com; s=arc-20160816; b=BhxW+qwD5SmUrHN0ikBISpoi54s9e4i5bkN/7TKvTx0NiTvVITQsydKVgKyCas2ala e7LEjr9jbUa6DydZq2hVtgufRqWipZ/joh7if+8F3/grkbLHpJKAQu9bo41Z0U6HbfDv uWcv1N2SpYACc/ZDra4ILMIY/+VxzuyY5xp5bDMISpeRrdXRqKuKrcGfcmPdkTg7aVkY xukl4TS7lTyLpTI886KNPs/kI8Vpci7JOVVnbjph8DXnk6SO68tqmX+mVkVwcMYlnv3k ImbIRGSuQV8tmQpA4ngEe9fDFnvty/KZpRsCKzfpsvs/0E7LCKlc9I/wKE6HghXwQgXf 4mhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dmarc-filter:delivered-to; bh=byUh0fkTXwGO90iS7vgYnvL9snEsS5I2Z7uquGyrEWo=; fh=dHLBnA+MhGtNtN2B2JMAELi4oD+gmgMg7DL8H0jYbkI=; b=x+VmcXanYtHYRz0fYzvBK0z3EMz9DDjZYBT+vv6ruro7Zzb3ulbm4Eq+WzUtIYGC9O Evjx3Ihi7ffibPC3e/Lfnv7cBBd6Zcw+dfc9vHF0lhtm2dF1lrvMgqtb01q/n4U6jGr1 SzPbS0+8ZYhEvTXhryzffjOgh67XNJHWql+SZH7urY0OzfbYKww2uQrfRjbhBSPI9Yt+ N+nQO4TjiFYxI3gxRMEMZ6EsQWp9j7mrypk7zQ43rxaLc1bcyIcT4MDuatHbitybDBu5 UnjrCvaCZFHvXCSDfRK+AnvB+SSPzJiN5j0N4rbE6RQGLHI+PSvjRMH0fR0IAvN5GtOZ GLtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WPfCWqjM; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id pk24-20020a170906d7b800b0099bcb54fae7si8886247ejb.512.2023.09.12.06.15.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 06:15:24 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WPfCWqjM; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9D1D238560A4 for ; Tue, 12 Sep 2023 13:15:23 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id 05B52385842A for ; Tue, 12 Sep 2023 13:15:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 05B52385842A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-ot1-x331.google.com with SMTP id 46e09a7af769-6c09f1f9df2so4197518a34.2 for ; Tue, 12 Sep 2023 06:15:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694524513; x=1695129313; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=byUh0fkTXwGO90iS7vgYnvL9snEsS5I2Z7uquGyrEWo=; b=WPfCWqjMrTedO9gAdJ5UW3Leg3Iqr5yuRWci2u1nFk6CPGk1F8QpiBvcTqvxVKFbXW Iot+sdMFlL/CiGHxeceV4VNyxsiwSOHFNxzlrBZYj5T5zIe9hesXGvrmvVdkIwQYUnxt 4vglXQaj3enKiQj1y4Gk2WTPZ7ZfTVMmNGguLpdaGzZchI5jxgo56Bge/+Pfx5I/wn7r LyIdACLUIX1x1A30CeGbmn+TzC1kf7S7oEkGkWqsZsWzCFF6e23e2X4cN3wBXaMg8zhw nM0eEf0Enfg8tDSjvwcCmCX2UrgHUs7+7cMChWt7+uB8aCbIC5W8XHw6OPOilo5Q2K0Y wU5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694524513; x=1695129313; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=byUh0fkTXwGO90iS7vgYnvL9snEsS5I2Z7uquGyrEWo=; b=MW+XGSTayGqLVjBzojla5oEqMYE3lwU3+dzqndAUcRYKkV5D9PkuMkeAznALmYCJtQ D+5B4fPff72YE/qK3Soi8B58mwwnL0WxZb0LRD21oANpVrdaYrXKSDVSptAcYLSE4jOm R/IzzCnb/45CghPtCbAAbcQYsqmyeP6Eb+M0s0bQbK3+SQJk0M+ZUXbUJnSdBInxI331 69MWT9yDJnR+az+9nEoHjsL0psWu46uhdt1MGkHAcVQ/M78Ybf/j0DrFdbrFLaV9VfwC AVmC0pWH1eLqG55iSg9DA1/sIhF7jQfICRcaQZ0ppMnjsmcRYQ5UJYpM5f/+FoA6OdYz H5qA== X-Gm-Message-State: AOJu0YziamEk5TCci2OPqz2avqGVRCnqLsDNbQtJHm+xRDQTYU4cQmM+ bwJ4SnHiOO1AlsweEpoMfpOrRnWp6GLhYVfIjSWJvA== X-Received: by 2002:a05:6830:1493:b0:6b5:8a98:f593 with SMTP id s19-20020a056830149300b006b58a98f593mr13139541otq.8.1694524513460; Tue, 12 Sep 2023 06:15:13 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:91cb:e4b5:40b4:e1ca:7dfe]) by smtp.gmail.com with ESMTPSA id x38-20020a4a97e9000000b00569c5eadf15sm4336813ooi.26.2023.09.12.06.15.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 06:15:12 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 2/2] linux: Use procutils_read_file on malloc-hugepages Date: Tue, 12 Sep 2023 10:15:06 -0300 Message-Id: <20230912131506.2571612-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230912131506.2571612-1-adhemerval.zanella@linaro.org> References: <20230912131506.2571612-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org Checked on x86_64-linux-gnu. --- sysdeps/aarch64/Makefile | 5 +- sysdeps/unix/sysv/linux/Makefile | 6 ++ sysdeps/unix/sysv/linux/malloc-hugepages.c | 68 ++++++++++------------ 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/sysdeps/aarch64/Makefile b/sysdeps/aarch64/Makefile index 6a9559e5f5..6c80ee8479 100644 --- a/sysdeps/aarch64/Makefile +++ b/sysdeps/aarch64/Makefile @@ -67,5 +67,8 @@ sysdep_routines += __mtag_tag_zero_region \ endif ifeq ($(subdir),malloc) -sysdep_malloc_debug_routines = __mtag_tag_zero_region __mtag_tag_region +sysdep_malloc_debug_routines += \ + __mtag_tag_zero_region \ + __mtag_tag_region \ + # sysdep_malloc_debug_routines endif diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 063719bae6..d4ab43c8f2 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -637,6 +637,12 @@ CFLAGS-mq_send.c += -fexceptions CFLAGS-mq_receive.c += -fexceptions endif +ifeq ($(subdir),malloc) +sysdep_malloc_debug_routines += \ + procutils \ + # sysdep_malloc_debug_routines +endif + ifeq ($(subdir),nscd) sysdep-CFLAGS += -DHAVE_EPOLL -DHAVE_INOTIFY -DHAVE_NETLINK CFLAGS-gai.c += -DNEED_NETLINK diff --git a/sysdeps/unix/sysv/linux/malloc-hugepages.c b/sysdeps/unix/sysv/linux/malloc-hugepages.c index 2f316474c1..cec034e07f 100644 --- a/sysdeps/unix/sysv/linux/malloc-hugepages.c +++ b/sysdeps/unix/sysv/linux/malloc-hugepages.c @@ -16,10 +16,12 @@ License along with the GNU C Library; see the file COPYING.LIB. If not, see . */ -#include #include +#include #include +#include #include +#include #include unsigned long int @@ -78,51 +80,41 @@ __malloc_thp_mode (void) return malloc_thp_mode_not_supported; } -static size_t -malloc_default_hugepage_size (void) +static int +proc_meminfo_parse (const char *s, void *arg) { - int fd = __open64_nocancel ("/proc/meminfo", O_RDONLY); - if (fd == -1) + enum { HUGEPAGESIZELEN = sizeof ("Hugepagesize:") - 1 }; + if (strncmp (s, "Hugepagesize:", HUGEPAGESIZELEN) != 0) return 0; - size_t hpsize = 0; + /* The default huge page size is in the form: + Hugepagesize: NUMBER kB */ + s += HUGEPAGESIZELEN; - char buf[512]; - off64_t off = 0; - while (1) + size_t hpsize = 0; + for (int i = 0; (s[i] >= '0' && s[i] <= '9') || s[i] == ' '; i++) { - ssize_t r = __pread64_nocancel (fd, buf, sizeof (buf) - 1, off); - if (r < 0) - break; - buf[r] = '\0'; - - /* If the tag is not found, read the last line again. */ - const char *s = strstr (buf, "Hugepagesize:"); - if (s == NULL) - { - char *nl = strrchr (buf, '\n'); - if (nl == NULL) - break; - off += (nl + 1) - buf; - continue; - } - - /* The default huge page size is in the form: - Hugepagesize: NUMBER kB */ - s += sizeof ("Hugepagesize: ") - 1; - for (int i = 0; (s[i] >= '0' && s[i] <= '9') || s[i] == ' '; i++) - { - if (s[i] == ' ') - continue; - hpsize *= 10; - hpsize += s[i] - '0'; - } - hpsize *= 1024; - break; + if (s[i] == ' ') + continue; + + if (INT_MULTIPLY_WRAPV (hpsize, 10, &hpsize) + || INT_ADD_WRAPV (hpsize, s[i] - '0', &hpsize)) + return -1; } + if (INT_MULTIPLY_WRAPV (hpsize, 1024, &hpsize)) + return -1; - __close_nocancel (fd); + *(size_t*) arg = hpsize; + return 1; +} +static size_t +malloc_default_hugepage_size (void) +{ + size_t hpsize = 0; + /* If procfs is not accessible of if huge page field is not present, + the 'hpsize' argument is not changed. */ + __procutils_read_file ("/proc/meminfo", proc_meminfo_parse, &hpsize); return hpsize; }