Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp1192413ybn; Wed, 2 Oct 2019 12:13:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqyWkNH26Bgs/skjNCQkYYpU5L8R0K7McFojWSSu5EiF3U3VBmwWpxxc1HjsZouG1kTmUG6/ X-Received: by 2002:a17:906:6805:: with SMTP id k5mr4456320ejr.50.1570043585453; Wed, 02 Oct 2019 12:13:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570043585; cv=none; d=google.com; s=arc-20160816; b=FYnckqwE7hs7EaIDwjB3uhXTiRN194j4vcOxG5FWxWpJqck5XidDSDrS1V87yLnPq2 cHv+Nd0Hm/mIbnFSlVNsFBKrca09w5zzDnq7R27MhYc2DTebh5//QrgbgxO5y1jFIsUG EPpn5LeQ9Lm2rjkRm4+9Ciazo5mWOHNArYZuymSscxaw+adqCOOso056YwWPZUzZNaCu lbwwFFSmxvitniTgcrq+c8OUGIfsoeo85HGLBRg7oAxm+Mhnnhy8uQAy3zRfPgGnV51z qSXAb+FvUktDcr0l7cIDyGg2Z4Y2HrKMA6X4m2hsIiTOWhrAB7gZ5HJe0B3m7URCaAvg gwRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=DIU79dqNaShJxjrdCAbmH8NAg0U+p3X44eNzonwKS1U=; b=zx25qRD0AcLDyVuxj/QOPbOvQ7hOYOlNgNep0neav2zC0TVyrIsunsGFQpswu1gQ+M 7wPQqvhNTzRPi2hgCKnEdOwFdp4c6+c/pXvpbqH7/Bcn0EpohdlhJStdiBbhT2dLCRQj n71QTih09woyu0T6HpXOFU2m09lU0hL8qaP6qLIq1Ypk2ycwyxDo4cG80XV0WNtRBZDr 9HcVWFIndugs4ANizs/3qfcGPqv1ZRIZbiGvbmONdr141a5yi7DRCyJhxazlNT7ZYxvv 2EXcXJz08qV6yQP8+iRL0llb2txmv9J0cxDCVV/BlORdAKANovwmLe2A/WOXfqCXFmt1 amSQ== ARC-Authentication-Results: i=1; mx.google.com; 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 f1si5097ejw.217.2019.10.02.12.12.41; Wed, 02 Oct 2019 12:13:05 -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; 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 S1729682AbfJBTJn (ORCPT + 99 others); Wed, 2 Oct 2019 15:09:43 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:35954 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729337AbfJBTIS (ORCPT ); Wed, 2 Oct 2019 15:08:18 -0400 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iFjyx-00036B-Rk; Wed, 02 Oct 2019 20:08:16 +0100 Received: from ben by deadeye with local (Exim 4.92.1) (envelope-from ) id 1iFjyp-0003ee-4V; Wed, 02 Oct 2019 20:08:07 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, Denis Kirjanov , "Hugh Dickins" , "Andrea Arcangeli" , "Peter Xu" , "Linus Torvalds" , "Mike Kravetz" , "Michal Hocko" , "Jason Gunthorpe" , "Mike Rapoport" , "Oleg Nesterov" , "Kirill A. Shutemov" , "Jann Horn" Date: Wed, 02 Oct 2019 20:06:51 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 60/87] coredump: fix race condition between collapse_huge_page() and core dumping In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.75-rc1 review patch. If anyone has any objections, please let me know. ------------------ 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 Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds [bwh: Backported to 3.16: - Don't set result variable; collapse_huge_range() returns void - Adjust filenames] Signed-off-by: Ben Hutchings --- --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -2439,6 +2439,10 @@ static inline void mmdrop(struct mm_stru * 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/huge_memory.c +++ b/mm/huge_memory.c @@ -2428,6 +2428,8 @@ static void collapse_huge_page(struct mm * handled by the anon_vma lock + PG_lock. */ down_write(&mm->mmap_sem); + if (!mmget_still_valid(mm)) + goto out; if (unlikely(khugepaged_test_exit(mm))) goto out;