aboutsummaryrefslogtreecommitdiffstats
path: root/include/litmus/locking.h
blob: cc62fa0cb0440d2a8019303060e94f144736d346 (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
#ifndef LITMUS_LOCKING_H
#define LITMUS_LOCKING_H

#include <linux/list.h>

struct litmus_lock_ops;

#ifdef CONFIG_LITMUS_NESTED_LOCKING
struct nested_info
{
	struct litmus_lock *lock;
	struct task_struct *hp_waiter_eff_prio;
	struct task_struct **hp_waiter_ptr;
    struct binheap_node hp_binheap_node;
};

static inline struct task_struct* top_priority(struct binheap* handle) {
	if(!binheap_empty(handle)) {
		return (struct task_struct*)(binheap_top_entry(handle, struct nested_info, hp_binheap_node)->hp_waiter_eff_prio);
	}
	return NULL;
}

void print_hp_waiters(struct binheap_node* n, int depth);
#endif

#define LOCK_NAME_LEN 32
struct litmus_lock_proc_ops {
	void (*add)(struct litmus_lock *l);
	void (*remove)(struct litmus_lock *l);
};

/* Generic base struct for LITMUS^RT userspace semaphores.
 * This structure should be embedded in protocol-specific semaphores.
 */
struct litmus_lock {
	struct litmus_lock_ops *ops;
	int type;

	int ident;

#ifdef CONFIG_LITMUS_NESTED_LOCKING
	struct nested_info nest;
//#ifdef CONFIG_DEBUG_SPINLOCK
	char cheat_lockdep[2];
	struct lock_class_key key;
//#endif
#endif

	struct litmus_lock_proc_ops *proc;
	struct proc_dir_entry *proc_entry;
	char name[LOCK_NAME_LEN];
};

#ifdef CONFIG_LITMUS_DGL_SUPPORT

#define MAX_DGL_SIZE CONFIG_LITMUS_MAX_DGL_SIZE

typedef struct dgl_wait_state {
	struct task_struct *task;	/* task waiting on DGL */
	struct litmus_lock *locks[MAX_DGL_SIZE];	/* requested locks in DGL */
	int size;			/* size of the DGL */
	int nr_remaining;	/* nr locks remainging before DGL is complete */
	int last_primary;	/* index lock in locks[] that has active priority */
	wait_queue_t wq_nodes[MAX_DGL_SIZE];
} dgl_wait_state_t;

void wake_or_wait_on_next_lock(dgl_wait_state_t *dgl_wait);
void select_next_lock(dgl_wait_state_t* dgl_wait /*, struct litmus_lock* prev_lock*/);

void init_dgl_waitqueue_entry(wait_queue_t *wq_node, dgl_wait_state_t* dgl_wait);
int dgl_wake_up(wait_queue_t *wq_node, unsigned mode, int sync, void *key);
void __waitqueue_dgl_remove_first(wait_queue_head_t *wq, dgl_wait_state_t** dgl_wait, struct task_struct **task);
#endif

typedef int (*lock_op_t)(struct litmus_lock *l);
typedef lock_op_t lock_close_t;
typedef lock_op_t lock_lock_t;
typedef lock_op_t lock_unlock_t;

typedef int (*lock_open_t)(struct litmus_lock *l, void* __user arg);
typedef void (*lock_free_t)(struct litmus_lock *l);

struct litmus_lock_ops {
	/* Current task tries to obtain / drop a reference to a lock.
	 * Optional methods, allowed by default. */
	lock_open_t open;
	lock_close_t close;

	/* Current tries to lock/unlock this lock (mandatory methods). */
	lock_lock_t lock;
	lock_unlock_t unlock;

	/* The lock is no longer being referenced (mandatory method). */
	lock_free_t deallocate;

#ifdef CONFIG_LITMUS_NESTED_LOCKING
	void (*propagate_increase_inheritance)(struct litmus_lock* l, struct task_struct* t, raw_spinlock_t* to_unlock, unsigned long irqflags);
	void (*propagate_decrease_inheritance)(struct litmus_lock* l, struct task_struct* t, raw_spinlock_t* to_unlock, unsigned long irqflags);
#endif

#ifdef CONFIG_LITMUS_DGL_SUPPORT
	raw_spinlock_t* (*get_dgl_spin_lock)(struct litmus_lock *l);
	int (*dgl_lock)(struct litmus_lock *l, dgl_wait_state_t* dgl_wait, wait_queue_t* wq_node);
	int (*is_owner)(struct litmus_lock *l, struct task_struct *t);
	void (*enable_priority)(struct litmus_lock *l, dgl_wait_state_t* dgl_wait);
#endif
};


/*
 Nested inheritance can be achieved with fine-grain locking when there is
 no need for DGL support, presuming locks are acquired in a partial order
 (no cycles!).  However, DGLs allow locks to be acquired in any order.  This
 makes nested inheritance very difficult (we don't yet know a solution) to
 realize with fine-grain locks, so we use a big lock instead.

 Code contains both fine-grain and coarse-grain methods together, side-by-side.
 Each lock operation *IS NOT* surrounded by ifdef/endif to help make code more
 readable.  However, this leads to the odd situation where both code paths
 appear together in code as if they were both active together.

 THIS IS NOT REALLY THE CASE!  ONLY ONE CODE PATH IS ACTUALLY ACTIVE!

 Example:
	lock_global_irqsave(coarseLock, flags);
	lock_fine_irqsave(fineLock, flags);

 Reality (coarse):
	lock_global_irqsave(coarseLock, flags);
	//lock_fine_irqsave(fineLock, flags);

 Reality (fine):
	//lock_global_irqsave(coarseLock, flags);
	lock_fine_irqsave(fineLock, flags);

 Be careful when you read code involving nested inheritance.
 */
#if defined(CONFIG_LITMUS_DGL_SUPPORT)
/* DGL requires a big lock to implement nested inheritance */
#define lock_global_irqsave(lock, flags)		raw_spin_lock_irqsave((lock), (flags))
#define lock_global(lock)						raw_spin_lock((lock))
#define trylock_global_irqsave(lock, flags)		raw_spin_trylock_irqsave((lock), (flags))
#define trylock_global(lock)					raw_spin_trylock((lock))
#define unlock_global_irqrestore(lock, flags)	raw_spin_unlock_irqrestore((lock), (flags))
#define unlock_global(lock)						raw_spin_unlock((lock))

/* fine-grain locking are no-ops with DGL support */
#define lock_fine_irqsave(lock, flags)
#define lock_fine(lock)
#define trylock_fine_irqsave(lock, flags)
#define trylock_fine(lock)
#define unlock_fine_irqrestore(lock, flags)
#define unlock_fine(lock)

#elif defined(CONFIG_LITMUS_NESTED_LOCKING)
/* Use fine-grain locking when DGLs are disabled. */
/* global locking are no-ops without DGL support */
#define lock_global_irqsave(lock, flags)
#define lock_global(lock)
#define trylock_global_irqsave(lock, flags)
#define trylock_global(lock)
#define unlock_global_irqrestore(lock, flags)
#define unlock_global(lock)

#define lock_fine_irqsave(lock, flags)			raw_spin_lock_irqsave((lock), (flags))
#define lock_fine(lock)							raw_spin_lock((lock))
#define trylock_fine_irqsave(lock, flags)		raw_spin_trylock_irqsave((lock), (flags))
#define trylock_fine(lock)						raw_spin_trylock((lock))
#define unlock_fine_irqrestore(lock, flags)		raw_spin_unlock_irqrestore((lock), (flags))
#define unlock_fine(lock)						raw_spin_unlock((lock))

#endif


void suspend_for_lock(void);


#endif