Skip to content

Commit f07069d

Browse files
drm/ttm: move memory accounting into vmwgfx v4
This is just another feature which is only used by VMWGFX, so move it into the driver instead. I've tried to add the accounting sysfs file to the kobject of the drm minor, but I'm not 100% sure if this works as expected. v2: fix typo in KFD and avoid 64bit divide v3: fix init order in VMWGFX v4: use pdev sysfs reference instead of drm Signed-off-by: Christian König <[email protected]> Reviewed-by: Zack Rusin <[email protected]> (v3) Tested-by: Nirmoy Das <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent d4bd777 commit f07069d

22 files changed

+111
-116
lines changed

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
118118
*/
119119
#define ESTIMATE_PT_SIZE(mem_size) ((mem_size) >> 14)
120120

121+
static size_t amdgpu_amdkfd_acc_size(uint64_t size)
122+
{
123+
size >>= PAGE_SHIFT;
124+
size *= sizeof(dma_addr_t) + sizeof(void *);
125+
126+
return __roundup_pow_of_two(sizeof(struct amdgpu_bo)) +
127+
__roundup_pow_of_two(sizeof(struct ttm_tt)) +
128+
PAGE_ALIGN(size);
129+
}
130+
121131
static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
122132
uint64_t size, u32 domain, bool sg)
123133
{
@@ -126,8 +136,7 @@ static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
126136
size_t acc_size, system_mem_needed, ttm_mem_needed, vram_needed;
127137
int ret = 0;
128138

129-
acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
130-
sizeof(struct amdgpu_bo));
139+
acc_size = amdgpu_amdkfd_acc_size(size);
131140

132141
vram_needed = 0;
133142
if (domain == AMDGPU_GEM_DOMAIN_GTT) {
@@ -174,8 +183,7 @@ static void unreserve_mem_limit(struct amdgpu_device *adev,
174183
{
175184
size_t acc_size;
176185

177-
acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
178-
sizeof(struct amdgpu_bo));
186+
acc_size = amdgpu_amdkfd_acc_size(size);
179187

180188
spin_lock(&kfd_mem_limit.mem_limit_lock);
181189
if (domain == AMDGPU_GEM_DOMAIN_GTT) {

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
523523
};
524524
struct amdgpu_bo *bo;
525525
unsigned long page_align, size = bp->size;
526-
size_t acc_size;
527526
int r;
528527

529528
/* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
@@ -546,9 +545,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
546545

547546
*bo_ptr = NULL;
548547

549-
acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
550-
sizeof(struct amdgpu_bo));
551-
552548
bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
553549
if (bo == NULL)
554550
return -ENOMEM;
@@ -577,8 +573,8 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
577573
bo->tbo.priority = 1;
578574

579575
r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
580-
&bo->placement, page_align, &ctx, acc_size,
581-
NULL, bp->resv, &amdgpu_bo_destroy);
576+
&bo->placement, page_align, &ctx, NULL,
577+
bp->resv, &amdgpu_bo_destroy);
582578
if (unlikely(r != 0))
583579
return r;
584580

drivers/gpu/drm/drm_gem_vram_helper.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
189189
struct drm_vram_mm *vmm = dev->vram_mm;
190190
struct ttm_device *bdev;
191191
int ret;
192-
size_t acc_size;
193192

194193
if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
195194
return ERR_PTR(-EINVAL);
@@ -216,7 +215,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
216215
}
217216

218217
bdev = &vmm->bdev;
219-
acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
220218

221219
gbo->bo.bdev = bdev;
222220
drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
@@ -226,8 +224,8 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
226224
* to release gbo->bo.base and kfree gbo.
227225
*/
228226
ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
229-
&gbo->placement, pg_align, false, acc_size,
230-
NULL, NULL, ttm_buffer_object_destroy);
227+
&gbo->placement, pg_align, false, NULL, NULL,
228+
ttm_buffer_object_destroy);
231229
if (ret)
232230
return ERR_PTR(ret);
233231

drivers/gpu/drm/nouveau/nouveau_bo.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,15 @@ nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
300300
struct sg_table *sg, struct dma_resv *robj)
301301
{
302302
int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
303-
size_t acc_size;
304303
int ret;
305304

306-
acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, sizeof(*nvbo));
307-
308305
nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
309306
nouveau_bo_placement_set(nvbo, domain, 0);
310307
INIT_LIST_HEAD(&nvbo->io_reserve_lru);
311308

312309
ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
313-
&nvbo->placement, align >> PAGE_SHIFT, false,
314-
acc_size, sg, robj, nouveau_bo_del_ttm);
310+
&nvbo->placement, align >> PAGE_SHIFT, false, sg,
311+
robj, nouveau_bo_del_ttm);
315312
if (ret) {
316313
/* ttm will call nouveau_bo_del_ttm if it fails.. */
317314
return ret;

drivers/gpu/drm/nouveau/nouveau_drv.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
#include <drm/ttm/ttm_bo_api.h>
5555
#include <drm/ttm/ttm_bo_driver.h>
5656
#include <drm/ttm/ttm_placement.h>
57-
#include <drm/ttm/ttm_memory.h>
5857

5958
#include <drm/drm_audio_component.h>
6059

drivers/gpu/drm/qxl/qxl_object.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ int qxl_bo_create(struct qxl_device *qdev,
138138
qxl_ttm_placement_from_domain(bo, domain);
139139

140140
r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
141-
&bo->placement, 0, &ctx, size,
142-
NULL, NULL, &qxl_ttm_bo_destroy);
141+
&bo->placement, 0, &ctx, NULL, NULL,
142+
&qxl_ttm_bo_destroy);
143143
if (unlikely(r != 0)) {
144144
if (r != -ERESTARTSYS)
145145
dev_err(qdev->ddev.dev,

drivers/gpu/drm/radeon/radeon_object.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ int radeon_bo_create(struct radeon_device *rdev,
159159
struct radeon_bo *bo;
160160
enum ttm_bo_type type;
161161
unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
162-
size_t acc_size;
163162
int r;
164163

165164
size = ALIGN(size, PAGE_SIZE);
@@ -173,9 +172,6 @@ int radeon_bo_create(struct radeon_device *rdev,
173172
}
174173
*bo_ptr = NULL;
175174

176-
acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
177-
sizeof(struct radeon_bo));
178-
179175
bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
180176
if (bo == NULL)
181177
return -ENOMEM;
@@ -230,8 +226,8 @@ int radeon_bo_create(struct radeon_device *rdev,
230226
/* Kernel allocation are uninterruptible */
231227
down_read(&rdev->pm.mclk_lock);
232228
r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
233-
&bo->placement, page_align, !kernel, acc_size,
234-
sg, resv, &radeon_ttm_bo_destroy);
229+
&bo->placement, page_align, !kernel, sg, resv,
230+
&radeon_ttm_bo_destroy);
235231
up_read(&rdev->pm.mclk_lock);
236232
if (unlikely(r != 0)) {
237233
return r;

drivers/gpu/drm/ttm/Makefile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
#
33
# Makefile for the drm device driver. This driver provides support for the
44

5-
ttm-y := ttm_memory.o ttm_tt.o ttm_bo.o \
6-
ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
7-
ttm_execbuf_util.o ttm_range_manager.o \
8-
ttm_resource.o ttm_pool.o ttm_device.o
5+
ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
6+
ttm_execbuf_util.o ttm_range_manager.o ttm_resource.o ttm_pool.o \
7+
ttm_device.o
98
ttm-$(CONFIG_AGP) += ttm_agp_backend.o
109

1110
obj-$(CONFIG_DRM_TTM) += ttm.o

drivers/gpu/drm/ttm/ttm_bo.c

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,6 @@ static void ttm_bo_release(struct kref *kref)
425425
struct ttm_buffer_object *bo =
426426
container_of(kref, struct ttm_buffer_object, kref);
427427
struct ttm_device *bdev = bo->bdev;
428-
size_t acc_size = bo->acc_size;
429428
int ret;
430429

431430
if (!bo->deleted) {
@@ -485,7 +484,6 @@ static void ttm_bo_release(struct kref *kref)
485484
if (!ttm_bo_uses_embedded_gem_object(bo))
486485
dma_resv_fini(&bo->base._resv);
487486
bo->destroy(bo);
488-
ttm_mem_global_free(&ttm_mem_glob, acc_size);
489487
}
490488

491489
void ttm_bo_put(struct ttm_buffer_object *bo)
@@ -1046,25 +1044,13 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
10461044
struct ttm_placement *placement,
10471045
uint32_t page_alignment,
10481046
struct ttm_operation_ctx *ctx,
1049-
size_t acc_size,
10501047
struct sg_table *sg,
10511048
struct dma_resv *resv,
10521049
void (*destroy) (struct ttm_buffer_object *))
10531050
{
1054-
struct ttm_mem_global *mem_glob = &ttm_mem_glob;
10551051
bool locked;
10561052
int ret = 0;
10571053

1058-
ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1059-
if (ret) {
1060-
pr_err("Out of kernel memory\n");
1061-
if (destroy)
1062-
(*destroy)(bo);
1063-
else
1064-
kfree(bo);
1065-
return -ENOMEM;
1066-
}
1067-
10681054
bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
10691055

10701056
kref_init(&bo->kref);
@@ -1081,7 +1067,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
10811067
bo->mem.bus.addr = NULL;
10821068
bo->moving = NULL;
10831069
bo->mem.placement = 0;
1084-
bo->acc_size = acc_size;
10851070
bo->pin_count = 0;
10861071
bo->sg = sg;
10871072
if (resv) {
@@ -1142,7 +1127,6 @@ int ttm_bo_init(struct ttm_device *bdev,
11421127
struct ttm_placement *placement,
11431128
uint32_t page_alignment,
11441129
bool interruptible,
1145-
size_t acc_size,
11461130
struct sg_table *sg,
11471131
struct dma_resv *resv,
11481132
void (*destroy) (struct ttm_buffer_object *))
@@ -1151,8 +1135,7 @@ int ttm_bo_init(struct ttm_device *bdev,
11511135
int ret;
11521136

11531137
ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1154-
page_alignment, &ctx, acc_size,
1155-
sg, resv, destroy);
1138+
page_alignment, &ctx, sg, resv, destroy);
11561139
if (ret)
11571140
return ret;
11581141

@@ -1163,20 +1146,6 @@ int ttm_bo_init(struct ttm_device *bdev,
11631146
}
11641147
EXPORT_SYMBOL(ttm_bo_init);
11651148

1166-
size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
1167-
unsigned long bo_size,
1168-
unsigned struct_size)
1169-
{
1170-
unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1171-
size_t size = 0;
1172-
1173-
size += ttm_round_pot(struct_size);
1174-
size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1175-
size += ttm_round_pot(sizeof(struct ttm_tt));
1176-
return size;
1177-
}
1178-
EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1179-
11801149
/*
11811150
* buffer object vm functions.
11821151
*/

drivers/gpu/drm/ttm/ttm_bo_util.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
309309

310310
kref_init(&fbo->base.kref);
311311
fbo->base.destroy = &ttm_transfered_destroy;
312-
fbo->base.acc_size = 0;
313312
fbo->base.pin_count = 0;
314313
if (bo->type != ttm_bo_type_sg)
315314
fbo->base.base.resv = &fbo->base.base._resv;

drivers/gpu/drm/ttm/ttm_device.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727

2828
#define pr_fmt(fmt) "[TTM DEVICE] " fmt
2929

30+
#include <linux/mm.h>
31+
3032
#include <drm/ttm/ttm_device.h>
31-
#include <drm/ttm/ttm_memory.h>
33+
#include <drm/ttm/ttm_tt.h>
3234
#include <drm/ttm/ttm_placement.h>
35+
#include <drm/ttm/ttm_bo_api.h>
3336

3437
#include "ttm_module.h"
3538

@@ -49,7 +52,9 @@ static void ttm_global_release(void)
4952
if (--ttm_glob_use_count > 0)
5053
goto out;
5154

52-
ttm_mem_global_release(&ttm_mem_glob);
55+
ttm_pool_mgr_fini();
56+
ttm_tt_mgr_fini();
57+
5358
__free_page(glob->dummy_read_page);
5459
memset(glob, 0, sizeof(*glob));
5560
out:
@@ -59,16 +64,23 @@ static void ttm_global_release(void)
5964
static int ttm_global_init(void)
6065
{
6166
struct ttm_global *glob = &ttm_glob;
67+
unsigned long num_pages;
68+
struct sysinfo si;
6269
int ret = 0;
6370
unsigned i;
6471

6572
mutex_lock(&ttm_global_mutex);
6673
if (++ttm_glob_use_count > 1)
6774
goto out;
6875

69-
ret = ttm_mem_global_init(&ttm_mem_glob);
70-
if (ret)
71-
goto out;
76+
si_meminfo(&si);
77+
78+
/* Limit the number of pages in the pool to about 50% of the total
79+
* system memory.
80+
*/
81+
num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
82+
ttm_pool_mgr_init(num_pages * 50 / 100);
83+
ttm_tt_mgr_init();
7284

7385
spin_lock_init(&glob->lru_lock);
7486
glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);

drivers/gpu/drm/ttm/ttm_pool.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,16 +404,10 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
404404
caching = pages + (1 << order);
405405
}
406406

407-
r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
408-
(1 << order) * PAGE_SIZE,
409-
ctx);
410-
if (r)
411-
goto error_free_page;
412-
413407
if (dma_addr) {
414408
r = ttm_pool_map(pool, order, p, &dma_addr);
415409
if (r)
416-
goto error_global_free;
410+
goto error_free_page;
417411
}
418412

419413
num_pages -= 1 << order;
@@ -427,9 +421,6 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
427421

428422
return 0;
429423

430-
error_global_free:
431-
ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE);
432-
433424
error_free_page:
434425
ttm_pool_free_page(pool, tt->caching, order, p);
435426

@@ -464,8 +455,6 @@ void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
464455

465456
order = ttm_pool_page_order(pool, p);
466457
num_pages = 1ULL << order;
467-
ttm_mem_global_free_page(&ttm_mem_glob, p,
468-
num_pages * PAGE_SIZE);
469458
if (tt->dma_address)
470459
ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
471460

drivers/gpu/drm/vmwgfx/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o vmwgfx_kms.o vmwgfx_drv.o \
99
vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
1010
vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
1111
vmwgfx_validation.o vmwgfx_page_dirty.o vmwgfx_streamoutput.o \
12-
ttm_object.o ttm_lock.o
12+
ttm_object.o ttm_lock.o ttm_memory.o
1313

1414
vmwgfx-$(CONFIG_TRANSPARENT_HUGEPAGE) += vmwgfx_thp.o
1515
obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o

0 commit comments

Comments
 (0)