Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3697846imm; Mon, 15 Oct 2018 02:35:39 -0700 (PDT) X-Google-Smtp-Source: ACcGV615PPtWN570gR7ydpwpOSzWRzBSYZReT+KwEyOoWphmySDNuDqQCtGeFoBiqz6SuDdbjcdY X-Received: by 2002:a17:902:7408:: with SMTP id g8-v6mr15989289pll.221.1539596139757; Mon, 15 Oct 2018 02:35:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539596139; cv=none; d=google.com; s=arc-20160816; b=xYVshQmt/K26fYcFqdjk0comNsZIqisH60TxyhkVytI6Wd0VI7HDxilevZ/J53UeBY 79cL1Rt+2UOxiBPa1yZ+gpymiX18GOA76dhfrREygBa8dbuyGMJ9rrKg/oK/zzrnW2m+ rsFTBVEo+D0XoDdxSrMmZakMVO6Ca8Ebm0ZtrbETUkADuDFOzfVC2RHCHdrjdMYgG9Ez 7/N0gk5dtO8JuVqkOi6P3+hn4wc6CFrmlJ+xuK5JLG5cpeb3hz+SMYr5iYqDncKCRqHN bmM365N8A3bZOLzoqTywp2ykDi+LVUnEjgcAR0J/GO8dtsMV9Uv7TqzDKwDmcXCAnYJt hiiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:message-id:date:to:from:subject; bh=qwA2QFBkazDsm30mx22asNxyewB8scLwPKUvOgqfZpM=; b=NKIiMhCGoQmO3F8GL4mVXYPTqUfK7eaxgv2tqeexnEKhEI8VwV+hCVFZNgY5GPnXQK hfF6CEhoUs0wmBxPgiGi5HlKn3LauzNfyf5+BQ2INej1d/0TeKWcnP1uRM28s48WzgU9 AL+GJ94wG+igDuLKDaDKh80CU23uJhjagnkaA8iKbqEJ3H9GPuw73pYxJPgUlsC5Y9BM zP4t3QChHlvbL4CcfEWbQcEZ/+OQJHbGcZ955tbcQhStkK9tP/uH+EMFBHwZKAQDEh1i NL1xfVOfESfevg97u8A6HZWxyKzw6lgZYZQftDwNDfQ8BqcK9bHt9kdXpzpokxopi4Je /IIA== 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=virtuozzo.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a127-v6si10766936pfb.24.2018.10.15.02.35.24; Mon, 15 Oct 2018 02:35:39 -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=virtuozzo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726868AbeJORSe (ORCPT + 99 others); Mon, 15 Oct 2018 13:18:34 -0400 Received: from relay.sw.ru ([185.231.240.75]:43214 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726421AbeJORSd (ORCPT ); Mon, 15 Oct 2018 13:18:33 -0400 Received: from [172.16.25.169] (helo=localhost.localdomain) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1gBzFp-0002gT-NM; Mon, 15 Oct 2018 12:33:37 +0300 Subject: [PATCH RFC v2] ksm: Assist buddy allocator to assemble 1-order pages From: Kirill Tkhai To: hughd@google.com, aarcange@redhat.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, andriy.shevchenko@linux.intel.com, mhocko@suse.com, rppt@linux.vnet.ibm.com, imbrenda@linux.vnet.ibm.com, corbet@lwn.net, ndesaulniers@google.com, ktkhai@virtuozzo.com, dave.jiang@intel.com, jglisse@redhat.com, jia.he@hxt-semitech.com, paulmck@linux.vnet.ibm.com, colin.king@canonical.com, jiang.biao2@zte.com.cn, linux-mm@kvack.org, linux-kernel@vger.kernel.org Date: Mon, 15 Oct 2018 12:33:36 +0300 Message-ID: <153959597844.26723.5798112367236156151.stgit@localhost.localdomain> User-Agent: StGit/0.18 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org v2: Style improvements try_to_merge_two_pages() merges two pages, one of them is a page of currently scanned mm, the second is a page with identical hash from unstable tree. Currently, we merge the page from unstable tree into the first one, and then free it. The idea of this patch is to prefer freeing that page of them, which has a free neighbour (i.e., neighbour with zero page_count()). This allows buddy allocator to assemble at least 1-order set from the freed page and its neighbour; this is a kind of cheep passive compaction. AFAIK, 1-order pages set consists of pages with PFNs [2n, 2n+1] (odd, even), so the neighbour's pfn is calculated via XOR with 1. We check the result pfn is valid and its page_count(), and prefer merging into @tree_page if neighbour's usage count is zero. There a is small difference with current behavior in case of error path. In case of the second try_to_merge_with_ksm_page() is failed, we return from try_to_merge_two_pages() with @tree_page removed from unstable tree. It does not seem to matter, but if we do not want a change at all, it's not a problem to move remove_rmap_item_from_tree() from try_to_merge_with_ksm_page() to its callers. Signed-off-by: Kirill Tkhai --- mm/ksm.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index 5b0894b45ee5..005508c86d0a 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1321,6 +1321,22 @@ static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item, { int err; + if (IS_ENABLED(CONFIG_COMPACTION)) { + unsigned long pfn; + + /* + * Find neighbour of @page containing 1-order pair + * in buddy-allocator and check whether it is free. + * If it is so, try to use @tree_page as ksm page + * and to free @page. + */ + pfn = page_to_pfn(page) ^ 1; + if (pfn_valid(pfn) && page_count(pfn_to_page(pfn)) == 0) { + swap(rmap_item, tree_rmap_item); + swap(page, tree_page); + } + } + err = try_to_merge_with_ksm_page(rmap_item, page, NULL); if (!err) { err = try_to_merge_with_ksm_page(tree_rmap_item,