From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 5A6842FE042 for ; Fri, 9 Jan 2026 02:17:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767925050; cv=none; b=F5BbW3CJdnJiFrQmXRVfh1lIEXJQu5l6GScLABdpDejFWEU/gvnV+cW7FzHrsENOvGXHdZXdRkYrOQeWR11sZQYy9ixAEYG4H9xc+wflZieDBHRmtKMP7fyaciHe0RGeIxdgYKqtakEg6k0Mk2+31hdSxINuQ+Kdt0sygBfzcXY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767925050; c=relaxed/simple; bh=9uOQuUF5u+fGu0zEOSX42+5A9cqnhje8ckhUy9/3BZ4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=JO4bAXgZ3mTCehIb9tXsyDtRt/dA+hAW/J0hjYYKntGdboTytB+plvAkPI9qT2km31gEwa+GdUdLPivohwuV4WYvBW9VZt6icfbUqVbgY8h6RE1dk0CvwI1LLMvUB422jRSK9B7m+u4KRxrCr5IPdLtI8j+3vEwUDNmyy4KC97A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=c1tk5u44; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="c1tk5u44" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-7f0db5700b2so2022160b3a.0 for ; Thu, 08 Jan 2026 18:17:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767925044; x=1768529844; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=PwRw5BMh44S+CRA0S+aM2X056BQ3MKgakAszDLhEDMA=; b=c1tk5u44LF+WNwrYm+htTOnoRHMBrJri3gaW3avw5X/NK8Cq7gjU3YX9hj9frGNao4 Jk9IWyA9p63y6WVAp0dDwjTKrJ9tekE3fYRbnhnfWRRi4o/Nk2BMprG5JEz+rWw5uAmI jdVOYxwU0cm4wQ4rKQpJDd5fQV+X1b/93M3xcd/gVnwg+K8TFfFWynwtfklK/5WHdYfQ 9LrhY/p/GQ5DPyJfGAIuPwnoL0RTwS8s4nazlGwU+OUu8OEclr86m3jzdQJSV8UYWUMm Qizvada/ZrDGiSXfTfUXOozDFU0GNmtzKo2YICpe7psFBj4sa5amxphp54bSRhdbs5wX krGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767925044; x=1768529844; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PwRw5BMh44S+CRA0S+aM2X056BQ3MKgakAszDLhEDMA=; b=bo1dKcieH6TER2UK7QkKx6Hb8B2Eqtd+oNST2SC+XiebpzZzO4GlujW1JxyAr8h+3z wPN1G43E+e8J5zhwSRGVXlzkR/zhhnvNSnw1t5wh440B8qhDWgf89JoMz01Y0nJ9ae/h WJ7WqNMeClANlk1MkPYx6fbAqeBcbfwso3iLRJLT4VJihqZZ5LZcCcV/pgzp5IFKWptr vJtkDmfvcKb17C2esUMaVQ0wwccuZBBlrpD34eQ4d4NXp4WMZ5kuQAvgo28m7tmfw60Z /38OhBcCqY3IhUjZxerU2W4CnNXc7QsJZhp44A6n2kgNeYYr5SS7cXTX1FTHXwYLTMoV uIrw== X-Forwarded-Encrypted: i=1; AJvYcCXLHmzblZGHjESdOthCIgZ5bpglT5t/6JYPxT52h2q6e4mwUh5Fs/gxTTfawG/Ue2Ro/Y8QMD9LVLCroOU=@vger.kernel.org X-Gm-Message-State: AOJu0YzIdydRORApm+W021zjWusl+yh2FDeuIzG7vOE5iVTvfUNwZBaB yvj3+LWDSRqXCRu2FTJqfxy7bQGEdytcf9zPs6ji/0y8ETUvC9P/WmGe X-Gm-Gg: AY/fxX7ezY7rsfuNyRZ2tbs/ApQvzZWGbODaz6EW1wspVEJ0w3gfVrHbINCHhZi7J1Y PE7EsGvCBq3Dtwy6FkUpwZu6eUL2dPmEg8gsZd/nZbxamCDKsZ1F8CW5BOIVt4nNtJKT0F1O7+c d+C7T3+yNaQFbNDNeME7pn8JegvR93SPaWyvnLIQeTAT6VAQYRgimZEoxoAqAfIbcjDoqwdc5KD C1JxITfyvyWw8RlHUHzM1vz3KwVfFZe7c7DW2nqjfH2fwx52a2vPi9KsEtcW90fT0Wqr9gYUvg0 4jhNFtEiBSsvLLVIdO/TuGNQkg/0VwIAsNmq1kC7oxnIyRJP8eZYblDaYtUdg71nQKLsq7eyO55 tfELeCOqPPyMP5hhlVi/s7Va78eK/pRKAmboHadTdZVAOC1uxPttKit0lzSqgtLfqBVbGZNIVhM Medoc= X-Google-Smtp-Source: AGHT+IGy2Z6iLGjL3DNp/CTf0kZT8VLqMc/3PkmnlEsgK70K/KkuQcvlpg+/MgjiWVU0E9PF5h/8+A== X-Received: by 2002:a05:6a20:244a:b0:35b:b97f:7bd2 with SMTP id adf61e73a8af0-3898f8f5711mr7503947637.10.1767925044125; Thu, 08 Jan 2026 18:17:24 -0800 (PST) Received: from localhost ([2a12:a304:100::205b]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c4cbf28ebe4sm9133374a12.4.2026.01.08.18.17.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jan 2026 18:17:23 -0800 (PST) Date: Fri, 9 Jan 2026 10:17:19 +0800 From: Jinchao Wang To: Matthew Wilcox Cc: Muchun Song , Oscar Salvador , David Hildenbrand , Andrew Morton , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, syzbot+2d9c96466c978346b55f@syzkaller.appspotmail.com, Zi Yan Subject: Re: [PATCH 2/2] Fix an AB-BA deadlock in hugetlbfs_punch_hole() involving page migration. Message-ID: References: <20260108123957.1123502-1-wangjinchao600@gmail.com> <20260108123957.1123502-2-wangjinchao600@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Thu, Jan 08, 2026 at 02:09:19PM +0000, Matthew Wilcox wrote: > On Thu, Jan 08, 2026 at 08:39:25PM +0800, Jinchao Wang wrote: > > The deadlock occurs due to the following lock ordering: > > > > Task A (punch_hole): Task B (migration): > > -------------------- ------------------- > > 1. i_mmap_lock_write(mapping) 1. folio_lock(folio) > > 2. folio_lock(folio) 2. i_mmap_lock_read(mapping) > > (blocks waiting for B) (blocks waiting for A) > > > > Task A is blocked in the punch-hole path: > > hugetlbfs_fallocate > > hugetlbfs_punch_hole > > hugetlbfs_zero_partial_page > > filemap_lock_hugetlb_folio > > filemap_lock_folio > > __filemap_get_folio > > folio_lock > > > > Task B is blocked in the migration path: > > migrate_pages > > migrate_hugetlbs > > unmap_and_move_huge_page > > remove_migration_ptes > > __rmap_walk_file > > i_mmap_lock_read > > > > To break this circular dependency, use filemap_lock_folio_nowait() in > > the punch-hole path. If the folio is already locked, Task A drops the > > i_mmap_rwsem and retries. This allows Task B to finish its rmap walk > > and release the folio lock. > > It looks like you didn't read the lock ordering at the top of mm/rmap.c > carefully enough: > > * hugetlbfs PageHuge() take locks in this order: > * hugetlb_fault_mutex (hugetlbfs specific page fault mutex) > * vma_lock (hugetlb specific lock for pmd_sharing) > * mapping->i_mmap_rwsem (also used for hugetlb pmd sharing) > * folio_lock > Thanks for the correction, Matthew. > So page migration is the one taking locks in the wrong order, not > holepunch. Maybe something like this instead? > I will test your suggested change and resend the fix. > > diff --git a/mm/migrate.c b/mm/migrate.c > index 5169f9717f60..4688b9e38cd2 100644 > --- a/mm/migrate.c > +++ b/mm/migrate.c > @@ -1458,6 +1458,7 @@ static int unmap_and_move_huge_page(new_folio_t get_new_folio, > int page_was_mapped = 0; > struct anon_vma *anon_vma = NULL; > struct address_space *mapping = NULL; > + enum ttu_flags ttu = 0; > > if (folio_ref_count(src) == 1) { > /* page was freed from under us. So we are done. */ > @@ -1498,8 +1499,6 @@ static int unmap_and_move_huge_page(new_folio_t get_new_folio, > goto put_anon; > > if (folio_mapped(src)) { > - enum ttu_flags ttu = 0; > - > if (!folio_test_anon(src)) { > /* > * In shared mappings, try_to_unmap could potentially > @@ -1516,16 +1515,17 @@ static int unmap_and_move_huge_page(new_folio_t get_new_folio, > > try_to_migrate(src, ttu); > page_was_mapped = 1; > - > - if (ttu & TTU_RMAP_LOCKED) > - i_mmap_unlock_write(mapping); > } > > if (!folio_mapped(src)) > rc = move_to_new_folio(dst, src, mode); > > if (page_was_mapped) > - remove_migration_ptes(src, !rc ? dst : src, 0); > + remove_migration_ptes(src, !rc ? dst : src, > + ttu ? RMP_LOCKED : 0); > + > + if (ttu & TTU_RMAP_LOCKED) > + i_mmap_unlock_write(mapping); > > unlock_put_anon: > folio_unlock(dst);