Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp6510626rwb; Wed, 18 Jan 2023 06:10:30 -0800 (PST) X-Google-Smtp-Source: AMrXdXt8jzFZcVPPpiN4I2g5T4Xvqg0E0CqRhTPBtJwECq5UyEbqkMfQQUKXIdXTztYjUfTJVoJB X-Received: by 2002:a17:903:41d2:b0:189:ba1f:b178 with SMTP id u18-20020a17090341d200b00189ba1fb178mr9868325ple.9.1674051030306; Wed, 18 Jan 2023 06:10:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674051030; cv=none; d=google.com; s=arc-20160816; b=rOEzYIlh3JoJWA2RhcyXbTRB3p5+nMRsKG16nVZ6zlfjsPOYPaRHTq9GMenlLT+VNr owqs13WMpXCs96H+TY8Z+GggHyYznK5DLsinZ1WRwyLZG2lRMICwQqWyjjr+iSk1m4IF DHGud50/FPi/YxCNJWSEWgtPLgrGiG27rhdb5lerMZfgrGOzIm5UfbB/ftGF70y/hWTL vyF8v+I80ZHXvg0JqWj5LCj+sZdLqKJPEp97M9tnJZfgepFNf9ZLyU7vwRuTTMmAoIq2 V6pP717Hxaj72nNDhzOUTtLMbpyF7i15QzW3qZjSO8iOmXjOdC108lWA4hcxy+MJDJhe 4VDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=0HpDGQASu6jsCGakE+zyQJRkRHgKI6AmZnZRKwM4p1o=; b=uu6gwgGdgf8cNGaaBAq4oBLSl/+Ic++umo0McKeP/iKTr41S2C7BcjO3O4o2Ix78aa 4q23Hhf51O0+nLM0cL/CtURmLx4woNtm+X5RQhRGQWZ3HDguWoldOWlWZmwNhuUTcjOm ZZ1S9YNvIGM51JCiLEhpnOPgE6JR+eyg8MCY5RfXEUye88lcmbBTPC8AmvZxkVLwZfG4 YPrzf8rE2uI3vqtXLiz3r7w2lrD5NfqkysiKSS2s1w8+YJY8iB/jPMnUGmApBvR9u416 IMzowJqz77mKaMteFbjKmWiLScEmCD/a7BKAV4m8+y7LgwDa4Ev+hNu3osIqH8XV30NV 0pIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KZxUrgqH; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e3-20020a170902744300b001929550b3e4si32841156plt.466.2023.01.18.06.10.23; Wed, 18 Jan 2023 06:10:30 -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=@google.com header.s=20210112 header.b=KZxUrgqH; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230246AbjARNxn (ORCPT + 47 others); Wed, 18 Jan 2023 08:53:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229960AbjARNwY (ORCPT ); Wed, 18 Jan 2023 08:52:24 -0500 Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 629C93865A for ; Wed, 18 Jan 2023 05:24:10 -0800 (PST) Received: by mail-io1-xd2a.google.com with SMTP id j1so8135503iob.6 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=iK8HdUSefMxn1Fh8TyGUi2Ebiv+0d2K3qfRB/kcRcS+ZD79JdFWsZscou07L+JLHTz Dg3BMugOs67/UMKcNzVmQW2RdVCpP5JtXCt8gqfOqWJb8truvOyzV2tmss5gPl0R3go5 GoURyZbCzyiFjkFeSbs1LMTnObuuoT+dJjMdU+v6UH246aLsEVRojQbui7aAlJoeJ8Xq II0iEQPsFz9OQRPFvguRLxYUuTW28jTKb/HWt95k9Uc/sobb/iMDz7BVBUQKPGIKX/5+ 5lM83+Z/RX1l3ra0l/7fWrH20OQ9cWKScanmxQYN3Wdod27PO2ljx3Xy2ttKriDeWiIo uPLg== X-Gm-Message-State: AFqh2krQsA4SpsOp8Ag5aZbZzMcCoAJ/kJ2/Xirqs0tyzDyczdLBz9Xa nH9NKO9FDXv7fMQHbTZrpM3xXLPK3Ugi6J/qygMzhg== 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-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 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...