drm/vmwgfx: Store cursor source mappings in plane_state
Subsystems (3)
PATCH 1/3 drm/vmwgfx: Store cursor source mappings in plane_state
We aren't tracking the map/unmap sequences correctly for the cursor plane's source buffer mapping during atomic commits. Instead relying on ref counting in vmw_bo_map_and_cache to paper over these mistakes.
map_and_cache is not thread safe and requires the BO to be reserved but we shouldn't reserve, or map, in atomic_update where we incorrectly assume the mapping is always cached.
Under stress test like IGT's kms_cursor_legacy the unmap at the start of prepare_fb can unmap the mapping and the map_and_cache in atomic_update will either need to redo the mapping or will crash because the mapping was removed while in use.
The best solution is simply to move the mapping to the plane_state and not duplicate it ensuring the each mapping is local to each atomic commit.
--- drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c | 84 +++++--------------- drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.h | 4 +- drivers/gpu/drm/vmwgfx/vmwgfx_kms.c | 1 + 3 files changed, 25 insertions(+), 64 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c +22 -62
@@ -112,10 +112,12 @@ static void vmw_cursor_update_mob(struct vmw_private *vmw, {SVGAGBCursorHeader *header;SVGAGBAlphaCursorHeader *alpha_header; -struct vmw_bo *bo =vmw_user_object_buffer (&vps->uo); -u32 *image =vmw_bo_map_and_cache (bo); +bool is_iomem; +u32 *image =ttm_kmap_obj_virtual (&vps->cursor.src_map, &is_iomem);const u32 image_size = vps->base.crtc_w * vps->base.crtc_h *sizeof (*image); +WARN_ON (is_iomem || !image); + header =vmw_bo_map_and_cache (vps->cursor.mob); alpha_header = &header->header.alphaHeader;
@@ -132,7 +134,6 @@ static void vmw_cursor_update_mob(struct vmw_private *vmw,memcpy (header +1 , image, image_size);vmw_write (vmw,SVGA_REG_CURSOR_MOBID ,vmw_bo_mobid (vps->cursor.mob)); -vmw_bo_unmap (bo);vmw_bo_unmap (vps->cursor.mob); }
@@ -469,9 +470,16 @@ vmw_cursor_plane_cleanup_fb(struct drm_plane *plane, {struct vmw_cursor_plane *vcp =vmw_plane_to_vcp (plane);struct vmw_plane_state *vps =vmw_plane_state_to_vps (old_state); +struct ttm_buffer_object *bo = vps->cursor.src_map.bo; +int ret; -if (!vmw_user_object_is_null (&vps->uo)) -vmw_user_object_unmap (&vps->uo); +if (bo) { + ret =ttm_bo_reserve (bo, false, false,NULL ); +ttm_bo_kunmap (&vps->cursor.src_map); +ttm_bo_unpin (bo); +if (ret ==0 ) +ttm_bo_unreserve (bo); + }vmw_cursor_mob_unmap (vps);vmw_cursor_mob_put (vcp, vps);
@@ -488,7 +496,6 @@ vmw_cursor_buffer_changed(struct vmw_plane_state *new_vps,struct vmw_bo *old_bo =vmw_user_object_buffer (&old_vps->uo);struct vmw_surface *surf;bool dirty = false; -int ret;if (new_bo != old_bo)return true;
@@ -507,52 +514,6 @@ vmw_cursor_buffer_changed(struct vmw_plane_state *new_vps,vmw_bo_dirty_clear (new_bo); }return dirty; - }else if (new_bo != old_bo) { -/* -* Currently unused because the top exits right away. -* In most cases buffer being different will mean -* that the contents is different. For the few percent -* of cases where that's not true the cost of doing -* the memcmp on all other seems to outweight the -* benefits. Leave the conditional to be able to -* trivially validate it by removing the initial -* if (new_bo != old_bo) at the start. -*/ -void *old_image; -void *new_image; -bool changed = false; -struct ww_acquire_ctx ctx; -const u32 size = new_vps->base.crtc_w * - new_vps->base.crtc_h *sizeof (u32); - -ww_acquire_init (&ctx, &reservation_ww_class); - - ret =ttm_bo_reserve (&old_bo->tbo, false, false, &ctx); -if (ret !=0 ) { -ww_acquire_fini (&ctx); -return true; - } - - ret =ttm_bo_reserve (&new_bo->tbo, false, false, &ctx); -if (ret !=0 ) { -ttm_bo_unreserve (&old_bo->tbo); -ww_acquire_fini (&ctx); -return true; - } - - old_image =vmw_bo_map_and_cache (old_bo); - new_image =vmw_bo_map_and_cache (new_bo); - -if (old_image && new_image && old_image != new_image) - changed =memcmp (old_image, new_image, size) != -0 ; - -ttm_bo_unreserve (&new_bo->tbo); -ttm_bo_unreserve (&old_bo->tbo); - -ww_acquire_fini (&ctx); - -return changed; }return false; }
@@ -605,7 +566,6 @@ int vmw_cursor_plane_prepare_fb(struct drm_plane *plane,int ret =0 ;if (!vmw_user_object_is_null (&vps->uo)) { -vmw_user_object_unmap (&vps->uo);vmw_user_object_unref (&vps->uo); }
@@ -630,6 +590,7 @@ int vmw_cursor_plane_prepare_fb(struct drm_plane *plane,case VMW_CURSOR_UPDATE_MOB : { bo =vmw_user_object_buffer (&vps->uo);if (bo) { +u32 size;struct ttm_operation_ctx ctx = { false, false }; ret =ttm_bo_reserve (&bo->tbo, true, false,NULL );
@@ -644,18 +605,16 @@ int vmw_cursor_plane_prepare_fb(struct drm_plane *plane,* vmw_bo_pin_reserved also validates, so to skip * the extra validation use ttm_bo_pin directly */ -if (!bo->tbo.pin_count) -ttm_bo_pin (&bo->tbo); +ttm_bo_pin (&bo->tbo);if (vmw_framebuffer_to_vfb (fb)->bo) { -const u32 size = new_state->crtc_w * - new_state->crtc_h * -sizeof (u32); - - (void )vmw_bo_map_and_cache_size (bo, size); + size = new_state->crtc_w * + new_state->crtc_h * +sizeof (u32); }else { -vmw_bo_map_and_cache (bo); + size = bo->tbo.base.size; } +ttm_bo_kmap (&bo->tbo,0 ,PFN_UP (size), &vps->cursor.src_map);ttm_bo_unreserve (&bo->tbo); }if (!vmw_user_object_is_null (&vps->uo)) {
@@ -770,7 +729,8 @@ vmw_cursor_plane_atomic_update(struct drm_plane *plane,case VMW_CURSOR_UPDATE_GB_ONLY : bo =vmw_user_object_buffer (&vps->uo);if (bo) - vmw_send_define_cursor_cmd(dev_priv, bo->map.virtual, + vmw_send_define_cursor_cmd(dev_priv, + vps->cursor.src_map.virtual, vps->base.crtc_w, vps->base.crtc_h, vps->base.hotspot_x,
drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.h +2 -2
@@ -13,6 +13,7 @@#include "drm/drm_file.h" #include "drm/drm_fourcc.h" #include "drm/drm_plane.h" +#include <drm/ttm/ttm_bo.h> #include <linux/types.h>
@@ -39,8 +40,7 @@ enum vmw_cursor_update_type {struct vmw_cursor_plane_state {enum vmw_cursor_update_type update_type; -bool changed; -bool surface_changed; +struct ttm_bo_kmap_obj src_map;struct vmw_bo *mob;struct {s32 hotspot_x;
drivers/gpu/drm/vmwgfx/vmwgfx_kms.c +1 -0
@@ -274,6 +274,7 @@ vmw_du_plane_duplicate_state(struct drm_plane *plane) vps->pinned =0 ; vps->cpp =0 ; +memset (&vps->cursor.src_map,0 ,sizeof (vps->cursor.src_map)); vps->cursor.mob =NULL ;/* Each ref counted resource needs to be acquired again */
Signature · 1 line
2.53.0
PATCH 2/3 drm/vmwgfx: Check for cursor plane changes earlier in prepare_fb
We can avoid extra work if we check for changes earlier in prepare_fb. This avoids mapping a number of BOs.
--- drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c | 21 ++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c +11 -10
@@ -588,6 +588,17 @@ int vmw_cursor_plane_prepare_fb(struct drm_plane *plane,break ;case VMW_CURSOR_UPDATE_GB_ONLY :case VMW_CURSOR_UPDATE_MOB : { +if (!vmw_user_object_is_null (&vps->uo)) { +if (!vmw_cursor_plane_changed (vps, old_vps) && + !vmw_cursor_buffer_changed (vps, old_vps)) { + vps->cursor.update_type =VMW_CURSOR_UPDATE_NONE ; +return 0 ; + } +if (vps->cursor.update_type ==VMW_CURSOR_UPDATE_MOB ) { +vmw_cursor_mob_get (vcp, vps); +vmw_cursor_mob_map (vps); + } + } bo =vmw_user_object_buffer (&vps->uo);if (bo) {u32 size;
@@ -617,16 +628,6 @@ int vmw_cursor_plane_prepare_fb(struct drm_plane *plane,ttm_bo_kmap (&bo->tbo,0 ,PFN_UP (size), &vps->cursor.src_map);ttm_bo_unreserve (&bo->tbo); } -if (!vmw_user_object_is_null (&vps->uo)) { -if (!vmw_cursor_plane_changed (vps, old_vps) && - !vmw_cursor_buffer_changed (vps, old_vps)) { - vps->cursor.update_type = -VMW_CURSOR_UPDATE_NONE ; - }else { -vmw_cursor_mob_get (vcp, vps); -vmw_cursor_mob_map (vps); - } - } }break ;case VMW_CURSOR_UPDATE_NONE :
Signature · 1 line
2.53.0
Zack Rusin
maintainerRe: [PATCH 2/3] drm/vmwgfx: Check for cursor plane changes earlier in prepare_fb
Quoted 49 lines · Ian Forbes
We can avoid extra work if we check for changes earlier in
prepare_fb. This avoids mapping a number of BOs.Signed-off-by Ian Forbes <[email protected]>--- drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c | 21 ++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c +11
-11@@ -588,6 +588,17 @@ int vmw_cursor_plane_prepare_fb(struct drm_plane *plane,break ;case VMW_CURSOR_UPDATE_GB_ONLY :case VMW_CURSOR_UPDATE_MOB : { +if (!vmw_user_object_is_null (&vps->uo)) { +if (!vmw_cursor_plane_changed (vps, old_vps) && + !vmw_cursor_buffer_changed (vps, old_vps)) { + vps->cursor.update_type =VMW_CURSOR_UPDATE_NONE ; +return 0 ; + } +if (vps->cursor.update_type ==VMW_CURSOR_UPDATE_MOB ) { +vmw_cursor_mob_get (vcp, vps); +vmw_cursor_mob_map (vps); + } + } bo =vmw_user_object_buffer (&vps->uo);if (bo) {u32 size;@@ -617,16 +628,6 @@ int vmw_cursor_plane_prepare_fb(struct drm_plane *plane,ttm_bo_kmap (&bo->tbo,0 ,PFN_UP (size), &vps->cursor.src_map);ttm_bo_unreserve (&bo->tbo); } -if (!vmw_user_object_is_null (&vps->uo)) { -if (!vmw_cursor_plane_changed (vps, old_vps) && - !vmw_cursor_buffer_changed (vps, old_vps)) { - vps->cursor.update_type = -VMW_CURSOR_UPDATE_NONE ; - }else { -vmw_cursor_mob_get (vcp, vps); -vmw_cursor_mob_map (vps); - } - } }break ;case VMW_CURSOR_UPDATE_NONE : --
Quoted 2 lines
2.53.0
That's going to leak if ttm_bo_reserve or ttm_bo_validate fail. cleanup_fb will only be called if prepared_fb succeeds. So, you'll either need to skip this patch or create local unwind logic for vmw_cursor_mob_get.
z
PATCH 3/3 drm/vmwgfx: Cache cursor MOB mappings
In reply to Ian ForbesWe map/unmap these for every cursor update. These mappings are small (64*64*4=16K) and pinned so we can cache them.
--- drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c | 74 ++------------------ 1 file changed, 4 insertions(+), 70 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c +4 -70
@@ -156,36 +156,11 @@ static void vmw_cursor_mob_destroy(struct vmw_bo **vbo)if (!(*vbo))return ; +vmw_bo_unmap (*vbo);ttm_bo_unpin (&(*vbo)->tbo);vmw_bo_unreference (vbo); } -/** -* vmw_cursor_mob_unmap - Unmaps the cursor mobs. -* -* @vps: state of the cursor plane -* -* Returns 0 on success -*/ - -static int -vmw_cursor_mob_unmap (struct vmw_plane_state *vps) -{ -int ret =0 ; -struct vmw_bo *vbo = vps->cursor.mob; - -if (!vbo || !vbo->map.virtual) -return 0 ; - - ret =ttm_bo_reserve (&vbo->tbo, true, false,NULL ); -if (likely (ret ==0 )) { -vmw_bo_unmap (vbo); -ttm_bo_unreserve (&vbo->tbo); - } - -return ret; -} -static void vmw_cursor_mob_put (struct vmw_cursor_plane *vcp,struct vmw_plane_state *vps) {
@@ -194,7 +169,6 @@ static void vmw_cursor_mob_put(struct vmw_cursor_plane *vcp,if (!vps->cursor.mob)return ; -vmw_cursor_mob_unmap (vps);/* Look for a free slot to return this mob to the cache. */ for (i =0 ; i <ARRAY_SIZE (vcp->cursor_mobs); i++) {
@@ -257,7 +231,7 @@ static int vmw_cursor_mob_get(struct vmw_cursor_plane *vcp,return 0 ; } } -/* Create a new mob if we can't find an existing one. */ +/* Create a new pinned mob if we can't find an existing one. */ ret =vmw_bo_create_and_populate (dev_priv, size,VMW_BO_DOMAIN_MOB , &vps->cursor.mob);
@@ -274,14 +248,13 @@ static int vmw_cursor_mob_get(struct vmw_cursor_plane *vcp,ttm_bo_unreserve (&vps->cursor.mob->tbo); goto teardown; } - +vmw_bo_map_and_cache (vps->cursor.mob);dma_fence_wait (&fence->base, false);dma_fence_put (&fence->base);ttm_bo_unreserve (&vps->cursor.mob->tbo);return 0 ; - teardown:vmw_cursor_mob_destroy (&vps->cursor.mob);return ret;
@@ -418,42 +391,6 @@ void vmw_cursor_plane_destroy(struct drm_plane *plane)drm_plane_cleanup (plane); } -/** -* vmw_cursor_mob_map - Maps the cursor mobs. -* -* @vps: plane_state -* -* Returns 0 on success -*/ - -static int -vmw_cursor_mob_map (struct vmw_plane_state *vps) -{ -int ret; -u32 size =vmw_cursor_mob_size (vps->cursor.update_type, - vps->base.crtc_w, vps->base.crtc_h); -struct vmw_bo *vbo = vps->cursor.mob; - -if (!vbo) -return -EINVAL ; - -if (vbo->tbo.base.size < size) -return -EINVAL ; - -if (vbo->map.virtual) -return 0 ; - - ret =ttm_bo_reserve (&vbo->tbo, false, false,NULL ); -if (unlikely (ret !=0 )) -return -ENOMEM ; - -vmw_bo_map_and_cache (vbo); - -ttm_bo_unreserve (&vbo->tbo); - -return 0 ; -} -/** * vmw_cursor_plane_cleanup_fb - Unpins the plane surface *
@@ -481,7 +418,6 @@ vmw_cursor_plane_cleanup_fb(struct drm_plane *plane,ttm_bo_unreserve (bo); } -vmw_cursor_mob_unmap (vps);vmw_cursor_mob_put (vcp, vps);vmw_du_plane_unpin_surf (vps);
@@ -594,10 +530,8 @@ int vmw_cursor_plane_prepare_fb(struct drm_plane *plane, vps->cursor.update_type =VMW_CURSOR_UPDATE_NONE ;return 0 ; } -if (vps->cursor.update_type ==VMW_CURSOR_UPDATE_MOB ) { +if (vps->cursor.update_type ==VMW_CURSOR_UPDATE_MOB )vmw_cursor_mob_get (vcp, vps); -vmw_cursor_mob_map (vps); - } } bo =vmw_user_object_buffer (&vps->uo);if (bo) {
Signature · 1 line
2.53.0
Zack Rusin
maintainerQuoted 55 lines · Ian Forbes
We aren't tracking the map/unmap sequences correctly for the cursor plane's source buffer mapping during atomic commits. Instead relying on ref counting in
vmw_bo_map_and_cacheto paper over these mistakes.
map_and_cacheis not thread safe and requires the BO to be reserved but we shouldn't reserve, or map, inatomic_updatewhere we incorrectly assume the mapping is always cached.Under stress test like IGT's
kms_cursor_legacythe unmap at the start ofprepare_fbcan unmap the mapping and themap_and_cacheinatomic_updatewill either need to redo the mapping or will crash because the mapping was removed while in use.The best solution is simply to move the mapping to the
plane_stateand not duplicate it ensuring the each mapping is local to each atomic commit.Fixes 965544150d1c ("drm/vmwgfx: Refactor cursor handling")Signed-off-by Ian Forbes <[email protected]>--- drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c | 84 +++++--------------- drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.h | 4 +- drivers/gpu/drm/vmwgfx/vmwgfx_kms.c | 1 + 3 files changed, 25 insertions(+), 64 deletions(-)drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c +4
-3@@ -112,10 +112,12 @@ static void vmw_cursor_update_mob(struct vmw_private *vmw, {SVGAGBCursorHeader *header;SVGAGBAlphaCursorHeader *alpha_header; -struct vmw_bo *bo =vmw_user_object_buffer (&vps->uo); -u32 *image =vmw_bo_map_and_cache (bo); +bool is_iomem; +u32 *image =ttm_kmap_obj_virtual (&vps->cursor.src_map, &is_iomem);const u32 image_size = vps->base.crtc_w * vps->base.crtc_h *sizeof (*image); +WARN_ON (is_iomem || !image); + header =vmw_bo_map_and_cache (vps->cursor.mob); alpha_header = &header->header.alphaHeader;@@ -132,7 +134,6 @@ static void vmw_cursor_update_mob(struct vmw_private *vmw,memcpy (header +1 , image, image_size);vmw_write (vmw,SVGA_REG_CURSOR_MOBID ,vmw_bo_mobid (vps->cursor.mob)); -vmw_bo_unmap (bo);vmw_bo_unmap (vps->cursor.mob); }@@ -469,9 +470,16 @@ vmw_cursor_plane_cleanup_fb(struct drm_plane *plane, {struct vmw_cursor_plane *vcp =vmw_plane_to_vcp (plane);
drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c vmw_cursor_plane_cleanup_fb(struct drm_plane *plane, · line 479
Quoted 10 lines
drivers/gpu/drm/vmwgfx/vmwgfx_cursor_plane.c +6
-2struct vmw_plane_state *vps =vmw_plane_state_to_vps (old_state); +struct ttm_buffer_object *bo = vps->cursor.src_map.bo; +int ret; -if (!vmw_user_object_is_null (&vps->uo)) -vmw_user_object_unmap (&vps->uo); +if (bo) { + ret =ttm_bo_reserve (bo, false, false,NULL ); +ttm_bo_kunmap (&vps->cursor.src_map); +ttm_bo_unpin (bo);
Minor nit: if ttm_bo_reserve fails then this ttm_bo_unpin is going to be a problem because unpin requires a reservation. Minor because ttm_bo_reserve is unlikely to fail here but we should still handle it.
z