[RFC 33/43] PKRAM: atomically add and remove link pages

From: Anthony Yznaga
Date: Wed May 06 2020 - 20:47:13 EST


Add and remove pkram_link pages from a pkram_obj atomically to prepare
for multithreading.

Signed-off-by: Anthony Yznaga <anthony.yznaga@xxxxxxxxxx>
---
mm/pkram.c | 27 ++++++++++++++++++---------
1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/mm/pkram.c b/mm/pkram.c
index 5f4e4d12865f..042c14dedc25 100644
--- a/mm/pkram.c
+++ b/mm/pkram.c
@@ -551,22 +551,31 @@ static void pkram_truncate(void)

static void pkram_add_link(struct pkram_link *link, struct pkram_obj *obj)
{
- link->link_pfn = obj->link_pfn;
- obj->link_pfn = page_to_pfn(virt_to_page(link));
+ __u64 link_pfn = page_to_pfn(virt_to_page(link));
+ __u64 *head = &obj->link_pfn;
+
+ do {
+ link->link_pfn = *head;
+ } while (cmpxchg64(head, link->link_pfn, link_pfn) != link->link_pfn);
}

static struct pkram_link *pkram_remove_link(struct pkram_obj *obj)
{
struct pkram_link *current_link;
+ __u64 *head = &obj->link_pfn;
+ __u64 head_pfn = *head;
+
+ while (head_pfn) {
+ current_link = pfn_to_kaddr(head_pfn);
+ if (cmpxchg64(head, head_pfn, current_link->link_pfn) == head_pfn) {
+ current_link->link_pfn = 0;
+ return current_link;
+ }

- if (!obj->link_pfn)
- return NULL;
-
- current_link = pfn_to_kaddr(obj->link_pfn);
- obj->link_pfn = current_link->link_pfn;
- current_link->link_pfn = 0;
+ head_pfn = *head;
+ }

- return current_link;
+ return NULL;
}

static void pkram_stream_init(struct pkram_stream *ps,
--
2.13.3