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: 10672433 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1517714D6 for ; Wed, 7 Nov 2018 14:21:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0432F2C0B3 for ; Wed, 7 Nov 2018 14:21:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ECB262C0CD; Wed, 7 Nov 2018 14:21:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 892272C0B3 for ; Wed, 7 Nov 2018 14:21:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Gkk9ZIYlk128crJcFwhfIiPEVrk+2Pp4mg1TspDY0bw=; b=RnEdWiIie2laP5 9xvZR+80eS2Jcm3XzEh9apNA7ZS96pE4MJKvOahcIxb7dmUGLQEcEt4PRNhx/cxHKEukb4MTGNZ3b VOSQnw9IsCRwX8R6KrVSkbg2DzcQk+D5MUP+uFhxXiNTIIHFcD7DBT+ZIYLgy+neTXgfviXGo6GmX /Wh8WSw2b27DWZST+iBJmqOSFFAhPt0WHHnkcnLZHwB2OSdqlsMLvEx0+3L410S5KfyluqSsSWPe+ 2SeomT61ONdOqj8W3NZ+rjC6mXwx4cqFB20IVhLmNwXqM84unVNd781JjH5cjvN1h91jE24/74OaN Jfijilg0m2V4KSrDQqYA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gKOhY-0007AD-GD; Wed, 07 Nov 2018 14:21:00 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gKOdK-00040y-UB for linux-arm-kernel@lists.infradead.org; Wed, 07 Nov 2018 14:16:40 +0000 Received: by mail-wm1-x342.google.com with SMTP id p2-v6so15830401wmc.2 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=uh+Vad328/fwNpUWfAdhMCcD6RAE+uDOvzp28AgSFFcFqoPISQXSerVincobgHnzfY b8m6lgqYzsECjT0YW+ckxthpam9ozf1ZsbnTVSmTHYV4Jd0hjZ8Obd/kWAevkrmOG3Ll bpmxjlVBQ/vDFaYUL6k/8D5HkmZEqxvexkrZ3761xbFYhQIeP5jTAGwHdjU2sXJd7n84 SV+sjLFbOo8lFUivyZcs3eJzZVRpaeD2OrxOzO5GH2WwukUFuGqjJXT6U82tLmO3754X LOS6R8zuF70K9t4rPnERZvoUa1KgtB3rknSwCHp37seKs8xf/Zh3Il5T+aq9JrJrb7s1 1t9Q== X-Gm-Message-State: AGRZ1gLQKHNnpnJS22NUGJI2wUN06ukaFOCWh6uDgk2rDZv2G/VdUMGd XnL/Zv4li/qY3u1A1DKrVbL7qrpsIfw= X-Google-Smtp-Source: AJdET5eWopr7wyqy/iYDf3Bh4lI6cE8PBnpV563ZoYfJnilosiFXWWqCmfzEj7v4qUdvBrKuLcXytg== 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 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181107_061638_997874_517D9E8D X-CRM114-Status: GOOD ( 17.06 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: marc.zyngier@arm.com, bhsharma@redhat.com, linux-efi@vger.kernel.org, will.deacon@arm.com, Ard Biesheuvel Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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(-) 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 */