Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp8113769imu; Tue, 4 Dec 2018 03:14:13 -0800 (PST) X-Google-Smtp-Source: AFSGD/Xw1oD5PYCQjcibGZD0ATwAbAK2i9/ic+p+W0TMAoyInjY7d1QiaFU7GJF5b+2ETu7eFhXd X-Received: by 2002:a63:4d:: with SMTP id 74mr16778384pga.248.1543922053234; Tue, 04 Dec 2018 03:14:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543922053; cv=none; d=google.com; s=arc-20160816; b=k/GKDZv5w+P5mh66nBdUmMaph0T312l+GJu1jLhuZwGbCfge/7uSXQmhotosyS2hNY th+Zo5h4aXhMrA0thnH4I+6hLWYp0NGZlUzlBnLzwgiZ4O59dSFbxI6i5P8CyeHdBcq2 QuYGUXlDhfoHRotVj6gULphesFbgKxkUl5h8DhEkvj/FBbMKXb6dZgT10ZfM0il08p87 B4UgfguWowhSq1Xv0Ip6/HjPcTVLzQK1w9Ix6DKzR43Qhfidw7Xk7UamEKTQ6uKi+0TI OalkzhVOiPLwjGxbSDJo4XSQwRdNwpRnfsPWR163mZ+NChZlO3qgHPRglEsh33WZcuDD PsdA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=MJTavIuGgIkdbgVVXnJL/Oz51QFgKZEBt8XhWFkpvrk=; b=Crg/1Lc3hOq70c2uTBxoZ1aWnBo3Cp3HbOaO2FITqf9HV/t/wDkJM+sypfTSBKYr3W XMN3kRiU6NecaQmVk5wEhxCGp7sJZwHHMVaqDlVh2UrxnH8/vpSWHQUtWosvLp3u9iqs 3f5rzrC53sSG08aUosxyiqeqWWl4pVbWhRXqLKDF7Ga4wCmbGfst2DM8rT9ZT3f0nBja UmcvEFMj6a738KMpNvJ8xz0oqcmy1k8AFvBR2mCikan5+PHi0ZfiZN84eO/vM4lPgkbh gbnD84zOKOiDTFwSHGHMFayKccHaRTq6qvWN++U/xRvubSSyGje9IpfxCtYs7V5yNQLK yBAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fIgG3xfk; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r14si16618684pfh.229.2018.12.04.03.13.58; Tue, 04 Dec 2018 03:14:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fIgG3xfk; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727666AbeLDLHr (ORCPT + 99 others); Tue, 4 Dec 2018 06:07:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:56898 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728491AbeLDLHn (ORCPT ); Tue, 4 Dec 2018 06:07:43 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 128A7214DB; Tue, 4 Dec 2018 11:07:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543921662; bh=qP65q3s4AUdQxbLnr5pEr8TTqMsevpkJqUCcTEkEPMI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fIgG3xfkx/nPtq1pK59l5vuEz6cVk9z/xssAKSowmF1g6+8GQhy7ZFOGGF54dnUdR V1V7lzpk0mm8OACTjsBURoGU88jTnZlyudJee0Uaez+4Wb0m1fHhrSWwSIh5qFhcLA ZphUe8oBFAnuKQVnSWdOR3I7L9Jk6LaMYZJSLb7U= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hugh Dickins , "Kirill A. Shutemov" , Jerome Glisse , Konstantin Khlebnikov , Matthew Wilcox , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.9 12/50] mm/khugepaged: collapse_shmem() do not crash on Compound Date: Tue, 4 Dec 2018 11:50:07 +0100 Message-Id: <20181204103715.144118790@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181204103714.485546262@linuxfoundation.org> References: <20181204103714.485546262@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ commit 06a5e1268a5fb9c2b346a3da6b97e85f2eba0f07 upstream. collapse_shmem()'s VM_BUG_ON_PAGE(PageTransCompound) was unsafe: before it holds page lock of the first page, racing truncation then extension might conceivably have inserted a hugepage there already. Fail with the SCAN_PAGE_COMPOUND result, instead of crashing (CONFIG_DEBUG_VM=y) or otherwise mishandling the unexpected hugepage - though later we might code up a more constructive way of handling it, with SCAN_SUCCESS. Link: http://lkml.kernel.org/r/alpine.LSU.2.11.1811261529310.2275@eggly.anvils Fixes: f3f0e1d2150b2 ("khugepaged: add support of collapse for tmpfs/shmem pages") Signed-off-by: Hugh Dickins Cc: Kirill A. Shutemov Cc: Jerome Glisse Cc: Konstantin Khlebnikov Cc: Matthew Wilcox Cc: [4.8+] Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/khugepaged.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index b87bd43993bd..e0cfc3a54b6a 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1400,7 +1400,15 @@ static void collapse_shmem(struct mm_struct *mm, */ VM_BUG_ON_PAGE(!PageLocked(page), page); VM_BUG_ON_PAGE(!PageUptodate(page), page); - VM_BUG_ON_PAGE(PageTransCompound(page), page); + + /* + * If file was truncated then extended, or hole-punched, before + * we locked the first page, then a THP might be there already. + */ + if (PageTransCompound(page)) { + result = SCAN_PAGE_COMPOUND; + goto out_unlock; + } if (page_mapping(page) != mapping) { result = SCAN_TRUNCATED; -- 2.17.1