From patchwork Fri Jul 7 04:32:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13304385 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 F0A8AC0015E for ; Fri, 7 Jul 2023 04:32:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A7D88D0003; Fri, 7 Jul 2023 00:32:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32F318D0001; Fri, 7 Jul 2023 00:32:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 180038D0003; Fri, 7 Jul 2023 00:32:20 -0400 (EDT) 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 06CF58D0001 for ; Fri, 7 Jul 2023 00:32:20 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C69D8A02B6 for ; Fri, 7 Jul 2023 04:32:19 +0000 (UTC) X-FDA: 80983543998.28.5A7AA03 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf15.hostedemail.com (Postfix) with ESMTP id E4A52A0007 for ; Fri, 7 Jul 2023 04:32:17 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=eODw2VAG; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3UZWnZAYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3UZWnZAYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688704337; 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=sOqq0WN+jYVzOAHPCJmC3T8ZmV+ri02/8UcPUSwENZU=; b=cIY83qP1u2WC0URkCKmoWXndWOvEd5UIFQKVAXZZYX0jVjWxfRTvrALhzyNjLiX/4a+Fw0 8DDZNBXgfo9+vU5vzkmzmPpuMTrkhlV7BkyMMThUM0O7pItS5iwqQySspk92hIIf7Kkm0x Uk9mPXTagL+jiUYdIzQB21gTSm0XzU4= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=eODw2VAG; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3UZWnZAYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3UZWnZAYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688704337; a=rsa-sha256; cv=none; b=bKr+6jcZyVH1xguJDrvFOInAIj3lhVIdfpuQ4T0XdAgSVxAHVM6/f5EYvQGoJcYAfgjW8g XvuBBFj9UAfKTr4bxNfZNfgBSkK7V8YHGqsb0FNq1nADM0jYl2Gr5/3HjSbw/TT+D233eY kRaBPyXG37zXnWpzscOV9V8X6iif7ig= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-56ff7b4feefso17595187b3.0 for ; Thu, 06 Jul 2023 21:32:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688704337; x=1691296337; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sOqq0WN+jYVzOAHPCJmC3T8ZmV+ri02/8UcPUSwENZU=; b=eODw2VAG4LYwUudSofmYsJnD5sDvgnvXm7FQ6WhI9uyNSxsp0njiK6thMN6LRU5jTF gYmW5dl0SjECbLtK8iZkOH2xEUNg27Y4ApUuh7waJQakQ9qHP1nx+nYn3YKoVQ98VxWz OY4YiR+GiMKjF2MsRbpXNOQvq0vJJsB3vwvYf8qNkr7WvRvnC7O9FwEtRfKLje9Z1BZP 9cOyuGcTKL1Y+u5/k71eAVQKBKvf+ar9q4SEqw+qNvkFIduc73n4qBi271JwmDnSiSe9 HnhcI0RwPWBtG3pS/6ZbVosWthJ25wdjphq0ZVDNfyhX/7kCWTDbFnICRKBZ285B6A6q 2nhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688704337; x=1691296337; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sOqq0WN+jYVzOAHPCJmC3T8ZmV+ri02/8UcPUSwENZU=; b=bjGqW9BIQyEG6iP62yK1eSg6jFADbVN1s0fMAFxgVTaXq5X57XTULRmc569PYyWfoX wU0N8woqkeGOkbrMnucM3jEdBaZE1MEVEGnFHVl+JuTc9+/stiugC6xUMygAnv/VS7kE RF1Doq7M4BT1aVcUZl5k2audJfh6Ky7jauh7hkEZSuPdVNts1qwtzpO6GDGnsju87i+a C/eVDVCxjG9JCSZ/rw7JwtDfRh1NCQQdWNU7NPRICu62XWX8NC4f/dfigudaMwjMz2sZ 2NerAkmbm1mFh1rbelWvhigrTrssVoQyU2gEhBMrFwww/2fFYHyjreVlxepKkOcS9V34 l02w== X-Gm-Message-State: ABy/qLYsSHqTn1562s9QHfyWyKEiprsJ51VHxQR6/aQhzfK+GXRfaaLs ftCST18twF7Z8XP/fTYBAVufd6uvWgw= X-Google-Smtp-Source: APBJJlHJJzo6xKqRfCwu3K4BMa/DSUqUwTqtDXrv/pQ5MfhlCzg1HW7hTPTkOQo0rodXtQODdOcQ37BKfgc= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:fd8f:e174:8bb4:814]) (user=surenb job=sendgmr) by 2002:a81:af1b:0:b0:573:6b9c:6fa1 with SMTP id n27-20020a81af1b000000b005736b9c6fa1mr27409ywh.1.1688704337053; Thu, 06 Jul 2023 21:32:17 -0700 (PDT) Date: Thu, 6 Jul 2023 21:32:11 -0700 In-Reply-To: <20230707043211.3682710-1-surenb@google.com> Mime-Version: 1.0 References: <20230707043211.3682710-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.390.g38632f3daf-goog Message-ID: <20230707043211.3682710-2-surenb@google.com> Subject: [PATCH 2/2] mm: lock newly mapped VMA which can be modified after it becomes visible From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: willy@infradead.org, liam.howlett@oracle.com, david@redhat.com, peterx@redhat.com, vbabka@suse.cz, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, hannes@cmpxchg.org, dave@stgolabs.net, ldufour@linux.ibm.com, hughd@google.com, punit.agrawal@bytedance.com, lstoakes@gmail.com, rientjes@google.com, axelrasmussen@google.com, jannh@google.com, shakeelb@google.com, tatashin@google.com, gthelen@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E4A52A0007 X-Stat-Signature: o7bkqft4d6hunnpay3dgs8tq7za57j5p X-HE-Tag: 1688704337-297630 X-HE-Meta: U2FsdGVkX1/9RZM1YUU8NbidxM4rPXmhd40j7eICnxD6c3Rwj6WaI346lq/+Kq0QohTfgQsmQE679dvBjXGPpyzgeLVKx0gGL8m34Fj67bySv+Bps6mwPHXqIU5EhtxlkQsPRXLGc2ZCj3O6Pm0KCVGzFJonfJbhDRuUac1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WXuMiJJEkrdalkq0pBigIOF6H0rzwwGsNe5drhk01s6Q8HHNFlqr/SY1/raiXwmJihanxuxkJumFeOF0KIJSd6pPpLKitsxHdIjcVtOc1ySCBP0U4KTbosrbKyNjLx2bV80FKFYOmXVFv//g5b9NW83WIkKq9b+8ipJQLdKVTAfVNlA/JPMes7NjEJuns7MUGi5suReRHrhUvIFTH+5fwtHuPsJeOVS4TxKbW3RjhZ+/tOXAmVeLCQKJzkKafGwyoAa0e9YWNtfo1Uvz2Y5QjCzJv2Swf0R65Kip2mJ7iDbI08AeA8Jya8wXfmwdOV99L4hJx+dh8WUe13fxQ/0hD7RKTqL+CuGl2VbzV/GOiA9pg7JRV0w0U1tLnImG5DyBUTouY= 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: mmap_region adds a newly created VMA into VMA tree and might modify it afterwards before dropping the mmap_lock. This poses a problem for page faults handled under per-VMA locks because they don't take the mmap_lock and can stumble on this VMA while it's still being modified. Currently this does not pose a problem since post-addition modifications are done only for file-backed VMAs, which are not handled under per-VMA lock. However, once support for handling file-backed page faults with per-VMA locks is added, this will become a race. Fix this by write-locking the VMA before inserting it into the VMA tree. Other places where a new VMA is added into VMA tree do not modify it after the insertion, so do not need the same locking. Signed-off-by: Suren Baghdasaryan --- mm/mmap.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/mmap.c b/mm/mmap.c index c66e4622a557..84c71431a527 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2812,6 +2812,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr, if (vma->vm_file) i_mmap_lock_write(vma->vm_file->f_mapping); + /* Lock the VMA since it is modified after insertion into VMA tree */ + vma_start_write(vma); vma_iter_store(&vmi, vma); mm->map_count++; if (vma->vm_file) {