Re: [PATCH v5 06/11] drm/gpuvm: Add DRM_GPUVA_REPEAT flag and logic

From: Danilo Krummrich

Date: Thu Mar 26 2026 - 19:22:27 EST


On Fri Mar 13, 2026 at 4:09 PM CET, Adrián Larumbe wrote:
> From: Asahi Lina <lina+kernel@xxxxxxxxxxxxx>
>
> To be able to support "fake sparse" mappings without relying on GPU page
> fault handling, drivers may need to create large (e.g. 4GiB) mappings of
> the same page repeatedly (or same range of pages). Doing this through
> individual mappings would be very wasteful. This can be handled better
> by using a flag on map creation, but to do it safely, drm_gpuvm needs to
> be aware of this special case.
>
> Add a flag that signals that a given mapping is a page mapping, which is
> repeated all over the entire requested VA range. This tweaks the
> sm_map() logic to treat the GEM offsets differently when mappings are
> a repeated ones so they are not incremented as they would be with regular
> mappings.
>
> The size of the GEM portion to repeat is passed through
> drm_gpuva::gem::range. Most of the time it will be a page size, but
> it can be bigger as long as it's less than drm_gpuva::va::range, and
> drm_gpuva::va::range is a multiple of drm_gpuva::gem::range.
>
> Signed-off-by: Asahi Lina <lina+kernel@xxxxxxxxxxxxx>
> Signed-off-by: Caterina Shablia <caterina.shablia@xxxxxxxxxxxxx>

In v4 I also mentioned:

I also think this feature deserves its own section in the global GPUVM
documentation -- please add a corresponding paragraph.

Also it seems that we need to update the documentation which shows all potential
cases when calling __drm_gpuvm_sm_map() [1].

[1] https://docs.kernel.org/gpu/drm-mm.html#split-and-merge

Why did this not happen?

> +static int validate_map_request(struct drm_gpuvm *gpuvm,
> + const struct drm_gpuva_op_map *op)
> +{
> + if (unlikely(!drm_gpuvm_range_valid(gpuvm, op->va.addr, op->va.range)))
> + return -EINVAL;
> +
> + if (op->flags & DRM_GPUVA_REPEAT) {
> + u64 va_range = op->va.range;
> +
> + /* For a repeated mapping, GEM range must be > 0
> + * and a multiple of the VA range.
> + */
> + if (unlikely(!op->gem.repeat_range ||
> + va_range < op->gem.repeat_range ||
> + do_div(va_range, op->gem.repeat_range)))
> + return -EINVAL;
> + }
> +
> + if (op->flags & DRM_GPUVA_INVALIDATED)
> + return -EINVAL;

This seems unrelated to this patch.

> +
> + return 0;
> +}
> +
> static int
> __drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm,
> const struct drm_gpuvm_ops *ops, void *priv,
> @@ -2429,7 +2475,8 @@ __drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm,
> u64 req_end = req_addr + req_range;
> int ret;
>
> - if (unlikely(!drm_gpuvm_range_valid(gpuvm, req_addr, req_range)))
> + ret = validate_map_request(gpuvm, &req->map);
> + if (unlikely(ret))
> return -EINVAL;
>
> drm_gpuvm_for_each_va_range_safe(va, next, gpuvm, req_addr, req_end) {
> @@ -2463,7 +2510,9 @@ __drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm,
> .va.addr = req_end,
> .va.range = range - req_range,
> .gem.obj = obj,
> - .gem.offset = offset + req_range,
> + .gem.repeat_range = va->gem.repeat_range,
> + .gem.offset = offset +
> + (va->flags & DRM_GPUVA_REPEAT ? 0 : req_range),

This seems to be a repeating pattern, can we factor this into a helper?