Thank you for your contribution! Sashiko AI review found 2 potential issue(s) 
to consider:
- [Medium] selftests/proc: 32-bit off_t truncation in proc-remove-mapped mmap()
- [Low] selftests/proc: unused len parameter in mem_bar() in proc-remove-mapped
--

commit df303f424b9a370ffbd0eec6c07ff2a99d0ee4ea
Author: Christian Brauner <[email protected]>

selftests/proc: check that a mapped entry can be removed

This commit adds a procfs selftest that maps a PCI device memory BAR via
/proc/bus/pci and removes the PCI device through sysfs to verify that removing
a mapped procfs entry does not block and leaves the file returning -EIO on
subsequent reads.

> diff --git a/tools/testing/selftests/proc/proc-remove-mapped.c 
> b/tools/testing/selftests/proc/proc-remove-mapped.c
> new file mode 100644
> index 0000000000000..92860fb2a8f40
> --- /dev/null
> +++ b/tools/testing/selftests/proc/proc-remove-mapped.c

[ ... ]

> +/* the first memory BAR of @dev */
> +static int mem_bar(const char *dev, unsigned long long *start,
> +                unsigned long long *len)
> +{
> +     unsigned long long s, e, flags;
> +     char path[PATH_MAX];
> +     int ret = -ENODEV;
> +     FILE *f;
> +
> +     snprintf(path, sizeof(path), SYSFS_DEVICES "%s/resource", dev);
> +     f = fopen(path, "r");
> +     if (!f)
> +             return -errno;
> +     while (fscanf(f, "%llx %llx %llx", &s, &e, &flags) == 3) {
> +             if ((flags & IORESOURCE_MEM) && e >= s) {
> +                     *start = s;
> +                     *len = e - s + 1;
> +                     ret = 0;
> +                     break;
> +             }
> +     }
> +     fclose(f);
> +     return ret;
> +}

[ ... ]

> +TEST_F(pci, remove_while_mapped)
> +{
> +     long psz = sysconf(_SC_PAGESIZE);
> +     unsigned long long start = 0, len = 0;
> +     char path[PATH_MAX];
> +     volatile unsigned int *p;
> +     unsigned int v;
> +     int fd;
> +
> +     if (mem_bar(self->dev, &start, &len))
> +             SKIP(return, "%s has no memory BAR", self->dev);

[Severity: Low]
This isn't a bug, but is the len parameter in mem_bar() needed? mem_bar()
writes *len = e - s + 1 and remove_while_mapped() passes &len, but len is
never read afterward because mmap() maps a single page of size psz.

> +
> +     fd = open(self->path, O_RDONLY | O_CLOEXEC);
> +     ASSERT_GE(fd, 0);
> +     if (ioctl(fd, PCIIOC_MMAP_IS_MEM)) {
> +             /* no PCI mmap on this architecture, or locked down */
> +             if (errno == EINVAL || errno == EPERM) {
> +                     close(fd);
> +                     SKIP(return, "PCIIOC_MMAP_IS_MEM: %s", strerror(errno));
> +             }
> +             ASSERT_TRUE(false) {
> +                     TH_LOG("PCIIOC_MMAP_IS_MEM: %s", strerror(errno));
> +             }
> +     }
> +     p = mmap(NULL, psz, PROT_READ, MAP_SHARED, fd, start);
> +     if (p == MAP_FAILED)
> +             SKIP(return, "mmap: %s", strerror(errno));

[Severity: Medium]
On 32-bit architectures, will passing start to mmap() in remove_while_mapped()
truncate or sign-extend PCI BAR addresses at or above 0x80000000?

Neither proc-remove-mapped.c nor tools/testing/selftests/proc/Makefile defines
_FILE_OFFSET_BITS to 64, so off_t is a 32-bit signed integer on 32-bit builds.
When mem_bar() parses a BAR physical address >= 0x80000000 into unsigned long
long start, converting start to a 32-bit signed off_t in mmap() either
truncates bits above 32 or produces a negative offset (which fails with EINVAL
in musl, or sign-extends the page offset passed to mmap2 in glibc so
pci_mmap_fits() in proc_bus_pci_mmap() fails with ENODEV and causes the test
to skip).

Should proc-remove-mapped.c define _FILE_OFFSET_BITS 64 before including the
system headers?

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=20

Reply via email to