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
|
#include <linux/module.h> #include <linux/kernel.h> #include <linux/slab.h> #include <linux/list.h> #include <linux/rculist.h> #include <linux/spinlock.h> #include <linux/preempt.h>
struct book { int id; char name[64]; char author[64]; int borrow; struct list_head node; struct rcu_head rcu; };
static LIST_HEAD(books); static spinlock_t books_lock;
static void book_reclaim_callback(struct rcu_head *rcu) { struct book *b = container_of(rcu, struct book, rcu);
pr_info("callback free : %lx, preempt_count : %d\n", (unsigned long)b, preempt_count()); kfree(b); }
static void add_book(int id, const char *name, const char *author) { struct book *b;
b = kzalloc(sizeof(struct book), GFP_KERNEL); if(!b) return;
b->id = id; strncpy(b->name, name, sizeof(b->name)); strncpy(b->author, author, sizeof(b->author)); b->borrow = 0;
spin_lock(&books_lock); list_add_rcu(&b->node, &books); spin_unlock(&books_lock); }
static int borrow_book(int id, int async) { struct book *b = NULL; struct book *new_b = NULL; struct book *old_b = NULL;
rcu_read_lock();
list_for_each_entry(b, &books, node) { if(b->id == id) { if(b->borrow) { rcu_read_unlock(); return -1; }
old_b = b; break; } }
if(!old_b) { rcu_read_unlock(); return -1; }
new_b = kzalloc(sizeof(struct book), GFP_ATOMIC); if(!new_b) { rcu_read_unlock(); return -1; }
memcpy(new_b, old_b, sizeof(struct book)); new_b->borrow = 1; spin_lock(&books_lock); list_replace_rcu(&old_b->node, &new_b->node); spin_unlock(&books_lock);
rcu_read_unlock();
if(async) { call_rcu(&old_b->rcu, book_reclaim_callback); }else { synchronize_rcu(); kfree(old_b); }
pr_info("borrow success %d, preempt_count : %d\n", id, preempt_count()); return 0; }
static int is_borrowed_book(int id) { struct book *b;
rcu_read_lock(); list_for_each_entry_rcu(b, &books, node) { if(b->id == id) { rcu_read_unlock(); return b->borrow; } } rcu_read_unlock();
pr_err("not exist book\n"); return -1; }
static int return_book(int id, int async) { struct book *b = NULL; struct book *new_b = NULL; struct book *old_b = NULL;
rcu_read_lock();
list_for_each_entry(b, &books, node) { if(b->id == id) { if(!b->borrow) { rcu_read_unlock(); return -1; }
old_b = b; break; } }
if(!old_b) { rcu_read_unlock(); return -1; }
new_b = kzalloc(sizeof(struct book), GFP_ATOMIC); if(!new_b) { rcu_read_unlock(); return -1; }
memcpy(new_b, old_b, sizeof(struct book)); new_b->borrow = 0; spin_lock(&books_lock); list_replace_rcu(&old_b->node, &new_b->node); spin_unlock(&books_lock);
rcu_read_unlock();
if(async) { call_rcu(&old_b->rcu, book_reclaim_callback); }else { synchronize_rcu(); kfree(old_b); }
pr_info("return success %d, preempt_count : %d\n", id, preempt_count()); return 0; }
static void delete_book(int id, int async) { struct book *b;
spin_lock(&books_lock); list_for_each_entry(b, &books, node) { if(b->id == id) {
list_del_rcu(&b->node); spin_unlock(&books_lock);
if(async) { call_rcu(&b->rcu, book_reclaim_callback); }else { synchronize_rcu(); kfree(b); } return; } } spin_unlock(&books_lock);
pr_err("not exist book\n"); }
static void print_book(int id) { struct book *b;
rcu_read_lock(); list_for_each_entry_rcu(b, &books, node) { if(b->id == id) {
pr_info("id : %d, name : %s, author : %s, borrow : %d, addr : %lx\n", \ b->id, b->name, b->author, b->borrow, (unsigned long)b); rcu_read_unlock(); return; } } rcu_read_unlock();
pr_err("not exist book\n"); }
static void test_example(int async) { add_book(0, "book1", "jb"); add_book(1, "book2", "jb");
print_book(0); print_book(1);
pr_info("book1 borrow : %d\n", is_borrowed_book(0)); pr_info("book2 borrow : %d\n", is_borrowed_book(1));
borrow_book(0, async); borrow_book(1, async);
print_book(0); print_book(1);
return_book(0, async); return_book(1, async);
print_book(0); print_book(1);
delete_book(0, async); delete_book(1, async);
print_book(0); print_book(1); }
static int list_rcu_example_init(void) { spin_lock_init(&books_lock);
test_example(0); test_example(1); return 0; }
static void list_rcu_example_exit(void) { return; }
module_init(list_rcu_example_init); module_exit(list_rcu_example_exit); MODULE_LICENSE("GPL");
|