From patchwork Fri Apr 14 14:28:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cupertino Miranda X-Patchwork-Id: 67740 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 7C2543858433 for ; Fri, 14 Apr 2023 14:28:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7C2543858433 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681482530; bh=mtjz2EAgbgqtboBCGAmylRSrdjlYQA9o9kUWAfhXkXU=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=vTkE7+oi502JynMcKm+rdLyVqfxT9aEF7hugTySE2FwvgncqxDyqms1pCvFD7Gnrp E55t8XBGZb11u1Are27cV/Clvvrp3h+lBFqPo28DJaQaS4MI9NGqzhZrrRoc2QCJUZ 0v2kFtGqgj+i30HEsUnqM/EZR89BSsH6MRD82GT8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 07A643858C39 for ; Fri, 14 Apr 2023 14:28:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 07A643858C39 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33E5rbmR028877 for ; Fri, 14 Apr 2023 14:28:26 GMT Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3pu0ttx6hf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 14 Apr 2023 14:28:25 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 33EEIk2L018829 for ; Fri, 14 Apr 2023 14:28:25 GMT Received: from nam02-bn1-obe.outbound.protection.outlook.com (mail-bn1nam02lp2044.outbound.protection.outlook.com [104.47.51.44]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3puwbt6wrg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 14 Apr 2023 14:28:25 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fSVJkRAD13MH+GiUgv0q2SvXd0FfRAZv8XtFFkfXC9dcExxwxMPNnanWFhurCg3pZlHKcyXtX6im9lXrlQYQxgvrDfhFJML1jnUrB/47DF1SmKkqU37G3Pxacd4YMV+6Dhu4ZLikaQ4fTng0jFRy23u6INbBGeD7tK2E4/0pibFgP8MPCWCFpVLwkQPJHfBTN1P6ElxHxVU2OXzOG1dHNj6crbxjIvr1w2Ny1kY6VfPKNalymTxwj2BOlaPxnzaS8gmGMAPtgOrfO410NNjDLZYGtTpzwR6m0b9bikPPgWIHQK4vvRtUpDA+qBA0/iefqbMDCaxmY8d+PT5vVidDaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=mtjz2EAgbgqtboBCGAmylRSrdjlYQA9o9kUWAfhXkXU=; b=iM2NMHNKhlpag9er/C6ft/q1XylRv79SSICYen44cY3cZ3wXWsRwb5q0BLq8MlQYtBc/nh0xTMHadcibbz8Q89Z5L6HI9YmCGAVrvdfC7cMZ5z+9wzPFNbGa/5Fl3EXU7H7q6ApwV3BPBX6HhblS5LpSBqQdzrhQxK2ihANtl7svODyyRZmE0UHivcztNb/lDXr35+twfTC4ZDtYTXRlq7WikxbpHsddEmXQmh6YflKmM8hU0wKD2lQbc44xGZok8BXpUoSEWFnloZKH3ZFwp4XnK4NzMA+lAmeEIB7vcJYqc14rSmRBBNiF1bNvVXbp0iBkZkbEwuSoxJukzEzGGw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from BN6PR1001MB2340.namprd10.prod.outlook.com (2603:10b6:405:30::36) by DM4PR10MB5919.namprd10.prod.outlook.com (2603:10b6:8:aa::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.47; Fri, 14 Apr 2023 14:28:23 +0000 Received: from BN6PR1001MB2340.namprd10.prod.outlook.com ([fe80::4998:96fd:86b1:a388]) by BN6PR1001MB2340.namprd10.prod.outlook.com ([fe80::4998:96fd:86b1:a388%6]) with mapi id 15.20.6277.036; Fri, 14 Apr 2023 14:28:23 +0000 To: libc-alpha@sourceware.org Cc: jose.marchesi@oracle.com, elena.zannoni@oracle.com, Cupertino Miranda Subject: [PATCH v6 1/1] Created tunable to force small pages on stack allocation. Date: Fri, 14 Apr 2023 15:28:10 +0100 Message-Id: <20230414142810.50663-2-cupertino.miranda@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230414142810.50663-1-cupertino.miranda@oracle.com> References: <20230414142810.50663-1-cupertino.miranda@oracle.com> X-ClientProxiedBy: LO4P123CA0587.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:295::15) To BN6PR1001MB2340.namprd10.prod.outlook.com (2603:10b6:405:30::36) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN6PR1001MB2340:EE_|DM4PR10MB5919:EE_ X-MS-Office365-Filtering-Correlation-Id: 57291401-b038-47f2-afc9-08db3cf4807a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cp3s/uA52VT7WXMHfU7t9L/ioLpRnMTySp+pka9nCwgS1TA3Z1eG/oguGU7NdXOcd05edGF8UQ3r3G8B/HQR23MNt5D1Mb2jbvKJqINgW3l5myfMVUXlWREoXmysHwyXjR3f0qZtChNxivhZIsB5btnp2FD26raOYAjjaKRg7Yk+7sBQYECrYrJv3SMpCWb04rteZToR/Qnq3XIrZqz2y63kf7VjiPYvndmXywIHm08To0ENZ4jMM9gjbplwkIhcDJAee5HRVdQQ3sTvCEYWhFtvsFjZSgF/aBOr0KlUS96m6IzWwM7MB5xXI70BG0n7AoXkfpBsPaQP3w4Wh5EJXFT2JWwR8jgnzlcWm+btD1VZv61vvy4a02epscCP9hYUBvUvqdRyzXP1dr0mOU8rJz7DYjdVmPiSKdE96Xz3MiZZZX6DqLC6boiMPcrYNGBdie5ATuZEn4vb1wHTw1MO6nYpKYXx5H/e5+JvfBLkJeIKEd3y0Rq+YaBQUxW/xth/QfpxadfjPWhsMzfh0G024hq+Hnwn2Tqv2lWUJmUVmBAzOa3xSrLaky15vbxUjs3G X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BN6PR1001MB2340.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(366004)(136003)(346002)(396003)(376002)(39860400002)(451199021)(6512007)(6506007)(1076003)(86362001)(186003)(38100700002)(2616005)(2906002)(6666004)(107886003)(316002)(5660300002)(6486002)(8936002)(66476007)(478600001)(66556008)(66946007)(44832011)(8676002)(41300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FjLCE1TNMV19IaA7BsZHWF7Zvwk4CAdel6rmVUAa+dVxmBAV5NwCxVZlSdWlQ+Bm8M5kgdCoqAIOLWlLpw139f4XMZ4yhTDqVMUjWvwOpPACCqALYObd+QWKGxOx0GUXK0h3JPIg8kCwpPm3rIvO3xrN+ZQz+ReB/1rqL9yaImHguM5Qe/L0b6zRLqVhcDodU0pzm9fkakZZVrVQvKzNvdCDdk9LfmwQtUhqOfXQQ/fvZsE4rTgLrz5afUfFnEGcTDwXNaKaPE322vOQ1BOWKm0aObA/uozM87ZMmwuCp61DXhEoHeJRI0YyaFVRO0bcBt3owyVOfnRqp7eSJQy6eD2AdmpFHS9JnGoxi+i6k1RgywBQpVEHWRzPmeQ3CDOCnKfgaLBiDalCYg+gqbPLJwKTWMitABNts0 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 57291401-b038-47f2-afc9-08db3cf4807a X-MS-Exchange-CrossTenant-AuthSource: BN6PR1001MB2340.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Apr 2023 14:28:23.0270 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: aYGv8DID3RexoYV7TtLy7bV2K2AQFVhQC3y5ama4vER2ljF81kIAdzJvqbQopw+LCjt5ItrGmIpk9vT9MWFPlK7ilGbChyXa9iutjwJ9nTg= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR10MB5919 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-14_07,2023-04-14_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 malwarescore=0 suspectscore=0 bulkscore=0 spamscore=0 phishscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2304140128 X-Proofpoint-ORIG-GUID: KmFWi6nHF_v2BGrNWHYZ_jzQzRfDbmRX X-Proofpoint-GUID: KmFWi6nHF_v2BGrNWHYZ_jzQzRfDbmRX X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Cupertino Miranda via Libc-alpha From: Cupertino Miranda Reply-To: Cupertino Miranda Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Created tunable glibc.pthread.stack_hugetlb to control when hugepages can be used for stack allocation. In case THP are enabled and glibc.pthread.stack_hugetlb is set to 0, glibc will madvise the kernel not to use allow hugepages for stack allocations. Changed from v1: - removed the __malloc_thp_mode calls to check if hugetlb is enabled. Changed from v2: - Added entry in manual/tunables.texi - Fixed tunable default to description - Code style corrections. Changes from v3: - Improve tunables.texi. Changes from v4: - Improved text in tunables.texi by suggestion of Adhemerval. Changes from v5: - Added a new entry in NEWS. --- NEWS | 3 +++ manual/tunables.texi | 15 +++++++++++++++ nptl/allocatestack.c | 6 ++++++ nptl/nptl-stack.c | 1 + nptl/nptl-stack.h | 3 +++ nptl/pthread_mutex_conf.c | 8 ++++++++ sysdeps/nptl/dl-tunables.list | 6 ++++++ 7 files changed, 42 insertions(+) diff --git a/NEWS b/NEWS index c54af824e0..941808049a 100644 --- a/NEWS +++ b/NEWS @@ -21,6 +21,9 @@ Major new features: * PRIb* and PRIB* macros from C2X have been added to . +* A new tunable, glibc.pthread.stack_hugetlb, can be used to disable + Transparent Huge Pages (THP) in stack allocation at pthread_create. + Deprecated and removed features, and other changes affecting compatibility: * In the Linux kernel for the hppa/parisc architecture some of the diff --git a/manual/tunables.texi b/manual/tunables.texi index 70dd2264c5..130f94b2bc 100644 --- a/manual/tunables.texi +++ b/manual/tunables.texi @@ -459,6 +459,21 @@ registration on behalf of the application. Restartable sequences are a Linux-specific extension. @end deftp +@deftp Tunable glibc.pthread.stack_hugetlb +This tunable controls whether to use Huge Pages in the stacks created by +@code{pthread_create}. This tunable only affects the stacks created by +@theglibc{}, it has no effect on stack assigned with +@code{pthread_attr_setstack}. + +The default is @samp{1} where the system default value is used. Setting +its value to @code{0} enables the use of @code{madvise} with +@code{MADV_NOHUGEPAGE} after stack creation with @code{mmap}. + +This is a memory utilization optimization, since internal glibc setup of either +the thread descriptor and the guard page might force the kernel to move the +thread stack originally backup by Huge Pages to default pages. +@end deftp + @node Hardware Capability Tunables @section Hardware Capability Tunables @cindex hardware capability tunables diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c index c7adbccd6f..f9d8cdfd08 100644 --- a/nptl/allocatestack.c +++ b/nptl/allocatestack.c @@ -369,6 +369,12 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp, if (__glibc_unlikely (mem == MAP_FAILED)) return errno; + /* Do madvise in case the tunable glibc.pthread.stack_hugetlb is + set to 0, disabling hugetlb. */ + if (__glibc_unlikely (__nptl_stack_hugetlb == 0) + && __madvise (mem, size, MADV_NOHUGEPAGE) != 0) + return errno; + /* SIZE is guaranteed to be greater than zero. So we can never get a null pointer back from mmap. */ assert (mem != NULL); diff --git a/nptl/nptl-stack.c b/nptl/nptl-stack.c index 5eb7773575..e829711cb5 100644 --- a/nptl/nptl-stack.c +++ b/nptl/nptl-stack.c @@ -21,6 +21,7 @@ #include size_t __nptl_stack_cache_maxsize = 40 * 1024 * 1024; +int32_t __nptl_stack_hugetlb = 1; void __nptl_stack_list_del (list_t *elem) diff --git a/nptl/nptl-stack.h b/nptl/nptl-stack.h index 34f8bbb15e..cf90b27c2b 100644 --- a/nptl/nptl-stack.h +++ b/nptl/nptl-stack.h @@ -27,6 +27,9 @@ /* Maximum size of the cache, in bytes. 40 MiB by default. */ extern size_t __nptl_stack_cache_maxsize attribute_hidden; +/* Should allow stacks to use hugetlb. (1) is default. */ +extern int32_t __nptl_stack_hugetlb; + /* Check whether the stack is still used or not. */ static inline bool __nptl_stack_in_use (struct pthread *pd) diff --git a/nptl/pthread_mutex_conf.c b/nptl/pthread_mutex_conf.c index 329c4cbb8f..60ef9095aa 100644 --- a/nptl/pthread_mutex_conf.c +++ b/nptl/pthread_mutex_conf.c @@ -45,6 +45,12 @@ TUNABLE_CALLBACK (set_stack_cache_size) (tunable_val_t *valp) __nptl_stack_cache_maxsize = valp->numval; } +static void +TUNABLE_CALLBACK (set_stack_hugetlb) (tunable_val_t *valp) +{ + __nptl_stack_hugetlb = (int32_t) valp->numval; +} + void __pthread_tunables_init (void) { @@ -52,5 +58,7 @@ __pthread_tunables_init (void) TUNABLE_CALLBACK (set_mutex_spin_count)); TUNABLE_GET (stack_cache_size, size_t, TUNABLE_CALLBACK (set_stack_cache_size)); + TUNABLE_GET (stack_hugetlb, int32_t, + TUNABLE_CALLBACK (set_stack_hugetlb)); } #endif diff --git a/sysdeps/nptl/dl-tunables.list b/sysdeps/nptl/dl-tunables.list index bd1ddb121d..4cde9500b6 100644 --- a/sysdeps/nptl/dl-tunables.list +++ b/sysdeps/nptl/dl-tunables.list @@ -33,5 +33,11 @@ glibc { maxval: 1 default: 1 } + stack_hugetlb { + type: INT_32 + minval: 0 + maxval: 1 + default: 1 + } } }