Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp8102221imu; Tue, 4 Dec 2018 03:03:04 -0800 (PST) X-Google-Smtp-Source: AFSGD/XfInAALJJ4xd9IsPh2DC5tlWmwy59WB38KMRLEFfc34q3RPyX8SKuE9LZD1OM4WcDsZlkk X-Received: by 2002:a65:66ce:: with SMTP id c14mr15085502pgw.450.1543921384643; Tue, 04 Dec 2018 03:03:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543921384; cv=none; d=google.com; s=arc-20160816; b=rQLVwdFSzoewO8V5l0f8w1mgVddYh3vlfsrIewVqR+g+MZnYNdPtLjcs5oQm04swxx 2Y3UGcMFFvJS9L333oaIGwMFwZkh2odggxEuUKXx7v48UyIMauM0G5lqP2fR41/6XukS 6N3/0tc/6KVaab/lGbgwr/tZNhddqH2BfAMKEo6UzFfdfOLPyUQroiTKGxvBcqvY9FV6 yVEjuWPIYWsKgTb2VbLIPyMZHWnrdckskHM9uhTEKS04dqpPNEQVuIDS3rIaIr2rC/+r +3eQerHhv69hVPM2sP4OMDEgWPuo4phpdIdEYGxC3+mYiqradpbgeXibiOO/0+oqNOpq 4Zfw== 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=TXBa7lYWJ1mD3EDzvOcgFdMg74tJtiK2P5QMDMCgHzc=; b=I2E6vOROuTIU1inBzb2p1AU36m5dS0Fj1QSkPsMvP5mh0Mgcj37sirJw7wYmJookuS pnyE0ho9+hXtoJMc3OkzFjpNKr6+3IMngEdT6oZo1c+6asK86AVrn5GF89dtzVlxOhw7 Yt+A/M0T622txgITa4HRHmJrD7DBX6BiaVPBVYI9qe5mDdjzJ6ahDMrKvVuJsLdW1Pl0 UTRs1KirmFovfxpsTpLvtA075lkD8qeU8NvFD3Ado1Fu2toy7aAP3hC4WLzExgSNz5pU Ort0RxQ5cI3REmPyy32sykZmfzZ1ob85nDjCJTGeaY9zbCrgCGkwmf19jZl//T4RJwez BbsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CVnpsrpa; 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 g33si15152882pgm.426.2018.12.04.03.02.48; Tue, 04 Dec 2018 03:03:04 -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=CVnpsrpa; 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 S1727473AbeLDLBY (ORCPT + 99 others); Tue, 4 Dec 2018 06:01:24 -0500 Received: from mail.kernel.org ([198.145.29.99]:46766 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727014AbeLDLBS (ORCPT ); Tue, 4 Dec 2018 06:01:18 -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 2CE24214C1; Tue, 4 Dec 2018 11:01:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543921277; bh=Y6NXbNvYtM1Fy7I2lfIRwONSxi+UDKXwlGGNOfL0XdY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CVnpsrpaX7hmSe6pfRts7DQYHdgwF/QCEzTcJtZZs/QrlISBOwUBt6hd3KNi+nPJF rcU2cel9A0JWrbVXckkXlzBsrKt1fP5tobrDnH8HaTnW8BQzp8jh1cI0M5O4uaP43O efIhIMt/UjedLGgD6cjnXhvGBWnjac8EzEoLt9Qk= 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.14 010/146] mm/khugepaged: collapse_shmem() do not crash on Compound Date: Tue, 4 Dec 2018 11:48:16 +0100 Message-Id: <20181204103727.206521255@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181204103726.750894136@linuxfoundation.org> References: <20181204103726.750894136@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.14-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 45bf0e5775f7..d27a73737f1a 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1401,7 +1401,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