From patchwork Mon Oct 15 15:30:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 10642053 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 6EDC2112B for ; Mon, 15 Oct 2018 15:31:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5C14829AF4 for ; Mon, 15 Oct 2018 15:31:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4FF0E29B05; Mon, 15 Oct 2018 15:31:03 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8B81829AF4 for ; Mon, 15 Oct 2018 15:31:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3B5126B000C; Mon, 15 Oct 2018 11:30:57 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 39B7E6B000A; Mon, 15 Oct 2018 11:30:57 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 103ED6B000E; Mon, 15 Oct 2018 11:30:56 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by kanga.kvack.org (Postfix) with ESMTP id 8E25C6B000A for ; Mon, 15 Oct 2018 11:30:56 -0400 (EDT) Received: by mail-wr1-f72.google.com with SMTP id e11-v6so16555819wrr.14 for ; Mon, 15 Oct 2018 08:30:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=LcedzQ9KbwO/uDel+s/QEII3xdfa0hfO6dWQDOtuNKg=; b=APQvNiB3+MB2USk4oqJOpLe1t/p8JcEVMcx2XTYSLxiX3bU8LM4j9JIk2AzbAMKmqb Ko/vR/q8iZ32rcQliMLcG+aGyDxZIDnww3IznQ66eyX9HALeC9YZG5s9+bjTpNuua0jl JuQhcX9FI9RRBf+Oo0OpqnuViETdHzFsAGEQ4+CTbXpngmC5qIqf91sMVFTrFRe40kxW TPH2bp6ay9C5tWmHlxR2ZqG0DHyq7tj9d+AHi0MptrJV9g4GIhFwxYjKEA9+j21GIhZv Aj+9Rd/LR+Xc2mpMpFCwpt3Sx+Zvce18oH9TeRsXQR8WZ7MA6ENOB3HZq3MBf+DOAGXV NU1w== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of osalvador.vilardaga@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=osalvador.vilardaga@gmail.com X-Gm-Message-State: ABuFfoi9fjhxlmisPyxvF1FCdetvHuCYj0bOWRzb5uJRECbvXJ7tTGcj VMQny5U2q+6ojts/SX5H0Fg0M8vfXFcWu2kV6qpH+8JbV5NHlnex047Nu9/2WNfajHJvJSS9iEP l6BhHPYPRu18Y1KICo16hYkKUspuaYAvF8WevZCP4ayJ7vwWWOMUfoJ4aGICwLBI0Xvnb33AiRL n0pXTWIfod9miI13WQiqsd7SoDxa5ocETBISoHnngdEEMRg54wJFYGUYwVwfJHM4qJ5ohFRnpl9 b/KGQIJLa+IT6uSvB3RqEOb4NqASbloaBvKGM30LPP/do6OQ/mBjDtQiPh0UNqG5IBYXOoFJiKc 5rUbez30rF5701/dK/T3fh/kVEkpwkKQQrhCc2FJcfcrjIUev1/1Kdyf+a54fQTQAKqXT+kRYw= = X-Received: by 2002:adf:8523:: with SMTP id 32-v6mr15627416wrh.72.1539617456037; Mon, 15 Oct 2018 08:30:56 -0700 (PDT) X-Received: by 2002:adf:8523:: with SMTP id 32-v6mr15627342wrh.72.1539617454778; Mon, 15 Oct 2018 08:30:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539617454; cv=none; d=google.com; s=arc-20160816; b=FXsapGS3/gpXqoJO4yMpUrVC51KQ6OhuUcbj2sn/6Y8DwdfDhpzkYLZMrhLfX2dwA0 Foy9+ayQCwoRMzC2CB1XnUqj1IKaiibwi7YK0Y9vaFjQ60VH8f/7B+caCCQNqMqAEKKB ivZOS9T3V6k8n49px120uuo3768CRiBibqQHtxqTeAfwp+4Mtod+HmdOTJ7yur9woADC GjdecDbgw9G5yj80UOD4ZdCNKamlXMgqHRxMU0ptR6qCfXnILNnqQBRsAcK+sHix0k0o RNRojFnhxmr0nYSN9kgdFRK55DiM0ltMZp+h7UUJDeJ8zhsqbQCE0dwK9UE/k2nBQU3C oPKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=LcedzQ9KbwO/uDel+s/QEII3xdfa0hfO6dWQDOtuNKg=; b=T4tK8Vh88OT7W9uS9ZZICDoYDGP081EBxJqDWQ4BOpK32SzPXQbMvAuDwYgpe63MiW K3+hdC0grFP5G+NKUBh9jusvV3spOtnhzPFd/BkwuwsxK9TlcMNKd9yZKmjsFlwzztY3 BopW2SrViSUC62xiATtJdfJ/sBzT1lwx2jR4qWu+aNq42hwl0z3ObFb1b7MH7QE6MrX7 /lwtknBjzhFNLyV6kqj3WYzMw0j3KX5Oi+0xjyuq2acuJqA93+KqaVw5KGZOURrsMD9T jAoXh6vs0cg1y+0dlI9NorPqEFbGxC6lwe0BbJnWxLS6xroQOgbrRFu8kcpO3RaEn/kS nFog== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of osalvador.vilardaga@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=osalvador.vilardaga@gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id 52-v6sor6075658wra.30.2018.10.15.08.30.54 for (Google Transport Security); Mon, 15 Oct 2018 08:30:54 -0700 (PDT) Received-SPF: pass (google.com: domain of osalvador.vilardaga@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; spf=pass (google.com: domain of osalvador.vilardaga@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=osalvador.vilardaga@gmail.com X-Google-Smtp-Source: ACcGV62VjkJMdZfzajn6NL4EUmCICXo7Qqlk5YGPd7/P8ID+nao/J70uqjwIO322xUcznzUwLmMJWA== X-Received: by 2002:adf:c00e:: with SMTP id z14-v6mr14904281wre.268.1539617454236; Mon, 15 Oct 2018 08:30:54 -0700 (PDT) Received: from techadventures.net (techadventures.net. [62.201.165.239]) by smtp.gmail.com with ESMTPSA id e14-v6sm9812528wrt.76.2018.10.15.08.30.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Oct 2018 08:30:52 -0700 (PDT) Received: from d104.suse.de (charybdis-ext.suse.de [195.135.221.2]) by techadventures.net (Postfix) with ESMTPA id F3C211259A7; Mon, 15 Oct 2018 17:30:51 +0200 (CEST) From: Oscar Salvador To: akpm@linux-foundation.org Cc: mhocko@suse.com, dan.j.williams@intel.com, yasu.isimatu@gmail.com, rppt@linux.vnet.ibm.com, malat@debian.org, linux-kernel@vger.kernel.org, pavel.tatashin@microsoft.com, jglisse@redhat.com, Jonathan.Cameron@huawei.com, rafael@kernel.org, david@redhat.com, dave.jiang@intel.com, linux-mm@kvack.org, alexander.h.duyck@linux.intel.com, Oscar Salvador Subject: [PATCH 2/5] mm/memory_hotplug: Create add/del_device_memory functions Date: Mon, 15 Oct 2018 17:30:31 +0200 Message-Id: <20181015153034.32203-3-osalvador@techadventures.net> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20181015153034.32203-1-osalvador@techadventures.net> References: <20181015153034.32203-1-osalvador@techadventures.net> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Oscar Salvador HMM/devm have a particular handling of memory-hotplug. They do not go through the common path, and so, they do not call either offline_pages() or online_pages(). The operations they perform are the following ones: 1) Create the linear mapping in case the memory is not private 2) Initialize the pages and add the sections 3) Move the pages to ZONE_DEVICE Due to this particular handling of hot-add/remove memory from HMM/devm, I think it would be nice to provide a helper function in order to make this cleaner, and not populate other regions with code that should belong to memory-hotplug. The helpers are named: del_device_memory add_device_memory The idea is that add_device_memory will be in charge of: a) call either arch_add_memory() or add_pages(), depending on whether we want a linear mapping b) online the memory sections that correspond to the pfn range c) call move_pfn_range_to_zone() being zone ZONE_DEVICE to expand zone/pgdat spanned pages and initialize its pages del_device_memory, on the other hand, will be in charge of: a) offline the memory sections that correspond to the pfn range b) call shrink_zone_pgdat_pages(), which shrinks node/zone spanned pages. c) call either arch_remove_memory() or __remove_pages(), depending on whether we need to tear down the linear mapping or not The reason behind step b) from add_device_memory() and step a) from del_device_memory is that now find_smallest/biggest_section_pfn will have to check for online sections, and not for valid sections as they used to do, because we call offline_mem_sections() in offline_pages(). In order to split up better the patches and ease the review, this patch will only make a) case work for add_device_memory(), and case c) for del_device_memory. The other cases will be added in the next patch. These two functions have to be called from devm/HMM code: dd_device_memory: - devm_memremap_pages() - hmm_devmem_pages_create() del_device_memory: - hmm_devmem_release - devm_memremap_pages_release One thing I do not know is whether we can move kasan calls out of the hotplug lock or not. If we can, we could move the hotplug lock within add/del_device_memory(). Signed-off-by: Oscar Salvador Reviewed-by: David Hildenbrand Reviewed-by: Pavel Tatashin --- include/linux/memory_hotplug.h | 11 +++++++++++ kernel/memremap.c | 11 ++++------- mm/hmm.c | 33 +++++++++++++++++---------------- mm/memory_hotplug.c | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 23 deletions(-) diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index 786cdfc9a974..cf014d5edbb2 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -111,8 +111,19 @@ extern int arch_remove_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap); extern int __remove_pages(struct zone *zone, unsigned long start_pfn, unsigned long nr_pages, struct vmem_altmap *altmap); + +#ifdef CONFIG_ZONE_DEVICE +extern int del_device_memory(int nid, unsigned long start, unsigned long size, + struct vmem_altmap *altmap, bool private_mem); +#endif + #endif /* CONFIG_MEMORY_HOTREMOVE */ +#ifdef CONFIG_ZONE_DEVICE +extern int add_device_memory(int nid, unsigned long start, unsigned long size, + struct vmem_altmap *altmap, bool private_mem); +#endif + /* reasonably generic interface to expand the physical pages */ extern int __add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages, struct vmem_altmap *altmap, bool want_memblock); diff --git a/kernel/memremap.c b/kernel/memremap.c index c95df6ed2d4a..b86bba8713b9 100644 --- a/kernel/memremap.c +++ b/kernel/memremap.c @@ -86,6 +86,8 @@ static void devm_memremap_pages_release(void *data) struct device *dev = pgmap->dev; struct resource *res = &pgmap->res; resource_size_t align_start, align_size; + struct vmem_altmap *altmap = pgmap->altmap_valid ? + &pgmap->altmap : NULL; unsigned long pfn; int nid; @@ -104,8 +106,7 @@ static void devm_memremap_pages_release(void *data) nid = dev_to_node(dev); mem_hotplug_begin(); - arch_remove_memory(nid, align_start, align_size, pgmap->altmap_valid ? - &pgmap->altmap : NULL); + del_device_memory(nid, align_start, align_size, altmap, true); kasan_remove_zero_shadow(__va(align_start), align_size); mem_hotplug_done(); @@ -204,11 +205,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) goto err_kasan; } - error = arch_add_memory(nid, align_start, align_size, altmap, false); - if (!error) - move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE], - align_start >> PAGE_SHIFT, - align_size >> PAGE_SHIFT, altmap); + error = add_device_memory(nid, align_start, align_size, altmap, true); mem_hotplug_done(); if (error) goto err_add_memory; diff --git a/mm/hmm.c b/mm/hmm.c index 42d79bcc8aab..d3e52ae71bd9 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -996,6 +996,7 @@ static void hmm_devmem_release(struct device *dev, void *data) struct zone *zone; struct page *page; int nid; + bool mapping; if (percpu_ref_tryget_live(&devmem->ref)) { dev_WARN(dev, "%s: page mapping is still live!\n", __func__); @@ -1010,12 +1011,15 @@ static void hmm_devmem_release(struct device *dev, void *data) zone = page_zone(page); nid = zone->zone_pgdat->node_id; - mem_hotplug_begin(); if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) - __remove_pages(zone, start_pfn, npages, NULL); + mapping = false; else - arch_remove_memory(nid, start_pfn << PAGE_SHIFT, - npages << PAGE_SHIFT, NULL); + mapping = true; + + mem_hotplug_begin(); + del_device_memory(nid, start_pfn << PAGE_SHIFT, npages << PAGE_SHIFT, + NULL, + mapping); mem_hotplug_done(); hmm_devmem_radix_release(resource); @@ -1026,6 +1030,7 @@ static int hmm_devmem_pages_create(struct hmm_devmem *devmem) resource_size_t key, align_start, align_size, align_end; struct device *device = devmem->device; int ret, nid, is_ram; + bool mapping; align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1); align_size = ALIGN(devmem->resource->start + @@ -1084,7 +1089,6 @@ static int hmm_devmem_pages_create(struct hmm_devmem *devmem) if (nid < 0) nid = numa_mem_id(); - mem_hotplug_begin(); /* * For device private memory we call add_pages() as we only need to * allocate and initialize struct page for the device memory. More- @@ -1096,20 +1100,17 @@ static int hmm_devmem_pages_create(struct hmm_devmem *devmem) * want the linear mapping and thus use arch_add_memory(). */ if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC) - ret = arch_add_memory(nid, align_start, align_size, NULL, - false); + mapping = true; else - ret = add_pages(nid, align_start >> PAGE_SHIFT, - align_size >> PAGE_SHIFT, NULL, false); - if (ret) { - mem_hotplug_done(); - goto error_add_memory; - } - move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE], - align_start >> PAGE_SHIFT, - align_size >> PAGE_SHIFT, NULL); + mapping = false; + + mem_hotplug_begin(); + ret = add_device_memory(nid, align_start, align_size, NULL, mapping); mem_hotplug_done(); + if (ret) + goto error_add_memory; + /* * Initialization of the pages has been deferred until now in order * to allow us to do the work while not holding the hotplug lock. diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 33d448314b3f..5874aceb81ac 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1889,4 +1889,45 @@ void remove_memory(int nid, u64 start, u64 size) unlock_device_hotplug(); } EXPORT_SYMBOL_GPL(remove_memory); + +#ifdef CONFIG_ZONE_DEVICE +int del_device_memory(int nid, unsigned long start, unsigned long size, + struct vmem_altmap *altmap, bool mapping) +{ + int ret; + unsigned long start_pfn = PHYS_PFN(start); + unsigned long nr_pages = size >> PAGE_SHIFT; + struct zone *zone = page_zone(pfn_to_page(pfn)); + + if (mapping) + ret = arch_remove_memory(nid, start, size, altmap); + else + ret = __remove_pages(zone, start_pfn, nr_pages, altmap); + + return ret; +} +#endif #endif /* CONFIG_MEMORY_HOTREMOVE */ + +#ifdef CONFIG_ZONE_DEVICE +int add_device_memory(int nid, unsigned long start, unsigned long size, + struct vmem_altmap *altmap, bool mapping) +{ + int ret; + unsigned long start_pfn = PHYS_PFN(start); + unsigned long nr_pages = size >> PAGE_SHIFT; + + if (mapping) + ret = arch_add_memory(nid, start, size, altmap, false); + else + ret = add_pages(nid, start_pfn, nr_pages, altmap, false); + + if (!ret) { + struct zone *zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE]; + + move_pfn_range_to_zone(zone, start_pfn, nr_pages, altmap); + } + + return ret; +} +#endif