Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp4181757imm; Mon, 18 Jun 2018 10:26:27 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJ4ZPfYsZsDsjtxuqY0Oq8Wa3zcuWB+sBTVJMpWy10/EarOKExw4Dup1ergalxFML29+Sp8 X-Received: by 2002:a63:384c:: with SMTP id h12-v6mr11820450pgn.230.1529342787211; Mon, 18 Jun 2018 10:26:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529342787; cv=none; d=google.com; s=arc-20160816; b=yRR/rNxVZWoQFqGlt7HJlty8d2D0Ns3qFffe75C7Is8wv+VM5a5ufKoGnMA/EcRiSw 8xE7SvUArNQqo3ZQgRvGVrgoPH+rctt1C7dthY9zS2n22AAqf33Y55XZJeiQiq7osb6T wKbjBcPybsY7Kup7MkHS5R8Z67m4rbRY4jqLmh90T0kKEodlyfLgk3BNpcOsv2GeZUI5 2YcXQe3LWY8aKfkacRr4TbK/stMfNuCCFMZwmW74Dm8CtLwC9ad2qJu7NEPdBCIKo11c wPF+pb4xghlr4liZvDz0LUfCtDXtF3A66s/CMz+Lt3esNUj+V9Z6xmRufioLeGadeMXI y3Qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=sKpouGROSmLroMt1dwuobig8fhFH/tg6uC1vylQGCx0=; b=T+LpH147QbfQwzRT2eN11LBzDT5WhalYS1caPQuEv7EqCZj8u5p0uqMu37ugByrThq VugZgXpoiAKC5NSfqK5DvtormFnVZMs5N/Jf34m9r0bD0o/E9Wra/tv17x0ZSpHPBX/Y h+MHAnBotdLWCzl+bT8vr431b6f+3EfJlBnu6N815A1e+siuaMfbPgJAVRij9rkLGrUo Am8oKx+bw+1xgiU6EqtjfUFnxdH623hA6Kpx3QWGrkjMJWaBt/JdvgmQ1aV8dgmyJ6Y1 oSZIHViZr6t0WwgrZkgjayh32tSIlyaR2kkSSKZcViaeYOFCthwO8ZyCSE2EnfU9xBIJ wL6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kZnWODZe; 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 p6-v6si15770729pfl.279.2018.06.18.10.26.13; Mon, 18 Jun 2018 10:26:27 -0700 (PDT) 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=kZnWODZe; 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 S935409AbeFRRZK (ORCPT + 99 others); Mon, 18 Jun 2018 13:25:10 -0400 Received: from mail-it0-f66.google.com ([209.85.214.66]:39440 "EHLO mail-it0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934963AbeFRRZG (ORCPT ); Mon, 18 Jun 2018 13:25:06 -0400 Received: by mail-it0-f66.google.com with SMTP id p185-v6so13108920itp.4 for ; Mon, 18 Jun 2018 10:25:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=sKpouGROSmLroMt1dwuobig8fhFH/tg6uC1vylQGCx0=; b=kZnWODZexCApx+8WFN1yp3hTWol2408j9RBm+3tP09PT+H5PPwWUNWQCCuGbggn7kB gbhS7DxnEZp1WBN6UA/RFNCB8E03qkhptG5W+CEIIESL39qxUsurMGW4T60bniGrMceI fFz3sFOztcF283Y8vkyGZeVT+TEFgfPOSnma0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=sKpouGROSmLroMt1dwuobig8fhFH/tg6uC1vylQGCx0=; b=ih7vGbM//GgVUCZGv0gRfbFBF9JUVaLxzhFkivytceC2OST8QSd0Pt5L03S7WOYv2/ LQRNAHcncGA+/hXAH4eOH1/di37AwQJX1jQbOYCUSOeI3dVztEC8o1wTm688AEcBpoLh KeP9iqspxkRDUGrAQRtVz++Fi65XYMRR7njl9HAmXi2J8fsPS9clhT/t0oPBy7j4uo9G cUag7gBNJ2idN8oQl/Ekh1hydzgCOSM2Yr8G5z1A4pIEWEpaBjoKvqNzJVKtVvZTSBsy c1GbGdU/7qjDRgby7eAlP5YNI+HcI0I5xp2Ov3C6iUZkZTHgmCGMBDlHRC0H8j+jH/gm 3Jsw== X-Gm-Message-State: APt69E3risaefYR5TSx36olDISgqwozrWqPP7rPX0oEI5TmgpO9ikfLw wfrFnqxv2RDRp3XfVkDgTLq1Ux2Vr3+UdMwrNM9Rww== X-Received: by 2002:a02:4187:: with SMTP id n7-v6mr10795529jad.86.1529342705976; Mon, 18 Jun 2018 10:25:05 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a6b:bbc7:0:0:0:0:0 with HTTP; Mon, 18 Jun 2018 10:25:05 -0700 (PDT) In-Reply-To: <1529342439.2333.60.camel@intel.com> References: <1529111365-32295-1-git-send-email-sai.praneeth.prakhya@intel.com> <1529342439.2333.60.camel@intel.com> From: Ard Biesheuvel Date: Mon, 18 Jun 2018 19:25:05 +0200 Message-ID: Subject: Re: [PATCH] x86/efi: Free allocated memory if remap fails To: Sai Praneeth Prakhya Cc: linux-efi , Linux Kernel Mailing List , Lee Chun-Yi , Borislav Petkov , Tony Luck , Dave Hansen , Bhupesh Sharma , Ricardo Neri , Peter Zijlstra , Ravi Shankar , Matt Fleming Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 18 June 2018 at 19:20, Sai Praneeth Prakhya wrote: > >> > It's a fact that memremap() and early_memremap() might never fail and >> > this code might never get a chance to run but to maintain good kernel >> > programming semantics, we might need this patch. >> > >> > Signed-off-by: Sai Praneeth Prakhya >> > Reviewed-by: Ricardo Neri >> Please don't include tags for reviews that did not happen on-list. >> > > Sure! Thanks for letting me know. > >> > @@ -450,10 +451,11 @@ void __init efi_free_boot_services(void) >> > >> > memunmap(new); >> > >> > - if (efi_memmap_install(new_phys, num_entries)) { >> > + if (efi_memmap_install(new_phys, num_entries)) >> > pr_err("Could not install new EFI memmap\n"); >> > - return; >> > - } >> > + >> > +free_mem: >> > + efi_memmap_free(new_phys, num_entries); >> Doesn't this free the memory map that you just installed? >> > > That's true! It's a bug. I will fix it. > >> > >> > } >> > >> > /** >> > + * efi_memmap_free - Free memory allocated by efi_memmap_alloc() >> > + * @mem: Physical address allocated by efi_memmap_alloc() >> > + * @num_entries: Number of entries in the allocated map. >> > + * >> > + * efi_memmap_alloc() allocates memory depending on whether mm_init() >> > + * has already been invoked or not. It uses either memblock or "normal" >> > + * page allocation. Use this function to free the memory allocated by >> > + * efi_memmap_alloc(). Since the allocation is done in two different >> > + * ways, similarly, we free it in two different ways. >> > + * >> > + */ >> > +void __init efi_memmap_free(phys_addr_t mem, unsigned int num_entries) >> > +{ >> > + unsigned long size = num_entries * efi.memmap.desc_size; >> > + unsigned int order = get_order(size); >> > + phys_addr_t end = mem + size - 1; >> > + >> > + if (slab_is_available()) { >> > + __free_pages(pfn_to_page(PHYS_PFN(mem)), order); >> How do you know that the memory you are freeing was allocated when >> slab_is_available() was already true? >> > > efi_memmap_free() should be used *only* in conjunction > with efi_memmap_alloc()(As I explicitly didn't mention this, maybe it might > have confused you). > > When allocating memory efi_memmap_alloc() does similar check > for slab_is_available() and if so, it allocates memory using alloc_pages(). > So, to free pages allocated using alloc_pages(), efi_memmap_free() > uses __free_pages(). > I understand that. But by abstracting away the free() routine as well as the alloc() routine, you are hiding this fact. What is preventing me from using efi_memmap_alloc() to allocate space for the memmap, and using efi_memmap_free() in another place? How are you preventing that this does not happen in a way where mm_init() may be called in the mean time? Whether __free_pages() should be used or memblock_free() is a property of the *allocation* itself, not of whether mm_init() has already been called. So if (!slab_is_available()), you can use memblock_free(). However, if (slab_is_available()), you cannot use __free_pages() because the allocation could have been made before mm_init() was called. >> > >> > + return; >> > + } >> > + >> > + if (memblock_free(mem, size)) >> > + pr_err("Failed to free mem from %pa to %pa\n", &mem, >> > &end); >> > +} >> > + > > Regards, > Sai