Thursday 21 April 2016

Ensure your stack is aligned correctly on aarch64 clone()

I got bitten this week with the clone() system call returning -EINVAL on aarch64 on code that worked fine on x86.  After re-reading the manual several times and looking at my code, I resorted to shoving in debug into the kernel to track down where the -EINVAL was occurring.

The answer to my issue is in arch/arm64/kernel/process.c, copy_thread():

         if (stack_start) {  
             if (is_compat_thread(task_thread_info(p)))  
                 childregs->compat_sp = stack_start;  
             /* 16-byte aligned stack mandatory on AArch64 */  
             else if (stack_start & 15)  
                 return -EINVAL;  
             else  
                 childregs->sp = stack_start;  
         }  

Ahah! The stack being passed into clone() has to be 16 byte aligned.  With this simple fix to my code, clone() worked.   Pity this was not in the documentation.

6 comments:

  1. Where are the system call man pages maintained? Is it very difficult to create a pull request?

    ReplyDelete
  2. "Pity this was not in the documentation."

    And a pity that the person who learned the information has not sent a patch or bug report to me :-). See https://www.kernel.org/doc/man-pages/reporting_bugs.html

    ReplyDelete
    Replies
    1. My apologies, I will be a better citizen next time. :-(

      Delete
    2. Oh -- you can be better this time :-). See https://www.kernel.org/doc/man-pages/reporting_bugs.html

      Delete
    3. Done. Sorry to take so long.

      Delete
  3. Fix landed in linux-next: https://git.kernel.org/cgit/linux/kernel/git/next/linux-next.git/commit/?id=e6d9a52543338603e25e71e0e4942f05dae0dd8a

    ReplyDelete