From patchwork Thu Feb 16 05:17:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13142516 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 D8B33C636D7 for ; Thu, 16 Feb 2023 05:18:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75FA66B00A0; Thu, 16 Feb 2023 00:18:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E96E6B00A1; Thu, 16 Feb 2023 00:18:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 513CE6B00A2; Thu, 16 Feb 2023 00:18:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 37DA26B00A0 for ; Thu, 16 Feb 2023 00:18:53 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1604F404B9 for ; Thu, 16 Feb 2023 05:18:53 +0000 (UTC) X-FDA: 80472000546.03.1C60063 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf25.hostedemail.com (Postfix) with ESMTP id 50109A000D for ; Thu, 16 Feb 2023 05:18:51 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=lxQtFymG; spf=pass (imf25.hostedemail.com: domain of 3urztYwYKCBE9B8v4sx55x2v.t532z4BE-331Crt1.58x@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3urztYwYKCBE9B8v4sx55x2v.t532z4BE-331Crt1.58x@flex--surenb.bounces.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=1676524731; 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=GTmH+3zLa4DS1pm6XKzoFa9XiN/n/492GQmNm0F2u98=; b=3O2pzh8d6PcULleJVZLHBtKCjRktROp7qR50XZpPUS7p7z5IvxAio1YJJ5rheGVSMizVpJ ilrAGfKL+SSAXnVX3ZckwurCv0P2YHy6NO6JXI/iYQPwlwHFNXODGOyvf/rJ9iJ/1+V593 NcWcS/CQNkEnxeefnztZeRRUWapXQB4= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=lxQtFymG; spf=pass (imf25.hostedemail.com: domain of 3urztYwYKCBE9B8v4sx55x2v.t532z4BE-331Crt1.58x@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3urztYwYKCBE9B8v4sx55x2v.t532z4BE-331Crt1.58x@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676524731; a=rsa-sha256; cv=none; b=2x1UwWF5gR0TRhLgDJ3tqKzrx8Xoqqi/zj+pOUd4ZdD6KFJutHpXzHXvQzYfR/vn6inN4T BZlq4FUUIO1KrjC00qVS3d0aLba0904sL3sDD2bMMtHRNAGZkuC8uoaCnMO/EFHNikl53A 1bZ5ke0IFp7f6bYp0lRwwESDktxWVlI= Received: by mail-yb1-f201.google.com with SMTP id 191-20020a2504c8000000b008e2fd9e34e6so854899ybe.9 for ; Wed, 15 Feb 2023 21:18:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GTmH+3zLa4DS1pm6XKzoFa9XiN/n/492GQmNm0F2u98=; b=lxQtFymGNoCMn73Q032qmSTTcj+ve7GubiXjG/S2/wZlI5A+wvms4mpLfeb6oKn81f gVVSU10rEKBXvaTQ4DI3+isYBuVvKtNDNUM4jT8VbxX9prWBulP37D7mcvfTTB0yv5Vd xLNBfAphNgvAxOXoC4bxmXgktQA4hGiUCCaPhABiIUzurEkqvA5yibf0xhi6vP6KsH63 2fMMNvtfnEstPxLz+UPzwP+9hfF+/M/equZBkR4oGxxGpJkcmKAi9IzofVMskpdF8tY7 tvoaCpVKwaaM8HBNFnnIuJry7ZsbTgV8SMkds4HojfN0Cbg+ccWTepT6dYOQ/gKplcjn qubQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=GTmH+3zLa4DS1pm6XKzoFa9XiN/n/492GQmNm0F2u98=; b=rBR3v4eaY2uMI4YeeOWQ1h1F580iKaYq61R0sXXnlrBcTG9y7FK/lRbMRTo0FLzDUm cKnMfvsrju4R32SKG/2w0eHeAQ2pebINCnSaBBeqiXfxL95tGxdlk9lCUU7THj9p/lnL o1GQOIbrr9ArPxksmxyDrvgrMgAJ+wOy32ymSBN/5YHpKVuPaUqgNHH4F+L75U0FDLb6 1jFKdnU/yYO9/WDOgZry8nwuo8ZAfxteDza0GBh2e9zgkT5ap/23BykIBhP+DQqhf5w2 6wCUhKe6GeudeMu9fKHRgunLOPZuB3MsP6uFcQjjiHNwZhAqeLVIBitPsn4MXnxJseml QbsQ== X-Gm-Message-State: AO0yUKVycLyo3flwrfU6Xu6UHXnMOvNXOuSbB3vOJYiAczoOkAlPdaSj 0WuXQwxr5kbHGuz7iOLGqm4C6+QHTzI= X-Google-Smtp-Source: AK7set/Pm4WgUpt5iRaimc/pvYKHTFOy9juHCFu4sSQHLZZAabCINcoiFbttYYVnL3GbT+1smZzLxo4F4W0= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:f781:d5ed:1806:6ebb]) (user=surenb job=sendgmr) by 2002:a81:441c:0:b0:52e:c79a:cda with SMTP id r28-20020a81441c000000b0052ec79a0cdamr12ywa.10.1676524730090; Wed, 15 Feb 2023 21:18:50 -0800 (PST) Date: Wed, 15 Feb 2023 21:17:38 -0800 In-Reply-To: <20230216051750.3125598-1-surenb@google.com> Mime-Version: 1.0 References: <20230216051750.3125598-1-surenb@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230216051750.3125598-24-surenb@google.com> Subject: [PATCH v3 23/35] mm/mmap: prevent pagefault handler from racing with mmu_notifier registration From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: 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, paulmck@kernel.org, mingo@redhat.com, will@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, chriscli@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, rppt@kernel.org, jannh@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, leewalsh@google.com, posk@google.com, michalechner92@googlemail.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, Suren Baghdasaryan X-Stat-Signature: kst3dczn59g6kj4s65kxb5xfpyu56acu X-Rspam-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e CSpqM6z4OKIbKIwHd9Lh82HUbKnH6OpwDwfcqA/6hNXO/Xa2E9IWYbVFrazgwlT0suxrFToBYJD2Q/PEfQen/uGx69cgl+vwjHdhZl+KStNGZwAjd0bYQCSGN+FWEsCS1Yq4ePkNSLD24Bz2aEDoTSkW+/g739V+8G/cTXy56hTd9RbSQRwf84iAvKIge+MAB7dCgox/afD+O+jELP1Nnrbylg85VytCZ95ARfVChDbPIxOce/tYdsYex3htAGvNFLyxcE2mUszTABrmhqe5nhCwQgvx+H6aWdHMqN9tPh5gUWKlzpVP9sZA4llaCKXetUNjANMiVe1y4MKf2glHPbyPU8WFbDNAPdl1JE15qTEd+P76gJbMGLRlXngHOw8jsRB3J7U2uUc0dPNcuhSScUP1Kzr55eYIGoUbES1PNPRxYvNAbySgA4xuwa51SwP7KejjRqnSxFY/pn7iGrtcgzolH75IHHqVewXn+VqyLrbi3LA4RSZ6f6AZx/7CCkn0WYzKENupVhfcBFSLUiNx3W6LMpfRWmc2WJyrZZA594+QRww06dpjmR2QKDDuwVjiZo7pdFPvgUheA1y4= 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: Page fault handlers might need to fire MMU notifications while a new notifier is being registered. Modify mm_take_all_locks to write-lock all VMAs and prevent this race with page fault handlers that would hold VMA locks. VMAs are locked before i_mmap_rwsem and anon_vma to keep the same locking order as in page fault handlers. Signed-off-by: Suren Baghdasaryan --- mm/mmap.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mm/mmap.c b/mm/mmap.c index 00f8c5798936..801608726be8 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -3501,6 +3501,7 @@ static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) * of mm/rmap.c: * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for * hugetlb mapping); + * - all vmas marked locked * - all i_mmap_rwsem locks; * - all anon_vma->rwseml * @@ -3523,6 +3524,13 @@ int mm_take_all_locks(struct mm_struct *mm) mutex_lock(&mm_all_locks_mutex); + mas_for_each(&mas, vma, ULONG_MAX) { + if (signal_pending(current)) + goto out_unlock; + vma_start_write(vma); + } + + mas_set(&mas, 0); mas_for_each(&mas, vma, ULONG_MAX) { if (signal_pending(current)) goto out_unlock; @@ -3612,6 +3620,7 @@ void mm_drop_all_locks(struct mm_struct *mm) if (vma->vm_file && vma->vm_file->f_mapping) vm_unlock_mapping(vma->vm_file->f_mapping); } + vma_end_write_all(mm); mutex_unlock(&mm_all_locks_mutex); }