Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp621559ybj; Thu, 7 May 2020 03:51:06 -0700 (PDT) X-Google-Smtp-Source: APiQypLygMr3WRUGcGf7zL8HVV8Pkoz/YrsI+2fptA8Ztm2MZpZz70vn38RTdCVTZRjiFNcMBla4 X-Received: by 2002:a17:906:6811:: with SMTP id k17mr11463227ejr.351.1588848665902; Thu, 07 May 2020 03:51:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588848665; cv=none; d=google.com; s=arc-20160816; b=ZU1N/G5i0gDqPTpqvavHObt1rBO0nOtXPplSZT1IGEWmlsHniEVWzud8hTf0KvGN23 6VbvWEOyV7HgT+sn01V5vb6UmL/Z1qgI72IWPmXNhpIXdzr08ofEIuAvN40dUSkfnuN0 kMJGvB69qP3Y+IKuvnBoGBvPA7KZt0CFODxkutl7x5mf/UFx1jkfdg2p8ZnNyF6e7oWq RkhbHJAk4rw0idf331GJVbUAqXgXhGknA4Ew8Vxf9OUkx5JY0LGLzr8jG+WbWVqGwoZa yGZWFi8EJ4sRO9q9pIfWo5tvMz06+uz7igNpdBpaT9rD0sTU0/ZnCkjXEa7RioxnR+5G Eicw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=HDJlw7bDQi8Wi4/pLpjel0dn8YQBpbxuhOnB1M+bLZ8=; b=xEhUKEdVNyWn8BHYd7cFPcvAAmvpY1H2UBdcXrvlSticekieh50yNEi1YeeDTeYqCp ZEgPUst70W5M9hDjqr3LUDs80eJxwlqFSuiXpKjiyVFS0VtpO/XZ6L4LGu8rfUOydeTY knPiXLJ0uoRAu9W/yeTiyY7OkLoUSk+VeAwt2bKN1NgFc9TanAZ4ondP2cZctbwzapdf VxqYAILx1fgXvSaDCCAB1Ioptfh7EagqgLddsDABApGWgy6BoGNK1oLBu1hT9f+WnxGh 4r7yV5tQFHTjlxuNMbk2xEla8ZQ6yTgTuYqWO6Yk+9GxbN1XSQJwsxJlffk4iS2LSIG0 6bFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=a80ZOeQu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x15si3022578edl.247.2020.05.07.03.50.41; Thu, 07 May 2020 03:51:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=a80ZOeQu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726701AbgEGKsX (ORCPT + 99 others); Thu, 7 May 2020 06:48:23 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:58435 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726367AbgEGKsX (ORCPT ); Thu, 7 May 2020 06:48:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588848500; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=HDJlw7bDQi8Wi4/pLpjel0dn8YQBpbxuhOnB1M+bLZ8=; b=a80ZOeQuLcZqsA9Vj6eSTtfm7wIdFc6LM5Y9r3WU0N8GlOTxispQVtrW/2D3zQHfxUJpHU +O+0yT5Fl3guVWOhAex15ribtX3DAjgdgAE3IVm4Wg3CJvXVM/Yglh/+6gxWao/5qaVjtJ gYY5lngQ6xXsST8B/u4T7TEGwBBEwhg= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-64-3n0f2JrJPIGr3o60Wz3o8w-1; Thu, 07 May 2020 06:48:19 -0400 X-MC-Unique: 3n0f2JrJPIGr3o60Wz3o8w-1 Received: by mail-wr1-f72.google.com with SMTP id a12so3208357wrv.3 for ; Thu, 07 May 2020 03:48:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=HDJlw7bDQi8Wi4/pLpjel0dn8YQBpbxuhOnB1M+bLZ8=; b=HBwMrS+xziGkDTMPloWDlT2zApxM4ABp5dXrfbEbBWxaUE6sa9Xj4aRALxis0K2+DT iYmqPOPtl3Gs/m/8aUeJ3pLWbtIwailtS9ceCpeA/XCBOM9EhZuhMdYFF9lwgku6k5/j RlzJwaBKYqOMJ4rGVojcNsVS6zhzqAos59ALXMFserhwAP/iOAu8+Gf6M9q7OQtsP3T6 wqXc6ny180hEooQlQC2bE3X4/AJkgH2BB7tJMJljzcICb+EAlx3IQw+Id2S6X4Qv6IyW 3yfWpWrRTRy13OADi5t5dcO9N95aS2xnKefVG11CMIzD0+1zZqLdRYXx3cAzkisDTm/I p1Pg== X-Gm-Message-State: AGi0PuZCIZOo71kHYh8Q0jQ6FWnlBkbTftc5Ek0HH49x+m6dQIrzKFk/ jUTGdDFqER0uYvhXQbqqCBUkEfpUBKNZqmqVw38gpe84TDmlULV5Ts/jez5LEqGekp51z+K/IEJ DKXXji5z0iL4h2C61Y0qFcLKE X-Received: by 2002:adf:a4c5:: with SMTP id h5mr14486156wrb.408.1588848497379; Thu, 07 May 2020 03:48:17 -0700 (PDT) X-Received: by 2002:adf:a4c5:: with SMTP id h5mr14486114wrb.408.1588848496853; Thu, 07 May 2020 03:48:16 -0700 (PDT) Received: from redhat.com (bzq-109-66-7-121.red.bezeqint.net. [109.66.7.121]) by smtp.gmail.com with ESMTPSA id s14sm7033661wmh.18.2020.05.07.03.48.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 May 2020 03:48:16 -0700 (PDT) Date: Thu, 7 May 2020 06:48:12 -0400 From: "Michael S. Tsirkin" To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, virtio-dev@lists.oasis-open.org, virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, Michal Hocko , Andrew Morton , Alexander Duyck , Michal Hocko , Pankaj Gupta , Juergen Gross , Konrad Rzeszutek Wilk , Pavel Tatashin , Vlastimil Babka , Johannes Weiner , Anthony Yznaga , Oscar Salvador , Mel Gorman , Mike Rapoport , Dan Williams , Anshuman Khandual , Qian Cai , Pingfan Liu Subject: Re: [PATCH v3 05/15] mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE Message-ID: <20200507064750-mutt-send-email-mst@kernel.org> References: <20200507103119.11219-1-david@redhat.com> <20200507103119.11219-6-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200507103119.11219-6-david@redhat.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 07, 2020 at 12:31:09PM +0200, David Hildenbrand wrote: > virtio-mem wants to allow to offline memory blocks of which some parts > were unplugged (allocated via alloc_contig_range()), especially, to later > offline and remove completely unplugged memory blocks. The important part > is that PageOffline() has to remain set until the section is offline, so > these pages will never get accessed (e.g., when dumping). The pages should > not be handed back to the buddy (which would require clearing PageOffline() > and result in issues if offlining fails and the pages are suddenly in the > buddy). > > Let's allow to do that by allowing to isolate any PageOffline() page > when offlining. This way, we can reach the memory hotplug notifier > MEM_GOING_OFFLINE, where the driver can signal that he is fine with > offlining this page by dropping its reference count. PageOffline() pages > with a reference count of 0 can then be skipped when offlining the > pages (like if they were free, however they are not in the buddy). > > Anybody who uses PageOffline() pages and does not agree to offline them > (e.g., Hyper-V balloon, XEN balloon, VMWare balloon for 2MB pages) will not > decrement the reference count and make offlining fail when trying to > migrate such an unmovable page. So there should be no observable change. > Same applies to balloon compaction users (movable PageOffline() pages), the > pages will simply be migrated. > > Note 1: If offlining fails, a driver has to increment the reference > count again in MEM_CANCEL_OFFLINE. > > Note 2: A driver that makes use of this has to be aware that re-onlining > the memory block has to be handled by hooking into onlining code > (online_page_callback_t), resetting the page PageOffline() and > not giving them to the buddy. > > Reviewed-by: Alexander Duyck > Acked-by: Michal Hocko > Tested-by: Pankaj Gupta > Cc: Andrew Morton > Cc: Juergen Gross > Cc: Konrad Rzeszutek Wilk > Cc: Pavel Tatashin > Cc: Alexander Duyck > Cc: Vlastimil Babka > Cc: Johannes Weiner > Cc: Anthony Yznaga > Cc: Michal Hocko > Cc: Oscar Salvador > Cc: Mel Gorman > Cc: Mike Rapoport > Cc: Dan Williams > Cc: Anshuman Khandual > Cc: Qian Cai > Cc: Pingfan Liu > Signed-off-by: David Hildenbrand Didn't you lose some Andrew Morton's ack on this? > --- > include/linux/page-flags.h | 10 +++++++++ > mm/memory_hotplug.c | 44 +++++++++++++++++++++++++++++--------- > mm/page_alloc.c | 24 +++++++++++++++++++++ > mm/page_isolation.c | 9 ++++++++ > 4 files changed, 77 insertions(+), 10 deletions(-) > > diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h > index 222f6f7b2bb3..6be1aa559b1e 100644 > --- a/include/linux/page-flags.h > +++ b/include/linux/page-flags.h > @@ -777,6 +777,16 @@ PAGE_TYPE_OPS(Buddy, buddy) > * not onlined when onlining the section). > * The content of these pages is effectively stale. Such pages should not > * be touched (read/write/dump/save) except by their owner. > + * > + * If a driver wants to allow to offline unmovable PageOffline() pages without > + * putting them back to the buddy, it can do so via the memory notifier by > + * decrementing the reference count in MEM_GOING_OFFLINE and incrementing the > + * reference count in MEM_CANCEL_OFFLINE. When offlining, the PageOffline() > + * pages (now with a reference count of zero) are treated like free pages, > + * allowing the containing memory block to get offlined. A driver that > + * relies on this feature is aware that re-onlining the memory block will > + * require to re-set the pages PageOffline() and not giving them to the > + * buddy via online_page_callback_t. > */ > PAGE_TYPE_OPS(Offline, offline) > > diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c > index 555137bd0882..936bfe208a6e 100644 > --- a/mm/memory_hotplug.c > +++ b/mm/memory_hotplug.c > @@ -1151,11 +1151,17 @@ struct zone *test_pages_in_a_zone(unsigned long start_pfn, > > /* > * Scan pfn range [start,end) to find movable/migratable pages (LRU pages, > - * non-lru movable pages and hugepages). We scan pfn because it's much > - * easier than scanning over linked list. This function returns the pfn > - * of the first found movable page if it's found, otherwise 0. > + * non-lru movable pages and hugepages). Will skip over most unmovable > + * pages (esp., pages that can be skipped when offlining), but bail out on > + * definitely unmovable pages. > + * > + * Returns: > + * 0 in case a movable page is found and movable_pfn was updated. > + * -ENOENT in case no movable page was found. > + * -EBUSY in case a definitely unmovable page was found. > */ > -static unsigned long scan_movable_pages(unsigned long start, unsigned long end) > +static int scan_movable_pages(unsigned long start, unsigned long end, > + unsigned long *movable_pfn) > { > unsigned long pfn; > > @@ -1167,18 +1173,30 @@ static unsigned long scan_movable_pages(unsigned long start, unsigned long end) > continue; > page = pfn_to_page(pfn); > if (PageLRU(page)) > - return pfn; > + goto found; > if (__PageMovable(page)) > - return pfn; > + goto found; > + > + /* > + * PageOffline() pages that are not marked __PageMovable() and > + * have a reference count > 0 (after MEM_GOING_OFFLINE) are > + * definitely unmovable. If their reference count would be 0, > + * they could at least be skipped when offlining memory. > + */ > + if (PageOffline(page) && page_count(page)) > + return -EBUSY; > > if (!PageHuge(page)) > continue; > head = compound_head(page); > if (page_huge_active(head)) > - return pfn; > + goto found; > skip = compound_nr(head) - (page - head); > pfn += skip - 1; > } > + return -ENOENT; > +found: > + *movable_pfn = pfn; > return 0; > } > > @@ -1441,7 +1459,8 @@ static int __ref __offline_pages(unsigned long start_pfn, > } > > do { > - for (pfn = start_pfn; pfn;) { > + pfn = start_pfn; > + do { > if (signal_pending(current)) { > ret = -EINTR; > reason = "signal backoff"; > @@ -1451,14 +1470,19 @@ static int __ref __offline_pages(unsigned long start_pfn, > cond_resched(); > lru_add_drain_all(); > > - pfn = scan_movable_pages(pfn, end_pfn); > - if (pfn) { > + ret = scan_movable_pages(pfn, end_pfn, &pfn); > + if (!ret) { > /* > * TODO: fatal migration failures should bail > * out > */ > do_migrate_range(pfn, end_pfn); > } > + } while (!ret); > + > + if (ret != -ENOENT) { > + reason = "unmovable page"; > + goto failed_removal_isolated; > } > > /* > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index 64fcaf86763b..6fb3dfca2a97 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -8230,6 +8230,19 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page, > if ((flags & MEMORY_OFFLINE) && PageHWPoison(page)) > continue; > > + /* > + * We treat all PageOffline() pages as movable when offlining > + * to give drivers a chance to decrement their reference count > + * in MEM_GOING_OFFLINE in order to indicate that these pages > + * can be offlined as there are no direct references anymore. > + * For actually unmovable PageOffline() where the driver does > + * not support this, we will fail later when trying to actually > + * move these pages that still have a reference count > 0. > + * (false negatives in this function only) > + */ > + if ((flags & MEMORY_OFFLINE) && PageOffline(page)) > + continue; > + > if (__PageMovable(page) || PageLRU(page)) > continue; > > @@ -8650,6 +8663,17 @@ __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) > offlined_pages++; > continue; > } > + /* > + * At this point all remaining PageOffline() pages have a > + * reference count of 0 and can simply be skipped. > + */ > + if (PageOffline(page)) { > + BUG_ON(page_count(page)); > + BUG_ON(PageBuddy(page)); > + pfn++; > + offlined_pages++; > + continue; > + } > > BUG_ON(page_count(page)); > BUG_ON(!PageBuddy(page)); > diff --git a/mm/page_isolation.c b/mm/page_isolation.c > index 2c11a38d6e87..f6d07c5f0d34 100644 > --- a/mm/page_isolation.c > +++ b/mm/page_isolation.c > @@ -151,6 +151,7 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages) > * a bit mask) > * MEMORY_OFFLINE - isolate to offline (!allocate) memory > * e.g., skip over PageHWPoison() pages > + * and PageOffline() pages. > * REPORT_FAILURE - report details about the failure to > * isolate the range > * > @@ -259,6 +260,14 @@ __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn, > else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page)) > /* A HWPoisoned page cannot be also PageBuddy */ > pfn++; > + else if ((flags & MEMORY_OFFLINE) && PageOffline(page) && > + !page_count(page)) > + /* > + * The responsible driver agreed to skip PageOffline() > + * pages when offlining memory by dropping its > + * reference in MEM_GOING_OFFLINE. > + */ > + pfn++; > else > break; > } > -- > 2.25.3