Repository navigation
Expand file tree
/
Copy pathneuron_crwl.c
More file actions
292 lines (247 loc) · 7.79 KB
/
Copy pathneuron_crwl.c
File metadata and controls
292 lines (247 loc) · 7.79 KB
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
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright 2021, Amazon.com, Inc. or its affiliates. All Rights Reserved
*/
#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/delay.h>
#include "neuron_device.h"
#include "neuron_dhal.h"
// Sleep range when waiting for lock state to be changed
#define NEURON_CRWL_SLEEP_MIN 10
#define NEURON_CRWL_SLEEP_MAX 20
// Number of times to retry (each retry takes between(10us to 20us)).
#define NEURON_CRWL_READER_MAX_RETRY (50 * 1000) // ~500ms
#define NEURON_CRWL_WRITER_MAX_RETRY (200 * 1000) // ~2sec
static inline int ncrwl_validate_uuid(struct neuron_device *nd, u32 nc_index,
struct neuron_uuid uuid)
{
struct neuron_crwl *crwl;
BUG_ON(nc_index >= MAX_NC_PER_DEVICE);
crwl = &nd->crwl[nc_index];
if (memcmp(&uuid, &crwl->uuid, sizeof(uuid)))
return -ENOENT;
if (crwl->writer_pid != task_tgid_nr(current)) {
pr_err("nd%dnc%d: pid:%d Invalid pid - writer:%d\n",
nd->device_index, nc_index, task_tgid_nr(current), crwl->writer_pid);
return -ENOENT;
}
return 0;
}
int ncrwl_reader_enter(struct neuron_device *nd, u32 nc_index, struct neuron_uuid uuid)
{
int ret = 0;
struct neuron_crwl *crwl;
u64 retry_count = 0;
if (nc_index >= MAX_NC_PER_DEVICE)
return -EINVAL;
crwl = &nd->crwl[nc_index];
mutex_lock(&crwl->lock);
// if writer lock is taken then wait for writer to finish updating the model.
while (crwl->writer_acquired) {
mutex_unlock(&crwl->lock);
if (retry_count > NEURON_CRWL_READER_MAX_RETRY) {
pr_err("nd%dnc%d: pid:%d - reader starved. writer:%d\n",
nd->device_index, nc_index, task_tgid_nr(current), crwl->writer_pid);
return -EBUSY;
}
retry_count++;
usleep_range(NEURON_CRWL_SLEEP_MIN, NEURON_CRWL_SLEEP_MAX);
mutex_lock(&crwl->lock);
}
ret = ncrwl_validate_uuid(nd, nc_index, uuid);
if (ret)
goto done;
crwl->reader_count++;
done:
mutex_unlock(&crwl->lock);
return ret;
}
int ncrwl_reader_exit(struct neuron_device *nd, u32 nc_index, struct neuron_uuid uuid)
{
int ret = 0;
struct neuron_crwl *crwl;
if (nc_index >= MAX_NC_PER_DEVICE)
return -EINVAL;
crwl = &nd->crwl[nc_index];
mutex_lock(&crwl->lock);
ret = ncrwl_validate_uuid(nd, nc_index, uuid);
if (ret)
goto done;
if (crwl->reader_count == 0) {
pr_err("nd%dnc%d: pid:%d - reader count is already 0\n", nd->device_index, nc_index,
task_tgid_nr(current));
ret = -EINVAL;
goto done;
}
crwl->reader_count--;
done:
mutex_unlock(&crwl->lock);
return ret;
}
int ncrwl_writer_enter(struct neuron_device *nd, u32 nc_index, struct neuron_uuid uuid)
{
int ret = 0;
struct neuron_crwl *crwl;
u64 retry_count = 0;
if (nc_index >= MAX_NC_PER_DEVICE)
return -EINVAL;
crwl = &nd->crwl[nc_index];
mutex_lock(&crwl->lock);
while (crwl->writer_acquired || // only one writer is allowed, so wait for other to release
crwl->reader_count) { // wait for old model's readers to finish
mutex_unlock(&crwl->lock);
if (retry_count > NEURON_CRWL_WRITER_MAX_RETRY) {
pr_err("nd%dnc%d: pid:%d - writer starved. readers:%lld writer:%d\n",
nd->device_index, nc_index, task_tgid_nr(current),
crwl->reader_count, crwl->writer_acquired);
return -EBUSY;
}
retry_count++;
if (retry_count % 20000 == 0) {
pr_info("nd%dnc%d: pid:%d - writer retrying(%lld/%d) readers:%lld writer:%d\n",
nd->device_index, nc_index, task_tgid_nr(current),
retry_count, NEURON_CRWL_WRITER_MAX_RETRY,
crwl->reader_count, crwl->writer_pid);
}
usleep_range(NEURON_CRWL_SLEEP_MIN, NEURON_CRWL_SLEEP_MAX);
mutex_lock(&crwl->lock);
}
crwl->writer_acquired = true;
/* some other thread same process already switched to required model then no need redo it. */
if (crwl->writer_pid == task_tgid_nr(current) &&
memcmp(&uuid, &crwl->uuid, sizeof(uuid)) == 0) {
ret = -EALREADY;
goto done;
}
crwl->writer_pid = task_tgid_nr(current);
memcpy(&crwl->uuid, &uuid, sizeof(uuid));
done:
mutex_unlock(&crwl->lock);
return ret;
}
int ncrwl_writer_downgrade(struct neuron_device *nd, u32 nc_index, struct neuron_uuid uuid)
{
int ret = 0;
struct neuron_crwl *crwl;
if (nc_index >= MAX_NC_PER_DEVICE)
return -EINVAL;
crwl = &nd->crwl[nc_index];
mutex_lock(&crwl->lock);
if (!crwl->writer_acquired) {
pr_err("nd%dnc%d: pid:%d - writer downgrade called without enter\n",
nd->device_index, nc_index, task_tgid_nr(current));
ret = -EINVAL;
goto done;
}
ret = ncrwl_validate_uuid(nd, nc_index, uuid);
if (ret)
goto done;
BUG_ON(crwl->reader_count != 0);
crwl->writer_acquired = false;
crwl->reader_count = 1;
done:
mutex_unlock(&crwl->lock);
return ret;
}
// Each entry points to NC and value points to PID acquired that NC.
static pid_t ncrwl_range_pids[MAX_NEURON_DEVICE_COUNT * MAX_NC_PER_DEVICE] = {0};
static int ncrwl_range_mark_cnt = 0;
DEFINE_MUTEX(ncrwl_range_lock); // lock to protect ncrwl_range_pids
int ncrwl_nc_range_mark(u32 nc_count, u32 start_nc_index, u32 end_nc_index,
u32 *max_range, volatile long unsigned int *result_map)
{
int i, j;
*max_range = 0;
if (start_nc_index > end_nc_index ||
start_nc_index >= MAX_NEURON_DEVICE_COUNT * MAX_NC_PER_DEVICE ||
end_nc_index >= MAX_NEURON_DEVICE_COUNT * MAX_NC_PER_DEVICE)
return -ENXIO;
mutex_lock(&ncrwl_range_lock);
for (i = start_nc_index; i <= end_nc_index; i++) {
int range_len = 1;
// skip already marked ones
if (ncrwl_range_pids[i] != 0)
continue;
// find how many consecutive neuron cores are free.
for (j = i; j <= end_nc_index && j < (i + nc_count); j++) {
if (ncrwl_range_pids[j] != 0)
break;
range_len = j - i + 1;
}
// if required number of NCs are free, mark them as acquired and update the result map
if (range_len >= nc_count) {
// notify election code of mark and provide prev range mark count
ndhal->ndhal_npe.npe_notify_mark(ncrwl_range_mark_cnt, true);
for (j = i; j <= end_nc_index && j < (i + nc_count); j++) {
ncrwl_range_pids[j] = task_tgid_nr(current);
set_bit(j, result_map);
ncrwl_range_mark_cnt++;
}
mutex_unlock(&ncrwl_range_lock);
pr_info("pid:%d claiming neuron cores: %02d-%02d", task_tgid_nr(current), i, i + nc_count - 1);
return 0;
}
if (*max_range < range_len)
*max_range = range_len;
i = j + 1;
}
mutex_unlock(&ncrwl_range_lock);
return -EBUSY;
}
void ncrwl_nc_range_unmark(volatile long unsigned int *free_map)
{
int i;
int first = -1;
int last;
mutex_lock(&ncrwl_range_lock);
for (i = 0; i < MAX_NEURON_DEVICE_COUNT * MAX_NC_PER_DEVICE; i++) {
if (test_bit(i, free_map) && ncrwl_range_pids[i] == task_tgid_nr(current)) {
first = (first == -1) ? i : first;
last = i;
ncrwl_range_pids[i] = 0;
ncrwl_range_mark_cnt--;
}
// notify election code of decremented range mark count
ndhal->ndhal_npe.npe_notify_mark(ncrwl_range_mark_cnt, false);
}
mutex_unlock(&ncrwl_range_lock);
if (first != -1) {
pr_info("pid:%d releasing neuron core in range: %02d-%02d", task_tgid_nr(current), first, last);
}
}
int ncrwl_nc_range_pid_get( uint32_t nc_index, pid_t *pid)
{
if (nc_index >= MAX_NEURON_DEVICE_COUNT * MAX_NC_PER_DEVICE) {
return -ENOENT;
}
mutex_lock(&ncrwl_range_lock);
*pid = ncrwl_range_pids[nc_index];
mutex_unlock(&ncrwl_range_lock);
return 0;
}
int ncrwl_range_mark_cnt_get(void)
{
int cnt = 0;
mutex_lock(&ncrwl_range_lock);
cnt = ncrwl_range_mark_cnt;
mutex_unlock(&ncrwl_range_lock);
return cnt;
}
void ncrwl_release_current_process(struct neuron_device *nd)
{
struct neuron_crwl *crwl;
int nc_index;
for (nc_index = 0; nc_index < MAX_NC_PER_DEVICE; nc_index++) {
crwl = &nd->crwl[nc_index];
mutex_lock(&crwl->lock);
if (crwl->writer_pid == task_tgid_nr(current)) {
crwl->reader_count = 0;
crwl->writer_pid = 0;
crwl->writer_acquired = false;
memset(&crwl->uuid, 0, sizeof(struct neuron_uuid));
}
mutex_unlock(&crwl->lock);
}
}