Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3472164imu; Sun, 11 Nov 2018 15:53:46 -0800 (PST) X-Google-Smtp-Source: AJdET5eXurqouY4y0opqgAZxaPGBLbSsYKPG4QT0y0amlbR+3IKSOAo6zqzQVtWZZGb8OYT4GktX X-Received: by 2002:a17:902:b181:: with SMTP id s1-v6mr5815361plr.51.1541980426600; Sun, 11 Nov 2018 15:53:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541980426; cv=none; d=google.com; s=arc-20160816; b=yKQZpOmwJkoD+f0eaeyuBRzO9cNQ3EKUSWj5W6SR+B9S9QOwDefg37Dqb2KuiVxV2o CfYlQ0uM9Cf7Q7fdn07UpeMXeDFqsOQFmTk9oxF1nFQXgOvobHvt5r3v1i8rLEW4+9Q1 TpFtqkdXNVEeEkDGEMg/ysGCobczQ+yS8GCmEv5e6PiE65s1WWcOXHpnYwJz9cft8/Fx QrkQbOYWcCYGkFj9MBd3Ow7cGEEFb3kQQJ7oUlNBK52gnhlodC8GSLngYNuSLe8z2SXC GVbYcsytj0oMRVUxnotsQd5b2bka2hQlQGkRTFSf7tr6R/ondJDdzclK7XjrwTLROv2+ aLZg== 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=qwVrp6fH8Kmlb28HwnPK4vPxVJMO9LMmZWGXzkzpLOA=; b=K4PkBjzRPLHMzxA8uDCfghh8eBRNfxYGsPWX8OUzSXN7PkHK0I+af18xUCk2jScGUu 0mcpi/loWBEJ7VC7kgq8RyFmrmh8barFyxoesIhLFH437YIaFcc0GyXk6KvV0+ZK/bbn bB8jkhePBJl7mHuBnvCbSuT4bdvAoWWd0k3EceHi/jWORYtu0M6M7al65EDE6/c5hF7h VyfoP3hzALY+iWEqHZcMf4+ls+r+a/c2oGe0WuCvNejgf1DKK8u2ng5+cGnCe4RbBqSk x62j60IcG0xtOBL7Xxc2+j28gJMbuwbFir4JDJLeNXdlCzqTTTJIkuFp5Q8Vq6YWRCbM YPLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=a8JU1TOX; 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 i5-v6si14285619pgg.559.2018.11.11.15.53.31; Sun, 11 Nov 2018 15:53:46 -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=a8JU1TOX; 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 S1732944AbeKLJna (ORCPT + 99 others); Mon, 12 Nov 2018 04:43:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:37584 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732794AbeKLIS0 (ORCPT ); Mon, 12 Nov 2018 03:18:26 -0500 Received: from localhost (unknown [206.108.79.134]) (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 4BC1B223C6; Sun, 11 Nov 2018 22:28:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975310; bh=VseTyUzO7RGFuI4GSdi4NwxtHE0SzLcqJFiPJO7AYTk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a8JU1TOXEf18mFHdRqkdrn/QBJ5NgYWZbFWF9wDGJnaapNh508pIbb9Skf8SB3mmh 7UnB89Sco+t99T9Pzcjy9sbYPYB/S7zQwwjQCUu3wOvkGW6Q/grdQ50/ty/Mgl/JXt g0NHaKRZjSb+CC8cSuY/jlMf6GH4L5Zimv1kvYRg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ralph Campbell , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , John Hubbard , Balbir Singh , Andrew Morton , Linus Torvalds Subject: [PATCH 4.19 254/361] mm/hmm: fix race between hmm_mirror_unregister() and mmu_notifier callback Date: Sun, 11 Nov 2018 14:20:01 -0800 Message-Id: <20181111221653.420795511@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221619.915519183@linuxfoundation.org> References: <20181111221619.915519183@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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. ------------------ From: Ralph Campbell commit 86a2d59841ab0b147ffc1b7b3041af87927cf312 upstream. In hmm_mirror_unregister(), mm->hmm is set to NULL and then mmu_notifier_unregister_no_release() is called. That creates a small window where mmu_notifier can call mmu_notifier_ops with mm->hmm equal to NULL. Fix this by first unregistering mmu notifier callbacks and then setting mm->hmm to NULL. Similarly in hmm_register(), set mm->hmm before registering mmu_notifier callbacks so callback functions always see mm->hmm set. Link: http://lkml.kernel.org/r/20181019160442.18723-4-jglisse@redhat.com Signed-off-by: Ralph Campbell Signed-off-by: Jérôme Glisse Reviewed-by: John Hubbard Reviewed-by: Jérôme Glisse Reviewed-by: Balbir Singh Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/hmm.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) --- a/mm/hmm.c +++ b/mm/hmm.c @@ -91,16 +91,6 @@ static struct hmm *hmm_register(struct m spin_lock_init(&hmm->lock); hmm->mm = mm; - /* - * We should only get here if hold the mmap_sem in write mode ie on - * registration of first mirror through hmm_mirror_register() - */ - hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops; - if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) { - kfree(hmm); - return NULL; - } - spin_lock(&mm->page_table_lock); if (!mm->hmm) mm->hmm = hmm; @@ -108,12 +98,27 @@ static struct hmm *hmm_register(struct m cleanup = true; spin_unlock(&mm->page_table_lock); - if (cleanup) { - mmu_notifier_unregister(&hmm->mmu_notifier, mm); - kfree(hmm); - } + if (cleanup) + goto error; + + /* + * We should only get here if hold the mmap_sem in write mode ie on + * registration of first mirror through hmm_mirror_register() + */ + hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops; + if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) + goto error_mm; return mm->hmm; + +error_mm: + spin_lock(&mm->page_table_lock); + if (mm->hmm == hmm) + mm->hmm = NULL; + spin_unlock(&mm->page_table_lock); +error: + kfree(hmm); + return NULL; } void hmm_mm_destroy(struct mm_struct *mm) @@ -278,12 +283,13 @@ void hmm_mirror_unregister(struct hmm_mi if (!should_unregister || mm == NULL) return; + mmu_notifier_unregister_no_release(&hmm->mmu_notifier, mm); + spin_lock(&mm->page_table_lock); if (mm->hmm == hmm) mm->hmm = NULL; spin_unlock(&mm->page_table_lock); - mmu_notifier_unregister_no_release(&hmm->mmu_notifier, mm); kfree(hmm); } EXPORT_SYMBOL(hmm_mirror_unregister);