Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4815072pxu; Tue, 22 Dec 2020 01:14:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJxdCUDABnTx7xtToF0BTO7VgUow8IpZvN1d/y4mK/U+T/z7WuCtkm9RfCzTAMSCQiR8PdAs X-Received: by 2002:a17:906:9888:: with SMTP id zc8mr18691402ejb.42.1608628484118; Tue, 22 Dec 2020 01:14:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608628484; cv=none; d=google.com; s=arc-20160816; b=At1lHws9ii/bHPhgmoI5eG1njKTpLHXf+fI5dkWU5zo/PwmhBHRbeSrV8gmxgGEqI6 iX2syjb7SXkfFj/E4K8B4VMKLDHECsL6LA/1yLNWqDASXb+r6NfFJ/RTOmqg6Zi+EcUJ MLxJQR0yXBEHGoGSFMKkbEzvcNu06n2hsxZPPhZ+4QiPpa1J/Y0YfqAaghIS1hzkG9GG FGKJ98wGsY5M2b+3/GlghcMEOr3RZHTBqKf+oNQ/+BV7tYReY+7g9GErpCFwx8zJjDjF aBBO/qaJDna9WtKYQo7q6dPb5sDGvOzSQmSDydss+q4/foaBkuz/jeBC7Pmzcj6QBTpL X4KA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:organization :from:references:cc:to:subject:dkim-signature; bh=/1stW+R22xfRIjdrT4M0wBA1uC6eA2ifO4unIEV3O6Y=; b=WqzuQRTMXGxPmJ/4CBqx8yWnpo/cXmupQhtqOoepqTLmKE3xmdikKAGwu1R2X+3LjH EAbJEnAHA98e0TJIF2Behrw4Bme8gcaNeC+tyRi0nVo4EglCUAmcZiUquZGwG1uShs7V EfY3iZaAj/OVj0jZFlAiPdrMgqv0z72AYDsoVGYZ9hU8KuBS6LKdkMI+6osWvATNzuwO zeB79AgRFa68vgxNYf7uBowunMR4qoOeBE6t90ju0SCtbAdCdcC1l7FlzuAOaXvm9zTo ZM/6GGhCqc2kCbjKcrfvWbg+9FYw6DXa+0Nn/8jaLT63LlUs8FxGo5cDv3jUhub+eXPK bcNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=boXQ1eSZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id zn8si11378564ejb.32.2020.12.22.01.14.07; Tue, 22 Dec 2020 01:14:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=boXQ1eSZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725913AbgLVJNS (ORCPT + 99 others); Tue, 22 Dec 2020 04:13:18 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:20487 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725785AbgLVJNR (ORCPT ); Tue, 22 Dec 2020 04:13:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1608628311; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/1stW+R22xfRIjdrT4M0wBA1uC6eA2ifO4unIEV3O6Y=; b=boXQ1eSZ3xQdyo2d0S4JGvlL/V/IPieHpcqq6EQ9Vdw8yWIqE2sW91y9u8Mk0NTFEXTpsX FDhRadlzdXWesZm3exgNiz5xhCzdW5v44YdRR4aEdkXOwYKOO++Au6kZbfxwqSxW4LyeO6 5Iza8cqrt4IdjXmx3VMclWu/9zwg5sw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-405-NZ1b3RPuOE-UTNTKUBr9wg-1; Tue, 22 Dec 2020 04:11:46 -0500 X-MC-Unique: NZ1b3RPuOE-UTNTKUBr9wg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 58CD910054FF; Tue, 22 Dec 2020 09:11:44 +0000 (UTC) Received: from [10.36.113.220] (ovpn-113-220.ams2.redhat.com [10.36.113.220]) by smtp.corp.redhat.com (Postfix) with ESMTP id A58BF6B54E; Tue, 22 Dec 2020 09:11:41 +0000 (UTC) Subject: Re: [RFC 1/2] arm64/mm: Fix pfn_valid() for ZONE_DEVICE based memory To: Anshuman Khandual , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: catalin.marinas@arm.com, will@kernel.org, ardb@kernel.org, Mark Rutland , James Morse , Robin Murphy , =?UTF-8?B?SsOpcsO0bWUgR2xpc3Nl?= , Dan Williams , Mike Rapoport , Michael Ellerman References: <1608621144-4001-1-git-send-email-anshuman.khandual@arm.com> <1608621144-4001-2-git-send-email-anshuman.khandual@arm.com> From: David Hildenbrand Organization: Red Hat GmbH Message-ID: <4b282848-d2d7-6156-4726-ce974b2dff41@redhat.com> Date: Tue, 22 Dec 2020 10:11:40 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: <1608621144-4001-2-git-send-email-anshuman.khandual@arm.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 22.12.20 08:12, Anshuman Khandual wrote: > pfn_valid() validates a pfn but basically it checks for a valid struct page > backing for that pfn. It should always return positive for memory ranges > backed with struct page mapping. But currently pfn_valid() fails for all > ZONE_DEVICE based memory types even though they have struct page mapping. > > pfn_valid() asserts that there is a memblock entry for a given pfn without > MEMBLOCK_NOMAP flag being set. The problem with ZONE_DEVICE based memory is > that they do not have memblock entries. Hence memblock_is_map_memory() will > invariably fail via memblock_search() for a ZONE_DEVICE based address. This > eventually fails pfn_valid() which is wrong. memblock_is_map_memory() needs > to be skipped for such memory ranges. As ZONE_DEVICE memory gets hotplugged > into the system via memremap_pages() called from a driver, their respective > memory sections will not have SECTION_IS_EARLY set. > > Normal hotplug memory will never have MEMBLOCK_NOMAP set in their memblock > regions. Because the flag MEMBLOCK_NOMAP was specifically designed and set > for firmware reserved memory regions. memblock_is_map_memory() can just be > skipped as its always going to be positive and that will be an optimization > for the normal hotplug memory. Like ZONE_DEVIE based memory, all hotplugged > normal memory too will not have SECTION_IS_EARLY set for their sections. > > Skipping memblock_is_map_memory() for all non early memory sections would > fix pfn_valid() problem for ZONE_DEVICE based memory and also improve its > performance for normal hotplug memory as well. > > Cc: Catalin Marinas > Cc: Will Deacon > Cc: Ard Biesheuvel > Cc: Robin Murphy > Cc: linux-arm-kernel@lists.infradead.org > Cc: linux-kernel@vger.kernel.org > Fixes: 73b20c84d42d ("arm64: mm: implement pte_devmap support") > Signed-off-by: Anshuman Khandual > --- > arch/arm64/mm/init.c | 12 ++++++++++++ > 1 file changed, 12 insertions(+) > > diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c > index 75addb36354a..ee23bda00c28 100644 > --- a/arch/arm64/mm/init.c > +++ b/arch/arm64/mm/init.c > @@ -225,6 +225,18 @@ int pfn_valid(unsigned long pfn) > > if (!valid_section(__pfn_to_section(pfn))) > return 0; > + > + /* > + * ZONE_DEVICE memory does not have the memblock entries. > + * memblock_is_map_memory() check for ZONE_DEVICE based > + * addresses will always fail. Even the normal hotplugged > + * memory will never have MEMBLOCK_NOMAP flag set in their > + * memblock entries. Skip memblock search for all non early > + * memory sections covering all of hotplug memory including > + * both normal and ZONE_DEVIE based. > + */ > + if (!early_section(__pfn_to_section(pfn))) > + return 1; Actually, I think we want to check for partial present sections. Maybe we can rather switch to generic pfn_valid() and tweak it to something like diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index fb3bf696c05e..7b1fcce5bd5a 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -1382,9 +1382,13 @@ static inline int pfn_valid(unsigned long pfn) return 0; /* * Traditionally early sections always returned pfn_valid() for - * the entire section-sized span. + * the entire section-sized span. Some archs might have holes in + * early sections, so double check with memblock if configured. */ - return early_section(ms) || pfn_section_valid(ms, pfn); + if (early_section(ms)) + return IS_ENABLED(CONFIG_EARLY_SECTION_MEMMAP_HOLES) ? + memblock_is_map_memory(pfn << PAGE_SHIFT) : 1; + return pfn_section_valid(ms, pfn); } #endif Which users are remaining that require us to add/remove memblocks when hot(un)plugging memory $ git grep KEEP_MEM | grep memory_hotplug mm/memory_hotplug.c: if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) mm/memory_hotplug.c: if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) mm/memory_hotplug.c: if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) { I think one user we would have to handle is arch/arm64/mm/mmap.c:valid_phys_addr_range(). AFAIS, powerpc at least does not rely on memblock_is_map_memory. -- Thanks, David / dhildenb