drm: Round size of SHM maps to PAGE_SIZE

Currently, userspace can fail to obtain the SAREA mapping (among other
reasons) if it passes SAREA_MAX to drmAddMap without aligning it to the
page size. This breaks for example on PowerPC with 64K pages and radeon
despite the kernel radeon actually doing the right rouding in the first
place.

The way SAREA_MAX is defined with a bunch of ifdef's and duplicated
between libdrm and the X server is gross, ultimately it should be
retrieved by userspace from the kernel, but in the meantime, we have
plenty of existing userspace built with bad values that need to work.

This patch works around broken userspace by rounding the requested size
in drm_addmap_core() of any SHM map to the page size. Since the backing
memory for SHM maps is also allocated within addmap_core, there is no
danger of adjacent memory being exposed due to the increased map size.
The only side effect is that drivers that previously tried to create or
access SHM maps using a size < PAGE_SIZE and failed (getting -EINVAL),
will now succeed at the cost of a little bit more memory used if that
happens to be when the map is created.

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>

authored by Benjamin Herrenschmidt and committed by Jesse Barnes b6741377 279e677f

+8
+8
drivers/gpu/drm/drm_bufs.c
··· 170 } 171 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n", 172 (unsigned long long)map->offset, map->size, map->type); 173 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) { 174 drm_free(map, sizeof(*map), DRM_MEM_MAPS); 175 return -EINVAL;
··· 170 } 171 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n", 172 (unsigned long long)map->offset, map->size, map->type); 173 + 174 + /* page-align _DRM_SHM maps. They are allocated here so there is no security 175 + * hole created by that and it works around various broken drivers that use 176 + * a non-aligned quantity to map the SAREA. --BenH 177 + */ 178 + if (map->type == _DRM_SHM) 179 + map->size = PAGE_ALIGN(map->size); 180 + 181 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) { 182 drm_free(map, sizeof(*map), DRM_MEM_MAPS); 183 return -EINVAL;