Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] drm/ttm: stale beneficial-order failure flag on resumed restore
  in ttm_pool_restore_and_alloc()
--

commit 5e2ee65385b61ac88536ccf836b08b653cbf5979
Author: Matthew Brost <[email protected]>

drm/ttm: Preallocate beneficial-order defrag pages outside the lock

This commit adds out-of-lock preallocation of beneficial-order pages for
TTM defragmentation moves and records the number of sub-optimal pages on
each ttm_tt at populate time.

> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
> index 79538d776dde3..fa76f880d5632 100644
> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
[ ... ]
> @@ -1384,7 +1628,7 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, 
> struct ttm_tt *tt,
>               gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
>  
>               tt->page_flags &= ~TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED;
> -             ttm_pool_alloc_state_init(tt, &alloc);
> +             ttm_pool_alloc_state_init(pool, tt, &alloc);
>               if (ctx->gfp_retry_mayfail)
>                       gfp |= __GFP_RETRY_MAYFAIL;
>  
> @@ -1414,11 +1658,16 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, 
> struct ttm_tt *tt,
>                       kfree(tt->restore);
>                       tt->restore = NULL;
>  
> +                     tt->nr_suboptimal_pages = alloc.nr_suboptimal_pages;
>                       return 0;
>               }
>       }
>  
> -     return __ttm_pool_alloc(pool, tt, ctx, &alloc, restore);
> +     ret = __ttm_pool_alloc(pool, tt, ctx, &alloc, restore);
> +     if (!ret)
> +             tt->nr_suboptimal_pages = alloc.nr_suboptimal_pages;
> +
> +     return ret;
>  }

[Severity: Low]
Can a resumed restore finish with TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED set
on tt->page_flags while tt->nr_suboptimal_pages is 0?

During a restore, ttm_pool_iter_lower_order() sets
TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED directly on tt->page_flags before the
lower-order fallback chunk is allocated, restored, and committed to
restore->snapshot_alloc.  If ttm_pool_restore_commit() is then interrupted
at restore->restored_pages == 0, it frees the uncommitted fallback page
without advancing restore->snapshot_alloc:

drivers/gpu/drm/ttm/ttm_pool.c:ttm_pool_restore_commit() {
    ...
        if (ret) {
                if (!restore->restored_pages) {
                        dma_addr_t *dma_addr = alloc->dma_addr ? 
&restore->first_dma : NULL;

                        ttm_pool_unmap_and_free(restore->pool, 
restore->alloced_page,
                                                dma_addr, 
restore->page_caching);
                        restore->restored_pages = nr;
                }
                return ret;
        }
    ...
}

When ttm_pool_restore_and_alloc() is called again to resume, it reloads
restore->snapshot_alloc without clearing
TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED.  If the resumed pass then succeeds at
the beneficial order, the restore completes with
TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED still set and tt->nr_suboptimal_pages
== 0, causing drivers to repeatedly queue an already optimal BO for
defragmentation.

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

Reply via email to