From patchwork Fri Oct 19 12:33:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kirill Tkhai X-Patchwork-Id: 10649273 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 D319B13A4 for ; Fri, 19 Oct 2018 12:34:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C0FD7288AA for ; Fri, 19 Oct 2018 12:34:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B4F5F288CF; Fri, 19 Oct 2018 12:34:20 +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 B95D6288AA for ; Fri, 19 Oct 2018 12:34:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 428436B0007; Fri, 19 Oct 2018 08:34:18 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3FE556B0008; Fri, 19 Oct 2018 08:34:18 -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 2A1486B000A; Fri, 19 Oct 2018 08:34:18 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lj1-f200.google.com (mail-lj1-f200.google.com [209.85.208.200]) by kanga.kvack.org (Postfix) with ESMTP id B147E6B0007 for ; Fri, 19 Oct 2018 08:34:17 -0400 (EDT) Received: by mail-lj1-f200.google.com with SMTP id p20-v6so10039269ljg.20 for ; Fri, 19 Oct 2018 05:34:17 -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=q2JKyL/fiUd1WwUOlK+CDIsfrq3ijP0czJt25kR5NtY=; b=eMQVIdSrCJ1RytlqVXDYX2plX1JC2t/meliRoIngOGBq8Ub2I1V9RT75Ef0bC2mCV6 dE0T85BsbWsM1eGfvGEl0j/fmMsTyemutkZus0xNhFmwx3toq/uXFa9Cd8GU92YLYgx3 98uM7VXgmGtTKxwfZTU162HwYy530Fd6pInOfnooQXE/QI1PYQ+nK51v818JpURjNMG/ uZNc983fukqDo55EV36JsO896QP8CX4Mx/6cSYg8tRZ9QrYPF97jsHDnNtuRWA76/tOm W3LIOJU99xT0CrtughIeQvBAYeT//tsYDXy6R01N2sYZEAwlWaGfXt0CECiXQS3vKZSx qkEQ== 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: ABuFfogSQObvDVHm1GSQeNr1pVTV7cRVzFw8Ko8dQfPxvoS65CRo/7Iv 6/lnUEwLVf62EXZ2WT7PYu9cCxnxviT+V9slWTr69T7pLDa3xbqnXJFpWhNfXIxkcM4Phx50NUs 4c9SuFbxlqAp6EoXnoMVG046gnR3VnNVk67AKMbGRyFCm168JepHMEa/FAMgipOX7Ug== X-Received: by 2002:a19:a686:: with SMTP id p128-v6mr2853016lfe.42.1539952456930; Fri, 19 Oct 2018 05:34:16 -0700 (PDT) X-Google-Smtp-Source: ACcGV6262SZ2NKxxIlbYnLP93sYsiXXCdV/QjMutucijTpvImokZjC7MeRPO24ThJ3XFhE5THd3p X-Received: by 2002:a19:a686:: with SMTP id p128-v6mr2852976lfe.42.1539952455578; Fri, 19 Oct 2018 05:34:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539952455; cv=none; d=google.com; s=arc-20160816; b=ikQUoJxNA9bao6WfAyvVmza0wlqTT7o/Uudg4QSI04qUQZ+z6f96M5xJZWxcyfsipD Lgn0MHQGsi7wCVj7RFsJJJ7q+5zj+UomdgRuuFh3lMPZyJwnsx4DbIE0lN7L1DgiOWJ/ YiCIDU7qSWh9foSgHaxSkGTfaNJKqy+D+IskMjQHj3pBUB0O6kxfhmHiL/8D9Lm2aF1y 617F+DG4AsyLx8ZKq4P+n/A79NO0HydoQwUy0zhyQdmG/0WHFstzQE7xSRKB8nO2tke6 hRYDcTMzlnC/cpCYZD/uofoWLx6UVLEObpX88KEvRK/QOHFM4noYWeolBBKlY2uvw1mf Lomg== 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=q2JKyL/fiUd1WwUOlK+CDIsfrq3ijP0czJt25kR5NtY=; b=Cx+QU2/10cnEai4EsQ3u3SNOwK9mc2EdjyjaTG7vDoIsnDyhJ33ABA+kpvQ3YfHKss Gdk997I6Dgkceu6FGmZxUYwQQMbK4MfpqdPgHNjTg+8e7TmCGZ19EMwL3sJAGZZjeNV3 ftYf10dfvu9YrBI40JDB+0pI0/kkJcCo1mTCOHL34xwM4eQ9rsMZ6yE+2Zxnmu5hZJM0 bySzu+loz1vJWWJxwdRG2+IEUMIF7MyP0LBwg0QEkgBmLoAFhoV0qlLs8rL789sRzzNz WkAqk/rXK0S5E1LFEuR7s7a/DoXwQ4GjpOXX8r2AVNN9ktLoOKHgzCEjYKyKwMho0AtY KFtQ== 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 b17-v6si21538787ljd.168.2018.10.19.05.34.15 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Oct 2018 05:34:15 -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 1gDTyF-0007WP-Hy; Fri, 19 Oct 2018 15:33:39 +0300 Subject: [PATCH v3] 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: Fri, 19 Oct 2018 15:33:39 +0300 Message-ID: <153995241537.4096.15189862239521235797.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 v3: Comment improvements. 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 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index 5b0894b45ee5..47c2dfd1cf4f 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1321,6 +1321,23 @@ 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 its count is 0. If so, we + * consider the neighbour as a free page (this is more + * probable than it's freezed via page_ref_freeze()), and + * we 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,