Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp687096lqd; Wed, 24 Apr 2024 13:52:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWCSobw0c/U7rxRJuk1dbEbkbwjfXPrlgS1IL9Fz05mM4v5r6YPDHH5GZ/+l6ZBOZmgewyKucLM/8v7qGSCouwBMTi5wj7BX+F3uQi2zA== X-Google-Smtp-Source: AGHT+IHVBa/cvVzSAv6hh51VO0nq7nK+pV1VbksspaQ6pB8l93IykVJ2Hw19uhpvK++xITUaem7B X-Received: by 2002:a50:bae3:0:b0:56f:e4a2:1640 with SMTP id x90-20020a50bae3000000b0056fe4a21640mr2234255ede.21.1713991952094; Wed, 24 Apr 2024 13:52:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713991952; cv=pass; d=google.com; s=arc-20160816; b=pRONrpwc2/EgEsHqTW8dzjk16linuPPcluk4vTEbPxzszbkQD6cAXRm1lJtI7OO6cb eSXE/WE/EiKxsADXQrYmhJmgweqf5I8i+UX0XvD7U2AOTfb+pHZTcFQH7jUBzzorTWCb ztCPtUjdwKROOsC4xlo92ibglZ0YYF96F/VHhH3SCWuH3RY8+SMS4cxQF5T/dedykhhs vBCC8mFHx6u9uf8iwS/loosjoitpDn5WQppNvm2qvC46Llv6reNP2GzyDmHvJcLPAgZr VGL+h9q9WUirjiTIp0eVxNMcqRxbUALuPm5OIupv4DAgRwWGZbbpbJ8keexLTobBjY1q HF0w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=loK2NjDgUOj5VY+1VMZ4MvQqP0Y2RVpNZst4dAXdww0=; fh=XlPYGkPf1uCcHvGzsRQ9m8gZAvub/i+RQeY/mg/8vVQ=; b=rl3s+lICkM6IX/cGVyPv8PO5fclFpJS4xiqGpleT/p455ftaU5wm5AI53NcEhcsGz6 74eV4RX6r/sxxBqatlhYhkHeuRgXFnVjtyyvD/rpHoJw4dAyyxWybRSpXfc1p1kJZxHT 8XwANwwAGTSU9kQ8QtsdUso3xD0j8ifOO114KDz2TwEkq4uBi38hCHRqfM+aKPsrky6y ++Gq+blaNCFQpDupAoUizRz6eQA4ZHfk3GVQjhZ32Nw7lrhbVhx7vQEwuBMaHAnm8NLO 16ihz4zSqVJQhhEXY28c867Pw0/n9gY7uBcTewNC13pjv0oiOATlW9sZCn0lawbP+hIM sHeg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bdOX6C31; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-157708-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157708-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id y19-20020a056402359300b0057224409c8asi2395324edc.202.2024.04.24.13.52.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 13:52:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-157708-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bdOX6C31; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-157708-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157708-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A83D41F21438 for ; Wed, 24 Apr 2024 20:52:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 73FAC15696B; Wed, 24 Apr 2024 20:52:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bdOX6C31" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8580A13A401 for ; Wed, 24 Apr 2024 20:52:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713991944; cv=none; b=YBuWYJfFGA8BP+G9WrtdgxnUuKuh7k741KaVQM0BZJ7AsfJAXKG0XU/KJnqfvByLgvnKWo176Qf+K50OsETdwwuUcPjCzyCRIhJq8jUrZWn8W7YEUrH7h5hA6blYhy0XjEqPBzwr49rJaen8VykJEwjI8R37XUycRWjpdM4Chx8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713991944; c=relaxed/simple; bh=U8vN6+xOk4zyZdpad0pX9zdS4fQ6q9OQDwYM3NEU+dI=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=jJu5d+74xVRmoPhw8XxGIuATELlNTzIJO+mPMjUSKGvmYQHOsgJ6N1Irz5/eSrWfImu6h82/KW3iNqKdyPiLw304MKzQ4xMJl8IKwwsGlESRitXMVClYZZT2eRdAccCGu9qCEAiQvRdriXIlDg3M2EtlzUzxKY06ZmZqDCQvHpw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bdOX6C31; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 138B0C2BD10 for ; Wed, 24 Apr 2024 20:52:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713991944; bh=U8vN6+xOk4zyZdpad0pX9zdS4fQ6q9OQDwYM3NEU+dI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=bdOX6C31ndrm0+qVhx83jYqY8oK3lKPL5yadH+mG8ffz2nqDG6FBEFw/dNMlUlOx0 Y76MZ2tSpfI/PlunIZEUjuttngRtLYoor/Jkk+NCqhhAkKqYJILZ36PyEJw66Ercw1 JePD7F0JP9/j1+waJMlAt+TrvZcErX5XwkFgChTclNH7Br6OfeJihaJMFWoi27CcL1 ONV0xL9fSDaQwh+SRK6waU3qMM7deu4llwH/A9Jju4jbWKmnHsdgU90F7J08gZyLTz 9wGBnxHdyobajpPZX6nwdKvnyxiVsCj40JaxbbXrbsO3HyunadrrLejtaVizeDrfeX oeGOOUygraOUg== Received: by mail-lj1-f169.google.com with SMTP id 38308e7fff4ca-2d895e2c6efso3725331fa.0 for ; Wed, 24 Apr 2024 13:52:23 -0700 (PDT) X-Gm-Message-State: AOJu0YzX1Xn8cgrVh2cS6oPTNZi8aXQU7RpDBWdYOOqMIpAW5QAldON+ xs5IYFUgqZpW944LPtgxnuoAv/03SopamIDLKpkwgtOhsT4h6z0vZlJssZTJAIp/isnPdymteSQ nk7T80Rq2glkMHC1XIePlcOwccNw= X-Received: by 2002:a2e:9d06:0:b0:2d8:6b0c:d91c with SMTP id t6-20020a2e9d06000000b002d86b0cd91cmr1948042lji.47.1713991942394; Wed, 24 Apr 2024 13:52:22 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240424155309.1719454-11-ardb+git@google.com> <87bk5yv87h.fsf@email.froward.int.ebiederm.org> In-Reply-To: <87bk5yv87h.fsf@email.froward.int.ebiederm.org> From: Ard Biesheuvel Date: Wed, 24 Apr 2024 22:52:10 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH 0/9] kexec x86 purgatory cleanup To: "Eric W. Biederman" Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Arnd Bergmann , kexec@lists.infradead.org, Nathan Chancellor , Nick Desaulniers , Kees Cook , Bill Wendling , Justin Stitt , Masahiro Yamada Content-Type: text/plain; charset="UTF-8" On Wed, 24 Apr 2024 at 22:04, Eric W. Biederman wrote: > > Ard Biesheuvel writes: > > > From: Ard Biesheuvel > > > > The kexec purgatory is built like a kernel module, i.e., a partially > > linked ELF object where each section is allocated and placed > > individually, and all relocations need to be fixed up, even place > > relative ones. > > > > This makes sense for kernel modules, which share the address space with > > the core kernel, and contain unresolved references that need to be wired > > up to symbols in other modules or the kernel itself. > > > > The purgatory, however, is a fully linked binary without any external > > references, or any overlap with the kernel's virtual address space. So > > it makes much more sense to create a fully linked ELF executable that > > can just be loaded and run anywhere in memory. > > It does have external references that are resolved when it is loaded. > It doesn't today, and it hasn't for a while, at least since commit e4160b2e4b02377c67f8ecd05786811598f39acd x86/purgatory: Fail the build if purgatory.ro has missing symbols which forces a build failure on unresolved external references, by doing a full link of the purgatory. > Further it is at least my impression that non-PIC code is more > efficient. PIC typically requires silly things like Global Offset > Tables that non-PIC code does not. At first glance this looks like a > code passivization. > Given that the 64-bit purgatory can be loaded in memory that is not 32-bit addressable, the PIC code is essentially a given, since the large code model is much worse (it uses 64-bit immediate for all function and variable symbols, and therefore always uses indirect calls) Please refer to https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/?h=x86/build&id=cba786af84a0f9716204e09f518ce3b7ada8555e for more details. (Getting pulled into that discussion is how I ended up looking into the purgatory in more detail) > Now at lot of functionality has been stripped out of purgatory so maybe > in it's stripped down this make sense, but I want to challenge the > notion that this is the obvious thing to do. > The diffstat speaks for itself - on x86, much of the allocation and relocation logic can simply be dropped when building the purgatory in this manner. > > The purgatory build on x86 has already switched over to position > > independent codegen, which only leaves a handful of absolute references, > > which can either be dropped (patch #3) or converted into a RIP-relative > > one (patch #4). That leaves a purgatory executable that can run at any > > offset in memory with applying any relocations whatsoever. > > I missed that conversation. Do you happen to have a pointer? I would > think the 32bit code is where the PIC would be most costly as the 32bit > x86 instruction set predates PIC being a common compilation target. > See link above. Note that this none of this is about 32-bit code - the purgatory as it exists today never drops out of long mode (and no 32-bit version appears to exist) > > Some tweaks are needed to deal with the difference between partially > > (ET_REL) and fully (ET_DYN/ET_EXEC) linked ELF objects, but with those > > in place, a substantial amount of complicated ELF allocation, placement > > and patching/relocation code can simply be dropped. > > Really? As I recall it only needed to handle a single allocation type, > and there were good reasons (at least when I wrote it) to patch symbols. > > Again maybe the fact that people have removed 90% of the functionality > makes this make sense, but that is not obvious at first glance. > Again, the patches and the diffstat speak for themselves - the linker applies all the relocations at build time, and emits all the sections into a single ELF segment that can be copied into memory and executed directly (modulo poking values into the global variables for the sha256 digest and the segment list) The last patch in the series shows which code we could drop from the generic kexec_file_load() implementation once other architectures adopt this scheme.