Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp664999pxk; Wed, 16 Sep 2020 13:47:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFbRqI5VAJ9IWUUxGWC8DtYW/RYjUuopxIS7jli0Io5Er4Dvm+eu/FUnkgok/ho7g9cPQm X-Received: by 2002:a17:906:60d3:: with SMTP id f19mr28315833ejk.141.1600289263265; Wed, 16 Sep 2020 13:47:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600289263; cv=none; d=google.com; s=arc-20160816; b=MZcH86IstN/FmAn6Ua7DOsIam3drYGSPVvI4EAPoGTNB0OQywjsMAPSEDkK5oA8k/0 4fMRZXhGaL19lRHxxgT/kLmtvNJHIl1m4O4rmmv6ypzMlAWej4eelLuDVWMtpZSnrcVm HSy7MHPTX5bDZfyA8za5P0ZVdEs67VQsquzp5Ap0/Nv6KI1FOxea64Je7pRKG4iNTzf5 xGbSOjGHQNsQfUZk6qhQi2RiA0Q2kxRXALkS3E0onHpZOIXwaUhNaYta/utGI8aBhKTZ 7pvmD9V+5VucsR3lonrBe5Zmx9O/sjoRDEJab1KzNB7m13t/1je6tLHfLfzADrHct4pO iEqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=0sirvuqazzshCbZE24EWceX5REl9RxuudL9lwx7DoI8=; b=W5gelGUtO4CU8/M+MBc2/aRMRNNrQsSmanViu7OWs342Y5KQjVUGaStuZ6GD8DpbuX 4eylIjSe+aE+syhIi5tUCdxi9hLQnpQ4gsf+BAqatx1G/1k8GPFEEQbe8Tr6/i2e1zOq s61S89gErHznG/iCr/hKs+E93e7yPq3/QWHnkuc/xmguANLasoTkuaGu9REHqQkkdC+U 4lAc18CtgdaG+XtBfjiI6utfEnjugZxdoB+sSeNFpdg9HLcHl+Prvnm6zvgEroek66W6 uKc8mWbNM7Ng8tUpSWKzC4WGrsQkm2iq0F55BzpWXwqYoVd7T/Yg4zsmtCmKqriDXgCT 015g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=casper.20170209 header.b=Ic68Vg7i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f19si12452615ejx.348.2020.09.16.13.47.19; Wed, 16 Sep 2020 13:47:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=casper.20170209 header.b=Ic68Vg7i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728487AbgIPUqY (ORCPT + 99 others); Wed, 16 Sep 2020 16:46:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726714AbgIPRB5 (ORCPT ); Wed, 16 Sep 2020 13:01:57 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1940FC006938 for ; Wed, 16 Sep 2020 07:05:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=0sirvuqazzshCbZE24EWceX5REl9RxuudL9lwx7DoI8=; b=Ic68Vg7iyk7XWdoCFOawJ0CNKp ZBJRI2EhsHoaSgmtHmVarEKFFat/VArg8kjPcBj7hxHQkOTJv1WZXOkILeYWjd/Kxwc0Jo1rViaZh bEIui5SolkKPEmzfMjzkH+kuiF07ugy1sIDdWgl0oAUD0k9IwHsf0C9al2CcEUcNQQZIhD8BpZGx5 Ukfphf342knAcSRXLw69kpJWU56h/tWDbgUJHCElCEgSvBgvqj4a8Ldazy1wYXioiv7ngoC/s2nd6 WssGZgS1W7QHgSkn8WjEp49/qyK4vzXpqoLoO2sLFUoJzcdpsLzRCidJVZAySVJFp8oAZd95g04j6 m0lW+mLA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1kIY3w-0002D6-OJ; Wed, 16 Sep 2020 14:05:32 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 44BC33006D0; Wed, 16 Sep 2020 16:05:32 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 33BED2B9285F4; Wed, 16 Sep 2020 16:05:32 +0200 (CEST) Date: Wed, 16 Sep 2020 16:05:32 +0200 From: peterz@infradead.org To: Jiri Olsa Cc: Namhyung Kim , Jiri Olsa , Arnaldo Carvalho de Melo , lkml , Ingo Molnar , Alexander Shishkin , Wade Mealing Subject: Re: [PATCHv2] perf: Fix race in perf_mmap_close function Message-ID: <20200916140532.GA1362448@hirez.programming.kicks-ass.net> References: <20200910104153.1672460-1-jolsa@kernel.org> <20200910144744.GA1663813@krava> <20200911074931.GA1714160@krava> <20200914205936.GD1714160@krava> <20200916115311.GE2301783@krava> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200916115311.GE2301783@krava> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 16, 2020 at 01:53:11PM +0200, Jiri Olsa wrote: > There's a possible race in perf_mmap_close when checking ring buffer's > mmap_count refcount value. The problem is that the mmap_count check is > not atomic because we call atomic_dec and atomic_read separately. > > perf_mmap_close: > ... > atomic_dec(&rb->mmap_count); > ... > if (atomic_read(&rb->mmap_count)) > goto out_put; > > > free_uid > > out_put: > ring_buffer_put(rb); /* could be last */ > > The race can happen when we have two (or more) events sharing same ring > buffer and they go through atomic_dec and then they both see 0 as refcount > value later in atomic_read. Then both will go on and execute code which > is meant to be run just once. The trival case should be protected by mmap_sem, we call vm_ops->close() with mmap_sem held for writing IIRC. But yes, I think it's possible to construct a failure here.