From patchwork Tue Apr 14 07:56:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11486991 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 48B93174A for ; Tue, 14 Apr 2020 07:56:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0AA38206D5 for ; Tue, 14 Apr 2020 07:56:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="c2dnPZDN" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0AA38206D5 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E23768E0003; Tue, 14 Apr 2020 03:56:53 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id DD3B98E0001; Tue, 14 Apr 2020 03:56:53 -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 CEA478E0003; Tue, 14 Apr 2020 03:56:53 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0159.hostedemail.com [216.40.44.159]) by kanga.kvack.org (Postfix) with ESMTP id B48F18E0001 for ; Tue, 14 Apr 2020 03:56:53 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 75D49181AEF2A for ; Tue, 14 Apr 2020 07:56:53 +0000 (UTC) X-FDA: 76705704306.17.cow54_1e7f2c536c35 X-Spam-Summary: 2,0,0,a09c766148daa593,d41d8cd98f00b204,songmuchun@bytedance.com,,RULES_HIT:41:69:355:379:541:800:960:968:973:988:989:1260:1311:1314:1345:1437:1515:1535:1542:1711:1730:1747:1777:1792:2393:2559:2562:2895:2899:2901:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:3871:3872:4250:4321:4605:5007:6117:6119:6120:6238:6261:6653:7264:7901:7903:7904:8660:8784:9121:9163:10004:11026:11232:11658:11914:12043:12296:12297:12438:12517:12519:12555:12679:12683:12740:12895:12986:13095:13148:13161:13229:13230:13870:13894:14096:14110:14181:14394:14721:21080:21324:21433:21444:21451:21622:21740:21990:22047:30003:30012:30029:30054:30056:30070,0,RBL:209.85.216.68:@bytedance.com:.lbl8.mailshell.net-66.100.201.201 62.2.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:25,LUA_SUMMARY:none X-HE-Tag: cow54_1e7f2c536c35 X-Filterd-Recvd-Size: 5874 Received: from mail-pj1-f68.google.com (mail-pj1-f68.google.com [209.85.216.68]) by imf03.hostedemail.com (Postfix) with ESMTP for ; Tue, 14 Apr 2020 07:56:52 +0000 (UTC) Received: by mail-pj1-f68.google.com with SMTP id e16so4646575pjp.1 for ; Tue, 14 Apr 2020 00:56:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=I3PJsIbLnGG+0LNZvpyyPpJKrgmiHJsFy1tLFZ5G7hg=; b=c2dnPZDNCBzzLF/SNgoQWxW/orovCCc+Q/60foy/H9tCof4ofS6NSGK24IxzzfHMq8 vLEVxD1O+wF9gfufRz9gW/l7P0zwkZDzAjvjjLtAecSy+8V6C1dmTz7e/Ztf+yiXfysA jbc3C9WOKMF65vZE4IUUx4f0VMSye4Su9wFOHt3NtmEBLhHhl768i8rZMsbM4Rd9vZEU BhbmS/FmUemOC0oO5F4WTejs0OUXbEmyc4trzUikxxcFvnzN1TNZ9Bp3PFkKE6uhZwy8 9nDP/wZ13X4Ve/arW/Puhsm/4jeSsLHX1+iXSGeQOjlmZHDQFH64cEDx8SG3/3igwwVP q9WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=I3PJsIbLnGG+0LNZvpyyPpJKrgmiHJsFy1tLFZ5G7hg=; b=lMzhciweWXJKbERMsOb6W/c+3jZET929l7LF5tOcecgVUU50OEAa/sjRzUZ2W5U/nZ roPFp3TZsX6dHlSJS0+HI/bgfQAX4pasf78ygWw35C+LqKMm549dsMoj7WkxcEoD9SsK YAmL62lkjXc8mdlXUfvbEtoan7rN9ixpVL2pfS1q5dGjaMhvX7EFaV0vkduW0Plig+RD Xj7C9xXkojeb/pzSWfv76haTC00QWZc2ni/v7CXO/pJz4mYFjGyBRpwTY/4Hcu3cNg1m AwdY9mHw7So2Bi4ya82gJVf0a/OmbnXrfLrWjhHBoCkJnK5b+Bla1Ca7TKz6TSsWJFZs n/8A== X-Gm-Message-State: AGi0PuZhRTVAkNe4ytPncQ8i071F9unBhXVQZl/ZmITH3A2iq2jaEmgP imxjYeeZ0WglBxczumtsMV7oxQ== X-Google-Smtp-Source: APiQypKpKG9coUBFC0Wb+ShFX1kW+n7CuUoWRS/IMPrXMqWW92Hl10XH9jEO4rfStFoJfPYhH6HyLg== X-Received: by 2002:a17:90a:bf8c:: with SMTP id d12mr26893363pjs.11.1586851011729; Tue, 14 Apr 2020 00:56:51 -0700 (PDT) Received: from Smcdef-MBP.local.net ([103.136.220.69]) by smtp.gmail.com with ESMTPSA id c201sm10568346pfc.73.2020.04.14.00.56.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Apr 2020 00:56:51 -0700 (PDT) From: Muchun Song To: akpm@linux-foundation.org, Markus.Elfring@web.de Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Xiongchun Duan Subject: [PATCH v2] mm/ksm: Fix NULL pointer dereference when KSM zero page is enabled Date: Tue, 14 Apr 2020 15:56:22 +0800 Message-Id: <20200414075622.69822-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) 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: The find_mergeable_vma can return NULL. In this case, it leads to crash when we access vma->vm_mm(its offset is 0x40) later in write_protect_page. And this case did happen on our server. The following calltrace is captured in kernel 4.19 with KSM zero page enabled. So add a vma check to fix it. -------------------------------------------------------------------------- BUG: unable to handle kernel NULL pointer dereference at 0000000000000040 PGD 0 P4D 0 Oops: 0000 [#1] SMP NOPTI CPU: 9 PID: 510 Comm: ksmd Kdump: loaded Tainted: G OE 4.19.36.bsk.9-amd64 #4.19.36.bsk.9 Hardware name: FOXCONN R-5111/GROOT, BIOS IC1B111F 08/17/2019 RIP: 0010:try_to_merge_one_page+0xc7/0x760 Code: 24 58 65 48 33 34 25 28 00 00 00 89 e8 0f 85 a3 06 00 00 48 83 c4 60 5b 5d 41 5c 41 5d 41 5e 41 5f c3 48 8b 46 08 a8 01 75 b8 <49> 8b 44 24 40 4c 8d 7c 24 20 b9 07 00 00 00 4c 89 e6 4c 89 ff 48 RSP: 0018:ffffadbdd9fffdb0 EFLAGS: 00010246 RAX: ffffda83ffd4be08 RBX: ffffda83ffd4be40 RCX: 0000002c6e800000 RDX: 0000000000000000 RSI: ffffda83ffd4be40 RDI: 0000000000000000 RBP: ffffa11939f02ec0 R08: 0000000094e1a447 R09: 00000000abe76577 R10: 0000000000000962 R11: 0000000000004e6a R12: 0000000000000000 R13: ffffda83b1e06380 R14: ffffa18f31f072c0 R15: ffffda83ffd4be40 FS: 0000000000000000(0000) GS:ffffa0da43b80000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000040 CR3: 0000002c77c0a003 CR4: 00000000007626e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 PKRU: 55555554 Call Trace: ? follow_page_pte+0x36d/0x5e0 ksm_scan_thread+0x115e/0x1960 ? remove_wait_queue+0x60/0x60 kthread+0xf5/0x130 ? try_to_merge_with_ksm_page+0x90/0x90 ? kthread_create_worker_on_cpu+0x70/0x70 ret_from_fork+0x1f/0x30 -------------------------------------------------------------------------- Fixes: e86c59b1b12d ("mm/ksm: improve deduplication of zero pages with colouring") Signed-off-by: Muchun Song Signed-off-by: Xiongchun Duan --- Change in v2: Update commit message and patch subject. mm/ksm.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index a558da9e71770..69b2f85e22d5b 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2112,8 +2112,11 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) down_read(&mm->mmap_sem); vma = find_mergeable_vma(mm, rmap_item->address); - err = try_to_merge_one_page(vma, page, - ZERO_PAGE(rmap_item->address)); + if (vma) + err = try_to_merge_one_page(vma, page, + ZERO_PAGE(rmap_item->address)); + else + err = -EFAULT; up_read(&mm->mmap_sem); /* * In case of failure, the page was not really empty, so we