Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp2741086ybh; Mon, 5 Aug 2019 06:08:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqyaJngSoEELRp+RiD63Csi340LHlm+nlpYDnvvBpSI2SgltnNruIJKcMEY84MM5v5wKrbbP X-Received: by 2002:a17:902:fa2:: with SMTP id 31mr146727695plz.38.1565010531559; Mon, 05 Aug 2019 06:08:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565010531; cv=none; d=google.com; s=arc-20160816; b=xHltC1v5+i8rS/0tDUWc5XfgkT7lzZ3M8kqe1w0uu+fNlzEsk/NUynQwggIuTfVnxa aEEnlK3VIuhzqbGN6HfH4Rz1upMXIWVLJ7l6s7ERg8gLPHpeVWgs+FuO9Kd2EMOhBwdw XbVSqiYx+w7RgN1SDFePGztXA9BsiGxsFdJfkcDto4xK0YhFzgbJst7Y7c1puRa7NVMf DoGKSz4GblJ5alh6B9mwdYAUgywap0PVvKWA52gpfuw+Wmy9HjtoRZuMK0rfpxMPy3V5 WKIw4x4LTjVpAT3vh6aEVv4moPwEs7+y3voQrw6BOFKhrqS+7W10fzcQNjzOXIDMGklN lLDQ== 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=u6X6I+sl8NnGFTb7WeSsgvnkiUKnN8my1rCaK7YvfXA=; b=O99xzPKO4s4dX+XNjJdNWwvXNUPRspagWE3bOnDEklXwNRMqzvaqIxNDEY6HA+zYqY pdro7LQOucqSN1HMF4whfvLMRP41psoy9C1DqcqLz6qT9dR0/WKf72XXHaWrAprWVPaV uat0shga3idNfjf5iN9clugfIHgVzzV8nhnpXhhMi1gRwx3gCXeOP1IWh831dIgrngna tKle+iJ1vWu25GnugsWdcaAcnzBAaeF/P6PKe2eS6XR9n2WLx5wBZ0hUZf1Jib4We9H3 lclmu7xK4EBZW4t/o6/y0d8bo0tgn65TCPyr5jawYGNheyiglIS587On9PnGZm5f4xTj yuDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LFJxO4ge; 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 v27si45245935pgn.14.2019.08.05.06.08.36; Mon, 05 Aug 2019 06:08:51 -0700 (PDT) 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=LFJxO4ge; 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 S1729481AbfHENGI (ORCPT + 99 others); Mon, 5 Aug 2019 09:06:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:42456 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729464AbfHENGF (ORCPT ); Mon, 5 Aug 2019 09:06:05 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.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 64134214C6; Mon, 5 Aug 2019 13:06:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1565010363; bh=+ZBO3H0BUQdx7MgxjFLxJ8cL4JIdW5aUO6C05obMmKE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LFJxO4genz6Szlu7Obxgm8vnDAxZxybH9urPakE46dFkKab++9ZRdEkQ7BGNKzZin dpOBF7pSPF4Qrci2AaMIg1o72sw5Xz8wbpXYgmjArFmBN99BI0UGdqaYP1aTM4mfrf QxqVATgdSnZxc83/pKY0GMPG5VmePM2Uw/Q57J+o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrea Arcangeli , Michal Hocko , "Kirill A. Shutemov" , Oleg Nesterov , Jann Horn , Hugh Dickins , Mike Rapoport , Mike Kravetz , Peter Xu , Jason Gunthorpe , Andrew Morton , Linus Torvalds , Ajay Kaher Subject: [PATCH 4.9 37/42] coredump: fix race condition between collapse_huge_page() and core dumping Date: Mon, 5 Aug 2019 15:03:03 +0200 Message-Id: <20190805124929.390092782@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190805124924.788666484@linuxfoundation.org> References: <20190805124924.788666484@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Andrea Arcangeli commit 59ea6d06cfa9247b586a695c21f94afa7183af74 upstream. When fixing the race conditions between the coredump and the mmap_sem holders outside the context of the process, we focused on mmget_not_zero()/get_task_mm() callers in 04f5866e41fb70 ("coredump: fix race condition between mmget_not_zero()/get_task_mm() and core dumping"), but those aren't the only cases where the mmap_sem can be taken outside of the context of the process as Michal Hocko noticed while backporting that commit to older -stable kernels. If mmgrab() is called in the context of the process, but then the mm_count reference is transferred outside the context of the process, that can also be a problem if the mmap_sem has to be taken for writing through that mm_count reference. khugepaged registration calls mmgrab() in the context of the process, but the mmap_sem for writing is taken later in the context of the khugepaged kernel thread. collapse_huge_page() after taking the mmap_sem for writing doesn't modify any vma, so it's not obvious that it could cause a problem to the coredump, but it happens to modify the pmd in a way that breaks an invariant that pmd_trans_huge_lock() relies upon. collapse_huge_page() needs the mmap_sem for writing just to block concurrent page faults that call pmd_trans_huge_lock(). Specifically the invariant that "!pmd_trans_huge()" cannot become a "pmd_trans_huge()" doesn't hold while collapse_huge_page() runs. The coredump will call __get_user_pages() without mmap_sem for reading, which eventually can invoke a lockless page fault which will need a functional pmd_trans_huge_lock(). So collapse_huge_page() needs to use mmget_still_valid() to check it's not running concurrently with the coredump... as long as the coredump can invoke page faults without holding the mmap_sem for reading. This has "Fixes: khugepaged" to facilitate backporting, but in my view it's more a bug in the coredump code that will eventually have to be rewritten to stop invoking page faults without the mmap_sem for reading. So the long term plan is still to drop all mmget_still_valid(). Link: http://lkml.kernel.org/r/20190607161558.32104-1-aarcange@redhat.com Fixes: ba76149f47d8 ("thp: khugepaged") Signed-off-by: Andrea Arcangeli Reported-by: Michal Hocko Acked-by: Michal Hocko Acked-by: Kirill A. Shutemov Cc: Oleg Nesterov Cc: Jann Horn Cc: Hugh Dickins Cc: Mike Rapoport Cc: Mike Kravetz Cc: Peter Xu Cc: Jason Gunthorpe Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds [Ajay: Just adjusted to apply on v4.9] Signed-off-by: Ajay Kaher Signed-off-by: Greg Kroah-Hartman --- include/linux/mm.h | 4 ++++ mm/khugepaged.c | 3 +++ 2 files changed, 7 insertions(+) --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1197,6 +1197,10 @@ void unmap_vmas(struct mmu_gather *tlb, * followed by taking the mmap_sem for writing before modifying the * vmas or anything the coredump pretends not to change from under it. * + * It also has to be called when mmgrab() is used in the context of + * the process, but then the mm_count refcount is transferred outside + * the context of the process to run down_write() on that pinned mm. + * * NOTE: find_extend_vma() called from GUP context is the only place * that can modify the "mm" (notably the vm_start/end) under mmap_sem * for reading and outside the context of the process, so it is also --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1004,6 +1004,9 @@ static void collapse_huge_page(struct mm * handled by the anon_vma lock + PG_lock. */ down_write(&mm->mmap_sem); + result = SCAN_ANY_PROCESS; + if (!mmget_still_valid(mm)) + goto out; result = hugepage_vma_revalidate(mm, address, &vma); if (result) goto out;