[IMPROVE] buffer: flush does not "disable" buffer
[kernel/swap-modules.git] / buffer / buffer_queue.c
1 /*
2  *  SWAP Buffer Module
3  *  modules/buffer/buffer_queue.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013  Alexander Aksenov <a.aksenov@samsung.com>: SWAP Buffer implement
22  *
23  */
24
25 /* SWAP buffer queues implementation */
26
27 /* For all memory allocation/deallocation operations, except buffer memory
28  * allocation/deallocation should be used 
29  *  memory_allocation(size_t memory_size)
30  *  memory_free(void *ptr)
31  * defines.
32  * For subbuffer allocation/deallocation operations should be used
33  *  buffer_allocation(size_t subbuffer_size)
34  *  buffer_free(void *ptr, size_t subbuffer_size)
35  * To get buffer pointer for any usage, EXCEPT ALLOCATION AND DEALLOCATION
36  * use the following define:
37  *  buffer_pointer(void *ptr_to_buffer_element_of_swap_buffer_structure)
38  * DO NOT USE SUBBUFFER PTR IN STRUCT SWAP_BUFFER WITHOUT THIS DEFINE!
39  * It will be ok for user space, but fail in kernel space.
40  *
41  * See space_dep_types_and_def.h for details */
42
43
44
45 #include "buffer_queue.h"
46 #include "swap_buffer_to_buffer_queue.h"
47 #include "swap_buffer_errors.h"
48
49 /* Queue structure. Consist of pointers to the first and the last elements of
50  * queue. */
51 struct queue {
52         struct swap_subbuffer *start_ptr;
53         struct swap_subbuffer *end_ptr;
54         struct sync_t queue_sync;
55 };
56
57 /* Write queue */
58 struct queue write_queue = {
59         .start_ptr = NULL,
60         .end_ptr = NULL,
61         .queue_sync = {
62                 .flags = 0x0
63         }
64 };
65
66 /* Read queue */
67 struct queue read_queue = {
68         .start_ptr = NULL,
69         .end_ptr = NULL,
70         .queue_sync = {
71                 .flags = 0x0
72         }
73 };
74
75 /* Pointers array. Points to busy buffers */
76 static struct swap_subbuffer **queue_busy = NULL;
77
78 /* Store last busy element */
79 static unsigned int queue_busy_last_element;
80
81 /* Subbuffers count */
82 static unsigned int queue_subbuffer_count = 0;
83
84 /* One subbuffer size */
85 static size_t queue_subbuffer_size = 0;
86
87 /* Busy list sync */
88 static struct sync_t buffer_busy_sync = {
89         .flags = 0x0
90 };
91
92 /* Memory pages count in one subbuffer */
93 static int pages_order_in_subbuffer = 0;
94
95
96 int buffer_queue_allocation(size_t subbuffer_size,
97                             unsigned int subbuffers_count)
98 {
99         unsigned int i = 0;
100         unsigned int j = 0;
101         unsigned int allocated_buffers = 0;
102         unsigned int allocated_structs = 0;
103         struct swap_subbuffer *clean_tmp_struct;
104         int result;
105
106         /* Static varibles initialization */
107         queue_subbuffer_size = subbuffer_size;
108         queue_subbuffer_count = subbuffers_count;
109         queue_busy_last_element = 0;
110
111         /* Set variable pages_in_subbuffer. It is used for allocation and
112          * deallocation memory pages and its value is returned from
113          * swap_buffer_get() and contains page count in one subbuffer.
114          * All this useful only in kernel space. In userspace it is dummy.*/
115         set_pages_order_in_subbuffer(queue_subbuffer_size);
116         /* Sync primitives initialization */
117         sync_init(&read_queue.queue_sync);
118         sync_init(&write_queue.queue_sync);
119         sync_init(&buffer_busy_sync);
120
121         /* Memory allocation for queue_busy */
122         queue_busy = memory_allocation(sizeof(&queue_busy) * queue_subbuffer_count);
123
124         if (!queue_busy) {
125                 result = -E_SB_NO_MEM_QUEUE_BUSY;
126                 goto buffer_allocation_error_ret;
127         }
128
129         /* Memory allocation for swap_subbuffer structures */
130
131         /* Allocation for first structure. */
132         write_queue.start_ptr = memory_allocation(sizeof(&write_queue.start_ptr));
133
134         if (!write_queue.start_ptr) {
135                 result = -E_SB_NO_MEM_BUFFER_STRUCT;
136                 goto buffer_allocation_queue_busy_free;
137         }
138         allocated_structs++;
139
140
141         write_queue.end_ptr = write_queue.start_ptr;
142
143         write_queue.end_ptr->next_in_queue = NULL;
144         write_queue.end_ptr->full_buffer_part = 0;
145         write_queue.end_ptr->data_buffer = buffer_allocation(queue_subbuffer_size);
146         if (!write_queue.end_ptr->data_buffer) {
147                 print_err("Cannot allocate memory for buffer 1\n");
148                 result = -E_SB_NO_MEM_DATA_BUFFER;
149                 goto buffer_allocation_error_free;
150         }
151         allocated_buffers++;
152
153         print_msg(" Buffer allocated = 0x%x\n", (unsigned long)write_queue.end_ptr->data_buffer);
154
155         sync_init(&write_queue.end_ptr->buffer_sync);
156
157         /* Buffer initialization */
158         memset(buffer_address(write_queue.end_ptr->data_buffer), 0, queue_subbuffer_size);
159
160         /* Allocation for other structures. */
161         for (i = 1; i < queue_subbuffer_count; i++) {
162                 write_queue.end_ptr->next_in_queue =
163                     memory_allocation(sizeof(write_queue.end_ptr->next_in_queue));
164                 if (!write_queue.end_ptr->next_in_queue) {
165                         result = -E_SB_NO_MEM_BUFFER_STRUCT;
166                         goto buffer_allocation_error_free;
167                 }
168                 allocated_structs++;
169
170                 /* Now next write_queue.end_ptr is next */
171                 write_queue.end_ptr = write_queue.end_ptr->next_in_queue;
172
173                 write_queue.end_ptr->next_in_queue = NULL;
174                 write_queue.end_ptr->full_buffer_part = 0;
175                 write_queue.end_ptr->data_buffer = 
176                         buffer_allocation(queue_subbuffer_size);
177                 if (!write_queue.end_ptr->data_buffer) {
178                         result = -E_SB_NO_MEM_DATA_BUFFER;
179                         goto buffer_allocation_error_free;
180                 }
181                 allocated_buffers++;
182
183                 print_msg(" Buffer allocated = 0x%x, pages_order = %d\n", 
184                           (unsigned long)buffer_address(write_queue.end_ptr->data_buffer), 
185                           pages_order_in_subbuffer);
186
187                 sync_init(&write_queue.end_ptr->buffer_sync);
188
189                 /* Buffer initialization */
190                 memset(buffer_address(write_queue.end_ptr->data_buffer), 0,
191                        queue_subbuffer_size);
192         }
193
194         return E_SB_SUCCESS;
195
196         /* In case of errors, this code is called */
197         /* Free all previously allocated memory */
198 buffer_allocation_error_free:
199         clean_tmp_struct = write_queue.start_ptr;
200
201         for (j = 0; j < allocated_structs; j++) {
202                 clean_tmp_struct = write_queue.start_ptr;
203                 if (allocated_buffers) {
204                         buffer_free(clean_tmp_struct->data_buffer, queue_subbuffer_size);
205                         allocated_buffers--;
206                 }
207                 if (write_queue.start_ptr != write_queue.end_ptr)
208                         write_queue.start_ptr = write_queue.start_ptr->next_in_queue;
209                 memory_free(clean_tmp_struct);
210         }
211         write_queue.end_ptr = NULL;
212         write_queue.start_ptr = NULL;
213
214 buffer_allocation_queue_busy_free:
215         memory_free(queue_busy);
216         queue_busy = NULL;
217
218 buffer_allocation_error_ret:
219         return result;
220 }
221
222 void buffer_queue_free(void)
223 {
224         struct swap_subbuffer *tmp = NULL;
225
226         //TODO Lock read list semaphore to prevent getting subbuffer from read list 
227         /* Set all write buffers to read list */
228         set_all_to_read_list();
229
230         /* Free buffers and structures memory that are in read list */
231         while (read_queue.start_ptr) {
232                 tmp = read_queue.start_ptr;
233                 read_queue.start_ptr = read_queue.start_ptr->next_in_queue;
234                 buffer_free(tmp->data_buffer, queue_subbuffer_size);
235                 print_msg(" Buffer free = 0x%x\n", (unsigned long)
236                            buffer_address(tmp->data_buffer));
237                 memory_free(tmp);
238         }
239
240         /* Free busy_list */
241         memory_free(queue_busy);
242         queue_busy = NULL;
243
244         queue_subbuffer_size = 0;
245         queue_subbuffer_count = 0;
246         read_queue.start_ptr = NULL;
247         read_queue.end_ptr = NULL;
248         write_queue.start_ptr = NULL;
249         write_queue.end_ptr = NULL;
250 }
251
252 static unsigned int is_buffer_enough(struct swap_subbuffer *subbuffer,
253                                      size_t size)
254 {
255         /* XXX Think about checking full_buffer_part for correctness 
256          * (<queue_subbuffer_size). It should be true, but if isn't (due to sources
257          * chaning, etc.) this function should be true! */
258         return ((queue_subbuffer_size-subbuffer->full_buffer_part) >= size) ? 1 : 0;
259 }
260
261 /* Get first subbuffer from read list */
262 struct swap_subbuffer *get_from_read_list(void)
263 {
264         struct swap_subbuffer *result = NULL;
265
266         /* Lock read sync primitive */
267         sync_lock(&read_queue.queue_sync);
268
269         if (read_queue.start_ptr == NULL) {
270                 result = NULL;
271                 goto get_from_read_list_unlock;
272         }
273
274         result = read_queue.start_ptr;
275
276         /* If this is the last readable buffer, read_queue.start_ptr next time will 
277          * points to NULL and that case is handled in the beginning of function
278          */
279         if (read_queue.start_ptr == read_queue.end_ptr) {
280                 read_queue.end_ptr = NULL;
281         }
282         read_queue.start_ptr = read_queue.start_ptr->next_in_queue;
283
284 get_from_read_list_unlock:
285         /* Unlock read sync primitive */
286         sync_unlock(&read_queue.queue_sync);
287
288         return result;
289 }
290
291 /* Add subbuffer to read list */
292 void add_to_read_list(struct swap_subbuffer *subbuffer)
293 {
294
295         /* Lock read sync primitive */
296         sync_lock(&read_queue.queue_sync);
297
298         if (!read_queue.start_ptr)
299                 read_queue.start_ptr = subbuffer;
300
301         if (read_queue.end_ptr) {
302                 read_queue.end_ptr->next_in_queue = subbuffer;
303
304                 read_queue.end_ptr = read_queue.end_ptr->next_in_queue;
305         } else {
306                 read_queue.end_ptr = subbuffer;
307         }
308         read_queue.end_ptr->next_in_queue = NULL;
309
310         /* Unlock read sync primitive */
311         sync_unlock(&read_queue.queue_sync);
312 }
313
314 /* Call add to read list and callback function from driver module */
315 int add_to_read_list_with_callback(struct swap_subbuffer *subbuffer)
316 {
317         int result = 0;
318
319         add_to_read_list(subbuffer);
320         // TODO Handle ret value
321         result = swap_buffer_callback(subbuffer);
322
323         return result;
324 }
325
326 /* Get first writable subbuffer from write list */
327 struct swap_subbuffer *get_from_write_list(size_t size, void **ptr_to_write)
328 {
329         struct swap_subbuffer *result = NULL;
330
331         /* Callbacks are called at the end of the function to prevent deadlocks */
332         struct queue callback_queue = {
333                 .start_ptr = NULL,
334                 .end_ptr = NULL,
335                 .queue_sync = {
336                         .flags = 0x0
337                 }
338         };
339         struct swap_subbuffer *tmp_buffer = NULL;
340
341         /* Init pointer */
342         *ptr_to_write = NULL;
343
344         /* Lock write list sync primitive */
345         sync_lock(&write_queue.queue_sync);
346
347         while (write_queue.start_ptr) {
348                 /* If start points to NULL => list is empty => exit */
349                 if (!write_queue.start_ptr) {
350                         result = NULL;
351                         goto get_from_write_list_unlock;
352                 }
353
354                 /* We're found subbuffer */
355                 if (is_buffer_enough(write_queue.start_ptr, size)) {
356
357                         result = write_queue.start_ptr;
358                         *ptr_to_write = (void *)((unsigned long)
359                                                  (buffer_address(result->data_buffer)) +
360                                                  result->full_buffer_part);
361
362                         /* Add data size to full_buffer_part. Very important to do it in
363                          * write_queue.queue_sync spinlock */
364                         write_queue.start_ptr->full_buffer_part += size;
365
366                         /* Lock rw sync. Should be unlocked in swap_buffer_write() */
367                         sync_lock(&result->buffer_sync);
368                         break;
369                 /* This subbuffer is not enough => it goes to read list */
370                 } else {
371                         result = write_queue.start_ptr;
372
373                         /* If we reached end of the list */
374                         if (write_queue.start_ptr == write_queue.end_ptr) {
375                                 write_queue.end_ptr = NULL;
376                         }
377
378                         /* Move start write pointer */
379                         write_queue.start_ptr = write_queue.start_ptr->next_in_queue;
380
381                         /* Add to callback list */
382                         if (!callback_queue.start_ptr)
383                                 callback_queue.start_ptr = result;
384
385                         if (callback_queue.end_ptr)
386                                 callback_queue.end_ptr->next_in_queue = result;
387                         callback_queue.end_ptr = result;
388                         callback_queue.end_ptr->next_in_queue = NULL;
389                         result = NULL;
390                 }
391         }
392
393 get_from_write_list_unlock:
394         /* Unlock write list sync primitive */
395         sync_unlock(&write_queue.queue_sync);
396
397         /* Adding buffers to read list and calling callbacks */
398         for (tmp_buffer = NULL; callback_queue.start_ptr; ) {
399                 if (callback_queue.start_ptr == callback_queue.end_ptr)
400                         callback_queue.end_ptr = NULL;
401
402                 tmp_buffer = callback_queue.start_ptr;
403                 callback_queue.start_ptr = callback_queue.start_ptr->next_in_queue;
404
405                 add_to_read_list_with_callback(tmp_buffer);
406         }
407
408         return result;
409 }
410
411 /* Add subbuffer to write list */
412 void add_to_write_list(struct swap_subbuffer *subbuffer)
413 {
414         sync_lock(&write_queue.queue_sync);
415
416         /* Reinitialize */
417         // TODO Useless memset
418 //      memset(buffer_address(subbuffer->data_buffer), 0, queue_subbuffer_size);
419         subbuffer->full_buffer_part = 0;
420
421         if (!write_queue.start_ptr)
422                 write_queue.start_ptr = subbuffer;
423
424         if (write_queue.end_ptr) {
425                 write_queue.end_ptr->next_in_queue = subbuffer;
426                 write_queue.end_ptr = write_queue.end_ptr->next_in_queue;
427         } else {
428                 write_queue.end_ptr = subbuffer;
429         }
430         write_queue.end_ptr->next_in_queue = NULL;
431
432         sync_unlock(&write_queue.queue_sync);
433 }
434
435 /* Add subbuffer to busy list when it is read from out of the buffer */
436 void add_to_busy_list(struct swap_subbuffer *subbuffer)
437 {
438         /* Lock busy sync primitive */
439         sync_lock(&buffer_busy_sync);
440
441         subbuffer->next_in_queue = NULL;
442         queue_busy[queue_busy_last_element] = subbuffer;
443         queue_busy_last_element += 1;
444
445         /* Unlock busy sync primitive */
446         sync_unlock(&buffer_busy_sync);
447 }
448
449 /* Remove subbuffer from busy list when it is released */
450 int remove_from_busy_list(struct swap_subbuffer *subbuffer)
451 {
452         int result = -E_SB_NO_SUBBUFFER_IN_BUSY; // For sanitization
453         int i;
454
455         /* Lock busy list sync primitive */
456         sync_lock(&buffer_busy_sync);
457
458         /* Sanitization and removing */
459         for (i = 0; i < queue_busy_last_element; i++) {
460                 if (queue_busy[i] == subbuffer) {
461                         /* Last element goes here and length is down 1 */
462                         queue_busy[i] = queue_busy[queue_busy_last_element - 1];
463                         queue_busy_last_element -= 1;
464                         result = E_SB_SUCCESS;
465                         break;
466                 }
467         }
468
469         /* Unlock busy list sync primitive */
470         sync_unlock(&buffer_busy_sync);
471
472         return result;
473 }
474
475 /* Get subbuffers count in read list */
476 /* XXX Think about locks */
477 int get_full_buffers_count(void)
478 {
479         int result = 0;
480         struct swap_subbuffer *buffer = read_queue.start_ptr;
481
482         while (buffer && buffer->full_buffer_part) {
483                 result += 1;
484                 buffer = buffer->next_in_queue;
485         }
486
487         return result;
488 }
489
490 /* Set all subbuffers in write list to read list */
491 void set_all_to_read_list(void)
492 {
493         struct swap_subbuffer *buffer = write_queue.start_ptr;
494
495         /* Locking write sync primitive */
496         sync_lock(&write_queue.queue_sync);
497
498         while (write_queue.start_ptr &&
499                write_queue.start_ptr->full_buffer_part) {
500                 /* Waiting till semaphore should be posted */
501
502 // TODO To think: It's not bad as it is, but maybe it would be better locking
503 // semaphore while changing its list? (Not bad now, cause buffer should have
504 // already been stopped).
505
506                 sync_lock(&buffer->buffer_sync);
507
508                 sync_unlock(&buffer->buffer_sync);
509
510                 buffer = write_queue.start_ptr;
511
512                 /* If we reached end of the list */
513                 if (write_queue.start_ptr == write_queue.end_ptr) {
514                         write_queue.end_ptr = NULL;
515                 }
516                 write_queue.start_ptr = write_queue.start_ptr->next_in_queue;
517
518                 add_to_read_list(buffer);
519         }
520
521         /* Unlocking write primitive */
522         sync_unlock(&write_queue.queue_sync);
523 }
524
525 /* Get subbuffers count in busy list */
526 /* XXX Think abount lock */
527 int get_busy_buffers_count(void)
528 {
529         return queue_busy_last_element;
530 }
531
532 /* Get memory pages count in subbuffer */
533 int get_pages_count_in_subbuffer(void)
534 {
535 /* Return 1 if pages order 0, or 2 of power pages_order_in_subbuffer otherwise */
536         return (pages_order_in_subbuffer) ? 2 << (pages_order_in_subbuffer - 1) : 1;
537 }