From nobody Wed Dec 17 05:55:59 2025 Received: from out-186.mta0.migadu.com (out-186.mta0.migadu.com [91.218.175.186]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15EF18248F for ; Wed, 8 May 2024 11:37:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.186 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715168277; cv=none; b=JHIoaUaBYn+goLvQlqwcJebgvo5sWZRXqwCo7eDrvNa9fXwSKEa4Kd3NlaFdaGTO0+ub+6ajNksoz0P0yTJPbAHUfK5EnUriKVm3WHhocapKyt3u3cs0u9Y8SBdIemAck+yFjd+F3EAGnIB1GOMapGxxt37UBqoGzUcAMaJQe3M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715168277; c=relaxed/simple; bh=D9LRp9I9bsmEz1efkArPgmxaWlXB5jt0gur2nsp9qF4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=evK6r7VriC1177rf2GK5R8hBsptY0mEtxD5yJxkpWe0aZ0ixDyFZLlq1JhySB2sbRA6IZigXP/CM7IoR8sHhBJ33YbMXAHUSbqYwllg/pzdxoGBR4BVm7VDTG3OeMwD4uDjy9QmJQmBmgRQtK+oMNUsiwNtwUX/V+Jx7zYo5quw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=SqGZ8FQl; arc=none smtp.client-ip=91.218.175.186 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="SqGZ8FQl" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1715168272; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WIIjmNAElQXsSD8PwI0lQYfcP7r/nQH9fPZcjkqWsMg=; b=SqGZ8FQlOHa+xVPFT5h/Hx5XD//A7WpJyKilXRNPlWE33c7fr0uqwMYQA4fQG/nwxpkHVz shXfsXlr5ZQZ1+G4VeYsUc5DOisjK3xLwwK5o6WplPtYysflyUNnQHTE1Hafs1styy0FH+ g10gnOP4bTG5XVNzuHhTjyHHxbwcYZ4= From: Chengming Zhou Date: Wed, 08 May 2024 19:35:41 +0800 Subject: [PATCH 1/2] mm/ksm: re-search unstable tree when tree_rmap_item is stale Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240508-b4-ksm-unstable-insert-v1-1-631cdbc2b77f@linux.dev> References: <20240508-b4-ksm-unstable-insert-v1-0-631cdbc2b77f@linux.dev> In-Reply-To: <20240508-b4-ksm-unstable-insert-v1-0-631cdbc2b77f@linux.dev> To: Andrew Morton , David Hildenbrand , Andrea Arcangeli , Stefan Roesch , Hugh Dickins Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com, Chengming Zhou X-Developer-Signature: v=1; a=ed25519-sha256; t=1715168266; l=1516; i=chengming.zhou@linux.dev; s=20240508; h=from:subject:message-id; bh=jA3W7oMJI30/U7Ia4BVOJNzApOh6jOjCo9MOgiNhdb0=; b=12jMhHwzT9u21DZ+cUWU0t74BSNgENYB7tSujAwgHFh3vUTNwYzyPBwg4lmtSu89lyWdO68id ISPO0DpTBD0DMKJkVzXvWnlRoCrekIWEAqHZkIYtzv0abUL6H7Ar0Wa X-Developer-Key: i=chengming.zhou@linux.dev; a=ed25519; pk=kx40VUetZeR6MuiqrM7kPCcGakk1md0Az5qHwb6gBdU= X-Migadu-Flow: FLOW_OUT From: Chengming Zhou Now unstable_tree_search_insert() will return NULL if encounter a stale tree_rmap_item, in which case get_mergeable_page() return NULL. More reasonable handling is to remove stale rmap_item out of unstable tree and re-search from root. That stale rmap_item will be reclaimed when next time of scan. So we can insert our rmap_item successfully instead of returning NULL. Signed-off-by: Chengming Zhou --- mm/ksm.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index e1034bf1c937..66219983eb3a 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2196,12 +2196,14 @@ struct ksm_rmap_item *unstable_tree_search_insert(s= truct ksm_rmap_item *rmap_ite { struct rb_node **new; struct rb_root *root; - struct rb_node *parent =3D NULL; + struct rb_node *parent; int nid; =20 nid =3D get_kpfn_nid(page_to_pfn(page)); root =3D root_unstable_tree + nid; +again: new =3D &root->rb_node; + parent =3D NULL; =20 while (*new) { struct ksm_rmap_item *tree_rmap_item; @@ -2211,8 +2213,10 @@ struct ksm_rmap_item *unstable_tree_search_insert(st= ruct ksm_rmap_item *rmap_ite cond_resched(); tree_rmap_item =3D rb_entry(*new, struct ksm_rmap_item, node); tree_page =3D get_mergeable_page(tree_rmap_item); - if (!tree_page) - return NULL; + if (!tree_page) { + remove_rmap_item_from_tree(tree_rmap_item); + goto again; + } =20 /* * Don't substitute a ksm page for a forked page. --=20 2.45.0 From nobody Wed Dec 17 05:55:59 2025 Received: from out-187.mta0.migadu.com (out-187.mta0.migadu.com [91.218.175.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34D1B82D64 for ; Wed, 8 May 2024 11:37:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715168279; cv=none; b=JIO1VRbwyFolmERoda849n3Jms5NyrCY423cn4BQVoXmMgdRl/H+bjuWq1zFkBOg8DIcnuZ8vtZ5MsiWHMjyZvzl0J0Q6GEl4nK5lX1T5ayitkVtKpdtrdWaQdfSQg7yW+/NVNeGm7crJoW7Bwzz7L1+cx49/fH3pR2a2EdpBbU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715168279; c=relaxed/simple; bh=2EmaZSwe/HzmbILQH+QzgMGxCqa7nYLUApNs+LSZ+CE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Sc6a8sONJR+MI/ip0Rmmidh5Qp8ITlTK4dys0CwPfjcCLNhbwl/T+dBlFnMKxSGAvxsBClZxzFvOJjbg48DpCmoFRGLCg1umhG/oScQg32B+nqx0x1t3UYXyUP8uIyYgxYBHdhsPOfJHye89lOWXsvRj7lvY8rCVyKK/H0B1OYM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=sGyqLmc/; arc=none smtp.client-ip=91.218.175.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="sGyqLmc/" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1715168276; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Rv7ODi3h9Zv4dLUYCuePOTA6Hl0snrx6MrZvSACKHJk=; b=sGyqLmc/ob4XobogabL2rrSlIMRKjg02+S9SDgwIWAeXY/S/Y4fNO+7AdahFYG1gt5nQMc FeOWgfKlTl/AWHPM5vJ5XSyb188vtNSzd5Gu2Awtw/JK01WBl1bAn4LwP2W6jnFzFQpCzV 8X6rdSZp8/58VgZd6vhLG0JiDVXmkYw= From: Chengming Zhou Date: Wed, 08 May 2024 19:35:42 +0800 Subject: [PATCH 2/2] mm/ksm: flush out migrated rmap_item to insert our rmap_item Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240508-b4-ksm-unstable-insert-v1-2-631cdbc2b77f@linux.dev> References: <20240508-b4-ksm-unstable-insert-v1-0-631cdbc2b77f@linux.dev> In-Reply-To: <20240508-b4-ksm-unstable-insert-v1-0-631cdbc2b77f@linux.dev> To: Andrew Morton , David Hildenbrand , Andrea Arcangeli , Stefan Roesch , Hugh Dickins Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com, Chengming Zhou X-Developer-Signature: v=1; a=ed25519-sha256; t=1715168266; l=1806; i=chengming.zhou@linux.dev; s=20240508; h=from:subject:message-id; bh=DfKRZMh+MWM/THv2xlN2nkqm18FgyrnKseZDpyL0ZnQ=; b=6PGv6hC8/MH+G6VHFjqPstHrk9XZsLDwOyKos4BGRZ6lxZ4an2xDm65nTC4XcJaq5kAVlo4W2 KhIGkkMPYrPAoNo3pgoyAzIgk8bBd2a0sC8o7yToiFqxOUBz2+i6ZkG X-Developer-Key: i=chengming.zhou@linux.dev; a=ed25519; pk=kx40VUetZeR6MuiqrM7kPCcGakk1md0Az5qHwb6gBdU= X-Migadu-Flow: FLOW_OUT From: Chengming Zhou If tree_page has been migrated to another NUMA node and across_nodes disabled, flush it out immediately and it will be put in the right unstable tree when next time. The good point is that we can retry to insert our rmap_item successfully to increase the merge possibility, and we don't need to bother to memcmp_pages() in this case. Signed-off-by: Chengming Zhou --- mm/ksm.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 66219983eb3a..b840fb55e1f4 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2226,6 +2226,18 @@ struct ksm_rmap_item *unstable_tree_search_insert(st= ruct ksm_rmap_item *rmap_ite return NULL; } =20 + /* + * If tree_page has been migrated to another NUMA node and + * across_nodes disabled, flush it out here and it will be + * put in the right unstable tree next time. So we can retry + * to insert our rmap_item successfully. + */ + if (!ksm_merge_across_nodes && + page_to_nid(tree_page) !=3D nid) { + remove_rmap_item_from_tree(tree_rmap_item); + goto again; + } + ret =3D memcmp_pages(page, tree_page); =20 parent =3D *new; @@ -2235,15 +2247,6 @@ struct ksm_rmap_item *unstable_tree_search_insert(st= ruct ksm_rmap_item *rmap_ite } else if (ret > 0) { put_page(tree_page); new =3D &parent->rb_right; - } else if (!ksm_merge_across_nodes && - page_to_nid(tree_page) !=3D nid) { - /* - * If tree_page has been migrated to another NUMA node, - * it will be flushed out and put in the right unstable - * tree next time: only merge with it when across_nodes. - */ - put_page(tree_page); - return NULL; } else { *tree_pagep =3D tree_page; return tree_rmap_item; --=20 2.45.0