source: trunk/kernel/mm/kcm.c @ 612

Last change on this file since 612 was 567, checked in by alain, 6 years ago

Complete restructuration of kernel locks.

File size: 9.9 KB
Line 
1/*
2 * kcm.c - Per cluster Kernel Cache Manager implementation.
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Alain Greiner    (2016,2017,2018)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <kernel_config.h>
26#include <hal_kernel_types.h>
27#include <hal_special.h>
28#include <busylock.h>
29#include <list.h>
30#include <printk.h>
31#include <bits.h>
32#include <ppm.h>
33#include <thread.h>
34#include <page.h>
35#include <cluster.h>
36#include <kmem.h>
37#include <kcm.h>
38
39
40//////////////////////////////////////////////////////////////////////////////////////
41// This static function returns pointer on an allocated block from an active page.
42// It returns NULL if no block available in selected page.
43// It changes the page status if required.
44//////////////////////////////////////////////////////////////////////////////////////
45// @ kcm      : pointer on kcm allocator.
46// @ kcm_page : pointer on active kcm page to use.
47/////////////////////////////////////////////////////////////////////////////////////
48static void * kcm_get_block( kcm_t      * kcm,
49                             kcm_page_t * kcm_page )
50{
51
52#if DEBUG_KCM
53uint32_t cycle = (uint32_t)hal_get_cycles();
54if( DEBUG_KCM < cycle )
55printk("\n[DBG] %s : thread %x enters for %s / page %x / count %d / active %d\n",
56__FUNCTION__ , CURRENT_THREAD , kmem_type_str( kcm->type ) ,
57(intptr_t)kcm_page , kcm_page->count , kcm_page->active );
58#endif
59
60        assert( kcm_page->active , "kcm_page should be active" );
61
62        // get first block available
63        int32_t index = bitmap_ffs( kcm_page->bitmap , kcm->blocks_nr );
64
65        assert( (index != -1) , "kcm_page should not be full" );
66
67        // allocate block
68        bitmap_clear( kcm_page->bitmap , index );
69
70        // increase kcm_page count
71        kcm_page->count ++;
72
73        // change the kcm_page to busy if no more free block in page
74        if( kcm_page->count >= kcm->blocks_nr )
75        {
76                kcm_page->active = 0;
77                list_unlink( &kcm_page->list);
78                kcm->active_pages_nr --;
79
80                list_add_first( &kcm->busy_root , &kcm_page->list);
81                kcm->busy_pages_nr ++;
82                kcm_page->busy = 1;
83        }
84
85        // compute return pointer
86        void * ptr = (void *)((intptr_t)kcm_page + CONFIG_KCM_SLOT_SIZE
87                     + (index * kcm->block_size) );
88
89#if DEBUG_KCM
90cycle = (uint32_t)hal_get_cycles();
91if( DEBUG_KCM < cycle )
92printk("\n[DBG] %s : thread %x exit / type  %s / ptr %x / page %x / count %d\n",
93       __FUNCTION__ , CURRENT_THREAD , kmem_type_str( kcm->type ) , (intptr_t)ptr ,
94(intptr_t)kcm_page , kcm_page->count );
95#endif
96
97        return ptr;
98}
99
100/////////////////////////////////////////////////////////////////////////////////////
101// This static function releases a previously allocated block.
102// It changes the kcm_page status if required.
103/////////////////////////////////////////////////////////////////////////////////////
104// @ kcm      : pointer on kcm allocator.
105// @ kcm_page : pointer on kcm_page.
106// @ ptr      : pointer on block to be released.
107/////////////////////////////////////////////////////////////////////////////////////
108static void kcm_put_block ( kcm_t      * kcm,
109                            kcm_page_t * kcm_page,
110                            void       * ptr )
111{
112        uint32_t     index;
113
114        // compute block index from block pointer
115        index = ((uint8_t *)ptr - (uint8_t *)kcm_page - CONFIG_KCM_SLOT_SIZE) / kcm->block_size;
116
117        assert( !bitmap_state( kcm_page->bitmap , index ) , "page already freed" );
118        assert( (kcm_page->count > 0) , "count already zero" );
119
120        bitmap_set( kcm_page->bitmap , index );
121        kcm_page->count --;
122
123        // change the page to active if it was busy
124        if( kcm_page->busy )
125        {
126                kcm_page->busy = 0;
127                list_unlink( &kcm_page->list );
128                kcm->busy_pages_nr --;
129
130                list_add_last( &kcm->active_root, &kcm_page->list );
131                kcm->active_pages_nr ++;
132                kcm_page->active = 1;
133        }
134
135        // change the kcm_page to free if last block in active page
136        if( (kcm_page->active) && (kcm_page->count == 0) )
137        {
138                kcm_page->active = 0;
139                list_unlink( &kcm_page->list);
140                kcm->active_pages_nr --;
141
142                list_add_first( &kcm->free_root , &kcm_page->list);
143                kcm->free_pages_nr ++;
144        }
145}
146
147/////////////////////////////////////////////////////////////////////////////////////
148// This static function allocates one page from PPM. It initializes
149// the kcm_page descriptor, and introduces the new kcm_page into freelist.
150/////////////////////////////////////////////////////////////////////////////////////
151static error_t freelist_populate( kcm_t * kcm )
152{
153        page_t     * page;
154        kcm_page_t * kcm_page;
155        kmem_req_t   req;
156
157        // get one page from local PPM
158        req.type  = KMEM_PAGE;
159        req.size  = 0;
160        req.flags = AF_KERNEL;
161        page = kmem_alloc( &req );
162
163        if( page == NULL )
164        {
165                printk("\n[ERROR] in %s : failed to allocate page in cluster %d\n",
166                       __FUNCTION__ , local_cxy );
167                return ENOMEM;
168        }
169
170        // get page base address
171        xptr_t base_xp = ppm_page2base( XPTR( local_cxy , page ) );
172        kcm_page = (kcm_page_t *)GET_PTR( base_xp );
173
174        // initialize KCM-page descriptor
175        bitmap_set_range( kcm_page->bitmap , 0 , kcm->blocks_nr );
176
177        kcm_page->busy          = 0;
178        kcm_page->active        = 0;
179        kcm_page->count      = 0;
180        kcm_page->kcm           = kcm;
181        kcm_page->page          = page;
182
183        // introduce new page in free-list
184        list_add_first( &kcm->free_root , &kcm_page->list );
185        kcm->free_pages_nr ++;
186
187        return 0;
188}
189
190/////////////////////////////////////////////////////////////////////////////////////
191// This private function gets one KCM page from the KCM freelist.
192// It populates the freelist if required.
193/////////////////////////////////////////////////////////////////////////////////////
194static kcm_page_t * freelist_get( kcm_t * kcm )
195{
196        error_t      error;
197        kcm_page_t * kcm_page;
198
199        // get a new page from PPM if freelist empty
200        if( kcm->free_pages_nr == 0 )
201        {
202                error = freelist_populate( kcm );
203                if( error ) return NULL;
204        }
205
206        // get first KCM page from freelist and unlink it
207        kcm_page = LIST_FIRST( &kcm->free_root, kcm_page_t , list );
208        list_unlink( &kcm_page->list );
209        kcm->free_pages_nr --;
210
211        return kcm_page;
212}
213
214//////////////////////////////
215void kcm_init( kcm_t    * kcm,
216                   uint32_t   type )
217{
218        // the kcm_page descriptor mut fit in the KCM slot
219        assert( (sizeof(kcm_page_t) <= CONFIG_KCM_SLOT_SIZE) ,
220                "KCM slot too small\n" );
221
222        // initialize lock
223        busylock_init( &kcm->lock , LOCK_KCM_STATE );
224
225        // initialize KCM type
226        kcm->type = type;
227
228        // initialize KCM page lists
229        kcm->free_pages_nr   = 0;
230        kcm->busy_pages_nr   = 0;
231        kcm->active_pages_nr = 0;
232        list_root_init( &kcm->free_root );
233        list_root_init( &kcm->busy_root );
234        list_root_init( &kcm->active_root );
235
236        // initialize block size
237        uint32_t block_size = ARROUND_UP( kmem_type_size( type ) , CONFIG_KCM_SLOT_SIZE );
238        kcm->block_size = block_size;
239
240        // initialize number of blocks per page
241        uint32_t  blocks_nr = (CONFIG_PPM_PAGE_SIZE - CONFIG_KCM_SLOT_SIZE) / block_size;
242        kcm->blocks_nr = blocks_nr;
243}
244
245///////////////////////////////
246void kcm_destroy( kcm_t * kcm )
247{
248        kcm_page_t   * kcm_page;
249        list_entry_t * iter;
250
251        // get KCM lock
252        busylock_acquire( &kcm->lock );
253
254        // release all free pages
255        LIST_FOREACH( &kcm->free_root , iter )
256        {
257                kcm_page = (kcm_page_t *)LIST_ELEMENT( iter , kcm_page_t , list );
258                list_unlink( iter );
259                kcm->free_pages_nr --;
260                ppm_free_pages( kcm_page->page );
261        }
262
263        // release all active pages
264        LIST_FOREACH( &kcm->active_root , iter )
265        {
266                kcm_page = (kcm_page_t *)LIST_ELEMENT( iter , kcm_page_t , list );
267                list_unlink( iter );
268                kcm->free_pages_nr --;
269                ppm_free_pages( kcm_page->page );
270        }
271
272        // release all busy pages
273        LIST_FOREACH( &kcm->busy_root , iter )
274        {
275                kcm_page = (kcm_page_t *)LIST_ELEMENT( iter , kcm_page_t , list );
276                list_unlink( iter );
277                kcm->free_pages_nr --;
278                ppm_free_pages( kcm_page->page );
279        }
280
281        // release KCM lock
282        busylock_release( &kcm->lock );
283}
284
285///////////////////////////////
286void * kcm_alloc( kcm_t * kcm )
287{
288        kcm_page_t * kcm_page;
289        void       * ptr = NULL;   // pointer on block
290
291        // get lock
292        busylock_acquire( &kcm->lock );
293
294        // get an active page
295        if( list_is_empty( &kcm->active_root ) )  // no active page => get one
296        {
297                // get a page from free list
298                kcm_page = freelist_get( kcm );
299
300                if( kcm_page == NULL )
301                {
302                        busylock_release( &kcm->lock );
303                        return NULL;
304                }
305
306                // insert page in active list
307                list_add_first( &kcm->active_root , &kcm_page->list );
308                kcm->active_pages_nr ++;
309                kcm_page->active = 1;
310        }
311        else                                    // get first page from active list
312        {
313                // get page pointer from active list
314                kcm_page = (kcm_page_t *)LIST_FIRST( &kcm->active_root , kcm_page_t , list );
315        }
316
317        // get a block from selected active page
318        // cannot fail, as an active page cannot be full...
319        ptr  = kcm_get_block( kcm , kcm_page );
320
321        // release lock
322        busylock_release( &kcm->lock );
323
324        return ptr;
325}
326
327///////////////////////////
328void kcm_free( void * ptr )
329{
330        kcm_page_t * kcm_page;
331        kcm_t      * kcm;
332
333        assert( (ptr != NULL) , "pointer cannot be NULL" );
334
335        kcm_page = (kcm_page_t *)((intptr_t)ptr & ~CONFIG_PPM_PAGE_MASK);
336        kcm      = kcm_page->kcm;
337
338        // get lock
339        busylock_acquire( &kcm->lock );
340
341        // release block
342        kcm_put_block( kcm , kcm_page , ptr );
343
344        // release lock
345        busylock_release( &kcm->lock );
346}
347
348////////////////////////////
349void kcm_print (kcm_t * kcm)
350{
351        printk("*** KCM type = %s / free_pages = %d / busy_pages = %d / active_pages = %d\n",
352               kmem_type_str( kcm->type ) ,
353               kcm->free_pages_nr ,
354               kcm->busy_pages_nr ,
355               kcm->active_pages_nr );
356}
Note: See TracBrowser for help on using the repository browser.