summaryrefslogtreecommitdiff
path: root/samples/rust/rust_semaphore_c.c
blob: 7672b0b4c105bed569e612dc673550ce5140f8c6 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Rust semaphore sample (in C, for comparison)
 *
 * This is a C implementation of `rust_semaphore.rs`. Refer to the description
 * in that file for details on the device.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/refcount.h>
#include <linux/wait.h>

#define IOCTL_GET_READ_COUNT _IOR('c', 1, u64)
#define IOCTL_SET_READ_COUNT _IOW('c', 1, u64)

struct semaphore_state {
	struct kref ref;
	struct miscdevice miscdev;
	wait_queue_head_t changed;
	struct mutex mutex;
	size_t count;
	size_t max_seen;
};

struct file_state {
	atomic64_t read_count;
	struct semaphore_state *shared;
};

static int semaphore_consume(struct semaphore_state *state)
{
	DEFINE_WAIT(wait);

	mutex_lock(&state->mutex);
	while (state->count == 0) {
		prepare_to_wait(&state->changed, &wait, TASK_INTERRUPTIBLE);
		mutex_unlock(&state->mutex);
		schedule();
		finish_wait(&state->changed, &wait);
		if (signal_pending(current))
			return -EINTR;
		mutex_lock(&state->mutex);
	}

	state->count--;
	mutex_unlock(&state->mutex);

	return 0;
}

static int semaphore_open(struct inode *nodp, struct file *filp)
{
	struct semaphore_state *shared =
		container_of(filp->private_data, struct semaphore_state, miscdev);
	struct file_state *state;

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

	kref_get(&shared->ref);
	state->shared = shared;
	atomic64_set(&state->read_count, 0);

	filp->private_data = state;

	return 0;
}

static ssize_t semaphore_write(struct file *filp, const char __user *buffer, size_t count,
			       loff_t *ppos)
{
	struct file_state *state = filp->private_data;
	struct semaphore_state *shared = state->shared;

	mutex_lock(&shared->mutex);

	shared->count += count;
	if (shared->count < count)
		shared->count = SIZE_MAX;

	if (shared->count > shared->max_seen)
		shared->max_seen = shared->count;

	mutex_unlock(&shared->mutex);

	wake_up_all(&shared->changed);

	return count;
}

static ssize_t semaphore_read(struct file *filp, char __user *buffer,
			      size_t count, loff_t *ppos)
{
	struct file_state *state = filp->private_data;
	char c = 0;
	int ret;

	if (count == 0 || *ppos > 0)
		return 0;

	ret = semaphore_consume(state->shared);
	if (ret)
		return ret;

	if (copy_to_user(buffer, &c, sizeof(c)))
		return -EFAULT;

	atomic64_add(1, &state->read_count);
	*ppos += 1;
	return 1;
}

static long semaphore_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	struct file_state *state = filp->private_data;
	void __user *buffer = (void __user *)arg;
	u64 value;

	switch (cmd) {
	case IOCTL_GET_READ_COUNT:
		value = atomic64_read(&state->read_count);
		if (copy_to_user(buffer, &value, sizeof(value)))
			return -EFAULT;
		return 0;
	case IOCTL_SET_READ_COUNT:
		if (copy_from_user(&value, buffer, sizeof(value)))
			return -EFAULT;
		atomic64_set(&state->read_count, value);
		return 0;
	default:
		return -EINVAL;
	}
}

static void semaphore_free(struct kref *kref)
{
	struct semaphore_state *device;

	device = container_of(kref, struct semaphore_state, ref);
	kfree(device);
}

static int semaphore_release(struct inode *nodp, struct file *filp)
{
	struct file_state *state = filp->private_data;

	kref_put(&state->shared->ref, semaphore_free);
	kfree(state);
	return 0;
}

static const struct file_operations semaphore_fops = {
	.owner = THIS_MODULE,
	.open = semaphore_open,
	.read = semaphore_read,
	.write = semaphore_write,
	.compat_ioctl = semaphore_ioctl,
	.release = semaphore_release,
};

static struct semaphore_state *device;

static int __init semaphore_init(void)
{
	int ret;
	struct semaphore_state *state;

	pr_info("Rust semaphore sample (in C, for comparison) (init)\n");

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

	mutex_init(&state->mutex);
	kref_init(&state->ref);
	init_waitqueue_head(&state->changed);

	state->miscdev.fops = &semaphore_fops;
	state->miscdev.minor = MISC_DYNAMIC_MINOR;
	state->miscdev.name = "semaphore";

	ret = misc_register(&state->miscdev);
	if (ret < 0) {
		kfree(state);
		return ret;
	}

	device = state;

	return 0;
}

static void __exit semaphore_exit(void)
{
	pr_info("Rust semaphore sample (in C, for comparison) (exit)\n");

	misc_deregister(&device->miscdev);
	kref_put(&device->ref, semaphore_free);
}

module_init(semaphore_init);
module_exit(semaphore_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Rust for Linux Contributors");
MODULE_DESCRIPTION("Rust semaphore sample (in C, for comparison)");