From patchwork Wed Jan 19 21:09:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Kozlyuk X-Patchwork-Id: 106096 Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 6153BA0351; Wed, 19 Jan 2022 22:09:38 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 215BA41147; Wed, 19 Jan 2022 22:09:38 +0100 (CET) Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam08on2060.outbound.protection.outlook.com [40.107.102.60]) by mails.dpdk.org (Postfix) with ESMTP id D4A284013F for ; Wed, 19 Jan 2022 22:09:36 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SYUlI+lzWvlPf6Z5dAtJqPp5Y3xLmZWfwfow5uNNgOrFv9eV3+8nDj/5bWItaxugrOW7ViN0FVOXFY+MpLDA/7p/x+r/R3E2jSCdXT93s+lZ9XqYY5uxjX2gaq+J/UcfPDNFvQytWLQrKmdr1soufjsRDe2SluEVIQU7N5rHKoeZ6WBYym7l5YVd1rC7IVTVhZrqU7tzzpBoTgoIbMXtfTJtgvt/R/yS6JtEnG9/JkUJfxLnVj5XeLfzxuSGYVRajAo9dDP0fsbJvNM4D1n6TNjZUjw65dr2NiLSA22DRNHOFL/7wysiP+6D+lZI2D9jISffR4ERD/zRiWTPpE5WfQ== 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=LJbd385sqE8FnFTIIB+CyeTkZs+dITXloEeCZhFtKrw=; b=nakDJHBVu3NZg3/ybzWNc+Im2YXwhpshuSVo0oSjN8tos2dGPEvaPaYQ89v2CGQFQ/lDbjrR/GobxGzh9Rs6qyJd4tywg+jm3Nszt4Zc57b8p+qot8lR+Ao2z45KhB2Ua2D6bHX3CwWxH1doNP1/BlC6mTtu+5Okl+2K6vI1fnbyfNSA5bULPlyiWVshS+PEOXFEzUVsSgq9ySEQJCN921FJkd7JDsQGElx+wCneJ8Wm1fcj3e5z90bGOhjYHNUSCDlCqEZHw1yxOX7gUzmHjUmv3ah4qk3VIVAAVLIsBXYUxE+C9Typ4EuVcIkhoejs7dAvt3ApvhgY/GNpWikUcw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.235) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LJbd385sqE8FnFTIIB+CyeTkZs+dITXloEeCZhFtKrw=; b=gzE9L1CK4BjtMS49zF1NyfHCK5LCx06uVacLPKSRji/BIYlGNwHMMLrMoVxU3+zV79xrOeXML048G6q+VPoJmUZJNjA6lJlhO15fvmB9ucyHOosKcQmuCTqXH48mFzkLr/5X54ENWCQNnI80r+EmDiP4LnnW7eHM++li6G6aOyl9VEuY30cAqQXDw8N69XxUo5Jd7zM0gMPJ09xkNrcuCwe/lUdRsGMfD9xBSvqZCgsYOtHTeQDzcN7S98LPoQPoorf/BAX32j/9HgllTPr/HtTfnPHu+kqNJQeBrwOrpUxYZMBt9K0VlWFHoLi5elSgpdZikPCCJnbxWUYHDFK0tg== Received: from MWHPR17CA0049.namprd17.prod.outlook.com (2603:10b6:300:93::11) by BY5PR12MB5527.namprd12.prod.outlook.com (2603:10b6:a03:1d5::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4888.11; Wed, 19 Jan 2022 21:09:34 +0000 Received: from CO1NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:300:93:cafe::fd) by MWHPR17CA0049.outlook.office365.com (2603:10b6:300:93::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4909.8 via Frontend Transport; Wed, 19 Jan 2022 21:09:34 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.235) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.235 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.235; helo=mail.nvidia.com; Received: from mail.nvidia.com (12.22.5.235) by CO1NAM11FT043.mail.protection.outlook.com (10.13.174.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.4909.7 via Frontend Transport; Wed, 19 Jan 2022 21:09:33 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by DRHQMAIL107.nvidia.com (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Wed, 19 Jan 2022 21:09:33 +0000 Received: from nvidia.com (10.126.230.35) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.986.9; Wed, 19 Jan 2022 13:09:31 -0800 From: Dmitry Kozlyuk To: CC: Bruce Richardson , Anatoly Burakov , Viacheslav Ovsiienko , David Marchand , Thomas Monjalon , Lior Margalit Subject: [PATCH v2 0/6] Fast restart with many hugepages Date: Wed, 19 Jan 2022 23:09:11 +0200 Message-ID: <20220119210917.765505-1-dkozlyuk@nvidia.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220117080801.481568-1-dkozlyuk@nvidia.com> References: <20220117080801.481568-1-dkozlyuk@nvidia.com> MIME-Version: 1.0 X-Originating-IP: [10.126.230.35] X-ClientProxiedBy: HQMAIL105.nvidia.com (172.20.187.12) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: baf3a5e7-cb56-44ba-58da-08d9db8ffe32 X-MS-TrafficTypeDiagnostic: BY5PR12MB5527:EE_ X-LD-Processed: 43083d15-7273-40c1-b7db-39efd9ccc17a,ExtAddr X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /FrkcMXYiwXztMVlrLQwoWPPsmwjtFsZ9kISk9nNZ6rAxmLz19AApu0vUu6vzC4x61am0cmy6XdKGNA3z21l+/xB3LVxwG6IvqhR0qOcb7R15RbUcuLvmfYhlLWnhL+9Y4Csd/DBkTGvfrmgnLG2L67TsPEXVgKv1esm5PFu2eDnJBdKsHTiRuTlNf/0973ZMZIyf4s2GYDZqT3DMMV+CCxdfK3x5GSOeQg17FEGpKJNlt6whfFHJNP9LmdpfnxXsr3R22E3PtaZsWzP5YbhJebtWtAqb3WGhNYLZNwODD+LO2FxqvSWZUhu0KbZoVuO1fNHhYT4USEjCaD9SqKc47mwLQDVHx/rA5+/zrteNFFgOZ75byuR6PM8Po0mrfD68CltuYtveyEbFeWKPVg76qEc7CAf87w6WTKpozQtFTz0Wwx8qX2YcBQnle+ysbUQZFAaglx2h/vhidntNLtkLmBcFIkiALJRFlLBeR44m1ymxaq1qy/OG6Z0AuWs9Yq0Ayxy5rwh0RHV8Js50OecUeaY/wluSW5hU478jJ4oJUYZeG7ObMyuWkqDwh1t/fZs2Wu+OBcp8RczYBUTUS91IdO/envGJGfDGl9jDixP8GwFO0sfVNphWbNeP32CLkmSI+YvNIRIJVfGDjnBrEm6E/asE/MXKcFPKktiJShkoyidAjYCiTCAAa9BMmPQ8aAtkAH+CDx5U+T+ju9hNGvLoleVSL/a9fcKyuZ8H3SmLw6yAV7FOWNJgDwW5aZlUq0xmJLV/9VVyU4uw5pww6/NUF8dN0hmQ3VZSv1yulu2WbCHg/Y97ouaSez9sLeHVjyQ79QfAUtwnA0/GWWmKPjfo1kItoUx1Y8xenSFTc9HeMWuzh88Kusa+Cip7nAhOG2Z X-Forefront-Antispam-Report: CIP:12.22.5.235; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(4636009)(46966006)(40470700002)(36840700001)(7696005)(508600001)(83380400001)(36860700001)(6916009)(5660300002)(186003)(2906002)(40460700001)(47076005)(81166007)(336012)(107886003)(55016003)(966005)(8936002)(86362001)(82310400004)(356005)(4326008)(6666004)(2616005)(70586007)(26005)(426003)(36756003)(1076003)(70206006)(54906003)(316002)(16526019)(6286002)(8676002)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Jan 2022 21:09:33.9904 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: baf3a5e7-cb56-44ba-58da-08d9db8ffe32 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[12.22.5.235]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT043.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5527 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patchset is a new design and implementation of [1]. v2: * Fix hugepage file removal when they are no longer used. Disable removal with --huge-unlink=never as intended. Document this behavior difference. (Bruce) * Improve documentation, commit messages, and naming. (Thomas) # Problem Statement Large allocations that involve mapping new hugepages are slow. This is problematic, for example, in the following use case. A single-process application allocates ~1TB of mempools at startup. Sometimes the app needs to restart as quick as possible. Allocating the hugepages anew takes as long as 15 seconds, while the new process could just pick up all the memory left by the old one (reinitializing the contents as needed). Almost all of mmap(2) time spent in the kernel is clearing the memory, i.e. filling it with zeros. This is done if a file in hugetlbfs is mapped for the first time system-wide, i.e. a hugepage is committed to prevent data leaks from the previous users of the same hugepage. For example, mapping 32 GB from a new file may take 2.16 seconds, while mapping the same pages again takes only 0.3 ms. Security put aside, e.g. when the environment is controlled, this effort is wasted for the memory intended for DMA, because its content will be overwritten anyway. Linux EAL explicitly removes hugetlbfs files at initialization and before mapping to force the kernel clear the memory. This allows the memory allocator to clean memory on only on freeing. # Solution Add a new mode allowing EAL to remap existing hugepage files. While it is intended to make restarts faster in the first place, it makes any startup faster except the cold one (with no existing files). It is the administrator who accepts security risks implied by reusing hugepages. The new mode is an opt-in and a warning is logged. The feature is Linux-only as it is related to mapping hugepages from files which only Linux does. It is inherently incompatible with --in-memory, for --huge-unlink see below. There is formally no breakage of API contract, but there is a behavior change in the new mode: rte_malloc*() and rte_memzone_reserve*() may return dirty memory (previously they were returning clean memory from free heap elements). Their contract has always explicitly allowed this, but still there may be users relying on the traditional behavior. Such users will need to fix their code to use the new mode. # Implementation ## User Interface There is --huge-unlink switch in the same area to remove hugepage files before mapping them. It is infeasible to use with the new mode, because the point is to keep hugepage files for fast future restarts. Extend --huge-unlink option to represent only valid combinations: * --huge-unlink=existing OR no option (for compatibility): unlink files at initialization and before opening them as a precaution. * --huge-unlink=always OR just --huge-unlink (for compatibility): same as above + unlink created files before mapping. * --huge-unlink=never: the new mode, do not unlink hugepages files, reuse them. This option was always Linux-only, but it is kept as common in case there are users who expect it to be a no-op on other systems. (Adding a separate --huge-reuse option was also considered, but there is no obvious benefit and more combinations to test.) ## EAL If a memseg is mapped dirty, it is marked with RTE_MEMSEG_FLAG_DIRTY so that the memory allocator may clear the memory if need be. See patch 5/6 description for details how this is done in different memory mapping modes. The memory manager tracks whether an element is clean or dirty. If rte_zmalloc*() allocates from a dirty element, the memory is cleared before handling it to the user. On freeing, the allocator joins adjacent free elements, but in the new mode it may not be feasible to clear the free memory if the joint element is dirty (contains dirty parts). In any case, memory will be cleared only once, either on freeing or on allocation. See patch 3/6 for details. Patch 2/6 adds a benchmark to see how time is distributed between allocation and freeing in different modes. Besides clearing memory, each mmap() call takes some time. For example, 1024 calls for 1 TB may take ~300 ms. The time of one call mapping N hugepages is O(N), because inside the kernel hugepages are allocated ony by one. Syscall overhead is negligeable even for one page. Hence, it does not make sense to reduce the number of mmap() calls, which would essentially move the loop over pages into the kernel. [1]: http://inbox.dpdk.org/dev/20211011085644.2716490-3-dkozlyuk@nvidia.com/ Dmitry Kozlyuk (6): doc: add hugepage mapping details app/test: add allocator performance benchmark mem: add dirty malloc element support eal: refactor --huge-unlink storage eal/linux: allow hugepage file reuse eal: extend --huge-unlink for hugepage file reuse app/test/meson.build | 2 + app/test/test_eal_flags.c | 25 +++ app/test/test_malloc_perf.c | 174 ++++++++++++++++++ doc/guides/linux_gsg/linux_eal_parameters.rst | 24 ++- .../prog_guide/env_abstraction_layer.rst | 107 ++++++++++- doc/guides/rel_notes/release_22_03.rst | 7 + lib/eal/common/eal_common_options.c | 48 ++++- lib/eal/common/eal_internal_cfg.h | 10 +- lib/eal/common/malloc_elem.c | 22 ++- lib/eal/common/malloc_elem.h | 11 +- lib/eal/common/malloc_heap.c | 18 +- lib/eal/common/rte_malloc.c | 21 ++- lib/eal/include/rte_memory.h | 8 +- lib/eal/linux/eal.c | 3 +- lib/eal/linux/eal_hugepage_info.c | 118 +++++++++--- lib/eal/linux/eal_memalloc.c | 173 ++++++++++------- lib/eal/linux/eal_memory.c | 2 +- 17 files changed, 644 insertions(+), 129 deletions(-) create mode 100644 app/test/test_malloc_perf.c