From patchwork Mon Oct 29 05:16:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miles Chen X-Patchwork-Id: 10658771 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F2DF6109C for ; Mon, 29 Oct 2018 05:22:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DE9FB293D4 for ; Mon, 29 Oct 2018 05:22:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CCA4C293F3; Mon, 29 Oct 2018 05:22:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5BB9E293D4 for ; Mon, 29 Oct 2018 05:22:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:To :From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=S3aVylfVmEeHyH/f59CMMY3tH3yrpIjgJR3r3Mo9BZ4=; b=O/ol7EqvOH8OFp xkbthR94Zk1wuyxEEa3iJahvNh47CJaamYswC85tN+8Kx4OItDfdku/Spol8wlHa9lqtdz9ztVk8t 8Sg/36cdpHiHDh8z8bE9LXyFy8/z4bY5DsiL2upZbnIp6zrC8tv7G27m58e1toS4KyOJvH7+Y3ttq LipGqrh1OgXx9tLaqcTWoenOvINZe+Qs+YTD9wAzeL79nXMEr6H0Z0nouIJqB6cQv0K9kH5kt0ay1 +ZQaHqcdI0XZo73T6qWIICJXO97EYYnV2AY0+ij1wLgSDGfdRiCa2dZSJdn4YbL+NRW11RLFdAkn2 YtzoaZ4rk8gNqRSl6kcQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gH00H-0000qC-2C; Mon, 29 Oct 2018 05:22:17 +0000 Received: from [210.61.82.184] (helo=mailgw02.mediatek.com) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gH00C-0000om-Bg; Mon, 29 Oct 2018 05:22:14 +0000 X-UUID: a9d98cb000d24868b7ff3670f881530e-20181029 X-UUID: a9d98cb000d24868b7ff3670f881530e-20181029 Received: from mtkcas06.mediatek.inc [(172.21.101.30)] by mailgw02.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 1181246590; Mon, 29 Oct 2018 13:16:42 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs03n2.mediatek.inc (172.21.101.182) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Mon, 29 Oct 2018 13:16:28 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1395.4 via Frontend Transport; Mon, 29 Oct 2018 13:16:28 +0800 From: To: Andrew Morton , Michal Hocko , Joe Perches , Matthew Wilcox Subject: [PATCH v3] mm/page_owner: use kvmalloc instead of kmalloc Date: Mon, 29 Oct 2018 13:16:16 +0800 Message-ID: <1540790176-32339-1-git-send-email-miles.chen@mediatek.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 X-TM-SNTS-SMTP: 86B9739B61A74A4CABEA7080C5AC30440014C9C39DD983E7247355CD1A4FED4F2000:8 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181028_222212_574983_649880E8 X-CRM114-Status: GOOD ( 12.79 ) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: wsd_upstream@mediatek.com, linux-kernel@vger.kernel.org, Michal Hocko , linux-mm@kvack.org, Miles Chen , linux-mediatek@lists.infradead.org, linux-arm-kernel@lists.infradead.org Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+patchwork-linux-mediatek=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Miles Chen The kbuf used by page owner is allocated by kmalloc(), which means it can use only normal memory and there might be a "out of memory" issue when we're out of normal memory. To solve this problem, use kvmalloc() to allocate kbuf from normal/highmem. But there is one problem here: kvmalloc() does not fallback to vmalloc for sub page allocations. So sub page allocation fails due to out of normal memory cannot fallback to vmalloc. Modify kvmalloc() to allow sub page allocations fallback to vmalloc when CONFIG_HIGHMEM=y and use kvmalloc() to allocate kbuf. Clamp buffer size to PAGE_SIZE to avoid arbitrary size allocation. Change since v2: - improve kvmalloc, allow sub page allocations fallback to vmalloc when CONFIG_HIGHMEM=y Change since v1: - use kvmalloc() - clamp buffer size to PAGE_SIZE Signed-off-by: Miles Chen Cc: Joe Perches Cc: Matthew Wilcox Cc: Michal Hocko --- mm/page_owner.c | 8 ++++---- mm/util.c | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index d80adfe702d3..a064cd046361 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -1,7 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include #include -#include #include #include #include @@ -351,7 +350,8 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn, .skip = 0 }; - kbuf = kmalloc(count, GFP_KERNEL); + count = count > PAGE_SIZE ? PAGE_SIZE : count; + kbuf = kvmalloc(count, GFP_KERNEL); if (!kbuf) return -ENOMEM; @@ -397,11 +397,11 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn, if (copy_to_user(buf, kbuf, ret)) ret = -EFAULT; - kfree(kbuf); + kvfree(kbuf); return ret; err: - kfree(kbuf); + kvfree(kbuf); return -ENOMEM; } diff --git a/mm/util.c b/mm/util.c index 8bf08b5b5760..7b1c59b9bfbf 100644 --- a/mm/util.c +++ b/mm/util.c @@ -416,10 +416,10 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node) ret = kmalloc_node(size, kmalloc_flags, node); /* - * It doesn't really make sense to fallback to vmalloc for sub page - * requests + * It only makes sense to fallback to vmalloc for sub page + * requests if we might be able to allocate highmem pages. */ - if (ret || size <= PAGE_SIZE) + if (ret || (!IS_ENABLED(CONFIG_HIGHMEM) && size <= PAGE_SIZE)) return ret; return __vmalloc_node_flags_caller(size, node, flags,