Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp5635905rwb; Tue, 17 Jan 2023 16:51:57 -0800 (PST) X-Google-Smtp-Source: AMrXdXvRoi11y4g3dowoaFxK3ROBtEvk9i4bYc8g36iw2XnTzw4CIGikTNexKc6BBCudXLmz96ME X-Received: by 2002:a05:6a21:3987:b0:af:be2a:208f with SMTP id ad7-20020a056a21398700b000afbe2a208fmr5226325pzc.33.1674003117144; Tue, 17 Jan 2023 16:51:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674003117; cv=none; d=google.com; s=arc-20160816; b=SZ9jnsgvz6fbm12+YdTHCRgGNWL40Pmhcb+ZuCdYprfsjB1NdqGWCkOlfclBcVmDqb 9ahRqtMDUjDPsKFkuS5gMANSbBdtSsENHSDUv1eD4J21FqH+4MqdRL7wPVuF+RTafI2H YkDlgQgxrgQ3FkMHV7GuWtEi25b3und32B2GEscpm6pt6Fwhk7NcBzvCW/NS/rjMns/C vJYZwaWny7zdc11aoorAzRDxIPhMKWjw833cOuvKRcDIGUvB+DvwdiovErayGYLW5yo+ ePbwJ89l5nFN6h17epUK05jm9VFg6oCDVbJ9VkgNSRcNFEgBTJsBHgtKivKvlbquQP1+ M4NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=10ybaKGDEMNrZJwsXfzl1FfnbRmTCPN7YpUINfERZ28=; b=wD4tV9ucm6R+k7gS9Yik74qcuvVxfYLFg+xEiLslCvFvgfIgiUTwv5fd0pt0ekVs8b LVioWO4EcjnXeoH9kN6dnv4ggW0GNhRWUiMGmLaRYBeUTF1jJOfKpnSMlPGGTwbR1lHt Lnzns0UCQH7ON9PU+2KYvsx/5I3dGlnas/TTsHgKJERV5FG2cTy5KKHXBm8rIzdveVeR LQ5dk6WgOxTnM5gIkXk4tHSr2Q+CdjwQwdZSP91VMEeXFJG08YETPfQ9MXGP/4Qro/K5 bo0SMSqcxrur2nXCkNC+Y49MufUr5sUWHq8j4hqv5rIpEvOWK5VED9adBDWjiC+VqO1n 0+4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="L/0tR0eK"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o14-20020a056a0015ce00b0058dd8430b2esi2090335pfu.213.2023.01.17.16.51.51; Tue, 17 Jan 2023 16:51:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="L/0tR0eK"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229550AbjARACi (ORCPT + 46 others); Tue, 17 Jan 2023 19:02:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229509AbjARACP (ORCPT ); Tue, 17 Jan 2023 19:02:15 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8C27B4E1E for ; Tue, 17 Jan 2023 15:15:57 -0800 (PST) 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=10ybaKGDEMNrZJwsXfzl1FfnbRmTCPN7YpUINfERZ28=; b=L/0tR0eKtGHYZtRFgbb4J/TazM jwcMUPJtwquGQc7O2qRObrMoc3DbrRyrPw6llbDvvKc5YR+S8oxReiTZyiHgud38bANV1BvJGVmPw ZBreiPK44g//2XbqFyhvcLxFA1G4radxeNrEsltk3gFb/MO2+7BwuQfND0zlYFjiNrTO0iqmTMIHY 6uf/lY6WYjCAp8nD1X/48axQ/8Csodfz/VbklYybkBH7olSZLVzmpCcW0/EEe0ND1WhiDbhXowLYY zk0RSsGT5eOn/3NHhN6A1QjXlTe2kTSyOLUzIG/lzVj7BI/KNG2VFr2sJfBSsh9nGSt7w8KZzyYH5 BUnxXFnw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHvAp-00A86K-5N; Tue, 17 Jan 2023 23:15:23 +0000 Date: Tue, 17 Jan 2023 23:15:23 +0000 From: Matthew Wilcox To: Suren Baghdasaryan Cc: Jann Horn , peterz@infradead.org, Ingo Molnar , Will Deacon , akpm@linux-foundation.org, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, dave@stgolabs.net, liam.howlett@oracle.com, ldufour@linux.ibm.com, laurent.dufour@fr.ibm.com, paulmck@kernel.org, luto@kernel.org, songliubraving@fb.com, peterx@redhat.com, david@redhat.com, dhowells@redhat.com, hughd@google.com, bigeasy@linutronix.de, kent.overstreet@linux.dev, punit.agrawal@bytedance.com, lstoakes@gmail.com, peterjung1337@gmail.com, rientjes@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, hughlynch@google.com, leewalsh@google.com, posk@google.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH 12/41] mm: add per-VMA lock and helper functions to control it Message-ID: References: <20230109205336.3665937-1-surenb@google.com> <20230109205336.3665937-13-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 17, 2023 at 02:36:47PM -0800, Suren Baghdasaryan wrote: > On Tue, Jan 17, 2023 at 1:46 PM Jann Horn wrote: > > On Tue, Jan 17, 2023 at 10:28 PM Suren Baghdasaryan wrote: > > > On Tue, Jan 17, 2023 at 10:03 AM Jann Horn wrote: > > > > One thing that might be gnarly here is that I think you might not be > > > > allowed to use up_read() to fully release ownership of an object - > > > > from what I remember, I think that up_read() (unlike something like > > > > spin_unlock()) can access the lock object after it's already been > > > > acquired by someone else. So if you want to protect against concurrent > > > > deletion, this might have to be something like: > > > > > > > > rcu_read_lock(); /* keeps vma alive */ > > > > up_read(&vma->lock); > > > > rcu_read_unlock(); > > > > > > But for deleting VMA one would need to write-lock the vma->lock first, > > > which I assume can't happen until this up_read() is complete. Is that > > > assumption wrong? > > > > __up_read() does: > > > > rwsem_clear_reader_owned(sem); > > tmp = atomic_long_add_return_release(-RWSEM_READER_BIAS, &sem->count); > > DEBUG_RWSEMS_WARN_ON(tmp < 0, sem); > > if (unlikely((tmp & (RWSEM_LOCK_MASK|RWSEM_FLAG_WAITERS)) == > > RWSEM_FLAG_WAITERS)) { > > clear_nonspinnable(sem); > > rwsem_wake(sem); > > } > > > > The atomic_long_add_return_release() is the point where we are doing > > the main lock-releasing. > > > > So if a reader dropped the read-lock while someone else was waiting on > > the lock (RWSEM_FLAG_WAITERS) and no other readers were holding the > > lock together with it, the reader also does clear_nonspinnable() and > > rwsem_wake() afterwards. > > But in rwsem_down_write_slowpath(), after we've set > > RWSEM_FLAG_WAITERS, we can return successfully immediately once > > rwsem_try_write_lock() sees that there are no active readers or > > writers anymore (if RWSEM_LOCK_MASK is unset and the cmpxchg > > succeeds). We're not necessarily waiting for the "nonspinnable" bit or > > the wake. > > > > So yeah, I think down_write() can return successfully before up_read() > > is done with its memory accesses. > > > > (Spinlocks are different - the kernel relies on being able to drop > > references via spin_unlock() in some places.) > > Thanks for bringing this up. I can add rcu_read_{lock/unlock) as you > suggested and that would fix the issue because we free VMAs from > call_rcu(). However this feels to me as an issue of rw_semaphore > design that this locking pattern is unsafe and might lead to UAF. We have/had this problem with normal mutexes too. It was the impetus for adding the struct completion which is very careful to not touch anything after the completion is, well, completed.