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 4CD6EC021B8 for ; Wed, 26 Feb 2025 16:11:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BAD2D28000A; Wed, 26 Feb 2025 11:11:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B5D81280008; Wed, 26 Feb 2025 11:11:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A251528000A; Wed, 26 Feb 2025 11:11:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 850CF280008 for ; Wed, 26 Feb 2025 11:11:41 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6E3C7A43B9 for ; Wed, 26 Feb 2025 16:11:40 +0000 (UTC) X-FDA: 83162586360.01.E01A069 Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) by imf22.hostedemail.com (Postfix) with ESMTP id 02C30C0023 for ; Wed, 26 Feb 2025 16:11:37 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=OdQRbcJw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of surenb@google.com designates 209.85.160.170 as permitted sender) smtp.mailfrom=surenb@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740586298; a=rsa-sha256; cv=none; b=7LRfRGmNYm9Gs4/X29Zb4QtYtABjpQptBvJbchFnpRb2Qj8ZAbdUEsg2YG1py17DmDyEOx wDdMAvoXhcWgvrryLSpQGN7djjvzJlXifOn7i1b7d5m8mi6XMVOMIzVZE3GfP15cDtq0mt xXWOiCRr6M2EVw6YEY7o5A8qFZUNubk= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=OdQRbcJw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of surenb@google.com designates 209.85.160.170 as permitted sender) smtp.mailfrom=surenb@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740586298; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to: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=pwCHfbhicWcn2tnaux2bdmjEmUqHPAmz5QiamhshwXM=; b=MEE000jXc6t8MNIvxOu4SSq5HiQzc/4pfavp+qFMt4nqjCm5ygqqNRKM49uysGauP9KlFT w/pzCI3vdzCvDKzWkoINs14BRXX0yHc8grglmkvgnt2lSCpaHaXmwgizazQN8DiyypQJAU 4t4FxgnsX9yERcVHGwi7MD6ihWwdcVg= Received: by mail-qt1-f170.google.com with SMTP id d75a77b69052e-471fa3b19bcso284741cf.0 for ; Wed, 26 Feb 2025 08:11:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740586297; x=1741191097; darn=kvack.org; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=pwCHfbhicWcn2tnaux2bdmjEmUqHPAmz5QiamhshwXM=; b=OdQRbcJwO0yt8ZAP1AYjLNsZwE7qJmrfkz2TuhSr9BRfOqO+4AcAQrE+dh9TLC1wKo VQOYXPYdSGfo2tgiodtZ1BDnsEtQjHuKSL9BAFrumMEWhXQR0XNMDN1uMUhLVIUBUw/C Taf/JmiXO+96iJral9d7xskDkOGthC4rGlGMfMT1SV0ZIHAzurgGlbOBtuGfNz1i2jWL UEFWKAEPaNmDYh5Fu7HeE/QFIW2/i0g8kRuHtZhwvCbLU9hztyoHKqFTw1cG3rJg79s/ phimL9BZZnm5z/96AXOZSEsYSoAAXOdGcjiYPJUnrCody08BQ5J4oPgNbvt60xgfOfEl 3jvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740586297; x=1741191097; h=content-transfer-encoding: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=pwCHfbhicWcn2tnaux2bdmjEmUqHPAmz5QiamhshwXM=; b=NXJIEbF8uS92tWpABeRVeRjjemALxz5FrbPfIuzBm3rBjit2uOaAk5I8KxdaCqlI8A cqOzhVOuJODSNMX2LtOzbj6F7Jxwbi7ttn2m+6c94/SlJJIXApbotWzmHal4YmfhKC0N +cAK8y+clnnLCtDm7HM7H1axyK5ZJR5/QtoaGcBZncdBU2c+r+79RZS1ejM1dx96MbXE tH3sywh8lYR5dZBMDMu92w/+0d6rBZLyJL+wa9YmLixgimaG/xhL7l0WA9+sHU3peacy n+E24NRqSsueoHFqAhL78zA129iCKq4gWQPWqK5VFKUQAdDtyBa0qSI8QvM5Hw7pR4te eTnA== X-Forwarded-Encrypted: i=1; AJvYcCU6v3oBjtdHG8a2bUXTiTCTiyKQpTkt+0WP+X9QBTTQqY39D96+XsP1NPh9RTx5/Pc7ApOWTSH1gg==@kvack.org X-Gm-Message-State: AOJu0YxT2mHoiW3Cv7RNHwdRTO3trNT/C4FUJgoMMmhTr+z/1vjaj3Uv t9HNoyGFOavcHX6fjTPhGLCqL6e1cdmnbgZm24KzlzOztKGAEMWrSnzZaVQZj8PH/lscGpa8isF J4FMJqeqHSw6jqcagktIwv0IN1mDgNL8XEFcV X-Gm-Gg: ASbGnctANgnSG//0C3qAOd1cXVSYXFNCIMNYXO2tP6B++IrgF6DCYzFMEkUwl8VXwxB NGKZh9Wz2llAty2zFm+LIlqtRsEgve/zL8p4gnr7o2yfv0/837247DxJHaFyJB1u6gtvfY9BPWR frphntylk= X-Google-Smtp-Source: AGHT+IFiCKmdVbYOt2KP//r3n7xxDe7R+UyaTUbVNy3crUUGmdiA3FmByLxZ4eeouS76WocPmOIKx6AeTUR+VOuemys= X-Received: by 2002:ac8:7e92:0:b0:471:f437:297b with SMTP id d75a77b69052e-47376e6ef47mr10126811cf.10.1740586296675; Wed, 26 Feb 2025 08:11:36 -0800 (PST) MIME-Version: 1.0 References: <20250225204613.2316092-1-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Wed, 26 Feb 2025 08:11:25 -0800 X-Gm-Features: AWEUYZmlMJKFg0nI_cM2J4ea8f_p7WdxOS84BPznEIazPvAF6Hulb6PhIwxyaZA Message-ID: Subject: Re: [PATCH 1/1] userfaultfd: do not block on locking a large folio with raised refcount To: "Liam R. Howlett" , Suren Baghdasaryan , akpm@linux-foundation.org, lokeshgidra@google.com, aarcange@redhat.com, 21cnbao@gmail.com, v-songbaohua@oppo.com, david@redhat.com, peterx@redhat.com, willy@infradead.org, 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-Rspam-User: X-Rspamd-Queue-Id: 02C30C0023 X-Rspamd-Server: rspam12 X-Stat-Signature: t7a6r51twb6r8akicjrotk4g995t6k1a X-HE-Tag: 1740586297-127249 X-HE-Meta: U2FsdGVkX19UH5ojlWOxsM/cIwsxAYlHE57QSkm8CsqLG4uQC6TNgaV2HycGeWJJq1QYFyyMy2VBTM/5FiyxFRE7qbnC1y/wy0La4OTbOEIkMAZQVFjop4iZtVArAzxGUZZI5Gl1f6J+sJO2yzOG/Qh2NyNqaxU7kxg7ThjL9+/T5Qj5W8KRQX2CfC4f9QOSMS73qKTHhvAWjOZLcQiy5sCvnRlt3mBjc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owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, Feb 26, 2025 at 6:59=E2=80=AFAM Liam R. Howlett wrote: > > * Suren Baghdasaryan [250225 15:46]: > > Lokesh recently raised an issue about UFFDIO_MOVE getting into a deadlo= ck > > 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 wit= h > > 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 proces= s > > 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 repea= t > > the same path. All this results in a livelock. > > An easy fix would be to avoid waiting for the folio lock while holding > > folio refcount, similar to madvise_free_huge_pmd() where folio lock is > > acquired before raising the folio refcount. > > Modify move_pages_pte() to try locking the folio first and if that fail= s > > and the folio is large then return EAGAIN without touching the folio > > refcount. If the folio is single-page then split_folio() is not called, > > 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 > > --- > > 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, p= md_t *dst_pmd, pmd_t *src_pmd, > > */ > > if (!src_folio) { > > struct folio *folio; > > + bool locked; > > > > /* > > * Pin the page while holding the lock to be sure= the > > @@ -1255,12 +1256,26 @@ static int move_pages_pte(struct mm_struct *mm,= pmd_t *dst_pmd, pmd_t *src_pmd, > > goto out; > > } > > > > + locked =3D folio_trylock(folio); > > + /* > > + * We avoid waiting for folio lock with a raised = refcount > > + * for large folios because extra refcounts will = result in > > + * split_folio() failing later and retrying. If m= ultiple > > + * tasks are trying to move a large folio we can = end > > + * livelocking. > > + */ > > + if (!locked && folio_test_large(folio)) { > > + spin_unlock(src_ptl); > > + err =3D -EAGAIN; > > + goto out; > > + } > > + > > Reversing the locking/folio_get() is okay because of the src_ptl spin > lock, right? It might be worth saying something about it in the > comment? That is correct. We take both folio lock and refcount before we drop PTL. I'll add a comment. Thanks! > > > 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); > > src_pte =3D dst_pte =3D NULL; > > > > base-commit: 801d47bd96ce22acd43809bc09e004679f707c39 > > -- > > 2.48.1.658.g4767266eb4-goog > >