From patchwork Tue Nov 6 11:37:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 150258 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp3775375ljp; Tue, 6 Nov 2018 03:37:48 -0800 (PST) X-Google-Smtp-Source: AJdET5czfJ4eHhGcA0eebDGpt2/bnGqI+Y9dOap+5PxBPy6F8vC5vrTNXcEUJchwz+EwJUkKijEs X-Received: by 2002:a17:902:8c86:: with SMTP id t6-v6mr26352334plo.55.1541504268250; Tue, 06 Nov 2018 03:37:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541504268; cv=none; d=google.com; s=arc-20160816; b=D8pVl1crHmQXmiVYaXRHXrb1clkODDiTVSWYZruQaA8yeyIMvhO2V6hLc25X359SA/ L+gl6nZCAj9ZSvbiIqBW8yd12hiWsXDh9ZIcdeub9U7zWPfHbtaKoQHBN5bMf5TQ4Kd7 qVjqAGaCt3oPWEkMb74j2J440vYBk5WZCdVmMhgPjmwBcGYMXMwxqixXG8SnuUX9CAeU ZdSqKwzkP2KrkopYvyBIiONHnDZGxfix0iOxNIjxIsc/lC9czXHXcMWg4aeTyQrlYmbo LvyKR9sr8P/UtPTuEP4VzvBk0gKNk5WHE0drxIKE87DdR8j3x8Ka27pgYOG1x9474g7B 0dSQ== 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=+krWoVZRI0iFhA51rBwg9mzqq8rHQsi9J/F6ndnPhTE=; b=i1vYH848/rqJ34a5RSC9fUgjuB8zfAkKjZGGI6TXa/SGo21LcIkpaAyPd8g34H8Sdb RmsY4ddS/zCwC83LqXpu0aMkyNGBuaeHJWaLXSKyUpxgyD7omeOccsnom+kquaeWC/NC g5vQfFNd70Tw6GO04aZMspSVMGQsC3se5z42v3GJK0nhgmWpRfod/G7owYm3SAGESw+w 5OG1sluXBXd7OQ1ZUU0LsHdKWsMWZMuQ2ENYEnN29SLqndkY4q76rymu+Ry6h+X6EnCH 5KVJwITm+wC2iaGOYNE99ce1RsLKZ3gntHbPj2hVkVLbwO5vwnxntGfD6EcegzQuPYkb Q7lQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LY32axis; spf=pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-efi-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 q10-v6si44073084pls.344.2018.11.06.03.37.48; Tue, 06 Nov 2018 03:37:48 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-efi-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=LY32axis; spf=pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-efi-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 S1729728AbeKFVCe (ORCPT + 3 others); Tue, 6 Nov 2018 16:02:34 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:51478 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730358AbeKFVCe (ORCPT ); Tue, 6 Nov 2018 16:02:34 -0500 Received: by mail-wm1-f66.google.com with SMTP id w7-v6so11062887wmc.1 for ; Tue, 06 Nov 2018 03:37:46 -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=+krWoVZRI0iFhA51rBwg9mzqq8rHQsi9J/F6ndnPhTE=; b=LY32axisnVrJ52ft8GtQ1HXEOK3HG5+SaYH34pZO7VoGsrPQWsn+L5GYi+7q4U7biC PwLkfQDamH54JYt19X6KAhkOR5/ddpkLgy3Dz3uBf7SRzdjxGr8omludHJaYHN33zN59 YEeZiCBNOSIIJ0a08OyVMs8dOwl9kdKjSpSDE= 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=+krWoVZRI0iFhA51rBwg9mzqq8rHQsi9J/F6ndnPhTE=; b=N14igJO7nwdBSBOj0xvidfKkrlQA5zEhcf77+u1XOvSSZa90xptQnybYxKDKyt6WL9 IOHL2IZIgm6FQ9wbYS9vRWbPUHM65d6+y7gKYm0JlA4MaQyN21F8A5tG5C4QVLdelV2n 2I66m/YXSLUzB+XAzIGeyKf0WjZHRbpet7GKgFWHYKtgde2AG0ZSxmy3yCvAsXg+2SP0 8UUgB8OOx0vS1U0giOpXpXv0TdxpqYJvYJoUn4kPRoJ3p88iOkls12cUZqmcyHDgM3We KNdPu2O6iTgQVFLNc+mISrVg0cSmROGlVoEFJY0FJLI7G3ZZRNwrNPIcYhFA+USJ4A6A sWDQ== X-Gm-Message-State: AGRZ1gLac22jwavwOFyq9RIPVnPR73fii/Qn36TbjS1npMw5fDaotVjU jOxGasM5bSIb3DxG/pFI76xIUQ== X-Received: by 2002:a1c:8a11:: with SMTP id m17-v6mr1635656wmd.15.1541504265543; Tue, 06 Nov 2018 03:37:45 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:3117:2b29:5ef7:a9e5]) by smtp.gmail.com with ESMTPSA id t187-v6sm1431529wmt.45.2018.11.06.03.37.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Nov 2018 03:37:44 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: bhsharma@redhat.com, linux-efi@vger.kernel.org, will.deacon@arm.com, mark.rutland@arm.com, marc.zyngier@arm.com, Ard Biesheuvel Subject: [PATCH 4/4] efi: reduce the amount of memblock reservations for persistent allocations Date: Tue, 6 Nov 2018 12:37:32 +0100 Message-Id: <20181106113732.16351-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181106113732.16351-1-ard.biesheuvel@linaro.org> References: <20181106113732.16351-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@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. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/efi.c | 29 ++++++++++++++------ include/linux/efi.h | 3 ++ 2 files changed, 24 insertions(+), 8 deletions(-) -- 2.19.1 diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index 85d2ec532816..2ca5a59f7568 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -978,22 +978,35 @@ static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock); int efi_mem_reserve_persistent(phys_addr_t addr, u64 size) { struct linux_efi_memreserve *rsv, *parent; - int rsvsize = EFI_MEMRESERVE_SIZE(1); + unsigned long prsv; + int index; if (efi.mem_reserve == EFI_INVALID_TABLE_ADDR) return -ENODEV; - rsv = kmalloc(rsvsize, GFP_KERNEL); - if (!rsv) - return -ENOMEM; - parent = memremap(efi.mem_reserve, sizeof(*rsv), MEMREMAP_WB); - if (!parent) { - kfree(rsv); + if (!parent) return -ENOMEM; + + /* first try to find a slot in an existing linked list entry */ + for (prsv = parent->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; + + memunmap(parent); + return 0; + } } - rsv->size = 1; + /* no slot found - allocate a new linked list entry */ + rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_KERNEL); + if (!rsv) + return -ENOMEM; + + 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 dfce82b2ca8a..1a1a081f7244 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -1727,4 +1727,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 */