From mboxrd@z Thu Jan 1 00:00:00 1970 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 A4A05C021BB for ; Tue, 25 Feb 2025 22:21:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EADD96B0095; Tue, 25 Feb 2025 17:21:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E5CDE6B0096; Tue, 25 Feb 2025 17:21:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D249B6B0098; Tue, 25 Feb 2025 17:21:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6AAA56B0095 for ; Tue, 25 Feb 2025 17:21:55 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 875A4140447 for ; Tue, 25 Feb 2025 22:21:54 +0000 (UTC) X-FDA: 83159890548.25.A2B755D Received: from mail-qt1-f179.google.com (mail-qt1-f179.google.com [209.85.160.179]) by imf26.hostedemail.com (Postfix) with ESMTP id 94E4C140002 for ; Tue, 25 Feb 2025 22:21:52 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="UCn3T/1S"; spf=pass (imf26.hostedemail.com: domain of surenb@google.com designates 209.85.160.179 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740522112; a=rsa-sha256; cv=none; b=K6PXIQDrozwesvm6OykRo+RD7DxdjzdKFScggtS9O9GYwLWfWLq2u6FqFzqwIwKIjJAOu/ rJXPkaaKC7dYrMHSAyUy1bvXYzA2Qm2DdshFKzGUnO55PwnvANzwqvsF2rhGH3gMajPshB f/MHvhUNdMH5sgMK1qPJUdYbX1B5K54= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="UCn3T/1S"; spf=pass (imf26.hostedemail.com: domain of surenb@google.com designates 209.85.160.179 as permitted sender) smtp.mailfrom=surenb@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=1740522112; 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=nU6IIXGWGh9H/ehdT9Qc/ulzK83hLj2Kl5Kcq106fII=; b=43csdFpuM3k622b+URfyRSRWaFfH/LinNq3Gkjo78+4zWafjyULpsbhwKV3XsM8o4jJgRR TtSLy7sbDJWluRLaCikUfyf76sJ8MoPldlrQRIvZCHDyrzWRAlnVj6L3X4Yza3DoJsk+1B 6NTXjfSPfCC23XVoO5CvoSNxMSsKyRA= Received: by mail-qt1-f179.google.com with SMTP id d75a77b69052e-471fa3b19bcso75761cf.0 for ; Tue, 25 Feb 2025 14:21:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740522111; x=1741126911; darn=kvack.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=nU6IIXGWGh9H/ehdT9Qc/ulzK83hLj2Kl5Kcq106fII=; b=UCn3T/1SxkIdgAgoKAOzrwBixNGhe5DVZVecHRN19kVQxGYtb07n3CdB+H3lnIc/A7 rzlk5PJa5ntfc+nZzFtbBj1W2Mk5IZy0dAGViBeD5Jf64lqzReQ4iENHEyZQUAHoNT30 C5GWPwbLUNlwmZUoJMW7BU76fw5BGXsdRGl9yfqBXubAXJ1GSbqEVZ3Be9ZK9FtVDbSQ Vp49Vfy0tlJ8r6Q83vkMDGg2K8cfZ75iStEpTL0JaXnwEjk5L+915kizrWhkie26vXRB lvZJA+KAPWTqU5tmLo3g+Tjf7rKx8WExArpP/0FCh/2wKhTkmD0ilCJ2pY0+t40xedsZ uvrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740522111; x=1741126911; 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=nU6IIXGWGh9H/ehdT9Qc/ulzK83hLj2Kl5Kcq106fII=; b=SRj7RaQrTCWPlVgFLydubGLi5b2Mq0x8kkcAVSWXFRrQgbMz9rc6nTtPjEs8Y1gTJf o4Z2FKuBu22sCcjEJ8mTofG7wyC4WFfXPH6lylOsMNNp+Cj/2Jrr1iLoTgK82FTP+2j7 1zjjer9pBQs2i2t4YeD0OtwyP+pJ1bCC/rHuDtb3cGI4jdQ31qnM8tHevg3fkaFLCX2N rpR8+0zvrOweikckA5BB/Hph6VzbyrB+SfmlBYJgydDGuW/71XHx6gzEOvCyZuKYHz0D vixT527rgRljL3rV6Skn0miAvWTWfb6XHozxPmDy9duxhEVZwFWfKwm/8xXON5HAxSsJ x3CA== X-Forwarded-Encrypted: i=1; AJvYcCW7f3ARkqrn7Gt9ApNopmiEeYDFZZO40LTpANVsL3fROGA8mo9pOcs9G7+33c8x01eet5gSxuwmJQ==@kvack.org X-Gm-Message-State: AOJu0Yzd79tpEtWyibzVSFQaEwh7cC2gwXJqJROOsvdxUKgxXtZ6CIcv SiyNc6wAPU08uWAFEVRnouyKH3Jt2vTm48R8K1IptxhILoIttYzsX46jKAY2T/0f3xhjuWWryG6 9k8a2ol4NMlTnGESzypMdDgEnb4MpnaxcA+uT X-Gm-Gg: ASbGncuijnZrazOKbmJAWgfkkstQyiVvNtUKY2/ypdiResnbAqCrbmOBOj2TX+Bk/IO 4CZJ/BT2LZUo3kwXJ6HjjM90A6KRAGGepYKjdt92U5mcM/52Lp6099U9pcq0m0aF6RhN6j9OLuB WsWDctHKk= X-Google-Smtp-Source: AGHT+IEq/AKzX+cYl7aJmSUqhtY1SddAAUnJW41lZVjVI/xrlYAHTtZS1Og5x8RQPWjaHO4K/43BSUS1zvjg+LAB+A4= X-Received: by 2002:ac8:7f95:0:b0:471:f257:7a5f with SMTP id d75a77b69052e-47376e8f38cmr7477371cf.13.1740522111074; Tue, 25 Feb 2025 14:21:51 -0800 (PST) MIME-Version: 1.0 References: <20250225204613.2316092-1-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Tue, 25 Feb 2025 14:21:39 -0800 X-Gm-Features: AWEUYZlr9psNh16uDKwukoh4oYDN-sgVhvIBloc0uSr9nmxcabIzsX5L3_guFh0 Message-ID: Subject: Re: [PATCH 1/1] userfaultfd: do not block on locking a large folio with raised refcount To: Peter Xu Cc: akpm@linux-foundation.org, lokeshgidra@google.com, aarcange@redhat.com, 21cnbao@gmail.com, v-songbaohua@oppo.com, david@redhat.com, willy@infradead.org, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, hughd@google.com, jannh@google.com, kaleshsingh@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 94E4C140002 X-Stat-Signature: 4q7pc565ettzrz776xdfaup1ximnusun X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1740522112-170053 X-HE-Meta: U2FsdGVkX19J9VQWdpGLLnVyfuW1OMlPfjX4mgAicpaCHLgm34TrS2s+emExhhZfJduDT30t+tsw1clD7qhiTWSfrLAygTZ4JrzXE5unhoVzVBluUdTUNKVPHOnqxyz0qt4l7A2n3Cqs8VhB+6dSJB7wTG1zyJOQWD6FdxiN/VYMiJyqjk5L/XpbQpPjMHYZn2McuQaxYU6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amicity=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: On Tue, Feb 25, 2025 at 2:12=E2=80=AFPM Suren Baghdasaryan wrote: > > On Tue, Feb 25, 2025 at 1:32=E2=80=AFPM Peter Xu wrot= e: > > > > On Tue, Feb 25, 2025 at 12:46:13PM -0800, Suren Baghdasaryan wrote: > > > Lokesh recently raised an issue about UFFDIO_MOVE getting into a dead= lock > > > state when it goes into split_folio() with raised folio refcount. > > > split_folio() expects the reference count to be exactly > > > mapcount + num_pages_in_folio + 1 (see can_split_folio()) and fails w= ith > > > EAGAIN otherwise. If multiple processes are trying to move the same > > > large folio, they raise the refcount (all tasks succeed in that) then > > > one of them succeeds in locking the folio, while others will block in > > > folio_lock() while keeping the refcount raised. The winner of this > > > race will proceed with calling split_folio() and will fail returning > > > EAGAIN to the caller and unlocking the folio. The next competing proc= ess > > > will get the folio locked and will go through the same flow. In the > > > meantime the original winner will be retried and will block in > > > folio_lock(), getting into the queue of waiting processes only to rep= eat > > > the same path. All this results in a livelock. > > > An easy fix would be to avoid waiting for the folio lock while holdin= g > > > folio refcount, similar to madvise_free_huge_pmd() where folio lock i= s > > > acquired before raising the folio refcount. > > > Modify move_pages_pte() to try locking the folio first and if that fa= ils > > > and the folio is large then return EAGAIN without touching the folio > > > refcount. If the folio is single-page then split_folio() is not calle= d, > > > so we don't have this issue. > > > Lokesh has a reproducer [1] and I verified that this change fixes the > > > issue. > > > > > > [1] https://github.com/lokeshgidra/uffd_move_ioctl_deadlock > > > > > > Reported-by: Lokesh Gidra > > > Signed-off-by: Suren Baghdasaryan > > > > Reviewed-by: Peter Xu > > > > One question irrelevant of this change below.. > > > > > --- > > > mm/userfaultfd.c | 17 ++++++++++++++++- > > > 1 file changed, 16 insertions(+), 1 deletion(-) > > > > > > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c > > > index 867898c4e30b..f17f8290c523 100644 > > > --- a/mm/userfaultfd.c > > > +++ b/mm/userfaultfd.c > > > @@ -1236,6 +1236,7 @@ static int move_pages_pte(struct mm_struct *mm,= pmd_t *dst_pmd, pmd_t *src_pmd, > > > */ > > > if (!src_folio) { > > > struct folio *folio; > > > + bool locked; > > > > > > /* > > > * Pin the page while holding the lock to be su= re the > > > @@ -1255,12 +1256,26 @@ static int move_pages_pte(struct mm_struct *m= m, pmd_t *dst_pmd, pmd_t *src_pmd, > > > goto out; > > > } > > > > > > + locked =3D folio_trylock(folio); > > > + /* > > > + * We avoid waiting for folio lock with a raise= d refcount > > > + * for large folios because extra refcounts wil= l result in > > > + * split_folio() failing later and retrying. If= multiple > > > + * tasks are trying to move a large folio we ca= n end > > > + * livelocking. > > > + */ > > > + if (!locked && folio_test_large(folio)) { > > > + spin_unlock(src_ptl); > > > + err =3D -EAGAIN; > > > + goto out; > > > + } > > > + > > > folio_get(folio); > > > src_folio =3D folio; > > > src_folio_pte =3D orig_src_pte; > > > spin_unlock(src_ptl); > > > > > > - if (!folio_trylock(src_folio)) { > > > + if (!locked) { > > > pte_unmap(&orig_src_pte); > > > pte_unmap(&orig_dst_pte); > > > > .. just notice this. Are these problematic? I mean, orig_*_pte are st= ack > > variables, afaict. I'd expect these things blow on HIGHPTE.. > > Ugh! Yes, I think so. From a quick look, move_pages_pte() is the only > place we have this issue and I don't see a reason for copying src_pte > and dst_pte values. I'll spend some more time trying to understand if > we really need these local copies. Ah, we copy the values to later check if PTEs changed from under us. But I see no reason we need to use orig_{src|dst}_pte instead of {src|dst}_pte when doing pte_unmap(). I think we can safely replace them with the original ones. WDYT? > > > > > > src_pte =3D dst_pte =3D NULL; > > > > > > base-commit: 801d47bd96ce22acd43809bc09e004679f707c39 > > > -- > > > 2.48.1.658.g4767266eb4-goog > > > > > > > -- > > Peter Xu > >