From patchwork Mon Aug 12 16:42:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 13760847 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 18A24C52D7C for ; Mon, 12 Aug 2024 16:49:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 932956B0089; Mon, 12 Aug 2024 12:49:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E2356B00A1; Mon, 12 Aug 2024 12:49:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A9FE6B00A2; Mon, 12 Aug 2024 12:49:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5ADE66B0089 for ; Mon, 12 Aug 2024 12:49:45 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CE6DC402FE for ; Mon, 12 Aug 2024 16:49:44 +0000 (UTC) X-FDA: 82444179888.18.27BC9B8 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) by imf02.hostedemail.com (Postfix) with ESMTP id C01DF80023 for ; Mon, 12 Aug 2024 16:49:42 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="MtJ/Z1kj"; spf=pass (imf02.hostedemail.com: domain of jannh@google.com designates 209.85.128.53 as permitted sender) smtp.mailfrom=jannh@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=1723481348; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/Pwh5Yck0NJ63ftZanRvFS8ng+FvBnwXPy4oihzU0QE=; b=122uyVfANKbYnHbsLzEV3CwUJtsvnPP3OHsYPqGpKnYIrP4vD66mz3ihm5qKx786eeBIml ejQ7vKqqGUTZ0Pt23rN29tdmOJDn72yRTwsCpxJFi61D8gu5fzTfLqUC74uBNciL36HyhW vjG6Rey/SI23e06L7W/3jPwUdDIO168= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="MtJ/Z1kj"; spf=pass (imf02.hostedemail.com: domain of jannh@google.com designates 209.85.128.53 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723481348; a=rsa-sha256; cv=none; b=8Hz3U3kJIPzuhvcTHe3h/gntIBcdWQyIz+IKc66jP5hbjyogBhzmKsK8/7xKUCGFras6zn WWtACdRnNWsV9GHkEkjpEXY8GsFSVEiEstqIGXkP2cLC+FOggwkEKzICZmwjZ8SMP9Pn4z r+DIAnq5GZbhnzBBpKD1qhRdZZQLjd4= Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-429d1a9363aso1055e9.1 for ; Mon, 12 Aug 2024 09:49:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723481381; x=1724086181; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=/Pwh5Yck0NJ63ftZanRvFS8ng+FvBnwXPy4oihzU0QE=; b=MtJ/Z1kjsEMGYU0Ny8TGUFRGVvwFk6aIrN5/cRbFGGcRYdF4hlGgK3qV1AvfBRB5pG xM+zL+M/SpuOqT9hjRcc69p3av/+kVVjeRdO08zY8TGG8g5O+LoiWjilatW8twAzakci tqPJrAvYGJLTckBwvZWQ1J8jlIDDKNAlm9XNWX+oKsovRcJK9QCMuJn1mAytwOeSxel5 mA+/WghaLbAe1KydBYinZ8l66XRJcsSzP49JCS5g/Z7L+K/zCOluKEXrCXqXGHfOjK9H FyjS/aTraSfYTXvNn2nW4nu73glZSjUcTE5yjK9x9UENXDql3/qooc5sj4Ee8Bl8dmZh o0JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723481381; x=1724086181; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/Pwh5Yck0NJ63ftZanRvFS8ng+FvBnwXPy4oihzU0QE=; b=FxPTOVL2Zm/0Yd32AXUiPB/YSDDypPYJs6Q7h3PUDTAv1nhKkO8PLSQQihO3sooSQt c9h2wqTUcghiBkdB1Fix16XEJsqXcg90zJjjO2hjKK9BozhiyglAHvDR+XW6dipnBEfm Q9ajL0RciIOPMJr87N77nnfRCmpTYXIGD6St9uQy1UNhjtaC+S1AumOQZkd0JyzibCFl OVdgsFavxZVGQjja3vrWC8iLMWTdMzR+goXnTCqa0mzmTy1luSrTDoMC/7TL0Y3/fs+m YO+2qbzziq7moTZ/OOXNkkRl7DgpkBhIf8STlyhUsPsWHKSn6kdeg62ruTNHQQaV25+5 Zf6A== X-Gm-Message-State: AOJu0YzyOLVOmkxwm/eidEv05pg9sgGKXNUggMXwdDLG7Ay50ZYjDkyt JliwDQ4GeinoyfGpzI2iBPlfm+cfCqH/wfjoC2DcHcqJkuhDMuL07WQU5dZHMg== X-Google-Smtp-Source: AGHT+IHfHCfh/WSuaqttUqyZE+PyNP9LtWGsB/jH2gcAVk2r+RVkhkgxxEdBJPnJp7LHUeTtjOy/kw== X-Received: by 2002:a05:600c:3d0f:b0:424:898b:522b with SMTP id 5b1f17b1804b1-429c827a41dmr2993975e9.1.1723480961958; Mon, 12 Aug 2024 09:42:41 -0700 (PDT) Received: from localhost ([2a00:79e0:9d:4:731e:4844:d154:4cec]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36e4ebd3416sm7942206f8f.100.2024.08.12.09.42.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Aug 2024 09:42:41 -0700 (PDT) From: Jann Horn Date: Mon, 12 Aug 2024 18:42:17 +0200 Subject: [PATCH 2/2] userfaultfd: Don't BUG_ON() if khugepaged yanks our page table MIME-Version: 1.0 Message-Id: <20240812-uffd-thp-flip-fix-v1-2-4fc1db7ccdd0@google.com> References: <20240812-uffd-thp-flip-fix-v1-0-4fc1db7ccdd0@google.com> In-Reply-To: <20240812-uffd-thp-flip-fix-v1-0-4fc1db7ccdd0@google.com> To: Andrew Morton , Pavel Emelyanov , Andrea Arcangeli , Hugh Dickins Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Jann Horn , stable@vger.kernel.org X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1723480955; l=1297; i=jannh@google.com; s=20240730; h=from:subject:message-id; bh=DZesoZHTRWCbLwwNstyM7gJ5jwQEg9bMdX+DVGz6a38=; b=ZH3EnaCLnxnBAMGfLvSGP0Eka8HeLuZeSjPlA42GsqIFj8e3TEDej8ARd5JVzffIeA0SjAHpP K2ENAU38N1DDTzCRll9WxUJ6b3Q4wSjqJacAcWC80k9zPshbwINOyyv X-Developer-Key: i=jannh@google.com; a=ed25519; pk=AljNtGOzXeF6khBXDJVVvwSEkVDGnnZZYqfWhP1V+C8= X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: C01DF80023 X-Stat-Signature: k5n1gijnd4613go13gsbz48m9ifng5ec X-HE-Tag: 1723481382-246625 X-HE-Meta: U2FsdGVkX18KY9biYES63iaxwEMiow+8QrJEBn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1dkCk3Q34cysD/qVKP21IXz61sYSoJxQWCDHu+U9ecHkSzvEuPl9izMTnRiSqTAsXxsiJnJR78R9kgGjniOR1PotnXMVJtVV++Z8xgg1VsC/xzwW3vJeghqa1DLNx+EHy3I2/sJUvkN3g+gNoZy/LDJ/jT49RGTQPhYDqT7UrKE4ViY90wF7rR0SiucocxXhaAfmK5kRvC+KlWND5eq82MqDlmq0+R6X+0aw/BYoPUqmZSQeipYg6wDAlRAVCD9f/2NJCdenp1ir6QmisUyyWMzI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Since khugepaged was changed to allow retracting page tables in file mappings without holding the mmap lock, these BUG_ON()s are wrong - get rid of them. We could also remove the preceding "if (unlikely(...))" block, but then we could reach pte_offset_map_lock() with transhuge pages not just for file mappings but also for anonymous mappings - which would probably be fine but I think is not necessarily expected. Cc: stable@vger.kernel.org Fixes: 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock") Signed-off-by: Jann Horn Reviewed-by: Qi Zheng Acked-by: David Hildenbrand --- mm/userfaultfd.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index ec3750467aa5..0dfa97db6feb 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -806,9 +806,10 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx, err = -EFAULT; break; } - - BUG_ON(pmd_none(*dst_pmd)); - BUG_ON(pmd_trans_huge(*dst_pmd)); + /* + * For shmem mappings, khugepaged is allowed to remove page + * tables under us; pte_offset_map_lock() will deal with that. + */ err = mfill_atomic_pte(dst_pmd, dst_vma, dst_addr, src_addr, flags, &folio);