From patchwork Wed Nov 7 19:16:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 10672917 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 989AB13AD for ; Wed, 7 Nov 2018 19:16:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8A6CC2C5C5 for ; Wed, 7 Nov 2018 19:16:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7B83C2C5CA; Wed, 7 Nov 2018 19:16:59 +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,UNPARSEABLE_RELAY 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 0585E2C5C5 for ; Wed, 7 Nov 2018 19:16:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C7376B0542; Wed, 7 Nov 2018 14:16:57 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 478466B0543; Wed, 7 Nov 2018 14:16:57 -0500 (EST) 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 38F436B0544; Wed, 7 Nov 2018 14:16:57 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by kanga.kvack.org (Postfix) with ESMTP id EB9B26B0542 for ; Wed, 7 Nov 2018 14:16:56 -0500 (EST) Received: by mail-pf1-f199.google.com with SMTP id n22-v6so16217371pff.2 for ; Wed, 07 Nov 2018 11:16:56 -0800 (PST) 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:from:to:cc :subject:date:message-id; bh=3gcZFYBZwCJhMzy2iLHl9m14GhjS5muXQGyf40agkhw=; b=FPGTXQ/son0QCHuZ94zI4wAHgOtrGdvz+69UgG4FDhu+QfqFOo3EWQM3OxritOBGpA aZy+Oy0oFwbJi5uj9x1ux+2hY/wxeiKh4JIrRewDNz5XVdw3Kkus8u8mjso/8wytBs54 FtgbuWzrO8X5sAEjzMuc3iAxCXdBVWSQpY9v56lD2fZmyIrvat4CGUp0/4hr0YRozVpb L0iKCHc5GhFRn8mCqtjzyc5zXkI0y+dBODqw/rG/kQndDTZ0bELdP6cr2x8SrRWqtAC/ 6gcjJfncEBvkCNF3O1hm4ibsvzWi/3AcZxzWYc2uczPTu1UYQpkIgVmBuYwNbISyytZN qK/A== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com X-Gm-Message-State: AGRZ1gLbwW3M1AEPDki5DD+4BQ2CrBMLHc0KFwgl4E4Gvj7jK6MjApMm GkHMy8fdR3q1TSIEhQlFtp19dSPv1UWBeaJ2IkAuIFm6qm+FyGNyqAOPMxHCYXFyKmUtEt7EV4o NKGYO0NJxlxmv+v9V19Bf0573ZEo5+wR50pHuap0MqSdP6NrgT3zDVjeSa+842KKI4w== X-Received: by 2002:a62:20c1:: with SMTP id m62-v6mr1530806pfj.111.1541618216593; Wed, 07 Nov 2018 11:16:56 -0800 (PST) X-Google-Smtp-Source: AJdET5eObjf/hmtMBel8RfnRxFQtawqlDN95epLax/mAcb+L7TZ1QDmyfm+WHApTOpDy0xXI1Y72 X-Received: by 2002:a62:20c1:: with SMTP id m62-v6mr1530727pfj.111.1541618215344; Wed, 07 Nov 2018 11:16:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541618215; cv=none; d=google.com; s=arc-20160816; b=uEy+ECN5jZmJH8Fn/dxHuBAdvEq5mmKzVP/xWlCtdleGSXjpg8bj3c9QbzeHpCqUlZ 120UKPKhdOUhM1HKeky/dMu0QqVnabe60hfLAk0fZBY9lX/1igOq337L7wT/oMnxoAFh eX3SVhyx6aunTE1ktBYxP/R3S99cIPDkOZqDnKF7nMEyExeK3eiwahKno90mlEeFyx+v nQclDC/20he45yRdUfjwM0LaYm9NdjGFZnXw5DcvtS8aSsB27of/1kcDxFr0/zWV90KP PqkZNWUWlBsLVXAsORPQZbKVjGF5OyrGCMtMPk7HVZ5OIh9mzXb+bGZ1npJMzXxYASKy Z9CQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from; bh=3gcZFYBZwCJhMzy2iLHl9m14GhjS5muXQGyf40agkhw=; b=NwU0t1bJIXIyG2XAe1gZoQSBrOhsn2jGziyY79UG6mCasFibT6YtdgWbZR+7/EU8UB JOcvQT1EuqD/j1Z//Mk6hPLjV/B93vke30XEqOEjgxWGZDRchtIVfjbl8ekpdUAeIz/N iPtW+AyS73nrYqUP+x3qow37c0fd4I5qabt7Sjlo+ojBExafeBIVJ94V8bqTz1eVKxaT zZtRouvE+2bN/8s1nRcCn/i4qkAzfhWcmAMwEpzrsKUdj4CEwz0KgtKlIJ3gTpV9gUD7 ZEbL+2yE7kgbKAzJW54GbeIFdt6FFino7Gsw2tEKtk+3e9zXIHXIFxMW1YvH07z+UOo/ GKSA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out30-132.freemail.mail.aliyun.com (out30-132.freemail.mail.aliyun.com. [115.124.30.132]) by mx.google.com with ESMTPS id p14-v6si1403611plo.363.2018.11.07.11.16.51 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Nov 2018 11:16:53 -0800 (PST) Received-SPF: pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.132 as permitted sender) client-ip=115.124.30.132; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R451e4;CH=green;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e07486;MF=yang.shi@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0TCUHVgb_1541618201; Received: from e19h19392.et15sqa.tbsite.net(mailfrom:yang.shi@linux.alibaba.com fp:SMTPD_---0TCUHVgb_1541618201) by smtp.aliyun-inc.com(127.0.0.1); Thu, 08 Nov 2018 03:16:49 +0800 From: Yang Shi To: mhocko@kernel.org, vbabka@suse.cz, hannes@cmpxchg.org, hughd@google.com, akpm@linux-foundation.org Cc: yang.shi@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] mm: vmscan: skip KSM page in direct reclaim if priority is low Date: Thu, 8 Nov 2018 03:16:40 +0800 Message-Id: <1541618201-120667-1-git-send-email-yang.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 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 When running some stress test, we ran into the below hung issue occasionally: INFO: task ksmd:205 blocked for more than 360 seconds. Tainted: G E 4.9.128-001.ali3000_nightly_20180925_264.alios7.x86_64 #1 "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. ksmd D 0 205 2 0x00000000 ffff882fa00418c0 0000000000000000 ffff882fa4b10000 ffff882fbf059d00 ffff882fa5bc1800 ffffc900190c7c28 ffffffff81725e58 ffffffff810777c0 00ffc900190c7c88 ffff882fbf059d00 ffffffff8138cc09 ffff882fa4b10000 Call Trace: [] ? __schedule+0x258/0x720 [] ? do_flush_tlb_all+0x30/0x30 [] ? free_cpumask_var+0x9/0x10 [] schedule+0x36/0x80 [] schedule_timeout+0x206/0x4b0 [] ? native_flush_tlb_others+0x11f/0x180 [] ? ktime_get+0x40/0xb0 [] io_schedule_timeout+0xda/0x170 [] ? bit_wait+0x60/0x60 [] bit_wait_io+0x1b/0x60 [] __wait_on_bit_lock+0x59/0xc0 [] __lock_page+0x86/0xa0 [] ? wake_atomic_t_function+0x60/0x60 [] ksm_scan_thread+0xeb9/0x1430 [] ? prepare_to_wait_event+0x100/0x100 [] ? try_to_merge_with_ksm_page+0x850/0x850 [] kthread+0xe6/0x100 [] ? kthread_park+0x60/0x60 [] ret_from_fork+0x46/0x60 ksmd found the suitable KSM page on the stable tree, an is trying to lock it. But, it is locked by direct reclaim path when walking its rmap to get the number of referenced PTEs. The KSM page rmap walk need iterate all rmap_item of the page and all rmap anon_vma of each rmap_item. So, it may take (# rmap_item * # children processes) loops. The number of loop might be very big in the worst case, and may take long time. Typically, direct reclaim will not intend to reclaim too many pages, and it is latency sensitive. So, it sounds not worth doing the long ksm page rmap walk to just reclaim one page. Skip KSM page in direct reclaim if the reclaim priority is low, but still try to reclaim KSM page with high priority. Signed-off-by: Yang Shi Signed-off-by: Yang Shi Signed-off-by: Andrew Morton --- mm/vmscan.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 62ac0c48..e821ad3 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1260,8 +1260,17 @@ static unsigned long shrink_page_list(struct list_head *page_list, } } - if (!force_reclaim) - references = page_check_references(page, sc); + if (!force_reclaim) { + /* + * Don't try to reclaim KSM page in direct reclaim if + * the priority is not high enough. + */ + if (PageKsm(page) && !current_is_kswapd() && + sc->priority > (DEF_PRIORITY - 2)) + references = PAGEREF_KEEP; + else + references = page_check_references(page, sc); + } switch (references) { case PAGEREF_ACTIVATE: @@ -2136,6 +2145,16 @@ static void shrink_active_list(unsigned long nr_to_scan, } } + /* + * Skip KSM page in direct reclaim if priority is not + * high enough. + */ + if (PageKsm(page) && !current_is_kswapd() && + sc->priority > (DEF_PRIORITY - 2)) { + putback_lru_page(page); + continue; + } + if (page_referenced(page, 0, sc->target_mem_cgroup, &vm_flags)) { nr_rotated += hpage_nr_pages(page); From patchwork Wed Nov 7 19:16:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 10672919 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 CDB4313BF for ; Wed, 7 Nov 2018 19:17:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C03372C5C5 for ; Wed, 7 Nov 2018 19:17:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B474E2C5CA; Wed, 7 Nov 2018 19:17:07 +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,UNPARSEABLE_RELAY 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 427652C5C5 for ; Wed, 7 Nov 2018 19:17:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C7376B0544; Wed, 7 Nov 2018 14:17:06 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4773E6B0545; Wed, 7 Nov 2018 14:17:06 -0500 (EST) 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 367556B0546; Wed, 7 Nov 2018 14:17:06 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by kanga.kvack.org (Postfix) with ESMTP id E84726B0544 for ; Wed, 7 Nov 2018 14:17:05 -0500 (EST) Received: by mail-pf1-f199.google.com with SMTP id g21-v6so4017184pfg.18 for ; Wed, 07 Nov 2018 11:17:05 -0800 (PST) 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:from:to:cc :subject:date:message-id:in-reply-to:references; bh=/KxFt5dloxXubkXGZ1swHl4ZSVHJJLNVYWkuQsyrEjg=; b=cFZz5t5QLr14F/t89grxS6BI/ZG1wBHV3pi9vIYYWFxZg6FFRyhUjiOQeu9q2Z901h Mxml3XpxN1tRm6WAw2PEYQw5iLIM8xF6MbyzjYsHo4kuFYQJdHZuBJlje912p22s/NiA FtY0zPjECMX4oOCyT2YzDNft6DJ12XSu+tfdhys+qeCqpr3YTqr7N1w+l+hRKPo70ROH m32pUfz2QYdCGb7bUhn8AOg2ReVcX2VMLHBG24hDvcP5k8cDaWlYtGQXqOoc/2Vc3LGN qhe7FemaqBZWgqaXpZS+J7KcEpMW3DOzhA6reGplBmomkPjp85tOVfeLEiDYLmaB8Lj1 I9sw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com X-Gm-Message-State: AGRZ1gKu4ksw/39enfXZYIq1J1tNaCHML8W07OWKsroobT9f81lVQlfM BSNCdRMd7ap+tRh6Amia+Rw58PLFBUxDZ9Q4ryCtfhyanxXXQw4lKiJqFgR/KBKJL5hmDnjEsSY uh3C5Vt+YmLI6ieqkKkxR2fsbxG4wvZRZGlaymjmppVeiTWG5mJaOXKmJuSVFVAO1+A== X-Received: by 2002:a62:4e01:: with SMTP id c1-v6mr1443036pfb.141.1541618225469; Wed, 07 Nov 2018 11:17:05 -0800 (PST) X-Google-Smtp-Source: AJdET5eq0WouPWpBq4skPOnwLLVcxmVIJKAXLoVcp36bGBLlOnX9KVIz8bcCQgJe/MjrH66kT+Iy X-Received: by 2002:a62:4e01:: with SMTP id c1-v6mr1442976pfb.141.1541618224251; Wed, 07 Nov 2018 11:17:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541618224; cv=none; d=google.com; s=arc-20160816; b=siJ13/MYAJr+HspI9/rLZzhDgvywomaJqIz1QuBxXHuyc1c/S8rEQa7R5nCopK/4Im 35N9PhczZ5GcZxNzZg/z+pfhHpeg7feKPt45yXcxmQd4On/nmruZtaMMyLxHCzcAdrVJ SzlBKLZUwMdIRQLT1w838439GJv5c9pZKc4jIFwUTj+hW3APi/FbVZUQbQxOyDoA9C11 S1myCGR14aEt3KXvEGX6KV6itdrgoV0UKHbtQ/P6A7Y/BWXNZdgdEyUq4dTaa1G51GFN N2GioVLgA+JeQ+yzv0eKeNbzjjNhMoL7ZT+uGoJnrgSgSC24QmQ/tewz3CLxSXFuduQP mVVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=/KxFt5dloxXubkXGZ1swHl4ZSVHJJLNVYWkuQsyrEjg=; b=1Eo/3/X2fKq+I0dIzJgUNy7MtEx9Bnm0Hlc0r55hlJZYRFRO+QA3JjX/XIr9uF9QyV iaQBGY+xgnrkcYvv9nuRRq5MzW9/QIXA0je+OxIxLs7+GttQuxqnI4gvjSl+QSpYPH22 vHEUaTEi/8MWN5tGVaoaL5LJtAynM68VWlKvTK3pArH7kYI9BIGUGAszyR+Q3xhGNBck ewfDLKvXz3nWSpwYISbwqZNr5jCMRNj8P8KYHXpp98Mb6Oo4RCzJ43Hb7N8IzvqGMyiC 6vX0YVAWpZLEWyk+e4zXF06zJnVGeaBrIryolakLhArW5Y+PPiW8j9S4A9mNy30ECfwe VnlA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out30-133.freemail.mail.aliyun.com (out30-133.freemail.mail.aliyun.com. [115.124.30.133]) by mx.google.com with ESMTPS id y16-v6si1160241pgk.479.2018.11.07.11.17.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Nov 2018 11:17:04 -0800 (PST) Received-SPF: pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.133 as permitted sender) client-ip=115.124.30.133; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R521e4;CH=green;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e07449;MF=yang.shi@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0TCUHVgb_1541618201; Received: from e19h19392.et15sqa.tbsite.net(mailfrom:yang.shi@linux.alibaba.com fp:SMTPD_---0TCUHVgb_1541618201) by smtp.aliyun-inc.com(127.0.0.1); Thu, 08 Nov 2018 03:16:49 +0800 From: Yang Shi To: mhocko@kernel.org, vbabka@suse.cz, hannes@cmpxchg.org, hughd@google.com, akpm@linux-foundation.org Cc: yang.shi@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] mm: ksm: do not block on page lock when searching stable tree Date: Thu, 8 Nov 2018 03:16:41 +0800 Message-Id: <1541618201-120667-2-git-send-email-yang.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1541618201-120667-1-git-send-email-yang.shi@linux.alibaba.com> References: <1541618201-120667-1-git-send-email-yang.shi@linux.alibaba.com> 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 ksmd need search stable tree to look for the suitable KSM page, but the KSM page might be locked for long time due to i.e. KSM page rmap walk. It sounds not worth waiting for the lock, the page can be skip, then try to merge it in the next scan to avoid long stall if its content is still intact. Introduce async mode to get_ksm_page() to not block on page lock, like what try_to_merge_one_page() does. Return -EBUSY if trylock fails, since NULL means not find suitable KSM page, which is a valid case. Signed-off-by: Yang Shi Reviewed-by: Kirill Tkhai --- mm/ksm.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 5b0894b..576803d 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -667,7 +667,7 @@ static void remove_node_from_stable_tree(struct stable_node *stable_node) } /* - * get_ksm_page: checks if the page indicated by the stable node + * __get_ksm_page: checks if the page indicated by the stable node * is still its ksm page, despite having held no reference to it. * In which case we can trust the content of the page, and it * returns the gotten page; but if the page has now been zapped, @@ -685,7 +685,8 @@ static void remove_node_from_stable_tree(struct stable_node *stable_node) * a page to put something that might look like our key in page->mapping. * is on its way to being freed; but it is an anomaly to bear in mind. */ -static struct page *get_ksm_page(struct stable_node *stable_node, bool lock_it) +static struct page *__get_ksm_page(struct stable_node *stable_node, + bool lock_it, bool async) { struct page *page; void *expected_mapping; @@ -728,7 +729,14 @@ static struct page *get_ksm_page(struct stable_node *stable_node, bool lock_it) } if (lock_it) { - lock_page(page); + if (async) { + if (!trylock_page(page)) { + put_page(page); + return ERR_PTR(-EBUSY); + } + } else + lock_page(page); + if (READ_ONCE(page->mapping) != expected_mapping) { unlock_page(page); put_page(page); @@ -751,6 +759,11 @@ static struct page *get_ksm_page(struct stable_node *stable_node, bool lock_it) return NULL; } +static struct page *get_ksm_page(struct stable_node *stable_node, bool lock_it) +{ + return __get_ksm_page(stable_node, lock_it, false); +} + /* * Removing rmap_item from stable or unstable tree. * This function will clean the information from the stable/unstable tree. @@ -1675,7 +1688,11 @@ static struct page *stable_tree_search(struct page *page) * It would be more elegant to return stable_node * than kpage, but that involves more changes. */ - tree_page = get_ksm_page(stable_node_dup, true); + tree_page = __get_ksm_page(stable_node_dup, true, true); + + if (PTR_ERR(tree_page) == -EBUSY) + return ERR_PTR(-EBUSY); + if (unlikely(!tree_page)) /* * The tree may have been rebalanced, @@ -2062,6 +2079,10 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) /* We first start with searching the page inside the stable tree */ kpage = stable_tree_search(page); + + if (PTR_ERR(kpage) == -EBUSY) + return; + if (kpage == page && rmap_item->head == stable_node) { put_page(kpage); return;