Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp822600ybz; Wed, 15 Apr 2020 20:00:41 -0700 (PDT) X-Google-Smtp-Source: APiQypJ2AfejX6b74ITf+OGQlKjufhcoOWTWXyzrWFy1HZmFsBeRU35SAW+l8fHmKpjdR6TGY6Q3 X-Received: by 2002:a17:906:6a02:: with SMTP id o2mr7622516ejr.223.1587006041195; Wed, 15 Apr 2020 20:00:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587006041; cv=none; d=google.com; s=arc-20160816; b=BXgbAQ4z1COkJYpST+SPP50X+bx3sL0f/OKEoJsc3bHUj1oiwpibo2OYuKPeL2B89t At6gd4MCjTvEeMeaci/vIGru4DOUZkFRIJ7QiR0g7txAZVJ3w0NVUiOTC+i97jVsGIv1 +EOm2/E4/QF5eho4XkcjQzqV7SvHzBQbR4KSnWr90801JYlWxqCTJ9v2AFob81giqeRX Nf1LCH21530PgjoV+MjZSBOc52bbBtXhsZHPvRO6Zw5J/vdKUIpErDUGavCfxboMO4um 5yVkE8HHw7U+yg0FCT2bhY+0hVhJbGnXaeTxQGQvkLhR6iZgHTWwKpx9VyIaemx+0EwT QAxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=Y/tMpoxKWOXl/JYEY9xseldpVp0Ih+lSy+9FAv7Jj0A=; b=puObS9s1DEZXUE4pGCyPh/RojBo4ZSrw2799txKiRmXwWRicrE1zsCYAqwMRqpcfOz ExqRcWf+bwd7QlDASNDrykhzuD2iYXKT6JSG87GmZ8StYQBz6hD5JLOq8JPIOBGzauqs PKXAxcdC0xa3v91FhuHPVOrpcGlMM/svjKHHM+mY54FTKFuALSI6VpspKmkM/TJHWo/L u0miFBUzESA1FC6PzmS4NTleLBtx8DrbA6gpBeGpjiMgAR50HRY6/LFn3ioUz5PvcmTI EAkAmJkLNvOVqLU831nWOj/KmbCH1qy3en149ayOZCbIZ0uIUxE6kKezkqLJcr/fQiT7 p5Bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VTCGhOdW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r21si5307284eju.300.2020.04.15.20.00.17; Wed, 15 Apr 2020 20:00:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VTCGhOdW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389177AbgDPC6q (ORCPT + 99 others); Wed, 15 Apr 2020 22:58:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:39350 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388679AbgDPC6n (ORCPT ); Wed, 15 Apr 2020 22:58:43 -0400 Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6F437206F9; Thu, 16 Apr 2020 02:58:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587005922; bh=rQigxixSDs+dCK5RAkj+qC1sYZkMkckPug+3KMi9k5g=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=VTCGhOdWESWZMgOFOGaBWb3FHZxCpiNo9+5o5kxwbGkx+eBkDICfzwW6Iug+9iS5u yGiF0lXPMvcgVRAMXp70oDXjoMNZD2GK59tSbCfdnR/lgqPZZqrtbDwkgob0Kqa+Te xdp/WemLu7/GJ0Oef8yiRYhLk/Lm+pY0fgC89Wyc= Date: Wed, 15 Apr 2020 19:58:41 -0700 From: Andrew Morton To: Muchun Song Cc: Markus.Elfring@web.de, david@redhat.com, ktkhai@virtuozzo.com, yang.shi@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Xiongchun Duan Subject: Re: [PATCH v4] mm/ksm: Fix NULL pointer dereference when KSM zero page is enabled Message-Id: <20200415195841.da4361916f662a0136a271a5@linux-foundation.org> In-Reply-To: <20200416025034.29780-1-songmuchun@bytedance.com> References: <20200416025034.29780-1-songmuchun@bytedance.com> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 16 Apr 2020 10:50:34 +0800 Muchun Song wrote: > The find_mergeable_vma can return NULL. In this case, it leads > to a crash when we access vm_mm(its offset is 0x40) later in > write_protect_page. And this case did happen on our server. The > following call trace is captured in kernel 4.19 with the following > patch applied and KSM zero page enabled on our server. > > commit e86c59b1b12d ("mm/ksm: improve deduplication of zero pages with colouring") > > So add a vma check to fix it. > > ... > > --- a/mm/ksm.c > +++ b/mm/ksm.c > @@ -2112,8 +2112,15 @@ 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 > + /** > + * If the vma is out of date, we do not need to > + * continue. > + */ > + err = 0; > up_read(&mm->mmap_sem); > /* > * In case of failure, the page was not really empty, so we Thanks. It's conventional to put braces around multi-line blocks such as this. Also, /** is specifically used to introduce kerneldoc comments. This comment is not a kerneldoc one so use /*. --- a/mm/ksm.c~mm-ksm-fix-null-pointer-dereference-when-ksm-zero-page-is-enabled-v4-fix +++ a/mm/ksm.c @@ -2112,15 +2112,16 @@ static void cmp_and_merge_page(struct pa down_read(&mm->mmap_sem); vma = find_mergeable_vma(mm, rmap_item->address); - if (vma) + if (vma) { err = try_to_merge_one_page(vma, page, ZERO_PAGE(rmap_item->address)); - else - /** + } else { + /* * If the vma is out of date, we do not need to * continue. */ err = 0; + } up_read(&mm->mmap_sem); /* * In case of failure, the page was not really empty, so we _