source: trunk/kernel/libk/user_dir.c @ 632

Last change on this file since 632 was 629, checked in by alain, 6 years ago

Remove the "giant" rwlock protecting the GPT, and
use the GPT_LOCKED attribute in each PTE to prevent
concurrent modifications of one GPT entry.
The version number has been incremented to 2.1.

File size: 17.9 KB
RevLine 
[613]1/*
2 * user_dir.c - kernel DIR related operations implementation.
3 *
[629]4 * Authors   Alain   Greiner (2016,2017,2018,2019)
[613]5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <hal_irqmask.h>
27#include <hal_remote.h>
28#include <thread.h>
29#include <xlist.h>
30#include <scheduler.h>
31#include <remote_queuelock.h>
32#include <user_dir.h>
33
34
35/////////////////////////////////////////////
36xptr_t user_dir_from_ident( intptr_t  ident )
37{
38    // get pointer on local process_descriptor
39    process_t * process = CURRENT_THREAD->process;
40
41    // get pointers on reference process
42    xptr_t      ref_xp  = process->ref_xp;
43    cxy_t       ref_cxy = GET_CXY( ref_xp );
44    process_t * ref_ptr = GET_PTR( ref_xp );
45
46    // get extended pointers on open directories list and lock 
47    xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->dir_root );
48    xptr_t lock_xp = XPTR( ref_cxy , &ref_ptr->dir_lock );
49
50    // get lock protecting open directories list
51    remote_queuelock_acquire( lock_xp );
52 
53    // scan reference process dir list
54    xptr_t           iter_xp;
55    xptr_t           dir_xp;
56    cxy_t            dir_cxy;
57    user_dir_t     * dir_ptr;
58    intptr_t         current;
59    bool_t           found = false;
60           
61    XLIST_FOREACH( root_xp , iter_xp )
62    {
63        dir_xp  = XLIST_ELEMENT( iter_xp , user_dir_t , list );
64        dir_cxy = GET_CXY( dir_xp );
65        dir_ptr = GET_PTR( dir_xp );
66        current = (intptr_t)hal_remote_lpt( XPTR( dir_cxy , &dir_ptr->ident ) );   
67        if( ident == current )
68        {
69            found = true;
70            break;
71        }
72    }
73
74    // relese lock protecting open directories list
75    remote_queuelock_release( lock_xp );
76 
77    if( found == false )  return XPTR_NULL;
78    else                  return dir_xp;
79
80}  // end user_dir_from_ident()
81
[614]82//////////////////////////////////////////////////
83user_dir_t * user_dir_create( vfs_inode_t * inode,
84                              xptr_t        ref_xp )
[613]85{ 
86    user_dir_t    * dir;               // local pointer on created user_dir_t
87    vseg_t        * vseg;              // local pointer on dirent array vseg
88    uint32_t        vseg_size;         // size of vseg in bytes
89    process_t     * ref_ptr;           // local pointer on reference process
90    cxy_t           ref_cxy;           // reference process cluster identifier
[614]91    pid_t           ref_pid;           // reference process PID
[613]92    xptr_t          gpt_xp;            // extended pointer on reference process GPT
[629]93    uint32_t        attr;              // attributes for all GPT entries
[613]94    uint32_t        dirents_per_page;  // number of dirent descriptors per page
95    xptr_t          page_xp;           // extended pointer on page descriptor 
96    page_t        * page;              // local pointer on page descriptor
97    xptr_t          base_xp;           // extended pointer on physical page base
98    struct dirent * base;              // local pointer on physical page base
99    uint32_t        total_dirents;     // total number of dirents in dirent array
100    uint32_t        total_pages;       // total number of pages for dirent array
[629]101    vpn_t           vpn_base;          // first page in dirent array vseg
102    vpn_t           vpn;               // current page in dirent array vseg
[613]103    ppn_t           ppn;               // ppn of currently allocated physical page
104    uint32_t        entries;           // number of dirent actually comied in one page
105    uint32_t        first_entry;       // index of first dentry to copy in dirent array
106    bool_t          done;              // last entry found and copied when true
107    list_entry_t    root;              // root of temporary list of allocated pages
108    uint32_t        page_id;           // page index in list of physical pages
109    kmem_req_t      req;               // kmem request descriptor
[629]110    ppn_t           fake_ppn;          // unused, but required by hal_gptlock_pte()
111    uint32_t        fake_attr;         // unused, but required by hal_gptlock_pte()
[613]112    error_t         error;
113
[629]114    // get cluster, local pointer, and pid of reference process
[614]115    ref_cxy = GET_CXY( ref_xp );
116    ref_ptr = GET_PTR( ref_xp );
117    ref_pid = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->pid ) );
[613]118
119#if DEBUG_USER_DIR
120uint32_t cycle = (uint32_t)hal_get_cycles();
121thread_t * this = CURRENT_THREAD;
122if( cycle > DEBUG_USER_DIR )
[614]123printk("\n[%s] thread[%x,%x] enter for inode (%x,%x) and process %x / cycle %d\n",
124__FUNCTION__, this->process->pid, this->trdid, local_cxy, inode, ref_pid, cycle );
[613]125#endif
126
127// check dirent size
128assert( ( sizeof(struct dirent) == 64), "sizeof(dirent) != 64\n");
129
130    // compute number of dirent per page
131    dirents_per_page = CONFIG_PPM_PAGE_SIZE >> 6;
132   
133    // initialise temporary list of pages
134    list_root_init( &root );
135
136    // allocate memory for a local user_dir descriptor
137    req.type  = KMEM_DIR;
138    req.flags = AF_ZERO;
139    dir       = kmem_alloc( &req );
140
141    if( dir == NULL )
142    {
143        printk("\n[ERROR] in %s : cannot allocate user_dir_t in cluster %x\n",
144        __FUNCTION__, local_cxy );
145        return NULL;
146    }
147
148    // Build an initialize the dirent array as a list of physical pages.
149    // For each iteration in this while loop:
150    // - allocate one physical 4 Kbytes (64 dirent slots)
151    // - call the relevant FS specific function to scan the directory mapper,
152    //   and copy up to 64 entries in the page.
153    // - register the page in a temporary list using the embedded page list_entry
154    // - exit when the last entry has been found (done == true).
155
156    // initialize loops variables
157    done          = false;
158    total_dirents = 0;
159    total_pages   = 0;
160    first_entry   = 0;
161
162    while( done == false )  // loop on physical pages
163    {
164        // allocate one physical page
165        req.type  = KMEM_PAGE;
166        req.size  = 0;
167        req.flags = AF_ZERO;
168        page      = kmem_alloc( &req );
169
170        if( page == NULL )
171        {
172            printk("\n[ERROR] in %s : cannot allocate page in cluster %x\n",
173            __FUNCTION__, ref_cxy );
174            goto user_dir_create_failure;
175        }
176
177        // get pointer on page base (array of dirents)
178        page_xp  = XPTR( local_cxy , page );
179        base_xp  = ppm_page2base( page_xp );
180        base     = GET_PTR( base_xp );
181
182        // call the relevant FS specific function to copy up to 64 dirents in page
183        error = vfs_fs_get_user_dir( inode,
184                                     base,
185                                     dirents_per_page,
186                                     first_entry,
187                                     false,        // don't create missing inodes
188                                     &entries,
189                                     &done );
190        if( error )
191        {
192            printk("\n[ERROR] in %s : cannot initialise dirent array in cluster %x\n",
193            __FUNCTION__, ref_cxy );
194            goto user_dir_create_failure;
195        }
196
197        // increment number of written dirents
198        total_dirents += entries;
199
200        // register page in temporary list
201        list_add_last( &root , &page->list ); 
202        total_pages++; 
203
204        // set first_entry for next iteration
205        first_entry = total_dirents;
206
207    } // end while
208       
[614]209#if DEBUG_USER_DIR
210if( cycle > DEBUG_USER_DIR )
211printk("\n[%s] thread[%x,%x] initialised dirent array / %d entries\n",
212__FUNCTION__, this->process->pid, this->trdid, total_dirents, cycle );
213#endif
214
[613]215    // compute required vseg size for a 64 bytes dirent
216    vseg_size = total_dirents << 6;
217
218    // create an ANON vseg and register it in reference process VSL
219    if( local_cxy == ref_cxy )
220    {
[614]221        vseg = vmm_create_vseg( ref_ptr,
[613]222                                VSEG_TYPE_ANON,
223                                0,                      // vseg base (unused)
224                                vseg_size,
225                                0,                      // file offset (unused)
226                                0,                      // file_size (unused)
227                                XPTR_NULL,              // mapper (unused)
[614]228                                local_cxy );
[613]229    }
230    else
231    {
232        rpc_vmm_create_vseg_client( ref_cxy,
233                                    ref_ptr,
234                                    VSEG_TYPE_ANON,
235                                    0,                     // vseg base (unused)
236                                    vseg_size,
237                                    0,                     // file offset (unused)
238                                    0,                     // file size (unused)
239                                    XPTR_NULL,             // mapper (unused)
[614]240                                    local_cxy,
[613]241                                    &vseg ); 
242    }
[614]243
[613]244    if( vseg == NULL )
245    {
[614]246        printk("\n[ERROR] in %s : cannot create vseg for user_dir in cluster %x\n",
[613]247        __FUNCTION__, ref_cxy);
248        goto user_dir_create_failure;
249    }
250
[614]251#if DEBUG_USER_DIR
[613]252if( cycle > DEBUG_USER_DIR )
253printk("\n[%s] thread[%x,%x] allocated vseg ANON / base %x / size %x\n",
[614]254__FUNCTION__, this->process->pid, this->trdid, vseg->min, vseg->max - vseg->min );
[613]255#endif
256
257// check vseg size
258assert( (total_pages == hal_remote_l32( XPTR( ref_cxy , &vseg->vpn_size ) ) ),
259"unconsistent vseg size for dirent array" );
260
[629]261    // build extended pointer on reference process GPT
[613]262    gpt_xp         = XPTR( ref_cxy , &ref_ptr->vmm.gpt );
263
[629]264    // build PTE attributes
265    attr = GPT_MAPPED   |
266           GPT_SMALL    |
267           GPT_READABLE |
268           GPT_CACHABLE |
269           GPT_USER     ;
270
[613]271    // get first vpn from vseg descriptor
[629]272    vpn_base = hal_remote_l32( XPTR( ref_cxy , &vseg->vpn_base ) );
[613]273
274    // scan the list of allocated physical pages to map
[629]275    // all physical pages in the reference process GPT
[613]276    page_id = 0;
277    while( list_is_empty( &root ) == false )
278    {
279        // get pointer on first page descriptor
280        page = LIST_FIRST( &root , page_t , list );
281
282        // compute ppn
283        ppn = ppm_page2ppn( XPTR( local_cxy , page ) );
[629]284
285        // compute vpn
286        vpn = vpn_base + page_id;
[613]287       
[629]288        // lock the PTE (and create PT2 if required)
289        error = hal_gpt_lock_pte( gpt_xp,
290                                  vpn,
291                                  &fake_attr,
292                                  &fake_ppn );
[613]293        if( error )
294        {
295            printk("\n[ERROR] in %s : cannot map vpn %x in GPT\n",
[629]296            __FUNCTION__, vpn );
[619]297
298            // delete the vseg
[629]299            if( ref_cxy == local_cxy)
300                vmm_delete_vseg( ref_pid, vpn_base << CONFIG_PPM_PAGE_SHIFT );
301            else 
302                rpc_vmm_delete_vseg_client( ref_cxy, ref_pid, vpn_base << CONFIG_PPM_PAGE_SHIFT );
[619]303
[613]304            // release the user_dir descriptor
305            req.type = KMEM_DIR;
306            req.ptr  = dir;
307            kmem_free( &req );
308            return NULL;
309        }
310
[629]311        // set PTE in GPT                         
312        hal_gpt_set_pte( gpt_xp,
313                         vpn,
314                         attr,
315                         ppn );
316
[614]317#if DEBUG_USER_DIR
[613]318if( cycle > DEBUG_USER_DIR )
319printk("\n[%s] thread[%x,%x] mapped vpn %x to ppn %x\n",
[614]320__FUNCTION__, this->process->pid, this->trdid, vpn + page_id, ppn );
[613]321#endif
322
323        // remove the page from temporary list
324        list_unlink( &page->list );
325
326        page_id++;
327
328    }  // end map loop
329
330// check number of pages
331assert( (page_id == total_pages) , "unconsistent pages number\n" );
332
333    // initialise user_dir_t structure
334    dir->current = 0;
335    dir->entries = total_dirents;
[629]336    dir->ident   = (intptr_t)(vpn_base << CONFIG_PPM_PAGE_SHIFT);
[613]337
338    // build extended pointers on root and lock of user_dir xlist in ref process
339    xptr_t root_xp  = XPTR( ref_cxy , &ref_ptr->dir_root );
340    xptr_t lock_xp  = XPTR( ref_cxy , &ref_ptr->dir_lock );
341
342    // build extended pointer on list field in user_dir structure
343    xptr_t entry_xp = XPTR( local_cxy , &dir->list );
344
345    // get lock protecting open directories list
346    remote_queuelock_acquire( lock_xp );
347
348    // register user_dir_t in reference process 
349    xlist_add_first( root_xp , entry_xp );
350
351    // release lock protecting  open directorie list
352    remote_queuelock_release( lock_xp );
353
354#if DEBUG_USER_DIR
355cycle = (uint32_t)hal_get_cycles();
356if( cycle > DEBUG_USER_DIR )
357printk("\n[%s] thread[%x,%x] created user_dir (%x,%x) / %d entries / cycle %d\n",
[614]358__FUNCTION__, this->process->pid, this->trdid, local_cxy, dir, total_dirents, cycle );
[613]359#endif
360
361    return dir;
362
363user_dir_create_failure:
364
365    // release local user_dir_t structure
366    req.type = KMEM_DIR;
367    req.ptr  = dir;
368    kmem_free( &req );
369
370    // release local physical pages
371    while( list_is_empty( &root ) == false )
372    {
373        page = LIST_FIRST( &root , page_t , list );
374        req.type  = KMEM_PAGE;
375        req.ptr   = page;
376        kmem_free( &req );
377    }
378
379    return NULL;
380
381}  // end user_dir_create()
382
[614]383////////////////////////////////////////
384void user_dir_destroy( user_dir_t * dir,
385                       xptr_t       ref_xp )
[613]386{
[614]387    thread_t     * this;       // local pointer on calling thread
388    process_t    * process;    // local pointer on calling process
[613]389    cluster_t    * cluster;    // local pointer on local cluster
390    intptr_t       ident;      // user pointer on dirent array
[614]391    xptr_t         ref_pid;    // reference process PID
[613]392    cxy_t          ref_cxy;    // reference process cluster identifier
393    process_t    * ref_ptr;    // local pointer on reference process
394    xptr_t         root_xp;    // root of xlist
395    xptr_t         lock_xp;    // extended pointer on lock protecting xlist
396    xptr_t         iter_xp;    // iteratot in xlist
397    reg_t          save_sr;    // for critical section
398    cxy_t          owner_cxy;  // owner process cluster
399    lpid_t         lpid;       // process local index
400    rpc_desc_t     rpc;        // rpc descriptor
[619]401    uint32_t       responses;  // response counter
[613]402     
[614]403    // get pointers on calling process & thread
[613]404    this    = CURRENT_THREAD;
405    process = this->process;
406    cluster = LOCAL_CLUSTER;
407
[614]408    // get cluster, local pointer, and PID of reference user process
409    ref_cxy = GET_CXY( ref_xp );
410    ref_ptr = GET_PTR( ref_xp );
411    ref_pid = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->pid ) );
412
[613]413#if DEBUG_USER_DIR
414uint32_t cycle = (uint32_t)hal_get_cycles();
415if( cycle > DEBUG_USER_DIR )
[614]416printk("\n[%s] thread[%x,%x] enter for user_dir (%x,%x) and process %x / cycle %d\n",
417__FUNCTION__, process->pid, this->trdid, local_cxy, dir, ref_pid, cycle );
[613]418#endif
419
420    // get user pointer on dirent array
421    ident = dir->ident;
422
423    // build extended pointer on lock protecting open directories list
424    lock_xp = XPTR( ref_cxy , &ref_ptr->dir_lock );
425
426    // get lock protecting open directories list
427    remote_queuelock_acquire( lock_xp );
428
429    // remove dir from reference process xlist
430    xlist_unlink( XPTR( local_cxy , &dir->list ) );
431
432    // release lock protecting open directories list
433    remote_queuelock_release( lock_xp );
434
435    // To delete all copies of the vseg containing the dirent array, the client thread
436    // send parallel RPCs to all clusters containing a client process copy (including
437    // the local cluster). It blocks and deschedules when all RPCs have been sent,
438    // to wait all RPC responses, and will be unblocked by the last RPC server thread.
439    // It allocates a - shared - RPC descriptor in the stack,  because all parallel
440    // server threads use the same input arguments, and the same response field.
441
442    // get owner cluster identifier and process lpid
[614]443    owner_cxy = CXY_FROM_PID( ref_pid );
444    lpid      = LPID_FROM_PID( ref_pid );
[613]445
446    // get root of list of copies and lock from owner cluster
447    root_xp   = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] );
448    lock_xp   = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] );
449
450    // mask IRQs
451    hal_disable_irq( &save_sr);
452
453    // client thread blocks itself
454    thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_RPC );
455
[619]456    // initialize responses counter
457    responses = 0;
458
459    // initialize a shared RPC descriptor
460    // can be shared, because no out arguments
461    rpc.rsp       = &responses;
[613]462    rpc.blocking  = false;
463    rpc.index     = RPC_VMM_DELETE_VSEG;
464    rpc.thread    = this;
465    rpc.lid       = this->core->lid;
[614]466    rpc.args[0]   = ref_pid;
[613]467    rpc.args[1]   = ident;
468
469    // take the lock protecting process copies
470    remote_queuelock_acquire( lock_xp );
471
472    // scan list of process copies
473    XLIST_FOREACH( root_xp , iter_xp )
474    {
475        // get extended pointer and cluster of process
476        xptr_t      process_xp  = XLIST_ELEMENT( iter_xp , process_t , copies_list );
477        cxy_t       process_cxy = GET_CXY( process_xp );
478
479        // atomically increment responses counter
[619]480        hal_atomic_add( &responses , 1 );
[613]481
[619]482        // send RPC to target cluster 
483        rpc_send( process_cxy , &rpc );
484    }
[613]485
486    // release the lock protecting process copies
487    remote_queuelock_release( lock_xp );
488
489    // client thread deschedule
[619]490    sched_yield("blocked on rpc_vmm_delete_vseg");
[613]491 
492    // restore IRQs
493    hal_restore_irq( save_sr);
494
495    // release local user_dir_t structure
496    kmem_req_t  req;
497    req.type = KMEM_DIR;
498    req.ptr  = dir;
499    kmem_free( &req );
500
501#if DEBUG_USER_DIR
502cycle = (uint32_t)hal_get_cycles();
503if( cycle > DEBUG_USER_DIR )
504printk("\n[%s] thread[%x,%x] deleted user_dir (%x,%x) / cycle %d\n",
505__FUNCTION__, process->pid, this->trdid, local_cxy, dir, cycle );
506#endif
507
508}  // end user_dir_destroy()
Note: See TracBrowser for help on using the repository browser.