From patchwork Thu Feb 22 08:08:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13566841 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 3BB27C48BF8 for ; Thu, 22 Feb 2024 08:08:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACA5A6B0072; Thu, 22 Feb 2024 03:08:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A53C56B0074; Thu, 22 Feb 2024 03:08:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8CCE06B0075; Thu, 22 Feb 2024 03:08:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7787A6B0072 for ; Thu, 22 Feb 2024 03:08:50 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D93B3120CC2 for ; Thu, 22 Feb 2024 08:08:49 +0000 (UTC) X-FDA: 81818713578.14.7CC304C Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.42]) by imf06.hostedemail.com (Postfix) with ESMTP id 9041818001E for ; Thu, 22 Feb 2024 08:08:47 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="bdm1/Tuh"; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf06.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.210.42 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708589328; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=Jiioa07cgkgC3rBe2WxD4CHl26Y2JVgv/alcGcefyf8=; b=xt6BpKs0ckU4asvRYGPr35a2kfNYJELPy5l/o6a1lBltAu6ABKtK4Cl1ANKCpdwLlK7peK X8iKixFPvZt+V6BxxNWI3GZSTB/yg545XTr+/7AWORGNZRZE7VfI8007Gt1tLON0wB2e4U aSh3K/60ww3lO0/HUxTyEnKFqgocY0c= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="bdm1/Tuh"; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf06.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.210.42 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708589328; a=rsa-sha256; cv=none; b=RM8NELKq/R/0j06uHI73/GBR9Lf+2fJrFMGj5oPsuvPLC0LGelfT4Gja9t9lcsJAdN3Dai +WqINolFlvxm4b4qE+pISRFxpX6NUof95rCkWVanAeH3ENFoxTfsjuM9EcOhx/2Fm0iEEa HTPKz1XihGcXaT9vM19J5ZhslStA90g= Received: by mail-ot1-f42.google.com with SMTP id 46e09a7af769-6e45439925aso832291a34.1 for ; Thu, 22 Feb 2024 00:08:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1708589326; x=1709194126; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Jiioa07cgkgC3rBe2WxD4CHl26Y2JVgv/alcGcefyf8=; b=bdm1/Tuhwhra3Zs9Uq9T5D3yMbxdVa2fLT76Gawl6jlNhieLpWcLzCBnUl0ln9GPQp RoXvMH95sVQUm1066nxdUSjGREnRvtUXd+aQ39eVFUkkl6hqAb/baVGB8/Di/GZmkOKw JyILnfdtlj6j/GmVQInm+Va2HOA8j3TwO+EbsYwQp4GyqRh0JQMXnuNV8KIZZiiLUcm7 koVhYcnkyaQWAMVkBeeqAIOTcaX22LritHqV9k1/VydU1/9BwSQISt77GlA/hVWyaK+p qJOnfMmsPoESlANe0x92JmLKX0ZywLgaWa9M09BuK5rFMSXpjsLPtCgygeZzg5CJN73x 25CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708589326; x=1709194126; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Jiioa07cgkgC3rBe2WxD4CHl26Y2JVgv/alcGcefyf8=; b=KkWJZbJ/ZfI2YZKbxYJo3gRE0PSii15qGHFml6gVjH6sWXC2FrEwTTqzGL8b25bGXV JlH37Ohfb1FzY6vL9BX/zHHy6ovAHm3T8MuX+Hc9kvFQDad9bQ4akxGXCN4sCsYpHsQI 5G+uFj/d3rYNxkcMa9b6rE7uYcUjmv7w45BHijGjW/ueE2OxfYtKL7TwrRmEnHUOShtM xMCC5CN3Qa0dyjF0KO82onxv2X9FSt/NpEPJqI3gt5dsVWvFAoKOR74m4g3UDBxvjPNb l8rvJA06UwIqXAMkPgv1vvKb22bzCwvORFg8lmk5wV5tTMWhIdrP2FB0jFvOjVpA2DhR uXYA== X-Gm-Message-State: AOJu0Yw9GUcjn+Skn2yehVwJxwWvL6Wb9ccyXbi/ch4iBg9L4kNUmHZC tpU3dRNL0bfsOD2IKwxL4hLh0sPsOAJfZR0rRMfjETxN7qnSKrb/FmkdseyL1sY= X-Google-Smtp-Source: AGHT+IH+9nNrSC0y7Upc3eli/kDFSR81+YsEAYLGSXBh7rYjW/RCRQTT4fM5+3Diy/npRha8UB/oZA== X-Received: by 2002:a05:6358:e49a:b0:178:9f1d:65e3 with SMTP id by26-20020a056358e49a00b001789f1d65e3mr19766252rwb.0.1708589326343; Thu, 22 Feb 2024 00:08:46 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.155]) by smtp.gmail.com with ESMTPSA id a3-20020a056a000c8300b006e4cf04e501sm659058pfv.13.2024.02.22.00.08.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 00:08:45 -0800 (PST) From: Qi Zheng To: akpm@linux-foundation.org, aarcange@redhat.com, surenb@google.com, david@redhat.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qi Zheng Subject: [PATCH] mm: userfaultfd: fix unexpected change to src_folio when UFFDIO_MOVE fails Date: Thu, 22 Feb 2024 16:08:15 +0800 Message-Id: <20240222080815.46291-1-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) MIME-Version: 1.0 X-Rspamd-Queue-Id: 9041818001E X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: pgzcj4tu365w4cp4qgiiaz9yq7hpxxhy X-HE-Tag: 1708589327-639639 X-HE-Meta: U2FsdGVkX19ScC2E7PW5+AI7EN273syZJ+ZgkkfxD/4TMPyLU7/wpXUbjnn5QNOjgyw4EIy6hu8AIa51yUQwfdrN8xyq159UHzgG33ebAcwxmrsiNGaTRI70k8N5JHrVuYYwKfkfvvcvAbtQoSfODgMvZK9+917qHk8SQxWexUYGS0zG7ri3JmiKpZU0QTSd9pCg1Re/pNDqGt7Jn9UI00thGztJmXobWAuLCQly67YWz0AntG/ZRSpjlRByi9milCV7W6RqO5jU5QitF+AfGSteK8aA78UdGZ+d920GFWQ7dkcuR6xdOUPdJfDERsklKYAZFeZYKEjR9IDe+WZ6bygwgxSMJ9orkpFdA611gZInXgvsqXg2vagAAF5InOSqMBNrfN4VxRBkr9NbmSgAbNoRQzV2792LUw1W2yP9cF5CSEN4Or8mZ/tajrRyQHgDHknoekjrbTgOGJs0wkU/zVxN9fplC5pVxTWFX9ANQh0cT6MIz+2nn3hqPEYeZxcmFqrlKZbZ0jYaKKYEJlNZg+UkVicEBkmP0hWXaoMN0lYPcwq/V7UkRxrxwPvlTTuyvyHJwN4JJATAv4rWVothVB5EPJjwWRHPqvgE3M8qnAWYMFRjSAHK5AvyDaMC+A6eLk8I+c/jpLGsOcv0RMfVJiOPJyUL5dJdAx181gTCrbOHu4Xpdgh0akgihI91WiOk2lY7iLOqrO1nKK/vH6vhaW5ZJxD//KMBdjTkQCEfsfFvimjRMsyf1Kkro1iLmJmhHhlVhDSsGJLX+ueERbvPRi4XMUsowvsh4vTlcKM8dgchYAX1d7oOjvKzDG6eNaoVCShuFzZuWywWTfu9pnalE99cMpWVjkYWEf/+Xc6vM3bKcaZ4VExEzVScMjGwICDd2pbOOSdleQYstrdEGrjTSs4iMT8HNLDQAc65gcYDIyd+wwkHN49sYVZ64gSg+nx2q3h5ovbk6Zp5bITAYWY /uVpuu35 VmwU9TAit/PDSDQduMzoY6A0ZUhPGxuH2pcvFCybPqepekdBvXSmya42CMzT86iWKiKQvaXdHgFH7JmS37myOPA55rqLMis3NiDpt5D53D1Ha1Xfl6icOQJ+vH7RNKP77vOb3xZ3nevdyQcj53V+Hwu4e14uIk4clf8GVUSUGtYmn0PI= 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: List-Subscribe: List-Unsubscribe: After ptep_clear_flush(), if we find that src_folio is pinned we will fail UFFDIO_MOVE and put src_folio back to src_pte entry, but the change to src_folio->{mapping,index} is not restored in this process. This is not what we expected, so fix it. Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI") Signed-off-by: Qi Zheng Reviewed-by: David Hildenbrand Reviewed-by: Suren Baghdasaryan --- mm/userfaultfd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 4744d6a96f96..503ea77c81aa 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -1008,9 +1008,6 @@ static int move_present_pte(struct mm_struct *mm, goto out; } - folio_move_anon_rmap(src_folio, dst_vma); - WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr)); - orig_src_pte = ptep_clear_flush(src_vma, src_addr, src_pte); /* Folio got pinned from under us. Put it back and fail the move. */ if (folio_maybe_dma_pinned(src_folio)) { @@ -1019,6 +1016,9 @@ static int move_present_pte(struct mm_struct *mm, goto out; } + folio_move_anon_rmap(src_folio, dst_vma); + WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr)); + orig_dst_pte = mk_pte(&src_folio->page, dst_vma->vm_page_prot); /* Follow mremap() behavior and treat the entry dirty after the move */ orig_dst_pte = pte_mkwrite(pte_mkdirty(orig_dst_pte), dst_vma);