Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4148562imu; Sat, 24 Nov 2018 20:08:23 -0800 (PST) X-Google-Smtp-Source: AFSGD/WPTFKm1ePODqrt4JDyAEiCMWOSVyapiWaUurv8E984KecW4JAOJlINFN/OUQ0YWo3Vu1GT X-Received: by 2002:a17:902:a988:: with SMTP id bh8-v6mr22270328plb.163.1543118903431; Sat, 24 Nov 2018 20:08:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543118903; cv=none; d=google.com; s=arc-20160816; b=a4xkN6wxP+3OWiZX+pM7Re1BEqCMk1p30g81ET8iGFLhVvZ5cvDDmuVTzQEhoCCPps GpWIDfcA7rL4pA6ijv4paj3rXOKczhwXnSvDm7zqaTTm85T/NCP5ZH2NuHeVA3/JjPy/ pCe/hdzGR2qLm925Lijsmwb06GpjlKEQdK/bysUis11q7GjPhbPf5X7S9KZowDYT5GFU 5UdO7ZNSptlFsbXVwGpfwwjzdFPZWZj7BE40FuuX/N+l4n0oZDvIMh8OMwPb79rSEWrV R5uaaANsjg2rjOa4MeFy1Vt5LkgF2fmvH/sqgCtQiSf4bR1vvnzgezctIcqoXREAL2YA 1OzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=BMCRA8Qma2ifeQJWad3ELZ4jvZOyoN5YsTTEz8tiwkQ=; b=OsMFZK6QQjVOSW4kSZYgNByv1//RpeUsCsALbLxKKoYmKCAFcN9mmXJcgOaMf/kRZH YuXvsGrnNV0Pm5dXsU5b/uTtvJaSg9PuON+CMWX8Lx6ECrwQHumtOfJ/TRHRQcWuQyeP fUFE/NfjihLndw+N+ZC/N7UOgQIvMeE/6VYy35Pmpm1UN8i3WEvBWuoguX8vfuOuDfdy VHS0sYGSuudsnmq1kufTGL7Qx71wi4c/FlCo/ueBHGE8D7pw8ZDpu14T1rC5mWr9RFdI mpZnTxox1elZWOv1IyUr9fuKxITYPKNQsfQj64KpcrxJwIsX5eG5htUa8aQGFFWL8+WM lIbg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h7si37137602pls.326.2018.11.24.20.08.07; Sat, 24 Nov 2018 20:08:23 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727003AbeKYO5l (ORCPT + 99 others); Sun, 25 Nov 2018 09:57:41 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48250 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726540AbeKYO5l (ORCPT ); Sun, 25 Nov 2018 09:57:41 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B92DA307EA99; Sun, 25 Nov 2018 04:07:33 +0000 (UTC) Received: from sky.random (ovpn-120-160.rdu2.redhat.com [10.10.120.160]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2BAAE103BAB6; Sun, 25 Nov 2018 04:07:30 +0000 (UTC) Date: Sat, 24 Nov 2018 23:07:29 -0500 From: Andrea Arcangeli To: Hugh Dickins Cc: Linus Torvalds , Andrew Morton , Baoquan He , Michal Hocko , Vlastimil Babka , David Hildenbrand , Mel Gorman , David Herrmann , Tim Chen , Kan Liang , Andi Kleen , Davidlohr Bueso , Peter Zijlstra , Christoph Lameter , Nicholas Piggin , pifang@redhat.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH] mm: put_and_wait_on_page_locked() while page is migrated Message-ID: <20181125040729.GF4932@redhat.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Sun, 25 Nov 2018 04:07:34 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Nov 24, 2018 at 07:21:07PM -0800, 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 negative > value of the 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