Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3793640ybi; Mon, 29 Jul 2019 12:44:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqzAebbVDUO/tKZRgy6nqqKLxzGvqEYs1jzXPU11zrLrq1TEimtFnXXD4Os/Icu/Cjihswc8 X-Received: by 2002:aa7:95bb:: with SMTP id a27mr39035217pfk.229.1564429491292; Mon, 29 Jul 2019 12:44:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564429491; cv=none; d=google.com; s=arc-20160816; b=P8Ni/8F/rOFtoqEeRX2ZocvUcmkQ/xvBSW9O+ZdNaEoFmgZ2STkXuTjVpeyOEBVjzc NG+d3j+tyUomCSyzUhOcUQ+MrktQouMYHC0xSO559lzyn98LZtjwu7M0k7FdVmH9QmSm lHhTcmFB0dS8+IJl/wY5dPpzAZAxY5410xvQNtVN+DjWB6dkOktpHETj82bGUbgdpxq4 xRKIZCb7lDYDVwhf9dl0b59Cn+YD3HgEG5YvDuZ0vP6j93EF2Bnr48P6BLyaXlX/CFMZ oO7L6ZrD0qrMXmjpZ6BbedbOxKBr7PP2VHmNz2+MsrZTHQeWW9uLNvB4NclEh183tW9w UJMg== 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=s9t/lkud/pgCXYMfJw8Asm3pBmYZTSj5RUPDTg2DJAI=; b=m/gpisJaZclLIy/29RwFXX/OIKSUICY6IyOG/mQ9nkOtozU1XJAqCAR4l0eN4QzMDm AJr+5juP75pTpikicNANIgo7d7lV7gMyvJIuunIguPIHfop1uQ6uAGRJ+UbLlb7rPcS1 9vkE4m68WSJTk6zSR4prDdQSG2rMBKkRYXPOT0Fgom0hYMBAKzaeDo2QRo+gsqPoGMPO s9cLMnPBLQR7G6S7rN5rpL5PPivPfmitqiC2SNCtUq7PtehKsYF8ds9sOVqz044UR3bj lAhPiZn5o4ufo++ZiLFMxNWLM5TWnU9gp+sraveuHXEZhQ5B3KrKekqruzNgdrz2ftlp JEZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=QUIWVHZg; 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 g12si29402742pgj.124.2019.07.29.12.44.36; Mon, 29 Jul 2019 12:44: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=QUIWVHZg; 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 S2389655AbfG2Tn3 (ORCPT + 99 others); Mon, 29 Jul 2019 15:43:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:59734 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389647AbfG2Tn0 (ORCPT ); Mon, 29 Jul 2019 15:43:26 -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 7091A21773; Mon, 29 Jul 2019 19:43:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564429405; bh=SCTpNe73w03WFMGCTgSs09EQkuxblGIon7LU4ul75hk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QUIWVHZg+LZUHkDQr+VI78tv2Th4WOUK4S9X+Y+6zBvrbBxFA4MZwum1elgEhh0Mr gjNZo3c128D3zGRtq0URUJpkOE9B1BttHFdFb5avGUK0MC5D+MQCjRgzsH4o86GCv+ c2KbdTX85I5TSzCOroTOO0OxO8kgLIjT7FCFughg= 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.19 088/113] mm/mmu_notifier: use hlist_add_head_rcu() Date: Mon, 29 Jul 2019 21:22:55 +0200 Message-Id: <20190729190716.582894769@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729190655.455345569@linuxfoundation.org> References: <20190729190655.455345569@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 82bb1a939c0e..06dedb175572 100644 --- a/mm/mmu_notifier.c +++ b/mm/mmu_notifier.c @@ -316,7 +316,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