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 08D26C54E76 for ; Tue, 17 Jan 2023 18:56:50 +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=3gipkUv0Pc0Dc+ua3gDNul7q+vshd/DFR+sKTX6AG3w=; b=SQGUb+vIo9vXlb KiRVKpGt/9d52/RJfHUMN0Ol+IR+EbRHxDxWr8eOYCIdyMJ2D9W0O5an/sEvy6SyePwIg/tEfFbee JiOn9JrvBEdWQhMh2BZlsS3Z250p8BOemWqCtAuTauI6Tep/xAnruLz8es7IaCLfKOP7KDumkI2r+ iKU/dvmXKe8gipsTycHp0LsdPeDp244LMi4QmltfJckB4uxmqHXZ73+prTBBnFbnOhySIdWNTSnDy VBw1yg8yzfeCKQAPzWUfMvdgX2X7ijN7EhCyJYfFfLN3C4bitazZnJh/e56jA/WqCzLsqCr1pnKKM ERkV7V9LzxRCM5DJYjEQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHr7g-00FW1Z-2i; Tue, 17 Jan 2023 18:55:52 +0000 Received: from mail-yb1-xb32.google.com ([2607:f8b0:4864:20::b32]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHr7c-00FVzq-FX for linux-arm-kernel@lists.infradead.org; Tue, 17 Jan 2023 18:55:50 +0000 Received: by mail-yb1-xb32.google.com with SMTP id p188so35277600yba.5 for ; Tue, 17 Jan 2023 10:55:46 -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=Hjk26ZZoZCfL6kjko8l/pSHnRgndFb4Mk147LRzA91E=; b=Mt+sJp5dwuIsL+Ko5cT1XtmvNX0+vXcya9w3KxCAGiP5wEFud5o530HskF9og8ZMkd ljvA9NKCih7KjYskMBb5clv4araVsgVH8bivehaUbVs1zXmC/Q33zJ2WQgBH8wLEh0Ww LO52zNPDud7WwXKOiScqFdlxb/HA84LcUefakhGKNdWzRjfSLYoc+FzRvi5aIbasr4/1 iGt43X07P5GiJPZJVy/5QSn6qq+xCbFLi962ogszxOLly8aqpiMkM3flx1tsWJ3TC+lM JwJqwtVwEAaEziK1WoW3RHASnzImrfdF+niEuev4vFd01otF7C+PJQGmm/v884HYR4zi v9EQ== 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=Hjk26ZZoZCfL6kjko8l/pSHnRgndFb4Mk147LRzA91E=; b=7Vu9JgdyGie4bdbirbvTdNvpcu8Ar6BUjT7xSTIKfaKGS86Ikky4kczFjlEtiKD/ci 8MQ9X7thMx7CDBYPBbS7VfyBpCJMHRO0wic1dtkDbrkoTdLhzbUtqzOjz1HAf/27mjz8 fgM1MDZA+gJsOVrKdYAVzSv/4IHpzOpDYFQ0EmXwyY3CaYztdZ0xCLvU+fUEACvlTzcm ghWubMaQRxPoRCpfDBsE/OsTy4T1zbzCviby7UndsivFOrDk+wpzK6/t3JMqOQ2m+VUY 7SjpsJqqihRNR7435Qwp3daLe6MbD2S3PsP2k/ybOOHBik1XtbJcxfjHRUqQHW/xFZcN u+2Q== X-Gm-Message-State: AFqh2koKjdjVjyv+SxCw7gEsKSWZLnqo1+ZS6fYhOuTqAmjhuzNMpb90 tqIy9V7E6z4HdwNmiLfkrzsZTILn/SUuPy+L4dz+gA== X-Google-Smtp-Source: AMrXdXsB68dzuUro7SI9IrfjHxCTvw8eBu3xk1m8gxflhvDRkqqF3qc7tx3EmnQTV75nr092zIYy1Q+gFJMC6cvTzv8= X-Received: by 2002:a5b:cc8:0:b0:7ba:78b1:9fcc with SMTP id e8-20020a5b0cc8000000b007ba78b19fccmr507280ybr.593.1673981745113; Tue, 17 Jan 2023 10:55:45 -0800 (PST) MIME-Version: 1.0 References: <20230109205336.3665937-1-surenb@google.com> <20230109205336.3665937-42-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Tue, 17 Jan 2023 10:55:33 -0800 Message-ID: Subject: Re: [PATCH 41/41] mm: replace rw_semaphore with atomic_t in vma_lock To: Matthew Wilcox Cc: Jann Horn , 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, peterz@infradead.org, 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_105548_549743_76931490 X-CRM114-Status: GOOD ( 28.52 ) 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 10:47 AM Matthew Wilcox wrote: > > On Tue, Jan 17, 2023 at 10:36:42AM -0800, Suren Baghdasaryan wrote: > > On Tue, Jan 17, 2023 at 10:31 AM Matthew Wilcox wrote: > > > > > > On Tue, Jan 17, 2023 at 10:26:32AM -0800, Suren Baghdasaryan wrote: > > > > On Tue, Jan 17, 2023 at 10:12 AM Jann Horn wrote: > > > > > > > > > > On Mon, Jan 9, 2023 at 9:55 PM Suren Baghdasaryan wrote: > > > > > > rw_semaphore is a sizable structure of 40 bytes and consumes > > > > > > considerable space for each vm_area_struct. However vma_lock has > > > > > > two important specifics which can be used to replace rw_semaphore > > > > > > with a simpler structure: > > > > > [...] > > > > > > static inline void vma_read_unlock(struct vm_area_struct *vma) > > > > > > { > > > > > > - up_read(&vma->vm_lock->lock); > > > > > > + if (atomic_dec_and_test(&vma->vm_lock->count)) > > > > > > + wake_up(&vma->vm_mm->vma_writer_wait); > > > > > > } > > > > > > > > > > I haven't properly reviewed this, but this bit looks like a > > > > > use-after-free because you're accessing the vma after dropping your > > > > > reference on it. You'd have to first look up the vma->vm_mm, then do > > > > > the atomic_dec_and_test(), and afterwards do the wake_up() without > > > > > touching the vma. Or alternatively wrap the whole thing in an RCU > > > > > read-side critical section if the VMA is freed with RCU delay. > > > > > > > > vm_lock->count does not control the lifetime of the VMA, it's a > > > > counter of how many readers took the lock or it's negative if the lock > > > > is write-locked. > > > > > > Yes, but ... > > > > > > Task A: > > > atomic_dec_and_test(&vma->vm_lock->count) > > > Task B: > > > munmap() > > > write lock > > > free VMA > > > synchronize_rcu() > > > VMA is really freed > > > wake_up(&vma->vm_mm->vma_writer_wait); > > > > > > ... vma is freed. > > > > > > Now, I think this doesn't occur. I'm pretty sure that every caller of > > > vma_read_unlock() is holding the RCU read lock. But maybe we should > > > have that assertion? > > > > Yep, that's what this patch is doing > > https://lore.kernel.org/all/20230109205336.3665937-27-surenb@google.com/ > > by calling vma_assert_no_reader() from __vm_area_free(). > > That's not enough though. Task A still has a pointer to vma after it > has called atomic_dec_and_test(), even after vma has been freed by > Task B, and before Task A dereferences vma->vm_mm. Ah, I see your point now. I guess I'll have to store vma->vm_mm in a local variable and call mmgrab() before atomic_dec_and_test(), then use it in wake_up() and call mmdrop(). Is that what you are thinking? _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel