diff mbox series

[RFC,v2,1/2] misc: pci_endpoint_test: Aggregate params checking for xfer

Message ID 20220906101555.106033-1-mie@igel.co.jp (mailing list archive)
State Superseded
Headers show
Series [RFC,v2,1/2] misc: pci_endpoint_test: Aggregate params checking for xfer | expand

Commit Message

Shunsuke Mie Sept. 6, 2022, 10:15 a.m. UTC
Each transfer test functions have same parameter checking code. This patch
unites those to an introduced function.

Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
---
Changes in v2:
* New patch
---
---
 drivers/misc/pci_endpoint_test.c | 35 ++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)

Comments

Greg KH Sept. 6, 2022, 10:58 a.m. UTC | #1
On Tue, Sep 06, 2022 at 07:15:54PM +0900, Shunsuke Mie wrote:
> Each transfer test functions have same parameter checking code. This patch
> unites those to an introduced function.
> 
> Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
> ---
> Changes in v2:
> * New patch
> ---
> ---
>  drivers/misc/pci_endpoint_test.c | 35 ++++++++++++++++++++++++++------
>  1 file changed, 29 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/misc/pci_endpoint_test.c b/drivers/misc/pci_endpoint_test.c
> index 8f786a225dcf..3bd9f135cdac 100644
> --- a/drivers/misc/pci_endpoint_test.c
> +++ b/drivers/misc/pci_endpoint_test.c
> @@ -332,6 +332,17 @@ static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
>  	return false;
>  }
>  
> +static int pci_endpoint_test_validate_xfer_params(struct device *dev,
> +		struct pci_endpoint_test_xfer_param *param, size_t alignment)
> +{
> +	if (param->size > SIZE_MAX - alignment) {
> +		dev_err(dev, "Maximum transfer data size exceeded\n");

Do not allow userspace to spam the kernel log.

This should be dev_dbg() at most.

thanks,

greg k-h
Greg KH Sept. 6, 2022, 10:58 a.m. UTC | #2
On Tue, Sep 06, 2022 at 07:15:54PM +0900, Shunsuke Mie wrote:
> Each transfer test functions have same parameter checking code. This patch
> unites those to an introduced function.
> 
> Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
> ---
> Changes in v2:
> * New patch
> ---
> ---
>  drivers/misc/pci_endpoint_test.c | 35 ++++++++++++++++++++++++++------
>  1 file changed, 29 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/misc/pci_endpoint_test.c b/drivers/misc/pci_endpoint_test.c
> index 8f786a225dcf..3bd9f135cdac 100644
> --- a/drivers/misc/pci_endpoint_test.c
> +++ b/drivers/misc/pci_endpoint_test.c
> @@ -332,6 +332,17 @@ static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
>  	return false;
>  }
>  
> +static int pci_endpoint_test_validate_xfer_params(struct device *dev,
> +		struct pci_endpoint_test_xfer_param *param, size_t alignment)
> +{
> +	if (param->size > SIZE_MAX - alignment) {
> +		dev_err(dev, "Maximum transfer data size exceeded\n");
> +		return -EINVAL;
> +	}
> +
> +	return 0;
> +}
> +
>  static bool pci_endpoint_test_copy(struct pci_endpoint_test *test,
>  				   unsigned long arg)
>  {
> @@ -363,9 +374,13 @@ static bool pci_endpoint_test_copy(struct pci_endpoint_test *test,
>  		return false;
>  	}
>  
> +	err = pci_endpoint_test_validate_xfer_params(dev, &param, alignment);
> +	if (err) {
> +		dev_err(dev, "Invalid parameter\n");

Also, you just printed 2 errors for the same error.  Not needed.
Shunsuke Mie Sept. 7, 2022, 1:59 a.m. UTC | #3
2022年9月6日(火) 19:58 Greg Kroah-Hartman <gregkh@linuxfoundation.org>:
>
> On Tue, Sep 06, 2022 at 07:15:54PM +0900, Shunsuke Mie wrote:
> > Each transfer test functions have same parameter checking code. This patch
> > unites those to an introduced function.
> >
> > Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
> > ---
> > Changes in v2:
> > * New patch
> > ---
> > ---
> >  drivers/misc/pci_endpoint_test.c | 35 ++++++++++++++++++++++++++------
> >  1 file changed, 29 insertions(+), 6 deletions(-)
> >
> > diff --git a/drivers/misc/pci_endpoint_test.c b/drivers/misc/pci_endpoint_test.c
> > index 8f786a225dcf..3bd9f135cdac 100644
> > --- a/drivers/misc/pci_endpoint_test.c
> > +++ b/drivers/misc/pci_endpoint_test.c
> > @@ -332,6 +332,17 @@ static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
> >       return false;
> >  }
> >
> > +static int pci_endpoint_test_validate_xfer_params(struct device *dev,
> > +             struct pci_endpoint_test_xfer_param *param, size_t alignment)
> > +{
> > +     if (param->size > SIZE_MAX - alignment) {
> > +             dev_err(dev, "Maximum transfer data size exceeded\n");
> > +             return -EINVAL;
> > +     }
> > +
> > +     return 0;
> > +}
> > +
> >  static bool pci_endpoint_test_copy(struct pci_endpoint_test *test,
> >                                  unsigned long arg)
> >  {
> > @@ -363,9 +374,13 @@ static bool pci_endpoint_test_copy(struct pci_endpoint_test *test,
> >               return false;
> >       }
> >
> > +     err = pci_endpoint_test_validate_xfer_params(dev, &param, alignment);
> > +     if (err) {
> > +             dev_err(dev, "Invalid parameter\n");
>
> Also, you just printed 2 errors for the same error.  Not needed.
I see. I'll also change to using the dev_dbg() in the next version.

Thanks,
Shunsuke
diff mbox series

Patch

diff --git a/drivers/misc/pci_endpoint_test.c b/drivers/misc/pci_endpoint_test.c
index 8f786a225dcf..3bd9f135cdac 100644
--- a/drivers/misc/pci_endpoint_test.c
+++ b/drivers/misc/pci_endpoint_test.c
@@ -332,6 +332,17 @@  static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
 	return false;
 }
 
+static int pci_endpoint_test_validate_xfer_params(struct device *dev,
+		struct pci_endpoint_test_xfer_param *param, size_t alignment)
+{
+	if (param->size > SIZE_MAX - alignment) {
+		dev_err(dev, "Maximum transfer data size exceeded\n");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 static bool pci_endpoint_test_copy(struct pci_endpoint_test *test,
 				   unsigned long arg)
 {
@@ -363,9 +374,13 @@  static bool pci_endpoint_test_copy(struct pci_endpoint_test *test,
 		return false;
 	}
 
+	err = pci_endpoint_test_validate_xfer_params(dev, &param, alignment);
+	if (err) {
+		dev_err(dev, "Invalid parameter\n");
+		return false;
+	}
+
 	size = param.size;
-	if (size > SIZE_MAX - alignment)
-		goto err;
 
 	use_dma = !!(param.flags & PCITEST_FLAGS_USE_DMA);
 	if (use_dma)
@@ -497,9 +512,13 @@  static bool pci_endpoint_test_write(struct pci_endpoint_test *test,
 		return false;
 	}
 
+	err = pci_endpoint_test_validate_xfer_params(dev, &param, alignment);
+	if (err) {
+		dev_err(dev, "Invalid parameter\n");
+		return false;
+	}
+
 	size = param.size;
-	if (size > SIZE_MAX - alignment)
-		goto err;
 
 	use_dma = !!(param.flags & PCITEST_FLAGS_USE_DMA);
 	if (use_dma)
@@ -595,9 +614,13 @@  static bool pci_endpoint_test_read(struct pci_endpoint_test *test,
 		return false;
 	}
 
+	err = pci_endpoint_test_validate_xfer_params(dev, &param, alignment);
+	if (err) {
+		dev_err(dev, "Invalid parameter\n");
+		return false;
+	}
+
 	size = param.size;
-	if (size > SIZE_MAX - alignment)
-		goto err;
 
 	use_dma = !!(param.flags & PCITEST_FLAGS_USE_DMA);
 	if (use_dma)