From patchwork Sat Jul 4 03:05:43 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 138471 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AF9E34BA7993 for ; Sat, 4 Jul 2026 03:11:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AF9E34BA7993 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=aC7/AOvu X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id 38CA54BA79AA for ; Sat, 4 Jul 2026 03:10:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 38CA54BA79AA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 38CA54BA79AA Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783134621; cv=none; b=qN/J9CfPL2jhCgcBMOH8Ra3PzBXKboR6dlJuguTYBRqClbZOkkfssW2umZ/r9xPsOECgpriBX80lmd5LOjzYnlDETZzlycSX0N3eZJfCL5/x76/bSYGacW5KfLPL+sdTYkpkMEIfHgKcf5uzIT456btZ03SbBumR/PkuTqeLFng= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783134621; c=relaxed/simple; bh=5mtvXJmxE7D2BdJg4F/GqPq2YeebVq42LGZs+HEN1vE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=v6zPpaUE3e0B3WQVnSx7eKltsdf0VYD7Rvp4tQM6MpLnVNW+afyxpGUy3W8uCqygU9AR4WNpnuHEy4VoILCdZHs7Yh6QcHO0LZVHUpXwzXBCsy0IJRmG8a3M7OeJ2c6ss5DKcgC3U+GixZeMdNUmZ0DIiST54IVu4SAma74WE3s= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=aC7/AOvu DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 38CA54BA79AA Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-845ea8924fdso1188825b3a.0 for ; Fri, 03 Jul 2026 20:10:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783134620; x=1783739420; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=/dFnWBlhw+OoC6VwAe7/4kh3Cri25YiShsF52E7Co7c=; b=aC7/AOvuIJUt35LZSBFIgbenBA3QFLsdpFkQABjjTVftkoxbRENmMpUQIbwKc0nwfr 59wpvClACdXUqGpxeoBuwu3afbkCLfa4CkWYuNYkXqTuxqohd9lRcEq7OOQU9l0keXgQ X5lp/hi2kU93q7qeAm4s+VGea/32tPVpoRfqAWZTlP5tdwtuY1h/yBRnU/J+/8S4nUWX SAe0R3gDiTfT3OfmQkpmlWI4GYuahQdgyTYA/Gs0a0Mfk2q6mRMyZQyQSk6tHiuSCrvw AqL4caLcLim3n0uNQ8MAllvdLXDFG5jfnb3sIPh3CYmAlEb2x8F7tGW13+imo7jOuEfw v+vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783134620; x=1783739420; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/dFnWBlhw+OoC6VwAe7/4kh3Cri25YiShsF52E7Co7c=; b=ErPlihbZLy04fv++mQbNFDEvBX3Fpk+Lck36qPaA+tgbhysJBazFgBImzLKTdvdoCG 9PTXcbGl+uthUHVHBfVOp0Hg4yKR3UHSVnXw9kHcWgIccAAWl1Eg39Rr2+ZplCCmOUgh mvM4kh3Z042VsTfxqGK4gDBDp6RRAgYM8z7ACvMR/BZG2sj7gVdmqUmXObQEUukqtbCw H//owGpJsFB3PL5E0eYFHdfdkukLEg/emt/7SBXsPGkYyPYFPa/EaiNQbsqeBkrU9PLw 7cK+LovVmPnBuvR5W+9U3CEk/FrIixAwQsFgUYW1jBC30BHYNv5Ol5toebTSD+lzoNSD rIug== X-Gm-Message-State: AOJu0Yzcsz505M5KydydMSMfyCz9EflhKg6CBZVXHn0CwyZhbnoS/SvY LUwGETR/ljHFW33FfnKduwKIrWkKj3LDY8cRu3/gvM/kW48kSQDLi0VGta8BkcbcBQA= X-Gm-Gg: AfdE7cmtupvhlWk5gimFwhIo/BVW5v9w3G8Jj1RapenEF34LdKaaUuB/U7w7H5181Jy zygFmqldPhzSTGDqhsEU4wHSRuHh46JTVKNisBJuKD9gwNIe9MoJfOv2MxNu5foKBVV7Un+UmPr F4BI7sQgGsBR+3LlB26xft5MbTC3yasE4ifaRx2sJW16HiY/wfhFJYrTXCjMnqSbq68/VNnhZ9b sacNcUj+f1j+RnILlWMPnvPELUhuE0sy34zT3Vmsh6b4bQBMALszVZoGSBO7AMbASmZLdZpBG6l sqD9/Tdn79uQr+jwUu6u7L7InppkRcgw+blaTYfuXP0wY5EUw8PuT4QYfYvZhHK68+dobOvsfdN 7ou2ZftnO3/TKR1PL6EVikaJ+njhOGANOL4qQ8EHiJ3dfHPASRu7q8bsvwaqeDOzU+bBe7B+TAQ 99ZUT7PXt97oQmTipuiZ/VmHJMKKG9Db0y2RU1hCbVEBYttjQ= X-Received: by 2002:a05:6a21:492:b0:3bf:6c08:4ec1 with SMTP id adf61e73a8af0-3c03e50ca70mr1980796637.54.1783134619811; Fri, 03 Jul 2026 20:10:19 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.56.179.68]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-13b3c85bde6sm41363864c88.9.2026.07.03.20.10.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Jul 2026 20:10:18 -0700 (PDT) Received: from gnu-tgl-3.localdomain (localhost [127.0.0.1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 2BFE8C03F8 for ; Sat, 04 Jul 2026 11:05:48 +0800 (CST) From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH] thp: Disable THP if THP isn't supported by kernel Date: Sat, 4 Jul 2026 11:05:43 +0800 Message-ID: <20260704030543.1077647-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.54.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3012.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_PBL, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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~patchwork=sourceware.org@sourceware.org Since DL_MAP_DEFAULT_THP_PAGESIZE is defined for x86-64, THP control is to set to madvise by default. If THP is disabled in x86-64 kernel, madvise (..., MADV_HUGEPAGE) returns -EINVAL to indicate that THP isn't supported. Add _dl_thp_madvise to disable THP in this case. This fixes BZ #34348. Signed-off-by: H.J. Lu Reviewed-by: Adhemerval Zanella --- sysdeps/unix/sysv/linux/dl-exec-post.h | 6 ++-- sysdeps/unix/sysv/linux/dl-load-post.h | 6 ++-- sysdeps/unix/sysv/linux/dl-thp-madvise.h | 35 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/dl-thp-madvise.h diff --git a/sysdeps/unix/sysv/linux/dl-exec-post.h b/sysdeps/unix/sysv/linux/dl-exec-post.h index 9a49486db2..4bc8a50dd1 100644 --- a/sysdeps/unix/sysv/linux/dl-exec-post.h +++ b/sysdeps/unix/sysv/linux/dl-exec-post.h @@ -17,6 +17,8 @@ License along with the GNU C Library; if not, see . */ +#include "dl-thp-madvise.h" + static inline void _dl_get_thp_config (void) { @@ -116,8 +118,8 @@ _dl_executable_postprocess (struct link_map *main_map, && ((ph->p_vaddr | ph->p_offset) & (thp_pagesize - 1)) == 0 && (ph->p_flags & (PF_W | PF_R)) == PF_R) { - int ret = __madvise ((void *) (main_map->l_addr + ph->p_vaddr), - ph->p_memsz, MADV_HUGEPAGE); + void *addr = (void *) (main_map->l_addr + ph->p_vaddr); + int ret = _dl_thp_madvise (addr, ph->p_memsz); if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) _dl_debug_printf ("\ madvise (0x%0*lx, 0x%0*lx, MADV_HUGEPAGE) returns %d\n", diff --git a/sysdeps/unix/sysv/linux/dl-load-post.h b/sysdeps/unix/sysv/linux/dl-load-post.h index 741c4ddcb1..935298bda5 100644 --- a/sysdeps/unix/sysv/linux/dl-load-post.h +++ b/sysdeps/unix/sysv/linux/dl-load-post.h @@ -17,6 +17,8 @@ License along with the GNU C Library; if not, see . */ +#include "dl-thp-madvise.h" + static bool _dl_segment_thp_eligible (const struct loadcmd *, size_t); /* After L has been mapped in, call madvise with MADV_HUGEPAGE for THP @@ -28,8 +30,8 @@ _dl_postprocess_loadcmd_extra (struct link_map *l, const struct loadcmd *c) if (GL(dl_thp_mode) == thp_mode_madvise && _dl_segment_thp_eligible (c, GL(dl_elf_thp_pagesize))) { - int ret = __madvise ((void *) (l->l_addr + c->mapstart), - c->mapend - c->mapstart, MADV_HUGEPAGE); + int ret = _dl_thp_madvise ((void *) (l->l_addr + c->mapstart), + c->mapend - c->mapstart); if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) _dl_debug_printf ("\ madvise (0x%0*lx, 0x%0*lx, MADV_HUGEPAGE) returns %d\n", diff --git a/sysdeps/unix/sysv/linux/dl-thp-madvise.h b/sysdeps/unix/sysv/linux/dl-thp-madvise.h new file mode 100644 index 0000000000..1d54d19987 --- /dev/null +++ b/sysdeps/unix/sysv/linux/dl-thp-madvise.h @@ -0,0 +1,35 @@ +/* _dl_thp_madvise. Linux version. + Copyright (C) 2026 Free Software Foundation, Inc. + Copyright The GNU Toolchain Authors. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Similar to madvise, but disable THP if the madvise syscall returns + -EINVAL which indicates that THP isn't supported by kernel. */ + +static inline int +_dl_thp_madvise (void *addr, size_t size) +{ + int res = INTERNAL_SYSCALL_CALL (madvise, addr, size, MADV_HUGEPAGE); + if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (res)) + && INTERNAL_SYSCALL_ERRNO (res) == EINVAL) + { + /* NB: Disable THP if THP isn't supported by kernel. */ + GL(dl_thp_mode) = thp_mode_not_supported; + GL(dl_elf_thp_control) = dl_elf_thp_control_disabled; + } + return res; +}