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 */