From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ua1-f44.google.com (mail-ua1-f44.google.com [209.85.222.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D560A2135C8 for ; Fri, 10 Jan 2025 17:14:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736529254; cv=none; b=d75xlC/B+kDy2rPCsFMm7bcSrm94ybfe/zlI96G3+Fcz1+te2Qo5+kdBvgSFKvF9Y8/N/qDdvjd2k0BmVFPg3b9WOlvnauAo62Zx0G17ipTZVuieu8Gogp1/6MUEsLcxA09lyH9pawrlPeXnWCfLVfrytliYYih8OvQ8Yi0yITY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736529254; c=relaxed/simple; bh=hYawAvQK4z4/EM9x/h419pqOYf9u2FKNL/KJIuICxCI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=JE63QQNw3j4oGYWcwlALqIze6rABowvJhGn21U1WleQshTP+uiQ8Ba6tYQ/DOPGLs/YKixDncWVuWVrrAwvKeUMBNqiML5teCQ3S542GKZ3upY3nuGlViSzTKmgt4ryzouvnfRUSxCmmoG0FqLLfWV3cEA/2HVhPp2oBqfwdqSk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=iNnLqTq6; arc=none smtp.client-ip=209.85.222.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="iNnLqTq6" Received: by mail-ua1-f44.google.com with SMTP id a1e0cc1a2514c-85c48f5e2c1so499950241.3 for ; Fri, 10 Jan 2025 09:14:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1736529252; x=1737134052; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:organization:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=tSkRy37H6rM6iPg8bNIcKLYDz6luWb9X0txbqDa0QZ0=; b=iNnLqTq6HY60rMQRxxP3kjGb6rC2qAiIKXIe9UNpb0LgtNSN6j0n+T63Lieq7a0/9Y uP36qUslCbaq/CJHO62jVQ+tiTHPHRfBoX7TlRAw4WCXLkSG3AzCnNsc6eHs0kLdW9qJ z4tSQILJago9wKxNmxr/zsTekREr+v5tZ21ygAdBa3w9JR+8EaYtkugi4IBD4U0DlNMU NdTV1NOhMyPegswxjYubV/AYMM2pMc7j1xRFTqm3lyZ6DBLxhz/nahWbi87xb1+SFgGG T+L9NS1F9rkAHzPPEf9ojUk7v/x9+tBpYdZ5421QRi9mb0ZPIFeQ93PWNIOP6N6o0EAk P80A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736529252; x=1737134052; h=content-transfer-encoding:in-reply-to:organization:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=tSkRy37H6rM6iPg8bNIcKLYDz6luWb9X0txbqDa0QZ0=; b=B99SUU/aXzDMzqV2e900Fsxyy60ibh4C2DdVxfsqaiViYV2g3TjnUFGHqmFbeoJEDu dPPU2r0maWCqU6OpIFpPaDiJy/UVRuCec3Up0/LtDlBWsyZf2sI52gnCwyLn3FV+SD7Y IJurtNSN/96lZxqvDBInNaiN3LP8R36hnP/owmKkBY3oyvnQC7r2y7vQ/9JqXEXdSAwv 6EEPC5nGh7Ik7OHU4ksPpCueJxHomTDkLlsVBYcceEOxEs27xQ6kRlZwzpZydM8Tsl0X XOi8VhMvCBgYXgONLIg/jTICGyk+nv1T6039HvDgZwt9YM/tiKL5HbqWNd2ylB/PkX7Y r/vA== X-Forwarded-Encrypted: i=1; AJvYcCWZVf7KzMUT0CclUm7YIBVSOw6opmMC6zvN0vLfe45hHX6z7JEF+Wq4YNFlVN/v0GDWaqTWD9WNlEpT6Sg=@vger.kernel.org X-Gm-Message-State: AOJu0YxZ8uJBbuuLMvUsufO6LiCOzc4mB+6PD5NEZkH1Dach7m2x4HF9 UJVJ/XTmOlN70cYItDZa8wFw1VX8+sUhmCgmHa9mQrw4FS0z+SbeN66doL4bSD4= X-Gm-Gg: ASbGncv5fXnccBjl+cCk1BEV8XMtYY4pWpcoSOzfq4IGH+2sXYv9xY9oI4sVe7hCc+F x5/r5/fIqIpog1ToghjpLKx1vIXzVXuEux/3vIeQLjk/Qms1U/jWDqoeClw8VPYOjxb3CSSQMjR hBg5qD/RmLxlcuAFrOXuK7LUK22ic/YiIsfxXiP9Z2iHeRbET2yjAiAjSrdZ0SfaBdHUYrLbwLU RzvGyW2Sx/uIzmiu49p0/ruovSmKCcbveWz6YmY6/AhUYmvvtSFA3cSb7PxSRedh51LSGItCLRP YSG9y/obZNSIYl0sCx93dfkpS1NviPAaHjEMAWjDXix/L6PYuLumu9lYlww= X-Google-Smtp-Source: AGHT+IE7dAuMZG7+lel5O9awhp1mA2IO9K4rb5X82dzI85h1Km81baJsCljju4F08rNe258gD0bWAw== X-Received: by 2002:a05:6122:3718:b0:51b:8949:c9ab with SMTP id 71dfb90a1353d-51c6c510233mr11450720e0c.9.1736529251699; Fri, 10 Jan 2025 09:14:11 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c0:f41c:e104:7f6c:f2c3:2134? ([2804:1b3:a7c0:f41c:e104:7f6c:f2c3:2134]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-51c7fd2dcf5sm2396162e0c.44.2025.01.10.09.14.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 Jan 2025 09:14:11 -0800 (PST) Message-ID: <5de4157b-33e0-440c-8a23-0f1a30253b5b@linaro.org> Date: Fri, 10 Jan 2025 14:14:07 -0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Prevent inconsistent CPU state after sequence of dlclose/dlopen To: Florian Weimer , Mathieu Desnoyers Cc: Peter Zijlstra , "libc-alpha@sourceware.org" , "carlos@redhat.com" , Mark Rutland , linux-kernel , x86@kernel.org, paulmck , Michael Jeanson References: <20250110165412.GC4213@noisy.programming.kicks-ass.net> <8c1ad304-61bb-4bdf-aa75-8633f3d0196c@efficios.com> <87ldvitx0t.fsf@oldenburg.str.redhat.com> Content-Language: en-US From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <87ldvitx0t.fsf@oldenburg.str.redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 10/01/25 14:10, Florian Weimer wrote: > * Mathieu Desnoyers: > >> On 2025-01-10 11:54, Peter Zijlstra wrote: >>> On Fri, Jan 10, 2025 at 10:55:36AM -0500, Mathieu Desnoyers wrote: >>>> Hi, >>>> >>>> I was discussing with Mark Rutland recently, and he pointed out that a >>>> sequence of dlclose/dlopen mapping new code at the same addresses in >>>> multithreaded environments is an issue on ARM, and possibly on Intel/AMD >>>> with the newer TLB broadcast maintenance. >>> What is the exact race? Should not munmap() invalidate the TLBs >>> before >>> it allows overlapping mmap() to complete? >> >> The race Mark mentioned (on ARM) is AFAIU the following scenario: >> >> CPU 0 CPU 1 >> >> - dlopen() >> - mmap PROT_EXEC @addr >> - fetch insn @addr, CPU state expects unchanged insn. >> - execute unrelated code >> - dlclose(addr) >> - munmap @addr >> - dlopen() >> - mmap PROT_EXEC @addr >> - fetch new insn @addr. Incoherent CPU state. > > Unmapping an object while code is executing in it is undefined. > > We have a problem with things like pthread_atfork handlers. We can't > use locking there because fork handlers are expected to perform ample > locking themselves, and an extra lock around them would run into lock > ordering issues. (We tried for unrelated reasons and saw deadlocks in > applications.) > > What we can do is bump a reference counter while we run a pthread_atfork > callback (we already associate them with DSOs) and skip the munmap part > in dlclose if the counter is not zero. We can complete the unmapping > after the fork handler returns (maybe in the parent only). We can also make dlclose a no-op (like some runtimes do), although this has other implications. > > There might be other callbacks besides fork handlers that have this > problem. A similar treatment is possible for some of them, hopefully > all of them in glibc. We cannot cover things like std::shared_ptr > destructor calls, though. But adding more barriers won't fix those, > either. > > Thanks, > Florian >