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 43158C38159 for ; Wed, 18 Jan 2023 13:24:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 722296B0071; Wed, 18 Jan 2023 08:24:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6AB326B0072; Wed, 18 Jan 2023 08:24:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 524886B0074; Wed, 18 Jan 2023 08:24:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 3D7D16B0071 for ; Wed, 18 Jan 2023 08:24:18 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 13D4C1C5E1E for ; Wed, 18 Jan 2023 13:24:18 +0000 (UTC) X-FDA: 80367988596.19.9E67096 Received: from mail-io1-f42.google.com (mail-io1-f42.google.com [209.85.166.42]) by imf04.hostedemail.com (Postfix) with ESMTP id 860A44002D for ; Wed, 18 Jan 2023 13:24:10 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=KZxUrgqH; spf=pass (imf04.hostedemail.com: domain of jannh@google.com designates 209.85.166.42 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674048250; 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=0HpDGQASu6jsCGakE+zyQJRkRHgKI6AmZnZRKwM4p1o=; b=qYwhurI8oHshGwViZw5C3gJ9uAVOwHYtW/oniXlik0EWlEFAgex6vdp8RtfIk8BZUSnHrN wzAAAG8ilw0JbYGaSHM9OJaiTEOKRykt2KDB1cYRxAqIteI/ZwkBNma2nBGpvwnFYQOvMn wdCPRU+ZddS3ePSmmd0veq4IZP4c6/s= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=KZxUrgqH; spf=pass (imf04.hostedemail.com: domain of jannh@google.com designates 209.85.166.42 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674048250; a=rsa-sha256; cv=none; b=MBA/QeLizcPoNhmDH06XAx61i0FZCIQZFC5N7nf+mbcP7mn4vnErRLaIRDNk+KDyFFkAbB 2nV/b/7qaGcE1xKCIQRtvMsUcWxn8VHlVGd6Np8IoefZYy5HAjs4H0N/+PcBRkxfHRr783 R0azKtIXisvtQTIqA5xzlXaTwdOypUc= Received: by mail-io1-f42.google.com with SMTP id n85so8756936iod.7 for ; Wed, 18 Jan 2023 05:24:10 -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=0HpDGQASu6jsCGakE+zyQJRkRHgKI6AmZnZRKwM4p1o=; b=KZxUrgqHOW6I35Thp3B9indQq7cNnlsKLrVWZ68ThfJfnc4b/iqwH1v/YsPHPNW6tp V0YdIdO+lePrdsnwJWIzf9EZxzjMQV8s2oSdmZzbMaVvMUHcivT98VYHp4GmPgxfVJiY QxdeupqxD5r1mKUYxTxYX6YDjubBfnW+bX+37WdITk+kfxRSs+qUwQDLOUr2sodzGy2R MfEBWGAPloJqULLT0MZkW9ZE6qzOTFs7ToE5ut4M3pmXc0glt5lwYpTTirTHRVX3Wzu1 0tT6YSymo3X385zYRZoJUJCesQvXGA7sPFARjqlTbO3/6fTFQPXzfX9Xa13K1La1cvUB MMjA== 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=0HpDGQASu6jsCGakE+zyQJRkRHgKI6AmZnZRKwM4p1o=; b=1YNG085O7nD6lP7FwyQJUe4tvhGk2xjtvz493VqJIDEuBo8xHOiS+alXDBi3X6tyIT tJbsZHruwoIFpAs6OJ5rCFXEzsqJRUdqkQPm46AtHOhHkc4Cj9hLbhhcE+qkN/BcMsrz ZS9hs8ZiR7nZLTe+MX8TKqkipFBxwtpSzhUdDScF6Q6Qu399uPKyd8+mdTqKoaTzQM+7 vu8bdYWr+QBQumYiypdpaC0fc3VttUd7eZtTHcEMOlY/y4I82VQSPSuN5UJCT7e8veAh VhPAvzKDaQKGRRp5frF9veHQ7QOnP1KZvo8sZ2EMdjRjiuMQz/0P/M0JHM2Qv1a4KKOO ztMA== X-Gm-Message-State: AFqh2kobMtGkrmjHVt8tHrOjGXivo+hqS1BCXb9DsR6a3xrJlDXLeh/C BL+ELbAsSsSLaEHJa8r7zPgPp5LjcPILku6oFu5GJg== X-Google-Smtp-Source: AMrXdXtVpr4jvFh+Nzy9mDXPMsxLoU3U3NYpeqIF3hovLZvVNxSrOgQi9liraHCx6fDJZNKaow5tcJKbajRkxzFbjws= X-Received: by 2002:a02:2a4b:0:b0:38c:886a:219a with SMTP id w72-20020a022a4b000000b0038c886a219amr814437jaw.133.1674048249506; Wed, 18 Jan 2023 05:24:09 -0800 (PST) MIME-Version: 1.0 References: <20230109205336.3665937-1-surenb@google.com> <20230109205336.3665937-13-surenb@google.com> In-Reply-To: From: Jann Horn Date: Wed, 18 Jan 2023 14:23:32 +0100 Message-ID: Subject: Re: [PATCH 12/41] mm: add per-VMA lock and helper functions to control it To: Michal Hocko Cc: Suren Baghdasaryan , peterz@infradead.org, Ingo Molnar , Will Deacon , akpm@linux-foundation.org, michel@lespinasse.org, jglisse@google.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 Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: g9omiyijhrmge3cxzbf95ehz8amxtk89 X-Rspamd-Queue-Id: 860A44002D X-HE-Tag: 1674048250-711281 X-HE-Meta: U2FsdGVkX18H7jqshOUdsywlFYZTSOVUdJeLmBmVR8JAb9C1X/T/hwV72TefGjrRGydCInag9+pgPTrccGrbton4F4AuSe1orQGe2IXKXewFJrgPcLGefb8xsrlA8gIPKrkn6dZKr1uN/6OcJsSIPYapGDchJCf7a78VNscbmD9X4Oa1IE1QzfmSlpmqimOoC51B1qBL/zCBk4yrYI31KClB4DfBBqCrxLxUtoFzoJhnAKIWVwpGbGH8y9bvtmnqqej08p/QoIN0few/eOxrfO7yGpibHfeeoxknKVb/7sBDe2z1KdQ2t18go+cReGtka4hGJ8mThfFdKMaqwbWOuoN0cwTXf+u4HZX7zpaPKO+wX73QGBoEGF5t9h06QnMj67yihtpi9AcbVoXRrSJGvMWAV7txIw3KUTw22yWxWvYaqoWR+QYwpLUdGQWdwoFU6aeAMw17Ms+Sj9mSZ0csdgGJk058ymLbcGCPNPEgUfkzOXEbVPjl8Rx1EUFtEUUXk056LD41UQ8rKTVF9YB7uSWrewC970DzD/mmn2EBhNjUkS87DQ4+D6c3EIAqYDXmgxBqd5T/n25/+bSi7SDjGmB1z4bHl/I5grxoLJi16D6A5WJCUNULmTB5SB9Otpalu3qm+RdfsSG7/ys1k1xuO81bN3KMNLQefwhjnFa4VE2G6Vdpqo+BviTZsTMHTY6GSwiMPoheMhrA0xwof+qMdad54/PWRU8diXJlXb6cwYfGLnBV+B2WMEL+l45bGRKUUSMroBn0/dQJhjAZRLO4fN5ffIo+nyhAlHbmtyk2ltQRdgZLaxRN1vNuubEwF9MsIiIWYXtVUHVkdw3tccgcCHq71AdqSyP8ICch38Mdro5k/q3TCIhHI77wRD0CjyZ9mmyNkgH4j/RN+pwlmMaD/llgdVMc7VnK5tFY1YXUwWwohlQoC51pXcglWne49YnP9lD6WM4/+zEY/VfJYRR xaE5q8uN IZigzm2YHEl2k7qA= 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 Wed, Jan 18, 2023 at 1:28 PM Michal Hocko wrote: > On Tue 17-01-23 19:02:55, Jann Horn wrote: > > +locking maintainers > > > > 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. > > Yes, I think you are right. From a look into the code it seems that > the UAF is quite unlikely as there is a ton of work to be done between > vma_write_lock used to prepare vma for removal and actual removal. > That doesn't make it less of a problem though. > > > 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 I'm not entirely sure about that, the locking folks might know better. > > I am not a locking expert but to me it looks like this should work > because the final cleanup would have to happen rcu_read_unlock. > > Thanks, I have completely missed this aspect of the locking when looking > into the code. > > Btw. looking at this again I have fully realized how hard it is actually > to see that vm_area_free is guaranteed to sync up with ongoing readers. > vma manipulation functions like __adjust_vma make my head spin. Would it > make more sense to have a rcu style synchronization point in > vm_area_free directly before call_rcu? This would add an overhead of > uncontended down_write of course. Something along those lines might be a good idea, but I think that rather than synchronizing the removal, it should maybe be something that splats (and bails out?) if it detects pending readers. If we get to vm_area_free() on a VMA that has pending readers, we might already be in a lot of trouble because the concurrent readers might have been traversing page tables while we were tearing them down or fun stuff like that. I think maybe Suren was already talking about something like that in another part of this patch series but I don't remember...