Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp609173rdb; Thu, 22 Feb 2024 13:41:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXhD+cz7xYh+YzfDYrrSQY9759yBR6XqDL1wkHVllZX//tBYPW+GoU0SDsQLQMwrPvNAqIuNlmuN/Cv4k/d0CKPKxnAONsJZeY/VuVIRg== X-Google-Smtp-Source: AGHT+IH3mXKBxeGKOYVhI9CgtOb5ngW0chj6yf1aJSVzfYAVmg11qe8xtAOJf7tILhQRMM1GKfCe X-Received: by 2002:ae9:e50d:0:b0:787:4161:9344 with SMTP id w13-20020ae9e50d000000b0078741619344mr298433qkf.5.1708638105371; Thu, 22 Feb 2024 13:41:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708638105; cv=pass; d=google.com; s=arc-20160816; b=aTIDnyn1n6hDDoo+c2wLpQca29wBKBlX3CpEvewTmWkRhXwkPL6Sr7heOYJVgaUK5e NcriU3vxPvXfLDvxFI/GuP78ljlVYJMsb/VDYfaM4HU8bgHh9sU7euijJjZ0nJNSoDsB mZgTisqq0yueC2ggLI96kAhn/a34Ba5DWAOZXj8mXBmDBJ6+jRzk01D40PQE9a8sRbml TbDea4v9CGgonIqpMTGRLLC6T3prC8fVt5Vc3i6kHECOGeR9y5JzFc8q2bgvejBoe6nO N3iGqRh4uhsK0tA0IJRYFHCtay7DhEOx7DF+y6JnZqdboDiiztjNyX8QUBofy5q5TREG s8Zg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=QmgCzE9LjwDzmPCc5NVDUCPpvcBzTrPnv764C/CP8R4=; fh=5eqy8pKT36lIy+h5W/6fiMaNFcdTKZCSaBq8WK7OTyE=; b=0x+QTkFZuW4Onu06I2gYkpQamQDyzR1TIMKfaS5YUOOFC1am8/SSFc5YMhZtkdRtGl RT+mvptgI1e7kW04pZba7ETV/68qfLpBcqNZr2l/B/NVxgzVmXwet2/3ef2MpS5RKK/1 mT6DRUwz+ISTU3e79SOOv2vyhte/E0wrs3AzUftYvg/TnxznJrWf9HnxK/Z7FuuyLKPP xW2ax/ezGG/ED3ieaXi+yPhYRhlp2dto26kOMratcAhU2Nmuw0unECF83FFbZ90UQxDJ jh51fn19a0elF0ToOB2YWRILzMHobltuDhcwaDKzC7PAXFbMjNkVpk74C9pkHyA1Vleg bzcA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ltjpLBje; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-77419-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77419-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d11-20020a05620a158b00b00787263b0500si13093563qkk.279.2024.02.22.13.41.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 13:41:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77419-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ltjpLBje; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-77419-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77419-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D514D1C24BB7 for ; Thu, 22 Feb 2024 21:41:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C2C4412D1E0; Thu, 22 Feb 2024 21:41:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ltjpLBje" Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7CB2473F1C for ; Thu, 22 Feb 2024 21:41:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708638098; cv=none; b=Uli2DPzTv5aFdiD+pWU5MXVJolULzPJlGe2P5fNrBQEl2Q+4IjkooAqWeVduryiQYB3T0PWu2ZqNOsQ/JnyExeyweQAUDLb9bf0i5FIivmV7oGvDrdBcg4c04KRZkLiZ2m3I+zv6GU0Ot/8exyso3ocyCCphwcd2VdS5NtDJYrU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708638098; c=relaxed/simple; bh=iMuX0s4VaALEnLzlD+D8IQh3rFJI+DIf/ErWvzT8vhk=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=fu8bGv8T2V1dTVidopQw2Xxghmu6cCPnYg7WwAN0hbhnQRFHLanVZt6U3GkepQXMjwNdiCFoBc3F1gQOqsmdAkvX/ULIOgWCAZSu7Z2439JZikUSCMfyDKAcMos8s06eHCkSFjQ0QEqpb/5W4Qw+9pLkyyHHcnZeV6lGu4Nw6us= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ltjpLBje; arc=none smtp.client-ip=209.85.219.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-yb1-f170.google.com with SMTP id 3f1490d57ef6-dcbcea9c261so192219276.3 for ; Thu, 22 Feb 2024 13:41:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708638095; x=1709242895; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=QmgCzE9LjwDzmPCc5NVDUCPpvcBzTrPnv764C/CP8R4=; b=ltjpLBjepsxTaPPzGKJEjVVFff5l0uVFJjYK/BbdKBRdlD9lT1r71o1zSQ/hHeuMx6 WlQfuNTxvMoS5fbslhoqRVqjvuk2tIiiBZMmTLXE/aDiU0rcYwIVybyP7mN+RCuWKjgn Y8PsSUSkDaE2Fl3OprRrRjwL3dTmxv6KhmXDOMQI+gGUcafWehC/06fdDdwuecuPeFP9 p9d2ypgh6q5OrBi+1GI+4+VNK4SgSGjqF7kMKqlMpzSyfBcVijYC4BurFvWAFg7yBHW2 MIfEjTkmXv3EFKJYP22bkwfWyal9Of8NcTu1YDECuNONuyNEeIG6lQRXMsY8PL1Nltm1 GW5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708638095; x=1709242895; h=content-transfer-encoding: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=QmgCzE9LjwDzmPCc5NVDUCPpvcBzTrPnv764C/CP8R4=; b=wvH6of0/WgPhUoaS1gFlmw8OzPimcIZXJBG0Q8qyBShG0wT34vpxUM+xluGzzDSEsk mY5BRn1+ZrWjKLYktDepZpP7QLqTn++2ZPonsreicFQWeTp/ctcrweGsoZJrHvdBPNeH HRD3ZgFCKx4vCHbOpaO1lsgpTkmzygd3WR6J5uqiAzSd7puhh7Vg050uyy42Xp4WWftX yFFpOIpg+xx+vt49bVtnd6knwYuW4RLdRGx+1oEpj1WKoXrD4y98Eed+CG2nts/E+R1y 7QP8hk2zfadaEgqfCm1R6e5Nhri/sjESIvxsrimEKboP+L1snihmx4gglKH/ItzLRu2d XMBw== X-Forwarded-Encrypted: i=1; AJvYcCUHXaDTvBXP9BhRIauRc0VaQLUsPq6wAqmHO611irnJKRyo85Sf26ZYS5DBQMzRE9xz/WGuCFy8apsU4d2kql+wu4WsBPEPpx27njeP X-Gm-Message-State: AOJu0YxMHou/RhQUCqK1gsqL3mB3zGQgHJoZ6E1fnXt2hJfPdJP+Ph4K YLQawThu3knpGVHrDd11GZynoaSzhS65hSVTJfK9/gQJmlf7ccw3B3/C4cZQh7D0A/adGxpRst4 XyHVrHhaRTQ/y1wSJw5PhcqABB1es5xbvXtog X-Received: by 2002:a05:690c:3388:b0:602:b697:dc60 with SMTP id fl8-20020a05690c338800b00602b697dc60mr472794ywb.50.1708638095219; Thu, 22 Feb 2024 13:41:35 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240222080815.46291-1-zhengqi.arch@bytedance.com> In-Reply-To: From: Suren Baghdasaryan Date: Thu, 22 Feb 2024 13:41:22 -0800 Message-ID: Subject: Re: [PATCH] mm: userfaultfd: fix unexpected change to src_folio when UFFDIO_MOVE fails To: David Hildenbrand Cc: Qi Zheng , akpm@linux-foundation.org, aarcange@redhat.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Feb 22, 2024 at 12:43=E2=80=AFAM David Hildenbrand wrote: > > On 22.02.24 09:08, Qi Zheng wrote: > > After ptep_clear_flush(), if we find that src_folio is pinned we will f= ail > > 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 > > --- > > 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 =3D 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 =3D mk_pte(&src_folio->page, dst_vma->vm_page_prot); > > /* Follow mremap() behavior and treat the entry dirty after the m= ove */ > > orig_dst_pte =3D pte_mkwrite(pte_mkdirty(orig_dst_pte), dst_vma); > > Indeed, LGTM. > > Reviewed-by: David Hildenbrand Thanks for catching this! Makes total sense to check before modification. Reviewed-by: Suren Baghdasaryan > > -- > Cheers, > > David / dhildenb >