From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 118C5A0A0C;
	Mon,  5 Jul 2021 13:07:11 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 96FE84068C;
	Mon,  5 Jul 2021 13:07:10 +0200 (CEST)
Received: from NAM10-BN7-obe.outbound.protection.outlook.com
 (mail-bn7nam10on2068.outbound.protection.outlook.com [40.107.92.68])
 by mails.dpdk.org (Postfix) with ESMTP id DAC564003C;
 Mon,  5 Jul 2021 13:07:08 +0200 (CEST)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=IObdOtZyviIiEDhXm3qIrVjSCGibIPNfRE6hzXFk+9+pFb0+gcZHEbVa4dl2+E+dqnkzt3ZHGvXz+2jwVLtwr78xbevPoJvo0mf3Sr7zUmX3elq41t/9DUgedgi6YRJ6aRQcckN7QmUnOonEuOtZ4gH7S5BA/ByF05OErPf5yHh3NHLXIu+bVx+kiKJ+1OHP1q5I7zvnv1oJvfSPcr/fO8Uh4CYtMN4Zg859UfoBwk1M6XNFvEbHVcJVCIdB6C55XlUSzskbQoL8cYboo0JOTpM2R3oWWEHDr5C989nNlIWr9DIYrOhnnAFLLJuIR0QryW9XKa81nHEfQST12HfobQ==
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=BXHhrhEaFOzr71U4ILT8FYGESq3P0ouvb9mi9cXkdEc=;
 b=EUlMkoBatbCK4uHqxkXLkYkVXGaeICCWdF7BXS+W15TBYfPaAZy23O9UE181SPAlWHOoaTxfOKqAjg8trx3n5pujlyLKyj9qfI0cDeHUY1y1XPDQ56Jac9yUXaTCSUvqrQ64G8yxEsQw0d2E+vIcS63r5Jm9T3pnKBMmf40EUf18gtbV/Hb89BfgnNgym7HGSI0vnf6Ttq53v0mz+qT0yKAlvJJlVGsi2eU4YsEKeE0GG+5MZbeQQrUHBw++8V1a4KbMI9+Kk6UKBHCsembpoyNCDdcnaIBszqmpRR8I5GeeNC25f2cxZuI5xyS2kp6leizHJ15kMzTWhKC1Kf95iA==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
 smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com;
 dkim=pass header.d=nvidia.com; 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=BXHhrhEaFOzr71U4ILT8FYGESq3P0ouvb9mi9cXkdEc=;
 b=MJf0z+/79YyKCU8mgM8RA1IwmU8FfCw5ADsOu4WuevO9uqJkWPezRFUSCfzpll26vgk31HfqGLYCnSw5fLUEdL1kmBYW6UmjOY4KH4DTcUsEwaPKQ2JPrAjUlr4K67IGDzYmSeemv0Qna0Ht/1AZIOHcy/gbgHmWqWs7JyqQxxL9QexhCXNis1j1IMcxkWi31otsuoixRy8o+QC7hrhgsXenk8NMC37ccz/kELtf96jeVRnpUgm6wMqGHzKex8nYXbu6bNBfH5FDoZUAhTKOSD3emNl4QsByYUyxIorh6GONIzT9Kk+nl2DbRCcjPQ5WnaAFIFYVG3Zhwtmjw9bZbg==
Received: from DM4PR12MB5373.namprd12.prod.outlook.com (2603:10b6:5:39a::17)
 by DM8PR12MB5493.namprd12.prod.outlook.com (2603:10b6:8:3d::10) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.32; Mon, 5 Jul
 2021 11:07:07 +0000
Received: from DM4PR12MB5373.namprd12.prod.outlook.com
 ([fe80::ac75:9b9a:a03f:1676]) by DM4PR12MB5373.namprd12.prod.outlook.com
 ([fe80::ac75:9b9a:a03f:1676%3]) with mapi id 15.20.4287.033; Mon, 5 Jul 2021
 11:07:07 +0000
From: "Xueming(Steven) Li" <xuemingl@nvidia.com>
To: Andrew Rybchenko <andrew.rybchenko@oktetlabs.ru>, Matan Azrad
 <matan@nvidia.com>, Shahaf Shuler <shahafs@nvidia.com>, Slava Ovsiienko
 <viacheslavo@nvidia.com>, NBU-Contact-Thomas Monjalon <thomas@monjalon.net>,
 Ferruh Yigit <ferruh.yigit@intel.com>
CC: "dev@dpdk.org" <dev@dpdk.org>, Viacheslav Galaktionov
 <viacheslav.galaktionov@oktetlabs.ru>, "stable@dpdk.org" <stable@dpdk.org>,
 "Xueming(Steven) Li" <xuemingl@nvidia.com>
Thread-Topic: [PATCH v2] ethdev: keep count of allocated and used representor
 ranges
Thread-Index: AQHXcYT3UIh+/PcEtU24vWRhd97KFKs0OJJw
Date: Mon, 5 Jul 2021 11:07:07 +0000
Message-ID: <DM4PR12MB53733C9408ABE6E3DB46B930A11C9@DM4PR12MB5373.namprd12.prod.outlook.com>
References: <20210702142322.821302-1-andrew.rybchenko@oktetlabs.ru>
 <20210705100252.1840901-1-andrew.rybchenko@oktetlabs.ru>
In-Reply-To: <20210705100252.1840901-1-andrew.rybchenko@oktetlabs.ru>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
authentication-results: oktetlabs.ru; dkim=none (message not signed)
 header.d=none;oktetlabs.ru; dmarc=none action=none header.from=nvidia.com;
x-originating-ip: [180.162.54.71]
x-ms-publictraffictype: Email
x-ms-office365-filtering-correlation-id: d49bb535-52de-41b3-a7d3-08d93fa50760
x-ms-traffictypediagnostic: DM8PR12MB5493:
x-ld-processed: 43083d15-7273-40c1-b7db-39efd9ccc17a,ExtAddr
x-ms-exchange-transport-forked: True
x-microsoft-antispam-prvs: <DM8PR12MB5493C894136DF32D225EC848A11C9@DM8PR12MB5493.namprd12.prod.outlook.com>
x-ms-oob-tlc-oobclassifiers: OLM:4941;
x-ms-exchange-senderadcheck: 1
x-microsoft-antispam: BCL:0;
x-microsoft-antispam-message-info: d3EHMcrQmNkkE50EMwHczy7BgN+AS0MedCo1+5g7Dgj//Eys0V1UyU8kDgqaPwXCs1pNVFHvIRjo805FA3KpYKEKj0ctOa/cH03Py+UdVOhaY7YB/BWNBuV4M7sVK4XhDnYPzHE6y8Zm45A6gFg8kY8yZbbwQFx7QTnzzIUFkdL0ra3+AOvtD3vkTnV4egWvQ868EvBWp27aG7ivsner+nmYCItHqkgvYekYI+hhpLZ6rQjX5irF5uF6Ncmw2umu0Fug8PIQhYGRpr9O7XeJMr3MxS2mlJri/RjJIRCDyt3dsp0lpnlLaEyhI9lTp5vlaE3CcN69AcF4ewEaWZswdGYDZ6pT1ShqRj+2smydRL+kE/bTrLnetqxFr1SCujGs0uIPSxSgzdBl29pMzDd/bxjbB6E/0K3nWYHvkFjIgWfVTJxc6keXaAQQOve3kj05EWCmg9156xgf6cMMuQAT+rSed8dX5jkqOB2OLbb2paA9FT2GAVEmeTc8H2e+Yo/oyBdVUcD5jYxYx57SzafW2DRmpSxX334+/ShyYqiCjjXhsQ189a5DtRLNQtGRfOkY7ZwfPhf6OB4H9Z1s7EemEntGDLgVqvPPtEH5WLkHulm175jpsOEE2Bn8CZXvik0jcV9gN+A5X+r7o572WYAcUA==
x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
 IPV:NLI; SFV:NSPM; H:DM4PR12MB5373.namprd12.prod.outlook.com; PTR:; CAT:NONE;
 SFS:(4636009)(39860400002)(396003)(136003)(346002)(366004)(376002)(478600001)(8676002)(66476007)(186003)(2906002)(55016002)(9686003)(5660300002)(83380400001)(4326008)(26005)(66946007)(122000001)(66556008)(86362001)(8936002)(71200400001)(7696005)(33656002)(76116006)(53546011)(54906003)(110136005)(6506007)(107886003)(64756008)(316002)(38100700002)(66446008)(52536014);
 DIR:OUT; SFP:1101; 
x-ms-exchange-antispam-messagedata-chunkcount: 1
x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?cai+aMgXLuqsqPN3RO3EcOkj9KyzlzHRV2S5UBCsZbfGWnbvkmgBlsxIyI5O?=
 =?us-ascii?Q?zqmfcQLvhFR0UKq+/iqNQW9Yti/c6jVJFGqzreF6kgfWCH+i3kqWlv0xOt97?=
 =?us-ascii?Q?O2YoMTqYIe9IchC2m6i/klaN8hi4AzBWOgh5zR5XPesU1mtSk8WSENXmnr+9?=
 =?us-ascii?Q?mBbdX47rnPOTg5ECf2i0EaGCu5YdXeCitKeDiOmARiOFc+aLUhXgYVNvACY1?=
 =?us-ascii?Q?V9gx/J3Mob90my+zMGssOttyVIn7bIsS5hH5zNLJTZKKKivHGOerOQIwnODE?=
 =?us-ascii?Q?p9fmxXiOMDmPds3THz3k8tCbDArChI111yrahGDvL6sFUwiNXecoK5VT96XL?=
 =?us-ascii?Q?+csEv6Rf9AB1gq2EhIRbXqq2CJUJwvxQiTKihhcgkYkSWm040kCE0ADxiIFI?=
 =?us-ascii?Q?iSunHZAOkdfYWmYXa6xF/nfb69zV59p2ngoaVlqmta0joNGrOIP6Flvy/S/m?=
 =?us-ascii?Q?qlYRs1CbeEIc5MRHrQCOUbigjbdvBCWVxc5pDIzXjzKJXOqNcge1nFkqpWHB?=
 =?us-ascii?Q?/Njspz5vuf3WEr9fhaxM8X+s+vZMTQl0p7l3tk4uJ+E6kvTLeNDC3xJRj+DL?=
 =?us-ascii?Q?DUmfObXOHnAMlWR25KZVylt7/E2Vy1TLTgsAxpvlATrR88Z9jX8jSwlVIUCQ?=
 =?us-ascii?Q?qgJOmE2SJjAXIYMgQ484UMIVZcUmRZ6xPp7JB60PJZAjQh8SzI7z3sUIvZ5w?=
 =?us-ascii?Q?+BmjsEKaGuKVE3U/T0WRuq7MB2Co6o4zhGmvgv7s6wKJ5se/ZDr4LO1O8jL5?=
 =?us-ascii?Q?OROEqaPKbUSUym5k2vo+W9P8rxIiqFPlzfZXsmNSXWl0TvWCCOc//5NNXziw?=
 =?us-ascii?Q?vuxLAARSNJM3PHkVx3wsViOfhi9yUOP0xiEJvLzTvIjCk30DR6aoIT9b5G7E?=
 =?us-ascii?Q?2yuIHDDr67HSONmOmXePOjk4vK4qT9L0hGt7ls5Loo4CskCojq83ni6unNt/?=
 =?us-ascii?Q?sdZNTv8ATmXxvdBONV9JntZWwYA69Rd8sNGYV4ISjUzem0eqOMt/u+xaQpf6?=
 =?us-ascii?Q?A72WZvn18OksKeZEySuedEv17lUIH2exDSbmIDssziUdx3E1h68b1dGLehhr?=
 =?us-ascii?Q?bXL9je0OrD/h6t3jLVZoPvbndXsP761iNP08BLiCb5avupAsLF4orKAGrmMA?=
 =?us-ascii?Q?MhgBDQ7QlU0Jr8AVOQLmO73Af5qEcA9G6miWr43i/yToVsQhdCcZ7WWKdYeJ?=
 =?us-ascii?Q?jafGO0Y4VDRPGMQTBVhVKVnOD6znBC26cXwXKhTZrDxffHypYxXRkZyH/o5S?=
 =?us-ascii?Q?vvaFVrr2lzPr8q3b0Fb44jUe1iRQkoL/BFaKeE3KGjSui7r3zz7D1FOxt4nH?=
 =?us-ascii?Q?+lQvsw619SJBCINxscvnL7yk?=
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
X-OriginatorOrg: Nvidia.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-AuthSource: DM4PR12MB5373.namprd12.prod.outlook.com
X-MS-Exchange-CrossTenant-Network-Message-Id: d49bb535-52de-41b3-a7d3-08d93fa50760
X-MS-Exchange-CrossTenant-originalarrivaltime: 05 Jul 2021 11:07:07.5076 (UTC)
X-MS-Exchange-CrossTenant-fromentityheader: Hosted
X-MS-Exchange-CrossTenant-id: 43083d15-7273-40c1-b7db-39efd9ccc17a
X-MS-Exchange-CrossTenant-mailboxtype: HOSTED
X-MS-Exchange-CrossTenant-userprincipalname: zVsZuTDiciv1ZxN0t4rw0FVfZ1xypTi6fmX3BhstHNw4YOxOw8IOJeOPK6ke8kev2cclnNYxHmRbtRzRLpfCzQ==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM8PR12MB5493
Subject: Re: [dpdk-dev] [PATCH v2] ethdev: keep count of allocated and used
 representor ranges
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>



> -----Original Message-----
> From: Andrew Rybchenko <andrew.rybchenko@oktetlabs.ru>
> Sent: Monday, July 5, 2021 6:03 PM
> To: Matan Azrad <matan@nvidia.com>; Shahaf Shuler <shahafs@nvidia.com>; S=
lava Ovsiienko <viacheslavo@nvidia.com>; NBU-
> Contact-Thomas Monjalon <thomas@monjalon.net>; Ferruh Yigit <ferruh.yigit=
@intel.com>; Xueming(Steven) Li
> <xuemingl@nvidia.com>
> Cc: dev@dpdk.org; Viacheslav Galaktionov <viacheslav.galaktionov@oktetlab=
s.ru>; stable@dpdk.org
> Subject: [PATCH v2] ethdev: keep count of allocated and used representor =
ranges
>=20
> From: Viacheslav Galaktionov <viacheslav.galaktionov@oktetlabs.ru>
>=20
> In its current state, the API can overflow the user-passed buffer if a ne=
w representor range appears between function calls.
>=20
> In order to solve this problem, augment the representor info structure wi=
th the numbers of allocated and initialized ranges. This way
> the users of this structure can be sure they will not overrun the buffer.
>=20
> Fixes: 85e1588ca72f ("ethdev: add API to get representor info")
> Cc: stable@dpdk.org
>=20
> Signed-off-by: Viacheslav Galaktionov <viacheslav.galaktionov@oktetlabs.r=
u>
> Signed-off-by: Andrew Rybchenko <andrew.rybchenko@oktetlabs.ru>
> ---
> v2:
>  - fixed a potential null pointer dereference
>=20
>  drivers/net/mlx5/mlx5_ethdev.c | 15 +++++++++++++++
>  lib/ethdev/rte_ethdev.c        |  6 ++++--
>  lib/ethdev/rte_ethdev.h        |  9 ++++++++-
>  3 files changed, 27 insertions(+), 3 deletions(-)
>=20
> diff --git a/drivers/net/mlx5/mlx5_ethdev.c b/drivers/net/mlx5/mlx5_ethde=
v.c index 90baee5aa4..c32eeefd88 100644
> --- a/drivers/net/mlx5/mlx5_ethdev.c
> +++ b/drivers/net/mlx5/mlx5_ethdev.c
> @@ -413,9 +413,15 @@ mlx5_representor_info_get(struct rte_eth_dev *dev,
>  	int n_type =3D 4; /* Representor types, VF, HPF@VF, SF and HPF@SF. */
>  	int n_pf =3D 2; /* Number of PFs. */
>  	int i =3D 0, pf;
> +	int n_entries;
>=20
>  	if (info =3D=3D NULL)
>  		goto out;
> +
> +	n_entries =3D n_type * n_pf;
> +	if ((uint32_t)n_entries > info->nb_ranges_alloc)
> +		n_entries =3D info->nb_ranges_alloc;
> +
>  	info->controller =3D 0;
>  	info->pf =3D priv->pf_bond >=3D 0 ? priv->pf_bond : 0;
>  	for (pf =3D 0; pf < n_pf; ++pf) {
> @@ -431,6 +437,8 @@ mlx5_representor_info_get(struct rte_eth_dev *dev,
>  		snprintf(info->ranges[i].name,
>  			 sizeof(info->ranges[i].name), "pf%dvf", pf);
>  		i++;
> +		if (i =3D=3D n_entries)
> +			break;
>  		/* HPF range of VF type. */
>  		info->ranges[i].type =3D RTE_ETH_REPRESENTOR_VF;
>  		info->ranges[i].controller =3D 0;
> @@ -443,6 +451,8 @@ mlx5_representor_info_get(struct rte_eth_dev *dev,
>  		snprintf(info->ranges[i].name,
>  			 sizeof(info->ranges[i].name), "pf%dvf", pf);
>  		i++;
> +		if (i =3D=3D n_entries)
> +			break;
>  		/* SF range. */
>  		info->ranges[i].type =3D RTE_ETH_REPRESENTOR_SF;
>  		info->ranges[i].controller =3D 0;
> @@ -455,6 +465,8 @@ mlx5_representor_info_get(struct rte_eth_dev *dev,
>  		snprintf(info->ranges[i].name,
>  			 sizeof(info->ranges[i].name), "pf%dsf", pf);
>  		i++;
> +		if (i =3D=3D n_entries)
> +			break;
>  		/* HPF range of SF type. */
>  		info->ranges[i].type =3D RTE_ETH_REPRESENTOR_SF;
>  		info->ranges[i].controller =3D 0;
> @@ -467,7 +479,10 @@ mlx5_representor_info_get(struct rte_eth_dev *dev,
>  		snprintf(info->ranges[i].name,
>  			 sizeof(info->ranges[i].name), "pf%dsf", pf);
>  		i++;
> +		if (i =3D=3D n_entries)
> +			break;
>  	}
> +	info->nb_ranges =3D i;
>  out:
>  	return n_type * n_pf;
>  }
> diff --git a/lib/ethdev/rte_ethdev.c b/lib/ethdev/rte_ethdev.c index c607=
eabb5b..30abc93b18 100644
> --- a/lib/ethdev/rte_ethdev.c
> +++ b/lib/ethdev/rte_ethdev.c
> @@ -5987,7 +5987,8 @@ rte_eth_representor_id_get(const struct rte_eth_dev=
 *ethdev,
>  			   int controller, int pf, int representor_port,
>  			   uint16_t *repr_id)
>  {
> -	int ret, n, i, count;
> +	int ret, n, count;
> +	uint32_t i;
>  	struct rte_eth_representor_info *info =3D NULL;
>  	size_t size;
>=20
> @@ -6011,6 +6012,7 @@ rte_eth_representor_id_get(const struct rte_eth_dev=
 *ethdev,
>  	info =3D calloc(1, size);
>  	if (info =3D=3D NULL)
>  		return -ENOMEM;
> +	info->nb_ranges_alloc =3D n;
>  	ret =3D rte_eth_representor_info_get(ethdev->data->port_id, info);
>  	if (ret < 0)
>  		goto out;
> @@ -6023,7 +6025,7 @@ rte_eth_representor_id_get(const struct rte_eth_dev=
 *ethdev,
>=20
>  	/* Locate representor ID. */
>  	ret =3D -ENOENT;
> -	for (i =3D 0; i < n; ++i) {
> +	for (i =3D 0; i < info->nb_ranges; ++i) {
>  		if (info->ranges[i].type !=3D type)
>  			continue;
>  		if (info->ranges[i].controller !=3D controller) diff --git a/lib/ethde=
v/rte_ethdev.h b/lib/ethdev/rte_ethdev.h index
> faf3bd901d..d2b27c351f 100644
> --- a/lib/ethdev/rte_ethdev.h
> +++ b/lib/ethdev/rte_ethdev.h
> @@ -4856,6 +4856,8 @@ struct rte_eth_representor_range {  struct rte_eth_=
representor_info {
>  	uint16_t controller; /**< Controller ID of caller device. */
>  	uint16_t pf; /**< Physical function ID of caller device. */
> +	uint32_t nb_ranges_alloc; /**< Size of the ranges array. */
> +	uint32_t nb_ranges; /**< Number of initialized ranges. */
>  	struct rte_eth_representor_range ranges[];/**< Representor ID range. */=
  };
>=20
> @@ -4871,11 +4873,16 @@ struct rte_eth_representor_info {
>   *   A pointer to a representor info structure.
>   *   NULL to return number of range entries and allocate memory
>   *   for next call to store detail.
> + *   The number of ranges that were written into this structure
> + *   will be placed into its nb_ranges field. This number cannot be
> + *   larger than the nb_ranges_alloc that by the user before calling
> + *   this function. It can be smaller than the value returned by the
> + *   function, however.
>   * @return
>   *   - (-ENOTSUP) if operation is not supported.
>   *   - (-ENODEV) if *port_id* invalid.
>   *   - (-EIO) if device is removed.
> - *   - (>=3D0) number of representor range entries supported by device.
> + *   - (>=3D0) number of available representor range entries.
>   */
>  __rte_experimental
>  int rte_eth_representor_info_get(uint16_t port_id,
> --
> 2.30.2

Reviewed-by: Xueming Li <xuemingl@nvidia.com>