Re: [PATCH v2 2/4] misc: pci_endpoint_test: Fix the return value of IOCTL

Damien Le Moal
2024-11-29T10:51:33Z

On 11/29/24 18:24, Manivannan Sadhasivam wrote:
> IOCTLs are supposed to return 0 for success and negative error codes for
> failure. Currently, this driver is returning 0 for failure and 1 for
> success, that's not correct. Hence, fix it!
>
> Reported-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Closes: https://lore.kernel.org/all/YvzNg5ROnxEApDgS@kroah.com
> Fixes: 2c156ac71c6b ("misc: Add host side PCI driver for PCI test function device")
> Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>

Looks OK to me.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

One nit below.

[...]

> static void pci_endpoint_test_remove(struct pci_dev *pdev)
> diff --git a/tools/pci/pcitest.c b/tools/pci/pcitest.c
> index 470258009ddc..545e04ad63a2 100644
> --- a/tools/pci/pcitest.c
> +++ b/tools/pci/pcitest.c
> @@ -16,7 +16,6 @@
>
> #include <linux/pcitest.h>
>
> -static char *result[] = { "NOT OKAY", "OKAY" };
> static char *irq[] = { "LEGACY", "MSI", "MSI-X" };
>
> struct pci_test {
> @@ -52,63 +51,65 @@ static int run_test(struct pci_test *test)
> ret = ioctl(fd, PCITEST_BAR, test->barnum);
> fprintf(stdout, "BAR%d:\t\t", test->barnum);
> if (ret < 0)
> - fprintf(stdout, "TEST FAILED\n");
> + fprintf(stdout, "NOT OKAY\n");
> else
> - fprintf(stdout, "%s\n", result[ret]);
> + fprintf(stdout, "OKAY\n");

Maybe replace all this "if (ret < 0) ... else ..." and all the ones below with
something a call to:

static void test_result(int ret)
{
fprintf(stdout, "%sOKAY\n", ret < 0 ? "NOT " : "");
}

or simply with the call:

fprintf(stdout, "%sOKAY\n", ret < 0 ? "NOT " : "");

to avoid all these repetition.

> }
>
> if (test->set_irqtype) {
> ret = ioctl(fd, PCITEST_SET_IRQTYPE, test->irqtype);
> fprintf(stdout, "SET IRQ TYPE TO %s:\t\t", irq[test->irqtype]);
> if (ret < 0)
> - fprintf(stdout, "FAILED\n");
> + fprintf(stdout, "NOT OKAY\n");
> else
> - fprintf(stdout, "%s\n", result[ret]);
> + fprintf(stdout, "OKAY\n");
> }
>
> if (test->get_irqtype) {
> ret = ioctl(fd, PCITEST_GET_IRQTYPE);
> fprintf(stdout, "GET IRQ TYPE:\t\t");
> - if (ret < 0)
> - fprintf(stdout, "FAILED\n");
> - else
> + if (ret < 0) {
> + fprintf(stdout, "NOT OKAY\n");
> + } else {
> fprintf(stdout, "%s\n", irq[ret]);
> + ret = 0;
> + }
> }
>
> if (test->clear_irq) {
> ret = ioctl(fd, PCITEST_CLEAR_IRQ);
> fprintf(stdout, "CLEAR IRQ:\t\t");
> if (ret < 0)
> - fprintf(stdout, "FAILED\n");
> + fprintf(stdout, "NOT OKAY\n");
> else
> - fprintf(stdout, "%s\n", result[ret]);
> + fprintf(stdout, "OKAY\n");
> }
>
> if (test->legacyirq) {
> ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
> fprintf(stdout, "LEGACY IRQ:\t");
> if (ret < 0)
> - fprintf(stdout, "TEST FAILED\n");
> + fprintf(stdout, "NOT OKAY\n");
> else
> - fprintf(stdout, "%s\n", result[ret]);
> + fprintf(stdout, "OKAY\n");
> }
>
> if (test->msinum > 0 && test->msinum <= 32) {
> ret = ioctl(fd, PCITEST_MSI, test->msinum);
> fprintf(stdout, "MSI%d:\t\t", test->msinum);
> if (ret < 0)
> - fprintf(stdout, "TEST FAILED\n");
> + fprintf(stdout, "NOT OKAY\n");
> else
> - fprintf(stdout, "%s\n", result[ret]);
> + fprintf(stdout, "OKAY\n");
> }
>
> if (test->msixnum > 0 && test->msixnum <= 2048) {
> ret = ioctl(fd, PCITEST_MSIX, test->msixnum);
> fprintf(stdout, "MSI-X%d:\t\t", test->msixnum);
> if (ret < 0)
> - fprintf(stdout, "TEST FAILED\n");
> + fprintf(stdout, "NOT OKAY\n");
> else
> - fprintf(stdout, "%s\n", result[ret]);
> + fprintf(stdout, "OKAY\n");
> }
>
> if (test->write) {
> @@ -118,9 +119,9 @@ static int run_test(struct pci_test *test)
> ret = ioctl(fd, PCITEST_WRITE, &param);
> fprintf(stdout, "WRITE (%7ld bytes):\t\t", test->size);
> if (ret < 0)
> - fprintf(stdout, "TEST FAILED\n");
> + fprintf(stdout, "NOT OKAY\n");
> else
> - fprintf(stdout, "%s\n", result[ret]);
> + fprintf(stdout, "OKAY\n");
> }
>
> if (test->read) {
> @@ -130,9 +131,9 @@ static int run_test(struct pci_test *test)
> ret = ioctl(fd, PCITEST_READ, &param);
> fprintf(stdout, "READ (%7ld bytes):\t\t", test->size);
> if (ret < 0)
> - fprintf(stdout, "TEST FAILED\n");
> + fprintf(stdout, "NOT OKAY\n");
> else
> - fprintf(stdout, "%s\n", result[ret]);
> + fprintf(stdout, "OKAY\n");
> }
>
> if (test->copy) {
> @@ -142,14 +143,14 @@ static int run_test(struct pci_test *test)
> ret = ioctl(fd, PCITEST_COPY, &param);
> fprintf(stdout, "COPY (%7ld bytes):\t\t", test->size);
> if (ret < 0)
> - fprintf(stdout, "TEST FAILED\n");
> + fprintf(stdout, "NOT OKAY\n");
> else
> - fprintf(stdout, "%s\n", result[ret]);
> + fprintf(stdout, "OKAY\n");
> }
>
> fflush(stdout);
> close(fd);
> - return (ret < 0) ? ret : 1 - ret; /* return 0 if test succeeded */
> + return ret;
> }
>
> int main(int argc, char **argv)

--
Damien Le Moal
Western Digital Research