Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp8097366imu; Tue, 4 Dec 2018 02:58:12 -0800 (PST) X-Google-Smtp-Source: AFSGD/VxAMNbbDq9cRHZFT+hNtCjeqS+CwmGobL/iffVTIGoSDQnkKKQf4M8HRUvE7rsz3rAWLBh X-Received: by 2002:a17:902:d01:: with SMTP id 1mr19937975plu.127.1543921092133; Tue, 04 Dec 2018 02:58:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543921092; cv=none; d=google.com; s=arc-20160816; b=Af/yeTW+25b/0E3S2EbB0ZBaERLqVWmvmCWxvRn3azvRBY/vo9OstPokxM34TvQzaQ T8wAFY4pPqxlHKVcCC0E0nG9Mvi9vB7wd6BREXCV/NiqJfZbZPP7ZqQhW1dXeBxuB9MS O+TgeXf7bFlSiB6Q819gCrwBOwHBRooruabA+Vaq3F1jDCADtFqSRsFVMsxmsLQy83E5 kPFuaPbmj68Z8C35IeLyUUAM5/nm1AV/2zriCrT1PJKm2g2HMTOqOkScTJ5RmWqrHOHq N1hL8fod5/t3EqJPCOs1u3uEW7m5B5s+bFb43bBdpWy2t3Qupw6TK++tcgLnGm508UDh 7bQg== 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=tsiaP4XU4NLR0/kYfatBSGIiw0cEO2TCkC77R8ecOe8=; b=uWujk+QZzL0gXnF6VnQrNTKlEDUp8/RMLLn6feJQF4MEZ0eP5zFyxEmoYv/3zTpBf6 52GnA7l/L2hnQ6ECMNNLNDI+JS5R5dktIJbh9sKonxRgUprdckzTXohRikGcOUK2kljB 6bkCRIR07qGmyGmEgXJttVGFt+xUrhvHc4gtGzvhPOQjswKbXlJCPumzWaExyNdpze5f EIrLjXMObVVuHDa92mXbFvsoJhGIcMeCRDQMpT43WcGRe9b726iHa8h2YF3xjTQ1tzOc wY/a3Aj5YGrLxgA0iVZry9BDEMXKuMURt3aT7KNJOU6hP1ZvSxnsQE1aBHzG6LVFFO+a QKGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vyQqtMza; 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 p189si9719542pfb.0.2018.12.04.02.57.56; Tue, 04 Dec 2018 02:58:12 -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=vyQqtMza; 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 S1726559AbeLDK4m (ORCPT + 99 others); Tue, 4 Dec 2018 05:56:42 -0500 Received: from mail.kernel.org ([198.145.29.99]:40110 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726536AbeLDK4l (ORCPT ); Tue, 4 Dec 2018 05:56:41 -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 CD42A214D9; Tue, 4 Dec 2018 10:56:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543921000; bh=RdL0LSlUOAuVtqt/N+1KTdXLzqA5bAxRE3xVB4fvv/0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vyQqtMzaD1S9U0VIoDwnN7ael4maRUVs7wmxUdofX2LKbVyRSFCPz4sxljv7N1qD4 5WcMr5XDCf0LNZp7hImYIxRqM8ObFdU9HsOA6Y4b80YpMNvFtqLRt0mwh0T2XKtHSz W3+LdgCNmemWEHd7+I2KJo4K1nyDflU45+Wnbl6I= 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.19 009/139] mm/khugepaged: collapse_shmem() do not crash on Compound Date: Tue, 4 Dec 2018 11:48:10 +0100 Message-Id: <20181204103650.366384496@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181204103649.950154335@linuxfoundation.org> References: <20181204103649.950154335@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.19-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 e2b13c04626e..fde5820be24d 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1398,7 +1398,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