linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [v2] usbip: dynamically allocate idev by nports found in sysfs
@ 2018-05-22 17:04 Michael Grzeschik
  0 siblings, 0 replies; 2+ messages in thread
From: Michael Grzeschik @ 2018-05-22 17:04 UTC (permalink / raw)
  To: shuah, linux-usb; +Cc: valentina.manea.m, gregkh, linux-kernel, kernel

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 <m.grzeschik@pengutronix.de>
---
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[];
 };
 
 

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [v2] usbip: dynamically allocate idev by nports found in sysfs
@ 2018-05-22 22:56 Shuah Khan
  0 siblings, 0 replies; 2+ messages in thread
From: Shuah Khan @ 2018-05-22 22:56 UTC (permalink / raw)
  To: Michael Grzeschik, linux-usb
  Cc: valentina.manea.m, gregkh, linux-kernel, kernel, Shuah Khan

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 <m.grzeschik@pengutronix.de>
> ---
> 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
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-05-22 22:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-05-22 22:56 [v2] usbip: dynamically allocate idev by nports found in sysfs Shuah Khan
  -- strict thread matches above, loose matches on Subject: below --
2018-05-22 17:04 Michael Grzeschik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).