From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B28CBC433EF for ; Wed, 20 Apr 2022 22:11:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EBD1D6B0071; Wed, 20 Apr 2022 18:11:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E6D3F6B0073; Wed, 20 Apr 2022 18:11:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0DC36B0074; Wed, 20 Apr 2022 18:11:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id BED3E6B0071 for ; Wed, 20 Apr 2022 18:11:45 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 8AE7412127C for ; Wed, 20 Apr 2022 22:11:45 +0000 (UTC) X-FDA: 79378655370.06.100C59C Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf13.hostedemail.com (Postfix) with ESMTP id 36E0920022 for ; Wed, 20 Apr 2022 22:11:43 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1D880619E1; Wed, 20 Apr 2022 22:11:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AB941C385A0; Wed, 20 Apr 2022 22:11:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1650492703; bh=3jG6oVH2gHG5S5CoAbfXYb/iLXrxT6JSgZNfp2QlZu8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=V/De21DIy005tQ4qGJq93QHov1T5vmFg6sTyk8WF3N9JVU1eXzvQe/vdLctKK9LgG DG8GbaEXYzmUGU+LHJID/Rpf9FhhPDRcx0w1T5E9maBKHQItsowHsJ9kVqx1GOt5gr nxRv/I+tWZ4n3svPJ/nVkxjDSbiwZ8f32a518Z9Q= Date: Wed, 20 Apr 2022 15:11:42 -0700 From: Andrew Morton To: Alistair Popple Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, christian.koenig@amd.com, jhubbard@nvidia.com, rcampbell@nvidia.com, Jason Gunthorpe Subject: Re: [PATCH v2] mm/mmu_notifier.c: Fix race in mmu_interval_notifier_remove() Message-Id: <20220420151142.f60307e749033a24ef0c68d5@linux-foundation.org> In-Reply-To: <20220420043734.476348-1-apopple@nvidia.com> References: <20220420043734.476348-1-apopple@nvidia.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b="V/De21DI"; spf=pass (imf13.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none X-Stat-Signature: c5tyed7x5b34k9trmq9wumx734xoicz1 X-Rspamd-Queue-Id: 36E0920022 X-Rspamd-Server: rspam04 X-Rspam-User: X-HE-Tag: 1650492703-982254 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, 20 Apr 2022 14:37:34 +1000 Alistair Popple wrote: > In some cases it is possible for mmu_interval_notifier_remove() to race > with mn_tree_inv_end() allowing it to return while the notifier data > structure is still in use. Consider the following sequence: > > CPU0 - mn_tree_inv_end() CPU1 - mmu_interval_notifier_remove() > ----------------------------------- ------------------------------------ > spin_lock(subscriptions->lock); > seq = subscriptions->invalidate_seq; > spin_lock(subscriptions->lock); spin_unlock(subscriptions->lock); > subscriptions->invalidate_seq++; > wait_event(invalidate_seq != seq); > return; > interval_tree_remove(interval_sub); kfree(interval_sub); > spin_unlock(subscriptions->lock); > wake_up_all(); > > As the wait_event() condition is true it will return immediately. This > can lead to use-after-free type errors if the caller frees the data > structure containing the interval notifier subscription while it is > still on a deferred list. Fix this by taking the appropriate lock when > reading invalidate_seq to ensure proper synchronisation. > > ... > > Fixes: 99cb252f5e68 ("mm/mmu_notifier: add an interval tree notifier") Do you think fix this should be backported into older kernels?