From patchwork Thu Nov 29 17:12:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 152413 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2633969ljp; Thu, 29 Nov 2018 09:12:53 -0800 (PST) X-Google-Smtp-Source: AFSGD/WAzUV1rZp6Gv9mJ22/9JZoQew+LIOJCKD1Ki7r8zZA2jlWOEfhUIXqqCCC35QVmVSAlxuG X-Received: by 2002:a17:902:8e8a:: with SMTP id bg10mr2293242plb.192.1543511573581; Thu, 29 Nov 2018 09:12:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543511573; cv=none; d=google.com; s=arc-20160816; b=LV5JuHnKGA6rmb2WPvciVBsCJ51ivdlkaGfza9mF/2WcsbV2wLV1jW6gHAPEaK09te eIKkWIIQ0pJv3dw3zghQixcmMy42aTX94FNbuyCPW8MDpcWcFzvnbpQWSpdzi9VhULE/ Y98ywZEGX1a5rS/M6aUCTalFmrF3RPr3QccaKvHkZDfy0b76EfKAlqLTT6giRJqVP0cZ 2c1Pc57vBa0louih+X/d4Oc5pfJ3AlawNctGdnsNjSYOcoK3UOTddiHRFl8y7LfYRdtP +Ek36eocufmZH5EfPTidFAZrFk8vu8EkzBiVH1KH/52Q3mCu9xCV3lJYrIAU22AzBDFc goNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XsSaaAOivGZ+SMEV1HNl9ikqQLGcv3idzYmN9Jqzt5c=; b=lQQCHXoyO4kexY7MKBfS9Gx9UsOeVMOW0oY15dA4KiU6noFcm0ydOIqIUBqmY3XCwO 4JHQhD44GEAopb2pDilxK1jqs05/8yuWvPN2ftuQNDMfnfNzT4E4A97H1GTPVls8yIH/ ogt/W8w7fo3/yO1RG07nC4ZXVL6DPD38BEw7tn+A+dvVYO3fzvCzi98f+7IDxIH9l4C6 XjhJ0CELN6Dxilh8/z0wlCf+iKRcft8tVRagnXegQT0YJFpO9Dop3kUhntqV+ZsXYY+K vV1QsLsx8fF02MAU83DZW6djo1DAuhGd/03s51frR1eqSSUUTdRnpijKePGXomgTthro VR/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dcC5p86o; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l124si2518448pfl.284.2018.11.29.09.12.52; Thu, 29 Nov 2018 09:12:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dcC5p86o; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730474AbeK3ES4 (ORCPT + 32 others); Thu, 29 Nov 2018 23:18:56 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:34606 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728157AbeK3ESz (ORCPT ); Thu, 29 Nov 2018 23:18:55 -0500 Received: by mail-wr1-f66.google.com with SMTP id j2so2695273wrw.1 for ; Thu, 29 Nov 2018 09:12:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XsSaaAOivGZ+SMEV1HNl9ikqQLGcv3idzYmN9Jqzt5c=; b=dcC5p86oeYdf0PuJPXvTl+IxGk6KAwIaDC40VblPUnrv10ctKWv/QPjA56MPMCwhOv hU199FHB3jis9/WVya/WmQqBCO4nYRQ0d2+pgQaDLnMuyyFK65YmLI6S88u29mjMQv0v U/k1IirkxTFV43Pui5b9993IyZqHYQTQnwNGo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XsSaaAOivGZ+SMEV1HNl9ikqQLGcv3idzYmN9Jqzt5c=; b=pfEsFMj8AOPug4reYWcDmezZSJQz1leSA7xgFe/g/DAEj1QBfTmEgQ2jYE6MBixP4o bg5fZPOhqC6rZi9vfij49ow7Zrsi2qt9oERPlunWI9DXdB3dIQGD2/5EXw2q+7zqJZLa fjLejl4cEYYNMLD33FDy+whKNhXEzUJBH9+RbjSUvnic56Ms8E5gfjEsTzA3bc0xECvM V6qb/4keZXAe4KJsmNhyE6CYEgQOhO6Ws/soq/FYLtlr28DErfT8OCI7HCOjuWJpts/x sTF7073zIo9xrf7LfRoEF6oDai1+aNTjcQepPtubKr2chw/VwJVtboaxvKMCSp2QryxE Ev5w== X-Gm-Message-State: AA+aEWZY0KUhrIAGNovzSZaNv8PnI2xFpC6XUI4VbEu8UOgwkQult45q EGi43M5wX7jswav9ngierv1cAA== X-Received: by 2002:adf:d0c9:: with SMTP id z9mr2063343wrh.317.1543511568971; Thu, 29 Nov 2018 09:12:48 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:f070:d240:312e:9f99]) by smtp.gmail.com with ESMTPSA id y185sm1593882wmg.34.2018.11.29.09.12.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 09:12:48 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Andy Lutomirski , Arend van Spriel , Bhupesh Sharma , Borislav Petkov , Dave Hansen , Eric Snowberg , Hans de Goede , Joe Perches , Jon Hunter , Julien Thierry , Marc Zyngier , Nathan Chancellor , Peter Zijlstra , Sai Praneeth Prakhya , Sedat Dilek , YiFei Zhu Subject: [PATCH 01/11] x86/efi: Allocate e820 buffer before calling efi_exit_boot_service Date: Thu, 29 Nov 2018 18:12:20 +0100 Message-Id: <20181129171230.18699-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181129171230.18699-1-ard.biesheuvel@linaro.org> References: <20181129171230.18699-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Snowberg Commit d64934019f6c ("x86/efi: Use efi_exit_boot_services()") introduced a regression on systems with large memory maps causing them to hang on boot. The first "goto get_map" that was removed from exit_boot insured there was enough room for the memory map when efi_call_early(exit_boot_services) was called. This happens when (nr_desc > ARRAY_SIZE(params->e820_table). Chain of events: exit_boot() efi_exit_boot_services() efi_get_memory_map <- at this point the mm can't grow over 8 desc priv_func() exit_boot_func() allocate_e820ext() <- new mm grows over 8 desc from e820 alloc efi_call_early(exit_boot_services) <- mm key doesn't match so retry efi_call_early(get_memory_map) <- not enough room for new mm system hangs This patch allocates the e820 buffer before calling efi_exit_boot_services and fixes the regression. Signed-off-by: Eric Snowberg Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/eboot.c | 65 ++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 24 deletions(-) -- 2.19.1 diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c index 8b4c5e001157..f7bad07bb251 100644 --- a/arch/x86/boot/compressed/eboot.c +++ b/arch/x86/boot/compressed/eboot.c @@ -1,3 +1,4 @@ + /* ----------------------------------------------------------------------- * * Copyright 2011 Intel Corporation; author Matt Fleming @@ -634,37 +635,54 @@ static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, return status; } +static efi_status_t allocate_e820(struct boot_params *params, + struct setup_data **e820ext, + u32 *e820ext_size) +{ + unsigned long map_size, desc_size, buff_size; + struct efi_boot_memmap boot_map; + efi_memory_desc_t *map; + efi_status_t status; + __u32 nr_desc; + + boot_map.map = ↦ + boot_map.map_size = &map_size; + boot_map.desc_size = &desc_size; + boot_map.desc_ver = NULL; + boot_map.key_ptr = NULL; + boot_map.buff_size = &buff_size; + + status = efi_get_memory_map(sys_table, &boot_map); + if (status != EFI_SUCCESS) + return status; + + nr_desc = buff_size / desc_size; + + if (nr_desc > ARRAY_SIZE(params->e820_table)) { + u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table); + + status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size); + if (status != EFI_SUCCESS) + return status; + } + + return EFI_SUCCESS; +} + struct exit_boot_struct { struct boot_params *boot_params; struct efi_info *efi; - struct setup_data *e820ext; - __u32 e820ext_size; }; static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg, struct efi_boot_memmap *map, void *priv) { - static bool first = true; const char *signature; __u32 nr_desc; efi_status_t status; struct exit_boot_struct *p = priv; - if (first) { - nr_desc = *map->buff_size / *map->desc_size; - if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) { - u32 nr_e820ext = nr_desc - - ARRAY_SIZE(p->boot_params->e820_table); - - status = alloc_e820ext(nr_e820ext, &p->e820ext, - &p->e820ext_size); - if (status != EFI_SUCCESS) - return status; - } - first = false; - } - signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE; memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32)); @@ -687,8 +705,8 @@ static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) { unsigned long map_sz, key, desc_size, buff_size; efi_memory_desc_t *mem_map; - struct setup_data *e820ext; - __u32 e820ext_size; + struct setup_data *e820ext = NULL; + __u32 e820ext_size = 0; efi_status_t status; __u32 desc_version; struct efi_boot_memmap map; @@ -702,8 +720,10 @@ static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) map.buff_size = &buff_size; priv.boot_params = boot_params; priv.efi = &boot_params->efi_info; - priv.e820ext = NULL; - priv.e820ext_size = 0; + + status = allocate_e820(boot_params, &e820ext, &e820ext_size); + if (status != EFI_SUCCESS) + return status; /* Might as well exit boot services now */ status = efi_exit_boot_services(sys_table, handle, &map, &priv, @@ -711,9 +731,6 @@ static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) if (status != EFI_SUCCESS) return status; - e820ext = priv.e820ext; - e820ext_size = priv.e820ext_size; - /* Historic? */ boot_params->alt_mem_k = 32 * 1024; From patchwork Thu Nov 29 17:12:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 152414 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2634008ljp; Thu, 29 Nov 2018 09:12:55 -0800 (PST) X-Google-Smtp-Source: AFSGD/W6pMWS5w50Czchp/P5F4e3ySpsVYu3YCUI78CW5HCZZoCNU+DFuJn5jsi94dxw0DGsFz1O X-Received: by 2002:a63:2f86:: with SMTP id v128mr1826577pgv.407.1543511575566; Thu, 29 Nov 2018 09:12:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543511575; cv=none; d=google.com; s=arc-20160816; b=0NkLM7mboQ8U79S4Nynb2fD/uLqXcdIUciLOdRpBGMqUaiIsiAx6maG6Y5WQQ5Y+Fr U0eN8gsSW2MX2/nDh/5WIHR1bJU+wuXpSDjQJnGnS9AOu1vwl+f9t/nCFKtRnRhNTfyQ zshnEMgAUXcGTfQ2R4vm2JeboFaIzaR/4YkuA5Dd6g7ji7YgoiXg7PRIQK7LNfgsimqh wMfpdbJM+wUH6rtPW4ec26ZCNU7ItXCXM+LFQWEeVjfERmiiW1Ov7JkkUOjldoXRHgLM GUtkwBqroPQPv+wO+AquqNRq1WJMub3zyfC+Ebe7IS4fKStBowAODJmRz9+B6wH6/NNk arDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=W6pQ+cWziH5wp1GX7RQuhsmgKROae0TuArs9rA7OjYM=; b=czC4MORKHucNNdCNH73MUP9WoBugpBIjYN3u1vSJIz2BwIzhjL8k/8zKA9RPDM1IBd Q2Lp/ZNrLQpHoEGNTwJCix6QdSdPN11yl2IaZiP5Tz/Irirkry/8XZ7/T0yX2cwOeRrG HcHoo78EU/Hup/WLYNVwss7ft90A/uVWmYo/ON1gg5AEcMUiMctkQc8+CcYp9YAghprp Yz1r/8McrDfxTJuu7dwLAdiizPFbIxjDaMj8ngsRboI7vdY8qktOzL8rPpXHNp7M1pAm GQmGkGhrfrkwf+ozBmjDlSggEZr/FOBEDYTO/Bsv27r1FDLFvUEQlkjyCzSSonp5ysU7 S73A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Nj8ypOD3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e8si2375768pgn.325.2018.11.29.09.12.55; Thu, 29 Nov 2018 09:12:55 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Nj8ypOD3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730502AbeK3ES6 (ORCPT + 32 others); Thu, 29 Nov 2018 23:18:58 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:42183 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728255AbeK3ES5 (ORCPT ); Thu, 29 Nov 2018 23:18:57 -0500 Received: by mail-wr1-f65.google.com with SMTP id q18so2675801wrx.9 for ; Thu, 29 Nov 2018 09:12:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=W6pQ+cWziH5wp1GX7RQuhsmgKROae0TuArs9rA7OjYM=; b=Nj8ypOD3m5NUeeLg32+/ZRUO7WepVP/zGVgu7Q921+VN4GoCB30KMRPfmc9P+sW49v rMiPjx0DQtqOXP9JaRFfCLdDDHMfoHsuOoAIdB+OiFMa/PFbB4pLv8CLFnBg221Co+IH L6oKyi3GO0r2h4sxm4Ofk5UlIbTgInq0LD8QQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=W6pQ+cWziH5wp1GX7RQuhsmgKROae0TuArs9rA7OjYM=; b=eRWCa6epoaRjIkdEhg3mDyw06eBkDUC/WE8c+z/RZIBMmDNhB4t/3MWlMLdNXMCCcN gPQHJewj4qSMwesiWWR5R2d4HIk40Y1J2SI5UL/TZO718lrz6Hutdh0CU1el5uwMxTEv dnUu+fGSferAyOBRbvDNqijcxMLn/L6hq4zPf/XP45Fe4zU3T/TYdkUyinkPNvMfEQq+ FtJvg0iA6Ns2wew1xA4CUTZqPHmUPz62OdYJuNqu34mUz9M7xJhjzK4INoTH/pqdkbED KOYj/Ci7lgdizvxIMMaq087iVzfDheNNgs34UakHc/IcTSOzk3GzBHloLHKtW8wFd8fW cmBQ== X-Gm-Message-State: AA+aEWZuzcjP5i7LOB0OXFQUWZh0MoGfX/8SbNQ4tI+jWr3Tfcx7jNg4 VYlP7BPt7m+JXcBUQ1o0NblZyA== X-Received: by 2002:adf:f550:: with SMTP id j16mr2112685wrp.258.1543511571113; Thu, 29 Nov 2018 09:12:51 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:f070:d240:312e:9f99]) by smtp.gmail.com with ESMTPSA id y185sm1593882wmg.34.2018.11.29.09.12.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 09:12:50 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Andy Lutomirski , Arend van Spriel , Bhupesh Sharma , Borislav Petkov , Dave Hansen , Eric Snowberg , Hans de Goede , Joe Perches , Jon Hunter , Julien Thierry , Marc Zyngier , Nathan Chancellor , Peter Zijlstra , Sai Praneeth Prakhya , Sedat Dilek , YiFei Zhu Subject: [PATCH 02/11] efi/fdt: Indentation fix Date: Thu, 29 Nov 2018 18:12:21 +0100 Message-Id: <20181129171230.18699-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181129171230.18699-1-ard.biesheuvel@linaro.org> References: <20181129171230.18699-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Julien Thierry Closing bracket seems to end a for statement when it is actually ending the contained if. Add some brackets to have clear delimitation of each scope. No functional change/fix, just fix the indentation. Signed-off-by: Julien Thierry Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/fdt.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) -- 2.19.1 diff --git a/drivers/firmware/efi/libstub/fdt.c b/drivers/firmware/efi/libstub/fdt.c index 0c0d2312f4a8..a3614f9b5f75 100644 --- a/drivers/firmware/efi/libstub/fdt.c +++ b/drivers/firmware/efi/libstub/fdt.c @@ -376,7 +376,7 @@ void *get_fdt(efi_system_table_t *sys_table, unsigned long *fdt_size) tables = (efi_config_table_t *) sys_table->tables; fdt = NULL; - for (i = 0; i < sys_table->nr_tables; i++) + for (i = 0; i < sys_table->nr_tables; i++) { if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) { fdt = (void *) tables[i].table; if (fdt_check_header(fdt) != 0) { @@ -385,7 +385,8 @@ void *get_fdt(efi_system_table_t *sys_table, unsigned long *fdt_size) } *fdt_size = fdt_totalsize(fdt); break; - } + } + } return fdt; } From patchwork Thu Nov 29 17:12:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 152416 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2634066ljp; Thu, 29 Nov 2018 09:12:59 -0800 (PST) X-Google-Smtp-Source: AFSGD/XVBAgauMzLDoRp/9GRy6XVtjSdkQCuInd9nnpdfHCaQ2M8x1GmblQVJPouwTxtY36yIc4E X-Received: by 2002:a63:f047:: with SMTP id s7mr1889338pgj.441.1543511579126; Thu, 29 Nov 2018 09:12:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543511579; cv=none; d=google.com; s=arc-20160816; b=Ns9Fa6zBOuH6TQbo92Y++Uwu8c9TbH0wAAVbXv7ODPGnYoBCn3gO6w/6u7Qlps6LUE 3wG/dfOshmkYNCRAy0CyeREIHBo5aj5vO8CcxLOGwofz7fKK4fWD+2lIA4M6bL8lhHbz XcyejbAdQ5BL4y5SMgfH48RK3VgyNTKQhgjA3T7KnLGRSce6RxwDU79jQqgfvVx00dEf 7gxj1hGQlWFL5CVK2vV3nz27XdrP7Ni8fm4P4xl0pPWN9Rr/SVl1tOzSv7uq2M4YKx/v pSlPCSUIdJLHBiNd+YKcLNTLJlORf3B++uCE0xBpxFZbM6vs6JOuUn0gTSjxooUk3Ohr KuIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9iTHNAMi/B7M59UNr1H/N7bE0xz4z3gfTlAxn3XU6zk=; b=OveSWujTVhiOW5BaNJD2t6J7GdE7N8OkSCVpLrGyhRfKkzGK5e/sZR7Wds5RPfPA20 V19BLl3TBSzWY8nLvDSDdXCa8Nu11K2Ys/WnggoM4u8zL8rW558K0Ie6wetpqniFx6EX 26C1Ph3pj5Px9qsc5aOPhm06WbBMZIENrntS3RsaOCQovK2CThdmW24NRIbdxvweg+9+ E2L4sAXoyXqXnJSaHqoLjqTy/t8z1YgMYDpp5KON9+UwQ2XzaBFEjhBRvHFm3HX16LJs 44FOiHm8X9vWoU2it/grf6n4O73AD9nAWiIibA7B7ILNsUmPrMBOE93WG0dUlp+lFgLC FHkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IggHAJei; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 2si2476101pgz.395.2018.11.29.09.12.58; Thu, 29 Nov 2018 09:12:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IggHAJei; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730535AbeK3ETB (ORCPT + 32 others); Thu, 29 Nov 2018 23:19:01 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:35048 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728554AbeK3ETB (ORCPT ); Thu, 29 Nov 2018 23:19:01 -0500 Received: by mail-wr1-f65.google.com with SMTP id 96so2696142wrb.2 for ; Thu, 29 Nov 2018 09:12:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9iTHNAMi/B7M59UNr1H/N7bE0xz4z3gfTlAxn3XU6zk=; b=IggHAJei/HY1eekMT9MKvSsKn2ZhjsFwMEfCojcv0lIodqCRBUHbdO8IqfqEPCge5c XqAVKSQE6Jc7+/UASrkQRlFwrIyPAziUKerwEFvHVV/ff5prnWM9DOFcWDDch0Xu7hlb PvTEJ6tDpRrmrFGOtTXlBs3p87E9yD9WX8CGw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9iTHNAMi/B7M59UNr1H/N7bE0xz4z3gfTlAxn3XU6zk=; b=IFPPzr+WoiYPpuel8giaExXcONCLYWWAi48CdhVsbNmj+n5pHCvK4+cNSQVnTt31Cp Ycv2MBpdLe0zArQeSMhCwfRbOSG1lAbxCAeEaLR7pFn55LyxLBgmoKLo+9EG0a4D+CxC Xp1VQNNF44gHSDTBmquwv/86ze6/H7jESvakZTB9UgJ6MJXjedqx2ZxgY0GBQQeQzlo6 nITG5QlFSarNcIlEgl+MRv3eVD/Qo9u92cQn1y4kHEWnwbR4rtzObbOEkZLpWeF5yS37 B55Kd3ty2QoUxWcmbkTfFgn706Kx5K8E/ESJIDZOQq5HZd64RzZHIq+eegpB3BeJ57m9 EWUQ== X-Gm-Message-State: AA+aEWYa4mbP2GVZod5P5r4YNBo9w3txoM/vNfKIxg3jOSnWUaSQLjyr Jaf5/YWqzrOer5fCDvKvBp2jSg== X-Received: by 2002:adf:9ec8:: with SMTP id b8mr2256654wrf.164.1543511574762; Thu, 29 Nov 2018 09:12:54 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:f070:d240:312e:9f99]) by smtp.gmail.com with ESMTPSA id y185sm1593882wmg.34.2018.11.29.09.12.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 09:12:53 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Andy Lutomirski , Arend van Spriel , Bhupesh Sharma , Borislav Petkov , Dave Hansen , Eric Snowberg , Hans de Goede , Joe Perches , Jon Hunter , Julien Thierry , Marc Zyngier , Nathan Chancellor , Peter Zijlstra , Sai Praneeth Prakhya , Sedat Dilek , YiFei Zhu Subject: [PATCH 04/11] x86/mm/pageattr: Introduce helper function to unmap EFI boot services Date: Thu, 29 Nov 2018 18:12:23 +0100 Message-Id: <20181129171230.18699-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181129171230.18699-1-ard.biesheuvel@linaro.org> References: <20181129171230.18699-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sai Praneeth Prakhya Ideally, after kernel assumes control of the platform, firmware shouldn't access EFI boot services code/data regions. But, it's noticed that this is not so true in many x86 platforms. Hence, during boot, kernel reserves EFI boot services code/data regions [1] and maps [2] them to efi_pgd so that call to set_virtual_address_map() doesn't fail. After returning from set_virtual_address_map(), kernel frees the reserved regions [3] but they still remain mapped. Hence, introduce kernel_unmap_pages_in_pgd() which will later be used to unmap EFI boot services code/data regions. While at it modify kernel_map_pages_in_pgd() by 1. Adding __init modifier because it's always used *only* during boot. 2. Add a warning if it's used after SMP is initialized because it uses __flush_tlb_all() which flushes mappings only on current CPU. Unmapping EFI boot services code/data regions will result in clearing PAGE_PRESENT bit and it shouldn't bother L1TF cases because it's already handled by protnone_mask() at arch/x86/include/asm/pgtable-invert.h. [1] efi_reserve_boot_services() [2] efi_map_region() -> __map_region() -> kernel_map_pages_in_pgd() [3] efi_free_boot_services() Signed-off-by: Sai Praneeth Prakhya Cc: Borislav Petkov Cc: Ingo Molnar Cc: Andy Lutomirski Cc: Dave Hansen Cc: Bhupesh Sharma Cc: Peter Zijlstra Reviewed-by: Thomas Gleixner Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/pgtable_types.h | 8 ++++-- arch/x86/mm/pageattr.c | 40 ++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) -- 2.19.1 diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pgtable_types.h index 106b7d0e2dae..d6ff0bbdb394 100644 --- a/arch/x86/include/asm/pgtable_types.h +++ b/arch/x86/include/asm/pgtable_types.h @@ -564,8 +564,12 @@ extern pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address, unsigned int *level); extern pmd_t *lookup_pmd_address(unsigned long address); extern phys_addr_t slow_virt_to_phys(void *__address); -extern int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address, - unsigned numpages, unsigned long page_flags); +extern int __init kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, + unsigned long address, + unsigned numpages, + unsigned long page_flags); +extern int __init kernel_unmap_pages_in_pgd(pgd_t *pgd, unsigned long address, + unsigned long numpages); #endif /* !__ASSEMBLY__ */ #endif /* _ASM_X86_PGTABLE_DEFS_H */ diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c index db7a10082238..bac35001d896 100644 --- a/arch/x86/mm/pageattr.c +++ b/arch/x86/mm/pageattr.c @@ -2338,8 +2338,8 @@ bool kernel_page_present(struct page *page) #endif /* CONFIG_DEBUG_PAGEALLOC */ -int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address, - unsigned numpages, unsigned long page_flags) +int __init kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address, + unsigned numpages, unsigned long page_flags) { int retval = -EINVAL; @@ -2353,6 +2353,8 @@ int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address, .flags = 0, }; + WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP"); + if (!(__supported_pte_mask & _PAGE_NX)) goto out; @@ -2374,6 +2376,40 @@ int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address, return retval; } +/* + * __flush_tlb_all() flushes mappings only on current CPU and hence this + * function shouldn't be used in an SMP environment. Presently, it's used only + * during boot (way before smp_init()) by EFI subsystem and hence is ok. + */ +int __init kernel_unmap_pages_in_pgd(pgd_t *pgd, unsigned long address, + unsigned long numpages) +{ + int retval; + + /* + * The typical sequence for unmapping is to find a pte through + * lookup_address_in_pgd() (ideally, it should never return NULL because + * the address is already mapped) and change it's protections. As pfn is + * the *target* of a mapping, it's not useful while unmapping. + */ + struct cpa_data cpa = { + .vaddr = &address, + .pfn = 0, + .pgd = pgd, + .numpages = numpages, + .mask_set = __pgprot(0), + .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW), + .flags = 0, + }; + + WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP"); + + retval = __change_page_attr_set_clr(&cpa, 0); + __flush_tlb_all(); + + return retval; +} + /* * The testcases use internal knowledge of the implementation that shouldn't * be exposed to the rest of the kernel. Include these directly here. From patchwork Thu Nov 29 17:12:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 152417 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2634105ljp; Thu, 29 Nov 2018 09:13:01 -0800 (PST) X-Google-Smtp-Source: AFSGD/UfevhGctWDmGDcUnwTUMaDq57LzhJB+0oU5Z+iZrhoqB47BLIdNE11LnqVgQRTeWFt6+bV X-Received: by 2002:a63:d005:: with SMTP id z5mr1931864pgf.64.1543511581268; Thu, 29 Nov 2018 09:13:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543511581; cv=none; d=google.com; s=arc-20160816; b=MEmKhSx5qwieF0YFYMQgHfKB7EWOdgEtgGaLx2i1Z9KSMKAeLZqbVyY4RuQJBK7I/i HwCrPnDo1A2soGBs9D98RdKJh+H+6k5hzVpVQN7XHFUpAeTsP6ad/yAEaLOEtJIFDBbJ MINEtQOePBjuLDOqhf+MSwbWOGlfadAM4gLlcFiIezWBZgAY0DqOIdO+pkKd6sp6wI26 C4HDmayPXAvuBHdL52AmArC8Kbq3XUeaDaAOszYcLEf9iMsegFGgeoUg/8ALPWmfJzso q7xv0Q/8EnhiVRXqd1s/we/mJ0iX7Fx1PXbRrKSb2RPkHF5ZsBGGhZa/QXe1m1tQAD5P xeyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=otiyS0qoWsuunniYKJ1BwM1dERK1qmd7Q4dc5cs0rtA=; b=lC4MwG/R6iZukVCk42MYaaGdyuD46XlJ/hN/kmWtK2KEEhCYBDF6XlEmtLwR14EEPh vuaSTLEXYJ0wfSfYz3NBb+kV4ytaMeMSQXdu82gHtCr1qBU6MlKiqrT9O1gTKNZaeaLF KbPcxlQ3wxZVxzvEb/EnQa6nzT7WClqocv2qKi3tJNyIMV0GX6+vlG1nKWauTKpD4ZJ0 22tDbFiLQ4m7wF/TK9wHK9KOGbvDTk4TaTDSBXyEVYER+O6KejDcbBUvWcgEf5nqFcnm Aiy02iEs8ohJu+4Xf8HbxB7roHVpFpxxTTJvwGUx9c5Df5rw3tzeRIIcZooj4OD/NP7y joqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=j7gRNaTR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j61si2643262plb.232.2018.11.29.09.13.00; Thu, 29 Nov 2018 09:13:01 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=j7gRNaTR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730576AbeK3ETD (ORCPT + 32 others); Thu, 29 Nov 2018 23:19:03 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:53164 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730504AbeK3ETC (ORCPT ); Thu, 29 Nov 2018 23:19:02 -0500 Received: by mail-wm1-f65.google.com with SMTP id r11-v6so3066032wmb.2 for ; Thu, 29 Nov 2018 09:12:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=otiyS0qoWsuunniYKJ1BwM1dERK1qmd7Q4dc5cs0rtA=; b=j7gRNaTRiQ5ED0EKIyqB5OZVqFlopVcjKBcwU7WVTKKDNPA+l+hh3yBjmBd0+ZXCNM rrR87AZhWnRlTg+jbZzCsdue9gXmLeHFBz40pmpno3bSdRjQOP9NLqdYERHBNX+HXkOf MIllGYKc+Lfp3mnThSgb2dqkpQADtQc7ybq/M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=otiyS0qoWsuunniYKJ1BwM1dERK1qmd7Q4dc5cs0rtA=; b=mJv/13dWVSKnArL7bClPWzGs4ZQBAyBIklvLD0gnn/Hxaf702l9g7p4mCKaMeEyfKE PWCNyWFCavJ9ci8bP1grqDFRNUXApA4d0M7v6Z+vmqf/UXgTrp8qy+bpHrZa1smraN0A TqPVVkZgxFCIqCzQ0tlHY4+C+bIkb7Dp4y0wkWKgsO+7SSqPY5Dfu9h2XEYpoaUdpMUP FKAG5hMDkZ+6qac9CgdQe6dxMxhuXVoP5P4Kggg4KKvQsufId+f4S1zTPqZNvMcfXY+k pBen166m5VssZPWMnySfuSikqMrOnVSWjWvrqd+C8I5hhQoBDI+Ekf33dZom/EHHNQ+0 LnrQ== X-Gm-Message-State: AA+aEWa6l26ZZMq2RjThiCnCoJmsfXONmQY44943iNsC5Y+TgIq4k3YL keCm6F1qFfR3HHWevuOvHqC9sw== X-Received: by 2002:a1c:6a01:: with SMTP id f1mr2608832wmc.17.1543511576404; Thu, 29 Nov 2018 09:12:56 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:f070:d240:312e:9f99]) by smtp.gmail.com with ESMTPSA id y185sm1593882wmg.34.2018.11.29.09.12.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 09:12:55 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Andy Lutomirski , Arend van Spriel , Bhupesh Sharma , Borislav Petkov , Dave Hansen , Eric Snowberg , Hans de Goede , Joe Perches , Jon Hunter , Julien Thierry , Marc Zyngier , Nathan Chancellor , Peter Zijlstra , Sai Praneeth Prakhya , Sedat Dilek , YiFei Zhu Subject: [PATCH 05/11] x86/efi: Unmap EFI boot services code/data regions from efi_pgd Date: Thu, 29 Nov 2018 18:12:24 +0100 Message-Id: <20181129171230.18699-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181129171230.18699-1-ard.biesheuvel@linaro.org> References: <20181129171230.18699-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sai Praneeth Prakhya efi_free_boot_services(), as the name suggests, frees EFI boot services code/data regions but forgets to unmap these regions from efi_pgd. This means that any code that's running in efi_pgd address space (e.g: any EFI runtime service) would still be able to access these regions but the contents of these regions would have long been over written by someone else. So, it's important to unmap these regions. Hence, introduce efi_unmap_pages() to unmap these regions from efi_pgd. After unmapping EFI boot services code/data regions, any illegal access by buggy firmware to these regions would result in page fault which will be handled by EFI specific fault handler. Signed-off-by: Sai Praneeth Prakhya Cc: Borislav Petkov Cc: Ingo Molnar Cc: Andy Lutomirski Cc: Dave Hansen Cc: Bhupesh Sharma Cc: Peter Zijlstra Acked-by: Thomas Gleixner Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) -- 2.19.1 diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 95e77a667ba5..09e811b9da26 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -369,6 +369,24 @@ void __init efi_reserve_boot_services(void) } } +/* + * Apart from having VA mappings for EFI boot services code/data regions, + * (duplicate) 1:1 mappings were also created as a quirk for buggy firmware. So, + * unmap both 1:1 and VA mappings. + */ +static void __init efi_unmap_pages(efi_memory_desc_t *md) +{ + pgd_t *pgd = efi_mm.pgd; + u64 pa = md->phys_addr; + u64 va = md->virt_addr; + + if (kernel_unmap_pages_in_pgd(pgd, pa, md->num_pages)) + pr_err("Failed to unmap 1:1 mapping for 0x%llx\n", pa); + + if (kernel_unmap_pages_in_pgd(pgd, va, md->num_pages)) + pr_err("Failed to unmap VA mapping for 0x%llx\n", va); +} + void __init efi_free_boot_services(void) { phys_addr_t new_phys, new_size; @@ -393,6 +411,13 @@ void __init efi_free_boot_services(void) continue; } + /* + * Before calling set_virtual_address_map(), EFI boot services + * code/data regions were mapped as a quirk for buggy firmware. + * Unmap them from efi_pgd before freeing them up. + */ + efi_unmap_pages(md); + /* * Nasty quirk: if all sub-1MB memory is used for boot * services, we can get here without having allocated the From patchwork Thu Nov 29 17:12:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 152418 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2634164ljp; Thu, 29 Nov 2018 09:13:03 -0800 (PST) X-Google-Smtp-Source: AFSGD/VJ6lp3Z9kM6lI1DoMX61WFoSYwX+eVUu/HgOlUm1eY3RhPqaRTjwxNIhekUOyvXeQTTmz0 X-Received: by 2002:a62:7892:: with SMTP id t140mr2148655pfc.237.1543511583344; Thu, 29 Nov 2018 09:13:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543511583; cv=none; d=google.com; s=arc-20160816; b=ZunB7DrAIhDudZAOMSEefWzc4EjI6iJ68mjl5tIhncbcvssZnyAApvir17ILh85oiS MRxtAF/sht5jkRW2BA7ZiGD66EKV20bbF4Vmi5RNHXBAKgd1w54vx8RlYwJyGc6BZK62 C+ncYPQTlLt04lIyPd8nRZa4Q6YD2AsZF9AyA7H/aADiLDhidf/2XhpjHjK/1fpuIj2S Al0ZTqfS4NVBHEzluv19DwvMNvttGspVWQSQlCRPya68Oy68Uqveppmwh/Das6lLPWsV YApQR4PZdwbIr7n3pW4T3nW6erjaktaYB9jgpLteBMC4ZKJ227HxK5hb4QxF1k2qbkyr V7ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kZ9qAKUFhDNWQgwFHfyYbRlwlOBWhnuPIzx1kDDdeg4=; b=XU1cqHg+TvBZvGwYzJmR/3/GLzOK4Z9ZeDhmb2rCq5J2tatmLECucjvFyjuLc4xycx 2yt6j6+l/t3V/4bf4uy3pB+BzQE0fXZAhiLLVS7drEzKAwzXX65I1xdiVuKBS/cD0kF8 ddRb/qdGcTxoxNq6MbMjObAcQere/D/MNNfVUH6DNtue0lcDsYH+5iIq4AgNKOTfDiCF SK/DQ9xyMAefqaXLJEDLAZYdQsJivZR77m/doKCvzR/ErXWirgs+jPVQr8UaHiXVdMj5 /4JfkGiHSiOS9QtHXWOREJafNrP2xIosuYfLHV8VCXDGNA66fQEjaGuzrVMdITp169in GvGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Yl67IxDW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r77-v6si2618065pfa.69.2018.11.29.09.13.02; Thu, 29 Nov 2018 09:13:03 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Yl67IxDW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730609AbeK3ETF (ORCPT + 32 others); Thu, 29 Nov 2018 23:19:05 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:36529 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730555AbeK3ETE (ORCPT ); Thu, 29 Nov 2018 23:19:04 -0500 Received: by mail-wr1-f68.google.com with SMTP id u3so2674561wrs.3 for ; Thu, 29 Nov 2018 09:12:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kZ9qAKUFhDNWQgwFHfyYbRlwlOBWhnuPIzx1kDDdeg4=; b=Yl67IxDWCaxwjil2nX89/whyxy5/ukSWgHYZpMBQQIot+PIfkOxMwhGseVxhd3BBtB D9M2MIOs9cboB2w/sSofoQkiNk8ae1wJgldy7FL+mMsL8bpX/tsOnPr3k20axn4Y65Wf 5rJz8R7H0vFt9oIPhrZlTL6EWZd4wY30CdsLA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kZ9qAKUFhDNWQgwFHfyYbRlwlOBWhnuPIzx1kDDdeg4=; b=fb2z00OxfoW/OUSsV6biCZAxOa8B1rD9n3Fb77pTWydJO9VTT3PzafZRsvLsp5v+Ha UHFqJpU0y8Hr9iLhOxfErrecZciJNnJCY3C6d4HX6MU0+n/O6BkMV3It+DK7iJTd9OXT byGUsEKALYgy+uAMIa+SRFOOJaOcLXtX9Prdky5rkAzr2fxzjqyNx3v2hNB+MkzCbjHt pe4JMF+5EWkmq6cap442CMdO9q7V2q8jl3RJnDZQVv4MiNxA/m4E9guGJF2eCAbHQn4h x68GW/z0x9VG4TPtk6+paPDwGeefXCVD6exPuo1ypaRH5+Kw6ayW4uASzS5tqllplzO2 a+jA== X-Gm-Message-State: AA+aEWYwOQLyox87D/4npZw6pVMi7pxZIp7+oCuT5yJ7V2WPzogjwRMJ a/ou4fuNiKl7hN7TCQAP3jMMtw== X-Received: by 2002:adf:b102:: with SMTP id l2mr2059081wra.296.1543511577963; Thu, 29 Nov 2018 09:12:57 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:f070:d240:312e:9f99]) by smtp.gmail.com with ESMTPSA id y185sm1593882wmg.34.2018.11.29.09.12.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 09:12:57 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Andy Lutomirski , Arend van Spriel , Bhupesh Sharma , Borislav Petkov , Dave Hansen , Eric Snowberg , Hans de Goede , Joe Perches , Jon Hunter , Julien Thierry , Marc Zyngier , Nathan Chancellor , Peter Zijlstra , Sai Praneeth Prakhya , Sedat Dilek , YiFei Zhu Subject: [PATCH 06/11] x86/efi: Move efi__boot_services() to arch/x86 Date: Thu, 29 Nov 2018 18:12:25 +0100 Message-Id: <20181129171230.18699-7-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181129171230.18699-1-ard.biesheuvel@linaro.org> References: <20181129171230.18699-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sai Praneeth Prakhya efi__boot_services() are x86 specific quirks and as such should be in asm/efi.h, so move them from linux/efi.h. Also, call efi_free_boot_services() from __efi_enter_virtual_mode() as it is x86 specific call and ideally shouldn't be part of init/main.c Signed-off-by: Sai Praneeth Prakhya Cc: Borislav Petkov Cc: Ingo Molnar Cc: Andy Lutomirski Cc: Dave Hansen Cc: Bhupesh Sharma Cc: Peter Zijlstra Acked-by: Thomas Gleixner Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/efi.h | 2 ++ arch/x86/platform/efi/efi.c | 2 ++ include/linux/efi.h | 3 --- init/main.c | 4 ---- 4 files changed, 4 insertions(+), 7 deletions(-) -- 2.19.1 diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index eea40d52ca78..d1e64ac80b9c 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -141,6 +141,8 @@ extern int __init efi_reuse_config(u64 tables, int nr_tables); extern void efi_delete_dummy_variable(void); extern void efi_switch_mm(struct mm_struct *mm); extern void efi_recover_from_page_fault(unsigned long phys_addr); +extern void efi_free_boot_services(void); +extern void efi_reserve_boot_services(void); struct efi_setup_data { u64 fw_vendor; diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index 7ae939e353cd..e1cb01a22fa8 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -993,6 +993,8 @@ static void __init __efi_enter_virtual_mode(void) panic("EFI call to SetVirtualAddressMap() failed!"); } + efi_free_boot_services(); + /* * Now that EFI is in virtual mode, update the function * pointers in the runtime service table to the new virtual addresses. diff --git a/include/linux/efi.h b/include/linux/efi.h index 100ce4a4aff6..2b3b33c83b05 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -1000,13 +1000,11 @@ extern void efi_memmap_walk (efi_freemem_callback_t callback, void *arg); extern void efi_gettimeofday (struct timespec64 *ts); extern void efi_enter_virtual_mode (void); /* switch EFI to virtual mode, if possible */ #ifdef CONFIG_X86 -extern void efi_free_boot_services(void); extern efi_status_t efi_query_variable_store(u32 attributes, unsigned long size, bool nonblocking); extern void efi_find_mirror(void); #else -static inline void efi_free_boot_services(void) {} static inline efi_status_t efi_query_variable_store(u32 attributes, unsigned long size, @@ -1046,7 +1044,6 @@ extern void efi_mem_reserve(phys_addr_t addr, u64 size); extern int efi_mem_reserve_persistent(phys_addr_t addr, u64 size); extern void efi_initialize_iomem_resources(struct resource *code_resource, struct resource *data_resource, struct resource *bss_resource); -extern void efi_reserve_boot_services(void); extern int efi_get_fdt_params(struct efi_fdt_params *params); extern struct kobject *efi_kobj; diff --git a/init/main.c b/init/main.c index ee147103ba1b..ccefcd8e855f 100644 --- a/init/main.c +++ b/init/main.c @@ -737,10 +737,6 @@ asmlinkage __visible void __init start_kernel(void) arch_post_acpi_subsys_init(); sfi_init_late(); - if (efi_enabled(EFI_RUNTIME_SERVICES)) { - efi_free_boot_services(); - } - /* Do the rest non-__init'ed, we're now alive */ arch_call_rest_init(); } From patchwork Thu Nov 29 17:12:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 152420 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2634223ljp; Thu, 29 Nov 2018 09:13:06 -0800 (PST) X-Google-Smtp-Source: AFSGD/XTgvMtjM0b2kzUwqI7gQXmzwFtCwpxwtVkJpNBbtLTFF8pqeB0hKAu/jWYqt1pApnwcSfL X-Received: by 2002:a17:902:24e1:: with SMTP id l30-v6mr2173466plg.42.1543511586195; Thu, 29 Nov 2018 09:13:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543511586; cv=none; d=google.com; s=arc-20160816; b=BBVOGxUQ7gh1627VTNMah9o3VxCFKhHxnSI8Diq88Ot3Ce8OywpK5BzGWlG8aCpX95 VAiI/pW3K056EtAALIDtHqlKSfwPmB+UR79upntlP+bIyb7pAOcZK0yiOK/C18q6XzT6 2vJNGL9RctLEIjlEwMvcSUbsb1bCDgjD6CQwYSORUa+qFu2e2gqqBVXKU3MxZCzRkk1O VJCjb6qOyRbKXkAO/NUMUsIDfsuxF5c5CMhuArGAMyFFpsuu+e2gwqCwiMFuSz+gvNls 8FliuFPYvgy0B+M64cU5W7yz7pL0MYz18+LonE3WfbzMlbnv7yt2bpzv8S62SYe/BvAL 15SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=phTFw25GwhUds+/fULRXb1CISzvAjAuut2sdVRJvPds=; b=YAq/+2R1AVx1xf8KaZOM5XAwCxVPO2bs7rlQISAmGSgbb0xEz6rvF6vNTyA7dL7ErM nFB+E2gvNFCS7vktZjWLh+bn2QT3RXcrI3nPoWLn/ju0MMcwpyciQ6XoWSZ4OsCrnxGR MymzO6JTCEhBW1e63t+tQ7KUPF1W+VqXc4yjTdG94B9tJ0rd6eIYCQiuh0NdpKPuXZ9V hPX9ffvNfKzaY1uO1A8Rft7mpK4pe1Nfi779tzTRvb5O9xIB9SjXJ8+0WiGsJu5wRMse xFiKYc/KNHD1CDksanm+Wqv+ePaRZPN4JAEEqJTR7IUJKYbp/Pcx/Zu4tRKhh6MLXiDZ XdoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ggxv7iOy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t18si2217885pgu.476.2018.11.29.09.13.05; Thu, 29 Nov 2018 09:13:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ggxv7iOy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730633AbeK3ETI (ORCPT + 32 others); Thu, 29 Nov 2018 23:19:08 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:38130 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728551AbeK3ETG (ORCPT ); Thu, 29 Nov 2018 23:19:06 -0500 Received: by mail-wr1-f65.google.com with SMTP id v13so2679655wrw.5 for ; Thu, 29 Nov 2018 09:13:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=phTFw25GwhUds+/fULRXb1CISzvAjAuut2sdVRJvPds=; b=Ggxv7iOyEyttur6m//qGQl3LeVPtDPb7YfuDpyWPri8vWXX604IlM02Hee+r7CPhBI jGMR/mS2BcklYerUzbqqmCSzGv34txBlNdPTWStxuiPIfL0+E4yUEFlC3r9KeX2fOjRN XMaRKVRKQVJS5ud5sGHN4vvoFm/yym5ww4OnY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=phTFw25GwhUds+/fULRXb1CISzvAjAuut2sdVRJvPds=; b=ivbdiVd45amvArWsX5pT7x1EGCFsLE4HayaJBom37FgBD+or/Brb36DSSNgjX2rIsj DSlpCrZC2EnoiaXmNNWdMj6Zgy7WoP8HLzDwgHTnyxQDBy8N5Ydhsu9IFGtUdF8hF01V tbheiYllZzV9WUZ6cnNsA5aJvCj07LQK7ekORPImfBdoYqEuDL2nd+wx72huiXZsnaQ/ RKym+M91P5JhOsRVjBc4N1Rnc13yUKJ4mBVmvF/kzrhfqjGntf5oKm+00N23U0M6ey+t slJOCdHQbAwTUCHJRpMIPBqwr5LdHG4UJRN0tXRoIBVyanpuWVBsmv/b1fmwIuceexY3 xnYg== X-Gm-Message-State: AA+aEWbwazElv6R5vWlSSf1a60g+hXoq7MXR+9KhbfxJa6IfMo5Zs0B1 HJ3TZ99B3Hup7utiTU5omqPPih8Mq7I= X-Received: by 2002:a5d:4dc8:: with SMTP id f8mr2209772wru.45.1543511581073; Thu, 29 Nov 2018 09:13:01 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:f070:d240:312e:9f99]) by smtp.gmail.com with ESMTPSA id y185sm1593882wmg.34.2018.11.29.09.12.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 09:13:00 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Andy Lutomirski , Arend van Spriel , Bhupesh Sharma , Borislav Petkov , Dave Hansen , Eric Snowberg , Hans de Goede , Joe Perches , Jon Hunter , Julien Thierry , Marc Zyngier , Nathan Chancellor , Peter Zijlstra , Sai Praneeth Prakhya , Sedat Dilek , YiFei Zhu Subject: [PATCH 08/11] firmware: efi: add NULL pointer checks in efivars api functions Date: Thu, 29 Nov 2018 18:12:27 +0100 Message-Id: <20181129171230.18699-9-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181129171230.18699-1-ard.biesheuvel@linaro.org> References: <20181129171230.18699-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Arend van Spriel Since commit: ce2e6db554fa ("brcmfmac: Add support for getting nvram contents from EFI variables") we have a device driver accessing the efivars API. Several functions in the efivars API assume __efivars is set, i.e., that they will be accessed only after efivars_register() has been called. However, the following NULL pointer access was reported calling efivar_entry_size() from the brcmfmac device driver. Unable to handle kernel NULL pointer dereference at virtual address 00000008 pgd = 60bfa5f1 [00000008] *pgd=00000000 Internal error: Oops: 5 [#1] SMP ARM ... Hardware name: NVIDIA Tegra SoC (Flattened Device Tree) Workqueue: events request_firmware_work_func PC is at efivar_entry_size+0x28/0x90 LR is at brcmf_fw_complete_request+0x3f8/0x8d4 [brcmfmac] pc : [] lr : [] psr: a00d0113 sp : ede7fe28 ip : ee983410 fp : c1787f30 r10: 00000000 r9 : 00000000 r8 : bf2b2258 r7 : ee983000 r6 : c1604c48 r5 : ede7fe88 r4 : edf337c0 r3 : 00000000 r2 : 00000000 r1 : ede7fe88 r0 : c17712c8 Flags: NzCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment none Control: 10c5387d Table: ad16804a DAC: 00000051 Disassembly showed that the local static variable __efivars is NULL, which is not entirely unexpected given that it is a non-EFI platform. So add a NULL pointer check to efivar_entry_size(), and to related functions while at it. In efivars_register() a couple of sanity checks are added as well. Cc: Hans de Goede Reported-by: Jon Hunter Signed-off-by: Arend van Spriel Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/vars.c | 99 +++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 21 deletions(-) -- 2.19.1 diff --git a/drivers/firmware/efi/vars.c b/drivers/firmware/efi/vars.c index 9336ffdf6e2c..fceaafd67ec6 100644 --- a/drivers/firmware/efi/vars.c +++ b/drivers/firmware/efi/vars.c @@ -318,7 +318,12 @@ EXPORT_SYMBOL_GPL(efivar_variable_is_removable); static efi_status_t check_var_size(u32 attributes, unsigned long size) { - const struct efivar_operations *fops = __efivars->ops; + const struct efivar_operations *fops; + + if (!__efivars) + return EFI_UNSUPPORTED; + + fops = __efivars->ops; if (!fops->query_variable_store) return EFI_UNSUPPORTED; @@ -329,7 +334,12 @@ check_var_size(u32 attributes, unsigned long size) static efi_status_t check_var_size_nonblocking(u32 attributes, unsigned long size) { - const struct efivar_operations *fops = __efivars->ops; + const struct efivar_operations *fops; + + if (!__efivars) + return EFI_UNSUPPORTED; + + fops = __efivars->ops; if (!fops->query_variable_store) return EFI_UNSUPPORTED; @@ -429,13 +439,18 @@ static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid, int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *), void *data, bool duplicates, struct list_head *head) { - const struct efivar_operations *ops = __efivars->ops; + const struct efivar_operations *ops; unsigned long variable_name_size = 1024; efi_char16_t *variable_name; efi_status_t status; efi_guid_t vendor_guid; int err = 0; + if (!__efivars) + return -EFAULT; + + ops = __efivars->ops; + variable_name = kzalloc(variable_name_size, GFP_KERNEL); if (!variable_name) { printk(KERN_ERR "efivars: Memory allocation failed.\n"); @@ -583,12 +598,14 @@ static void efivar_entry_list_del_unlock(struct efivar_entry *entry) */ int __efivar_entry_delete(struct efivar_entry *entry) { - const struct efivar_operations *ops = __efivars->ops; efi_status_t status; - status = ops->set_variable(entry->var.VariableName, - &entry->var.VendorGuid, - 0, 0, NULL); + if (!__efivars) + return -EINVAL; + + status = __efivars->ops->set_variable(entry->var.VariableName, + &entry->var.VendorGuid, + 0, 0, NULL); return efi_status_to_err(status); } @@ -607,12 +624,17 @@ EXPORT_SYMBOL_GPL(__efivar_entry_delete); */ int efivar_entry_delete(struct efivar_entry *entry) { - const struct efivar_operations *ops = __efivars->ops; + const struct efivar_operations *ops; efi_status_t status; if (down_interruptible(&efivars_lock)) return -EINTR; + if (!__efivars) { + up(&efivars_lock); + return -EINVAL; + } + ops = __efivars->ops; status = ops->set_variable(entry->var.VariableName, &entry->var.VendorGuid, 0, 0, NULL); @@ -650,13 +672,19 @@ EXPORT_SYMBOL_GPL(efivar_entry_delete); int efivar_entry_set(struct efivar_entry *entry, u32 attributes, unsigned long size, void *data, struct list_head *head) { - const struct efivar_operations *ops = __efivars->ops; + const struct efivar_operations *ops; efi_status_t status; efi_char16_t *name = entry->var.VariableName; efi_guid_t vendor = entry->var.VendorGuid; if (down_interruptible(&efivars_lock)) return -EINTR; + + if (!__efivars) { + up(&efivars_lock); + return -EINVAL; + } + ops = __efivars->ops; if (head && efivar_entry_find(name, vendor, head, false)) { up(&efivars_lock); return -EEXIST; @@ -687,12 +715,17 @@ static int efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor, u32 attributes, unsigned long size, void *data) { - const struct efivar_operations *ops = __efivars->ops; + const struct efivar_operations *ops; efi_status_t status; if (down_trylock(&efivars_lock)) return -EBUSY; + if (!__efivars) { + up(&efivars_lock); + return -EINVAL; + } + status = check_var_size_nonblocking(attributes, size + ucs2_strsize(name, 1024)); if (status != EFI_SUCCESS) { @@ -700,6 +733,7 @@ efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor, return -ENOSPC; } + ops = __efivars->ops; status = ops->set_variable_nonblocking(name, &vendor, attributes, size, data); @@ -727,9 +761,13 @@ efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor, int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes, bool block, unsigned long size, void *data) { - const struct efivar_operations *ops = __efivars->ops; + const struct efivar_operations *ops; efi_status_t status; + if (!__efivars) + return -EINVAL; + + ops = __efivars->ops; if (!ops->query_variable_store) return -ENOSYS; @@ -829,13 +867,18 @@ EXPORT_SYMBOL_GPL(efivar_entry_find); */ int efivar_entry_size(struct efivar_entry *entry, unsigned long *size) { - const struct efivar_operations *ops = __efivars->ops; + const struct efivar_operations *ops; efi_status_t status; *size = 0; if (down_interruptible(&efivars_lock)) return -EINTR; + if (!__efivars) { + up(&efivars_lock); + return -EINVAL; + } + ops = __efivars->ops; status = ops->get_variable(entry->var.VariableName, &entry->var.VendorGuid, NULL, size, NULL); up(&efivars_lock); @@ -861,12 +904,14 @@ EXPORT_SYMBOL_GPL(efivar_entry_size); int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes, unsigned long *size, void *data) { - const struct efivar_operations *ops = __efivars->ops; efi_status_t status; - status = ops->get_variable(entry->var.VariableName, - &entry->var.VendorGuid, - attributes, size, data); + if (!__efivars) + return -EINVAL; + + status = __efivars->ops->get_variable(entry->var.VariableName, + &entry->var.VendorGuid, + attributes, size, data); return efi_status_to_err(status); } @@ -882,14 +927,19 @@ EXPORT_SYMBOL_GPL(__efivar_entry_get); int efivar_entry_get(struct efivar_entry *entry, u32 *attributes, unsigned long *size, void *data) { - const struct efivar_operations *ops = __efivars->ops; efi_status_t status; if (down_interruptible(&efivars_lock)) return -EINTR; - status = ops->get_variable(entry->var.VariableName, - &entry->var.VendorGuid, - attributes, size, data); + + if (!__efivars) { + up(&efivars_lock); + return -EINVAL; + } + + status = __efivars->ops->get_variable(entry->var.VariableName, + &entry->var.VendorGuid, + attributes, size, data); up(&efivars_lock); return efi_status_to_err(status); @@ -921,7 +971,7 @@ EXPORT_SYMBOL_GPL(efivar_entry_get); int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, unsigned long *size, void *data, bool *set) { - const struct efivar_operations *ops = __efivars->ops; + const struct efivar_operations *ops; efi_char16_t *name = entry->var.VariableName; efi_guid_t *vendor = &entry->var.VendorGuid; efi_status_t status; @@ -940,6 +990,11 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, if (down_interruptible(&efivars_lock)) return -EINTR; + if (!__efivars) { + err = -EINVAL; + goto out; + } + /* * Ensure that the available space hasn't shrunk below the safe level */ @@ -956,6 +1011,8 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, } } + ops = __efivars->ops; + status = ops->set_variable(name, vendor, attributes, *size, data); if (status != EFI_SUCCESS) { err = efi_status_to_err(status); From patchwork Thu Nov 29 17:12:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 152422 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2634691ljp; Thu, 29 Nov 2018 09:13:28 -0800 (PST) X-Google-Smtp-Source: AFSGD/UAB0NpzdXCQo+Ii9b+89mBWv76rPfwAu9e8gU9CiN5stp6Vg9e04qoK81y+lgsNA5Bz9hU X-Received: by 2002:a17:902:b093:: with SMTP id p19mr2250107plr.135.1543511608701; Thu, 29 Nov 2018 09:13:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543511608; cv=none; d=google.com; s=arc-20160816; b=G6ERz5XN7V5N47/ei3DIyH3hAoDGQbB4ThTguxafLWDUJmIkUzvkd9GTNbQcALj/cV bQr/9JVfFoU/m7m5GMmfFfpCjfgmZ2BLRwgjCMPr52W3wNrjTe1Gag9PualenciBBXgt 8rmIAO7hwS5ofH3tVoHx3ey+1m3d3lZL3RLpnXSwk2ArM2n1nRXs/637rgTdbzwgsAmA Mn2ZXZJ3lOc8TTk8Z6xwtRZ2EA6K21pjMYjwkjoUICmZuCZYzJSDInvgxmq8y7nbzTJO DPt86HGo3+WLsgc/XL3wn1zHBxA3nlwWHILEzzuDQQyQ0dL1I3ExYQNB583BUH2cVSNR KlOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Vz5y5QfDkngla5yyvKovxaI5hAxDbbXuYQKEXtGsLK0=; b=yJZAUanvKHzjESlPUyisxsEe5XwZ94s27mXT+OBQD2Z0FXgdbkbcCWDc/y1Zx7/R0h 0EAOZC00FLl0lqrxXLBWwYHjZOHelWcX+BG24XSY4QIamq0pyF1s2g0IYRA0cxBJvJ9o 6RO7+zKadlU/3GUvhFFVRZOpe4KHYETDJJVZG4cwc5XvAKVEsEr+5Gd0+eakWwElmtPE xLFOm8BhegBOSEgL4jyj+SwLDYh0kIZIY2CxAUeDq4qeurkwLSDaia4VxtS9iSQwmEzX TOO09ilhdDMjcusOVCAT39Dgyj/SllUv56Gci909Imto1IQu4T4G4TNCZyT4gakdOXqY 7Ksg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=A2eS3OBs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v35si2495315pgl.130.2018.11.29.09.13.28; Thu, 29 Nov 2018 09:13:28 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=A2eS3OBs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730651AbeK3ETK (ORCPT + 32 others); Thu, 29 Nov 2018 23:19:10 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:39723 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730577AbeK3ETJ (ORCPT ); Thu, 29 Nov 2018 23:19:09 -0500 Received: by mail-wr1-f67.google.com with SMTP id t27so2682424wra.6 for ; Thu, 29 Nov 2018 09:13:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Vz5y5QfDkngla5yyvKovxaI5hAxDbbXuYQKEXtGsLK0=; b=A2eS3OBsFIVDm3SbMQ7DUpIRVJaS4Tf4DsriQrtmhjV/xWOrFJrAGLsgYBTc5/fFlA OpVuFxOZ/rcVN87UaesapdcPSRxKvEXGo3RvgB8nKzD/b1pspyeh3U+iCODuXeRt3xk5 MZCuXvCIBlyLxDmtKe+GunAIP/SJBTBSiVHpg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Vz5y5QfDkngla5yyvKovxaI5hAxDbbXuYQKEXtGsLK0=; b=NZ4L9Wsw1zzJ/S6hOftba8aBSrL9z8hTy1B5EkUxLwjpO6Dt0Ggq2YgknS8fMUaMG7 uK5XOH7fuI+oOKAJlUxzpBsIf6nXni5pKuMbTSOcZGgIXyQEGn1UjMrNL285b/GcESt2 Iqe/fdj2W8s0ydEHBbQaqSBQlsYjeA71OoxMwBKzJcU012ZJBaV6U4gzxqzV5UCzzs/e Gr4/h5OLJhM4RDRgwNinicNrBHm408iLWfy4mJA9BniDE1t44WKtq4T80zpIq9meeCue qbasTHSx/9NP22Myw4ukHVsx0iXigO3yOKJlAoXPKAtPGkPyUl+kYAY2+fuUM4lfGuAP 1TvA== X-Gm-Message-State: AA+aEWaULf2RzkrE2pyKvkDOJyd8XEqLhZUCyovv/Qi1RujmYrfDWTFm mSlua7ooIjpizRo5o8hcs8OtRg== X-Received: by 2002:a5d:6105:: with SMTP id v5mr2115260wrt.63.1543511582682; Thu, 29 Nov 2018 09:13:02 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:f070:d240:312e:9f99]) by smtp.gmail.com with ESMTPSA id y185sm1593882wmg.34.2018.11.29.09.13.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 09:13:01 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Andy Lutomirski , Arend van Spriel , Bhupesh Sharma , Borislav Petkov , Dave Hansen , Eric Snowberg , Hans de Goede , Joe Perches , Jon Hunter , Julien Thierry , Marc Zyngier , Nathan Chancellor , Peter Zijlstra , Sai Praneeth Prakhya , Sedat Dilek , YiFei Zhu Subject: [PATCH 09/11] efi: permit multiple entries in persistent memreserve data structure Date: Thu, 29 Nov 2018 18:12:28 +0100 Message-Id: <20181129171230.18699-10-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181129171230.18699-1-ard.biesheuvel@linaro.org> References: <20181129171230.18699-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In preparation of updating efi_mem_reserve_persistent() to cause less fragmentation when dealing with many persistent reservations, update the struct definition and the code that handles it currently so it can describe an arbitrary number of reservations using a single linked list entry. The actual optimization will be implemented in a subsequent patch. Tested-by: Marc Zyngier Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/efi.c | 39 +++++++++++++++++-------- drivers/firmware/efi/libstub/arm-stub.c | 2 +- include/linux/efi.h | 13 +++++++-- 3 files changed, 38 insertions(+), 16 deletions(-) -- 2.19.1 diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index 415849bab233..80b11521627a 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -602,21 +602,33 @@ int __init efi_apply_persistent_mem_reservations(void) while (prsv) { struct linux_efi_memreserve *rsv; - - /* reserve the entry itself */ - memblock_reserve(prsv, sizeof(*rsv)); - - rsv = early_memremap(prsv, sizeof(*rsv)); - if (rsv == NULL) { + u8 *p; + int i; + + /* + * Just map a full page: that is what we will get + * anyway, and it permits us to map the entire entry + * before knowing its size. + */ + p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE), + PAGE_SIZE); + if (p == NULL) { pr_err("Could not map UEFI memreserve entry!\n"); return -ENOMEM; } - if (rsv->size) - memblock_reserve(rsv->base, rsv->size); + rsv = (void *)(p + prsv % PAGE_SIZE); + + /* reserve the entry itself */ + memblock_reserve(prsv, EFI_MEMRESERVE_SIZE(rsv->size)); + + for (i = 0; i < atomic_read(&rsv->count); i++) { + memblock_reserve(rsv->entry[i].base, + rsv->entry[i].size); + } prsv = rsv->next; - early_memunmap(rsv, sizeof(*rsv)); + early_memunmap(p, PAGE_SIZE); } } @@ -985,6 +997,7 @@ static int __init efi_memreserve_map_root(void) int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size) { struct linux_efi_memreserve *rsv; + int rsvsize = EFI_MEMRESERVE_SIZE(1); int rc; if (efi_memreserve_root == (void *)ULONG_MAX) @@ -996,12 +1009,14 @@ int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size) return rc; } - rsv = kmalloc(sizeof(*rsv), GFP_ATOMIC); + rsv = kmalloc(rsvsize, GFP_ATOMIC); if (!rsv) return -ENOMEM; - rsv->base = addr; - rsv->size = size; + rsv->size = 1; + atomic_set(&rsv->count, 1); + rsv->entry[0].base = addr; + rsv->entry[0].size = size; spin_lock(&efi_mem_reserve_persistent_lock); rsv->next = efi_memreserve_root->next; diff --git a/drivers/firmware/efi/libstub/arm-stub.c b/drivers/firmware/efi/libstub/arm-stub.c index 3d36142cf812..9e20159ea5f5 100644 --- a/drivers/firmware/efi/libstub/arm-stub.c +++ b/drivers/firmware/efi/libstub/arm-stub.c @@ -86,8 +86,8 @@ void install_memreserve_table(efi_system_table_t *sys_table_arg) } rsv->next = 0; - rsv->base = 0; rsv->size = 0; + atomic_set(&rsv->count, 0); status = efi_call_early(install_configuration_table, &memreserve_table_guid, diff --git a/include/linux/efi.h b/include/linux/efi.h index 2b3b33c83b05..4f27640fdcdc 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -1712,9 +1712,16 @@ extern struct efi_runtime_work efi_rts_work; extern struct workqueue_struct *efi_rts_wq; struct linux_efi_memreserve { - phys_addr_t next; - phys_addr_t base; - phys_addr_t size; + int size; // allocated size of the array + atomic_t count; // number of entries used + phys_addr_t next; // pa of next struct instance + struct { + phys_addr_t base; + phys_addr_t size; + } entry[0]; }; +#define EFI_MEMRESERVE_SIZE(count) (sizeof(struct linux_efi_memreserve) + \ + (count) * sizeof(((struct linux_efi_memreserve *)0)->entry[0])) + #endif /* _LINUX_EFI_H */ From patchwork Thu Nov 29 17:12:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 152423 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2634718ljp; Thu, 29 Nov 2018 09:13:30 -0800 (PST) X-Google-Smtp-Source: AFSGD/UAybau6Rs6CJaHgj1XCJVbtMI5eIgygCP9XR3DaavOMTODFA4jl790gfWCHVcfPLh3vnoi X-Received: by 2002:a17:902:8f83:: with SMTP id z3mr2207807plo.328.1543511609979; Thu, 29 Nov 2018 09:13:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543511609; cv=none; d=google.com; s=arc-20160816; b=A3Z7N470SWmJQO8YPvHFML6lNr4fTbHS4reAOwh5QHXN9cfOl/SJIF+eCvQ+7avDUc hbeoRhWaPLHnbEv3iDeiZR9Si0mzexDJl/EPili/orFHJ8TjDGNFgXX6+6rCFGa3Ease 0k8Cq0eh6cTIy9Qda1HrZZc45YZLzf1UPreKtBKleQ+eBMb+ETgpxf0xYIgDaqxQV347 7ZIiOQzt2FbEnTL7v/QtdFL/QUUAP2VU0xrCLA2vq0MYSVGK/dDQ2qioR+1B49tq5Wn2 OlRBlRwzV1djo9G5vD+3SYvwRrD+Mazw+HdRMZnsjy17QNqSI94C3T+j+zRFIeid60zz rKCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Zf26VLC73KBeOMRN+TciYckaN/0BTjlAJnyTuYhXxyU=; b=D/fi6ORTEp7hjp1VJmT7n4Fu//kPNtW0Sw53JGs1KrhBVdvYo+FeHFXMlfXc3wqf2d cipkxXXWBRHMdfNsHjYcxxwH6O39zKVn2eKmupJltkgfbOrOAeqkwtROReoqk+DayRS8 h5/1Nk2X5CLvchTfpzdfDJ+j+y0Vaof7wtvju0c6kdf47py+DXyK35KEoBjdvlfDbOfs wPD4s01FVfvvyx4nZvU68ac0RgfxQiy26U/joMU6TpdKEBkefqdMhHjY6LNz4XPMmvj3 MLIw4qsYO5DPZiKsGozI2BTi0Ep5vxVwdXuUauLowTu/nxpwXqlNs3NaUTGWEV3t9P7i K7Bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=anRe2fKf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w5si2660107pfl.279.2018.11.29.09.13.29; Thu, 29 Nov 2018 09:13:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=anRe2fKf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730729AbeK3ETc (ORCPT + 32 others); Thu, 29 Nov 2018 23:19:32 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:40473 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730634AbeK3ETK (ORCPT ); Thu, 29 Nov 2018 23:19:10 -0500 Received: by mail-wr1-f66.google.com with SMTP id p4so2674623wrt.7 for ; Thu, 29 Nov 2018 09:13:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Zf26VLC73KBeOMRN+TciYckaN/0BTjlAJnyTuYhXxyU=; b=anRe2fKfurgWjUShMpRaDkx3BLJlCKBVQ1II1d9aRyRnpxxlaZrmNwEvXXKWN8qFKR iEnqvs3yVLKmUanB444pieAbiBOvgRAszRNoulw96X2WVA8ghoeyTnNpUIAw5u+8LwTH +ULYQrOvH4vX2YYWHnLcoW3ddR8/2hwnWbmBg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Zf26VLC73KBeOMRN+TciYckaN/0BTjlAJnyTuYhXxyU=; b=Vop0UNB+CvDKk0hWuSFzBUvSGYnbDAfXFCGNlcK4jN9GeM+GJX8AJkCjSzAPMO21t8 h282Fd4Zp2dPGISDQIu4oDPMrm8C34H7KLKWZKiyq31PVyB4FARuWx/x3AkZoYIThrA+ mSwf0z8ggNjvnm9QwxInW9/XAQgXtI1pzg33IFazNo6kqZdO6i/+TnVvn263/2FfI/Lu +bz2zwvqVFgtcuIPEEuGILO2hXTIdJBX2bFkVxtpMpfdR/ENhBygPcb90hVM0toA6wwD /lgO36AddsS0jM5QShggFSfHT/PB3UZXx5upnCB3s3C5xBQVWHzZ0865Lhqmc0IbZP0e JLBA== X-Gm-Message-State: AA+aEWbs591ZIia8a+dOJQYPcJAG8iaRDz3Fx3+lrSP+XFfrjpd6C3BY hW395rv+XOfND00kdfkdUldIvg== X-Received: by 2002:adf:9361:: with SMTP id 88mr2080952wro.204.1543511584259; Thu, 29 Nov 2018 09:13:04 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:f070:d240:312e:9f99]) by smtp.gmail.com with ESMTPSA id y185sm1593882wmg.34.2018.11.29.09.13.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 09:13:03 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Andy Lutomirski , Arend van Spriel , Bhupesh Sharma , Borislav Petkov , Dave Hansen , Eric Snowberg , Hans de Goede , Joe Perches , Jon Hunter , Julien Thierry , Marc Zyngier , Nathan Chancellor , Peter Zijlstra , Sai Praneeth Prakhya , Sedat Dilek , YiFei Zhu Subject: [PATCH 10/11] efi: reduce the amount of memblock reservations for persistent allocations Date: Thu, 29 Nov 2018 18:12:29 +0100 Message-Id: <20181129171230.18699-11-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181129171230.18699-1-ard.biesheuvel@linaro.org> References: <20181129171230.18699-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The current implementation of efi_mem_reserve_persistent() is rather naive, in the sense that for each invocation, it creates a separate linked list entry to describe the reservation. Since the linked list entries themselves need to persist across subsequent kexec reboots, every reservation created this way results in two memblock_reserve() calls at the next boot. On arm64 systems with 100s of CPUs, this may result in a excessive number of memblock reservations, and needless fragmentation. So instead, make use of the newly updated struct linux_efi_memreserve layout to put multiple reservations into a single linked list entry. This should get rid of the numerous tiny memblock reservations, and effectively cut the total number of reservations in half on arm64 systems with many CPUs. Tested-by: Marc Zyngier Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/efi.c | 20 +++++++++++++++++--- include/linux/efi.h | 3 +++ 2 files changed, 20 insertions(+), 3 deletions(-) -- 2.19.1 diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index 80b11521627a..e90bc32c2670 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -998,7 +998,8 @@ int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size) { struct linux_efi_memreserve *rsv; int rsvsize = EFI_MEMRESERVE_SIZE(1); - int rc; + unsigned long prsv; + int rc, index; if (efi_memreserve_root == (void *)ULONG_MAX) return -ENODEV; @@ -1009,11 +1010,24 @@ int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size) return rc; } - rsv = kmalloc(rsvsize, GFP_ATOMIC); + /* first try to find a slot in an existing linked list entry */ + for (prsv = efi_memreserve_root->next; prsv; prsv = rsv->next) { + rsv = __va(prsv); + index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size); + if (index < rsv->size) { + rsv->entry[index].base = addr; + rsv->entry[index].size = size; + + return 0; + } + } + + /* no slot found - allocate a new linked list entry */ + rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC); if (!rsv) return -ENOMEM; - rsv->size = 1; + rsv->size = EFI_MEMRESERVE_COUNT(PAGE_SIZE); atomic_set(&rsv->count, 1); rsv->entry[0].base = addr; rsv->entry[0].size = size; diff --git a/include/linux/efi.h b/include/linux/efi.h index 4f27640fdcdc..becd5d76a207 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -1724,4 +1724,7 @@ struct linux_efi_memreserve { #define EFI_MEMRESERVE_SIZE(count) (sizeof(struct linux_efi_memreserve) + \ (count) * sizeof(((struct linux_efi_memreserve *)0)->entry[0])) +#define EFI_MEMRESERVE_COUNT(size) (((size) - sizeof(struct linux_efi_memreserve)) \ + / sizeof(((struct linux_efi_memreserve *)0)->entry[0])) + #endif /* _LINUX_EFI_H */ From patchwork Thu Nov 29 17:12:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 152421 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2634318ljp; Thu, 29 Nov 2018 09:13:10 -0800 (PST) X-Google-Smtp-Source: AFSGD/UJH+A8H33ta0+tqcYMPG2P6f/5Q0nzsxIVzm+MW8wfHx9pJWPiSP+Wr758qGT15QGaJ7A5 X-Received: by 2002:a62:6ec8:: with SMTP id j191mr2186355pfc.198.1543511590392; Thu, 29 Nov 2018 09:13:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543511590; cv=none; d=google.com; s=arc-20160816; b=cpPf1wk+NDue0RPj3h86anE+YY5hBkgurjuLtURhFFnhKV4RNFMeDzMwjYchU8Pd5n Xy87uI++L055SLmcbjB4ZQy07KmYl8+TB1c0P133f1sMKWhpQ7fM18V5g/jBcfnvc8au PaBcJQSRMoi2rrkrdgChomgnA9I1wRJgOtfW/VYIShflLMRpDx4F7Doj9bOz/wbm60m/ hi4LYiG6SMyhD5ziZ3XPXrHzcN0o/mvwOiDaMnZ/IhapJK2vVXpBVBhf5jVqxbKsPshl JYWLL4oMsmc/COXqNarfiKIg73AqyuYnokx0njY0WltdsiQo79Ipwm/IUBtMsS/tbCVE 6fYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=B3Orb74WwT6irLdvQjoh7i+GpPzyw+FEcB0oGPiZXlw=; b=lvBuFzO+wSY9m5rwNcDmIpLd5bQyli57+3uPHWeLPy00x4Tpbi3buzVHgrbiex53pH mCdh6ZudNAdPuLOdeqlZzn4Hh6NUdT2prNWgkcBSAhq66oQD6JsPAd/8AoSN6Ov7NX68 lbNKddWJrMO1qfsu6FKfQoqCHK7axxIe+sxkdGC/JwzmO7L8GsowHxyw2+eGx6PyGIin TbTBXPjIvh88Q3Z4fIS+ZP+I4+6O8V8JEj46WsiUGOgrtFsch1qw8Ae0HA4GdahBcJMG PAqnWcPagbDsymsI/JTGkulk/bbMed0mVAdcoy3SoPemAbyq+UtIcdRF/H4bbxcMux5s GKvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=O+euQ7cO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n8si2538523plp.137.2018.11.29.09.13.10; Thu, 29 Nov 2018 09:13:10 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=O+euQ7cO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730687AbeK3ETM (ORCPT + 32 others); Thu, 29 Nov 2018 23:19:12 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:41339 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728551AbeK3ETL (ORCPT ); Thu, 29 Nov 2018 23:19:11 -0500 Received: by mail-wr1-f66.google.com with SMTP id x10so2665592wrs.8 for ; Thu, 29 Nov 2018 09:13:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=B3Orb74WwT6irLdvQjoh7i+GpPzyw+FEcB0oGPiZXlw=; b=O+euQ7cOPUSvTs0X63EO/sOSn7bDdrMGZJ5nE1modkea/IlEoTYp6VkHHZtZa/61aA B+VBxGQZ6y/i+q9i6B3V5aJqNEin4OkrJAnfp7SShnyPgSmw4KBttr4E/6qg8f0uY/Kq mPFOiayoNqoeagrCpxVdR1sjiUWaJgacNx0mA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=B3Orb74WwT6irLdvQjoh7i+GpPzyw+FEcB0oGPiZXlw=; b=Aq25pkAYql6XfUaboU8DrDEv1dVlzBZ6Fo1inTYHiAJB7BkLNTz8kaaOheyTMtpEFR uyZ8/KRS8mI7Dv56Tc0k1qCptdRiqS3xLN9Y4ho8fzGu1Z6lNoln0pQBCCACk0s3d2zU pFZdpIqSJERcIC7bU8tttHUhqq0qQ975of9YNPKSSwLzr0efFVb63Etzm6eUJXMV5gs/ n5R8EZVUKbEU4g7b8OVt/Ba80AZ8cqE2cApcGHFDu+eYwZNSI2ITgL6BhZ4Y+LU6hTrK 6YRaqYBSrU/xjkH93fOBUAUXuKaelDj0HkIKLSslAL6yFhqoMhOcxRFGYP0OLZfBVrEC KBWg== X-Gm-Message-State: AA+aEWbFElPFttcdDKYKvVnfBjsZbIZa+Kr98YutbygD5BmbdWP3ontO MS4ygdA9Vkff+OwctXTM6lLKgg== X-Received: by 2002:adf:8228:: with SMTP id 37mr2092166wrb.160.1543511585964; Thu, 29 Nov 2018 09:13:05 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:f070:d240:312e:9f99]) by smtp.gmail.com with ESMTPSA id y185sm1593882wmg.34.2018.11.29.09.13.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 09:13:05 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Andy Lutomirski , Arend van Spriel , Bhupesh Sharma , Borislav Petkov , Dave Hansen , Eric Snowberg , Hans de Goede , Joe Perches , Jon Hunter , Julien Thierry , Marc Zyngier , Nathan Chancellor , Peter Zijlstra , Sai Praneeth Prakhya , Sedat Dilek , YiFei Zhu Subject: [PATCH 11/11] efi/x86: earlyprintk - Fix infinite loop on some screen widths Date: Thu, 29 Nov 2018 18:12:30 +0100 Message-Id: <20181129171230.18699-12-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181129171230.18699-1-ard.biesheuvel@linaro.org> References: <20181129171230.18699-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: YiFei Zhu An affected screen resolution is 1366 x 768, which width is not divisible by 8, the default font width. On such screens, when longer lines are earlyprintk'ed, overflow-to-next-line can never trigger, due to the left-most x-coordinate of the next character always less than the screen width. Earlyprintk will infinite loop in trying to print the rest of the string but unable to, due to the line being full. This patch makes the trigger consider the right-most x-coordinate, instead of left-most, as the value to compare against the screen width threshold. Signed-off-by: YiFei Zhu Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/early_printk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- 2.19.1 diff --git a/arch/x86/platform/efi/early_printk.c b/arch/x86/platform/efi/early_printk.c index 7476b3b097e1..7138bc7a265c 100644 --- a/arch/x86/platform/efi/early_printk.c +++ b/arch/x86/platform/efi/early_printk.c @@ -183,7 +183,7 @@ early_efi_write(struct console *con, const char *str, unsigned int num) num--; } - if (efi_x >= si->lfb_width) { + if (efi_x + font->width > si->lfb_width) { efi_x = 0; efi_y += font->height; }