drm: Preserve SHMLBA bits in hash key for _DRM_SHM mappings.
Subsystems (2)
PATCH 1/1 drm: Preserve SHMLBA bits in hash key for _DRM_SHM mappings.
Ok, this is the last DRM bug I am aware of with Radeon on sparc64. What kills me is that I fixed this bug 6 or 7 years ago :)
--------------------
drm: Preserve SHMLBA bits in hash key for _DRM_SHM mappings.
Platforms such as sparc64 have D-cache aliasing issues. We cannot allow virtual mappings in different contexts to be such that two cache lines can be loaded for the same backing data. Updates to one cache line won't be seen by accesses to the other cache line.
Code in sparc64 and other architectures solve this problem by making sure that all userland mappings of MAP_SHARED objects have the same virtual address base. They implement this by keying off of the page offset, and using that to choose a suitably consistent virtual address for mmap() requests.
Making things even worse, getting this wrong on sparc64 can result in hangs during DRM lock acquisition. This is because, at least on UltraSPARC-III, normal loads consult the D-cache but atomics such as 'cas' (which is what cmpxchg() is implement using) only consult the L2 cache. So if a D-cache alias is inserted, the load can see different data than the atomic, and we'll loop forever because the atomic compare-and-exchange will never complete successfully.
So to make this all work properly, we need to make sure that the hash address computed by drm_map_handle() preserves the SHMLBA relevant bits, and that's what this patch does for _DRM_SHM mappings.
As a historical note, many years ago this bug didn't exist because we used to just use the low 32-bits of the address as the hash and just hope for the best. This preserved the SHMLBA bits properly. But when the hashtab code was added to DRM, this was no longer the case.
--- drivers/gpu/drm/drm_bufs.c | 35 +++++++++++++++++++++++++++++++---- 1 files changed, 31 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_bufs.c +31 -4
@@ -34,6 +34,8 @@*/ #include <linux/vmalloc.h> +#include <linux/log2.h> +#include <asm/shmparam.h> #include "drmP.h" resource_size_t drm_get_resource_start (struct drm_device *dev,unsigned int resource)
@@ -83,9 +85,11 @@ static struct drm_map_list *drm_find_matching_map(struct drm_device *dev, }static int drm_map_handle (struct drm_device *dev,struct drm_hash_item *hash, -unsigned long user_token,int hashed_handle) +unsigned long user_token,int hashed_handle,int shm) { -int use_hashed_handle; +int use_hashed_handle, shift; +unsigned long add; +#if (BITS_PER_LONG ==64 ) use_hashed_handle = ((user_token &0xFFFFFFFF00000000UL ) || hashed_handle);#elif (BITS_PER_LONG ==32 )
@@ -101,9 +105,31 @@ static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,if (ret != -EINVAL )return ret; } + + shift =0 ; + add =DRM_MAP_HASH_OFFSET >>PAGE_SHIFT ; +if (shm && (SHMLBA >PAGE_SIZE )) { +int bits =ilog2 (SHMLBA >>PAGE_SHIFT ) +1 ; + +/* For shared memory, we have to preserve the SHMLBA +* bits of the eventual vma->vm_pgoff value during +* mmap(). Otherwise we run into cache aliasing problems +* on some platforms. On these platforms, the pgoff of +* a mmap() request is used to pick a suitable virtual +* address for the mmap() region such that it will not +* cause cache aliasing problems. +* +* Therefore, make sure the SHMLBA relevant bits of the +* hash value we use are equal to those in the original +* kernel virtual address. +*/ + shift = bits; + add |= ((user_token >>PAGE_SHIFT ) & ((1UL << bits) -1UL )); + } +return drm_ht_just_insert_please (&dev->map_hash, hash, user_token,32 -PAGE_SHIFT -3 , -0 ,DRM_MAP_HASH_OFFSET >>PAGE_SHIFT ); + shift, add); }/**
@@ -323,7 +349,8 @@ static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,/* We do it here so that dev->struct_mutex protects the increment */ user_token = (map->type == _DRM_SHM) ? (unsigned long )map->handle : map->offset; - ret =drm_map_handle (dev, &list->hash, user_token,0 ); + ret =drm_map_handle (dev, &list->hash, user_token,0 , + (map->type == _DRM_SHM));if (ret) {if (map->type == _DRM_REGISTERS)iounmap (map->handle);
Signature · 1 line
1.6.1.2.350.g88cc
Quoted 34 lines · On Wed, 18 Feb 2009 15:41:02 -0800 (PST) David Miller <[email protected]>…
Platforms such as sparc64 have D-cache aliasing issues. We cannot allow virtual mappings in different contexts to be such that two cache lines can be loaded for the same backing data. Updates to one cache line won't be seen by accesses to the other cache line.
Code in sparc64 and other architectures solve this problem by making sure that all userland mappings of
MAP_SHAREDobjects have the same virtual address base. They implement this by keying off of the page offset, and using that to choose a suitably consistent virtual address formmap()requests.Making things even worse, getting this wrong on sparc64 can result in hangs during DRM lock acquisition. This is because, at least on UltraSPARC-III, normal loads consult the D-cache but atomics such as 'cas' (which is what
cmpxchg()is implement using) only consult the L2 cache. So if a D-cache alias is inserted, the load can see different data than the atomic, and we'll loop forever because the atomic compare-and-exchange will never complete successfully.So to make this all work properly, we need to make sure that the hash address computed by
drm_map_handle()preserves the SHMLBA relevant bits, and that's what this patch does for_DRM_SHMmappings.As a historical note, many years ago this bug didn't exist because we used to just use the low 32-bits of the address as the hash and just hope for the best. This preserved the SHMLBA bits properly. But when the hashtab code was added to DRM, this was no longer the case.
...
#include <
linux/vmalloc.h>
drivers/gpu/drm/drm_bufs.c line 39
Quoted 3 lines
drivers/gpu/drm/drm_bufs.c +2
-0+#include <linux/log2.h> +#include <asm/shmparam.h> #include "drmP.h"
The inclusion of asm/shmparam.h direct from a driver is a bit risky. It assumes that asm/shmparam.h is compileable in isolation from the additional things which include/linux/shm.h includes. In particular, asm/page.h.
eg:
arch/xtensa/include/asm/shmparam.h
#define SHMLBA ((PAGE_SIZE > DCACHE_WAY_SIZE)? PAGE_SIZE : DCACHE_WAY_SIZE)
But including linux/shm.h here seems a bit silly. We'll see..
From: Andrew Morton <[email protected]>
Date: Thu, 19 Feb 2009 15:27:26 -0800
Quoted 7 lines
eg:
arch/xtensa/include/asm/shmparam.h
#define SHMLBA ((PAGE_SIZE>DCACHE_WAY_SIZE)?PAGE_SIZE:DCACHE_WAY_SIZE)But including
linux/shm.hhere seems a bit silly. We'll see..
If DRM even builds on XTENSA, let alone is usable there, I'll buy you a lollipop.
Quoted 5 lines · On Fri, 20 Feb 2009 00:54:14 -0800 (PST) David Miller <[email protected]>…
From: Andrew Morton <[email protected]>
Date: Thu, 19 Feb 2009 15:27:26 -0800Quoted 2 lines
eg:
Quoted 8 lines
Quoted 5 lines
arch/xtensa/include/asm/shmparam.h
#define SHMLBA ((PAGE_SIZE>DCACHE_WAY_SIZE)?PAGE_SIZE:DCACHE_WAY_SIZE)But including
linux/shm.hhere seems a bit silly. We'll see..If DRM even builds on XTENSA, let alone is usable there, I'll buy you a lollipop.
heh, OK. But the risk is there.
<tries to test sparc32 allmodconfig>
scripts/mod/empty.c:1: error: -m64 is not supported by this configuration scripts/mod/empty.c:1: error: -mlong-double-64 not allowed with -m64 scripts/mod/empty.c:1: error: -mcmodel= is not supported on 32 bit systems
<retires hurt>