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: 10670159 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 48CEC15A6 for ; Tue, 6 Nov 2018 11:38:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 357FE29BD8 for ; Tue, 6 Nov 2018 11:38:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 24BF429BFC; Tue, 6 Nov 2018 11:38:51 +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 B601429BD8 for ; Tue, 6 Nov 2018 11:38:50 +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=zyztgnk7kI3ubmiJzQF0kInubDiY61PF87YapgbrRN0=; b=hiAg6aRYm+TCiR /klkvGB6B9TJoq+G2A/ewVx3AntvhwNZ0NkWJaislRCSwUjxXUTTSErqa40Kdpf/uXP/luz9zBCqe tEKK+jpybiijGLPY4nhjWOrFRUxdC1rAoTj5FH6QvOAo9bmhzmWPf2AewJOGcYGog1y1hD7tSkmmk MoEO5WZe5Ihw1fSMM+JkPl9PIyQxl9jJlwpfU/oN9yX/TRXF8x7l3oa7wYlkZXpbInD65X31mEH0A tNoJ8hHJSA2SzwshAC3Q9S25dEqZ3KAC9tLRqQ/wyow+chTAZ915f9fDU1XfW6uN0jk6YLmOR3iV3 c+SN3jQDJWOaL03t5xgQ==; 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 1gJzgv-00008y-VU; Tue, 06 Nov 2018 11:38:42 +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 1gJzgA-0007cg-UV for linux-arm-kernel@lists.infradead.org; Tue, 06 Nov 2018 11:37:57 +0000 Received: by mail-wm1-x342.google.com with SMTP id r63-v6so11122929wma.4 for ; Tue, 06 Nov 2018 03:37:47 -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=jB55RWqhdQ12n7wCY+gbni5jltT5BGMG/d+oWVZwlqGyfoHmGlQ5/XUJMRFBR21X6T nal93mDxoqvVi7ADAopJ5pi2Ab5fJDCmboKASkv+2lEWcW5M3O1zQ8RT8pDs7qby/Olp UCc4RHApTcK5uYOWF1wskVYM3cXOl63oSYA/AVw+SVTLGRV9960nSKRGQMOXec7AbS1O pfGOP7KQ/z0BHUczqtc4rSAvAoZU/f4TqSKMClsKKjo+hSHXLH1Cyjpucl0o9568r7yg J0LYyoauZCAQFsPpd3hRTCmGKjeIC41oG/zdocyIPmXzvLe5NCSO3D0qoAmy/8nZmstp q4dw== X-Gm-Message-State: AGRZ1gIjqcrRGXVjKftzps6Ex1lsgjeXflOT1QRDG2wjFxyFDhisKpBn 0tUhfRXBUKKHO8kjIeeIwMkk9yRHABs= X-Google-Smtp-Source: AJdET5dmblwAjcwZQJw8GPZUJXev9PUfW2AwbT9ayu866c3HSxwYnNQhq9PDGd3xQkXNQAZnbhK0Rw== 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 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181106_033755_015414_F93CEAE0 X-CRM114-Status: GOOD ( 16.64 ) 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: mark.rutland@arm.com, linux-efi@vger.kernel.org, Ard Biesheuvel , marc.zyngier@arm.com, bhsharma@redhat.com, will.deacon@arm.com 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. 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 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 */