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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 32BC9C00A5A for ; Tue, 17 Jan 2023 22:38:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=sX1MwhzgAp/C31sD5eXuucky6lnhNmrO3I+pXPNgY/I=; b=4A5zQ4LPACwpn+ tsA/E6j60K1WYQPvUIh/FtzBdeDhrZQRXnACxbZAKUDveNDSFowiTfhL5KEkjvGqAyu/eh7RgET0Y G3uY+zXMspPdYHkPVjl2rcrx/+dxKBPjvUwyLzZLqVFibmx5PsUoJUGiBtmauzPLSI49c4ROgv+us CJGXhpEvYgcjRsotwV13IMhFUrQrjT9W8V31gOIzIW8waFHPpG/CKQAMm321Yyy683uDPJC8DB/yw tNv37lVRU3XIMmRTHlMNeiA9s2k/JaJCBHY2pPwIFsw9sGQyD7Ros25SWwWf8Dl+lVfTTNgM3cd3a 8LHisXDLKBPmOgbWdMKg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHuZo-00G7Kt-0f; Tue, 17 Jan 2023 22:37:08 +0000 Received: from mail-yb1-xb2b.google.com ([2607:f8b0:4864:20::b2b]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHuZj-00G7Hr-Ez for linux-arm-kernel@lists.infradead.org; Tue, 17 Jan 2023 22:37:04 +0000 Received: by mail-yb1-xb2b.google.com with SMTP id 20so16901197ybl.0 for ; Tue, 17 Jan 2023 14:36:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=lm5RcDiW+2rGYFCauDaSwKbjJDggVLN2u6xrwVauKK0=; b=KfwF+ChqQI+spUNrTrCSVtdcoF8j4D0OPeze22WkmAhXl1Ur+BZ1OCwKXZcEIx8hfQ a7ONLKPiTn/QuLt6KSIjYS4Y80WenNjPid00GPCbKS8iDZHStcl5tUCHc3R3GK/eDLoG QPWWrKo4rNJk86Jhb+yPUhpv6SQMlfPAFQ73OSt5M/CLajTFfWZXd3Wxwp12inxJu+y6 lP1Od8JnRwb/hbZQYbCYZy5IrI1v2NmOpcMZCXJTOwyTANa2XaglPW8Il8Q+vMrbXtJ/ niyJNGhwX1gcX6jFIAyiAosZn3ypXcghVF309a2hAgVCwZY7kHVhn903odOn3nwV/X6x YH+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lm5RcDiW+2rGYFCauDaSwKbjJDggVLN2u6xrwVauKK0=; b=0saPpwzcV+EvAvx1bf1v1xMX4UscAO0uV24yfM9gqrUastJ/K1k0X8AGweLgiZU2Zr WX0FyxxV7Ri87+DwgcrEANh/6ee5fsa9HVY+wGIouzD+7R+9UICWZnnhjaFZlwPWOj1l hUuMkH7W0uG3rXWO57weCW/ujTOXQZ1B8xjkMRn1X/6Cj73OxIc25EggRhxzGfnLe/Jj cJYQhO39i9y5L1+iNIarCWfyyNhdVku+92AZB9XovCCKI3g/kY9BQlhMzbe7UcSc3eSW qVdl9ETV5DGxqWV0E6ifyA0beauMyG+0ve4IVzCIXNSNrgOn0TfZPuNiiTecw2fO2shO 4ZYA== X-Gm-Message-State: AFqh2krGX4vdIKFpjHAI3L9euE43o2iwvAzPNs0hAI0lWffPGA7vAShM ncj0v3LuVOqrV2brLMvug6o+1r3/LYXWYQRNGJqlEg== X-Google-Smtp-Source: AMrXdXtGV2vg2X6848Sq5L1OnzZ5Haar7yCqj+6oRCCeUvwlEhxuO6ABcx4I+h0UVyNSvKE5u7BBhe5bspBtFhh/7Hs= X-Received: by 2002:a05:6902:685:b0:7e9:646d:2da3 with SMTP id i5-20020a056902068500b007e9646d2da3mr702515ybt.340.1673995017981; Tue, 17 Jan 2023 14:36:57 -0800 (PST) MIME-Version: 1.0 References: <20230109205336.3665937-1-surenb@google.com> <20230109205336.3665937-13-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Tue, 17 Jan 2023 14:36:47 -0800 Message-ID: Subject: Re: [PATCH 12/41] mm: add per-VMA lock and helper functions to control it To: Jann Horn Cc: 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, willy@infradead.org, 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230117_143703_522167_6D3AAAD1 X-CRM114-Status: GOOD ( 32.62 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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: > > > > > > +locking maintainers > > > > Thanks! I'll CC the locking maintainers in the next posting. > > > > > > > > On Mon, Jan 9, 2023 at 9:54 PM Suren Baghdasaryan wrote: > > > > Introduce a per-VMA rw_semaphore to be used during page fault handling > > > > instead of mmap_lock. Because there are cases when multiple VMAs need > > > > to be exclusively locked during VMA tree modifications, instead of the > > > > usual lock/unlock patter we mark a VMA as locked by taking per-VMA lock > > > > exclusively and setting vma->lock_seq to the current mm->lock_seq. When > > > > mmap_write_lock holder is done with all modifications and drops mmap_lock, > > > > it will increment mm->lock_seq, effectively unlocking all VMAs marked as > > > > locked. > > > [...] > > > > +static inline void vma_read_unlock(struct vm_area_struct *vma) > > > > +{ > > > > + up_read(&vma->lock); > > > > +} > > > > > > 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. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel