File: uvm_tracker_test.c

package info (click to toggle)
nvidia-open-gpu-kernel-modules 550.163.01-4
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 87,488 kB
  • sloc: ansic: 1,143,669; cpp: 22,547; sh: 3,721; makefile: 627; python: 315
file content (462 lines) | stat: -rw-r--r-- 16,212 bytes parent folder | download | duplicates (3)
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
/*******************************************************************************
    Copyright (c) 2015-2019 NVIDIA Corporation

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to
    deal in the Software without restriction, including without limitation the
    rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    sell copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

        The above copyright notice and this permission notice shall be
        included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.

*******************************************************************************/

#include "uvm_channel.h"
#include "uvm_global.h"
#include "uvm_hal.h"
#include "uvm_push.h"
#include "uvm_test.h"
#include "uvm_tracker.h"
#include "uvm_va_space.h"

static NV_STATUS assert_tracker_is_completed(uvm_tracker_t *tracker)
{
    TEST_NV_CHECK_RET(uvm_tracker_query(tracker));
    TEST_CHECK_RET(uvm_tracker_is_completed(tracker));
    TEST_NV_CHECK_RET(uvm_tracker_wait(tracker));
    TEST_NV_CHECK_RET(uvm_tracker_check_errors(tracker));
    TEST_CHECK_RET(tracker->size == 0);
    uvm_tracker_remove_completed(tracker);
    uvm_tracker_clear(tracker);

    return NV_OK;
}

static NV_STATUS assert_tracker_is_not_completed(uvm_tracker_t *tracker)
{
    uvm_tracker_remove_completed(tracker);
    TEST_CHECK_RET(uvm_tracker_query(tracker) == NV_WARN_MORE_PROCESSING_REQUIRED);
    TEST_CHECK_RET(!uvm_tracker_is_completed(tracker));
    TEST_NV_CHECK_RET(uvm_tracker_check_errors(tracker));
    TEST_CHECK_RET(tracker->size != 0);

    return NV_OK;
}

// This test schedules some GPU work behind a semaphore and then allows the GPU
// to progress one tracker entry at a time verifying that the tracker entries
// are completed as expected.
static NV_STATUS test_tracker_completion(uvm_va_space_t *va_space)
{
    uvm_gpu_t *gpu;
    uvm_tracker_t tracker;
    uvm_gpu_semaphore_t sema;
    NvU32 count = 0;
    NvU32 payload;
    NV_STATUS status = NV_OK;
    uvm_spin_loop_t spin;

    // TODO: Bug 4008734: [UVM][HCC] Extend secure tracking semaphore mechanism
    //                     to all semaphore
    // This test allocates semaphore in vidmem and then releases it from the CPU
    // SEC2 channels cannot target semaphores in vidmem. Moreover, CPU cannot
    // directly release values to vidmem for CE channels.
    if (g_uvm_global.conf_computing_enabled)
        return NV_OK;

    gpu = uvm_va_space_find_first_gpu(va_space);
    TEST_CHECK_RET(gpu != NULL);

    TEST_NV_CHECK_RET(uvm_gpu_semaphore_alloc(gpu->semaphore_pool, &sema));

    uvm_tracker_init(&tracker);
    TEST_NV_CHECK_GOTO(assert_tracker_is_completed(&tracker), done);

    // The following assumes that it's possible to begin a small push that won't
    // be able to finish (it's behind a semaphore that will be released from the
    // CPU later) for each channel on a each GPU.
    for_each_va_space_gpu(gpu, va_space) {
        uvm_channel_pool_t *pool;

        uvm_for_each_pool(pool, gpu->channel_manager) {
            uvm_channel_t *channel;

            // Skip WLC channels as they are used for secure work launch
            if (uvm_channel_pool_is_wlc(pool))
                continue;

            // Skip LCIC channels as those can't accept pushes
            if (uvm_channel_pool_is_lcic(pool))
                continue;
            uvm_for_each_channel_in_pool(channel, pool) {
                uvm_push_t push;
                NvU64 semaphore_gpu_va;

                ++count;
                TEST_NV_CHECK_GOTO(uvm_push_begin_on_channel(channel, &push, "Test push"), done);

                semaphore_gpu_va = uvm_gpu_semaphore_get_gpu_va(&sema, gpu, uvm_channel_is_proxy(channel));

                // Acquire increasing semaphore payloads on all channels so that they can be completed one by one
                gpu->parent->host_hal->semaphore_acquire(&push, semaphore_gpu_va, count);

                uvm_push_end(&push);

                if (count & 1)
                    TEST_NV_CHECK_GOTO(uvm_tracker_add_push_safe(&tracker, &push), done);
                else
                    TEST_NV_CHECK_GOTO(uvm_tracker_add_push(&tracker, &push), done);
            }
        }
    }

    TEST_NV_CHECK_GOTO(assert_tracker_is_not_completed(&tracker), done);

    for (payload = 0; payload < count; ++payload) {
        TEST_CHECK_GOTO(tracker.size == count - payload, done);
        TEST_NV_CHECK_GOTO(assert_tracker_is_not_completed(&tracker), done);

        // Release the next payload allowing a single channel to complete
        uvm_gpu_semaphore_set_payload(&sema, payload + 1);

        uvm_spin_loop_init(&spin);
        while (tracker.size == count - payload) {
            UVM_SPIN_LOOP(&spin);
            uvm_tracker_remove_completed(&tracker);
        }

        TEST_CHECK_GOTO(tracker.size == count - payload - 1, done);
    }

    TEST_NV_CHECK_GOTO(assert_tracker_is_completed(&tracker), done);

done:
    uvm_gpu_semaphore_free(&sema);
    uvm_tracker_wait_deinit(&tracker);
    return status;
}

static NV_STATUS test_tracker_basic(uvm_va_space_t *va_space)
{
    uvm_gpu_t *gpu;
    uvm_channel_t *any_channel;
    uvm_tracker_t tracker;
    uvm_tracker_entry_t entry;
    NvU32 count = 0;
    NV_STATUS status = NV_OK;

    gpu = uvm_va_space_find_first_gpu(va_space);
    if (gpu == NULL)
        return NV_ERR_INVALID_STATE;

    any_channel = uvm_channel_any(gpu->channel_manager);
    if (any_channel == NULL)
        return NV_ERR_INVALID_STATE;

    uvm_tracker_init(&tracker);
    TEST_CHECK_GOTO(assert_tracker_is_completed(&tracker) == NV_OK, done);

    // Some channel
    entry.channel = any_channel;
    entry.value = 1;

    status = uvm_tracker_add_entry(&tracker, &entry);
    TEST_CHECK_GOTO(status == NV_OK, done);
    TEST_CHECK_RET(tracker.size == 1);

    status = uvm_tracker_add_entry(&tracker, &entry);
    TEST_CHECK_GOTO(status == NV_OK, done);
    TEST_CHECK_RET(tracker.size == 1);
    TEST_CHECK_RET(uvm_tracker_get_entries(&tracker)[0].channel == entry.channel);
    TEST_CHECK_RET(uvm_tracker_get_entries(&tracker)[0].value == entry.value);

    entry.value = 10;
    status = uvm_tracker_add_entry(&tracker, &entry);
    TEST_CHECK_GOTO(status == NV_OK, done);
    TEST_CHECK_RET(tracker.size == 1);
    TEST_CHECK_RET(uvm_tracker_get_entries(&tracker)[0].channel == entry.channel);
    TEST_CHECK_RET(uvm_tracker_get_entries(&tracker)[0].value == entry.value);

    // Adding an older value for the same channel should have no effect
    entry.value = 5;
    status = uvm_tracker_add_entry(&tracker, &entry);
    TEST_CHECK_GOTO(status == NV_OK, done);
    TEST_CHECK_RET(tracker.size == 1);
    TEST_CHECK_RET(uvm_tracker_get_entries(&tracker)[0].value == 10);

    uvm_tracker_clear(&tracker);

    TEST_CHECK_GOTO(assert_tracker_is_completed(&tracker) == NV_OK, done);

    for_each_va_space_gpu(gpu, va_space) {
        uvm_channel_pool_t *pool;

        uvm_for_each_pool(pool, gpu->channel_manager) {
            uvm_channel_t *channel;

            uvm_for_each_channel_in_pool(channel, pool) {
                entry.channel = channel;
                entry.value = uvm_channel_update_completed_value(channel);
                if (count & 1)
                    status = uvm_tracker_add_entry_safe(&tracker, &entry);
                else
                    status = uvm_tracker_add_entry(&tracker, &entry);
                TEST_CHECK_GOTO(status == NV_OK, done);
                ++count;
            }
        }
    }

    TEST_CHECK_GOTO(tracker.size == count, done);

    // All the entries that we added are already completed
    TEST_CHECK_GOTO(assert_tracker_is_completed(&tracker) == NV_OK, done);

    count = 0;
    for_each_va_space_gpu(gpu, va_space) {
        uvm_channel_pool_t *pool;

        uvm_for_each_pool(pool, gpu->channel_manager) {
            uvm_channel_t *channel;

            // Skip LCIC channels as those can't accept pushes
            if (uvm_channel_pool_is_lcic(pool))
                continue;
            uvm_for_each_channel_in_pool(channel, pool) {
                uvm_push_t push;
                status = uvm_push_begin_on_channel(channel, &push, "Test push");
                TEST_CHECK_GOTO(status == NV_OK, done);

                uvm_push_end(&push);

                TEST_NV_CHECK_GOTO(uvm_tracker_add_push(&tracker, &push), done);
                ++count;
            }
        }
    }

    TEST_CHECK_GOTO(tracker.size == count, done);
    TEST_CHECK_GOTO(uvm_tracker_wait(&tracker) == NV_OK, done);
    // After a wait, the tracker should be complete
    TEST_CHECK_GOTO(assert_tracker_is_completed(&tracker) == NV_OK, done);

done:
    uvm_tracker_deinit(&tracker);
    return status;
}

static NV_STATUS test_tracker_overwrite(uvm_va_space_t *va_space)
{
    uvm_gpu_t *gpu;
    uvm_channel_t *any_channel;
    uvm_tracker_t tracker, dup_tracker;
    uvm_tracker_entry_t entry;
    uvm_tracker_entry_t *entry_iter, *dup_entry_iter;
    NV_STATUS status = NV_OK;
    bool dup_tracker_init = false;
    NvU32 count = 0;

    gpu = uvm_va_space_find_first_gpu(va_space);
    if (gpu == NULL)
        return NV_ERR_INVALID_STATE;

    any_channel = uvm_channel_any(gpu->channel_manager);
    if (any_channel == NULL)
        return NV_ERR_INVALID_STATE;

    uvm_tracker_init(&tracker);
    TEST_CHECK_GOTO(assert_tracker_is_completed(&tracker) == NV_OK, done);

    // Some channel
    entry.channel = any_channel;
    entry.value = 1;

    status = uvm_tracker_add_entry(&tracker, &entry);
    TEST_CHECK_GOTO(status == NV_OK, done);
    TEST_CHECK_RET(tracker.size == 1);
    TEST_CHECK_RET(uvm_tracker_get_entries(&tracker)[0].channel == entry.channel);
    TEST_CHECK_RET(uvm_tracker_get_entries(&tracker)[0].value == entry.value);

    status = uvm_tracker_init_from(&dup_tracker, &tracker);
    TEST_CHECK_GOTO(status == NV_OK, done);
    dup_tracker_init = true;
    TEST_CHECK_RET(dup_tracker.size == 1);
    TEST_CHECK_RET(uvm_tracker_get_entries(&dup_tracker)[0].channel == entry.channel);
    TEST_CHECK_RET(uvm_tracker_get_entries(&dup_tracker)[0].value == entry.value);

    entry.value = 2;

    uvm_tracker_overwrite_with_entry(&dup_tracker, &entry);
    TEST_CHECK_RET(dup_tracker.size == 1);
    TEST_CHECK_RET(uvm_tracker_get_entries(&dup_tracker)[0].channel == entry.channel);
    TEST_CHECK_RET(uvm_tracker_get_entries(&dup_tracker)[0].value == entry.value);

    for_each_va_space_gpu(gpu, va_space) {
        uvm_channel_pool_t *pool;

        uvm_for_each_pool(pool, gpu->channel_manager) {
            uvm_channel_t *channel;

            uvm_for_each_channel_in_pool(channel, pool) {
                entry.channel = channel;
                entry.value = uvm_channel_update_completed_value(channel);
                status = uvm_tracker_add_entry(&tracker, &entry);
                TEST_CHECK_GOTO(status == NV_OK, done);
                ++count;
            }
        }
    }
    TEST_CHECK_GOTO(tracker.size == count, done);

    status = uvm_tracker_overwrite(&dup_tracker, &tracker);
    TEST_CHECK_GOTO(dup_tracker.size == count, done);
    for_each_tracker_entry(dup_entry_iter, &dup_tracker) {
        bool found = false;
        for_each_tracker_entry(entry_iter, &tracker) {
            if (entry_iter->channel == dup_entry_iter->channel && entry_iter->value == dup_entry_iter->value) {
                found = true;
                break;
            }
        }
        TEST_CHECK_RET(found);
    }
    for_each_tracker_entry(entry_iter, &tracker) {
        bool found = false;
        for_each_tracker_entry(dup_entry_iter, &dup_tracker) {
            if (entry_iter->channel == dup_entry_iter->channel && entry_iter->value == dup_entry_iter->value) {
                found = true;
                break;
            }
        }
        TEST_CHECK_RET(found);
    }

done:
    uvm_tracker_deinit(&tracker);
    if (dup_tracker_init)
        uvm_tracker_deinit(&dup_tracker);
    return status;
}

static NV_STATUS test_tracker_add_tracker(uvm_va_space_t *va_space)
{
    uvm_gpu_t *gpu;
    uvm_channel_t *any_channel;
    uvm_tracker_t tracker, dup_tracker;
    uvm_tracker_entry_t entry;
    uvm_tracker_entry_t *entry_iter, *dup_entry_iter;
    NV_STATUS status = NV_OK;
    NvU32 count = 0;

    gpu = uvm_va_space_find_first_gpu(va_space);
    if (gpu == NULL)
        return NV_ERR_INVALID_STATE;

    any_channel = uvm_channel_any(gpu->channel_manager);
    if (any_channel == NULL)
        return NV_ERR_INVALID_STATE;

    uvm_tracker_init(&tracker);
    uvm_tracker_init(&dup_tracker);
    TEST_CHECK_GOTO(assert_tracker_is_completed(&tracker) == NV_OK, done);

    // Some channel
    entry.channel = any_channel;
    entry.value = 1;

    status = uvm_tracker_add_entry(&tracker, &entry);
    TEST_CHECK_GOTO(status == NV_OK, done);
    TEST_CHECK_RET(tracker.size == 1);
    TEST_CHECK_RET(uvm_tracker_get_entries(&tracker)[0].channel == entry.channel);
    TEST_CHECK_RET(uvm_tracker_get_entries(&tracker)[0].value == entry.value);

    status = uvm_tracker_add_tracker(&dup_tracker, &tracker);
    TEST_CHECK_GOTO(status == NV_OK, done);
    TEST_CHECK_RET(dup_tracker.size == 1);
    TEST_CHECK_RET(uvm_tracker_get_entries(&dup_tracker)[0].channel == entry.channel);
    TEST_CHECK_RET(uvm_tracker_get_entries(&dup_tracker)[0].value == entry.value);

    for_each_va_space_gpu(gpu, va_space) {
        uvm_channel_pool_t *pool;

        uvm_for_each_pool(pool, gpu->channel_manager) {
            uvm_channel_t *channel;

            uvm_for_each_channel_in_pool(channel, pool) {
                entry.channel = channel;
                entry.value = uvm_channel_update_completed_value(channel);
                status = uvm_tracker_add_entry(&tracker, &entry);
                TEST_CHECK_GOTO(status == NV_OK, done);
                ++count;
            }
        }
    }
    TEST_CHECK_GOTO(tracker.size == count, done);

    status = uvm_tracker_add_tracker_safe(&dup_tracker, &tracker);
    TEST_CHECK_GOTO(dup_tracker.size == count, done);
    for_each_tracker_entry(dup_entry_iter, &dup_tracker) {
        bool found = false;
        for_each_tracker_entry(entry_iter, &tracker) {
            if (entry_iter->channel == dup_entry_iter->channel && entry_iter->value == dup_entry_iter->value) {
                found = true;
                break;
            }
        }
        TEST_CHECK_RET(found);
    }
    for_each_tracker_entry(entry_iter, &tracker) {
        bool found = false;
        for_each_tracker_entry(dup_entry_iter, &dup_tracker) {
            if (entry_iter->channel == dup_entry_iter->channel && entry_iter->value == dup_entry_iter->value) {
                found = true;
                break;
            }
        }
        TEST_CHECK_RET(found);
    }

done:
    uvm_tracker_deinit(&tracker);
    uvm_tracker_deinit(&dup_tracker);
    return status;
}

NV_STATUS uvm_test_tracker_sanity(UVM_TEST_TRACKER_SANITY_PARAMS *params, struct file *filp)
{
    NV_STATUS status;
    uvm_va_space_t *va_space = uvm_va_space_get(filp);

    uvm_va_space_down_read_rm(va_space);

    status = test_tracker_basic(va_space);
    if (status != NV_OK)
        goto done;

    status = test_tracker_completion(va_space);
    if (status != NV_OK)
        goto done;

    status = test_tracker_overwrite(va_space);
    if (status != NV_OK)
        goto done;

    status = test_tracker_add_tracker(va_space);
    if (status != NV_OK)
        goto done;

done:
    uvm_va_space_up_read_rm(va_space);

    return status;
}