Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp5387873rwb; Tue, 17 Jan 2023 13:04:56 -0800 (PST) X-Google-Smtp-Source: AMrXdXvSiWj9JzoJiZ1+2XB4yTlWIvTPSvw0GS6LKziGm254IPsrxKrGh0559Khd9HZcAnKGidpx X-Received: by 2002:a17:90a:86ca:b0:225:d697:41ea with SMTP id y10-20020a17090a86ca00b00225d69741eamr4226831pjv.23.1673989496243; Tue, 17 Jan 2023 13:04:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673989496; cv=none; d=google.com; s=arc-20160816; b=IhgCJNJ7GBSBoswXOsepwMmzvXfcBNF70ncX0CY/FY4rx7HPJQMvljlKE27b3XO7hG Ix1KD7hc/HuyEzQv9l5ObcmHnpRcdUXJECEwjCX7LxvWITUkZrXS9oiWV3VbUSsD2hyd f2aIgJfXIV2ggeLd/c0/hyHJxtLkVEisTqiChuQQ9tTolbQztcuQjhZnTTf1vznYCXmD 4jTDyi06aoK2ty1x/1IrpP/NPNZK4rwZ+IKTr6cvnQe/1VLQTNxmxD6TZqH6VDYvBYTF 3iSryVk+VZA1BBWOR4ytTDylRLoN71sU2X4Pl2lou/8s/X0Jey7ssJDrj1w6WEOtwLrY HfCQ== 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=4j3tl0DYASqrjkqUxwz46bG4ROPSgLPddwJUIyGPfX8=; b=dowFdspp1pcBJ/LR/XI5Kx1ku0uq4eBxoOMLSzaWa6DVqPgFUXL1//6i6tC4vF3KS2 iyQau2sjHrUe2IOxAUiBhIo0gFE7RZOgmBKG6borT3Za7du4r9d92nDnFsAX0NPaJ41O DKvpLd3ixtnLi7qbU3X6huW6Fr1vR/fpGttraSWnTWn64SYYPmyBTok39Pr9nQWTBjT6 n7kF5vlKZBef5U+Wl5N/ozYyv4899cqDPCah41axX2L979Jnt/WjPEpkppQRYrmpXwX1 o6+OHC8MVp3vqTBtiH9lKDdLMs6L0INxIrD4GIRInmzWQf3FnDWjLkMHBNLWuAW/Zyir GVpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=hBh7iHpA; 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 oo10-20020a17090b1c8a00b002294c989fafsi11358046pjb.52.2023.01.17.13.04.50; Tue, 17 Jan 2023 13:04:56 -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=hBh7iHpA; 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 S235314AbjAQTVt (ORCPT + 47 others); Tue, 17 Jan 2023 14:21:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232731AbjAQTMp (ORCPT ); Tue, 17 Jan 2023 14:12:45 -0500 Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D59637B7E for ; Tue, 17 Jan 2023 10:26:44 -0800 (PST) Received: by mail-yb1-xb33.google.com with SMTP id v19so29278839ybv.1 for ; Tue, 17 Jan 2023 10:26:44 -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=4j3tl0DYASqrjkqUxwz46bG4ROPSgLPddwJUIyGPfX8=; b=hBh7iHpA0qfvzb6NYfBFfodVZoJZG89lkfXRit37mYAmeC5nBFxn8EQS0LUAe3jcVo C5FRsaxj+EKLI2jWmHfjp8neugrXB9pI/nKBzQoYA1YpgFoWtnyOa5v24hYecpIUquUN KSNNz94C5dAuBzIbNy+BM3HlLgWGdJfq88KPVCdqr5XOURSiVIt8NC83fhsqzZZfL4An QFEI0BAISvqo9iX+zoQ1BM4zTAWiTj/t7PqnS+AcCO7l4eOGIMMkEF+giJ7BJLV4QoxO D/AunpSl6jrX3m5nr8I4wWYNarHbEt/jf3IwcNd/YSKXxcntJzTg/UwFDPfUl4301hBU s7qQ== 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=4j3tl0DYASqrjkqUxwz46bG4ROPSgLPddwJUIyGPfX8=; b=0kmhzCyV4vOJvL8Igcw4u3VgQOB9EGGbsl+HHZ2dBLZ+NNZE81qxnOlS1AaK9Dwg65 IEiR9Lf9ei9uCQhuotdLJ9Dg5HNc8VPni0qqRwqcFDI646Ckfjz0wXdvoBpUDBCRo4Tp AdMSK0vrWo/F4rBtePNyFBTdvWbbvpkdOhl3zHj5/UU7iumrZD1e80usi4t8IVcPTUVU iuP++V0oYu7ervp7fd+DkbwT/hukap/lOtLj6ALKGOTIucfhgAcsdPG4JU8myrC+ygMU BfEIld18ItAXzFuROyrVVczvE9GiI8jpbg4HY09KsD98w+Uu9mI6k7KYl17Ie2edPu8o KAAA== X-Gm-Message-State: AFqh2kp1hgn2FD98/cb30hsjybIYCX9iITYiVQhZDzi2yYt/D5AFO45I qQ4l3PHEs9UgOF4Ciy8Xlhb5e+YnD8KcigvLc5vrpA== X-Received: by 2002:a25:ceca:0:b0:7e4:115c:9cf6 with SMTP id x193-20020a25ceca000000b007e4115c9cf6mr537549ybe.316.1673980003301; Tue, 17 Jan 2023 10:26:43 -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:26:32 -0800 Message-ID: Subject: Re: [PATCH 41/41] mm: replace rw_semaphore with atomic_t in vma_lock To: Jann Horn Cc: 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, 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 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 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.