Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp5483268rwb; Tue, 17 Jan 2023 14:34:30 -0800 (PST) X-Google-Smtp-Source: AMrXdXvUvTPlN+8+v9BQngqQnjldDJ5rLMJahI0MKIKyB1Vg+0cQ4qkGaiePFBNsZxzGT6IFR3L/ X-Received: by 2002:a17:902:e749:b0:188:6300:596a with SMTP id p9-20020a170902e74900b001886300596amr6476030plf.7.1673994870718; Tue, 17 Jan 2023 14:34:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673994870; cv=none; d=google.com; s=arc-20160816; b=HFxFbt6TlhqUH9qutJwv4OoUuXnFcBF68frikiToSZQ4R+ZoFZB94rrclkmw3lf632 tPejmB1hHu2waHTZ6mevx6m0a8W22bRVCIQn+eN9+6K7wx9Raj92QwElSFNTzjdpr223 TDfJqmi1uBl4CsTQBSvx2favheAiSvF9YX1b6akccA2BI/dVZVHgLezzTka/9smSNtJS W4zbqK5tE9JA/gxX/8+ilgya32vxeLDhdrp2lDgDxQqN0K1vNVMn+T/2/jAAihMnSOuH yDTmlS2UhJ6ZMLd6I0H73nZN6r0bWvIjGhXXQPByM9rqKLNvSCQDfC/qKnziqRexeEv+ 7k4A== 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=OSFcPHbwPU7VuQqTvb7CRygx4qEQJSCX4opQ3eOpE/E=; b=Mm6RnrdK2muPAjswfYPhWsnSmzRu+fPcinaE65fHl1ZVwl6d3MXt4IVjQft7M6iuh0 QRgTsEt7v0WGFQjXJRwQLUNkJmcDyrKX2216t6D4kZ36U7+ggVAAkAfcL2DhasUvCmxY 6rww0Ihp8mx0kQ7fuVRpSMS9KcK3DHbWih1EaW6D25DA8LB16mJNA/KArxWoLBpQSNFW pADfzNM35Z/NL+8c/UN8iuFnDcYsEYOSvtqUtZIXt7zED/6elroTBvNqFNVtq4qjpfM8 CYxeWVJ5XwugH+vs0Vwz3KXRooR581iFLSPelmNctIdCQbKXne/DaVpAvoDFbW1kKfjZ 8eKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oK5gCq5x; 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 m132-20020a633f8a000000b00476e640ddfesi34615290pga.80.2023.01.17.14.34.25; Tue, 17 Jan 2023 14:34: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=oK5gCq5x; 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 S229816AbjAQV3s (ORCPT + 46 others); Tue, 17 Jan 2023 16:29:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229622AbjAQV3M (ORCPT ); Tue, 17 Jan 2023 16:29:12 -0500 Received: from mail-il1-x133.google.com (mail-il1-x133.google.com [IPv6:2607:f8b0:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C69C1A4A2 for ; Tue, 17 Jan 2023 11:51:38 -0800 (PST) Received: by mail-il1-x133.google.com with SMTP id v6so6406974ilq.3 for ; Tue, 17 Jan 2023 11:51:38 -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=OSFcPHbwPU7VuQqTvb7CRygx4qEQJSCX4opQ3eOpE/E=; b=oK5gCq5xbP6niBRqF/QRL7Bq2gVthVqJQbspwhXXQ7OnRSHWI5AiE5QK1LTqtcMGRn HeyRbfCQAKtC4T3gzbb8Wzg7491gbqjvpVBGSplHQdv+8mbaq/6L7WWL28izPBjIqoix RcZwdqu/IkMGD9/tcQk+epsczoRPYFq75Ly73PSeVqekeXO1isqgk/+r+nTbDSVMXuGR 7D+jZPUfzbSoBt77De0gD6WjMDA5N64/6EgjY16694eHJ1oNyDGZlC2jU7fD9KrrnhM9 KHzaaaT+da49MDKCEZDrLz7ulbS8/gmsAhEEGJNDyRrKINMsAmuavctzqCV6f7UGaTFS kFGg== 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=OSFcPHbwPU7VuQqTvb7CRygx4qEQJSCX4opQ3eOpE/E=; b=3Q05DoWYZc6YPf+LNAkb15wCQN3kMm6prZ+iAST4W9iyM3bBMEHx0XcCDzkiO395qu WUriw9mGoCLkuRmHYJTqxChlKnQGn9RZ/zClAr9ct/rWaSE6eOKTS2haMaM9CY/vzFL2 2IHOqgE1WMMVByff+iXECljiDCq137oi1JCabGpvVX6QvD+en90c9wamalpFBoUL5ct3 1AIC7+Nq9Tc4mSwPLUUaDavbnYR3V69g804e+rcgJVG9N+3U5gyk1/zDQhy4DW/VRlLA NisHZi0BCMe1b3QT1tFxYjVoVkZefKaeuOMe3IADH4eoc6K3kpsfZPSo5ljBF0SanIz4 2y0Q== X-Gm-Message-State: AFqh2kqYeU4qQSkOQN9ktBue6amnPXmfZxt/83RZLPUgEOXplYae7vyZ UrQipoRovMDAxdKr8wel5SkFKo9/TBCjBBOJNGcbuA== X-Received: by 2002:a92:c5c6:0:b0:30c:877:db26 with SMTP id s6-20020a92c5c6000000b0030c0877db26mr453316ilt.101.1673985097438; Tue, 17 Jan 2023 11:51:37 -0800 (PST) MIME-Version: 1.0 References: <20230109205336.3665937-1-surenb@google.com> <20230109205336.3665937-33-surenb@google.com> In-Reply-To: <20230109205336.3665937-33-surenb@google.com> From: Jann Horn Date: Tue, 17 Jan 2023 20:51:01 +0100 Message-ID: Subject: Re: [PATCH 32/41] mm: prevent userfaults to be handled under per-vma lock To: Suren Baghdasaryan 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 Mon, Jan 9, 2023 at 9:55 PM Suren Baghdasaryan wrote: > Due to the possibility of handle_userfault dropping mmap_lock, avoid fault > handling under VMA lock and retry holding mmap_lock. This can be handled > more gracefully in the future. > > Signed-off-by: Suren Baghdasaryan > Suggested-by: Peter Xu > --- > mm/memory.c | 7 +++++++ > 1 file changed, 7 insertions(+) > > diff --git a/mm/memory.c b/mm/memory.c > index 20806bc8b4eb..12508f4d845a 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -5273,6 +5273,13 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, > if (!vma->anon_vma) > goto inval; > > + /* > + * Due to the possibility of userfault handler dropping mmap_lock, avoid > + * it for now and fall back to page fault handling under mmap_lock. > + */ > + if (userfaultfd_armed(vma)) > + goto inval; This looks racy wrt concurrent userfaultfd_register(). I think you'll want to do the userfaultfd_armed(vma) check _after_ locking the VMA, and ensure that the userfaultfd code write-locks the VMA before changing the __VM_UFFD_FLAGS in vma->vm_flags. > if (!vma_read_trylock(vma)) > goto inval; > > -- > 2.39.0 >