From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 150D4A0C4C; Tue, 21 Sep 2021 10:17:03 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id A946F410E4; Tue, 21 Sep 2021 10:17:00 +0200 (CEST) Received: from AZHDRRW-EX01.nvidia.com (azhdrrw-ex01.nvidia.com [20.51.104.162]) by mails.dpdk.org (Postfix) with ESMTP id 9D23940F35 for ; Tue, 21 Sep 2021 10:16:58 +0200 (CEST) Received: from NAM10-DM6-obe.outbound.protection.outlook.com (104.47.58.100) by mxs.oss.nvidia.com (10.13.234.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.858.15; Tue, 21 Sep 2021 01:16:57 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=F5P8cbr1kTr/1fzGbU06krRMIpyaCZIVzL0dry3NpKqe1Nw9HTnvziydxJSAHq9s9q4uk6ZaJp6pIXnDA//uI+NB8S55oYGbKU+z86pC30Ac3yOG2GngjP0ovaQJnBMgpWzlbIZN6sPukH+YD/UC9ltePy1Sy+kO1bnIKb7n6BQG8la5yFShlM/pca7WktMAlVQTueIYcdxTRiRhlsnWinBmXryE0Hl9QPmjlIQQVBTH4wHZTOVaW7cA3ttXxYGw+J9BS24sIAfZgzwKSGNlDXOetTwmsuCzeANvyybHKC8ysYwoG839tzjPoHL4n2dhvasM9l0qY9ei17fk7j+eCw== 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; bh=R6RwgOdgGmCeTcQORVoYSTeWG7nnDo2E4sDsgoH34x0=; b=kHXkAQiiJPBBSMiLOxcvYEtyrWV8Yb4LMbMizycAGYsz/uLjRv1nncLxuMmuuGWIjQRH4+3hSAOQZv8CRc7TyyZL7I0eHyxeG8wFum3Iu5AYr8WQOa5283H1yBUMJjo//kB/FLV1CriZ6x1IiTfOe7e920StNgNeoHlAyJ5czyQWTCmeSXFuUfdESLCKJox2I146GktVhQvY4Lp6AHvy8hv1EkvgCL8S6CMauDM0OdxX5fEU3jEA6IjQJlpzjKTcGLTsGPvjPHKkb59zw5c074W05cb3wSPZmyQRYbIjpSdDv8WtC3pn6OOh0t32xHVRxEoR9Fk0Oj9M8mgPZ3LsOg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.112.35) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=quarantine sp=none 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=R6RwgOdgGmCeTcQORVoYSTeWG7nnDo2E4sDsgoH34x0=; b=hB0uZYlNeeTR9UIO7wun28xbXnVl5sixRMzswzUrKMnadKSaTzN98ysID7DbCKoyOPKdxR5bYBudUJiFnhpxAnB9BLnno+7tYuKQCdgdqTfQ7mS7ZjIxkyjW++aCwDr1FZrKI2v8J7eOAPJhbr886+1LCEBK6Dy/H+AO52OyFGUmLxtj1Fszo2XFFdda9RQs51Yq2yAIigfU5+pjTo6v5j3mTgJ9tAQidd3KKe7Tt0pu9VETU6YtEWMKzKDOI7TQfBWHaEuWGXqJ2agupO6qyQvDFl6rr3Rx75P9LLyG+P8ggEzVJvGSXmdX5qHckk6IG98JxEFu9cx7zZe0Fhx3kQ== Received: from DM5PR20CA0027.namprd20.prod.outlook.com (2603:10b6:3:13d::13) by PH0PR12MB5452.namprd12.prod.outlook.com (2603:10b6:510:d7::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14; Tue, 21 Sep 2021 08:16:56 +0000 Received: from DM6NAM11FT004.eop-nam11.prod.protection.outlook.com (2603:10b6:3:13d:cafe::a4) by DM5PR20CA0027.outlook.office365.com (2603:10b6:3:13d::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14 via Frontend Transport; Tue, 21 Sep 2021 08:16:55 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.112.35) smtp.mailfrom=nvidia.com; intel.com; dkim=none (message not signed) header.d=none;intel.com; dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.112.35 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.112.35; helo=mail.nvidia.com; Received: from mail.nvidia.com (216.228.112.35) by DM6NAM11FT004.mail.protection.outlook.com (10.13.172.217) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.4523.14 via Frontend Transport; Tue, 21 Sep 2021 08:16:55 +0000 Received: from DRHQMAIL107.nvidia.com (10.27.9.16) by HQMAIL111.nvidia.com (172.20.187.18) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Tue, 21 Sep 2021 08:16:55 +0000 Received: from nvidia.com (172.20.187.5) by DRHQMAIL107.nvidia.com (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Tue, 21 Sep 2021 08:16:53 +0000 From: To: CC: Anatoly Burakov , Dmitry Kozlyuk , John Levon , "Viacheslav Ovsiienko" Date: Tue, 21 Sep 2021 11:16:30 +0300 Message-ID: <20210921081632.858873-2-dkozlyuk@nvidia.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210921081632.858873-1-dkozlyuk@nvidia.com> References: <20210920125302.820273-1-dkozlyuk@nvidia.com> <20210921081632.858873-1-dkozlyuk@nvidia.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [172.20.187.5] X-ClientProxiedBy: HQMAIL111.nvidia.com (172.20.187.18) To DRHQMAIL107.nvidia.com (10.27.9.16) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5adebb96-078a-45e7-9d7d-08d97cd82cff X-MS-TrafficTypeDiagnostic: PH0PR12MB5452: X-Microsoft-Antispam-PRVS: X-MS-Exchange-Transport-Forked: True X-MS-Oob-TLC-OOBClassifiers: OLM:5236; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gKD3DUjQvoPplgSWqGh8IYUNX2YeTXN8QUXRYyvKLwcUXCCErnw1IIbAnhLWrybBfU1ITpe3XqJKrCmKRrXwlAKrmHFOEKjQcmGmxSRcQg6QZoMgpdOG7P3IyG76HhRWyTOz5ezmZnvwIKY5nAkivCwSfbu0wRBRsVhe8RZ/jrVs3ycLLmC6ZqA/xvjaS6dtM28TBfk2PX/2WNaqlWxc+lsZ7F5CO+1dMFNCyGAiOwCesM83jor40KFy2dQhGWJennwN+QuOwLnzKknheNxh9nYKXBvRtT8iSd19Z4Qsnq2Mf2T/q507T6eR0i3O23CKSHSwrCvLDUcyQ8hvYBkGu9sHufpFGw88JOSd3rypxi+YmUlLKtgKDnLAwrRs0LYAGSKm970BWcZin8saobt/3qdlKPZ4dB10LWKSefdbkxOljSr4UQJm+dASzTrmqAiZOAeUdjmmIaQ7A4QEklru+UbPuj9zq6qY+6m0bfPW0lvTFaxQ5WDvqRPwFeCtozP2anewnomK3GaRITPYTUJw5hWJme0FiSN9IC41N0/elG5CNjQTP0IkAXipjSxCbyPyIuR+rZythd7TcnaUsWgHRmzNe4RwHjpGaaeGwYwv48sjAi0gmwHPCJ9RSHN3jO8kl+Pf4XBQUHfbxZnexhYa80UbE3l5ZD2aBpGEfDKvTnWFM4k4nronfnL/+rb+daKr24F6QAjCSGBd7yKE9hBqTw== X-Forefront-Antispam-Report: CIP:216.228.112.35; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:schybrid04.nvidia.com; CAT:NONE; SFS:(4636009)(46966006)(36840700001)(26005)(508600001)(107886003)(6666004)(55016002)(7696005)(336012)(7636003)(426003)(6916009)(186003)(86362001)(47076005)(83380400001)(36860700001)(316002)(1076003)(16526019)(70206006)(70586007)(2616005)(6286002)(356005)(2906002)(2876002)(36756003)(5660300002)(8676002)(54906003)(4326008)(36906005)(82310400003)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2021 08:16:55.8286 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5adebb96-078a-45e7-9d7d-08d97cd82cff X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.112.35]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT004.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB5452 Subject: [dpdk-dev] [PATCH v5 1/3] eal/linux: make hugetlbfs analysis reusable 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 Sender: "dev" From: Dmitry Kozlyuk get_hugepage_dir() searched for a hugetlbfs mount with a given page size using handcraft parsing of /proc/mounts and mixing traversal logic with selecting the needed entry. Separate code to enumerate hugetlbfs mounts to eal_hugepage_mount_walk() taking a callback that can inspect already parsed entries. Use mntent(3) API for parsing. This allows to reuse enumeration logic in subsequent patches. Signed-off-by: Dmitry Kozlyuk Reviewed-by: Viacheslav Ovsiienko --- lib/eal/linux/eal_hugepage_info.c | 153 +++++++++++++++++++----------- lib/eal/linux/eal_hugepage_info.h | 39 ++++++++ 2 files changed, 135 insertions(+), 57 deletions(-) create mode 100644 lib/eal/linux/eal_hugepage_info.h diff --git a/lib/eal/linux/eal_hugepage_info.c b/lib/eal/linux/eal_hugepage_info.c index d97792cade..193282e779 100644 --- a/lib/eal/linux/eal_hugepage_info.c +++ b/lib/eal/linux/eal_hugepage_info.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -34,6 +35,7 @@ #include "eal_private.h" #include "eal_internal_cfg.h" #include "eal_hugepages.h" +#include "eal_hugepage_info.h" #include "eal_filesystem.h" static const char sys_dir_path[] = "/sys/kernel/mm/hugepages"; @@ -195,73 +197,110 @@ get_default_hp_size(void) return size; } -static int -get_hugepage_dir(uint64_t hugepage_sz, char *hugedir, int len) +int +eal_hugepage_mount_walk(eal_hugepage_mount_walk_cb *cb, void *cb_arg) { - enum proc_mount_fieldnames { - DEVICE = 0, - MOUNTPT, - FSTYPE, - OPTIONS, - _FIELDNAME_MAX - }; - static uint64_t default_size = 0; - const char proc_mounts[] = "/proc/mounts"; - const char hugetlbfs_str[] = "hugetlbfs"; - const size_t htlbfs_str_len = sizeof(hugetlbfs_str) - 1; - const char pagesize_opt[] = "pagesize="; - const size_t pagesize_opt_len = sizeof(pagesize_opt) - 1; - const char split_tok = ' '; - char *splitstr[_FIELDNAME_MAX]; - char buf[BUFSIZ]; - int retval = -1; - const struct internal_config *internal_conf = - eal_get_internal_configuration(); - - FILE *fd = fopen(proc_mounts, "r"); - if (fd == NULL) - rte_panic("Cannot open %s\n", proc_mounts); + static const char PATH[] = "/proc/mounts"; + static const char OPTION[] = "pagesize"; + + static uint64_t default_size; + + FILE *f = NULL; + struct mntent mntent; + char strings[PATH_MAX]; + char *hugepage_sz_str; + uint64_t hugepage_sz; + bool stopped = false; + int ret = -1; + + f = setmntent(PATH, "r"); + if (f == NULL) { + RTE_LOG(ERR, EAL, "%s(): setmntent(%s): %s\n", + __func__, PATH, strerror(errno)); + goto exit; + } if (default_size == 0) default_size = get_default_hp_size(); - while (fgets(buf, sizeof(buf), fd)){ - if (rte_strsplit(buf, sizeof(buf), splitstr, _FIELDNAME_MAX, - split_tok) != _FIELDNAME_MAX) { - RTE_LOG(ERR, EAL, "Error parsing %s\n", proc_mounts); - break; /* return NULL */ + ret = 0; + while (getmntent_r(f, &mntent, strings, sizeof(strings)) != NULL) { + if (strcmp(mntent.mnt_type, "hugetlbfs") != 0) + continue; + + hugepage_sz_str = hasmntopt(&mntent, OPTION); + if (hugepage_sz_str != NULL) { + hugepage_sz_str += strlen(OPTION) + 1; /* +1 for '=' */ + hugepage_sz = rte_str_to_size(hugepage_sz_str); + if (hugepage_sz == 0) { + RTE_LOG(DEBUG, EAL, "Cannot parse hugepage size from '%s' for %s\n", + mntent.mnt_opts, mntent.mnt_dir); + continue; + } + } else { + RTE_LOG(DEBUG, EAL, "Hugepage filesystem at %s without %s option\n", + mntent.mnt_dir, OPTION); + hugepage_sz = default_size; } - /* we have a specified --huge-dir option, only examine that dir */ - if (internal_conf->hugepage_dir != NULL && - strcmp(splitstr[MOUNTPT], internal_conf->hugepage_dir) != 0) - continue; + if (cb(mntent.mnt_dir, hugepage_sz, cb_arg) != 0) { + stopped = true; + break; + } + } - if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){ - const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt); + if (ferror(f) || (!stopped && !feof(f))) { + RTE_LOG(ERR, EAL, "%s(): getmntent_r(): %s\n", + __func__, strerror(errno)); + ret = -1; + goto exit; + } - /* if no explicit page size, the default page size is compared */ - if (pagesz_str == NULL){ - if (hugepage_sz == default_size){ - strlcpy(hugedir, splitstr[MOUNTPT], len); - retval = 0; - break; - } - } - /* there is an explicit page size, so check it */ - else { - uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]); - if (pagesz == hugepage_sz) { - strlcpy(hugedir, splitstr[MOUNTPT], len); - retval = 0; - break; - } - } - } /* end if strncmp hugetlbfs */ - } /* end while fgets */ +exit: + if (f != NULL) + endmntent(f); + return ret; +} - fclose(fd); - return retval; +struct match_hugepage_mount_arg { + uint64_t hugepage_sz; + char *hugedir; + int hugedir_len; + bool done; +}; + +static int +match_hugepage_mount(const char *path, uint64_t hugepage_sz, void *cb_arg) +{ + const struct internal_config *internal_conf = + eal_get_internal_configuration(); + struct match_hugepage_mount_arg *arg = cb_arg; + + /* we have a specified --huge-dir option, only examine that dir */ + if (internal_conf->hugepage_dir != NULL && + strcmp(path, internal_conf->hugepage_dir) != 0) + return 0; + + if (hugepage_sz == arg->hugepage_sz) { + strlcpy(arg->hugedir, path, arg->hugedir_len); + arg->done = true; + return 1; + } + + return 0; +} + +static int +get_hugepage_dir(uint64_t hugepage_sz, char *hugedir, int len) +{ + struct match_hugepage_mount_arg arg = { + .hugepage_sz = hugepage_sz, + .hugedir = hugedir, + .hugedir_len = len, + .done = false, + }; + int ret = eal_hugepage_mount_walk(match_hugepage_mount, &arg); + return ret == 0 && arg.done ? 0 : -1; } /* diff --git a/lib/eal/linux/eal_hugepage_info.h b/lib/eal/linux/eal_hugepage_info.h new file mode 100644 index 0000000000..c7efa37c66 --- /dev/null +++ b/lib/eal/linux/eal_hugepage_info.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2021 NVIDIA CORPORATION & AFFILIATES. + */ + +#ifndef _EAL_HUGEPAGE_INFO_ +#define _EAL_HUGEPAGE_INFO_ + +#include + +/** + * Function called for each hugetlbfs mount point. + * + * @param path + * Mount point directory. + * @param hugepage_sz + * Hugepage size for the mount or default system hugepage size. + * @param arg + * User data. + * + * @return + * 0 to continue walking, 1 to stop. + */ +typedef int (eal_hugepage_mount_walk_cb)(const char *path, uint64_t hugepage_sz, + void *arg); + +/** + * Enumerate hugetlbfs mount points. + * + * @param cb + * Function called for each mount point. + * @param cb_arg + * User data passed to the callback. + * + * @return + * 0 on success, negative on failure. + */ +int eal_hugepage_mount_walk(eal_hugepage_mount_walk_cb *cb, void *cb_arg); + +#endif /* _EAL_HUGEPAGE_INFO_ */ -- 2.25.1