Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1709485imu; Thu, 10 Jan 2019 01:28:58 -0800 (PST) X-Google-Smtp-Source: ALg8bN5Vikq6eLY4Tc6cjQhIyC5VyXfCoe+SOJKxFUv1COHvL/KaLOXlZxj2ndGRj0eueCtvxxpN X-Received: by 2002:a65:4ccb:: with SMTP id n11mr8814557pgt.257.1547112538758; Thu, 10 Jan 2019 01:28:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547112538; cv=none; d=google.com; s=arc-20160816; b=ynq3WtFWlODd90vcoCeHvnnvoN9mSQCtTQfAYsOoIe2BhISAfzVlytxM96z/Pk+XU7 DT14fhCK5v/KNE6wlekBCA6do6z+wSxTQDyY0sOEX0bvtIKm5IXG3T7u1HUa8Q8Glvqa tQMQKgLxWzY1y50aBZcq6mUrf6p8If97dFk1ge3bDItY5j50W+bEUQnAk7K8endGw7vi O4WQN24lh7Bc7fsfqVjwYVmXH7snGO7vZAH5TilNeqwjz2t47syDtWQqg9ZvvKUnPdW8 2E2OreCdPNvXG0fbXV3y0xXu5ejiGBpsE5xeHxo+LnYHTGAxkGRSX58Su0uy/qNfGwJT YwUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:openpgp:from:references:cc:to:subject; bh=JAOTf4BVudgnhU2okgcYQfbwnmgv/lOLxERzqjfLWl0=; b=CHU0FARolmUA3J2OrORASqdW//HWaOoUxOhx0rJGUKdEhAFqN/aEGneZTEGdAS01Km Lp0ITbpePa+iBhWe5JwCnOUruUDzcNleSjvzSKOtEokedd6bOXTj7rH7XHPfAN5CBtZR pysPcIjfJ51Nn5jCbo2qwsQS29eeEsJE8nr8k/18VpXL7LPktJHCLggN9Hpcib601HYD ktqa3UYobbfCpEYaosnYPuyIcG8DC7/9n+tHOdI/ykQMaBjqz6xWufxZsGj/izeA3ivL 0DvMX7cnTuEK+2+3vDh6+a+NVlRMUMzcdT/jg6zBhRT8ZCbFARa9hQaJtJ/bKfGUnD7V VV6Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i190si43624556pfc.116.2019.01.10.01.28.35; Thu, 10 Jan 2019 01:28:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727882AbfAJJ0k (ORCPT + 99 others); Thu, 10 Jan 2019 04:26:40 -0500 Received: from mx2.suse.de ([195.135.220.15]:57480 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726255AbfAJJ0k (ORCPT ); Thu, 10 Jan 2019 04:26:40 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id DB78FAE85; Thu, 10 Jan 2019 09:26:37 +0000 (UTC) Subject: Re: [PATCHi v2] mm: put_and_wait_on_page_locked() while page is migrated To: Hugh Dickins Cc: Linus Torvalds , Andrew Morton , Baoquan He , Michal Hocko , Andrea Arcangeli , David Hildenbrand , Mel Gorman , David Herrmann , Tim Chen , Kan Liang , Andi Kleen , Davidlohr Bueso , Peter Zijlstra , Christoph Lameter , Nick Piggin , pifang@redhat.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Konstantin Khlebnikov References: From: Vlastimil Babka Openpgp: preference=signencrypt Message-ID: Date: Thu, 10 Jan 2019 10:26:36 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.3 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/26/18 8:27 PM, Hugh Dickins wrote: > Waiting on a page migration entry has used wait_on_page_locked() all > along since 2006: but you cannot safely wait_on_page_locked() without > holding a reference to the page, and that extra reference is enough to > make migrate_page_move_mapping() fail with -EAGAIN, when a racing task > faults on the entry before migrate_page_move_mapping() gets there. > > And that failure is retried nine times, amplifying the pain when > trying to migrate a popular page. With a single persistent faulter, > migration sometimes succeeds; with two or three concurrent faulters, > success becomes much less likely (and the more the page was mapped, > the worse the overhead of unmapping and remapping it on each try). > > This is especially a problem for memory offlining, where the outer > level retries forever (or until terminated from userspace), because > a heavy refault workload can trigger an endless loop of migration > failures. wait_on_page_locked() is the wrong tool for the job. > > David Herrmann (but was he the first?) noticed this issue in 2014: > https://marc.info/?l=linux-mm&m=140110465608116&w=2 > > Tim Chen started a thread in August 2017 which appears relevant: > https://marc.info/?l=linux-mm&m=150275941014915&w=2 > where Kan Liang went on to implicate __migration_entry_wait(): > https://marc.info/?l=linux-mm&m=150300268411980&w=2 > and the thread ended up with the v4.14 commits: > 2554db916586 ("sched/wait: Break up long wake list walk") > 11a19c7b099f ("sched/wait: Introduce wakeup boomark in wake_up_page_bit") > > Baoquan He reported "Memory hotplug softlock issue" 14 November 2018: > https://marc.info/?l=linux-mm&m=154217936431300&w=2 > > We have all assumed that it is essential to hold a page reference while > waiting on a page lock: partly to guarantee that there is still a struct > page when MEMORY_HOTREMOVE is configured, but also to protect against > reuse of the struct page going to someone who then holds the page locked > indefinitely, when the waiter can reasonably expect timely unlocking. > > But in fact, so long as wait_on_page_bit_common() does the put_page(), > and is careful not to rely on struct page contents thereafter, there is > no need to hold a reference to the page while waiting on it. That does > mean that this case cannot go back through the loop: but that's fine for > the page migration case, and even if used more widely, is limited by the > "Stop walking if it's locked" optimization in wake_page_function(). > > Add interface put_and_wait_on_page_locked() to do this, using "behavior" > enum in place of "lock" arg to wait_on_page_bit_common() to implement it. > No interruptible or killable variant needed yet, but they might follow: > I have a vague notion that reporting -EINTR should take precedence over > return from wait_on_page_bit_common() without knowing the page state, > so arrange it accordingly - but that may be nothing but pedantic. > > __migration_entry_wait() still has to take a brief reference to the > page, prior to calling put_and_wait_on_page_locked(): but now that it > is dropped before waiting, the chance of impeding page migration is > very much reduced. Should we perhaps disable preemption across this? > > shrink_page_list()'s __ClearPageLocked(): that was a surprise! This > survived a lot of testing before that showed up. PageWaiters may have > been set by wait_on_page_bit_common(), and the reference dropped, just > before shrink_page_list() succeeds in freezing its last page reference: > in such a case, unlock_page() must be used. Follow the suggestion from > Michal Hocko, just revert a978d6f52106 ("mm: unlockless reclaim") now: > that optimization predates PageWaiters, and won't buy much these days; > but we can reinstate it for the !PageWaiters case if anyone notices. > > It does raise the question: should vmscan.c's is_page_cache_freeable() > and __remove_mapping() now treat a PageWaiters page as if an extra > reference were held? Perhaps, but I don't think it matters much, since > shrink_page_list() already had to win its trylock_page(), so waiters are > not very common there: I noticed no difference when trying the bigger > change, and it's surely not needed while put_and_wait_on_page_locked() > is only used for page migration. > > Reported-and-tested-by: Baoquan He > Signed-off-by: Hugh Dickins > Acked-by: Michal Hocko > Reviewed-by: Andrea Arcangeli For the record, anyone backporting this to older kernels should make sure to also include 605ca5ede764 ("mm/huge_memory.c: reorder operations in __split_huge_page_tail()") or they are in for a lot of fun, like me. Long story [1] short, Konstantin was correct in 605ca5ede764 changelog, although it wasn't the main known issue he was fixing: clear_compound_head() also must be called before unfreezing page reference because after successful get_page_unless_zero() might follow put_page() which needs correct compound_head(). Which is exactly what happens in __migration_entry_wait(): if (!get_page_unless_zero(page)) goto out; pte_unmap_unlock(ptep, ptl); put_and_wait_on_page_locked(page); -> does put_page(page) while waiting on the THP split (which inserts those migration entries) to finish. Before put_and_wait_on_page_locked() it would wait first, and only then do put_page() on a page that's no longer tail page, so it would work out despite the dangerous get_page_unless_zero() on a tail page. Now it doesn't :) Now if only 605ca5ede764 had a CC:stable and a Fixes: tag... Machine Learning won this round though, because 605ca5ede764 was added to 4.14 stable by Sasha... [1] https://bugzilla.suse.com/show_bug.cgi?id=1119962#c16