From patchwork Thu Oct 11 10:52:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kirill Tkhai X-Patchwork-Id: 10636449 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 698B41508 for ; Thu, 11 Oct 2018 10:53:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 51ABE2B2AA for ; Thu, 11 Oct 2018 10:53:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4434E2B2AC; Thu, 11 Oct 2018 10:53:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 45DD62B2AA for ; Thu, 11 Oct 2018 10:52:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2AA0F6B0003; Thu, 11 Oct 2018 06:52:58 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 258B66B0005; Thu, 11 Oct 2018 06:52:58 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 16FA06B0006; Thu, 11 Oct 2018 06:52:58 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) by kanga.kvack.org (Postfix) with ESMTP id 974DA6B0003 for ; Thu, 11 Oct 2018 06:52:57 -0400 (EDT) Received: by mail-lf1-f70.google.com with SMTP id a25-v6so1147464lfi.9 for ; Thu, 11 Oct 2018 03:52:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:subject:from :to:date:message-id:user-agent:mime-version :content-transfer-encoding; bh=2esULjDgiDQTzDZzabv7NDzsk+NqyZ/STBqRI+TWRPA=; b=j8/BrKsBUPS8Q1JBg+3nyO97NzsRM5+sQOEVx0PIqCmQYGFPMpmKIS/uJdlCOAIwNh Q998scJv+ol6PgQ5h8p3i8eEqp/awIccbztatTIBeHhd74yMLwK5QWc8f6yJOdf7Ygzm 20fCLj0mJquRw3mrvUz5Tny0HFOYra1k03oHmsvktlzw2DAVS6mndTWBh5Uq4/t2PXaL ipQ1+93eyYoH0baQ8yvSGVslKPD4GOroAvaJaiUCQiTvmHHSbNQDoBDY5Qn+pctVOwSm xDN2j/WCVPXq52QTRnVk1is8+CDEiPlhw1BWa9bjEgcBZIb2CLf4eItIoJH+3tAlbOdn nMjA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of ktkhai@virtuozzo.com designates 185.231.240.75 as permitted sender) smtp.mailfrom=ktkhai@virtuozzo.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com X-Gm-Message-State: ABuFfojlwJDmxD/pDGy8YIEyetn3/h2aVqyj4QKasApyQDGauyln3ygS VwY2YNGw46Rq5bga/czsqTCACiRBwXJKdI/pOolqTQXuE7hD+G4fiCtWQuSDv93K5mQdLukE5zl KNPmpD9VJH/9wWgRKgXODf6hoUCcJ4rE9byAHdyWzWTdZrRAu0brnmn7EdH467l474A== X-Received: by 2002:a2e:20f:: with SMTP id 15-v6mr817941ljc.141.1539255176826; Thu, 11 Oct 2018 03:52:56 -0700 (PDT) X-Google-Smtp-Source: ACcGV62UsLL0Lk/OoLU2weAiMC9gxprBJKv0iLr6MKjc1Pg4qKSvyS9svNeP1F1yrG7zmIuCH4Ru X-Received: by 2002:a2e:20f:: with SMTP id 15-v6mr817904ljc.141.1539255175735; Thu, 11 Oct 2018 03:52:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539255175; cv=none; d=google.com; s=arc-20160816; b=kQ9R2asLm599L67peJZhQB0271qgAiy1JprLxWuJYoqXKzmV0sm8kzDFobg77K+nOl i9dEjH56ctNRFchInfB41sPYOZXotKQL6ugjjmc2YVkTnwfpGrRoa/Ehek7Z6/Pt/ZDI rYQcqXzcWy4AloDYKfN1+CTq8IoXOlK9QCbY8E9kADbCJjI0nzB8p+OhdoU+7yoqy9jK iuaWP+XKDvm+a4GjQrdgylpJuMCHLZgVELHYCe4kaUVYGYhQ4g5oOOcdWSdhFvHh1s3b IzEQlAMIzQy4c8Ti3HLaaLwJXtYH8+oiypqo13hRSZvMG3xvEccRaaRnWN+fNOCAuAs1 bXlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:message-id:date :to:from:subject; bh=2esULjDgiDQTzDZzabv7NDzsk+NqyZ/STBqRI+TWRPA=; b=mS7g0U71Kv9xQDxmJBlBvyZP3c/MO9gBxTsyNjwDUr7bcVSuUpYklrV1om5RJBPEHh FDciPVH1Lw5tz9SxnUO4II2aIZ1syeyW9y0241nvIdTT0Sbisb+WjwbKDcBq6saLC1Dv OLvjXZF0wuhn47ApokdptNme3QQG1md2BRbVt3SzbYX2D7BEC6qI3nqsmybts12GTdCA a6SKypJJk6v89Z9oFqW+ewED2pn1K4yo1Oa2n/TZVU3M7glbxuulyODTqKrtY7626mFp I8/64cotXFiaqPwsOvh/0oE5bV+B2HwX98pdMQvgdpE2RUTDtKx9eb7BKi3AuTJFTfmU K8Hw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ktkhai@virtuozzo.com designates 185.231.240.75 as permitted sender) smtp.mailfrom=ktkhai@virtuozzo.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Received: from relay.sw.ru (relay.sw.ru. [185.231.240.75]) by mx.google.com with ESMTPS id z22-v6si18905134lfh.57.2018.10.11.03.52.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Oct 2018 03:52:55 -0700 (PDT) Received-SPF: pass (google.com: domain of ktkhai@virtuozzo.com designates 185.231.240.75 as permitted sender) client-ip=185.231.240.75; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ktkhai@virtuozzo.com designates 185.231.240.75 as permitted sender) smtp.mailfrom=ktkhai@virtuozzo.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Received: from [172.16.25.169] (helo=localhost.localdomain) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1gAYZr-0000Nx-Hy; Thu, 11 Oct 2018 13:52:23 +0300 Subject: [PATCH RFC] ksm: Assist buddy allocator to assemble 1-order pages From: Kirill Tkhai To: 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: Thu, 11 Oct 2018 13:52:22 +0300 Message-ID: <153925511661.21256.9692370932417728663.stgit@localhost.localdomain> User-Agent: StGit/0.18 MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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 | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index 5b0894b45ee5..b83ca37e28f0 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1321,6 +1321,21 @@ 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,