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 DCDD7C4707F for ; Thu, 14 Apr 2022 13:50:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 726556B0073; Thu, 14 Apr 2022 09:50:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D6A46B0074; Thu, 14 Apr 2022 09:50:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 54F2C6B0075; Thu, 14 Apr 2022 09:50:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id 472C06B0073 for ; Thu, 14 Apr 2022 09:50:08 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 0F495122C51 for ; Thu, 14 Apr 2022 13:50:08 +0000 (UTC) X-FDA: 79355618496.22.5C47E92 Received: from mail-qv1-f53.google.com (mail-qv1-f53.google.com [209.85.219.53]) by imf02.hostedemail.com (Postfix) with ESMTP id 78F6E80009 for ; Thu, 14 Apr 2022 13:50:07 +0000 (UTC) Received: by mail-qv1-f53.google.com with SMTP id a5so4025434qvx.1 for ; Thu, 14 Apr 2022 06:50:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=DZ02OiwvpoOudfUuXhmFdF/LMUteXQjwkth1cncdRqE=; b=TRd/DJlidLiTENryi8hJOWQ7DbU+V+oJV+aCWm7ID73Is1heA2raX3GCKDUv58xmYs rXfzFch2ZWXj5lPBdzvlcZXULsz+ON8KUQIWuj1IEd8CJQh0Lrq2mf1XjoQ7NRQlyAwA xVoLGZ4D5Du0JlIVo6Bvyr2xQyITzQQzkBrWcbiuHlP45mzbJPhiZAY93fo8uPCpMp1V 3U20Cm6H+boQDQEovyKCsV4Xzn/0Gg/lozu6yM0rpm6xuWP6WyD46oBFVKTlrYP1D7K0 dUtxo1AoSVsgMQn9u4OdRCZtjLm5f7WULVVUIlGc79nK7Ujhjb8xZvGvOT5H3uQ8oj/4 5UWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=DZ02OiwvpoOudfUuXhmFdF/LMUteXQjwkth1cncdRqE=; b=02Omwp7IGug2f64mhvSEARJILkjht55489M2PMKbqQHop8cefhS7bRrmb1yZBlUSqu gf7+qh4/VcqEvqFn4hqP3+ikFiCQeWVX8hyK61ZyyPlqI11KveywmmsZAN7lQzKsaaOl AzGi59UH4dWrLZqYSGhPJNS8xs7mmSajIawhvcT22XOjVqDash/6sSiSOv6xVQgwHhVW YyzOtPoYID5nR2TGZ9pSY1tx5LfOOGtgV3Sk5hDpcNvVo+VIJVAecWWw5CXHStzvvkFk wSINfiEEZNFV94aMGOgVw2wznNhpG8Ai52yh7cVoqguJUChxX5R8WxRlbzeNZ1DfAQXV EqYw== X-Gm-Message-State: AOAM531lSwMHeASGGAZsrTQbbIFe9sPfRgBympypE+KkZyrdSl04bRiH r0xUzR3BHdwGOR+bJ33Ru7rdVQ== X-Google-Smtp-Source: ABdhPJwCAeRQPqvRqkol66RwYWsGBA2YHqxCqfXW0PhVDSBI90WE7X6BH4PXSunjumR5qg4yyWkNeg== X-Received: by 2002:a05:6214:89:b0:444:4e37:9d7a with SMTP id n9-20020a056214008900b004444e379d7amr3390462qvr.119.1649944206719; Thu, 14 Apr 2022 06:50:06 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-142-162-113-129.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.162.113.129]) by smtp.gmail.com with ESMTPSA id u5-20020a05622a198500b002ee933faf83sm1198877qtc.73.2022.04.14.06.50.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Apr 2022 06:50:05 -0700 (PDT) Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1nezrJ-0023v3-5t; Thu, 14 Apr 2022 10:50:05 -0300 Date: Thu, 14 Apr 2022 10:50:05 -0300 From: Jason Gunthorpe To: Alistair Popple Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, christian.koenig@amd.com, jhubbard@nvidia.com, rcampbell@nvidia.com Subject: Re: [PATCH] mm/mmu_notifier.c: Fix race in mmu_interval_notifier_remove() Message-ID: <20220414135005.GJ64706@ziepe.ca> References: <20220414031810.1787209-1-apopple@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220414031810.1787209-1-apopple@nvidia.com> X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 78F6E80009 X-Stat-Signature: a66sgmb3hamo7c6m1nbdc45c1g75zdqo Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=ziepe.ca header.s=google header.b="TRd/DJli"; dmarc=none; spf=pass (imf02.hostedemail.com: domain of jgg@ziepe.ca designates 209.85.219.53 as permitted sender) smtp.mailfrom=jgg@ziepe.ca X-Rspam-User: X-HE-Tag: 1649944207-97109 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 Thu, Apr 14, 2022 at 01:18:10PM +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 changing invalidate_seq to an > atomic type as it is read outside of the lock and moving the increment > until after deferred lists have been updated. Oh, yes, that is a mistake. I would not solve it with more unlocked atomics though, this is just a simple case of a missing lock - can you look at this and if you like it post it as a patch please? diff --git a/mm/mmu_notifier.c b/mm/mmu_notifier.c index 459d195d2ff64b..f45ff1b7626a62 100644 --- a/mm/mmu_notifier.c +++ b/mm/mmu_notifier.c @@ -1036,6 +1036,18 @@ int mmu_interval_notifier_insert_locked( } EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert_locked); +static bool +mmu_interval_seq_released(struct mmu_notifier_subscriptions *subscriptions, + unsigned long seq) +{ + bool ret; + + spin_lock(&subscriptions->lock); + ret = subscriptions->invalidate_seq != seq; + spin_unlock(&subscriptions->lock); + return ret; +} + /** * mmu_interval_notifier_remove - Remove a interval notifier * @interval_sub: Interval subscription to unregister @@ -1083,7 +1095,7 @@ void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub) lock_map_release(&__mmu_notifier_invalidate_range_start_map); if (seq) wait_event(subscriptions->wq, - READ_ONCE(subscriptions->invalidate_seq) != seq); + mmu_interval_seq_released(subscriptions, seq)); /* pairs with mmgrab in mmu_interval_notifier_insert() */ mmdrop(mm); Signed-off-by: Jason Gunthorpe