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 940CCA0A0F; Mon, 5 Jul 2021 14:50:14 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D059B411D2; Mon, 5 Jul 2021 14:50:10 +0200 (CEST) Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam07on2073.outbound.protection.outlook.com [40.107.212.73]) by mails.dpdk.org (Postfix) with ESMTP id 9CC6A4003C for ; Mon, 5 Jul 2021 14:50:08 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YnNbDdH4FXtdZVTMT44xlRA4HXaJGCBsHoEfP89HCkObGBX6k0TiRrms+SGmN4wTGdBsN6zy8hpimPyWFHzzfJfYf3cR+IxEmBOZa3ky5tzPEpxjw7SBZe71MogGFJV3z6Hpyqy2CkUWOrs00DCRPlnhjPv9S9w81l82TDuRSSnvCXQKedJiOD1DLinPdU1UkJ1S5Tczce3n5fpYTHXG6wsg0POUPVDmwgyvnsMqajZm7bEnTkFraNAicObpHkw8gaIGDxRLvWFeuRzmyG0ZLvIcYVv1yw1upNeqZmbiMdqGKQGbnpn9IPcmKY6NUerV/jmLAujfaoAKEum4ylICkA== 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-SenderADCheck; bh=EzAhVRISwFsdw0J26/1ykpw8XFGEUa4Wvu8iDiW4fws=; b=TMkLa9163DaaI9PNU4Kqn62cnhCMmCH37LWfO49dSMikucA9VelmZ2ljTNqXe+kaW3ip3/KrSp41YB7WwNwl6zmpAoc8vjgFGmMIn22hfOpq2LFBHms4EkGlTb4Akb79ZiYQtcutE6t2/rTE4+QQW/a8yp9vElSF8yH4oOrNoirYnERM5kIm7+mQGu41+2tKydVoagqQyqCfWtV0+BAcEC6AYLVTg3S6c6TYqZygtZFcC9F/lo45IJ/oJQWm08O9v3PZhMYnrjiKktASl8K/lQequeqLOFrH/PRfc5y8tglZBdQiuFI0IoA9K9ACUQR4kRWMBOF33OeCHKOwCL0lXg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.112.34) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=none 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=EzAhVRISwFsdw0J26/1ykpw8XFGEUa4Wvu8iDiW4fws=; b=TdU94jfHD6TSRDC2F0Ai22XVi4uhSnnaRkCEmeqspXyedKTC001TMp+HwlBZXONtbqxkfXHw5wVSfq49wATC2c90vCHHgrYKZGjAps4yha7s/sRETNs+ZQe9O6pbPGInEeu3RfP7VPJ8ovUWn51ij1DZ3lSMEOploKHQG1cuwufoTRXBgt+WyOn/xWIakPcIbXCUi1JK+sIi6FrzSJ7Bp4BYUqbR2P+ygq51o0Km1Hy7IFstSGp5si3k7Li82In0rhTiNJAY+jtmpsJfe74zgd2wl4n086gYzp/lfqC0HjZgZBC0rdHYjj3CtVjgMKC/mjtN2G+HCqo+Sx1mpYJbDw== Received: from MWHPR15CA0033.namprd15.prod.outlook.com (2603:10b6:300:ad::19) by CY4PR12MB1608.namprd12.prod.outlook.com (2603:10b6:910:d::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.29; Mon, 5 Jul 2021 12:50:07 +0000 Received: from CO1NAM11FT059.eop-nam11.prod.protection.outlook.com (2603:10b6:300:ad:cafe::d1) by MWHPR15CA0033.outlook.office365.com (2603:10b6:300:ad::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.23 via Frontend Transport; Mon, 5 Jul 2021 12:50:07 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.112.34) 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.34 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.112.34; helo=mail.nvidia.com; Received: from mail.nvidia.com (216.228.112.34) by CO1NAM11FT059.mail.protection.outlook.com (10.13.174.160) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.4287.22 via Frontend Transport; Mon, 5 Jul 2021 12:50:07 +0000 Received: from nvidia.com (172.20.187.5) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 5 Jul 2021 12:50:05 +0000 From: Dmitry Kozlyuk To: CC: Anatoly Burakov , Viacheslav Ovsiienko Date: Mon, 5 Jul 2021 15:49:47 +0300 Message-ID: <20210705124950.902701-2-dkozlyuk@nvidia.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210705124950.902701-1-dkozlyuk@nvidia.com> References: <20210705124950.902701-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 HQMAIL107.nvidia.com (172.20.187.13) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f4dcedb1-c312-4090-4f33-08d93fb36ab9 X-MS-TrafficTypeDiagnostic: CY4PR12MB1608: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5236; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JUeIiWl+rr5fpQZQ1zLEnPv0Qb49pwzhoRPD5ySLOWvGuYkKEWl/GhhJUZey+ldkb5WBXDgZau9biZO2TqrWblj6kZydfj8b1iWAlGuF5I1BwCsDmLbDOgefNwC0HJdpZixePaoXJwVa9yjiEzpuZO74O2tWTajK4xaOdB3jw8VcfORpz7aiV8tU3cFdgpyGpPFTUZ3OB7LlX1nHZMXQB/lGVIlqHcXeKsi1/GXKiXzUrk4fFlZSjhrE3lmlZgeKSPB8llgwYPPFYrFsOLPoh77aeq9uyENkb+uRfbzXIuayYo8Xwxa6FqLeaL3zhOj5cLY3OfTCeUKEtW/+i1MpAKUtMo8kNARD+SuoW9b4j3w6lWotapU3QrFmBdM6HF1yUX8/MOI3uRZRENXq1X0rsmZpzU42MODgfkyU6Qrk05Eb7DHdMusXhdTnGjfBVEwCpMzX2tRvGzA+h0himZOpldGWozJR/w2X5nkMyVIHUhyipZllOVNs6RHzpRZKzdFHQt5GVsmKffEgyhigITbWGkyLkQzs3JQ3yZB/1TGQtKBZuHfHjmvXPyw+CFQJ+ozXB/vPjVmgLCUErvPovl6SuRSXlaF94xEhnqcNarIuZGP9igkFcEEBuLodgGmVL0OhK1lkcD2A0w5r8I5fgNOjeq6tbllyfF6wdmI8BqZvtZBOa+PsVNNx4EgHF82JnMbEGwRkfkllSJE8XPX0bmKhsQ== X-Forefront-Antispam-Report: CIP:216.228.112.34; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:schybrid03.nvidia.com; CAT:NONE; SFS:(4636009)(376002)(346002)(39860400002)(136003)(396003)(46966006)(36840700001)(6916009)(82310400003)(86362001)(7696005)(6666004)(7636003)(36756003)(83380400001)(478600001)(47076005)(82740400003)(26005)(16526019)(186003)(6286002)(36860700001)(55016002)(36906005)(8676002)(107886003)(336012)(2616005)(5660300002)(70586007)(70206006)(426003)(356005)(2906002)(8936002)(316002)(54906003)(4326008)(1076003); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Jul 2021 12:50:07.0751 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f4dcedb1-c312-4090-4f33-08d93fb36ab9 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.34]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT059.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1608 Subject: [dpdk-dev] [PATCH 21.11 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" 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..a090c0a5b5 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 *m; + char *hugepage_sz_str; + uint64_t hugepage_sz; + 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; + do { + m = getmntent(f); + if (m == NULL) + break; - /* 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) + if (strcmp(m->mnt_fsname, "hugetlbfs") != 0) continue; - if (strncmp(splitstr[FSTYPE], hugetlbfs_str, htlbfs_str_len) == 0){ - const char *pagesz_str = strstr(splitstr[OPTIONS], pagesize_opt); - - /* 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; - } + hugepage_sz_str = hasmntopt(m, 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", + m->mnt_opts, m->mnt_dir); + continue; } - } /* end if strncmp hugetlbfs */ - } /* end while fgets */ + } else { + RTE_LOG(DEBUG, EAL, "Hugepage filesystem at %s without %s option\n", + m->mnt_dir, OPTION); + hugepage_sz = default_size; + } - fclose(fd); - return retval; + if (cb(m->mnt_dir, hugepage_sz, cb_arg) != 0) + break; + } while (m != NULL); + + if (ferror(f) && !feof(f)) { + RTE_LOG(DEBUG, EAL, "%s(): getmntent(): %s\n", + __func__, strerror(errno)); + ret = -1; + goto exit; + } + +exit: + if (f != NULL) + endmntent(f); + return ret; +} + +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