Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3814744ybi; Mon, 29 Jul 2019 13:09:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqy3OZm7u1z0sv+K7AJ0/qKZyNPNr/tYQveE2oeIITlztwChtcp7l9dqoCW0XCfJ9+tcHkT5 X-Received: by 2002:a17:90a:ac14:: with SMTP id o20mr115120170pjq.114.1564430957498; Mon, 29 Jul 2019 13:09:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564430957; cv=none; d=google.com; s=arc-20160816; b=e5UV9vMmvnMC0q/M9xgzy6VXq48cUSEirkR6aXR7gR1mze3mS3HTKTJnc3IWiQcVbV oF95wTo//WFEF33AxQMmWX20w1nfyx5ZdJDduGoDTVhP5f7gXPi86m7HJOl1qU+871ls r0yp5ionMwDAx6s3frYpkhQ62ABbyIZKmdnW9n6qFzyTnEjuhQRj9iUDmSuXejLHvqHd Do4ojgY7PTSOuPwc5Mo7XEVPzOrErW7XR9XAVvSreAvYfYxwhTI9bdANf6nF8DCWWN3y 1vfOCsN67kpigDD3IJortDtGMK//9ZuUoNkebp3iQsYOCPzHvYHsmddxTvKm0uQlMZmp gbOA== 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=Y4Q3SjOSoGsdqqXte/el2NvPmIBBfvFV/39Gm3GEVXQ=; b=cXES1KzYa4WqFgFy1QttP71LmhyxwjKbvfJZ+FAm9XNfTLbbWwvtoqZIlhcgHfSAWA qeLuhs+jicCbscif1oJgWXHIixFpKSY9hD4d8CvXMAUCipYePgvIPMR+8mMxmMMTSSxl pifQvQRMCxftYIYtADSfQpmY3J9MAZwg5kP8iQCQZhmuvzxSX4vY+GWWDc22D5wbjXsE EtFp/hr4ysogk1DADV1k6Pmp0aJRwNLTyqi7zrPzsnmG+WKQCniTej56Zf7Qft8p2Lox dMjO3PXIAfFTrlAJkiGXHFX6dJLfF09PEllTWBoeo43CEdW/Ym24fnqvdrAekyjjnUjW Lm/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1Y6gneyb; 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 h12si24436751pjt.4.2019.07.29.13.09.02; Mon, 29 Jul 2019 13:09:17 -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=1Y6gneyb; 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 S2388657AbfG2TjK (ORCPT + 99 others); Mon, 29 Jul 2019 15:39:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:54170 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388939AbfG2TjG (ORCPT ); Mon, 29 Jul 2019 15:39:06 -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 CE33E2054F; Mon, 29 Jul 2019 19:39:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564429146; bh=C2ERHxkMjstK/3REuWR56W15NHHguzmwGCiAkdCUl30=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1Y6gneybSTOC1aJQ/suVXem0uja9DJ84hM6KHfiFLCt/2NEADdqf4K9vTHKRyffiA pKnKVzvk4LXqIMWyM8R4Dyhq12nDMohM2oBM7s3YksBm8gGrtnF/ti7JFhONAxh1ki /TXhdRK+abGpZxaRsCXayBE0t1dWb4H7ZU85XceQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jean-Philippe Brucker , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Michal Hocko , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.14 275/293] mm/mmu_notifier: use hlist_add_head_rcu() Date: Mon, 29 Jul 2019 21:22:46 +0200 Message-Id: <20190729190845.383054872@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729190820.321094988@linuxfoundation.org> References: <20190729190820.321094988@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 [ Upstream commit 543bdb2d825fe2400d6e951f1786d92139a16931 ] Make mmu_notifier_register() safer by issuing a memory barrier before registering a new notifier. This fixes a theoretical bug on weakly ordered CPUs. For example, take this simplified use of notifiers by a driver: my_struct->mn.ops = &my_ops; /* (1) */ mmu_notifier_register(&my_struct->mn, mm) ... hlist_add_head(&mn->hlist, &mm->mmu_notifiers); /* (2) */ ... Once mmu_notifier_register() releases the mm locks, another thread can invalidate a range: mmu_notifier_invalidate_range() ... hlist_for_each_entry_rcu(mn, &mm->mmu_notifiers, hlist) { if (mn->ops->invalidate_range) The read side relies on the data dependency between mn and ops to ensure that the pointer is properly initialized. But the write side doesn't have any dependency between (1) and (2), so they could be reordered and the readers could dereference an invalid mn->ops. mmu_notifier_register() does take all the mm locks before adding to the hlist, but those have acquire semantics which isn't sufficient. By calling hlist_add_head_rcu() instead of hlist_add_head() we update the hlist using a store-release, ensuring that readers see prior initialization of my_struct. This situation is better illustated by litmus test MP+onceassign+derefonce. Link: http://lkml.kernel.org/r/20190502133532.24981-1-jean-philippe.brucker@arm.com Fixes: cddb8a5c14aa ("mmu-notifiers: core") Signed-off-by: Jean-Philippe Brucker Cc: Jérôme Glisse Cc: Michal Hocko Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/mmu_notifier.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/mmu_notifier.c b/mm/mmu_notifier.c index 314285284e6e..70d0efb06374 100644 --- a/mm/mmu_notifier.c +++ b/mm/mmu_notifier.c @@ -267,7 +267,7 @@ static int do_mmu_notifier_register(struct mmu_notifier *mn, * thanks to mm_take_all_locks(). */ spin_lock(&mm->mmu_notifier_mm->lock); - hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list); + hlist_add_head_rcu(&mn->hlist, &mm->mmu_notifier_mm->list); spin_unlock(&mm->mmu_notifier_mm->lock); mm_drop_all_locks(mm); -- 2.20.1