Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9108643pxu; Mon, 28 Dec 2020 06:52:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJxf54pZqNdeBcAh758Gaz6QQdUoSbQI90HIIkJb/qiCFmC5Xwd/nOBf9JFelqArCSgXClk5 X-Received: by 2002:a17:906:2f8b:: with SMTP id w11mr35778151eji.246.1609167124985; Mon, 28 Dec 2020 06:52:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609167124; cv=none; d=google.com; s=arc-20160816; b=fV/gzcRt8bpbwOiaQDCusPcgQsi+knQNKIscGT3x+gbct64qLGN88EgM0b7GoTr8sU GeVcGy/w4EzGiMNtavLCee9iNeQ1ZlM1jR8i1U7WiuBX41f9M/ZF1J3i5Errhuj21qh5 JK/z33bGLZf6afYOu4mxjbCRcSJNC6GwWy9oBD2HwwwA5aqvDaFiOiYu+Uz0mul5tu5j 3l9ioaSBTx/WXK0TViy7NG8PD4Apk2j89CM2Q8QVcSZFFCFS33D2dB8xzOQrbP+hNZdD KHWB8bsMLN761kz76q7VxDTGN8OCY9GiD5/zH0lu5XcRU3FGF5TwJWi5jpAlJPB9xQv+ 9GCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=71LJaPumKSh73//x2chsukNaCnr17OI031vJ5lvbSq4=; b=RtjbAWhUe24WgM9yhiMzONDqg1xjW4LhOcmoXrQoOwjJSDZ6aZN8m4qzzjNVvjwcWE VHTVK6k8EBSNfiGm4K9tdDp13dZ/17cv+P1gDBNgxHVFVEWcEQrNhDZrUbE2Yc9YjDoZ wX/hzO3ioAAhqhXKkxMRcBBgS0H56joYD5c+l8qwzBs8rzdJtqW9T4YYY0bu/f4cAQLJ 131sKzalZZtIKt6QE3UPngn3tfZCesfHuQxHmDQ8cDh8Aizx3z9z+fi3ouht0gyXKf0R nv26dZO4B7SP8IoRbpd/C/IFxjntX36NgiTaUvKrifxkQ73/m0wHOlFGkGuhI9kI+ElS 4frw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rWA5kzBc; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 11si18234427ejw.721.2020.12.28.06.51.42; Mon, 28 Dec 2020 06:52:04 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=rWA5kzBc; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2437979AbgL1OVi (ORCPT + 99 others); Mon, 28 Dec 2020 09:21:38 -0500 Received: from mail.kernel.org ([198.145.29.99]:56472 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2502245AbgL1OVF (ORCPT ); Mon, 28 Dec 2020 09:21:05 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 90F5C2063A; Mon, 28 Dec 2020 14:20:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609165219; bh=qtsEKy5ZXF8em1TQ1ZvSRUrYNAtf84KIJgezj7ANK8U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rWA5kzBcF3K6ds+KLHAAQnlkC10Vv83WQ9SObe7TpK37617alMmqBty/QshgcwTXK tYbGHvsvQ49XhDLKrdjk2M0j6hwSwXk1F6lYNmhR5PcAItaiwn2stupSwZSuvDuei6 EBukBZ4Z37qhJjAQ3pQ2bYXLKGdgcsLRRxa43E08= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Oscar Salvador , Vlastimil Babka , Naoya Horiguchi , Dan Williams , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.10 455/717] mm,memory_failure: always pin the page in madvise_inject_error Date: Mon, 28 Dec 2020 13:47:33 +0100 Message-Id: <20201228125042.767776904@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Oscar Salvador [ Upstream commit 1e8aaedb182d6ddffc894b832e4962629907b3e0 ] madvise_inject_error() uses get_user_pages_fast to translate the address we specified to a page. After [1], we drop the extra reference count for memory_failure() path. That commit says that memory_failure wanted to keep the pin in order to take the page out of circulation. The truth is that we need to keep the page pinned, otherwise the page might be re-used after the put_page() and we can end up messing with someone else's memory. E.g: CPU0 process X CPU1 madvise_inject_error get_user_pages put_page page gets reclaimed process Y allocates the page memory_failure // We mess with process Y memory madvise() is meant to operate on a self address space, so messing with pages that do not belong to us seems the wrong thing to do. To avoid that, let us keep the page pinned for memory_failure as well. Pages for DAX mappings will release this extra refcount in memory_failure_dev_pagemap. [1] ("23e7b5c2e271: mm, madvise_inject_error: Let memory_failure() optionally take a page reference") Link: https://lkml.kernel.org/r/20201207094818.8518-1-osalvador@suse.de Fixes: 23e7b5c2e271 ("mm, madvise_inject_error: Let memory_failure() optionally take a page reference") Signed-off-by: Oscar Salvador Suggested-by: Vlastimil Babka Acked-by: Naoya Horiguchi Cc: Vlastimil Babka Cc: Dan Williams Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/madvise.c | 9 +-------- mm/memory-failure.c | 6 ++++++ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 13f5677b93222..9abf4c5f2bce2 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -908,14 +908,7 @@ static int madvise_inject_error(int behavior, } else { pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n", pfn, start); - /* - * Drop the page reference taken by get_user_pages_fast(). In - * the absence of MF_COUNT_INCREASED the memory_failure() - * routine is responsible for pinning the page to prevent it - * from being released back to the page allocator. - */ - put_page(page); - ret = memory_failure(pfn, 0); + ret = memory_failure(pfn, MF_COUNT_INCREASED); } if (ret) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 71295bb984af6..fd653c9953cfd 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1231,6 +1231,12 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags, loff_t start; dax_entry_t cookie; + if (flags & MF_COUNT_INCREASED) + /* + * Drop the extra refcount in case we come from madvise(). + */ + put_page(page); + /* * Prevent the inode from being freed while we are interrogating * the address_space, typically this would be handled by -- 2.27.0