Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3101426ybi; Thu, 18 Jul 2019 21:03:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqxny9MTes73gETw44nuFTJc/peftv4jL5my7E9fyBGpd0pwnhqALO15/Z3x7ex1LxZzDAU/ X-Received: by 2002:a65:420c:: with SMTP id c12mr51794817pgq.125.1563509016168; Thu, 18 Jul 2019 21:03:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563509016; cv=none; d=google.com; s=arc-20160816; b=y3dM/t2bb3IpI75tVpW3fVLyTDiuPChVfhuxPEVIR7spcByv/GDvyikz9CFIwVU2RR eyjjnQ2iwNqNliCgwFxAg/ZctYlS9o0ouX/6xzqn6JinWQ/YtvpiBQtYWmB20q/4eQT6 xKonOBIbMljCZ8MiP8mJn+GgWFgifzDpnc/o6alca945wrkf6zSP21vsIMa0inFx4dXe ns9R4l/776zrwCDT0XJhkV3I5WqerScsy5EMhIQM50avRaoTR9KS20MgMvdZpVpZizwj tOoHbRUgbl08lidW/Bq4XiuQFqbzZQ3p82Zw+u97XIUqIs1ldlMe3bSBPI/qMc4ZofHh YodQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=63j+qOtSj7B52TrYDueZlOawTDgIg+N3a6voRDB1pkk=; b=XzMNUTi3lyiVnNZna4uDDEJD1H+O45JuITZ+rjNfJd1+1YgD9B6GaZ+gHewOpyYwym NeoEtTolbMw1SNthQG15d8hrCF5cBaj0N9q+RJvP1Pa7TZTqYbaUoDvwXVd6dZQ7iCYL k1UfLxn134j1XPHuAkpUFcFGFH36dNAWeVmB5bnpMLXAtwyenQ7N25rkQzaniNKe+vlN zIVytBPb7rFINPywuWqWK+CcTAlTPXmoXL7d2Ium1jBnIcG/5YDnLPbuYO6640Eh+lrR LXIxQsDx3+QmwyKjOT6EfBONuv0S8PhNWrKvYLyX7NSaIFVRyWKDIyDCVaFNqQ6eM8gB o6hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nbhu0qyg; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h9si524710pgp.435.2019.07.18.21.03.21; Thu, 18 Jul 2019 21:03:36 -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=nbhu0qyg; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729945AbfGSECT (ORCPT + 99 others); Fri, 19 Jul 2019 00:02:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:33994 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729863AbfGSECJ (ORCPT ); Fri, 19 Jul 2019 00:02:09 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B3D7121850; Fri, 19 Jul 2019 04:02:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563508928; bh=HQrvNFEnxnjgzUQx9V4YPtTFsDvuJV/3ioxb0Fb6eWs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nbhu0qygy3Y8xM+Xjbu3ijacmDU/iyroXXEVprvcHKKrey4Q+i3KeSnFgC5NBzWcy Vf1MltgdjGm4G+YhbCv38twAnTi2n9TIQh+BZlaFkHmPTpvrCX+btfPSoeqx967Qwu jcw+Mi1Vp6CSzpJor+68P2JSH9lTGNXLChnEdKPA= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jean-Philippe Brucker , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Michal Hocko , Andrew Morton , Linus Torvalds , Sasha Levin , linux-mm@kvack.org Subject: [PATCH AUTOSEL 5.2 162/171] mm/mmu_notifier: use hlist_add_head_rcu() Date: Thu, 18 Jul 2019 23:56:33 -0400 Message-Id: <20190719035643.14300-162-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190719035643.14300-1-sashal@kernel.org> References: <20190719035643.14300-1-sashal@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jean-Philippe Brucker [ 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 513b9607409d..b5670620aea0 100644 --- a/mm/mmu_notifier.c +++ b/mm/mmu_notifier.c @@ -274,7 +274,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