diff mbox series

PCI: endpoint: pci-epf-test: Make use of cached 'epc_features' in pci_epf_test_core_init()

Message ID 20240417-pci-epf-test-fix-v1-1-653c911d1faa@linaro.org
State New
Headers show
Series PCI: endpoint: pci-epf-test: Make use of cached 'epc_features' in pci_epf_test_core_init() | expand

Commit Message

Manivannan Sadhasivam April 17, 2024, 5:17 p.m. UTC
Instead of getting the epc_features from pci_epc_get_features() API, use
the cached pci_epf_test::epc_features value to avoid the NULL check. Since
the NULL check is already performed in pci_epf_test_bind(), having one more
check in pci_epf_test_core_init() is redundant and it is not possible to
hit the NULL pointer dereference. This also leads to the following smatch
warning:

drivers/pci/endpoint/functions/pci-epf-test.c:784 pci_epf_test_core_init()
error: we previously assumed 'epc_features' could be null (see line 747)

Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/linux-pci/024b5826-7180-4076-ae08-57d2584cca3f@moroto.mountain/
Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
---
 drivers/pci/endpoint/functions/pci-epf-test.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)


---
base-commit: 6e47dcb2ca223211c43c37497836cd9666c70674
change-id: 20240417-pci-epf-test-fix-2209ae22be80

Best regards,

Comments

Niklas Cassel April 17, 2024, 5:49 p.m. UTC | #1
On Wed, Apr 17, 2024 at 10:47:25PM +0530, Manivannan Sadhasivam wrote:
> Instead of getting the epc_features from pci_epc_get_features() API, use
> the cached pci_epf_test::epc_features value to avoid the NULL check. Since
> the NULL check is already performed in pci_epf_test_bind(), having one more
> check in pci_epf_test_core_init() is redundant and it is not possible to
> hit the NULL pointer dereference. This also leads to the following smatch
> warning:
> 
> drivers/pci/endpoint/functions/pci-epf-test.c:784 pci_epf_test_core_init()
> error: we previously assumed 'epc_features' could be null (see line 747)
> 
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/linux-pci/024b5826-7180-4076-ae08-57d2584cca3f@moroto.mountain/
> Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>

I think you forgot:
Fixes: a01e7214bef9 ("PCI: endpoint: Remove "core_init_notifier" flag")


> ---
>  drivers/pci/endpoint/functions/pci-epf-test.c | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
> index 977fb79c1567..0d28f413cb07 100644
> --- a/drivers/pci/endpoint/functions/pci-epf-test.c
> +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
> @@ -743,11 +743,10 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
>  	bool msi_capable = true;
>  	int ret;
>  
> -	epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
> -	if (epc_features) {
> -		msix_capable = epc_features->msix_capable;
> -		msi_capable = epc_features->msi_capable;
> -	}
> +	epc_features = epf_test->epc_features;

How about:

index 977fb79c1567..4d6105c07ac0 100644
--- a/drivers/pci/endpoint/functions/pci-epf-test.c
+++ b/drivers/pci/endpoint/functions/pci-epf-test.c
@@ -735,7 +735,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
 {
        struct pci_epf_test *epf_test = epf_get_drvdata(epf);
        struct pci_epf_header *header = epf->header;
-       const struct pci_epc_features *epc_features;
+       const struct pci_epc_features *epc_features = epf_test->epc_features;
        struct pci_epc *epc = epf->epc;
        struct device *dev = &epf->dev;
        bool linkup_notifier = false;
@@ -743,12 +743,6 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
        bool msi_capable = true;
        int ret;
 
-       epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
-       if (epc_features) {
-               msix_capable = epc_features->msix_capable;
-               msi_capable = epc_features->msi_capable;
-       }
-
        if (epf->vfunc_no <= 1) {
                ret = pci_epc_write_header(epc, epf->func_no, epf->vfunc_no, header);
                if (ret) {
@@ -761,6 +755,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
        if (ret)
                return ret;
 
+       msi_capable = epc_features->msi_capable;
        if (msi_capable) {
                ret = pci_epc_set_msi(epc, epf->func_no, epf->vfunc_no,
                                      epf->msi_interrupts);
@@ -770,6 +765,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
                }
        }
 
+       msix_capable = epc_features->msix_capable;
        if (msix_capable) {
                ret = pci_epc_set_msix(epc, epf->func_no, epf->vfunc_no,
                                       epf->msix_interrupts,
@@ -814,11 +810,9 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
        void *base;
        enum pci_barno test_reg_bar = epf_test->test_reg_bar;
        enum pci_barno bar;
-       const struct pci_epc_features *epc_features;
+       const struct pci_epc_features *epc_features = epf_test->epc_features;
        size_t test_reg_size;
 
-       epc_features = epf_test->epc_features;
-
        test_reg_bar_size = ALIGN(sizeof(struct pci_epf_test_reg), 128);
 
        msix_capable = epc_features->msix_capable;


Instead?

That way, we assign msi_capable/msix_capable just before the if-statement
where it is used. (Which matches how we already assign msix_capable just
before the if-statement in pci_epf_test_alloc_space().)


Kind regards,
Niklas


> +
> +	msix_capable = epc_features->msix_capable;
> +	msi_capable = epc_features->msi_capable;
>  
>  	if (epf->vfunc_no <= 1) {
>  		ret = pci_epc_write_header(epc, epf->func_no, epf->vfunc_no, header);
> 
> ---
> base-commit: 6e47dcb2ca223211c43c37497836cd9666c70674
> change-id: 20240417-pci-epf-test-fix-2209ae22be80
> 
> Best regards,
> -- 
> Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
>
Dan Carpenter April 17, 2024, 5:54 p.m. UTC | #2
On Wed, Apr 17, 2024 at 07:49:45PM +0200, Niklas Cassel wrote:
> @@ -761,6 +755,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
>         if (ret)
>                 return ret;
>  
> +       msi_capable = epc_features->msi_capable;
>         if (msi_capable) {

Or just:  if (epc_features->msi_capable) {

;)

>                 ret = pci_epc_set_msi(epc, epf->func_no, epf->vfunc_no,
>                                       epf->msi_interrupts);

regards,
dan carpenter
Niklas Cassel April 17, 2024, 5:54 p.m. UTC | #3
On Wed, Apr 17, 2024 at 07:49:45PM +0200, Niklas Cassel wrote:
> On Wed, Apr 17, 2024 at 10:47:25PM +0530, Manivannan Sadhasivam wrote:
> > Instead of getting the epc_features from pci_epc_get_features() API, use
> > the cached pci_epf_test::epc_features value to avoid the NULL check. Since
> > the NULL check is already performed in pci_epf_test_bind(), having one more
> > check in pci_epf_test_core_init() is redundant and it is not possible to
> > hit the NULL pointer dereference. This also leads to the following smatch
> > warning:
> > 
> > drivers/pci/endpoint/functions/pci-epf-test.c:784 pci_epf_test_core_init()
> > error: we previously assumed 'epc_features' could be null (see line 747)
> > 
> > Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > Closes: https://lore.kernel.org/linux-pci/024b5826-7180-4076-ae08-57d2584cca3f@moroto.mountain/
> > Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
> 
> I think you forgot:
> Fixes: a01e7214bef9 ("PCI: endpoint: Remove "core_init_notifier" flag")
> 
> 
> > ---
> >  drivers/pci/endpoint/functions/pci-epf-test.c | 9 ++++-----
> >  1 file changed, 4 insertions(+), 5 deletions(-)
> > 
> > diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
> > index 977fb79c1567..0d28f413cb07 100644
> > --- a/drivers/pci/endpoint/functions/pci-epf-test.c
> > +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
> > @@ -743,11 +743,10 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> >  	bool msi_capable = true;
> >  	int ret;
> >  
> > -	epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
> > -	if (epc_features) {
> > -		msix_capable = epc_features->msix_capable;
> > -		msi_capable = epc_features->msi_capable;
> > -	}
> > +	epc_features = epf_test->epc_features;
> 
> How about:
> 
> index 977fb79c1567..4d6105c07ac0 100644
> --- a/drivers/pci/endpoint/functions/pci-epf-test.c
> +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
> @@ -735,7 +735,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
>  {
>         struct pci_epf_test *epf_test = epf_get_drvdata(epf);
>         struct pci_epf_header *header = epf->header;
> -       const struct pci_epc_features *epc_features;
> +       const struct pci_epc_features *epc_features = epf_test->epc_features;
>         struct pci_epc *epc = epf->epc;
>         struct device *dev = &epf->dev;
>         bool linkup_notifier = false;
> @@ -743,12 +743,6 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
>         bool msi_capable = true;
>         int ret;
>  
> -       epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
> -       if (epc_features) {
> -               msix_capable = epc_features->msix_capable;
> -               msi_capable = epc_features->msi_capable;
> -       }
> -
>         if (epf->vfunc_no <= 1) {
>                 ret = pci_epc_write_header(epc, epf->func_no, epf->vfunc_no, header);
>                 if (ret) {
> @@ -761,6 +755,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
>         if (ret)
>                 return ret;
>  
> +       msi_capable = epc_features->msi_capable;
>         if (msi_capable) {
>                 ret = pci_epc_set_msi(epc, epf->func_no, epf->vfunc_no,
>                                       epf->msi_interrupts);
> @@ -770,6 +765,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
>                 }
>         }
>  
> +       msix_capable = epc_features->msix_capable;
>         if (msix_capable) {
>                 ret = pci_epc_set_msix(epc, epf->func_no, epf->vfunc_no,
>                                        epf->msix_interrupts,
> @@ -814,11 +810,9 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
>         void *base;
>         enum pci_barno test_reg_bar = epf_test->test_reg_bar;
>         enum pci_barno bar;
> -       const struct pci_epc_features *epc_features;
> +       const struct pci_epc_features *epc_features = epf_test->epc_features;
>         size_t test_reg_size;
>  
> -       epc_features = epf_test->epc_features;
> -
>         test_reg_bar_size = ALIGN(sizeof(struct pci_epf_test_reg), 128);
>  
>         msix_capable = epc_features->msix_capable;
> 
> 
> Instead?
> 
> That way, we assign msi_capable/msix_capable just before the if-statement
> where it is used. (Which matches how we already assign msix_capable just
> before the if-statement in pci_epf_test_alloc_space().)

...or just kill the local variables:
bool msi_capable/msix_capable in pci_epf_test_core_init(), and
bool msix_capable pci_epf_test_alloc_space()
and just do:

if (epc_features->msix_capable) / if (epc_features->msi_capable)

directly?


Kind regards,
Niklas
Manivannan Sadhasivam April 18, 2024, 5:43 a.m. UTC | #4
On Wed, Apr 17, 2024 at 07:49:45PM +0200, Niklas Cassel wrote:
> On Wed, Apr 17, 2024 at 10:47:25PM +0530, Manivannan Sadhasivam wrote:
> > Instead of getting the epc_features from pci_epc_get_features() API, use
> > the cached pci_epf_test::epc_features value to avoid the NULL check. Since
> > the NULL check is already performed in pci_epf_test_bind(), having one more
> > check in pci_epf_test_core_init() is redundant and it is not possible to
> > hit the NULL pointer dereference. This also leads to the following smatch
> > warning:
> > 
> > drivers/pci/endpoint/functions/pci-epf-test.c:784 pci_epf_test_core_init()
> > error: we previously assumed 'epc_features' could be null (see line 747)
> > 
> > Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > Closes: https://lore.kernel.org/linux-pci/024b5826-7180-4076-ae08-57d2584cca3f@moroto.mountain/
> > Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
> 
> I think you forgot:
> Fixes: a01e7214bef9 ("PCI: endpoint: Remove "core_init_notifier" flag")
> 

No, that's not the correct fixes tag I suppose. This redudant check is
introduced by commit, 5e50ee27d4a5 ("PCI: pci-epf-test: Add support to defer
core initialization") and this commit removes the redundant check (fixing smatch
warning is a side effect). So if the fixes tag needs to be added, then this
commit should be referenced.

> 
> > ---
> >  drivers/pci/endpoint/functions/pci-epf-test.c | 9 ++++-----
> >  1 file changed, 4 insertions(+), 5 deletions(-)
> > 
> > diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
> > index 977fb79c1567..0d28f413cb07 100644
> > --- a/drivers/pci/endpoint/functions/pci-epf-test.c
> > +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
> > @@ -743,11 +743,10 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> >  	bool msi_capable = true;
> >  	int ret;
> >  
> > -	epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
> > -	if (epc_features) {
> > -		msix_capable = epc_features->msix_capable;
> > -		msi_capable = epc_features->msi_capable;
> > -	}
> > +	epc_features = epf_test->epc_features;
> 
> How about:
> 
> index 977fb79c1567..4d6105c07ac0 100644
> --- a/drivers/pci/endpoint/functions/pci-epf-test.c
> +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
> @@ -735,7 +735,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
>  {
>         struct pci_epf_test *epf_test = epf_get_drvdata(epf);
>         struct pci_epf_header *header = epf->header;
> -       const struct pci_epc_features *epc_features;
> +       const struct pci_epc_features *epc_features = epf_test->epc_features;
>         struct pci_epc *epc = epf->epc;
>         struct device *dev = &epf->dev;
>         bool linkup_notifier = false;
> @@ -743,12 +743,6 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
>         bool msi_capable = true;
>         int ret;
>  
> -       epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
> -       if (epc_features) {
> -               msix_capable = epc_features->msix_capable;
> -               msi_capable = epc_features->msi_capable;
> -       }
> -
>         if (epf->vfunc_no <= 1) {
>                 ret = pci_epc_write_header(epc, epf->func_no, epf->vfunc_no, header);
>                 if (ret) {
> @@ -761,6 +755,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
>         if (ret)
>                 return ret;
>  
> +       msi_capable = epc_features->msi_capable;
>         if (msi_capable) {
>                 ret = pci_epc_set_msi(epc, epf->func_no, epf->vfunc_no,
>                                       epf->msi_interrupts);
> @@ -770,6 +765,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
>                 }
>         }
>  
> +       msix_capable = epc_features->msix_capable;
>         if (msix_capable) {
>                 ret = pci_epc_set_msix(epc, epf->func_no, epf->vfunc_no,
>                                        epf->msix_interrupts,
> @@ -814,11 +810,9 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
>         void *base;
>         enum pci_barno test_reg_bar = epf_test->test_reg_bar;
>         enum pci_barno bar;
> -       const struct pci_epc_features *epc_features;
> +       const struct pci_epc_features *epc_features = epf_test->epc_features;
>         size_t test_reg_size;
>  
> -       epc_features = epf_test->epc_features;
> -
>         test_reg_bar_size = ALIGN(sizeof(struct pci_epf_test_reg), 128);
>  
>         msix_capable = epc_features->msix_capable;
> 
> 
> Instead?
> 
> That way, we assign msi_capable/msix_capable just before the if-statement
> where it is used. (Which matches how we already assign msix_capable just
> before the if-statement in pci_epf_test_alloc_space().)
> 

Ok, if we go with this pattern, then pci_epf_test_set_bar() also needs to be
updated.

- Mani
Manivannan Sadhasivam April 18, 2024, 5:47 a.m. UTC | #5
On Wed, Apr 17, 2024 at 07:54:56PM +0200, Niklas Cassel wrote:
> On Wed, Apr 17, 2024 at 07:49:45PM +0200, Niklas Cassel wrote:
> > On Wed, Apr 17, 2024 at 10:47:25PM +0530, Manivannan Sadhasivam wrote:
> > > Instead of getting the epc_features from pci_epc_get_features() API, use
> > > the cached pci_epf_test::epc_features value to avoid the NULL check. Since
> > > the NULL check is already performed in pci_epf_test_bind(), having one more
> > > check in pci_epf_test_core_init() is redundant and it is not possible to
> > > hit the NULL pointer dereference. This also leads to the following smatch
> > > warning:
> > > 
> > > drivers/pci/endpoint/functions/pci-epf-test.c:784 pci_epf_test_core_init()
> > > error: we previously assumed 'epc_features' could be null (see line 747)
> > > 
> > > Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > > Closes: https://lore.kernel.org/linux-pci/024b5826-7180-4076-ae08-57d2584cca3f@moroto.mountain/
> > > Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
> > 
> > I think you forgot:
> > Fixes: a01e7214bef9 ("PCI: endpoint: Remove "core_init_notifier" flag")
> > 
> > 
> > > ---
> > >  drivers/pci/endpoint/functions/pci-epf-test.c | 9 ++++-----
> > >  1 file changed, 4 insertions(+), 5 deletions(-)
> > > 
> > > diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
> > > index 977fb79c1567..0d28f413cb07 100644
> > > --- a/drivers/pci/endpoint/functions/pci-epf-test.c
> > > +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
> > > @@ -743,11 +743,10 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> > >  	bool msi_capable = true;
> > >  	int ret;
> > >  
> > > -	epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
> > > -	if (epc_features) {
> > > -		msix_capable = epc_features->msix_capable;
> > > -		msi_capable = epc_features->msi_capable;
> > > -	}
> > > +	epc_features = epf_test->epc_features;
> > 
> > How about:
> > 
> > index 977fb79c1567..4d6105c07ac0 100644
> > --- a/drivers/pci/endpoint/functions/pci-epf-test.c
> > +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
> > @@ -735,7 +735,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> >  {
> >         struct pci_epf_test *epf_test = epf_get_drvdata(epf);
> >         struct pci_epf_header *header = epf->header;
> > -       const struct pci_epc_features *epc_features;
> > +       const struct pci_epc_features *epc_features = epf_test->epc_features;
> >         struct pci_epc *epc = epf->epc;
> >         struct device *dev = &epf->dev;
> >         bool linkup_notifier = false;
> > @@ -743,12 +743,6 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> >         bool msi_capable = true;
> >         int ret;
> >  
> > -       epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
> > -       if (epc_features) {
> > -               msix_capable = epc_features->msix_capable;
> > -               msi_capable = epc_features->msi_capable;
> > -       }
> > -
> >         if (epf->vfunc_no <= 1) {
> >                 ret = pci_epc_write_header(epc, epf->func_no, epf->vfunc_no, header);
> >                 if (ret) {
> > @@ -761,6 +755,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> >         if (ret)
> >                 return ret;
> >  
> > +       msi_capable = epc_features->msi_capable;
> >         if (msi_capable) {
> >                 ret = pci_epc_set_msi(epc, epf->func_no, epf->vfunc_no,
> >                                       epf->msi_interrupts);
> > @@ -770,6 +765,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> >                 }
> >         }
> >  
> > +       msix_capable = epc_features->msix_capable;
> >         if (msix_capable) {
> >                 ret = pci_epc_set_msix(epc, epf->func_no, epf->vfunc_no,
> >                                        epf->msix_interrupts,
> > @@ -814,11 +810,9 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
> >         void *base;
> >         enum pci_barno test_reg_bar = epf_test->test_reg_bar;
> >         enum pci_barno bar;
> > -       const struct pci_epc_features *epc_features;
> > +       const struct pci_epc_features *epc_features = epf_test->epc_features;
> >         size_t test_reg_size;
> >  
> > -       epc_features = epf_test->epc_features;
> > -
> >         test_reg_bar_size = ALIGN(sizeof(struct pci_epf_test_reg), 128);
> >  
> >         msix_capable = epc_features->msix_capable;
> > 
> > 
> > Instead?
> > 
> > That way, we assign msi_capable/msix_capable just before the if-statement
> > where it is used. (Which matches how we already assign msix_capable just
> > before the if-statement in pci_epf_test_alloc_space().)
> 
> ...or just kill the local variables:
> bool msi_capable/msix_capable in pci_epf_test_core_init(), and
> bool msix_capable pci_epf_test_alloc_space()
> and just do:
> 
> if (epc_features->msix_capable) / if (epc_features->msi_capable)
> 
> directly?
> 

Yeah, that will also work.

- Mani
Niklas Cassel April 18, 2024, 6:46 a.m. UTC | #6
On Thu, Apr 18, 2024 at 11:13:19AM +0530, Manivannan Sadhasivam wrote:
> On Wed, Apr 17, 2024 at 07:49:45PM +0200, Niklas Cassel wrote:
> > On Wed, Apr 17, 2024 at 10:47:25PM +0530, Manivannan Sadhasivam wrote:
> > > Instead of getting the epc_features from pci_epc_get_features() API, use
> > > the cached pci_epf_test::epc_features value to avoid the NULL check. Since
> > > the NULL check is already performed in pci_epf_test_bind(), having one more
> > > check in pci_epf_test_core_init() is redundant and it is not possible to
> > > hit the NULL pointer dereference. This also leads to the following smatch
> > > warning:
> > > 
> > > drivers/pci/endpoint/functions/pci-epf-test.c:784 pci_epf_test_core_init()
> > > error: we previously assumed 'epc_features' could be null (see line 747)
> > > 
> > > Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > > Closes: https://lore.kernel.org/linux-pci/024b5826-7180-4076-ae08-57d2584cca3f@moroto.mountain/
> > > Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
> > 
> > I think you forgot:
> > Fixes: a01e7214bef9 ("PCI: endpoint: Remove "core_init_notifier" flag")
> > 
> 
> No, that's not the correct fixes tag I suppose. This redudant check is
> introduced by commit, 5e50ee27d4a5 ("PCI: pci-epf-test: Add support to defer
> core initialization") and this commit removes the redundant check (fixing smatch
> warning is a side effect). So if the fixes tag needs to be added, then this
> commit should be referenced.

Well, you have a Closes: tag that links to a bug report about a smatch
warning that was introduced with 5e50ee27d4a5 ("PCI: pci-epf-test: Add
support to defer core initialization").

So if you want to reference another commit, then you should probably
drop the Closes: tag.


> 
> > 
> > > ---
> > >  drivers/pci/endpoint/functions/pci-epf-test.c | 9 ++++-----
> > >  1 file changed, 4 insertions(+), 5 deletions(-)
> > > 
> > > diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
> > > index 977fb79c1567..0d28f413cb07 100644
> > > --- a/drivers/pci/endpoint/functions/pci-epf-test.c
> > > +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
> > > @@ -743,11 +743,10 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> > >  	bool msi_capable = true;
> > >  	int ret;
> > >  
> > > -	epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
> > > -	if (epc_features) {
> > > -		msix_capable = epc_features->msix_capable;
> > > -		msi_capable = epc_features->msi_capable;
> > > -	}
> > > +	epc_features = epf_test->epc_features;
> > 
> > How about:
> > 
> > index 977fb79c1567..4d6105c07ac0 100644
> > --- a/drivers/pci/endpoint/functions/pci-epf-test.c
> > +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
> > @@ -735,7 +735,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> >  {
> >         struct pci_epf_test *epf_test = epf_get_drvdata(epf);
> >         struct pci_epf_header *header = epf->header;
> > -       const struct pci_epc_features *epc_features;
> > +       const struct pci_epc_features *epc_features = epf_test->epc_features;
> >         struct pci_epc *epc = epf->epc;
> >         struct device *dev = &epf->dev;
> >         bool linkup_notifier = false;
> > @@ -743,12 +743,6 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> >         bool msi_capable = true;
> >         int ret;
> >  
> > -       epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
> > -       if (epc_features) {
> > -               msix_capable = epc_features->msix_capable;
> > -               msi_capable = epc_features->msi_capable;
> > -       }
> > -
> >         if (epf->vfunc_no <= 1) {
> >                 ret = pci_epc_write_header(epc, epf->func_no, epf->vfunc_no, header);
> >                 if (ret) {
> > @@ -761,6 +755,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> >         if (ret)
> >                 return ret;
> >  
> > +       msi_capable = epc_features->msi_capable;
> >         if (msi_capable) {
> >                 ret = pci_epc_set_msi(epc, epf->func_no, epf->vfunc_no,
> >                                       epf->msi_interrupts);
> > @@ -770,6 +765,7 @@ static int pci_epf_test_core_init(struct pci_epf *epf)
> >                 }
> >         }
> >  
> > +       msix_capable = epc_features->msix_capable;
> >         if (msix_capable) {
> >                 ret = pci_epc_set_msix(epc, epf->func_no, epf->vfunc_no,
> >                                        epf->msix_interrupts,
> > @@ -814,11 +810,9 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
> >         void *base;
> >         enum pci_barno test_reg_bar = epf_test->test_reg_bar;
> >         enum pci_barno bar;
> > -       const struct pci_epc_features *epc_features;
> > +       const struct pci_epc_features *epc_features = epf_test->epc_features;
> >         size_t test_reg_size;
> >  
> > -       epc_features = epf_test->epc_features;
> > -
> >         test_reg_bar_size = ALIGN(sizeof(struct pci_epf_test_reg), 128);
> >  
> >         msix_capable = epc_features->msix_capable;
> > 
> > 
> > Instead?
> > 
> > That way, we assign msi_capable/msix_capable just before the if-statement
> > where it is used. (Which matches how we already assign msix_capable just
> > before the if-statement in pci_epf_test_alloc_space().)
> > 
> 
> Ok, if we go with this pattern, then pci_epf_test_set_bar() also needs to be
> updated.

pci_epf_test_set_bar() ? I presume that you mean pci_epf_test_alloc_space().

How about a 1/2 patch that modifies pci_epf_test_core_init() and Closes: the
bug report, and a 2/2 patch that modifies pci_epf_test_alloc_space() ?


Kind regards,
Niklas
Manivannan Sadhasivam April 18, 2024, 6:53 a.m. UTC | #7
On Thu, Apr 18, 2024 at 08:46:47AM +0200, Niklas Cassel wrote:
> On Thu, Apr 18, 2024 at 11:13:19AM +0530, Manivannan Sadhasivam wrote:
> > On Wed, Apr 17, 2024 at 07:49:45PM +0200, Niklas Cassel wrote:
> > > On Wed, Apr 17, 2024 at 10:47:25PM +0530, Manivannan Sadhasivam wrote:
> > > > Instead of getting the epc_features from pci_epc_get_features() API, use
> > > > the cached pci_epf_test::epc_features value to avoid the NULL check. Since
> > > > the NULL check is already performed in pci_epf_test_bind(), having one more
> > > > check in pci_epf_test_core_init() is redundant and it is not possible to
> > > > hit the NULL pointer dereference. This also leads to the following smatch
> > > > warning:
> > > > 
> > > > drivers/pci/endpoint/functions/pci-epf-test.c:784 pci_epf_test_core_init()
> > > > error: we previously assumed 'epc_features' could be null (see line 747)
> > > > 
> > > > Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > > > Closes: https://lore.kernel.org/linux-pci/024b5826-7180-4076-ae08-57d2584cca3f@moroto.mountain/
> > > > Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
> > > 
> > > I think you forgot:
> > > Fixes: a01e7214bef9 ("PCI: endpoint: Remove "core_init_notifier" flag")
> > > 
> > 
> > No, that's not the correct fixes tag I suppose. This redudant check is
> > introduced by commit, 5e50ee27d4a5 ("PCI: pci-epf-test: Add support to defer
> > core initialization") and this commit removes the redundant check (fixing smatch
> > warning is a side effect). So if the fixes tag needs to be added, then this
> > commit should be referenced.
> 
> Well, you have a Closes: tag that links to a bug report about a smatch
> warning that was introduced with 5e50ee27d4a5 ("PCI: pci-epf-test: Add
> support to defer core initialization").
> 
> So if you want to reference another commit, then you should probably
> drop the Closes: tag.
> 

Then checkpatch will complain... But I think I can keep the two tags? One is for
fixing the redudant check and another is for the smatch warning reported.

> 
> > 
> > > 

[...]

> > > That way, we assign msi_capable/msix_capable just before the if-statement
> > > where it is used. (Which matches how we already assign msix_capable just
> > > before the if-statement in pci_epf_test_alloc_space().)
> > > 
> > 
> > Ok, if we go with this pattern, then pci_epf_test_set_bar() also needs to be
> > updated.
> 
> pci_epf_test_set_bar() ? I presume that you mean pci_epf_test_alloc_space().
> 

Oops. I referred from an old branch.

> How about a 1/2 patch that modifies pci_epf_test_core_init() and Closes: the
> bug report, and a 2/2 patch that modifies pci_epf_test_alloc_space() ?
> 

Yes, that's the plan.

- Mani
Niklas Cassel April 18, 2024, 7:14 a.m. UTC | #8
On Thu, Apr 18, 2024 at 12:23:08PM +0530, Manivannan Sadhasivam wrote:
> On Thu, Apr 18, 2024 at 08:46:47AM +0200, Niklas Cassel wrote:
> > On Thu, Apr 18, 2024 at 11:13:19AM +0530, Manivannan Sadhasivam wrote:
> > > On Wed, Apr 17, 2024 at 07:49:45PM +0200, Niklas Cassel wrote:
> > > > On Wed, Apr 17, 2024 at 10:47:25PM +0530, Manivannan Sadhasivam wrote:
> > > > > Instead of getting the epc_features from pci_epc_get_features() API, use
> > > > > the cached pci_epf_test::epc_features value to avoid the NULL check. Since
> > > > > the NULL check is already performed in pci_epf_test_bind(), having one more
> > > > > check in pci_epf_test_core_init() is redundant and it is not possible to
> > > > > hit the NULL pointer dereference. This also leads to the following smatch
> > > > > warning:
> > > > > 
> > > > > drivers/pci/endpoint/functions/pci-epf-test.c:784 pci_epf_test_core_init()
> > > > > error: we previously assumed 'epc_features' could be null (see line 747)
> > > > > 
> > > > > Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > > > > Closes: https://lore.kernel.org/linux-pci/024b5826-7180-4076-ae08-57d2584cca3f@moroto.mountain/
> > > > > Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
> > > > 
> > > > I think you forgot:
> > > > Fixes: a01e7214bef9 ("PCI: endpoint: Remove "core_init_notifier" flag")
> > > > 
> > > 
> > > No, that's not the correct fixes tag I suppose. This redudant check is
> > > introduced by commit, 5e50ee27d4a5 ("PCI: pci-epf-test: Add support to defer
> > > core initialization") and this commit removes the redundant check (fixing smatch
> > > warning is a side effect). So if the fixes tag needs to be added, then this
> > > commit should be referenced.
> > 
> > Well, you have a Closes: tag that links to a bug report about a smatch
> > warning that was introduced with 5e50ee27d4a5 ("PCI: pci-epf-test: Add
> > support to defer core initialization").
> > 
> > So if you want to reference another commit, then you should probably
> > drop the Closes: tag.
> > 
> 
> Then checkpatch will complain... But I think I can keep the two tags? One is for
> fixing the redudant check and another is for the smatch warning reported.

Yes, I think so too.

You can have Fixes: to the commit that introduced the redundant check,
since this was obviously not the correct thing to do, and then perhaps
just mention commit 5e50ee27d4a5 ("PCI: pci-epf-test: Add support to
defer core initialization") somewhere in the commit log.


Kind regards,
Niklas
Manivannan Sadhasivam April 18, 2024, 7:30 a.m. UTC | #9
On Thu, Apr 18, 2024 at 09:14:23AM +0200, Niklas Cassel wrote:
> On Thu, Apr 18, 2024 at 12:23:08PM +0530, Manivannan Sadhasivam wrote:
> > On Thu, Apr 18, 2024 at 08:46:47AM +0200, Niklas Cassel wrote:
> > > On Thu, Apr 18, 2024 at 11:13:19AM +0530, Manivannan Sadhasivam wrote:
> > > > On Wed, Apr 17, 2024 at 07:49:45PM +0200, Niklas Cassel wrote:
> > > > > On Wed, Apr 17, 2024 at 10:47:25PM +0530, Manivannan Sadhasivam wrote:
> > > > > > Instead of getting the epc_features from pci_epc_get_features() API, use
> > > > > > the cached pci_epf_test::epc_features value to avoid the NULL check. Since
> > > > > > the NULL check is already performed in pci_epf_test_bind(), having one more
> > > > > > check in pci_epf_test_core_init() is redundant and it is not possible to
> > > > > > hit the NULL pointer dereference. This also leads to the following smatch
> > > > > > warning:
> > > > > > 
> > > > > > drivers/pci/endpoint/functions/pci-epf-test.c:784 pci_epf_test_core_init()
> > > > > > error: we previously assumed 'epc_features' could be null (see line 747)
> > > > > > 
> > > > > > Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > > > > > Closes: https://lore.kernel.org/linux-pci/024b5826-7180-4076-ae08-57d2584cca3f@moroto.mountain/
> > > > > > Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
> > > > > 
> > > > > I think you forgot:
> > > > > Fixes: a01e7214bef9 ("PCI: endpoint: Remove "core_init_notifier" flag")
> > > > > 
> > > > 
> > > > No, that's not the correct fixes tag I suppose. This redudant check is
> > > > introduced by commit, 5e50ee27d4a5 ("PCI: pci-epf-test: Add support to defer
> > > > core initialization") and this commit removes the redundant check (fixing smatch
> > > > warning is a side effect). So if the fixes tag needs to be added, then this
> > > > commit should be referenced.
> > > 
> > > Well, you have a Closes: tag that links to a bug report about a smatch
> > > warning that was introduced with 5e50ee27d4a5 ("PCI: pci-epf-test: Add
> > > support to defer core initialization").
> > > 
> > > So if you want to reference another commit, then you should probably
> > > drop the Closes: tag.
> > > 
> > 
> > Then checkpatch will complain... But I think I can keep the two tags? One is for
> > fixing the redudant check and another is for the smatch warning reported.
> 
> Yes, I think so too.
> 
> You can have Fixes: to the commit that introduced the redundant check,

That is 5e50ee27d4a5.

> since this was obviously not the correct thing to do, and then perhaps
> just mention commit 5e50ee27d4a5 ("PCI: pci-epf-test: Add support to
> defer core initialization") somewhere in the commit log.

You mean a01e7214bef9 here?

- Mani
Niklas Cassel April 18, 2024, 7:40 a.m. UTC | #10
On Thu, Apr 18, 2024 at 01:00:23PM +0530, Manivannan Sadhasivam wrote:
> On Thu, Apr 18, 2024 at 09:14:23AM +0200, Niklas Cassel wrote:
> > On Thu, Apr 18, 2024 at 12:23:08PM +0530, Manivannan Sadhasivam wrote:
> > > On Thu, Apr 18, 2024 at 08:46:47AM +0200, Niklas Cassel wrote:
> > > > On Thu, Apr 18, 2024 at 11:13:19AM +0530, Manivannan Sadhasivam wrote:
> > > > > On Wed, Apr 17, 2024 at 07:49:45PM +0200, Niklas Cassel wrote:
> > > > > > On Wed, Apr 17, 2024 at 10:47:25PM +0530, Manivannan Sadhasivam wrote:
> > > > > > > Instead of getting the epc_features from pci_epc_get_features() API, use
> > > > > > > the cached pci_epf_test::epc_features value to avoid the NULL check. Since
> > > > > > > the NULL check is already performed in pci_epf_test_bind(), having one more
> > > > > > > check in pci_epf_test_core_init() is redundant and it is not possible to
> > > > > > > hit the NULL pointer dereference. This also leads to the following smatch
> > > > > > > warning:
> > > > > > > 
> > > > > > > drivers/pci/endpoint/functions/pci-epf-test.c:784 pci_epf_test_core_init()
> > > > > > > error: we previously assumed 'epc_features' could be null (see line 747)
> > > > > > > 
> > > > > > > Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > > > > > > Closes: https://lore.kernel.org/linux-pci/024b5826-7180-4076-ae08-57d2584cca3f@moroto.mountain/
> > > > > > > Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
> > > > > > 
> > > > > > I think you forgot:
> > > > > > Fixes: a01e7214bef9 ("PCI: endpoint: Remove "core_init_notifier" flag")
> > > > > > 
> > > > > 
> > > > > No, that's not the correct fixes tag I suppose. This redudant check is
> > > > > introduced by commit, 5e50ee27d4a5 ("PCI: pci-epf-test: Add support to defer
> > > > > core initialization") and this commit removes the redundant check (fixing smatch
> > > > > warning is a side effect). So if the fixes tag needs to be added, then this
> > > > > commit should be referenced.
> > > > 
> > > > Well, you have a Closes: tag that links to a bug report about a smatch
> > > > warning that was introduced with 5e50ee27d4a5 ("PCI: pci-epf-test: Add
> > > > support to defer core initialization").
> > > > 
> > > > So if you want to reference another commit, then you should probably
> > > > drop the Closes: tag.
> > > > 
> > > 
> > > Then checkpatch will complain... But I think I can keep the two tags? One is for
> > > fixing the redudant check and another is for the smatch warning reported.
> > 
> > Yes, I think so too.
> > 
> > You can have Fixes: to the commit that introduced the redundant check,
> 
> That is 5e50ee27d4a5.

Yes :)


> 
> > since this was obviously not the correct thing to do, and then perhaps
> > just mention commit 5e50ee27d4a5 ("PCI: pci-epf-test: Add support to
> > defer core initialization") somewhere in the commit log.
> 
> You mean a01e7214bef9 here?

Yes :)
(I copied the wrong SHA1 here...)


Kind regards,
Niklas
diff mbox series

Patch

diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
index 977fb79c1567..0d28f413cb07 100644
--- a/drivers/pci/endpoint/functions/pci-epf-test.c
+++ b/drivers/pci/endpoint/functions/pci-epf-test.c
@@ -743,11 +743,10 @@  static int pci_epf_test_core_init(struct pci_epf *epf)
 	bool msi_capable = true;
 	int ret;
 
-	epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
-	if (epc_features) {
-		msix_capable = epc_features->msix_capable;
-		msi_capable = epc_features->msi_capable;
-	}
+	epc_features = epf_test->epc_features;
+
+	msix_capable = epc_features->msix_capable;
+	msi_capable = epc_features->msi_capable;
 
 	if (epf->vfunc_no <= 1) {
 		ret = pci_epc_write_header(epc, epf->func_no, epf->vfunc_no, header);