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: 10658769 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 633CD109C for ; Mon, 29 Oct 2018 05:17:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 529D229525 for ; Mon, 29 Oct 2018 05:17:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3FB9C29553; Mon, 29 Oct 2018 05:17:01 +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,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1DBFA29525 for ; Mon, 29 Oct 2018 05:16:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DED5C6B035A; Mon, 29 Oct 2018 01:16:57 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D9B036B035B; Mon, 29 Oct 2018 01:16:57 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C3CDE6B035C; Mon, 29 Oct 2018 01:16:57 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by kanga.kvack.org (Postfix) with ESMTP id 7E33B6B035A for ; Mon, 29 Oct 2018 01:16:57 -0400 (EDT) Received: by mail-pf1-f200.google.com with SMTP id n9-v6so6602837pfg.12 for ; Sun, 28 Oct 2018 22:16:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:mime-version; bh=0KNNKw1/amMnFlX3bpSyjsiBE9Q1u0znYuBXSNQ4r/U=; b=t8otFD3VKYx2nWder6t/RGkrD8E5F5MvZgFNoFKEAu2XP0SqaqvonSv3/ZqYDJYge3 b6mQXT+g8mbXAKSdvNEW+muDRgflNy96R6HAA/V3+sMUMrbHsripLDiT6D9d88yuKCrV eQIQyFZGjyScjU5euMaa9yc5orUsfqPJF2rgMZDBfkIJU/SGA7LHnreF+iTeSMkdRAzT 6+wdBcfA1P96duDORzRjZRZ+EK9an+ifmajRBlGlAr+/uesJNoatdgpirD0Zr9RA1qph z5vV2Uh/NUetc+ooj5ZMvz4B1BFDSckAe6GuQSCFxaRiu8cVhN5i6Qy+1jv4lRtVqORj 5vqQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of miles.chen@mediatek.com designates 210.61.82.184 as permitted sender) smtp.mailfrom=miles.chen@mediatek.com X-Gm-Message-State: AGRZ1gIr9asJ3bffbLzCwkzWhDCLkQ5Jwuc1ohEgB2iTyFIGo2x4tt6X rGmYrdjlj9Dqi0lnW1sflUIpolQ90fwTmj8R4qzrZ9AmO7GvkuPWKrV0rcZ86xvmBTsELVOn3NY cRkeWYZBIfSLr4Ki1NxXancNF9jWpXj37dDH37y8De3vhJ5Fp+t3xw7bv2xypByA5bA== X-Received: by 2002:a63:801:: with SMTP id 1mr12241878pgi.275.1540790217190; Sun, 28 Oct 2018 22:16:57 -0700 (PDT) X-Google-Smtp-Source: AJdET5fIv72biywilca/9jKIk6s+Qb97HapI/PfsCi3EuJl0/NsdFzJdMTAc60LWlHMZe5Q87QW3 X-Received: by 2002:a63:801:: with SMTP id 1mr12241845pgi.275.1540790216347; Sun, 28 Oct 2018 22:16:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540790216; cv=none; d=google.com; s=arc-20160816; b=W+TTtBR5Kz5Wh5R1Vu6VoQ2Et5vZj4cmXFPvyQsqQmsr0AtHptfY6NY80l0w+vZ5Vc hh9kbbX19wP+qQVu6ld1NlwElCfgBwvEYwv8mGILSUunJRJLfABbGGRDb7PPuP8z0fmB wc04A1bV/ikB5FtHxXkEkrhn1xiezQ7VztoQEj5i6skmsf8W2Pqbhlj8mXidtsKcpueN 3RVB1wkErds+yExlmi1eMEgbDRWR1KHjKmBbbhYBAsn6FiALApbaFLDwq4fwqB2By0cY LLKKB8QC6k/2qFJkQVcIBULOeXaXxVeT6xEqVJ7lIMHYIfF6sTOa8FpTUh2lur1oTIZw NXOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:message-id:date:subject:cc:to:from; bh=0KNNKw1/amMnFlX3bpSyjsiBE9Q1u0znYuBXSNQ4r/U=; b=wmFgBalhBb4kNBh5MZcrxHMTPRKhghTvAhC9Tg7bz2hvYfrrWC3ftyZMj+qdi6oh7a /z8qdpbgMON+wL79J4Pi/yxtiIbCGqDKbc1vsded7a7i8uO+O2pWPgoAtw9XPFH2I8dq FYIFkBMHgUkTg8BVzdpHU/qennH9Ei0I1YDIy2aSFiBZ5d+EnoWv2IiI8Lfy4oB91J6N 9hDGhqMQJGfn0T6pFmQprIlV4MH0ppUJCj0ZUppWVaqeC2zNjaNwtRtBaYUIGOp1QEJQ fhW4dcNPyYgBJiyLBZRf+7IPoj1xroPLlMqZB29sJkDova2syKZLUHvOJXDFy/ZpS9GF v2xA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of miles.chen@mediatek.com designates 210.61.82.184 as permitted sender) smtp.mailfrom=miles.chen@mediatek.com Received: from mailgw02.mediatek.com ([210.61.82.184]) by mx.google.com with ESMTPS id u10-v6si19932553pgg.180.2018.10.28.22.16.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Oct 2018 22:16:56 -0700 (PDT) Received-SPF: pass (google.com: domain of miles.chen@mediatek.com designates 210.61.82.184 as permitted sender) client-ip=210.61.82.184; Authentication-Results: mx.google.com; spf=pass (google.com: domain of miles.chen@mediatek.com designates 210.61.82.184 as permitted sender) smtp.mailfrom=miles.chen@mediatek.com 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 CC: , , , , , Miles Chen , "Michal Hocko" 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-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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,