From patchwork Mon Oct 15 09:33:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kirill Tkhai X-Patchwork-Id: 10641433 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 9A187157A for ; Mon, 15 Oct 2018 09:34:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 85C6029368 for ; Mon, 15 Oct 2018 09:34:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 78BC229430; Mon, 15 Oct 2018 09:34:13 +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 D4BE229368 for ; Mon, 15 Oct 2018 09:34:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 64EF46B0008; Mon, 15 Oct 2018 05:34:11 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 5FE826B000A; Mon, 15 Oct 2018 05:34:11 -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 515A56B000C; Mon, 15 Oct 2018 05:34:11 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) by kanga.kvack.org (Postfix) with ESMTP id D553B6B0008 for ; Mon, 15 Oct 2018 05:34:10 -0400 (EDT) Received: by mail-lf1-f72.google.com with SMTP id a8-v6so2911328lfa.20 for ; Mon, 15 Oct 2018 02:34:10 -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=qwA2QFBkazDsm30mx22asNxyewB8scLwPKUvOgqfZpM=; b=S/NogikA8Q1VAlfvmkDxQFRjinT4+udFiXBL5yTANWkNpOop002OHW+yBFSzYBPrOw VF+fEObZeCQNRIbK781gd4z7SKAPGtLKXY+/Pr7phBjqt2e9MBHYalkJEWHwzwQBHZs5 Q/J7g3wqzQ1LpUMs2hcpUaOsNIroH6vv2Cd/DUvACd7HN04VCYuuwRddKTumMfYDTshg GPndDxH5Gb4ipUZoPOrfWJacXKTScXJbcaxMrJSgNpZsM7aVXGWXA+i84jkteGF8FJPv 2RfR+oYWg0roOE8G2n6iaQMGrtVYkcU8rxFTpUOmLtnjCgZa7Dbmgkm+BSd8z9uiDSbN CR3A== 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: ABuFfojCTtQgCGK0cApH+W2cqhrSp3UbH8Ka3868lsVKku+UXTbPfQTt XXQlZAx3D3iKB7MTStz2hQ1bqKqm6AKVnz8j+dHYaNEijepvkJIJNE44MyOLVLguBJbFY/YVJwj PpKztVS3shlftS+UFucaKPOnTOxIX7WAc0TLjp0HxYXVQdxFWEVCT6MY046e0sNRYmQ== X-Received: by 2002:a19:db42:: with SMTP id s63-v6mr3794932lfg.107.1539596050050; Mon, 15 Oct 2018 02:34:10 -0700 (PDT) X-Google-Smtp-Source: ACcGV616bO8Hqe7rjxEybB57CWBxCOk/uFNihLmrXs3taND43VrOnf6BT5emu4/9GA7O/tfAtcvn X-Received: by 2002:a19:db42:: with SMTP id s63-v6mr3794875lfg.107.1539596048769; Mon, 15 Oct 2018 02:34:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539596048; cv=none; d=google.com; s=arc-20160816; b=Dgwb9zkVJJlIJh2KGw1AJeJYDeDdC3oIB8bWBUhIX8hm/t82ApEUYtSd8EunIuZFnK QIzVFZ0w38HL4emtyIMsO7bM3NR4MV2bSk9igpm4guofh4QRNfTTGwMzix0vQBUN8wak NsXg6jN3fnlXGiH5YjwISDyWEdEX8+07w0GT8Px8xCpcg7Y51o1VdtrL06R9L1vGPs47 dap+Ir/jrEhet+DyGhfKDDlRhwJHyY5orgEieFzRhW1Y/dczHECwz+pHOZ3YiePFI07g 8sTv366MIRG2ktWfgZcD3dfANtXa5McoRtHmG4TX/4kM6E/eAfOwIxGeUQcIFTTCi66k w8oQ== 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=qwA2QFBkazDsm30mx22asNxyewB8scLwPKUvOgqfZpM=; b=CvgGXmzNWsQBtbOJJZeGwHpHHipxPF2zmSrPC6s3bt7jluK0fOiGAzJO4qIH6oXzig h/8Eg9WSG9q9z4yVbaEH/+f3SmrAHihRaXat1MvALG5DMbElvqRwmPA94Luo09Xs8Xj3 va32xuEC5H/jHNzV9xOVllhYNhDBprZJSfeRdLGhEpqWjwU6tIFMWkXASFNc+rqHYe53 n/Mqc71rYsB/42vGZhhesABjR1h969HLUhqxPhTCQ5/wBAqQ77Uxh9gQ4m7VfjZ21ZuM RtUXAT7U94U2zfTx1pKKziRxpFUDQmhfuV6Y0xJ46PnP4UfjqMhbAHzMQ6+JJoRxcsR1 ONJg== 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 o64-v6si7827922lfc.108.2018.10.15.02.34.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Oct 2018 02:34:08 -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 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 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 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,