Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3399060imu; Mon, 28 Jan 2019 04:16:43 -0800 (PST) X-Google-Smtp-Source: ALg8bN7DoYSesH6TEHPZC3ykK5aphE5gcLar+O1J4bAl/MzuRUvd+5mhnSGI8E180YOBpiPnoKI3 X-Received: by 2002:a17:902:5a0b:: with SMTP id q11mr21755837pli.186.1548677803070; Mon, 28 Jan 2019 04:16:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548677803; cv=none; d=google.com; s=arc-20160816; b=mdxX8YaSmc0M1o32Y1zWbpOoUvAauBE6BlLMxrtFYng/fYmx7AqCEuo+33rpOmim5U 7uWlvyT4NH9MnhXH5UZUKrnHGVAxQAtfLKUP4YE1Mv/44l0v6kzNTcPrYjvs1ouNDt18 XphymvjX6iMKUT5nyLmgXI0lMptSmgtp8mzZvj9tgEXwcXaJDFdXk4x726GHD9IvRKCG XvLVf9hURvTX8dM7lOU4dH8rA/kvNTASAV1qQy5d6+4qhcE9w0bDj4PkK/A98MyFvaun TDBolVZHKbDxxAiR1c4f99KhsaXSs3CKcSjC55eY+TtUuxyINtrHwhTavr/goXussHeA fqJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=TK96U1go4zqbtsuVGbVmpPmjd+GVv1COkR+NvBptkt8=; b=BAmI5eQeSMComNMfXvj4dN9Zd1ZSiwKuOwzkpOyWOmCqFHeJe7c9z81jHEHqY6+WCm /KpoCrwIAsep2x3jNCNn0XFv/7wGKERzqfOQ1yQeKWHgjQOP9tuNbwfrEz+6JXoW/GU6 bXCanf2hVGlCXCLhFQdgnVe4BhI2s0Ap1scDVXj1fwtYMPGwARLMoNgwykm4Q7T8FxxQ 7g3uMPmwquSqJoyP9nu1dFmU8i3ntwyoKuWwi8CEfMZS+arqL/B331jf3tlsswftIIRT RuLlW7nxeV2aR3lXV6M91NEKnym3tbr+d5x4VsW0X+BQnMdOLnPl5ij4qeB2rmTSKF1E RhDw== 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 123si33222926pfx.109.2019.01.28.04.16.26; Mon, 28 Jan 2019 04:16:43 -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 S1726765AbfA1MQX (ORCPT + 99 others); Mon, 28 Jan 2019 07:16:23 -0500 Received: from mx1.redhat.com ([209.132.183.28]:60986 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726669AbfA1MQX (ORCPT ); Mon, 28 Jan 2019 07:16:23 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9AC81C002965; Mon, 28 Jan 2019 12:16:22 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-107.ams2.redhat.com [10.36.117.107]) by smtp.corp.redhat.com (Postfix) with ESMTP id E60895D969; Mon, 28 Jan 2019 12:16:10 +0000 (UTC) From: David Hildenbrand To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, David Hildenbrand , Andrew Morton , Mel Gorman , "Kirill A. Shutemov" , Michal Hocko , Naoya Horiguchi , Jan Kara , Andrea Arcangeli , Dominik Brodowski , Matthew Wilcox , Vratislav Bendel , Rafael Aquini Subject: [PATCH RFC] mm: migrate: don't rely on PageMovable() of newpage after unlocking it Date: Mon, 28 Jan 2019 13:16:09 +0100 Message-Id: <20190128121609.9528-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 28 Jan 2019 12:16:23 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org While debugging some crashes related to virtio-balloon deflation that happened under the old balloon migration code, I stumbled over a possible race I think. What we experienced: drivers/virtio/virtio_balloon.c:release_pages_balloon(): - WARNING: CPU: 13 PID: 6586 at lib/list_debug.c:59 __list_del_entry+0xa1/0xd0 - list_del corruption. prev->next should be ffffe253961090a0, but was dead000000000100 Turns out after having added the page to a local list when dequeuing, the page would suddenly be moved to an LRU list before we would free it via the local list, corrupting both lists. So a page we own and that is !LRU was moved to an LRU list. For us, this was triggered by backporting 195a8c43e93d8 ("virtio-balloon: deflate via a page list") onto old balloon compaction code, but I think this only made the BUG become visible and the race still exists in new !LRU migraton code. My theory: In __unmap_and_move(), we lock the old and newpage and perform the migration. In case of vitio-balloon, the new page will become movable, the old page will no longer be movable. However, after unlocking newpage, I think there is nothing stopping the newpage from getting dequeued and freed by virtio-balloon. This will result in the newpage 1. No longer having PageMovable() 2. Getting moved to the local list before finally freeing it (using page->lru) Back in the migration thread in __unmap_and_move(), we would after unlocking the newpage suddenly no longer have PageMovable(newpage) and will therefore call putback_lru_page(newpage), modifying page->lru although that list is still in use by virtio-balloon. To summarize, we have a race between migrating the newpage and checking for PageMovable(newpage). Instead of checking __PageMovable(newpage), we can simply rely on is_lru. Because if the old page was not an LRU page, the new page also shouldn't be. If there is a problem with that, we could check for __PageMovable(newpage) before unlocking the new page. As I am not yet sure if this race actually exists, especially also upstream, I am sending this as RFC. Cc: Andrew Morton Cc: Mel Gorman Cc: "Kirill A. Shutemov" Cc: Michal Hocko Cc: Naoya Horiguchi Cc: Jan Kara Cc: Andrea Arcangeli Cc: Dominik Brodowski Cc: Matthew Wilcox Cc: Vratislav Bendel Cc: Rafael Aquini Reported-by: Vratislav Bendel Signed-off-by: David Hildenbrand --- mm/migrate.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 4512afab46ac..31e002270b05 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1135,10 +1135,12 @@ static int __unmap_and_move(struct page *page, struct page *newpage, * If migration is successful, decrease refcount of the newpage * which will not free the page because new page owner increased * refcounter. As well, if it is LRU page, add the page to LRU - * list in here. + * list in here. Don't rely on PageMovable(newpage), as that could + * already have changed after unlocking newpage (e.g. + * virtio-balloon deflation). */ if (rc == MIGRATEPAGE_SUCCESS) { - if (unlikely(__PageMovable(newpage))) + if (unlikely(!is_lru)) put_page(newpage); else putback_lru_page(newpage); -- 2.17.2