aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/fdso.c
blob: e23e7d5a5daadd84ca6893bebf058c9b56af19e9 (plain) (blame)
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
/* fdso.c - file descriptor attached shared objects
 *
 * (c) 2007 B. Brandenburg, LITMUS^RT project
 *
 * Notes:
 *   - objects descriptor (OD) tables are not cloned during a fork.
 *   - objects are created on-demand, and freed after the last reference
 *     is dropped.
 *   - for now, object types are hard coded.
 *   - As long as we have live objects, we keep a reference to the inode.
 */

#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/mutex.h>
#include <linux/file.h>
#include <asm/uaccess.h>

#include <litmus/fdso.h>

extern struct fdso_ops generic_lock_ops;

#ifdef CONFIG_LITMUS_AFFINITY_LOCKING
extern struct fdso_ops generic_affinity_ops;
#endif

static const struct fdso_ops* fdso_ops[] = {
	&generic_lock_ops, /* FMLP_SEM */
	&generic_lock_ops, /* SRP_SEM */

	&generic_lock_ops, /* MPCP_SEM */
	&generic_lock_ops, /* MPCP_VS_SEM */
	&generic_lock_ops, /* DPCP_SEM */
	&generic_lock_ops, /* PCP_SEM */

	&generic_lock_ops, /* FIFO_MUTEX */
	&generic_lock_ops, /* IKGLP_SEM */
	&generic_lock_ops, /* KFMLP_SEM */
#ifdef CONFIG_LITMUS_AFFINITY_LOCKING
	&generic_affinity_ops, /* IKGLP_SIMPLE_GPU_AFF_OBS */
	&generic_affinity_ops, /* IKGLP_GPU_AFF_OBS */
	&generic_affinity_ops, /* KFMLP_SIMPLE_GPU_AFF_OBS */
	&generic_affinity_ops, /* KFMLP_GPU_AFF_OBS */
#endif
	&generic_lock_ops, /* PRIOQ_MUTEX */
};

static int fdso_create(void** obj_ref, obj_type_t type, void* __user config)
{
	BUILD_BUG_ON(ARRAY_SIZE(fdso_ops) != MAX_OBJ_TYPE + 1);

	if (fdso_ops[type]->create)
		return fdso_ops[type]->create(obj_ref, type, config);
	else
		return -EINVAL;
}

static void fdso_destroy(obj_type_t type, void* obj)
{
	fdso_ops[type]->destroy(type, obj);
}

static int fdso_open(struct od_table_entry* entry, void* __user config)
{
	if (fdso_ops[entry->obj->type]->open)
		return fdso_ops[entry->obj->type]->open(entry, config);
	else
		return 0;
}

static int fdso_close(struct od_table_entry* entry)
{
	if (fdso_ops[entry->obj->type]->close)
		return fdso_ops[entry->obj->type]->close(entry);
	else
		return 0;
}

/* inode must be locked already */
static int alloc_inode_obj(struct inode_obj_id** obj_ref,
			   struct inode* inode,
			   obj_type_t type,
			   unsigned int id,
			   void* __user config)
{
	struct inode_obj_id* obj;
	void* raw_obj;
	int err;

	obj = kmalloc(sizeof(*obj), GFP_KERNEL);
	if (!obj) {
		return -ENOMEM;
	}

	err = fdso_create(&raw_obj, type, config);
	if (err != 0) {
		kfree(obj);
		return err;
	}

	INIT_LIST_HEAD(&obj->list);
	atomic_set(&obj->count, 1);
	obj->type  = type;
	obj->id    = id;
	obj->obj   = raw_obj;
	obj->inode = inode;

	list_add(&obj->list, &inode->i_obj_list);
	atomic_inc(&inode->i_count);

	printk(KERN_DEBUG "alloc_inode_obj(%p, %d, %d): object created\n", inode, type, id);

	*obj_ref = obj;
	return 0;
}

/* inode must be locked already */
static struct inode_obj_id* get_inode_obj(struct inode* inode,
					  obj_type_t type,
					  unsigned int id)
{
	struct list_head* pos;
	struct inode_obj_id* obj = NULL;

	list_for_each(pos, &inode->i_obj_list) {
		obj = list_entry(pos, struct inode_obj_id, list);
		if (obj->id == id && obj->type == type) {
			atomic_inc(&obj->count);
			return obj;
		}
	}
	printk(KERN_DEBUG "get_inode_obj(%p, %d, %d): couldn't find object\n", inode, type, id);
	return NULL;
}


static void put_inode_obj(struct inode_obj_id* obj)
{
	struct inode* inode;
	int let_go = 0;

	inode = obj->inode;
	if (atomic_dec_and_test(&obj->count)) {

		mutex_lock(&inode->i_obj_mutex);
		/* no new references can be obtained */
		if (!atomic_read(&obj->count)) {
			list_del(&obj->list);
			fdso_destroy(obj->type, obj->obj);
			kfree(obj);
			let_go = 1;
		}
		mutex_unlock(&inode->i_obj_mutex);
		if (let_go)
			iput(inode);
	}
}

static struct od_table_entry*  get_od_entry(struct task_struct* t)
{
	struct od_table_entry* table;
	int i;


	table = t->od_table;
	if (!table) {
		table = kzalloc(sizeof(*table) * MAX_OBJECT_DESCRIPTORS,
				GFP_KERNEL);
		t->od_table = table;
	}

	for (i = 0; table &&  i < MAX_OBJECT_DESCRIPTORS; i++)
		if (!table[i].used) {
			table[i].used = 1;
			return table + i;
		}
	return NULL;
}

static int put_od_entry(struct od_table_entry* od)
{
	put_inode_obj(od->obj);
	od->used = 0;
	return 0;
}

static long close_od_entry(struct od_table_entry *od)
{
	long ret;

	/* Give the class a chance to reject the close. */
	ret = fdso_close(od);
	if (ret == 0)
		ret = put_od_entry(od);

	return ret;
}

void exit_od_table(struct task_struct* t)
{
	int i;

	if (t->od_table) {
		for (i = 0; i < MAX_OBJECT_DESCRIPTORS; i++)
			if (t->od_table[i].used)
				close_od_entry(t->od_table + i);
		kfree(t->od_table);
		t->od_table = NULL;
	}
}

static int do_sys_od_open(struct file* file, obj_type_t type, int id,
			  void* __user config)
{
	int idx = 0, err = 0;
	struct inode* inode;
	struct inode_obj_id* obj = NULL;
	struct od_table_entry* entry;

	inode = file->f_dentry->d_inode;

	entry = get_od_entry(current);
	if (!entry)
		return -ENOMEM;

	mutex_lock(&inode->i_obj_mutex);
	obj = get_inode_obj(inode, type, id);
	if (!obj)
		err = alloc_inode_obj(&obj, inode, type, id, config);
	if (err != 0) {
		obj = NULL;
		idx = err;
		entry->used = 0;
	} else {
		entry->obj   = obj;
		entry->class = fdso_ops[type];
		idx = entry - current->od_table;
	}

	mutex_unlock(&inode->i_obj_mutex);

	/* open only if creation succeeded */
	if (!err)
		err = fdso_open(entry, config);
	if (err < 0) {
		/* The class rejected the open call.
		 * We need to clean up and tell user space.
		 */
		if (obj)
			put_od_entry(entry);
		idx = err;
	}

	return idx;
}


struct od_table_entry* get_entry_for_od(int od)
{
	struct task_struct *t = current;

	if (!t->od_table)
		return NULL;
	if (od < 0 || od >= MAX_OBJECT_DESCRIPTORS)
		return NULL;
	if (!t->od_table[od].used)
		return NULL;
	return t->od_table + od;
}


asmlinkage long sys_od_open(int fd, int type, int obj_id, void* __user config)
{
	int ret = 0;
	struct file*  file;

	/*
	   1) get file from fd, get inode from file
	   2) lock inode
	   3) try to lookup object
	   4) if not present create and enqueue object, inc inode refcnt
	   5) increment refcnt of object
	   6) alloc od_table_entry, setup ptrs
	   7) unlock inode
	   8) return offset in od_table as OD
	 */

	if (type < MIN_OBJ_TYPE || type > MAX_OBJ_TYPE) {
		ret = -EINVAL;
		goto out;
	}

	file = fget(fd);
	if (!file) {
		ret = -EBADF;
		goto out;
	}

	ret = do_sys_od_open(file, type, obj_id, config);

	fput(file);

out:
	return ret;
}


asmlinkage long sys_od_close(int od)
{
	int ret = -EINVAL;
	struct task_struct *t = current;

	if (od < 0 || od >= MAX_OBJECT_DESCRIPTORS)
		return ret;

	if (!t->od_table || !t->od_table[od].used)
		return ret;


	ret = close_od_entry(t->od_table + od);

	return ret;
}