Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp5283467imm; Tue, 19 Jun 2018 08:01:22 -0700 (PDT) X-Google-Smtp-Source: ADUXVKK66ALgpPYEVL1pvQe9n5qCCkI4JLeWxZ6AeDxvaScpy2ZOTv+8cosdzcOlQFo/KxrQSTFN X-Received: by 2002:a62:3d05:: with SMTP id k5-v6mr18434758pfa.122.1529420482421; Tue, 19 Jun 2018 08:01:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529420482; cv=none; d=google.com; s=arc-20160816; b=S3C9U7LpUdZ9NJuI3+6IS7ZqlrCp7cldkXXzMyGh8L1XjjATKTnxuJwlzmQDzku5Kd T0a6RW9pDuuVHtZhNOzBT8yRpdPw3ew9nTEQIZJN9gQcS3bV3w5G/6uB2RfXJbBAxeI/ 833WqVKs/JR0WEoAN1s12BGBX4azbF5a5DusI1MDk9KIRdgAHTSlW4uQOj5FvwZ2wSzF qtg52aBk/dYgnzzf2bOfzAqhEhO0WFvAHhuVtgYzDOTs68R5Mfb1EhTC2cOx3JPjGl7e 4/rgBPfyY8dLAcbHbzH+aWen8Ot8/Y77Iyu/W7n5PYK2pVe+JDls19AanvlHAFA0IDkd naZQ== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=RR74RyYK0PrRrj9AIEg/CZ6fsFKqGRaayDeq0NSG8EQ=; b=zraKhZtWL+bdz1BK747b3xr+aajGJ6W+jnXc+d6RYPYoyY7eRX+g/EKh4T59c7wABl rdvS3fyMZbpTd0yJ+ri4msz69kb1tT+NrFMpw4AwuCbFHqv01iK8d/OXgcossmLRM+54 RG3CKvddzEsL5DzZ4x9krRxTWYPMrtytvdzJv10nhBjAHFg7FnC2LOxJXT4pcwZmx2l5 bAeI20jfvaRjNkNGO7ZIGLka66LNW8K+idZ+fBb/upEgWyb7mMM93qlevRqcBchdyvMc ZxhqIpt2bdz8hL5Wcs2pw2yNMm+7cUgTEFR2rNyL2qodBbr4nrJvuj6GrI92LD3UCgyE gVPA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n15-v6si15145991pgf.45.2018.06.19.08.01.02; Tue, 19 Jun 2018 08:01:22 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966532AbeFSPAM (ORCPT + 99 others); Tue, 19 Jun 2018 11:00:12 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:51950 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965698AbeFSPAK (ORCPT ); Tue, 19 Jun 2018 11:00:10 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 678E480D; Tue, 19 Jun 2018 08:00:10 -0700 (PDT) Received: from [10.1.206.34] (melchizedek.cambridge.arm.com [10.1.206.34]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 73B723F557; Tue, 19 Jun 2018 08:00:07 -0700 (PDT) Subject: Re: [PATCH v2 1/4] arm64: export memblock_reserve()d regions via /proc/iomem To: Dave Kleikamp , AKASHI Takahiro , catalin.marinas@arm.com, will.deacon@arm.com, akpm@linux-foundation.org, ard.biesheuvel@linaro.org Cc: mark.rutland@arm.com, lorenzo.pieralisi@arm.com, graeme.gregory@linaro.org, al.stone@linaro.org, bhsharma@redhat.com, tbaicar@codeaurora.org, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, hanjun.guo@linaro.org, sudeep.holla@arm.com, dyoung@redhat.com, linux-arm-kernel@lists.infradead.org References: <20180619064424.6642-1-takahiro.akashi@linaro.org> <20180619064424.6642-2-takahiro.akashi@linaro.org> <72307608-90e0-4842-edc1-d3b284782940@oracle.com> From: James Morse Message-ID: Date: Tue, 19 Jun 2018 16:00:05 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <72307608-90e0-4842-edc1-d3b284782940@oracle.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Dave, On 19/06/18 14:37, Dave Kleikamp wrote: > On 06/19/2018 01:44 AM, AKASHI Takahiro wrote: >> +static int __init reserve_memblock_reserved_regions(void) >> +{ >> + phys_addr_t start, end, roundup_end = 0; >> + struct resource *mem, *res; >> + u64 i; >> + >> + for_each_reserved_mem_region(i, &start, &end) { >> + if (end <= roundup_end) >> + continue; /* done already */ >> + >> + start = __pfn_to_phys(PFN_DOWN(start)); >> + end = __pfn_to_phys(PFN_UP(end)) - 1; >> + roundup_end = end; >> + >> + res = kzalloc(sizeof(*res), GFP_ATOMIC); >> + if (WARN_ON(!res)) >> + return -ENOMEM; >> + res->start = start; >> + res->end = end; >> + res->name = "reserved"; >> + res->flags = IORESOURCE_MEM; >> + >> + mem = request_resource_conflict(&iomem_resource, res); >> + /* >> + * We expected memblock_reserve() regions to conflict with >> + * memory created by request_standard_resources(). >> + */ >> + if (WARN_ON_ONCE(!mem)) >> + continue; >> + kfree(res); > > Why is kfree() after the conditional continue? This is a memory leak. request_resource_conflict() inserts res into the iomem_resource tree, or returns the conflict if there is already something at this address. We expect something at this address: a 'System RAM' section added by request_resource(). This code wants the conflicting 'System RAM' entry so that reserve_region_with_split() can fill in the gaps below it with 'reserved'. See the commit-message for an example. If there was no conflict, it means the memory map doesn't look like we expect, we can't pass NULL to reserve_region_with_split(), and we just inserted the 'reserved' at the top level. Freeing res at this point would be a use-after-free hanging from /proc/iomem. This code generates a WARN_ON_ONCE() and leaves the 'reserved' description where it is. Trying to cleanup here is pointless, we can remove the resource entry and free it ... but we still want to report it as reserved, which is what just happened, just not quite how we we wanted it. If you ever see this warning, it means some RAM stopped being nomap between request_resources() and reserve_memblock_reserved_regions(). I can't find any case where that ever happens. If all that makes sense: how can I improve the comment above the WARN_ON_ONCE() to make it clearer? Thanks, James >> + >> + reserve_region_with_split(mem, start, end, "reserved"); >> + } >> + >> + return 0; >> +} >> +arch_initcall(reserve_memblock_reserved_regions); >> + >> u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID }; >> >> void __init setup_arch(char **cmdline_p) >>