From patchwork Wed Nov 7 14:16:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 150396 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp5221122ljp; Wed, 7 Nov 2018 06:16:34 -0800 (PST) X-Google-Smtp-Source: AJdET5e32i9QcZFTvw3bY+oKKA38P6ESL187H3CAhkEKpFYrYE4EVA6t+ktgHWtUY4XfzPR89O3L X-Received: by 2002:a63:441e:: with SMTP id r30mr343765pga.128.1541600194508; Wed, 07 Nov 2018 06:16:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541600194; cv=none; d=google.com; s=arc-20160816; b=LVqjzb9kG+IzWDFbwf7SzV3empprIk97qYhw5K9xkkW3booLpuEs82Op4urrUCzLxN yBVxzkq0JAy3mTHaFMuW1Tw1wBfY0eUzgA3AgQyQm/1XA1WDMSEZHc+hVeasp8huV9Im Gfr3mBUlzRr3nc8cjEHkycFkUm/OxVsDcUiVglu4g5oWuuOX5lGJFSUeJrS+MHETgA7v m6pVWjLOEDACI4Zs4g9pEbvWYZTG2Nc1+R14VAJIvLaly6vY8MWsxCt4Jwuh8AruYxoW Qm7XuV8iy7c/VXyQbHYKJ9MCQpTZxhPxNsiyzDWmO5bMEB9uTyZjXEusdOi1vZnMuHU2 cBzQ== 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=VJyGFvkW+sds6Swb6NM29U54YL8cp7noX7x5ydcenvM=; b=WVVRX/53ealKoldeWfMEQviEofPhfFqtFsfC+Pa0uNjPFFSa70QWbCe8097cniOe9b h2pQZ1oRUYreq3D9Fbyn/Pqr6vnxHhAB1UIiaAsssjpOjLX2C3RIvJqXO4+JrZW8I7o9 n2zfQt3aYVHmrH2gr9tad1oFix8C2pkvlaycmo32+IRLjEeOESnxDeth8vgmZQCsVjyf qnTkkWEl4fHjBKzXLooKdw3GBOtFwGUlKMQ4jmxGFB+8cVjMUxJXzo+mE8t3ocqiu3Po ZAYpi90tXd/T4e58BjodyrrlNYQ0Hk91Ho07g7RL5r2QoXzcjRrfKApIxVA5J/IvgcHJ iYtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VNXHGdwH; 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 r3-v6si703022pgn.313.2018.11.07.06.16.34; Wed, 07 Nov 2018 06:16:34 -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=VNXHGdwH; 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 S1726825AbeKGXrH (ORCPT + 3 others); Wed, 7 Nov 2018 18:47:07 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:38453 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730783AbeKGXrH (ORCPT ); Wed, 7 Nov 2018 18:47:07 -0500 Received: by mail-wm1-f65.google.com with SMTP id f2-v6so2257471wme.3 for ; Wed, 07 Nov 2018 06:16:33 -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=VJyGFvkW+sds6Swb6NM29U54YL8cp7noX7x5ydcenvM=; b=VNXHGdwHMksHbAThvdqD0wkk958QjAz/AkS3mbC+6WlTAJUrBev8wZr+aPuYO520LI hlBogISgm2Q+hoZ/D7yXL2PryPAI8vKKBxcdzHmBpaRs5oV5Cdv52ydivYRbpqmPey79 rCgYg0FE4tuJKBxBkc2BiFl/b6ZwQAEfmA9ZI= 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=VJyGFvkW+sds6Swb6NM29U54YL8cp7noX7x5ydcenvM=; b=Xr6JbalSVyKHC/j4Qk/J94cIfZnL7WMF6MJS6FZnpic5Kn7dPxu7AL1MOFCH69ZfuN oHHTa3ye9yEhtmFD7LQNwSbIGZSFn/1R0r/MV0ZnV6JjpKtTagrtisdT4exQ8Iy4o8jM EQJGSSpmPG4DaDJi1w7Pal8zOhBQUWYWMOSanVziYcz/gZaVZiCPRX/t2RnWel714voU NB0rrPQ1sjnViji9M9eHNqJOhXwcfIiJTp555hudYLzpotfBten+SJXR5OKIHaWOYjgp ThqGnyujBfCCuFmfoFNlZxlsm6O3Evn8eWPHxrXsXqOkyZeGeJgU83sWvEO4JffEHqxl DBYw== X-Gm-Message-State: AGRZ1gJbxMpp7rmNIH3NLPxv+K+nO9iTwPk7mBztK+dohON0/ojs3bT6 jbx6lxS2ZPiYJzCquJtUIQVGjA== X-Received: by 2002:a1c:1b91:: with SMTP id b139-v6mr337952wmb.37.1541600192368; Wed, 07 Nov 2018 06:16:32 -0800 (PST) Received: from localhost.localdomain (laubervilliers-657-1-83-120.w92-154.abo.wanadoo.fr. [92.154.90.120]) by smtp.gmail.com with ESMTPSA id q2sm948447wrx.77.2018.11.07.06.16.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Nov 2018 06:16:31 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, linux@armlinux.org.uk Cc: marc.zyngier@arm.com, will.deacon@arm.com, bhsharma@redhat.com, linux-efi@vger.kernel.org, Ard Biesheuvel Subject: [PATCH v2 6/6] efi: reduce the amount of memblock reservations for persistent allocations Date: Wed, 7 Nov 2018 15:16:11 +0100 Message-Id: <20181107141611.12076-7-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181107141611.12076-1-ard.biesheuvel@linaro.org> References: <20181107141611.12076-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. Tested-by: Marc Zyngier 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 59f8ac93c759..347028cb392f 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 */