Skip to content

Commit 7b2d55d

Browse files
Jérôme Glissetorvalds
authored andcommitted
mm/ZONE_DEVICE: special case put_page() for device private pages
A ZONE_DEVICE page that reach a refcount of 1 is free ie no longer have any user. For device private pages this is important to catch and thus we need to special case put_page() for this. Link: http://lkml.kernel.org/r/20170817000548.32038-9-jglisse@redhat.com Signed-off-by: Jérôme Glisse <jglisse@redhat.com> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Ross Zwisler <ross.zwisler@linux.intel.com> Cc: Aneesh Kumar <aneesh.kumar@linux.vnet.ibm.com> Cc: Balbir Singh <bsingharora@gmail.com> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: David Nellans <dnellans@nvidia.com> Cc: Evgeny Baskakov <ebaskakov@nvidia.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: John Hubbard <jhubbard@nvidia.com> Cc: Mark Hairgrove <mhairgrove@nvidia.com> Cc: Michal Hocko <mhocko@kernel.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Sherry Cheung <SCheung@nvidia.com> Cc: Subhash Gutti <sgutti@nvidia.com> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Cc: Bob Liu <liubo95@huawei.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5042db4 commit 7b2d55d

4 files changed

Lines changed: 67 additions & 10 deletions

File tree

include/linux/memremap.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ struct dev_pagemap {
126126
void *devm_memremap_pages(struct device *dev, struct resource *res,
127127
struct percpu_ref *ref, struct vmem_altmap *altmap);
128128
struct dev_pagemap *find_dev_pagemap(resource_size_t phys);
129+
130+
static inline bool is_zone_device_page(const struct page *page);
131+
132+
static inline bool is_device_private_page(const struct page *page)
133+
{
134+
return is_zone_device_page(page) &&
135+
page->pgmap->type == MEMORY_DEVICE_PRIVATE;
136+
}
129137
#else
130138
static inline void *devm_memremap_pages(struct device *dev,
131139
struct resource *res, struct percpu_ref *ref,
@@ -144,6 +152,11 @@ static inline struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
144152
{
145153
return NULL;
146154
}
155+
156+
static inline bool is_device_private_page(const struct page *page)
157+
{
158+
return false;
159+
}
147160
#endif
148161

149162
/**

include/linux/mm.h

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/page_ext.h>
2424
#include <linux/err.h>
2525
#include <linux/page_ref.h>
26+
#include <linux/memremap.h>
2627

2728
struct mempolicy;
2829
struct anon_vma;
@@ -792,25 +793,25 @@ static inline bool is_zone_device_page(const struct page *page)
792793
{
793794
return page_zonenum(page) == ZONE_DEVICE;
794795
}
795-
796-
static inline bool is_device_private_page(const struct page *page)
797-
{
798-
/* See MEMORY_DEVICE_PRIVATE in include/linux/memory_hotplug.h */
799-
return ((page_zonenum(page) == ZONE_DEVICE) &&
800-
(page->pgmap->type == MEMORY_DEVICE_PRIVATE));
801-
}
802796
#else
803797
static inline bool is_zone_device_page(const struct page *page)
804798
{
805799
return false;
806800
}
801+
#endif
807802

808-
static inline bool is_device_private_page(const struct page *page)
803+
#ifdef CONFIG_DEVICE_PRIVATE
804+
void put_zone_device_private_page(struct page *page);
805+
#else
806+
static inline void put_zone_device_private_page(struct page *page)
809807
{
810-
return false;
811808
}
812809
#endif
813810

811+
static inline bool is_device_private_page(const struct page *page);
812+
813+
DECLARE_STATIC_KEY_FALSE(device_private_key);
814+
814815
static inline void get_page(struct page *page)
815816
{
816817
page = compound_head(page);
@@ -826,6 +827,18 @@ static inline void put_page(struct page *page)
826827
{
827828
page = compound_head(page);
828829

830+
/*
831+
* For private device pages we need to catch refcount transition from
832+
* 2 to 1, when refcount reach one it means the private device page is
833+
* free and we need to inform the device driver through callback. See
834+
* include/linux/memremap.h and HMM for details.
835+
*/
836+
if (static_branch_unlikely(&device_private_key) &&
837+
unlikely(is_device_private_page(page))) {
838+
put_zone_device_private_page(page);
839+
return;
840+
}
841+
829842
if (put_page_testzero(page))
830843
__put_page(page);
831844
}

kernel/memremap.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
* General Public License for more details.
1212
*/
1313
#include <linux/radix-tree.h>
14-
#include <linux/memremap.h>
1514
#include <linux/device.h>
1615
#include <linux/types.h>
1716
#include <linux/pfn_t.h>
@@ -500,3 +499,27 @@ struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
500499
return pgmap ? pgmap->altmap : NULL;
501500
}
502501
#endif /* CONFIG_ZONE_DEVICE */
502+
503+
504+
#ifdef CONFIG_DEVICE_PRIVATE
505+
void put_zone_device_private_page(struct page *page)
506+
{
507+
int count = page_ref_dec_return(page);
508+
509+
/*
510+
* If refcount is 1 then page is freed and refcount is stable as nobody
511+
* holds a reference on the page.
512+
*/
513+
if (count == 1) {
514+
/* Clear Active bit in case of parallel mark_page_accessed */
515+
__ClearPageActive(page);
516+
__ClearPageWaiters(page);
517+
518+
page->mapping = NULL;
519+
520+
page->pgmap->page_free(page, page->pgmap->data);
521+
} else if (!count)
522+
__put_page(page);
523+
}
524+
EXPORT_SYMBOL(put_zone_device_private_page);
525+
#endif /* CONFIG_DEVICE_PRIVATE */

mm/hmm.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@
2525
#include <linux/sched.h>
2626
#include <linux/swapops.h>
2727
#include <linux/hugetlb.h>
28+
#include <linux/jump_label.h>
2829
#include <linux/mmu_notifier.h>
2930

3031

32+
/*
33+
* Device private memory see HMM (Documentation/vm/hmm.txt) or hmm.h
34+
*/
35+
DEFINE_STATIC_KEY_FALSE(device_private_key);
36+
EXPORT_SYMBOL(device_private_key);
37+
38+
3139
#ifdef CONFIG_HMM
3240
static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
3341

0 commit comments

Comments
 (0)