From nobody Wed Jul 24 10:34:57 2024 X-Original-To: freebsd-arm@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WTVl82smhz5Rj17; Wed, 24 Jul 2024 10:35:00 +0000 (UTC) (envelope-from melounmichal@gmail.com) Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "WR4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WTVl80kQsz4RyW; Wed, 24 Jul 2024 10:35:00 +0000 (UTC) (envelope-from melounmichal@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-3686b554cfcso3254662f8f.1; Wed, 24 Jul 2024 03:35:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721817298; x=1722422098; darn=freebsd.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:reply-to:user-agent:mime-version:date:message-id:from :sender:from:to:cc:subject:date:message-id:reply-to; bh=mXWNOFIbZTQB0C+Lci5G/wZK34fXBGR60XXmvO6lmU0=; b=Nm68IkH2VJjVz4EEY4rv8y6u7jEKf0Vv6k4PbbBBpFh7o8l7g0CVwQCK3Xsr7LmeV5 piMC6WGIuDKprRmNeFCH7R5BXVVF/DBLFoufbmjOck4yLj28tGkNVvJSZZBD0+vPurvV lrI23g+KS/36aFe5YSsOg/N4hDV0Jw/HWCfjpWnR4A56a72Ij3t3UF9eFIGO9/tmy082 Q9M33x4Tq5s9tLQLboy2QTiXk8dmgB6fxb27AZdRgTteXzZWrtqlcm3F7GqlfvbXFGFk rpGoRmEC4DPI2gKhm2wpeMVCnyzZCefDH05qNr9AI1FjUTRr3GAlAZ88cfb+QW2r819B Cy5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721817298; x=1722422098; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:reply-to:user-agent:mime-version:date:message-id:from :sender:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=mXWNOFIbZTQB0C+Lci5G/wZK34fXBGR60XXmvO6lmU0=; b=e8maBPhQym6N39eYFfE10Zy0uPaZNxRgeTvBN7W7mGZk2noqAFBFaQG8dgSIjINFjL uKtdBFq8wWaFDt4Q9gWCFCFYN+bXk1XFdNhcYPsGA14e/PWQsXGakpnv++23qozpmLCK vxjuvu/GCfmho716IvFnAMCP8Vb3QUctO5Ll1QUlIU+q9JT7q8wHSOWNVodu8yRPbBCz NGZ0uIeb/QwvjeXwLM27APa21h0oUuTSkI5FM3FyNZWrowdQStjom1wRyeHyb97SSTjP FdOWlKZeq2Yrwfl3CqiPekkezVEjJVnqxF1rDwivzEB7+PqQv4yNA2e+OzgncQc5KWGa 66CQ== X-Forwarded-Encrypted: i=1; AJvYcCVUW4domGbVDd1jx+U417dq8u25dXdwx7qmxmaC6a+xbMsOa/Z/zSCU9K4w0d10zx4mMW24hsWPDfpAd4hpAp84BScp0qEcvUYy79yCELIGBQ3j1N4x+hy6p/yHtLSPp0wg0tLw X-Gm-Message-State: AOJu0YzQGFaga2gstMGgIow2KzvGZrf7FGx4TZ4K+R0h4WwfRKMXOGHT 7pVZmaatEuJ3dM+yxuwCLSz8cRhPFQEHO2IOz6pSA/4bYr4lC7KkMKh4ZlJS X-Google-Smtp-Source: AGHT+IGUq/JDzTQGjFJqhycW0Pc+FSq8+X2aRe+nVEfFl7ap097Fb3MATZkSSJ6/q5sgextx5l+3vQ== X-Received: by 2002:adf:f9cb:0:b0:368:68d3:32b3 with SMTP id ffacd0b85a97d-369f5a8f8c1mr1148706f8f.26.1721817298118; Wed, 24 Jul 2024 03:34:58 -0700 (PDT) Received: from ?IPV6:2001:67c:14a0:5fe0:841e:45d2:e338:10c2? ([2001:67c:14a0:5fe0:841e:45d2:e338:10c2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-368787ed580sm13824230f8f.112.2024.07.24.03.34.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 Jul 2024 03:34:57 -0700 (PDT) From: "mmel@freebsd.org" X-Google-Original-From: "mmel@freebsd.org" Message-ID: Date: Wed, 24 Jul 2024 12:34:57 +0200 List-Id: Porting FreeBSD to ARM processors List-Archive: https://lists.freebsd.org/archives/freebsd-arm List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-arm@FreeBSD.org MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Reply-To: mmel@freebsd.org Subject: Re: armv7-on-aarch64 stuck at urdlck To: Konstantin Belousov , John F Carr Cc: Mark Millard , FreeBSD Current , "freebsd-arm@freebsd.org" References: <86185657-e521-466b-89e2-f291aaac10a6@freebsd.org> <0EF18174-8735-46A4-BD71-FFA3472B319F@yahoo.com> <33251aa3-681f-4d17-afe9-953490afeaf0@gmail.com> <0DD19771-3AAB-469E-981B-1203F1C28233@yahoo.com> <6a969609-fa0e-419d-83d5-e4fcf0f6ec35@freebsd.org> Content-Language: cs, en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spamd-Bar: ---- X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; TAGGED_FROM(0.00)[]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US] X-Rspamd-Queue-Id: 4WTVl80kQsz4RyW On 24.07.2024 12:24, Konstantin Belousov wrote: > On Tue, Jul 23, 2024 at 08:11:13PM +0000, John F Carr wrote: >> On Jul 23, 2024, at 13:46, Michal Meloun wrote: >>> >>> On 23.07.2024 11:36, Konstantin Belousov wrote: >>>> On Tue, Jul 23, 2024 at 09:53:41AM +0200, Michal Meloun wrote: >>>>> The good news is that I'm finally able to generate a working/locking >>>>> test case. The culprit (at least for me) is if "-mcpu" is used when >>>>> compiling libthr (e.g. indirectly injected via CPUTYPE in /etc/make.conf). >>>>> If it is not used, libthr is broken (regardless of -O level or debug/normal >>>>> build), but -mcpu=cortex-a15 will always produce a working libthr. >>>> I think this is very significant progress. >>>> Do you plan to drill down more to see what is going on? >>> >>> So the problem is now clear, and I fear it may apply to other architectures as well. >>> dlopen_object() (from rtld_elf), >>> https://cgit.freebsd.org/src/tree/libexec/rtld-elf/rtld.c#n3766, >>> holds the rtld_bind_lock write lock for almost the entire time a new library is loaded. >>> If the code uses a yet unresolved symbol to load the library, the rtl_bind() function attempts to get read lock of rtld_bind_lock and a deadlock occurs. >>> >>> In this case, it round_up() in _thr_stack_fix_protection, >>> https://cgit.freebsd.org/src/tree/lib/libthr/thread/thr_stack.c#n136. >>> Issued by __aeabi_uidiv (since not all armv7 processors support HW divide). >>> >>> Unfortunately, I'm not sure how to fix it. The compiler can emit __aeabi_<> in any place, and I'm not sure if it can resolve all the symbols used by rtld_eld and libthr beforehand. >>> >>> >>> Michal >>> >> >> In this case (but not for all _aeabi_ functions) we can avoid division >> as long as page size is a power of 2. >> >> The function is >> >> static inline size_t >> round_up(size_t size) >> { >> if (size % _thr_page_size != 0) >> size = ((size / _thr_page_size) + 1) * >> _thr_page_size; >> return size; >> } >> >> The body can be condensed to >> >> return (size + _thr_page_size - 1) & ~(_thr_page_size - 1); >> >> This is shorter in both lines of code and instruction bytes. > > Lets not allow this to be lost. Could anybody confirm that the patch > below fixes the issue? > > commit d560f4f6690a48476565278fd07ca131bf4eeb3c > Author: Konstantin Belousov > Date: Wed Jul 24 13:17:55 2024 +0300 > > rtld: avoid division in __thr_map_stacks_exec() > > The function is called by rtld with the rtld bind lock write-locked, > when fixing the stack permission during dso load. Not every ARMv7 CPU > supports the div, which causes the recursive entry into rtld to resolve > the __aeabi_uidiv symbol, causing self-lock. > > Workaround the problem by using roundup2() instead of open-coding less > efficient formula. > > Diagnosed by: mmel > Based on submission by: John F Carr > Sponsored by: The FreeBSD Foundation > MFC after: 1 week > For final resolving of deadlocks, after a full day of digging, I'm very much incline of adding -znow to the linker flags for libthr.so (and maybe also for ld-elf.so). The runtime cost of resolving all symbols at startup is very low. Direct pre-solving in _thr_rtld_init() is problematic for the _aeabi_* symbols, since they don't have an official C prototypes, and some are not compatible with C calling conventions. Warner, Konstantin, could you please comment on this? Michal