Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp3628095imm; Mon, 10 Sep 2018 22:37:01 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdaj4JXwG6txz6cv9sb6AVi68aMDw4gPr9z1A5uFXqqvkNOjB0lLmtBVqMLeaGDEV4XadtVR X-Received: by 2002:a63:9c19:: with SMTP id f25-v6mr26671152pge.447.1536644221608; Mon, 10 Sep 2018 22:37:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536644221; cv=none; d=google.com; s=arc-20160816; b=xobgyIlfHRVDobfvH483brZXM6zgiNl4qLo1owz9UZi2c0CTOsBbTMAjKk8FDfFxUO o5Vyic8Cc9jUzfk+0H72u/axPL5qnb8GDnrtbzAa03SEnsvlxudwSXzyzAXQy9EgU4Vw 8JL6ICq8NZOYLNS3IhYcJTfpeA6r+QQcfYd72BZu6/sSH6FTUqe+7V4tQum0a1QPBSfx RvoufUeMiSgOk6qNJnRH9vsR2V2J0+WvsmsdfYBEMrTBtVsD4iMBaiG3Sgo2qqKyYgsf tiBtFDtPfDTmBykxXj0N+rJdXfwLAIUcrs9AoC4mwYaoqwNwNY46FaSvCTl0ZhASE93c kMhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=bzauRD9o+W09eWXP1DarQ026TVo0Oh5DmnYxjFzEkFI=; b=b0tvjRFUgIUr+CJeUpGDgK3VMqbdI/Sf+jxjmn3l0eAyDyYA0Dtgv4GcCRqpC7B8h3 nmrpT7FLIhvCqLFXKJDnmPWWIo8+AQvBCox0Co4EC2wXF+fQZHHz5Pf4Slti7mUffde9 IJngZTc7RW/0JCCmtN3oycycPOf6NtX6TVWX01Jla88BGCT+DnSnlH7JtUV3tSMRGWC0 IOGVAzPu3luwmve1N01wNGpAEJkVTV2q8yCtfzmj+in9XQcxySqU+L9lpahtrdF6f0QS iE90k0TKjhvYP8wVeNT6zvgoZof+Q+TXAwhH1vp9VfvxdxnCV1o9EgoG1Qg67B2lB/XY yxbQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q17-v6si19657948pfi.183.2018.09.10.22.36.46; Mon, 10 Sep 2018 22:37:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727413AbeIKKeC (ORCPT + 99 others); Tue, 11 Sep 2018 06:34:02 -0400 Received: from mga17.intel.com ([192.55.52.151]:62537 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726426AbeIKKeC (ORCPT ); Tue, 11 Sep 2018 06:34:02 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Sep 2018 22:36:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,359,1531810800"; d="scan'208";a="262426326" Received: from aaronlu.sh.intel.com ([10.239.159.44]) by fmsmga006.fm.intel.com with ESMTP; 10 Sep 2018 22:36:26 -0700 From: Aaron Lu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Dave Hansen , Michal Hocko , Vlastimil Babka , Mel Gorman , Matthew Wilcox , Daniel Jordan , Tariq Toukan , Yosef Lev , Jesper Dangaard Brouer Subject: [RFC PATCH 3/9] mm: introduce smp_list_splice to prepare for concurrent LRU adds Date: Tue, 11 Sep 2018 13:36:10 +0800 Message-Id: <20180911053616.6894-4-aaron.lu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180911053616.6894-1-aaron.lu@intel.com> References: <20180911053616.6894-1-aaron.lu@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daniel Jordan Now that we splice a local list onto the LRU, prepare for multiple tasks doing this concurrently by adding a variant of the kernel's list splicing API, list_splice, that's designed to work with multiple tasks. Although there is naturally less parallelism to be gained from locking the LRU head this way, the main benefit of doing this is to allow removals to happen concurrently. The way lru_lock is today, an add needlessly blocks removal of any page but the first in the LRU. For now, hold lru_lock as writer to serialize the adds to ensure the function is correct for a single thread at a time. Yosef Lev came up with this algorithm. [aaronlu: drop LRU related code, keep only list related code] Suggested-by: Yosef Lev Signed-off-by: Daniel Jordan --- include/linux/list.h | 1 + lib/list.c | 60 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/include/linux/list.h b/include/linux/list.h index 0fd9c87dd14b..5f203fb55939 100644 --- a/include/linux/list.h +++ b/include/linux/list.h @@ -48,6 +48,7 @@ static inline bool __list_del_entry_valid(struct list_head *entry) #endif extern void smp_list_del(struct list_head *entry); +extern void smp_list_splice(struct list_head *list, struct list_head *head); /* * Insert a new entry between two known consecutive entries. diff --git a/lib/list.c b/lib/list.c index 4d0949ea1a09..104faa144abf 100644 --- a/lib/list.c +++ b/lib/list.c @@ -10,17 +10,18 @@ #include /* - * smp_list_del is a variant of list_del that allows concurrent list removals - * under certain assumptions. The idea is to get away from overly coarse - * synchronization, such as using a lock to guard an entire list, which - * serializes all operations even though those operations might be happening on - * disjoint parts. + * smp_list_del and smp_list_splice are variants of list_del and list_splice, + * respectively, that allow concurrent list operations under certain + * assumptions. The idea is to get away from overly coarse synchronization, + * such as using a lock to guard an entire list, which serializes all + * operations even though those operations might be happening on disjoint + * parts. * * If you want to use other functions from the list API concurrently, * additional synchronization may be necessary. For example, you could use a * rwlock as a two-mode lock, where readers use the lock in shared mode and are - * allowed to call smp_list_del concurrently, and writers use the lock in - * exclusive mode and are allowed to use all list operations. + * allowed to call smp_list_* functions concurrently, and writers use the lock + * in exclusive mode and are allowed to use all list operations. */ /** @@ -156,3 +157,48 @@ void smp_list_del(struct list_head *entry) entry->next = LIST_POISON1; entry->prev = LIST_POISON2; } + +/** + * smp_list_splice - thread-safe splice of two lists + * @list: the new list to add + * @head: the place to add it in the first list + * + * Safely handles concurrent smp_list_splice operations onto the same list head + * and concurrent smp_list_del operations of any list entry except @head. + * Assumes that @head cannot be removed. + */ +void smp_list_splice(struct list_head *list, struct list_head *head) +{ + struct list_head *first = list->next; + struct list_head *last = list->prev; + struct list_head *succ; + + /* + * Lock the front of @head by replacing its next pointer with NULL. + * Should another thread be adding to the front, wait until it's done. + */ + succ = READ_ONCE(head->next); + while (succ == NULL || cmpxchg(&head->next, succ, NULL) != succ) { + cpu_relax(); + succ = READ_ONCE(head->next); + } + + first->prev = head; + last->next = succ; + + /* + * It is safe to write to succ, head's successor, because locking head + * prevents succ from being removed in smp_list_del. + */ + succ->prev = last; + + /* + * Pairs with the implied full barrier before the cmpxchg above. + * Ensures the write that unlocks the head is seen last to avoid list + * corruption. + */ + smp_wmb(); + + /* Simultaneously complete the splice and unlock the head node. */ + WRITE_ONCE(head->next, first); +} -- 2.17.1