Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4459260pxv; Tue, 27 Jul 2021 07:54:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQlqYWhY5HO9SC4z9JhjknRUU5g0/djjI94q3H9Oi9moHpgHc9NQUN7eiEXP25mQYM+0cj X-Received: by 2002:a17:906:edd2:: with SMTP id sb18mr22551651ejb.82.1627397682373; Tue, 27 Jul 2021 07:54:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627397682; cv=none; d=google.com; s=arc-20160816; b=lPjnNbSgEPsT0R5FMbKtxpJGXNBkwWA9o5iQ4fjznom4vcrKXQNKvpIukPw+aRdo4i ZIgOSNzrpi9j5EDZzruc9lfwmzsqrZHdn+3gCjJ7zQl+44pLsdbhZcB6foHikFIWC9yT tjcKtrQ+CAsS8zjC2fD14VmE2gzMMeJEgReY0Ou2fNx3z0/SG0wj3QbsirwtTehf53zW hMjYgZy+fSIfYaTEtlBJeh1PBzBmHViQIRCvIMyoK01TZG79KrlfMwJHEVKbKeRgi7/Y 0DzlsfsXkMmkPAywbPgXdDgyNBeWjagF0KYVegIpmmKOlN3B1G9Z8tgKJjuf5FdIiUFo YK2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=u7hQ4J2hpU60/jgo3zeHMdSiWSCNnf23ExTlAmk0I/I=; b=a2Qca+rMamks9XhzbRbjx3SZMmWyvsodYgGR2ax0oCURIRlKwNmHVq9OnC6q407iaK ZcG3EW/2CXMDbCeZuY3H2xEjLvQc4Kj3UCnh1Uae8Kn6gq+sT9CTuuJ2IyLfOYhaO1y+ bJQflcSU0B8MSs+jhpwgfdocgdOIy0I0SAIG8MZVSXWgzVtCZJcj18NG6BSATgOfZNRR W0sAlvIaRItr5SYA6nYr8VjPuiXrYExJEx4r5TIcbxApv0OPH26kAtYDIhvjlmIagaQL /kclmqmnEAAcu68xjKWa/0NDGXn4dQt98i+qcinBUq7FbgAOdkVRzp5cFoKoa1Oz8Zex 0oqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=S5U4Sw6R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k20si3273915ejj.639.2021.07.27.07.54.19; Tue, 27 Jul 2021 07:54:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=S5U4Sw6R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237088AbhG0Ou7 (ORCPT + 99 others); Tue, 27 Jul 2021 10:50:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:36020 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236881AbhG0Ott (ORCPT ); Tue, 27 Jul 2021 10:49:49 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 190B261AFF; Tue, 27 Jul 2021 14:49:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1627397365; bh=czPyPIxu2PTKCugBK9YtQzlToUphKkxEXLs4PNmX7jg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S5U4Sw6RZr+k+Bx+qsSik83rWJ1gJ3J+Xnjocua3sMYzOKHbVYjQfC9+EelM8p+bs 4yYTcFQ7ggj7/9L/nDbgjY7ayt505P/zljlokLVPofqFMQrEvNVfoW9HMuU4TqA0Co TdN+v3mbVV1heJZaRF5cU7QERPA97113HBKPcg2JvNFknquwsyW9tlVacdjnIMqXSX giQafSwRthQmNq0eRh7VJskmn2tjqTQ0Upx5ScJFTxOr6ZLAaYFUTQjrmyn933A70S 74C6f/8vRnm4eN8EXBJeJORsy9hMmPFHVQgiyP9SqQifHV6cf8WoTCKh6RQ+RTwTdh dqfKPOvYSQEWw== From: Arnd Bergmann To: Andrew Morton Cc: Arnd Bergmann , Catalin Marinas , Will Deacon , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , "David S. Miller" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , Al Viro , "Eric W. Biederman" , Christoph Hellwig , Feng Tang , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v5 1/6] kexec: move locking into do_kexec_load Date: Tue, 27 Jul 2021 16:48:54 +0200 Message-Id: <20210727144859.4150043-2-arnd@kernel.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210727144859.4150043-1-arnd@kernel.org> References: <20210727144859.4150043-1-arnd@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Arnd Bergmann The locking is the same between the native and compat version of sys_kexec_load(), so it can be done in the common implementation to reduce duplication. Co-developed-by: Eric Biederman Co-developed-by: Christoph Hellwig Signed-off-by: Arnd Bergmann --- kernel/kexec.c | 44 ++++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/kernel/kexec.c b/kernel/kexec.c index c82c6c06f051..9c7aef8f4bb6 100644 --- a/kernel/kexec.c +++ b/kernel/kexec.c @@ -110,6 +110,17 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments, unsigned long i; int ret; + /* + * Because we write directly to the reserved memory region when loading + * crash kernels we need a mutex here to prevent multiple crash kernels + * from attempting to load simultaneously, and to prevent a crash kernel + * from loading over the top of a in use crash kernel. + * + * KISS: always take the mutex. + */ + if (!mutex_trylock(&kexec_mutex)) + return -EBUSY; + if (flags & KEXEC_ON_CRASH) { dest_image = &kexec_crash_image; if (kexec_crash_image) @@ -121,7 +132,8 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments, if (nr_segments == 0) { /* Uninstall image */ kimage_free(xchg(dest_image, NULL)); - return 0; + ret = 0; + goto out_unlock; } if (flags & KEXEC_ON_CRASH) { /* @@ -134,7 +146,7 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments, ret = kimage_alloc_init(&image, entry, nr_segments, segments, flags); if (ret) - return ret; + goto out_unlock; if (flags & KEXEC_PRESERVE_CONTEXT) image->preserve_context = 1; @@ -171,6 +183,8 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments, arch_kexec_protect_crashkres(); kimage_free(image); +out_unlock: + mutex_unlock(&kexec_mutex); return ret; } @@ -247,21 +261,8 @@ SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments, ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT)) return -EINVAL; - /* Because we write directly to the reserved memory - * region when loading crash kernels we need a mutex here to - * prevent multiple crash kernels from attempting to load - * simultaneously, and to prevent a crash kernel from loading - * over the top of a in use crash kernel. - * - * KISS: always take the mutex. - */ - if (!mutex_trylock(&kexec_mutex)) - return -EBUSY; - result = do_kexec_load(entry, nr_segments, segments, flags); - mutex_unlock(&kexec_mutex); - return result; } @@ -301,21 +302,8 @@ COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry, return -EFAULT; } - /* Because we write directly to the reserved memory - * region when loading crash kernels we need a mutex here to - * prevent multiple crash kernels from attempting to load - * simultaneously, and to prevent a crash kernel from loading - * over the top of a in use crash kernel. - * - * KISS: always take the mutex. - */ - if (!mutex_trylock(&kexec_mutex)) - return -EBUSY; - result = do_kexec_load(entry, nr_segments, ksegments, flags); - mutex_unlock(&kexec_mutex); - return result; } #endif -- 2.29.2