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

Last change on this file since 310 was 182, checked in by max@…, 7 years ago

lock leak

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