2018-05-18 14:41:07

by Michael Grzeschik

[permalink] [raw]
Subject: [PATCH] usbip: dynamically allocate idev by nports found in sysfs

As the amount of available ports varies by the kernels build
configuration. To remove the limitation of the fixed 128 ports
we allocate the amount of idevs by using the number we get
from the kernel.

Signed-off-by: Michael Grzeschik <[email protected]>
---
tools/usb/usbip/libsrc/vhci_driver.c | 11 ++++++++---
tools/usb/usbip/libsrc/vhci_driver.h | 3 +--
2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c
index c9c81614a66ad..9a8acfc7697fa 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -266,11 +266,11 @@ int usbip_vhci_driver_open(void)
if (vhci_driver->nports <= 0) {
err("no available ports");
goto err;
- } else if (vhci_driver->nports > MAXNPORT) {
- err("port number exceeds %d", MAXNPORT);
- goto err;
}

+ vhci_driver->idev = calloc(vhci_driver->nports,
+ sizeof(struct usbip_imported_device));
+
vhci_driver->ncontrollers = get_ncontrollers();
dbg("available controllers: %d", vhci_driver->ncontrollers);

@@ -287,6 +287,9 @@ int usbip_vhci_driver_open(void)
err:
udev_device_unref(vhci_driver->hc_device);

+ if (vhci_driver->idev)
+ free(vhci_driver->idev);
+
if (vhci_driver)
free(vhci_driver);

@@ -305,6 +308,8 @@ void usbip_vhci_driver_close(void)

udev_device_unref(vhci_driver->hc_device);

+ free(vhci_driver->idev);
+
free(vhci_driver);

vhci_driver = NULL;
diff --git a/tools/usb/usbip/libsrc/vhci_driver.h b/tools/usb/usbip/libsrc/vhci_driver.h
index 418b404d51210..67dbd1551e159 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.h
+++ b/tools/usb/usbip/libsrc/vhci_driver.h
@@ -13,7 +13,6 @@

#define USBIP_VHCI_BUS_TYPE "platform"
#define USBIP_VHCI_DEVICE_NAME "vhci_hcd.0"
-#define MAXNPORT 128

enum hub_speed {
HUB_SPEED_HIGH = 0,
@@ -41,7 +40,7 @@ struct usbip_vhci_driver {

int ncontrollers;
int nports;
- struct usbip_imported_device idev[MAXNPORT];
+ struct usbip_imported_device *idev;
};


--
2.17.0



2018-05-22 16:07:34

by Shuah Khan

[permalink] [raw]
Subject: Re: [PATCH] usbip: dynamically allocate idev by nports found in sysfs

Hi Michael,

Thanks for the patch. Couple of comments below:

On 05/18/2018 08:39 AM, Michael Grzeschik wrote:
> As the amount of available ports varies by the kernels build
> configuration. To remove the limitation of the fixed 128 ports
> we allocate the amount of idevs by using the number we get
> from the kernel.
>
> Signed-off-by: Michael Grzeschik <[email protected]>
> ---
> tools/usb/usbip/libsrc/vhci_driver.c | 11 ++++++++---
> tools/usb/usbip/libsrc/vhci_driver.h | 3 +--
> 2 files changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c
> index c9c81614a66ad..9a8acfc7697fa 100644
> --- a/tools/usb/usbip/libsrc/vhci_driver.c
> +++ b/tools/usb/usbip/libsrc/vhci_driver.c
> @@ -266,11 +266,11 @@ int usbip_vhci_driver_open(void)
> if (vhci_driver->nports <= 0) {
> err("no available ports");
> goto err;
> - } else if (vhci_driver->nports > MAXNPORT) {
> - err("port number exceeds %d", MAXNPORT);
> - goto err;
> }
>
> + vhci_driver->idev = calloc(vhci_driver->nports,
> + sizeof(struct usbip_imported_device));
> +

Missing check for memory allocation failure. Please add it.

> vhci_driver->ncontrollers = get_ncontrollers();
> dbg("available controllers: %d", vhci_driver->ncontrollers);
>
> @@ -287,6 +287,9 @@ int usbip_vhci_driver_open(void)
> err:
> udev_device_unref(vhci_driver->hc_device);
>
> + if (vhci_driver->idev)
> + free(vhci_driver->idev);
> +
> if (vhci_driver)
> free(vhci_driver);
>
> @@ -305,6 +308,8 @@ void usbip_vhci_driver_close(void)
>
> udev_device_unref(vhci_driver->hc_device);
>
> + free(vhci_driver->idev);
> +
> free(vhci_driver);
>
> vhci_driver = NULL;
> diff --git a/tools/usb/usbip/libsrc/vhci_driver.h b/tools/usb/usbip/libsrc/vhci_driver.h
> index 418b404d51210..67dbd1551e159 100644
> --- a/tools/usb/usbip/libsrc/vhci_driver.h
> +++ b/tools/usb/usbip/libsrc/vhci_driver.h
> @@ -13,7 +13,6 @@
>
> #define USBIP_VHCI_BUS_TYPE "platform"
> #define USBIP_VHCI_DEVICE_NAME "vhci_hcd.0"
> -#define MAXNPORT 128
>
> enum hub_speed {
> HUB_SPEED_HIGH = 0,
> @@ -41,7 +40,7 @@ struct usbip_vhci_driver {
>
> int ncontrollers;
> int nports;
> - struct usbip_imported_device idev[MAXNPORT];
> + struct usbip_imported_device *idev;
> };
>
>
>

Rest looks good.

thanks,
-- Shuah

2018-05-22 17:05:47

by Michael Grzeschik

[permalink] [raw]
Subject: [PATCH v2] usbip: dynamically allocate idev by nports found in sysfs

As the amount of available ports varies by the kernels build
configuration. To remove the limitation of the fixed 128 ports
we allocate the amount of idevs by using the number we get
from the kernel.

Signed-off-by: Michael Grzeschik <[email protected]>
---
v1 -> v2: - reworked memory allocation into one calloc call
- added error path on allocation failure

tools/usb/usbip/libsrc/vhci_driver.c | 14 +++++++++-----
tools/usb/usbip/libsrc/vhci_driver.h | 3 +--
2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c
index c9c81614a66ad..6e2a9edfd1f0d 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -242,13 +242,20 @@ static int read_record(int rhport, char *host, unsigned long host_len,

int usbip_vhci_driver_open(void)
{
+ int nports = get_nports();
+
udev_context = udev_new();
if (!udev_context) {
err("udev_new failed");
return -1;
}

- vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
+ vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver) +
+ nports * sizeof(struct usbip_imported_device));
+ if (!vhci_driver) {
+ err("vhci_driver allocation failed");
+ return -1;
+ }

/* will be freed in usbip_driver_close() */
vhci_driver->hc_device =
@@ -260,15 +267,12 @@ int usbip_vhci_driver_open(void)
goto err;
}

- vhci_driver->nports = get_nports();
+ vhci_driver->nports = nports;
dbg("available ports: %d", vhci_driver->nports);

if (vhci_driver->nports <= 0) {
err("no available ports");
goto err;
- } else if (vhci_driver->nports > MAXNPORT) {
- err("port number exceeds %d", MAXNPORT);
- goto err;
}

vhci_driver->ncontrollers = get_ncontrollers();
diff --git a/tools/usb/usbip/libsrc/vhci_driver.h b/tools/usb/usbip/libsrc/vhci_driver.h
index 418b404d51210..6c9aca2167051 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.h
+++ b/tools/usb/usbip/libsrc/vhci_driver.h
@@ -13,7 +13,6 @@

#define USBIP_VHCI_BUS_TYPE "platform"
#define USBIP_VHCI_DEVICE_NAME "vhci_hcd.0"
-#define MAXNPORT 128

enum hub_speed {
HUB_SPEED_HIGH = 0,
@@ -41,7 +40,7 @@ struct usbip_vhci_driver {

int ncontrollers;
int nports;
- struct usbip_imported_device idev[MAXNPORT];
+ struct usbip_imported_device idev[];
};


--
2.17.0


2018-05-22 22:57:11

by Shuah Khan

[permalink] [raw]
Subject: Re: [PATCH v2] usbip: dynamically allocate idev by nports found in sysfs

On 05/22/2018 11:04 AM, Michael Grzeschik wrote:
> As the amount of available ports varies by the kernels build
> configuration. To remove the limitation of the fixed 128 ports
> we allocate the amount of idevs by using the number we get
> from the kernel.
>
> Signed-off-by: Michael Grzeschik <[email protected]>
> ---
> v1 -> v2: - reworked memory allocation into one calloc call
> - added error path on allocation failure
>
> tools/usb/usbip/libsrc/vhci_driver.c | 14 +++++++++-----
> tools/usb/usbip/libsrc/vhci_driver.h | 3 +--
> 2 files changed, 10 insertions(+), 7 deletions(-)
>
> diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c
> index c9c81614a66ad..6e2a9edfd1f0d 100644
> --- a/tools/usb/usbip/libsrc/vhci_driver.c
> +++ b/tools/usb/usbip/libsrc/vhci_driver.c
> @@ -242,13 +242,20 @@ static int read_record(int rhport, char *host, unsigned long host_len,
>
> int usbip_vhci_driver_open(void)
> {
> + int nports = get_nports();
>

I missed this error leg in my previous comments. get_nports() could return
errorwhich is -1.

> udev_context = udev_new();
> if (!udev_context) {
> err("udev_new failed");
> return -1;
> }
>
> - vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
> + vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver) +
> + nports * sizeof(struct usbip_imported_device));

nports could be -1 at this point.

> + if (!vhci_driver) {
> + err("vhci_driver allocation failed");
> + return -1;
> + }
>
> /* will be freed in usbip_driver_close() */
> vhci_driver->hc_device =
> @@ -260,15 +267,12 @@ int usbip_vhci_driver_open(void)
> goto err;
> }
>
> - vhci_driver->nports = get_nports();
> + vhci_driver->nports = nports;
> dbg("available ports: %d", vhci_driver->nports);
>
> if (vhci_driver->nports <= 0) {
> err("no available ports");
> goto err;
This check should move up along with the get_nports() call.

> - } else if (vhci_driver->nports > MAXNPORT) {
> - err("port number exceeds %d", MAXNPORT);
> - goto err;
> }
>
> vhci_driver->ncontrollers = get_ncontrollers();
> diff --git a/tools/usb/usbip/libsrc/vhci_driver.h b/tools/usb/usbip/libsrc/vhci_driver.h
> index 418b404d51210..6c9aca2167051 100644
> --- a/tools/usb/usbip/libsrc/vhci_driver.h
> +++ b/tools/usb/usbip/libsrc/vhci_driver.h
> @@ -13,7 +13,6 @@
>
> #define USBIP_VHCI_BUS_TYPE "platform"
> #define USBIP_VHCI_DEVICE_NAME "vhci_hcd.0"
> -#define MAXNPORT 128
>
> enum hub_speed {
> HUB_SPEED_HIGH = 0,
> @@ -41,7 +40,7 @@ struct usbip_vhci_driver {
>
> int ncontrollers;
> int nports;
> - struct usbip_imported_device idev[MAXNPORT];
> + struct usbip_imported_device idev[];
> };
>
>
>

thanks,
-- Shuah

2018-05-23 09:23:48

by Michael Grzeschik

[permalink] [raw]
Subject: [PATCH v3] usbip: dynamically allocate idev by nports found in sysfs

As the amount of available ports varies by the kernels build
configuration. To remove the limitation of the fixed 128 ports
we allocate the amount of idevs by using the number we get
from the kernel.

Signed-off-by: Michael Grzeschik <[email protected]>
---
v1 -> v2: - reworked memory allocation into one calloc call
- added error path on allocation failure
v2 -> v3: - moved check for available nports to beginning of function

tools/usb/usbip/libsrc/vhci_driver.c | 24 ++++++++++++++----------
tools/usb/usbip/libsrc/vhci_driver.h | 3 +--
2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c
index c9c81614a66ad..c5db1be784bab 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -242,13 +242,25 @@ static int read_record(int rhport, char *host, unsigned long host_len,

int usbip_vhci_driver_open(void)
{
+ int nports = get_nports();
+
+ if (nports <= 0) {
+ err("no available ports");
+ return -1;
+ }
+
udev_context = udev_new();
if (!udev_context) {
err("udev_new failed");
return -1;
}

- vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
+ vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver) +
+ nports * sizeof(struct usbip_imported_device));
+ if (!vhci_driver) {
+ err("vhci_driver allocation failed");
+ return -1;
+ }

/* will be freed in usbip_driver_close() */
vhci_driver->hc_device =
@@ -260,17 +272,9 @@ int usbip_vhci_driver_open(void)
goto err;
}

- vhci_driver->nports = get_nports();
+ vhci_driver->nports = nports;
dbg("available ports: %d", vhci_driver->nports);

- if (vhci_driver->nports <= 0) {
- err("no available ports");
- goto err;
- } else if (vhci_driver->nports > MAXNPORT) {
- err("port number exceeds %d", MAXNPORT);
- goto err;
- }
-
vhci_driver->ncontrollers = get_ncontrollers();
dbg("available controllers: %d", vhci_driver->ncontrollers);

diff --git a/tools/usb/usbip/libsrc/vhci_driver.h b/tools/usb/usbip/libsrc/vhci_driver.h
index 418b404d51210..6c9aca2167051 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.h
+++ b/tools/usb/usbip/libsrc/vhci_driver.h
@@ -13,7 +13,6 @@

#define USBIP_VHCI_BUS_TYPE "platform"
#define USBIP_VHCI_DEVICE_NAME "vhci_hcd.0"
-#define MAXNPORT 128

enum hub_speed {
HUB_SPEED_HIGH = 0,
@@ -41,7 +40,7 @@ struct usbip_vhci_driver {

int ncontrollers;
int nports;
- struct usbip_imported_device idev[MAXNPORT];
+ struct usbip_imported_device idev[];
};


--
2.17.0


2018-05-23 16:46:00

by Shuah Khan

[permalink] [raw]
Subject: Re: [PATCH v3] usbip: dynamically allocate idev by nports found in sysfs

On 05/23/2018 03:22 AM, Michael Grzeschik wrote:
> As the amount of available ports varies by the kernels build
> configuration. To remove the limitation of the fixed 128 ports
> we allocate the amount of idevs by using the number we get
> from the kernel.
>
> Signed-off-by: Michael Grzeschik <[email protected]>
> ---
> v1 -> v2: - reworked memory allocation into one calloc call
> - added error path on allocation failure
> v2 -> v3: - moved check for available nports to beginning of function
>

Hmm. With this patch I see a segfault when I run usbip port command.
I think this patch is incomplete and more changes are needed to the
code that references the idev array.

I can't take this patch.

thanks,
-- Shuah

2018-05-23 21:02:30

by Michael Grzeschik

[permalink] [raw]
Subject: Re: [PATCH v3] usbip: dynamically allocate idev by nports found in sysfs

On Wed, May 23, 2018 at 10:44:57AM -0600, Shuah Khan wrote:
> On 05/23/2018 03:22 AM, Michael Grzeschik wrote:
> > As the amount of available ports varies by the kernels build
> > configuration. To remove the limitation of the fixed 128 ports
> > we allocate the amount of idevs by using the number we get
> > from the kernel.
> >
> > Signed-off-by: Michael Grzeschik <[email protected]>
> > ---
> > v1 -> v2: - reworked memory allocation into one calloc call
> > - added error path on allocation failure
> > v2 -> v3: - moved check for available nports to beginning of function
> >
>
> Hmm. With this patch I see a segfault when I run usbip port command.
> I think this patch is incomplete and more changes are needed to the
> code that references the idev array.
>
> I can't take this patch.

I will test it again tomorrow.

Thanks,
Michael

--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |


Attachments:
(No filename) (1.17 kB)
signature.asc (849.00 B)
Download all attachments

2018-05-24 07:40:07

by Michael Grzeschik

[permalink] [raw]
Subject: Re: [PATCH v3] usbip: dynamically allocate idev by nports found in sysfs

On Wed, May 23, 2018 at 10:44:57AM -0600, Shuah Khan wrote:
> On 05/23/2018 03:22 AM, Michael Grzeschik wrote:
> > As the amount of available ports varies by the kernels build
> > configuration. To remove the limitation of the fixed 128 ports
> > we allocate the amount of idevs by using the number we get
> > from the kernel.
> >
> > Signed-off-by: Michael Grzeschik <[email protected]>
> > ---
> > v1 -> v2: - reworked memory allocation into one calloc call
> > - added error path on allocation failure
> > v2 -> v3: - moved check for available nports to beginning of function
> >
>
> Hmm. With this patch I see a segfault when I run usbip port command.
> I think this patch is incomplete and more changes are needed to the
> code that references the idev array.
>
> I can't take this patch.

I missed that get_nports depends on vhci_driver->hc_device which is
not initialized that early. I will rework it to get the hc_device
by parameter and send v4.

Thanks,
Michael

--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |


Attachments:
(No filename) (1.30 kB)
signature.asc (849.00 B)
Download all attachments