<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Aug 24, 2020 at 9:34 PM Gedare Bloom <<a href="mailto:gedare@rtems.org">gedare@rtems.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sat, Aug 22, 2020 at 11:27 PM Utkarsh Rai <<a href="mailto:utkarsh.rai60@gmail.com" target="_blank">utkarsh.rai60@gmail.com</a>> wrote:<br>
><br>
><br>
><br>
> On Sat, Aug 22, 2020 at 11:32 PM Gedare Bloom <<a href="mailto:gedare@rtems.org" target="_blank">gedare@rtems.org</a>> wrote:<br>
>><br>
>> I have some comments below. I'm not that happy with the lack of design<br>
>> discussion during the iteration of this code. While it is a little<br>
>> easier to critique the code, it is also a bit wasteful because I have<br>
>> to also comment on stylistic problems, and when some decisions you<br>
>> make while coding end up not being correct or aren't easily understood<br>
>> during code review then you might have to spend more time recoding. It<br>
>> can be hard to find the balance between design and code reviews,<br>
>><br>
>> On Sat, Aug 22, 2020 at 9:19 AM Utkarsh Rai <<a href="mailto:utkarsh.rai60@gmail.com" target="_blank">utkarsh.rai60@gmail.com</a>> wrote:<br>
>> > +<br>
>> > +#if defined ( RTEMS_THREAD_STACK_PROTECTION )<br>
>> > +<br>
>> > +/**<br>
>> > + * The following defines the control block of a shared stack. Each stack can have<br>
>> > + * different sharing attributes.<br>
>> > + */<br>
>> > +typedef struct<br>
>> > +{<br>
>> > + /** Access flags of the shared stack */<br>
>> > + uint32_t access_flags;<br>
>> > + /** This is the stack address of the sharing thread*/<br>
>> > + void* shared_stack_area;<br>
>> > + /** Stack size of the sharing thread*/<br>
>> > + size_t shared_stack_size;<br>
>> > + /** This is the stack address of the target stack. Maybe this area is not<br>
>> > + * needed but this helps in selecting the target thread during stack sharing.<br>
>> > + */<br>
>> > + void* target_stack_area;<br>
>> I'm confused about the difference between shared_stack_area and<br>
>> target_stack_area<br>
>><br>
>> > + /** Error checking for valid target stack address. This is also used to<br>
>> > + * distinguish between a normal mmap operation and a stack sharing operation.<br>
>> > + */<br>
>> I don't really know what this means.<br>
><br>
><br>
> This is related to the mmap() call. When we share stack using mmap, two of the important parameters that reference to the target thread-stack<br>
> and sharing thread-stack are -'void* addr' which is the address of the target thread stack and 'int fd' that is the file descriptor to the shared memory<br>
> object corresponding to the thread stack being shared. Now, stack sharing is different from a normal mmap operation in the sense that we need to add<br>
> the sharing stack attributes to the stack control structure of the thread that is specified by 'addr' as compared to adding it to the chain of mappings. To select the<br>
> target thread we need to iterate over all the threads, in the case we do not find any thread, we can proceed with our normal mmap operation otherwise we add the<br>
> sharing thread stack attribute to the stack control structure of the target thread. We have the _Thread_Iterate() function but it only has two parameters the pointer to the<br>
> function that performs operation during each iteration and a parameter related to this operation. My decision to specify the 'target_stack_area', the 'shared_stack_area' and 'stack_shared' in the structure<br>
> was so that I could squeeze in as much information to select the thread, set the sharing stack parameters in the thread, and validate(through stack_shared attribute) in the mmap() call that this is<br>
> in fact a stack-sharing operation, in a single iteration.<br>
><br>
<br>
Maybe I should ask this on the mmap patch itself, but I'm more<br>
interested in the design than the code right now. I think I'll just<br>
ask a few questions:<br>
<br>
The 'sharing' stack is the stack that is getting r/w permission added<br>
to another thread, and the 'target' thread is the thread that is<br>
getting the r/w permission to access the shared stack. We should not<br>
really care about the 'sharing thread' or the 'target stack'?<br></blockquote><div><br></div><div>If the mmap() call for sharing thread stack assumes that it is being called from the context of the 'target thread', then no, we should not. </div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
If mmap() assumes the calling context then the executing thread is the<br>
"target thread" and you shouldn't have to search for it? In typical<br>
POSIX implementations, mmap() assumes the executing process context<br>
called it, but can we make the same assumption?<br>
<br>
For mmap() the 'addr' should be the 'sharing' stack's (base) address<br>
not the 'target' stack, because you want to be accessing the 'sharing'<br>
stack memory? We don't have any virtual memory mapping, so the address<br>
of the sharing stack needs to be used. Or maybe it can be NULL, or<br>
even ignored, since the mmap() call should return the base address of<br>
the 'sharing' stack? The fd should be able to find all the information<br>
needed about the 'sharing' stack.</blockquote><div><br></div><div>My confusion was, as you asked above, whether we can assume that the mmap() was called by the executing process</div><div>context. I was making the mmap() call from the 'POSIX_Init' thread and passing the address of the target thread and then </div><div>selecting the thread inside mmap(). Can we do something like <a href="https://gist.github.com/ur10/a1829b7b8648521d90703be014e3feef">this</a> in the application to ensure that the mmap() is called </div><div>from the desired thread?</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
-Gedare<br>
</blockquote></div></div>