Patch series "virtio-mem: disallow mapping virtio-mem memory via /dev/mem", v5. Let's add the basic infrastructure to exclude some physical memory regions marked as "IORESOURCE_SYSTEM_RAM" completely from /dev/mem access, even though they are not marked IORESOURCE_BUSY and even though "iomem=relaxed" is set. Resource IORESOURCE_EXCLUSIVE for that purpose instead of adding new flags to express something similar to "soft-busy" or "not busy yet, but already prepared by a driver and not to be mapped by user space". Use it for virtio-mem, to disallow mapping any virtio-mem memory via /dev/mem to user space after the virtio-mem driver was loaded. This patch (of 3): We end up traversing subtrees of ranges we are not interested in; let's optimize this case, skipping such subtrees, cleaning up the function a bit. For example, in the following configuration (/proc/iomem): 00000000-00000fff : Reserved 00001000-00057fff : System RAM 00058000-00058fff : Reserved 00059000-0009cfff : System RAM 0009d000-000fffff : Reserved 000a0000-000bffff : PCI Bus 0000:00 000c0000-000c3fff : PCI Bus 0000:00 000c4000-000c7fff : PCI Bus 0000:00 000c8000-000cbfff : PCI Bus 0000:00 000cc000-000cffff : PCI Bus 0000:00 000d0000-000d3fff : PCI Bus 0000:00 000d4000-000d7fff : PCI Bus 0000:00 000d8000-000dbfff : PCI Bus 0000:00 000dc000-000dffff : PCI Bus 0000:00 000e0000-000e3fff : PCI Bus 0000:00 000e4000-000e7fff : PCI Bus 0000:00 000e8000-000ebfff : PCI Bus 0000:00 000ec000-000effff : PCI Bus 0000:00 000f0000-000fffff : PCI Bus 0000:00 000f0000-000fffff : System ROM 00100000-3fffffff : System RAM 40000000-403fffff : Reserved 40000000-403fffff : pnp 00:00 40400000-80a79fff : System RAM ... We don't have to look at any children of "0009d000-000fffff : Reserved" if we can just skip these 15 items directly because the parent range is not of interest.
Showing the changes to kernel (1 of 1 file). Show all
Unified · Split
| @@ -73,6 +73,18 @@ static struct resource *next_resource(struct resource *p) · 72 unmodified lines | |||
| 73 | return p->sibling; | 73 | return p->sibling; |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | static struct resource *next_resource_skip_children(struct resource *p) | ||
| 77 | { | ||
| 78 | while (!p->sibling && p->parent) | ||
| 79 | p = p->parent; | ||
| 80 | return p->sibling; | ||
| 81 | } | ||
| 82 | |||
| 83 | #define for_each_resource(_root, _p, _skip_children) \ | ||
| 84 | for ((_p) = (_root)->child; (_p); \ | ||
| 85 | (_p) = (_skip_children) ? next_resource_skip_children(_p) : \ | ||
| 86 | next_resource(_p)) | ||
| 87 | |||
| 76 | static void *r_next(struct seq_file *m, void *v, loff_t *pos) | 88 | static void *r_next(struct seq_file *m, void *v, loff_t *pos) |
| 77 | { | 89 | { |
| 78 | struct resource *p = v; | 90 | struct resource *p = v; |
| @@ -1712,10 +1724,9 @@ static int strict_iomem_checks; · 1633 unmodified lines | |||
| 1712 | */ | 1724 | */ |
| 1713 | bool iomem_is_exclusive(u64 addr) | 1725 | bool iomem_is_exclusive(u64 addr) |
| 1714 | { | 1726 | { |
| 1715 | struct resource *p = &iomem_resource; | 1727 | bool skip_children = false, err = false; |
| 1716 | bool err = false; | ||
| 1717 | loff_t l; | ||
| 1718 | int size = PAGE_SIZE; | 1728 | int size = PAGE_SIZE; |
| 1729 | struct resource *p; | ||
| 1719 | 1730 | ||
| 1720 | if (!strict_iomem_checks) | 1731 | if (!strict_iomem_checks) |
| 1721 | return false; | 1732 | return false; |
| @@ -1723,15 +1734,19 @@ bool iomem_is_exclusive(u64 addr) · 1 unmodified line | |||
| 1723 | addr = addr & PAGE_MASK; | 1734 | addr = addr & PAGE_MASK; |
| 1724 | 1735 | ||
| 1725 | read_lock(&resource_lock); | 1736 | read_lock(&resource_lock); |
| 1726 | for (p = p->child; p ; p = r_next(NULL, p, &l)) { | 1737 | for_each_resource(&iomem_resource, p, skip_children) { |
| 1727 | /* | 1738 | /* |
| 1728 | * We can probably skip the resources without | 1739 | * We can probably skip the resources without |
| 1729 | * IORESOURCE_IO attribute? | 1740 | * IORESOURCE_IO attribute? |
| 1730 | */ | 1741 | */ |
| 1731 | if (p->start >= addr + size) | 1742 | if (p->start >= addr + size) |
| 1732 | break; | 1743 | break; |
| 1733 | if (p->end < addr) | 1744 | if (p->end < addr) { |
| 1745 | skip_children = true; | ||
| 1734 | continue; | 1746 | continue; |
| 1747 | } | ||
| 1748 | skip_children = false; | ||
| 1749 | |||
| 1735 | /* | 1750 | /* |
| 1736 | * A resource is exclusive if IORESOURCE_EXCLUSIVE is set | 1751 | * A resource is exclusive if IORESOURCE_EXCLUSIVE is set |
| 1737 | * or CONFIG_IO_STRICT_DEVMEM is enabled and the | 1752 | * or CONFIG_IO_STRICT_DEVMEM is enabled and the |