From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 07F95A00E6 for ; Wed, 7 Aug 2019 10:39:51 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 531104CA9; Wed, 7 Aug 2019 10:39:50 +0200 (CEST) Received: from EUR01-VE1-obe.outbound.protection.outlook.com (mail-eopbgr140107.outbound.protection.outlook.com [40.107.14.107]) by dpdk.org (Postfix) with ESMTP id 4664734F0; Wed, 7 Aug 2019 10:39:49 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AAaVJLV7NWaEQ8IbcsjNRH4aqLEg1rbTLxPkq1/2fsRAFUpZ7jQqRbKrTCJQFfTKwl1jJX6s/5G3uetRVabtQT2dDoQIfnqamKWCRmZ8jlOdS6EYCz557iTgyN75xQqhqK3kf325eaFFPwibxzS/pFYEoBXDVAk9diNGlzsHcVzt83gcC8u/cBH9Yw/8niPUmUJDEHbjRsjImmUYQ9RSHmi43B0WzLuEIN1X2d8BZ8fVoBELpRbEALQpxyLf7/01EcHAHLgg826Db98Dju6YZCERjAUz/1vq7wIoUXKqBUbvgdRE7RFdt2ZE40cPdaFeT8FTFao0Nmn4j70L0z1Cmg== 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=YLtua57QqhF1OSP24tGjtnqZHCIYXkQIKyBIUFM7ABg=; b=FjKeAnqxGcjbYGAtfiVV56kMOTuiraKp1AMwe5p8LKdH1DPOHD12oe4cJt2ncssVBK7P+q21y0Rgad4EG/ZxW3BLA1nKxoxFk02iPG6f0ifRcax0jdpwHC2qjHEn1UiSu0iF/ztEDbTG/3vNqrULSYnHDctNQ870hMLZq3E7wXr3b45lcUY0140y62RdrPJ2vzJKim4HZH8nl/GCZD9yit60/LeZlBVvXj7E4DL2tCnaDLlXMxq6rmFQRv6R/ZRR+Sq69x3SMYDAqEiLKy+A4UyNNjjq9wrIjexKmKzLtvTReQc7RMy/YpYGsH0DDe1nDQMHikJdMsEgwGvnGZevlA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 131.228.2.17) smtp.rcpttodomain=intel.com smtp.mailfrom=nokia.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=nokia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nokia.onmicrosoft.com; s=selector1-nokia-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=YLtua57QqhF1OSP24tGjtnqZHCIYXkQIKyBIUFM7ABg=; b=NrFzIuuM53xJ3nUstH2RdL79efF+G8ceYjYXpW4a/AzhyBxv5sjX8XpYEEaixrkXeU1dUOmfg04Oak5eHs3bfEQeP0eJ2LcX9j8C4aVlobh+TgaDbd6oex77O3pqGunFEjE+eU3Z+jFdvx+Nnve61k3C19Sj11Rs0zGrDMNpGkM= Received: from AM4PR07CA0018.eurprd07.prod.outlook.com (2603:10a6:205:1::31) by DB6PR07MB3192.eurprd07.prod.outlook.com (2603:10a6:6:18::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2157.13; Wed, 7 Aug 2019 08:39:48 +0000 Received: from VE1EUR03FT027.eop-EUR03.prod.protection.outlook.com (2a01:111:f400:7e09::201) by AM4PR07CA0018.outlook.office365.com (2603:10a6:205:1::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2157.8 via Frontend Transport; Wed, 7 Aug 2019 08:39:48 +0000 Authentication-Results: spf=pass (sender IP is 131.228.2.17) smtp.mailfrom=nokia.com; intel.com; dkim=none (message not signed) header.d=none;intel.com; dmarc=pass action=none header.from=nokia.com; Received-SPF: Pass (protection.outlook.com: domain of nokia.com designates 131.228.2.17 as permitted sender) receiver=protection.outlook.com; client-ip=131.228.2.17; helo=fihe3nok0735.emea.nsn-net.net; Received: from fihe3nok0735.emea.nsn-net.net (131.228.2.17) by VE1EUR03FT027.mail.protection.outlook.com (10.152.18.154) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2052.18 via Frontend Transport; Wed, 7 Aug 2019 08:39:47 +0000 Received: from lfs-up-04.novalocal ([10.157.84.234]) by fihe3nok0735.emea.nsn-net.net (GMO) with ESMTP id x778dl7A022160; Wed, 7 Aug 2019 08:39:47 GMT Received: by lfs-up-04.novalocal (Postfix, from userid 62146483) id 1238C5BD5; Wed, 7 Aug 2019 11:39:47 +0300 (EEST) From: Julien Meunier To: Pablo de Lara , Declan Doherty CC: , Date: Wed, 7 Aug 2019 11:39:46 +0300 Message-ID: <20190807083946.39309-1-julien.meunier@nokia.com> X-Mailer: git-send-email 2.10.2 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:131.228.2.17; IPV:CAL; SCL:-1; CTRY:FI; EFV:NLI; SFV:NSPM; SFS:(10019020)(4636009)(346002)(376002)(396003)(39860400002)(136003)(2980300002)(199004)(189003)(476003)(486006)(2616005)(44832011)(126002)(53936002)(2906002)(47776003)(76130400001)(52956003)(1076003)(356004)(36756003)(50466002)(48376002)(68736007)(26826003)(186003)(26005)(51416003)(54906003)(110136005)(16586007)(42186006)(50226002)(81166006)(103686004)(81156014)(8936002)(8676002)(106002)(316002)(4326008)(6266002)(14444005)(305945005)(70586007)(86362001)(336012)(5024004)(478600001)(70206006)(5660300002); DIR:OUT; SFP:1102; SCL:1; SRVR:DB6PR07MB3192; H:fihe3nok0735.emea.nsn-net.net; FPR:; SPF:Pass; LANG:en; PTR:InfoDomainNonexistent; A:1; MX:1; MIME-Version: 1.0 Content-Type: text/plain X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 522b687b-6e3f-4621-b72e-08d71b12ce39 X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(5600148)(711020)(4605104)(4709080)(1401327)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328); SRVR:DB6PR07MB3192; X-MS-TrafficTypeDiagnostic: DB6PR07MB3192: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-Forefront-PRVS: 01221E3973 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: KSB4gDv/LzKIkpmUBxrPwehbri9MwaTuGVXrXJpj5GnifUJMf4btN5FUknEai/ZZO4B9WLUpodrCx4thTZiJm4t/5rpJI3AtcqNaOf3oGn+jr1FxaMyrCjUiHDDd+CRuLzctoKYEMa45luI2cIDgBBjGNG3LkaK65JGLgLhpsBmMgfrb/BlQgfa40sxMWBA9khztyklwOrrwIk9EoFYzkB+V+LLPhg4r9Jrn9mR7dZvHdylU6ihEaX7bDRAlygkSmqnWAYE48dkDzyWLXBWeNM+/zjVM34YE8kwCAiENLue6uDpAD0SEL4Q7un2cBbtUjLvKJUpyLoSD1mGEph2n6Yl+JeO2oc5H8maBkA4DQ5CGuAPpzKkpcgrazs22WFiTYsyawFs0FWS4nx59DYGAV+ufLgauR0iWTxk2Tjg47nU= X-OriginatorOrg: nokia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Aug 2019 08:39:47.7543 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 522b687b-6e3f-4621-b72e-08d71b12ce39 X-MS-Exchange-CrossTenant-Id: 5d471751-9675-428d-917b-70f44f9630b0 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5d471751-9675-428d-917b-70f44f9630b0; Ip=[131.228.2.17]; Helo=[fihe3nok0735.emea.nsn-net.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR07MB3192 Subject: [dpdk-dev] [PATCH] cryptodev: fix invalid dev_id after a pmd close X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 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" Each cryptodev are indexed with its dev_id in the global rte_crypto_devices variable. nb_devs is incremented / decremented each time a cryptodev is created / deleted. The goal of nb_devs is to prevent the user to get an invalid dev_id. Let's imagine DPDK has configured N cryptodevs. If the cryptodev=1 is removed at runtime, the latest cryptodev N cannot be accessible, because nb_devs=N-1. In order to prevent this kind of behavior, let's remove the check with nb_devs and iterate in all the rte_crypto_devices elements: if data is not NULL, that means a valid cryptodev is available. Fixes: d11b0f30df88 ("cryptodev: introduce API and framework for crypto devices") Cc: stable@dpdk.org Signed-off-by: Julien Meunier --- lib/librte_cryptodev/rte_cryptodev.c | 42 ++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/librte_cryptodev/rte_cryptodev.c b/lib/librte_cryptodev/rte_cryptodev.c index 43bc335..c8c7ffd 100644 --- a/lib/librte_cryptodev/rte_cryptodev.c +++ b/lib/librte_cryptodev/rte_cryptodev.c @@ -49,9 +49,7 @@ struct rte_cryptodev *rte_cryptodevs = rte_crypto_devices; static struct rte_cryptodev_global cryptodev_globals = { .devs = rte_crypto_devices, - .data = { NULL }, - .nb_devs = 0, - .max_devs = RTE_CRYPTO_MAX_DEVS + .data = { NULL } }; /* spinlock for crypto device callbacks */ @@ -512,7 +510,7 @@ rte_cryptodev_pmd_get_named_dev(const char *name) if (name == NULL) return NULL; - for (i = 0; i < cryptodev_globals.max_devs; i++) { + for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) { dev = &cryptodev_globals.devs[i]; if ((dev->attached == RTE_CRYPTODEV_ATTACHED) && @@ -523,12 +521,21 @@ rte_cryptodev_pmd_get_named_dev(const char *name) return NULL; } +static uint8_t +rte_cryptodev_is_valid_device_data(uint8_t dev_id) +{ + if (rte_crypto_devices[dev_id].data == NULL) + return 0; + + return 1; +} + unsigned int rte_cryptodev_pmd_is_valid_dev(uint8_t dev_id) { struct rte_cryptodev *dev = NULL; - if (dev_id >= cryptodev_globals.nb_devs) + if (!rte_cryptodev_is_valid_device_data(dev_id)) return 0; dev = rte_cryptodev_pmd_get_dev(dev_id); @@ -547,12 +554,15 @@ rte_cryptodev_get_dev_id(const char *name) if (name == NULL) return -1; - for (i = 0; i < cryptodev_globals.nb_devs; i++) + for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) { + if (!rte_cryptodev_is_valid_device_data(i)) + continue; if ((strcmp(cryptodev_globals.devs[i].data->name, name) == 0) && (cryptodev_globals.devs[i].attached == RTE_CRYPTODEV_ATTACHED)) return i; + } return -1; } @@ -560,7 +570,13 @@ rte_cryptodev_get_dev_id(const char *name) uint8_t rte_cryptodev_count(void) { - return cryptodev_globals.nb_devs; + uint8_t i, dev_count = 0; + + for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) + if (cryptodev_globals.devs[i].data != NULL) + dev_count++; + + return dev_count; } uint8_t @@ -568,7 +584,7 @@ rte_cryptodev_device_count_by_driver(uint8_t driver_id) { uint8_t i, dev_count = 0; - for (i = 0; i < cryptodev_globals.max_devs; i++) + for (i = 0; i < RTE_CRYPTO_MAX_DEVS; i++) if (cryptodev_globals.devs[i].driver_id == driver_id && cryptodev_globals.devs[i].attached == RTE_CRYPTODEV_ATTACHED) @@ -583,9 +599,10 @@ rte_cryptodev_devices_get(const char *driver_name, uint8_t *devices, { uint8_t i, count = 0; struct rte_cryptodev *devs = cryptodev_globals.devs; - uint8_t max_devs = cryptodev_globals.max_devs; - for (i = 0; i < max_devs && count < nb_devices; i++) { + for (i = 0; i < RTE_CRYPTO_MAX_DEVS && count < nb_devices; i++) { + if (!rte_cryptodev_is_valid_device_data(i)) + continue; if (devs[i].attached == RTE_CRYPTODEV_ATTACHED) { int cmp; @@ -736,8 +753,6 @@ rte_cryptodev_pmd_allocate(const char *name, int socket_id) TAILQ_INIT(&(cryptodev->link_intr_cbs)); cryptodev->attached = RTE_CRYPTODEV_ATTACHED; - - cryptodev_globals.nb_devs++; } return cryptodev; @@ -766,7 +781,6 @@ rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev) return ret; cryptodev->attached = RTE_CRYPTODEV_DETACHED; - cryptodev_globals.nb_devs--; return 0; } @@ -1099,7 +1113,7 @@ rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info) { struct rte_cryptodev *dev; - if (dev_id >= cryptodev_globals.nb_devs) { + if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) { CDEV_LOG_ERR("Invalid dev_id=%d", dev_id); return; } -- 2.10.2