linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] PCI: hv: Use struct_size() helper
@ 2020-05-25 16:43 Gustavo A. R. Silva
  2020-05-26  9:38 ` Wei Liu
  2020-05-28 14:41 ` Lorenzo Pieralisi
  0 siblings, 2 replies; 3+ messages in thread
From: Gustavo A. R. Silva @ 2020-05-25 16:43 UTC (permalink / raw)
  To: K. Y. Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
	Lorenzo Pieralisi, Rob Herring, Bjorn Helgaas
  Cc: linux-hyperv, linux-pci, linux-kernel, Gustavo A. R. Silva, Kees Cook

One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:

struct hv_dr_state {
	...
        struct hv_pcidev_description func[];
};

struct pci_bus_relations {
	...
        struct pci_function_description func[];
} __packed;

Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes.

So, replace the following forms:

offsetof(struct hv_dr_state, func) +
	(sizeof(struct hv_pcidev_description) *
	(relations->device_count))

offsetof(struct pci_bus_relations, func) +
	(sizeof(struct pci_function_description) *
	(bus_rel->device_count))

with:

struct_size(dr, func, relations->device_count)

and

struct_size(bus_rel, func, bus_rel->device_count)

respectively.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/pci/controller/pci-hyperv.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c
index 892f3a742117a..bf40ff09c99d6 100644
--- a/drivers/pci/controller/pci-hyperv.c
+++ b/drivers/pci/controller/pci-hyperv.c
@@ -2213,10 +2213,8 @@ static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
 	struct hv_dr_state *dr;
 	int i;
 
-	dr = kzalloc(offsetof(struct hv_dr_state, func) +
-		     (sizeof(struct hv_pcidev_description) *
-		      (relations->device_count)), GFP_NOWAIT);
-
+	dr = kzalloc(struct_size(dr, func, relations->device_count),
+		     GFP_NOWAIT);
 	if (!dr)
 		return;
 
@@ -2250,10 +2248,8 @@ static void hv_pci_devices_present2(struct hv_pcibus_device *hbus,
 	struct hv_dr_state *dr;
 	int i;
 
-	dr = kzalloc(offsetof(struct hv_dr_state, func) +
-		     (sizeof(struct hv_pcidev_description) *
-		      (relations->device_count)), GFP_NOWAIT);
-
+	dr = kzalloc(struct_size(dr, func, relations->device_count),
+		     GFP_NOWAIT);
 	if (!dr)
 		return;
 
@@ -2447,9 +2443,8 @@ static void hv_pci_onchannelcallback(void *context)
 
 				bus_rel = (struct pci_bus_relations *)buffer;
 				if (bytes_recvd <
-				    offsetof(struct pci_bus_relations, func) +
-				    (sizeof(struct pci_function_description) *
-				     (bus_rel->device_count))) {
+					struct_size(bus_rel, func,
+						    bus_rel->device_count)) {
 					dev_err(&hbus->hdev->device,
 						"bus relations too small\n");
 					break;
@@ -2462,9 +2457,8 @@ static void hv_pci_onchannelcallback(void *context)
 
 				bus_rel2 = (struct pci_bus_relations2 *)buffer;
 				if (bytes_recvd <
-				    offsetof(struct pci_bus_relations2, func) +
-				    (sizeof(struct pci_function_description2) *
-				     (bus_rel2->device_count))) {
+					struct_size(bus_rel2, func,
+						    bus_rel2->device_count)) {
 					dev_err(&hbus->hdev->device,
 						"bus relations v2 too small\n");
 					break;
-- 
2.26.2


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

* Re: [PATCH] PCI: hv: Use struct_size() helper
  2020-05-25 16:43 [PATCH] PCI: hv: Use struct_size() helper Gustavo A. R. Silva
@ 2020-05-26  9:38 ` Wei Liu
  2020-05-28 14:41 ` Lorenzo Pieralisi
  1 sibling, 0 replies; 3+ messages in thread
From: Wei Liu @ 2020-05-26  9:38 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: K. Y. Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
	Lorenzo Pieralisi, Rob Herring, Bjorn Helgaas, linux-hyperv,
	linux-pci, linux-kernel, Gustavo A. R. Silva, Kees Cook

On Mon, May 25, 2020 at 11:43:19AM -0500, Gustavo A. R. Silva wrote:
> One of the more common cases of allocation size calculations is finding
> the size of a structure that has a zero-sized array at the end, along
> with memory for some number of elements for that array. For example:
> 
> struct hv_dr_state {
> 	...
>         struct hv_pcidev_description func[];
> };
> 
> struct pci_bus_relations {
> 	...
>         struct pci_function_description func[];
> } __packed;
> 
> Make use of the struct_size() helper instead of an open-coded version
> in order to avoid any potential type mistakes.
> 
> So, replace the following forms:
> 
> offsetof(struct hv_dr_state, func) +
> 	(sizeof(struct hv_pcidev_description) *
> 	(relations->device_count))
> 
> offsetof(struct pci_bus_relations, func) +
> 	(sizeof(struct pci_function_description) *
> 	(bus_rel->device_count))
> 
> with:
> 
> struct_size(dr, func, relations->device_count)
> 
> and
> 
> struct_size(bus_rel, func, bus_rel->device_count)
> 
> respectively.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Reviewed-by: Wei Liu <wei.liu@kernel.org>

FAOD I expect this patch to go through pci tree.

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

* Re: [PATCH] PCI: hv: Use struct_size() helper
  2020-05-25 16:43 [PATCH] PCI: hv: Use struct_size() helper Gustavo A. R. Silva
  2020-05-26  9:38 ` Wei Liu
@ 2020-05-28 14:41 ` Lorenzo Pieralisi
  1 sibling, 0 replies; 3+ messages in thread
From: Lorenzo Pieralisi @ 2020-05-28 14:41 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: K. Y. Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
	Rob Herring, Bjorn Helgaas, linux-hyperv, linux-pci,
	linux-kernel, Gustavo A. R. Silva, Kees Cook

On Mon, May 25, 2020 at 11:43:19AM -0500, Gustavo A. R. Silva wrote:
> One of the more common cases of allocation size calculations is finding
> the size of a structure that has a zero-sized array at the end, along
> with memory for some number of elements for that array. For example:
> 
> struct hv_dr_state {
> 	...
>         struct hv_pcidev_description func[];
> };
> 
> struct pci_bus_relations {
> 	...
>         struct pci_function_description func[];
> } __packed;
> 
> Make use of the struct_size() helper instead of an open-coded version
> in order to avoid any potential type mistakes.
> 
> So, replace the following forms:
> 
> offsetof(struct hv_dr_state, func) +
> 	(sizeof(struct hv_pcidev_description) *
> 	(relations->device_count))
> 
> offsetof(struct pci_bus_relations, func) +
> 	(sizeof(struct pci_function_description) *
> 	(bus_rel->device_count))
> 
> with:
> 
> struct_size(dr, func, relations->device_count)
> 
> and
> 
> struct_size(bus_rel, func, bus_rel->device_count)
> 
> respectively.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  drivers/pci/controller/pci-hyperv.c | 22 ++++++++--------------
>  1 file changed, 8 insertions(+), 14 deletions(-)

Applied to pci/hv, thanks.

Lorenzo

> diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c
> index 892f3a742117a..bf40ff09c99d6 100644
> --- a/drivers/pci/controller/pci-hyperv.c
> +++ b/drivers/pci/controller/pci-hyperv.c
> @@ -2213,10 +2213,8 @@ static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
>  	struct hv_dr_state *dr;
>  	int i;
>  
> -	dr = kzalloc(offsetof(struct hv_dr_state, func) +
> -		     (sizeof(struct hv_pcidev_description) *
> -		      (relations->device_count)), GFP_NOWAIT);
> -
> +	dr = kzalloc(struct_size(dr, func, relations->device_count),
> +		     GFP_NOWAIT);
>  	if (!dr)
>  		return;
>  
> @@ -2250,10 +2248,8 @@ static void hv_pci_devices_present2(struct hv_pcibus_device *hbus,
>  	struct hv_dr_state *dr;
>  	int i;
>  
> -	dr = kzalloc(offsetof(struct hv_dr_state, func) +
> -		     (sizeof(struct hv_pcidev_description) *
> -		      (relations->device_count)), GFP_NOWAIT);
> -
> +	dr = kzalloc(struct_size(dr, func, relations->device_count),
> +		     GFP_NOWAIT);
>  	if (!dr)
>  		return;
>  
> @@ -2447,9 +2443,8 @@ static void hv_pci_onchannelcallback(void *context)
>  
>  				bus_rel = (struct pci_bus_relations *)buffer;
>  				if (bytes_recvd <
> -				    offsetof(struct pci_bus_relations, func) +
> -				    (sizeof(struct pci_function_description) *
> -				     (bus_rel->device_count))) {
> +					struct_size(bus_rel, func,
> +						    bus_rel->device_count)) {
>  					dev_err(&hbus->hdev->device,
>  						"bus relations too small\n");
>  					break;
> @@ -2462,9 +2457,8 @@ static void hv_pci_onchannelcallback(void *context)
>  
>  				bus_rel2 = (struct pci_bus_relations2 *)buffer;
>  				if (bytes_recvd <
> -				    offsetof(struct pci_bus_relations2, func) +
> -				    (sizeof(struct pci_function_description2) *
> -				     (bus_rel2->device_count))) {
> +					struct_size(bus_rel2, func,
> +						    bus_rel2->device_count)) {
>  					dev_err(&hbus->hdev->device,
>  						"bus relations v2 too small\n");
>  					break;
> -- 
> 2.26.2
> 

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

end of thread, other threads:[~2020-05-28 14:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-25 16:43 [PATCH] PCI: hv: Use struct_size() helper Gustavo A. R. Silva
2020-05-26  9:38 ` Wei Liu
2020-05-28 14:41 ` Lorenzo Pieralisi

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).