[Devel] [PATCH rh7 v3 10/10] tcache: Move add/sub out of pni->lock

Kirill Tkhai ktkhai at virtuozzo.com
Wed Aug 16 17:52:38 MSK 2017


This minimizes number of operations happening under pni->lock.
Note, that we do add before linking to the list, so parallel
shrink does not make nr_pages negative.

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 mm/tcache.c |   12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/mm/tcache.c b/mm/tcache.c
index 5faa39009ec..d1a2c53e11a 100644
--- a/mm/tcache.c
+++ b/mm/tcache.c
@@ -261,8 +261,9 @@ static void tcache_lru_add(struct tcache_pool *pool, struct page *page)
 	struct tcache_nodeinfo *ni = &tcache_nodeinfo[nid];
 	struct tcache_pool_nodeinfo *pni = &pool->nodeinfo[nid];
 
-	spin_lock(&pni->lock);
 	atomic_long_inc(&ni->nr_pages);
+
+	spin_lock(&pni->lock);
 	pni->nr_pages++;
 	list_add_tail(&page->lru, &pni->lru);
 
@@ -300,6 +301,7 @@ static void tcache_lru_del(struct tcache_pool *pool, struct page *page,
 	int nid = page_to_nid(page);
 	struct tcache_nodeinfo *ni = &tcache_nodeinfo[nid];
 	struct tcache_pool_nodeinfo *pni = &pool->nodeinfo[nid];
+	bool deleted = false;
 
 	spin_lock(&pni->lock);
 
@@ -308,7 +310,7 @@ static void tcache_lru_del(struct tcache_pool *pool, struct page *page,
 		goto out;
 
 	__tcache_lru_del(pni, page);
-	atomic_long_dec(&ni->nr_pages);
+	deleted = true;
 
 	if (reused)
 		pni->recent_gets++;
@@ -323,6 +325,8 @@ static void tcache_lru_del(struct tcache_pool *pool, struct page *page,
 	}
 out:
 	spin_unlock(&pni->lock);
+	if (deleted)
+		atomic_long_dec(&ni->nr_pages);
 }
 
 static int tcache_create_pool(void)
@@ -1071,8 +1075,6 @@ tcache_lru_isolate(int nid, struct page **pages, int nr_to_isolate)
 	if (!nr_isolated)
 		goto unlock;
 
-	atomic_long_sub(nr_isolated, &ni->nr_pages);
-
 	if (!RB_EMPTY_NODE(rbn) || !list_empty(&pni->lru)) {
 		spin_lock(&ni->lock);
 		if (!RB_EMPTY_NODE(rbn))
@@ -1088,6 +1090,8 @@ tcache_lru_isolate(int nid, struct page **pages, int nr_to_isolate)
 	spin_unlock_irq(&pni->lock);
 	tcache_put_pool(pni->pool);
 out:
+	if (nr_isolated)
+		atomic_long_sub(nr_isolated, &ni->nr_pages);
 	return nr_isolated;
 }
 



More information about the Devel mailing list