From patchwork Mon May 21 22:35:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 10417019 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7F94A6053B for ; Mon, 21 May 2018 22:45:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6C1A828ACE for ; Mon, 21 May 2018 22:45:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5FF8F28AD3; Mon, 21 May 2018 22:45:36 +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 6B36128ACE for ; Mon, 21 May 2018 22:45:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3B8356B0008; Mon, 21 May 2018 18:45:34 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 341EB6B000A; Mon, 21 May 2018 18:45:34 -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 20A056B000C; Mon, 21 May 2018 18:45:34 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl0-f72.google.com (mail-pl0-f72.google.com [209.85.160.72]) by kanga.kvack.org (Postfix) with ESMTP id D4F346B0008 for ; Mon, 21 May 2018 18:45:33 -0400 (EDT) Received: by mail-pl0-f72.google.com with SMTP id b31-v6so10931707plb.5 for ; Mon, 21 May 2018 15:45:33 -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:subject:from :to:cc:date:message-id:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=km5uJUaSmUvOCL7Omk0nQPmVkxIUr0rf6x6m6Wg8SYg=; b=OhZc83keHtOQJyjTp/2r3MB4SG/Hmwyr3qtTbOIFLzGIj1UwggdxlUxQKC2FpmUvYX AjBiDTE8qosS+WxVVY3Eh8Lk0E4PDBxkFcPVQjX5uFJrXteCMZZomN+jpqzmJ/3AexP0 5AOTKVr1NNq++Pa+fQJCGolQO4H/hI+cTyiBWIg7w6947RsNydCF/3LuosuGhnBJQrc6 4BCiFSyYtHSRrGQaoTE6m7zvDGgMLzDphTVLrHbUxFBJojJ/vZRIwBRJBZBYPCGHCCEp F0isl/AzCSRgY5/yTh7pREhiEHiwxjbTTgVB20idHPGDnmqHyxz1QEs64WCfLJHf+B5I QQBg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=dan.j.williams@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: ALKqPwdr2eWA7jFWa00NIQ3KusActeuSsv3Sh7TjkHfiB7o52dhfXy29 2Cd/9hBTiBNKhEMJdIFM/C1X21+Ojbt3LKh4YWOTyenhAKgYb18IujHmteTzX0Iq8t2EAxIcupn w8vDJ4XPbFJOygNoyEJJz4GILYow41WCS28/JWo7tCIrew3Nran/zJtyWSu9ZEn4StQ== X-Received: by 2002:a62:3f81:: with SMTP id z1-v6mr21780404pfj.216.1526942733543; Mon, 21 May 2018 15:45:33 -0700 (PDT) X-Google-Smtp-Source: AB8JxZo7fwsCKQJ2y31kVfZSOCDzSDWlM0fSSr2j7LCva9QNv4LJBbDxvUeenJGltLxUevGjoA9f X-Received: by 2002:a62:3f81:: with SMTP id z1-v6mr21780385pfj.216.1526942732804; Mon, 21 May 2018 15:45:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526942732; cv=none; d=google.com; s=arc-20160816; b=062b5OVjHrB+OL4g1H7AmERRadmiL/nLbLoexpLQqZBzzAx630BGSCrC0q05ad0Tka CxsgV15os87Omm7X3f0LipHu0uFEml9BqwA6WIyrlyHpFNSkVdVNF5X9PSHpVyjZbkq9 ak/VMRCbAF/OkrnoowZbQYrzVuk+lHzrfXQVfLb+Qjvd6LSfInVUt/wSySKIcIXEjpxb w/97zpcbaJM0TfbjDFAVqfl3gKPOuTAWNxlLdDwKQXn6yVFadArCc91onHpW9P3QkNJJ nyrZtQDIhCv9gUbgd5PtfHyxPAOGhunahtmlsiNo9s/vRb3bYz0QfHMdioF94ytX0Upz wGjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject :arc-authentication-results; bh=km5uJUaSmUvOCL7Omk0nQPmVkxIUr0rf6x6m6Wg8SYg=; b=hKqUpPGk79O9bLxtxBDCYxPsJHdOko+QLN3z4nfjv8ToTp3eAkO+398ygv3sAF5HOE ziO0/PZXk3nDe8f5rk26mDIoVUdw1zgY91++O1OcHt+Jt7YmpcFKNvA5k46+8hxpwh3V qDiGF8CSCpNaFMFxd0utGYfIwZEI2u2W5vzVt8FXfQrE0O4lfpS+HGI+SAFNzsL07pEN ogVMmvKdHLAPyNFf5aCKHYrwlPYyI3muqryos7voqm2IWaQLZEchJ9eQR3G2+noySfLp r7stkXf+9lS2/VTRPq/4vCK4e4RBYwuS7HC0nfkdnZeEk8TMXT2sRbwec2tKBxibqv/1 syUA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=dan.j.williams@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga06.intel.com (mga06.intel.com. [134.134.136.31]) by mx.google.com with ESMTPS id h8-v6si15645208pls.502.2018.05.21.15.45.32 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 May 2018 15:45:32 -0700 (PDT) Received-SPF: pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.31 as permitted sender) client-ip=134.134.136.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=dan.j.williams@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 May 2018 15:45:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,427,1520924400"; d="scan'208";a="57248863" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by fmsmga001.fm.intel.com with ESMTP; 21 May 2018 15:45:31 -0700 Subject: [PATCH 4/5] mm, hmm: replace hmm_devmem_pages_create() with devm_memremap_pages() From: Dan Williams To: akpm@linux-foundation.org Cc: Christoph Hellwig , =?utf-8?b?SsOpcsO0bWU=?= Glisse , Logan Gunthorpe , linux-mm@kvack.org, linux-kernel@vger.kernel.org Date: Mon, 21 May 2018 15:35:34 -0700 Message-ID: <152694213486.5484.5340142369038375338.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <152694211402.5484.2277538346144115181.stgit@dwillia2-desk3.amr.corp.intel.com> References: <152694211402.5484.2277538346144115181.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-2-gc94f MIME-Version: 1.0 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 Commit e8d513483300 "memremap: change devm_memremap_pages interface to use struct dev_pagemap" refactored devm_memremap_pages() to allow a dev_pagemap instance to be supplied. Passing in a dev_pagemap interface simplifies the design of pgmap type drivers in that they can rely on container_of() to lookup any private data associated with the given dev_pagemap instance. In addition to the cleanups this also gives hmm users multi-order-radix improvements that arrived with commit ab1b597ee0e4 "mm, devm_memremap_pages: use multi-order radix for ZONE_DEVICE lookups" Cc: Christoph Hellwig Cc: "Jérôme Glisse" Cc: Logan Gunthorpe Signed-off-by: Dan Williams --- kernel/memremap.c | 29 +++++++- mm/hmm.c | 194 +++++++---------------------------------------------- 2 files changed, 49 insertions(+), 174 deletions(-) diff --git a/kernel/memremap.c b/kernel/memremap.c index 30d96be5a965..bece24419d6d 100644 --- a/kernel/memremap.c +++ b/kernel/memremap.c @@ -395,11 +395,32 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) goto err_pfn_remap; mem_hotplug_begin(); - 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, + + /* + * For device private memory we call add_pages() as we only need to + * allocate and initialize struct page for the device memory. More- + * over the device memory is un-accessible thus we do not want to + * create a linear mapping for the memory like arch_add_memory() + * would do. + * + * For all other device memory types, which are accessible by + * the CPU, we do want the linear mapping and thus use + * arch_add_memory(). + */ + if (pgmap->type == MEMORY_DEVICE_PRIVATE) { + error = add_pages(nid, align_start >> PAGE_SHIFT, + align_size >> PAGE_SHIFT, NULL, false); + } else { + struct zone *zone; + + error = arch_add_memory(nid, align_start, align_size, altmap, + false); + zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE]; + if (!error) + move_pfn_range_to_zone(zone, align_start >> PAGE_SHIFT, align_size >> PAGE_SHIFT, altmap); + } + mem_hotplug_done(); if (error) goto err_add_memory; diff --git a/mm/hmm.c b/mm/hmm.c index 8aa9d9fbb87b..a4162406067c 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -975,162 +975,6 @@ static void hmm_devmem_free(struct page *page, void *data) devmem->ops->free(devmem, page); } -static DEFINE_MUTEX(hmm_devmem_lock); -static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL); - -static void hmm_devmem_radix_release(struct resource *resource) -{ - resource_size_t key, align_start, align_size; - - align_start = resource->start & ~(PA_SECTION_SIZE - 1); - align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE); - - mutex_lock(&hmm_devmem_lock); - for (key = resource->start; - key <= resource->end; - key += PA_SECTION_SIZE) - radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT); - mutex_unlock(&hmm_devmem_lock); -} - -static void hmm_devmem_release(void *data) -{ - struct hmm_devmem *devmem = data; - struct device *dev = devmem->device; - struct resource *resource = devmem->resource; - struct dev_pagemap *pgmap = &devmem->pagemap; - unsigned long start_pfn, npages; - struct zone *zone; - struct page *page; - - if (pgmap->registered && percpu_ref_tryget_live(&devmem->ref)) { - dev_WARN(dev, "%s: page mapping is still live!\n", __func__); - percpu_ref_put(&devmem->ref); - } - - /* pages are dead and unused, undo the arch mapping */ - start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT; - npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT; - - page = pfn_to_page(start_pfn); - zone = page_zone(page); - - mem_hotplug_begin(); - if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) - __remove_pages(zone, start_pfn, npages, NULL); - else - arch_remove_memory(start_pfn << PAGE_SHIFT, - npages << PAGE_SHIFT, NULL); - mem_hotplug_done(); - - hmm_devmem_radix_release(resource); -} - -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; - unsigned long pfn; - - align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1); - align_size = ALIGN(devmem->resource->start + - resource_size(devmem->resource), - PA_SECTION_SIZE) - align_start; - - is_ram = region_intersects(align_start, align_size, - IORESOURCE_SYSTEM_RAM, - IORES_DESC_NONE); - if (is_ram == REGION_MIXED) { - WARN_ONCE(1, "%s attempted on mixed region %pr\n", - __func__, devmem->resource); - return -ENXIO; - } - if (is_ram == REGION_INTERSECTS) - return -ENXIO; - - if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY) - devmem->pagemap.type = MEMORY_DEVICE_PUBLIC; - else - devmem->pagemap.type = MEMORY_DEVICE_PRIVATE; - - devmem->pagemap.res = *devmem->resource; - devmem->pagemap.page_fault = hmm_devmem_fault; - devmem->pagemap.page_free = hmm_devmem_free; - devmem->pagemap.dev = devmem->device; - devmem->pagemap.ref = &devmem->ref; - devmem->pagemap.data = devmem; - - mutex_lock(&hmm_devmem_lock); - align_end = align_start + align_size - 1; - for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) { - struct hmm_devmem *dup; - - dup = radix_tree_lookup(&hmm_devmem_radix, - key >> PA_SECTION_SHIFT); - if (dup) { - dev_err(device, "%s: collides with mapping for %s\n", - __func__, dev_name(dup->device)); - mutex_unlock(&hmm_devmem_lock); - ret = -EBUSY; - goto error; - } - ret = radix_tree_insert(&hmm_devmem_radix, - key >> PA_SECTION_SHIFT, - devmem); - if (ret) { - dev_err(device, "%s: failed: %d\n", __func__, ret); - mutex_unlock(&hmm_devmem_lock); - goto error_radix; - } - } - mutex_unlock(&hmm_devmem_lock); - - nid = dev_to_node(device); - 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- - * over the device memory is un-accessible thus we do not want to - * create a linear mapping for the memory like arch_add_memory() - * would do. - * - * For device public memory, which is accesible by the CPU, we do - * 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); - 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); - mem_hotplug_done(); - - for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) { - struct page *page = pfn_to_page(pfn); - - page->pgmap = &devmem->pagemap; - } - return 0; - -error_add_memory: - untrack_pfn(NULL, PHYS_PFN(align_start), align_size); -error_radix: - hmm_devmem_radix_release(devmem->resource); -error: - return ret; -} - /* * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory * @@ -1154,6 +998,7 @@ struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops, { struct hmm_devmem *devmem; resource_size_t addr; + void *result; int ret; static_branch_enable(&device_private_key); @@ -1208,14 +1053,18 @@ struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops, devmem->pfn_last = devmem->pfn_first + (resource_size(devmem->resource) >> PAGE_SHIFT); - ret = hmm_devmem_pages_create(devmem); - if (ret) - return ERR_PTR(ret); + devmem->pagemap.type = MEMORY_DEVICE_PRIVATE; + devmem->pagemap.res = *devmem->resource; + devmem->pagemap.page_fault = hmm_devmem_fault; + devmem->pagemap.page_free = hmm_devmem_free; + devmem->pagemap.altmap_valid = false; + devmem->pagemap.registered = false; + devmem->pagemap.ref = &devmem->ref; + devmem->pagemap.data = devmem; - ret = devm_add_action_or_reset(device, hmm_devmem_release, devmem); - if (ret) - return ERR_PTR(ret); - devmem->pagemap.registered = true; + result = devm_memremap_pages(devmem->device, &devmem->pagemap); + if (IS_ERR(result)) + return result; ret = devm_add_action_or_reset(device, hmm_devmem_ref_kill, &devmem->ref); if (ret) @@ -1230,6 +1079,7 @@ struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops, struct resource *res) { struct hmm_devmem *devmem; + void *result; int ret; if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY) @@ -1262,14 +1112,18 @@ struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops, devmem->pfn_last = devmem->pfn_first + (resource_size(devmem->resource) >> PAGE_SHIFT); - ret = hmm_devmem_pages_create(devmem); - if (ret) - return ERR_PTR(ret); + devmem->pagemap.type = MEMORY_DEVICE_PUBLIC; + devmem->pagemap.res = *devmem->resource; + devmem->pagemap.page_fault = hmm_devmem_fault; + devmem->pagemap.page_free = hmm_devmem_free; + devmem->pagemap.altmap_valid = false; + devmem->pagemap.registered = false; + devmem->pagemap.ref = &devmem->ref; + devmem->pagemap.data = devmem; - ret = devm_add_action_or_reset(device, hmm_devmem_release, devmem); - if (ret) - return ERR_PTR(ret); - devmem->pagemap.registered = true; + result = devm_memremap_pages(devmem->device, &devmem->pagemap); + if (IS_ERR(result)) + return result; ret = devm_add_action_or_reset(device, hmm_devmem_ref_kill, &devmem->ref); if (ret)