From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x234.google.com (mail-oi1-x234.google.com [IPv6:2607:f8b0:4864:20::234]) by sourceware.org (Postfix) with ESMTPS id BD7873858D37 for ; Thu, 3 Nov 2022 21:22:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BD7873858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oi1-x234.google.com with SMTP id r76so3336284oie.13 for ; Thu, 03 Nov 2022 14:22:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:in-reply-to:organization:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=OtCmqX0uosPfY32EhMZ/uHKKLy5uFo3oEBGInalHQkE=; b=KTwlSf/Nu0al6VP4n6nlZECWD+eAJzZ1N82VlMg3fBxLYkV7C11uCXA+zdT8ebA3ik mDamfjObipmTF4G1cKugika6lw81/e2nBl6uqtZWbfo/PtfPLs5uUjRoF0UuNi3RUcKa cK8MpMWfCTR7JOpjMMTI9OVgGrGt6MDGg6PWcDD/tiuDBIskloawVC/dO/r56ykfLi7B jhIYk9bLpY5ew4C3CthcLX4I7BiXrMU9GFm49cUkbQP7JLAJxhlyuPfsTkwxXPPPX0D1 u/RsCJRcBh9Us0WVh81NrLQm1oowsg+xFnbEzU8TCHlnktkvqMASyS7gd5itXI9A5QWg EvXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OtCmqX0uosPfY32EhMZ/uHKKLy5uFo3oEBGInalHQkE=; b=UNHUFee4mw2CILDU7+Z/hcmeIXs2H5yWjKOZ6aQmNRUAr2PScwn48wnaTJzPVpDPwF hTiQmKy4X61Unti0M5ILex6ZFHH1vURZo0dqkyg8RblpdSeOcZ4g98d2SgJVQ3awyG67 U5rwLsIr1oCtufd/y4hmqDlRW9uYVblFjAHlxJnetTXEKXQUUkrYpLD9wAAlCpREjwtX vIRcvHT1RBsSr/TvrLWb9sF+h5aIDYFRthRuY5+embh6xxGKrLjTDEhLSdcWUxK5mTZK v0SsbfzN61j2b3cSzXQkI/vIS0wVsV57AMPd/dFQb5NX4t7/WXb52oT57E5tepiT1Zs+ rODQ== X-Gm-Message-State: ACrzQf3bA06lz7FGQS7smvQ0ZNWYnJCx6yLVWCOJ8UrPdDuOO2YVHKvS pWg1BuZFAXqU394GIxuQAHFHRd/qRwleQIJd X-Google-Smtp-Source: AMsMyM7BYMirxkmhhWYdm+h9k33uhDFzuFl5QS8Ba7e0XLl724guSbBnpmg+MDekpDOOVLLvpSp72w== X-Received: by 2002:a54:4702:0:b0:35a:288d:9d01 with SMTP id k2-20020a544702000000b0035a288d9d01mr9121495oik.260.1667510540908; Thu, 03 Nov 2022 14:22:20 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c0:a9f4:4405:8c19:a65e:e640? ([2804:1b3:a7c0:a9f4:4405:8c19:a65e:e640]) by smtp.gmail.com with ESMTPSA id y81-20020a4a4554000000b00480dac71228sm611009ooa.24.2022.11.03.14.22.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Nov 2022 14:22:19 -0700 (PDT) Message-ID: <1a5cc9ec-de78-cb4d-3bd3-7f37dc666f73@linaro.org> Date: Thu, 3 Nov 2022 18:22:16 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [PATCH v2 4/9] aarch64: Add the clone3 wrapper Content-Language: en-US From: Adhemerval Zanella Netto To: Szabolcs Nagy Cc: libc-alpha@sourceware.org, Christian Brauner , "H.J. Lu" References: <20220930192613.3491147-1-adhemerval.zanella@linaro.org> <20220930192613.3491147-5-adhemerval.zanella@linaro.org> <1d4ce210-2b28-b061-9780-f643eaa80a27@linaro.org> <8a3dc5d9-b731-4c45-7252-8157ba0be6c2@linaro.org> Organization: Linaro In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 03/11/22 13:55, Adhemerval Zanella Netto wrote: > > > On 03/11/22 13:52, Szabolcs Nagy wrote: >> The 11/03/2022 13:39, Adhemerval Zanella Netto wrote: >>> >>> >>> On 03/11/22 13:31, Szabolcs Nagy wrote: >>>> The 11/03/2022 13:22, Adhemerval Zanella Netto wrote: >>>>> >>>>> >>>>> On 03/11/22 11:01, Szabolcs Nagy wrote: >>>>>> The 11/03/2022 10:15, Adhemerval Zanella Netto wrote: >>>>>>> On 02/11/22 09:12, Szabolcs Nagy wrote: >>>>>>>> The 09/30/2022 16:26, Adhemerval Zanella via Libc-alpha wrote: >>>>>>>>> It follow the internal signature: >>>>>>>>> >>>>>>>>> extern int clone3 (struct clone_args *__cl_args, size_t __size, >>>>>>>>> int (*__func) (void *__arg), void *__arg); >>>>>>>>> >>>>>>>>> And x86_64 semantics to return EINVAL if either cl_args or func >>>>>>>>> is NULL. The stack is 16-byte aligned prior executing func. >>>>>>>> >>>>>>>> "x86_64 semantics" sounds wrong: maybe this should be documented? >>>>>>>> i'd expect 0 cl_args/func to be UB like in most posix apis. >>>>>>> >>>>>>> Right, I think it is worth to document the function semantic >>>>>>> properly at least on its internal header (include/clone_internal.h). >>>>>>> H.J also added a new clone3.h headers, which is not currently installed >>>>>>> that I am inclined to just remove it from now. We might reinstate >>>>>>> if/when we decide to provide the clone3 as an ABI. >>>>>>> >>>>>>> And returning EINVAL for 0 cl_args/func aligns with our exported clone >>>>>>> interface, where EINVAL is also returned for 0 function argument. >>>>>> >>>>>> ok. >>>>>> >>>>>>>> >>>>>>>> and aligning sp in the child fails if signals are allowed there >>>>>>>> (pthreads does not allow signals now, direct callers might). >>>>>>>> i dont know if that's a concert (or if unaligned stack is >>>>>>>> something we should fix up in clone3). >>>>>>> >>>>>>> It was overlooked on initial x86_64 clone3 implementation as well. I >>>>>>> think it better to just return EINVAL for unaligned stacks and avoid >>>>>>> to change the stack pointer in the created thread. >>>>>> >>>>>> long time ago linux did that on aarch64, but it was removed: >>>>>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e6d9a52543338603e25e71e0e4942f05dae0dd8a >>>>>> >>>>>> i think in clone3 the kernel should have aligned (it knows >>>>>> the bounds now), doing it in the userspace wrapper is weird >>>>>> (should we adjust the stack size?). and not doing it at all >>>>>> makes clone3 hard to use portably (user has to know target >>>>>> specific pcs requirements). >>>>>> >>>>>> not sure what's the best way forward. >>>>> >>>>> I think the stack size won't matter much here, at least not from >>>>> kernel point of view (the resulting stack size will most likely >>>>> be page aligned anyway). But I think this kernel commit makes a good >>>>> point that silently adjusting the stack in userland is not the >>>>> correct approach, I think H.J has done to make it consistent with >>>>> glibc clone implementation which does it. >>>>> >>>>> IMHO the best approach would to just remove the stack alignment, >>>>> since it incurs the signal handling issue. >>>> >>>> current generic clone callers dont align the stack and >>>> e.g. unaligned pthread custom stack should work. >>>> >>>> so we have to do arch specific stack alignment somewhere, >>>> maybe in pthread_create? >>> >>> I am thinking on __clone_internal, where if an unaligned stack is >>> used it creates a new clone_args struct with adjust arguments. It >>> can adjust the struct in place (not sure which is better). >> >> if the api is not exposed, then i think the arg can be modified >> in place. (if clone3 api is exposed to users then we should not >> modify user structs unless the clone3 api contract explicitly >> allows this.) >> >> either aligning in pthread_create or __clone_internal works for me, >> the target specific clone3 syscall should not in case that gets >> exposed to users. >> > > The arg modification would be done only internally by __clone_internal, > if we ever export __clone3 it will not mess with stack alignment (my > idea is to remove it from x86_64 as well). All the internal usage of __clone_internal are done with all signal masked, so aligning the stack is currently safe. However, I still think moving out the stack alignment of __clone3 is still a net gain: it remove an implementation detail (block/unblock signals) and simplifies the arch-specific code. However it makes a possible libc wrap clunky, the caller will need to know the ABI stack alignment prior to the call since kernel does not automatically align the stack.