summaryrefslogtreecommitdiff
path: root/test/test_rcu_defer.c
blob: 820d050eadc00d26a0911260889b0e63d61dbd17 (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
213
214
215
/*
 * Copyright (c) 2014-2018 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * This test module is a stress test, expected to never terminate, of the
 * work deferring functionality of the rcu module. It creates three
 * threads, a producer, a consumer, and a reader. The producer allocates
 * a page and writes it. It then transfers the page to the consumer, using
 * the rcu interface to update the global page pointer. Once at the
 * consumer, the rcu interface is used to defer the release of the page.
 * Concurrently, the reader accesses the page and checks its content when
 * available. These accesses are performed inside a read-side critical
 * section and should therefore never fail.
 *
 * Each thread regularly prints a string to report that it's making progress.
 */

#include <stddef.h>
#include <stdio.h>
#include <string.h>

#include <kern/condition.h>
#include <kern/error.h>
#include <kern/kmem.h>
#include <kern/macros.h>
#include <kern/mutex.h>
#include <kern/panic.h>
#include <kern/rcu.h>
#include <kern/thread.h>
#include <kern/work.h>
#include <machine/page.h>
#include <test/test.h>
#include <vm/vm_kmem.h>

#define TEST_LOOPS_PER_PRINT 100000

struct test_pdsc {
    struct work work;
    void *addr;
};

#define TEST_VALIDATION_BYTE 0xab

static struct mutex test_lock;
static struct condition test_condition;
static struct test_pdsc *test_pdsc;

static struct kmem_cache test_pdsc_cache;

static void
test_alloc(void *arg)
{
    struct test_pdsc *pdsc;
    unsigned long nr_loops;

    (void)arg;

    nr_loops = 0;

    mutex_lock(&test_lock);

    for (;;) {
        while (test_pdsc != NULL) {
            condition_wait(&test_condition, &test_lock);
        }

        pdsc = kmem_cache_alloc(&test_pdsc_cache);

        if (pdsc != NULL) {
            pdsc->addr = vm_kmem_alloc(PAGE_SIZE);

            if (pdsc->addr != NULL) {
                memset(pdsc->addr, TEST_VALIDATION_BYTE, PAGE_SIZE);
            }
        }

        rcu_store_ptr(test_pdsc, pdsc);
        condition_signal(&test_condition);

        if ((nr_loops % TEST_LOOPS_PER_PRINT) == 0) {
            printf("alloc ");
        }

        nr_loops++;
    }
}

static void
test_deferred_free(struct work *work)
{
    struct test_pdsc *pdsc;

    pdsc = structof(work, struct test_pdsc, work);

    if (pdsc->addr != NULL) {
        vm_kmem_free(pdsc->addr, PAGE_SIZE);
    }

    kmem_cache_free(&test_pdsc_cache, pdsc);
}

static void
test_free(void *arg)
{
    struct test_pdsc *pdsc;
    unsigned long nr_loops;

    (void)arg;

    nr_loops = 0;

    mutex_lock(&test_lock);

    for (;;) {
        while (test_pdsc == NULL) {
            condition_wait(&test_condition, &test_lock);
        }

        pdsc = test_pdsc;
        rcu_store_ptr(test_pdsc, NULL);

        if (pdsc != NULL) {
            work_init(&pdsc->work, test_deferred_free);
            rcu_defer(&pdsc->work);
        }

        condition_signal(&test_condition);

        if ((nr_loops % TEST_LOOPS_PER_PRINT) == 0) {
            printf("free ");
        }

        nr_loops++;
    }
}

static void
test_read(void *arg)
{
    const struct test_pdsc *pdsc;
    const unsigned char *s;
    unsigned long nr_loops;

    (void)arg;

    nr_loops = 0;

    for (;;) {
        rcu_read_enter();

        pdsc = rcu_load_ptr(test_pdsc);

        if (pdsc != NULL) {
            s = (const unsigned char *)pdsc->addr;

            if (s != NULL) {
                for (unsigned int i = 0; i < PAGE_SIZE; i++) {
                    if (s[i] != TEST_VALIDATION_BYTE) {
                        panic("invalid content");
                    }
                }

                if ((nr_loops % TEST_LOOPS_PER_PRINT) == 0) {
                    printf("read ");
                }

                nr_loops++;
            }
        }

        rcu_read_leave();
    }
}

void
test_setup(void)
{
    struct thread_attr attr;
    struct thread *thread;
    int error;

    condition_init(&test_condition);
    mutex_init(&test_lock);

    kmem_cache_init(&test_pdsc_cache, "test_pdsc",
                    sizeof(struct test_pdsc), 0, NULL, 0);

    thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_alloc");
    thread_attr_set_detached(&attr);
    error = thread_create(&thread, &attr, test_alloc, NULL);
    error_check(error, "thread_create");

    thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_free");
    thread_attr_set_detached(&attr);
    error = thread_create(&thread, &attr, test_free, NULL);
    error_check(error, "thread_create");

    thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_read");
    thread_attr_set_detached(&attr);
    error = thread_create(&thread, &attr, test_read, NULL);
    error_check(error, "thread_create");
}