Skip to content

Commit 4e03b58

Browse files
mchehabrodrigovivi
authored andcommitted
drm/xe/uapi: Reject bo creation of unaligned size
For xe bo creation we request passing size which matches system or vram minimum page alignment. This way we want to ensure userspace is aware of region constraints and not aligned allocations will be rejected returning EINVAL. v2: - Rebase, Update uAPI documentation. (Thomas) v3: - Adjust the dma-buf kunit test accordingly. (Thomas) v4: - Fixed rebase conflicts and updated commit message. (Francois) Signed-off-by: Mauro Carvalho Chehab <[email protected]> Signed-off-by: Zbigniew Kempczyński <[email protected]> Signed-off-by: Thomas Hellström <[email protected]> Reviewed-by: Maarten Lankhorst <[email protected]> Signed-off-by: Francois Dugast <[email protected]> Reviewed-by: José Roberto de Souza <[email protected]> Signed-off-by: Rodrigo Vivi <[email protected]>
1 parent 2bec307 commit 4e03b58

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

drivers/gpu/drm/xe/tests/xe_dma_buf.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,21 @@ static void xe_test_dmabuf_import_same_driver(struct xe_device *xe)
109109
struct drm_gem_object *import;
110110
struct dma_buf *dmabuf;
111111
struct xe_bo *bo;
112+
size_t size;
112113

113114
/* No VRAM on this device? */
114115
if (!ttm_manager_type(&xe->ttm, XE_PL_VRAM0) &&
115116
(params->mem_mask & XE_BO_CREATE_VRAM0_BIT))
116117
return;
117118

119+
size = PAGE_SIZE;
120+
if ((params->mem_mask & XE_BO_CREATE_VRAM0_BIT) &&
121+
xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K)
122+
size = SZ_64K;
123+
118124
kunit_info(test, "running %s\n", __func__);
119-
bo = xe_bo_create_user(xe, NULL, NULL, PAGE_SIZE, DRM_XE_GEM_CPU_CACHING_WC,
120-
ttm_bo_type_device, params->mem_mask);
125+
bo = xe_bo_create_user(xe, NULL, NULL, size, DRM_XE_GEM_CPU_CACHING_WC,
126+
ttm_bo_type_device, XE_BO_CREATE_USER_BIT | params->mem_mask);
121127
if (IS_ERR(bo)) {
122128
KUNIT_FAIL(test, "xe_bo_create() failed with err=%ld\n",
123129
PTR_ERR(bo));

drivers/gpu/drm/xe/xe_bo.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,7 @@ struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
12221222
};
12231223
struct ttm_placement *placement;
12241224
uint32_t alignment;
1225+
size_t aligned_size;
12251226
int err;
12261227

12271228
/* Only kernel objects should set GT */
@@ -1232,23 +1233,30 @@ struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
12321233
return ERR_PTR(-EINVAL);
12331234
}
12341235

1235-
if (!bo) {
1236-
bo = xe_bo_alloc();
1237-
if (IS_ERR(bo))
1238-
return bo;
1239-
}
1240-
12411236
if (flags & (XE_BO_CREATE_VRAM_MASK | XE_BO_CREATE_STOLEN_BIT) &&
12421237
!(flags & XE_BO_CREATE_IGNORE_MIN_PAGE_SIZE_BIT) &&
12431238
xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) {
1244-
size = ALIGN(size, SZ_64K);
1239+
aligned_size = ALIGN(size, SZ_64K);
1240+
if (type != ttm_bo_type_device)
1241+
size = ALIGN(size, SZ_64K);
12451242
flags |= XE_BO_INTERNAL_64K;
12461243
alignment = SZ_64K >> PAGE_SHIFT;
1244+
12471245
} else {
1248-
size = ALIGN(size, PAGE_SIZE);
1246+
aligned_size = ALIGN(size, SZ_4K);
1247+
flags &= ~XE_BO_INTERNAL_64K;
12491248
alignment = SZ_4K >> PAGE_SHIFT;
12501249
}
12511250

1251+
if (type == ttm_bo_type_device && aligned_size != size)
1252+
return ERR_PTR(-EINVAL);
1253+
1254+
if (!bo) {
1255+
bo = xe_bo_alloc();
1256+
if (IS_ERR(bo))
1257+
return bo;
1258+
}
1259+
12521260
bo->tile = tile;
12531261
bo->size = size;
12541262
bo->flags = flags;
@@ -1566,7 +1574,7 @@ struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile
15661574
struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
15671575
const void *data, size_t size, u32 flags)
15681576
{
1569-
struct xe_bo *bo = xe_managed_bo_create_pin_map(xe, tile, size, flags);
1577+
struct xe_bo *bo = xe_managed_bo_create_pin_map(xe, tile, ALIGN(size, PAGE_SIZE), flags);
15701578

15711579
if (IS_ERR(bo))
15721580
return bo;

include/uapi/drm/xe_drm.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,13 @@ struct drm_xe_query_mem_region {
207207
*
208208
* When the kernel allocates memory for this region, the
209209
* underlying pages will be at least @min_page_size in size.
210-
*
211-
* Important note: When userspace allocates a GTT address which
212-
* can point to memory allocated from this region, it must also
213-
* respect this minimum alignment. This is enforced by the
214-
* kernel.
210+
* Buffer objects with an allowable placement in this region must be
211+
* created with a size aligned to this value.
212+
* GPU virtual address mappings of (parts of) buffer objects that
213+
* may be placed in this region must also have their GPU virtual
214+
* address and range aligned to this value.
215+
* Affected IOCTLS will return %-EINVAL if alignment restrictions are
216+
* not met.
215217
*/
216218
__u32 min_page_size;
217219
/**
@@ -517,9 +519,8 @@ struct drm_xe_gem_create {
517519
__u64 extensions;
518520

519521
/**
520-
* @size: Requested size for the object
521-
*
522-
* The (page-aligned) allocated size for the object will be returned.
522+
* @size: Size of the object to be created, must match region
523+
* (system or vram) minimum alignment (&min_page_size).
523524
*/
524525
__u64 size;
525526

0 commit comments

Comments
 (0)