summaryrefslogtreecommitdiff
path: root/kernel/cgroup/drm.c
blob: 71c0cbd117ef3263165ad954238d456b057973ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright 2023 Intel
 * Partially based on the rdma and misc controllers, which bear the following copyrights:
 *
 * Copyright 2020 Google LLC
 * Copyright (C) 2016 Parav Pandit <pandit.parav@gmail.com>
 */

#include <linux/cgroup.h>
#include <linux/cgroup_drm.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/parser.h>
#include <linux/slab.h>

#include <drm/drm_device.h>
#include <drm/drm_drv.h>
#include <drm/drm_file.h>
#include <drm/drm_managed.h>

struct drmcgroup_state {
	struct cgroup_subsys_state css;

	struct list_head pools;
};

struct drmcgroup_pool_state {
	struct drmcgroup_device *device;
	struct drmcgroup_resource {
		s64 max, used;
	} resources[DRMCG_MAX_REGIONS];

	s64 usage_sum;

	struct list_head	css_node;
	struct list_head	dev_node;
};

static DEFINE_MUTEX(drmcg_mutex);
static LIST_HEAD(drmcg_devices);

static inline struct drmcgroup_state *
css_to_drmcs(struct cgroup_subsys_state *css)
{
	return container_of(css, struct drmcgroup_state, css);
}

static inline struct drmcgroup_state *get_current_drmcg(void)
{
	return css_to_drmcs(task_get_css(current, drm_cgrp_id));
}

static struct drmcgroup_state *parent_drmcg(struct drmcgroup_state *cg)
{
	return cg->css.parent ? css_to_drmcs(cg->css.parent) : NULL;
}

static void free_cg_pool_locked(struct drmcgroup_pool_state *pool)
{
	lockdep_assert_held(&drmcg_mutex);

	list_del(&pool->css_node);
	list_del(&pool->dev_node);
	kfree(pool);
}

static void
set_resource_max(struct drmcgroup_pool_state *pool, int i, u64 new_max)
{
	pool->resources[i].max = new_max;
}

static void set_all_resource_max_limit(struct drmcgroup_pool_state *rpool)
{
	int i;

	for (i = 0; i < DRMCG_MAX_REGIONS; i++)
		set_resource_max(rpool, i, S64_MAX);
}

static void drmcs_offline(struct cgroup_subsys_state *css)
{
	struct drmcgroup_state *drmcs = css_to_drmcs(css);
	struct drmcgroup_pool_state *pool, *next;

	mutex_lock(&drmcg_mutex);
	list_for_each_entry_safe(pool, next, &drmcs->pools, css_node) {
		if (!pool->usage_sum) {
			free_cg_pool_locked(pool);
		} else {
			/* Reset all regions, last uncharge will remove pool */
			set_all_resource_max_limit(pool);
		}
	}
	mutex_unlock(&drmcg_mutex);
}

static void drmcs_free(struct cgroup_subsys_state *css)
{
	struct drmcgroup_state *drmcs = css_to_drmcs(css);

	kfree(drmcs);
}

bool drmcs_grouped(struct drmcgroup_state *limitcs,
		   struct drmcgroup_state *testcs)
{
	while (testcs && limitcs != testcs)
		testcs = parent_drmcg(testcs);

	return testcs != NULL;
}
EXPORT_SYMBOL_GPL(drmcs_grouped);

static struct cgroup_subsys_state *
drmcs_alloc(struct cgroup_subsys_state *parent_css)
{
	struct drmcgroup_state *drmcs = kzalloc(sizeof(*drmcs), GFP_KERNEL);
	if (!drmcs)
		return ERR_PTR(-ENOMEM);

	INIT_LIST_HEAD(&drmcs->pools);
	return &drmcs->css;
}

static struct drmcgroup_pool_state *
find_cg_pool_locked(struct drmcgroup_state *drmcs, struct drmcgroup_device *dev)
{
	struct drmcgroup_pool_state *pool;

	list_for_each_entry(pool, &drmcs->pools, css_node)
		if (pool->device == dev)
			return pool;

	return NULL;
}

static struct drmcgroup_pool_state *
get_cg_pool_locked(struct drmcgroup_state *drmcs, struct drmcgroup_device *dev)
{
	struct drmcgroup_pool_state *pool;

	pool = find_cg_pool_locked(drmcs, dev);
	if (pool)
		return pool;

	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
	if (!pool)
		return ERR_PTR(-ENOMEM);

	pool->device = dev;
	set_all_resource_max_limit(pool);

	list_add_tail(&pool->css_node, &drmcs->pools);
	list_add_tail(&pool->dev_node, &dev->pools);
	return pool;
}

void drmcg_unregister_device(struct drmcgroup_device *cgdev)
{
	struct drmcgroup_pool_state *pool, *next;

	mutex_lock(&drmcg_mutex);
	list_del(&cgdev->list);

	list_for_each_entry_safe(pool, next, &cgdev->pools, dev_node)
		free_cg_pool_locked(pool);
	mutex_unlock(&drmcg_mutex);
	kfree(cgdev->name);
}

EXPORT_SYMBOL_GPL(drmcg_unregister_device);

int drmcg_register_device(struct drm_device *dev,
			  struct drmcgroup_device *cgdev)
{
	char *name = kstrdup(dev->unique, GFP_KERNEL);
	if (!name)
		return -ENOMEM;

	INIT_LIST_HEAD(&cgdev->pools);
	mutex_lock(&drmcg_mutex);
	cgdev->name = name;
	list_add_tail(&cgdev->list, &drmcg_devices);
	mutex_unlock(&drmcg_mutex);

	return 0;
}
EXPORT_SYMBOL_GPL(drmcg_register_device);

static int drmcg_max_show(struct seq_file *sf, void *v)
{
	struct drmcgroup_state *drmcs = css_to_drmcs(seq_css(sf));
	struct drmcgroup_device *dev;

	mutex_lock(&drmcg_mutex);
	list_for_each_entry(dev, &drmcg_devices, list) {
		struct drmcgroup_pool_state *pool = find_cg_pool_locked(drmcs, dev);		int i;

		seq_puts(sf, dev->name);

		for (i = 0; i < DRMCG_MAX_REGIONS; i++) {
			if (!dev->regions[i].name)
				continue;

			if (pool && pool->resources[i].max < S64_MAX)
				seq_printf(sf, " region.%s=%lld", dev->regions[i].name,
					   pool->resources[i].max);
			else
				seq_printf(sf, " region.%s=max", dev->regions[i].name);
		}

		seq_putc(sf, '\n');
	}
	mutex_unlock(&drmcg_mutex);

	return 0;
}

static struct drmcgroup_device *drmcg_get_device_locked(const char *name)
{
	struct drmcgroup_device *dev;

	lockdep_assert_held(&drmcg_mutex);

	list_for_each_entry(dev, &drmcg_devices, list)
		if (!strcmp(name, dev->name))
			return dev;

	return NULL;
}

static void try_to_free_cg_pool_locked(struct drmcgroup_pool_state *pool)
{
	struct drmcgroup_device *dev = pool->device;
	u32 i;

	/* Memory charged to this pool */
	if (pool->usage_sum)
		return;

	for (i = 0; i < DRMCG_MAX_REGIONS; i++) {
		if (!dev->regions[i].name)
			continue;

		/* Is a specific limit set? */
		if (pool->resources[i].max < S64_MAX)
			return;
	}

	/*
	 * No user of the pool and all entries are set to defaults;
	 * safe to delete this pool.
	 */
	free_cg_pool_locked(pool);
}


static void
uncharge_cg_locked(struct drmcgroup_state *drmcs,
		   struct drmcgroup_device *cgdev,
		   u32 index, u64 size)
{
	struct drmcgroup_pool_state *pool;

	pool = find_cg_pool_locked(drmcs, cgdev);

	if (unlikely(!pool)) {
		pr_warn("Invalid device %p or drm cgroup %p\n", cgdev, drmcs);
		return;
	}

	pool->resources[index].used -= size;

	/*
	 * A negative count (or overflow) is invalid,
	 * it indicates a bug in the rdma controller.
	 */
	WARN_ON_ONCE(pool->resources[index].used < 0);
	pool->usage_sum--;
	try_to_free_cg_pool_locked(pool);
}

static void drmcg_uncharge_hierarchy(struct drmcgroup_state *drmcs,
				     struct drmcgroup_device *cgdev,
				     struct drmcgroup_state *stop_cg,
				     u32 index, u64 size)
{
	struct drmcgroup_state *p;

	if (!drmcs)
		return;

	mutex_lock(&drmcg_mutex);

	for (p = drmcs; p != stop_cg; p = parent_drmcg(p))
		uncharge_cg_locked(p, cgdev, index, size);

	mutex_unlock(&drmcg_mutex);

	css_put(&drmcs->css);
}

void drmcg_uncharge(struct drmcgroup_state *drmcs,
		    struct drmcgroup_device *cgdev,
		    u32 index, u64 size)
{
	if (index >= DRMCG_MAX_REGIONS)
		return;

	drmcg_uncharge_hierarchy(drmcs, cgdev, NULL, index, size);
}
EXPORT_SYMBOL_GPL(drmcg_uncharge);

void drmcs_put(struct drmcgroup_state *drmcs)
{
	if (drmcs)
		css_put(&drmcs->css);
}
EXPORT_SYMBOL_GPL(drmcs_put);

int drmcg_try_charge(struct drmcgroup_state **drmcs,
		     struct drmcgroup_state **limitcs,
		     struct drmcgroup_device *cgdev,
		     u32 index, u64 size)
{
	struct drmcgroup_state *cg, *p;
	struct drmcgroup_pool_state *pool;
	u64 new;
	int ret = 0;

	if (limitcs)
		*limitcs = NULL;

	if (index >= DRMCG_MAX_REGIONS)
		return -EINVAL;

	/*
	 * hold on to css, as cgroup can be removed but resource
	 * accounting happens on css.
	 */
	cg = get_current_drmcg();

	mutex_lock(&drmcg_mutex);
	for (p = cg; p; p = parent_drmcg(p)) {
		pool = get_cg_pool_locked(p, cgdev);
		if (IS_ERR(pool)) {
			ret = PTR_ERR(pool);
			goto err;
		} else {
			new = pool->resources[index].used + size;
			if (new > pool->resources[index].max || new > S64_MAX) {
				ret = -EAGAIN;
				if (limitcs) {
					css_get(&p->css);
					*limitcs = p;
				}
				goto err;
			} else {
				pool->resources[index].used = new;
				pool->usage_sum++;
			}
		}
	}
	mutex_unlock(&drmcg_mutex);

	*drmcs = cg;
	return 0;

err:
	mutex_unlock(&drmcg_mutex);
	drmcg_uncharge_hierarchy(cg, cgdev, p, index, size);
	return ret;
}
EXPORT_SYMBOL_GPL(drmcg_try_charge);

static s64 parse_resource(char *c, char **retname)
{
	substring_t argstr;
	char *name, *value = c;
	size_t len;
	int ret;
	u64 retval;

	name = strsep(&value, "=");
	if (!name || !value)
		return -EINVAL;

	/* Only support region setting for now */
	if (strncmp(name, "region.", 7))
		return -EINVAL;
	else
		name += 7;

	*retname = name;
	len = strlen(value);

	argstr.from = value;
	argstr.to = value + len;

	ret = match_u64(&argstr, &retval);
	if (ret >= 0) {
		if (retval > S64_MAX)
			return -EINVAL;
		return retval;
	}
	if (!strncmp(value, "max", len))
		return S64_MAX;

	/* Not u64 or max, error */
	return -EINVAL;
}

static int drmcg_parse_limits(char *options,
			      u64 *limits, char **enables)
{
	char *c;
	int num_limits = 0;

	/* parse resource options */
	while ((c = strsep(&options, " ")) != NULL) {
		s64 limit;

		if (num_limits >= DRMCG_MAX_REGIONS)
			return -EINVAL;

		limit = parse_resource(c, &enables[num_limits]);
		if (limit < 0)
			return limit;

		limits[num_limits++] = limit;
	}
	return num_limits;
}

static ssize_t drmcg_max_write(struct kernfs_open_file *of,
			       char *buf, size_t nbytes, loff_t off)
{
	struct drmcgroup_state *drmcs = css_to_drmcs(of_css(of));
	struct drmcgroup_device *dev;
	struct drmcgroup_pool_state *pool;
	char *options = strstrip(buf);
	char *dev_name = strsep(&options, " ");
	u64 limits[DRMCG_MAX_REGIONS];
	u64 new_limits[DRMCG_MAX_REGIONS];
	char *regions[DRMCG_MAX_REGIONS];
	int num_limits, i;
	unsigned long set_mask = 0;
	int err = 0;

	if (!dev_name)
		return -EINVAL;

	num_limits = drmcg_parse_limits(options, limits, regions);
	if (num_limits < 0)
		return num_limits;
	if (!num_limits)
		return -EINVAL;

	/*
	 * Everything is parsed into key=value pairs now, take lock and attempt to update
	 * For good measure, set -EINVAL when a key is set twice.
	 */
	mutex_lock(&drmcg_mutex);

	dev = drmcg_get_device_locked(dev_name);
	if (!dev) {
		err = -ENODEV;
		goto err;
	}

	pool = get_cg_pool_locked(drmcs, dev);
	if (IS_ERR(pool)) {
		err = PTR_ERR(pool);
		goto err;
	}

	/* Lookup region names and set new_limits to the index */
	for (i = 0; i < num_limits; i++) {
		int j;

		for (j = 0; j < DRMCG_MAX_REGIONS; j++)
			if (dev->regions[j].name &&
			    !strcmp(regions[i], dev->regions[j].name))
				break;

		if (j == DRMCG_MAX_REGIONS ||
		    set_mask & BIT(j)) {
			err = -EINVAL;
			goto err_put;
		}

		set_mask |= BIT(j);
		new_limits[j] = limits[i];
	}

	/* And commit */
	for_each_set_bit(i, &set_mask, DRMCG_MAX_REGIONS)
		set_resource_max(pool, i, new_limits[i]);

err_put:
	try_to_free_cg_pool_locked(pool);
err:
	mutex_unlock(&drmcg_mutex);

	return err ?: nbytes;
}

static int drmcg_current_show(struct seq_file *sf, void *v)
{
	struct drmcgroup_state *drmcs = css_to_drmcs(seq_css(sf));
	struct drmcgroup_device *dev;

	mutex_lock(&drmcg_mutex);
	list_for_each_entry(dev, &drmcg_devices, list) {
		struct drmcgroup_pool_state *pool = find_cg_pool_locked(drmcs, dev);
		int i;

		seq_puts(sf, dev->name);

		for (i = 0; i < DRMCG_MAX_REGIONS; i++) {
			if (!dev->regions[i].name)
				continue;

			seq_printf(sf, " region.%s=%lld", dev->regions[i].name,
				   pool ? pool->resources[i].used : 0ULL);
		}

		seq_putc(sf, '\n');
	}
	mutex_unlock(&drmcg_mutex);

	return 0;
}

static int drmcg_capacity_show(struct seq_file *sf, void *v)
{
	struct drmcgroup_device *dev;
	int i;

	list_for_each_entry(dev, &drmcg_devices, list) {
		seq_puts(sf, dev->name);
		for (i = 0; i < DRMCG_MAX_REGIONS; i++)
			if (dev->regions[i].name)
				seq_printf(sf, " region.%s=%lld",
					   dev->regions[i].name,
					   dev->regions[i].size);
		seq_putc(sf, '\n');
	}
	return 0;
}

static struct cftype files[] = {
	{
		.name = "max",
		.write = drmcg_max_write,
		.seq_show = drmcg_max_show,
		.flags = CFTYPE_NOT_ON_ROOT,
	},
	{
		.name = "current",
		.seq_show = drmcg_current_show,
		.flags = CFTYPE_NOT_ON_ROOT,
	},
	{
		.name = "capacity",
		.seq_show = drmcg_capacity_show,
		.flags = CFTYPE_ONLY_ON_ROOT,
	},
	{ } /* Zero entry terminates. */
};

struct cgroup_subsys drm_cgrp_subsys = {
	.css_alloc	= drmcs_alloc,
	.css_free	= drmcs_free,
	.css_offline	= drmcs_offline,
	.legacy_cftypes	= files,
	.dfl_cftypes	= files,
};