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 56076C00A5A for ; Tue, 17 Jan 2023 23:15:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E7FDD6B0073; Tue, 17 Jan 2023 18:15:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E2F156B0072; Tue, 17 Jan 2023 18:15:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF8236B0073; Tue, 17 Jan 2023 18:15:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id BF3CC6B0071 for ; Tue, 17 Jan 2023 18:15:50 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9213F4044B for ; Tue, 17 Jan 2023 23:15:50 +0000 (UTC) X-FDA: 80365850460.30.13B1668 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf08.hostedemail.com (Postfix) with ESMTP id 1C1BA16001A for ; Tue, 17 Jan 2023 23:15:47 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b="L/0tR0eK"; spf=none (imf08.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673997349; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=10ybaKGDEMNrZJwsXfzl1FfnbRmTCPN7YpUINfERZ28=; b=o+JnWFq1VS0jkfaf78YpxG7sPTEPCusDrmG8ZE2hW0yFstn0S4hZ2xGWQfSgorcUqyo1s7 /aM7zzrWRKmxHJJf24VW12llh4hk26kyJNAZIsYxiFiuK9SCZuibaBvLL2CF57/Whc6mw0 XiwWI4Xoo2OOIwcIULIFQU4a6ejtYr0= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b="L/0tR0eK"; spf=none (imf08.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673997349; a=rsa-sha256; cv=none; b=tLHAhdbyF+NMvPGCvtbYO0+ucOlYBshJJx/YImdDCrxBAagJKp3c5P3EXDO7e9qtHgfd/T T9P0PYablsQlaeKrJ5J2NqG+kYp8Gypc7R/RZ4YX+aiyuqc4HttR349Nvi2vJ+CiuJnQNN gnx/WMe+e0Jr3b0ZiliKcl/15/USJhc= 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-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 1C1BA16001A X-Stat-Signature: wfzf5in9h6jcggoo4tetbm6owrgq8ogj X-Rspam-User: X-HE-Tag: 1673997347-382634 X-HE-Meta: U2FsdGVkX1+ji/fJgzK7ga9zBQQjtjBhu6I2BdVh0jYajwXyJPo1qJXG38CToJJ4F1bGEtkTdWWy53aQFbU4kjVmkUAOlaCJo1EiBUV0Hr1dTBWm2jiCn/Mrs7ydTr9ioNcnIbJVMtuLYS8ZCWuIla7EibuloUG8RvUpQ+vIs6KVxVPdkfeHf2YrRaaTRQXK+nmbAyYvhF+rM8CGTALu7Z5Sk7Irvstjli+XWqmtJKneKRxPJL01rt1rsAxHwQNXHNQJvuvQ0xbF7Bekg8+tny7fgivWumrNYeLnxsMyPDIK9Tym0kTXsS7WZigG3sZn2Jz0Dj9mY/Nad02hgSy+mDOjh56DooriRemWcnu5614g58pv76Egq/RaLFUJNO2NdP0/QXhHUuriqYIKkQKDVlQ7hdPDkm1sGX9ivPXX4l5I8qpYfQoqWZ+beenWBNhfC5i//bhcvYRmIsDrXR4UnlCvstdIPNn9h+sH/CkeosNcLjy+IcqtuxC2C5ShwV6aVNN+OZEISWJPHRS2Uk7mfE8ayXmuHFhmHOnq+0IzcYLVIhiBz9UB2EByko+Ns8uay4iihgyvUXBeReArS/ng2xtN3Noz7zIuv8dyl2Zdzb78IVApM3AICj0ZF2Dv3cpNY3t6UVIms+KxOCpwQEFUCnPH2FrMKGIQpO/xfBZkDJq1fIyiFWlQyXL2HewKAKcO4absax0Gxtaf3n2D6IPqAJ0wXEbF95lIbuoG+MAbY5B0HjYWGiCWt+psasD2PobSORCFK5X/FSRf+uVSirysXZs+NIjCBgVXn5tK9t+pnZGO3kbQdPPyCdnSOWdtDpHjfmvWJJDZtoLCDFH+2v8cusUZPBWqP/ZwhCyigyVvb9Q= 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 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.