From patchwork Thu Feb 16 05:17:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13142518 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 CAB4DC636CC for ; Thu, 16 Feb 2023 05:18:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C23E6B00A1; Thu, 16 Feb 2023 00:18:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 64C106B00A3; Thu, 16 Feb 2023 00:18:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C5516B00A4; Thu, 16 Feb 2023 00:18:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 327926B00A1 for ; Thu, 16 Feb 2023 00:18:58 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C0ED3120B37 for ; Thu, 16 Feb 2023 05:18:57 +0000 (UTC) X-FDA: 80472000714.01.64D76CE Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf30.hostedemail.com (Postfix) with ESMTP id E52628001B for ; Thu, 16 Feb 2023 05:18:55 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=hu+sMS9o; spf=pass (imf30.hostedemail.com: domain of 3v7ztYwYKCBYEGD09x2AA270.yA8749GJ-886Hwy6.AD2@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3v7ztYwYKCBYEGD09x2AA270.yA8749GJ-886Hwy6.AD2@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676524735; a=rsa-sha256; cv=none; b=RdM5WaT1R6pe1Q2Qqw8f82yGk2hn/3ZQR3h3fCMwt/8+0S3qHn2wHpKm7yjFypSiFdnnWQ mkcTQC9HitbFD/sqWdXXNNOATEz5fFiKL5lxPQMArvsoyYr+7xg7W56QgSf/npdyqTKvkM oFZCJ/0U4fgbplyLTl56xsjmZMmjUS8= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=hu+sMS9o; spf=pass (imf30.hostedemail.com: domain of 3v7ztYwYKCBYEGD09x2AA270.yA8749GJ-886Hwy6.AD2@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3v7ztYwYKCBYEGD09x2AA270.yA8749GJ-886Hwy6.AD2@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=1676524735; 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=rll6a/VW4pEw1AhNBjbFoMRbi+XwYRGQoRwSBUD0bh4=; b=M1AR7A96fobkg0ywsqV6UemGJSjvsDq79sQtcrQ/Hrefmkqw3MmAvNqE0XhPISLjb+WuDk O9iWCBUJ5jx2nWYnD6ZZLAy5q5lT2eClpDEKEV2MnWMlSklqL8OE+8IXd98spOvFA02W8X BQnmZgwvJ16k+YTv70WqH4YhDtlVmIM= Received: by mail-yb1-f202.google.com with SMTP id n20-20020a25da14000000b008fa1d22bd55so840616ybf.21 for ; Wed, 15 Feb 2023 21:18:55 -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=rll6a/VW4pEw1AhNBjbFoMRbi+XwYRGQoRwSBUD0bh4=; b=hu+sMS9oSFHR1r7khxhf1UbXFcs2Z++5UsWdyoPEf98HLaKpCA/5NqLhTjKtFbNcAI bq4IC94z3LeKr+N8fZ27HaBeoB9eV7FJUK2PbLxLqtWEgkZI6C3sySZgRKxnS+2i/7yz RFGxaAOIl8opKirVI8bfAZAj2T7sVtyi7tZlmgl6KCk8dFxbKL8va87izUWyHNnCWm+i wFd5WvNjbROGmsVfKcufeNfiLR4xBKkikebAtyhw//UrLzCCkecCcI/c1WZAFUB1nZ5g ONtU8uW/bb5j1wyQf5MWCYTwREIrfwMyez4h+y24cgMUk3OrfInLJF7DZne80Aw4Hh2T oQOw== 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=rll6a/VW4pEw1AhNBjbFoMRbi+XwYRGQoRwSBUD0bh4=; b=qyqK37WHagZn82q2GYDwTG7PvL9uW25bXwGvvp8LX+Q98mXO6d8tFZ6a7/30srG5T+ 9ksQRWFITde77a6SoPBc+WsY7bmTQpu7X67MlQx2Tfc3TsrIxcUqZBZzRc4Svuvg/HqS 2vr7VE346wg5NJiNsH315EgCkA1/46cgG4iD1aT++5JyPokx4vLTYBrZDNSlOSFrVO5p UkrWL0lHUzpTi66PqTmDmuxzrIhjd7WmOq6vjEFhEL1qvvIJAHOjPF/E1mwhG4We+cAv +9Z3jQyOsacqKsFYz/UIlkBKvSwu5z9hGofLrudUBZPZH7qbZCg0BR62ADat0BzVxAqV BwqA== X-Gm-Message-State: AO0yUKVI4EbVK8WSPanhuL8NCRPRaaT4koiObXlFzH4TqLBo/PFQb+sb msb0P8yFjfQSUCwPW2nYnc+iBpq0E5c= X-Google-Smtp-Source: AK7set/t52sA7P5kGXoASmYABiID5pIEjMuKzn7Z6yq6NOdnfI/iNhk3E99tS5Gm4PH95uk7/Rdn/fKqkhY= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:f781:d5ed:1806:6ebb]) (user=surenb job=sendgmr) by 2002:a25:9012:0:b0:874:380b:887e with SMTP id s18-20020a259012000000b00874380b887emr545965ybl.239.1676524735071; Wed, 15 Feb 2023 21:18:55 -0800 (PST) Date: Wed, 15 Feb 2023 21:17:40 -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-26-surenb@google.com> Subject: [PATCH v3 25/35] mm: introduce lock_vma_under_rcu to be used from arch-specific code 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-Rspam-User: X-Rspamd-Queue-Id: E52628001B X-Rspam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NG59xRFWbWlTvN+JX8eED2P8PenNbKjyeffXY2POQmWNLaplx1N6HUf2/t1SLsmF5nzjxcy7M+MIupcJup/bD3M0dfN1YarWnVKlgeT1x7BNsiw7U5GWZSon82y8GlmmkA/RIfA2dy6dtctqu/9NBgH0U1pCbonCxl+p3cnYW/uW49PUYlfVzbAOjy3A/wFnaxtZFLQy5xA3phMTmQQ4LMDbP09zUyJsE7HbmfENubCWAFFY/jy2pllfOijwIZFFu+ErCFK+v0cyUohWYpBqKuoTxT2fKE2TtybrISo5zPVWswShv54kcuNTf4P2NK48GK2oH44obkPMhbSuPBeENoNggoj3lq24otZhF7Dj78nE20b6NtS6CNAlOt2/eIkx7QErY5WWJvXsfwPBNH5vaoQ2mAL+psnjJLqPqA4+rySjZgJ+Uz8wSMVcVQHLYygCl537VJw0TIa0McsqUHRV6TA4T0h2EvtvEIModo87tIwrHq4MnTMKs/QAjLvzkIGqNwkg8vrcu1cxPxuijWmJnJ0vQaGtmXvbdXS1kXUCEyLqFLAS20pe9AiQbkw= 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: Introduce lock_vma_under_rcu function to lookup and lock a VMA during page fault handling. When VMA is not found, can't be locked or changes after being locked, the function returns NULL. The lookup is performed under RCU protection to prevent the found VMA from being destroyed before the VMA lock is acquired. VMA lock statistics are updated according to the results. For now only anonymous VMAs can be searched this way. In other cases the function returns NULL. Signed-off-by: Suren Baghdasaryan --- include/linux/mm.h | 3 +++ mm/memory.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 3f98344f829c..36172bb38e6b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -701,6 +701,9 @@ static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached) vma->detached = detached; } +struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, + unsigned long address); + #else /* CONFIG_PER_VMA_LOCK */ static inline void vma_init_lock(struct vm_area_struct *vma) {} diff --git a/mm/memory.c b/mm/memory.c index 8177c59ffd2d..5e1c124552a1 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5220,6 +5220,52 @@ vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, } EXPORT_SYMBOL_GPL(handle_mm_fault); +#ifdef CONFIG_PER_VMA_LOCK +/* + * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be + * stable and not isolated. If the VMA is not found or is being modified the + * function returns NULL. + */ +struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, + unsigned long address) +{ + MA_STATE(mas, &mm->mm_mt, address, address); + struct vm_area_struct *vma; + + rcu_read_lock(); +retry: + vma = mas_walk(&mas); + if (!vma) + goto inval; + + /* Only anonymous vmas are supported for now */ + if (!vma_is_anonymous(vma)) + goto inval; + + if (!vma_start_read(vma)) + goto inval; + + /* Check since vm_start/vm_end might change before we lock the VMA */ + if (unlikely(address < vma->vm_start || address >= vma->vm_end)) { + vma_end_read(vma); + goto inval; + } + + /* Check if the VMA got isolated after we found it */ + if (vma->detached) { + vma_end_read(vma); + /* The area was replaced with another one */ + goto retry; + } + + rcu_read_unlock(); + return vma; +inval: + rcu_read_unlock(); + return NULL; +} +#endif /* CONFIG_PER_VMA_LOCK */ + #ifndef __PAGETABLE_P4D_FOLDED /* * Allocate p4d page table.