From patchwork Wed Jul 2 10:10:02 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 4464621 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 37D07BEEAA for ; Wed, 2 Jul 2014 10:13:07 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0B7BA2024F for ; Wed, 2 Jul 2014 10:13:06 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F107B2022A for ; Wed, 2 Jul 2014 10:13:04 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1X2HUv-0002XX-Nd; Wed, 02 Jul 2014 10:10:41 +0000 Received: from mail-we0-f180.google.com ([74.125.82.180]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1X2HUm-0002OX-D3 for linux-arm-kernel@lists.infradead.org; Wed, 02 Jul 2014 10:10:33 +0000 Received: by mail-we0-f180.google.com with SMTP id x48so10954296wes.25 for ; Wed, 02 Jul 2014 03:10:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=CrT8GM3EEVGaaPvgMXY2SnH7F9Y4E0Aa13Z8roJunNA=; b=I8G/oPj7JvE0bbFVu198hDfzBkwcCLh6bsV068OnsSKfDZl+bfVQPytXTMohr0VWVe fKIOEQMWafes/1A0v5m704Z9xBJRO2KXyKGppOzCFwIdVLHKlZ6zL5FQ5ausiCRRbN8d TOaRKanubyo+swHlVxtW84cFcznZidhW03HXv0nsgdTfODQRwSkRUGYa2hYsZZx9kXAn uoUzNSzZX7LgrwsdzEKBQTVpg/eGY3iFNXwuW2Xs75P7kFS94A3GwnNgpkPOFlky4a9c gPUYg4c14ViZHsJmRd42+J9cXfEGeqE1VKAnt/fz1X6qbSSgRMydLZc/MGXtrvMcYYBC RnsA== X-Gm-Message-State: ALoCoQnYiSItzJZVaQwkFLv6xke8dfMN266vzvRj0AAldRPkzreVgh/C8Ra5WYx8kP2iATajodmy X-Received: by 10.180.21.200 with SMTP id x8mr43170162wie.67.1404295810829; Wed, 02 Jul 2014 03:10:10 -0700 (PDT) Received: from ards-macbook-pro.local ([37.183.67.249]) by mx.google.com with ESMTPSA id hq20sm1966801wib.0.2014.07.02.03.10.09 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 02 Jul 2014 03:10:10 -0700 (PDT) From: Ard Biesheuvel To: matt.fleming@intel.com, x86@kernel.org Subject: [PATCH 2/2] efi: implement mandatory locking for UEFI Runtime Services Date: Wed, 2 Jul 2014 12:10:02 +0200 Message-Id: <1404295802-28030-2-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1404295802-28030-1-git-send-email-ard.biesheuvel@linaro.org> References: <1404295802-28030-1-git-send-email-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140702_031032_778911_6ADEC8FB X-CRM114-Status: GOOD ( 13.84 ) X-Spam-Score: -0.7 (/) Cc: linux-efi@vger.kernel.org, Ard Biesheuvel , catalin.marinas@arm.com, msalter@redhat.com, leif.lindholm@linaro.org, roy.franz@linaro.org, hpa@zytor.com, linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP According to section 7.1 of the UEFI spec, Runtime Services are not fully reentrant, and there are particular combinations of calls that need to be serialized. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/runtime-wrappers.c | 109 +++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 10 deletions(-) diff --git a/drivers/firmware/efi/runtime-wrappers.c b/drivers/firmware/efi/runtime-wrappers.c index 10daa4bbb258..6588d054af99 100644 --- a/drivers/firmware/efi/runtime-wrappers.c +++ b/drivers/firmware/efi/runtime-wrappers.c @@ -15,10 +15,50 @@ */ #include -#include /* spinlock_t */ +#include +#include #include /* + * According to section 7.1 of the UEFI spec, Runtime Services are not fully + * reentrant, and there are particular combinations of calls that need to be + * serialized. + * + * Table 31. Rules for Reentry Into Runtime Services + * +------------------------------------+-------------------------------+ + * | If previous call is busy in | Forbidden to call | + * +------------------------------------+-------------------------------+ + * | Any | SetVirtualAddressMap() | + * +------------------------------------+-------------------------------+ + * | ConvertPointer() | ConvertPointer() | + * +------------------------------------+-------------------------------+ + * | SetVariable() | ResetSystem() | + * | UpdateCapsule() | | + * | SetTime() | | + * | SetWakeupTime() | | + * | GetNextHighMonotonicCount() | | + * +------------------------------------+-------------------------------+ + * | GetVariable() | GetVariable() | + * | GetNextVariableName() | GetNextVariableName() | + * | SetVariable() | SetVariable() | + * | QueryVariableInfo() | QueryVariableInfo() | + * | UpdateCapsule() | UpdateCapsule() | + * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() | + * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() | + * +------------------------------------+-------------------------------+ + * | GetTime() | GetTime() | + * | SetTime() | SetTime() | + * | GetWakeupTime() | GetWakeupTime() | + * | SetWakeupTime() | SetWakeupTime() | + * +------------------------------------+-------------------------------+ + * + * The first two are disregarded here, as SetVirtualAddressMap() is called + * only once, and very early, and ConvertPointer() is not exposed at all. + */ +static DEFINE_MUTEX(var_ro_mutex); +static DEFINE_MUTEX(var_rw_mutex); + +/* * As per commit ef68c8f87ed1 ("x86: Serialize EFI time accesses on rtc_lock"), * the EFI specification requires that callers of the time related runtime * functions serialize with other CMOS accesses in the kernel, as the EFI time @@ -78,14 +118,25 @@ static efi_status_t virt_efi_get_variable(efi_char16_t *name, unsigned long *data_size, void *data) { - return efi_call_virt(get_variable, name, vendor, attr, data_size, data); + efi_status_t status; + + mutex_lock(&var_ro_mutex); + status = efi_call_virt(get_variable, name, vendor, attr, data_size, + data); + mutex_unlock(&var_ro_mutex); + return status; } static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) { - return efi_call_virt(get_next_variable, name_size, name, vendor); + efi_status_t status; + + mutex_lock(&var_ro_mutex); + status = efi_call_virt(get_next_variable, name_size, name, vendor); + mutex_unlock(&var_ro_mutex); + return status; } static efi_status_t virt_efi_set_variable(efi_char16_t *name, @@ -94,7 +145,15 @@ static efi_status_t virt_efi_set_variable(efi_char16_t *name, unsigned long data_size, void *data) { - return efi_call_virt(set_variable, name, vendor, attr, data_size, data); + efi_status_t status; + + mutex_lock(&var_ro_mutex); + mutex_lock(&var_rw_mutex); + status = efi_call_virt(set_variable, name, vendor, attr, data_size, + data); + mutex_unlock(&var_rw_mutex); + mutex_unlock(&var_ro_mutex); + return status; } static efi_status_t virt_efi_query_variable_info(u32 attr, @@ -102,16 +161,28 @@ static efi_status_t virt_efi_query_variable_info(u32 attr, u64 *remaining_space, u64 *max_variable_size) { + efi_status_t status; + if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) return EFI_UNSUPPORTED; - return efi_call_virt(query_variable_info, attr, storage_space, - remaining_space, max_variable_size); + mutex_lock(&var_ro_mutex); + status = efi_call_virt(query_variable_info, attr, storage_space, + remaining_space, max_variable_size); + mutex_unlock(&var_ro_mutex); + return status; } static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) { - return efi_call_virt(get_next_high_mono_count, count); + efi_status_t status; + + mutex_lock(&var_ro_mutex); + mutex_lock(&var_rw_mutex); + status = efi_call_virt(get_next_high_mono_count, count); + mutex_unlock(&var_rw_mutex); + mutex_unlock(&var_ro_mutex); + return status; } static void virt_efi_reset_system(int reset_type, @@ -119,17 +190,30 @@ static void virt_efi_reset_system(int reset_type, unsigned long data_size, efi_char16_t *data) { + unsigned long flags; + + mutex_lock(&var_rw_mutex); + spin_lock_irqsave(&rtc_lock, flags); __efi_call_virt(reset_system, reset_type, status, data_size, data); + spin_unlock_irqrestore(&rtc_lock, flags); + mutex_unlock(&var_rw_mutex); } static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, unsigned long count, unsigned long sg_list) { + efi_status_t status; + if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) return EFI_UNSUPPORTED; - return efi_call_virt(update_capsule, capsules, count, sg_list); + mutex_lock(&var_ro_mutex); + mutex_lock(&var_rw_mutex); + status = efi_call_virt(update_capsule, capsules, count, sg_list); + mutex_unlock(&var_rw_mutex); + mutex_unlock(&var_ro_mutex); + return status; } static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, @@ -137,11 +221,16 @@ static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, u64 *max_size, int *reset_type) { + efi_status_t status; + if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) return EFI_UNSUPPORTED; - return efi_call_virt(query_capsule_caps, capsules, count, max_size, - reset_type); + mutex_lock(&var_ro_mutex); + status = efi_call_virt(query_capsule_caps, capsules, count, max_size, + reset_type); + mutex_unlock(&var_ro_mutex); + return status; } void efi_native_runtime_setup(void)