Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754689AbdCPPOp (ORCPT ); Thu, 16 Mar 2017 11:14:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38824 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754579AbdCPPOl (ORCPT ); Thu, 16 Mar 2017 11:14:41 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com BB9A54DD43 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=jglisse@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com BB9A54DD43 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= To: akpm@linux-foundation.org, , linux-mm@kvack.org Cc: John Hubbard , Naoya Horiguchi , David Nellans , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Dan Williams , Ross Zwisler Subject: [HMM 03/16] mm/ZONE_DEVICE/free-page: callback when page is freed v3 Date: Thu, 16 Mar 2017 12:05:22 -0400 Message-Id: <1489680335-6594-4-git-send-email-jglisse@redhat.com> In-Reply-To: <1489680335-6594-1-git-send-email-jglisse@redhat.com> References: <1489680335-6594-1-git-send-email-jglisse@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 16 Mar 2017 15:03:52 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2508 Lines: 80 When a ZONE_DEVICE page refcount reach 1 it means it is free and nobody is holding a reference on it (only device to which the memory belong do). Add a callback and call it when that happen so device driver can implement their own free page management. Changes since v2: - Move page refcount in put_zone_device_page() Changes since v1: - Do not update devm_memremap_pages() to take extra argument Signed-off-by: Jérôme Glisse Cc: Dan Williams Cc: Ross Zwisler --- include/linux/memremap.h | 6 ++++++ kernel/memremap.c | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/linux/memremap.h b/include/linux/memremap.h index 29d2cca..3e04f58 100644 --- a/include/linux/memremap.h +++ b/include/linux/memremap.h @@ -35,19 +35,25 @@ static inline struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start) } #endif +typedef void (*dev_page_free_t)(struct page *page, void *data); + /** * struct dev_pagemap - metadata for ZONE_DEVICE mappings + * @page_free: free page callback when page refcount reach 1 * @altmap: pre-allocated/reserved memory for vmemmap allocations * @res: physical address range covered by @ref * @ref: reference count that pins the devm_memremap_pages() mapping * @dev: host device of the mapping for debug + * @data: privata data pointer for page_free * @flags: memory flags see MEMORY_* in memory_hotplug.h */ struct dev_pagemap { + dev_page_free_t page_free; struct vmem_altmap *altmap; const struct resource *res; struct percpu_ref *ref; struct device *dev; + void *data; int flags; }; diff --git a/kernel/memremap.c b/kernel/memremap.c index c821946..19df1f5 100644 --- a/kernel/memremap.c +++ b/kernel/memremap.c @@ -190,7 +190,14 @@ EXPORT_SYMBOL(get_zone_device_page); void put_zone_device_page(struct page *page) { - page_ref_dec(page); + int count = page_ref_dec_return(page); + + /* + * If refcount is 1 then page is freed and refcount is stable as nobody + * holds a reference on the page. + */ + if (page->pgmap->page_free && count == 1) + page->pgmap->page_free(page, page->pgmap->data); put_dev_pagemap(page->pgmap); } @@ -331,6 +338,8 @@ void *devm_memremap_pages(struct device *dev, struct resource *res, pgmap->ref = ref; pgmap->res = &page_map->res; pgmap->flags = MEMORY_DEVICE; + pgmap->page_free = NULL; + pgmap->data = NULL; mutex_lock(&pgmap_lock); error = 0; -- 2.4.11