From patchwork Mon Oct 23 15:42:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: wang wei X-Patchwork-Id: 13433049 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E80E5C001E0 for ; Mon, 23 Oct 2023 15:42:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 600E06B00D7; Mon, 23 Oct 2023 11:42:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 58A2A6B00D8; Mon, 23 Oct 2023 11:42:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 451246B00D9; Mon, 23 Oct 2023 11:42:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 2FD376B00D7 for ; Mon, 23 Oct 2023 11:42:34 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id BEBF2140268 for ; Mon, 23 Oct 2023 15:42:33 +0000 (UTC) X-FDA: 81377143386.02.03C94DB Received: from m15.mail.163.com (m15.mail.163.com [45.254.50.219]) by imf08.hostedemail.com (Postfix) with ESMTP id 0F77816001F for ; Mon, 23 Oct 2023 15:42:28 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=163.com header.s=s110527 header.b=dLPinXuP; spf=pass (imf08.hostedemail.com: domain of a929244872@163.com designates 45.254.50.219 as permitted sender) smtp.mailfrom=a929244872@163.com; dmarc=pass (policy=none) header.from=163.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698075751; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=zYiXtfVmofgN8fsdrL5yrW9lXdRUcGbVHp2UDjk1nZ8=; b=pEyJsw6nqJyEPZfK26+k611boqiRqWD3YE7lMZns96vQEBBECWtMtgNwxmWyR3vAkNJV46 n11pounhjv2pINRCG2MlLYRit85v1l6/zH8ZHdcjSbV69MJ3+A+opGLV+I3bvtF4ZZbz7q DNTpD86Gnw+R4FkgQo1U+XQHFosvF9c= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=163.com header.s=s110527 header.b=dLPinXuP; spf=pass (imf08.hostedemail.com: domain of a929244872@163.com designates 45.254.50.219 as permitted sender) smtp.mailfrom=a929244872@163.com; dmarc=pass (policy=none) header.from=163.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698075751; a=rsa-sha256; cv=none; b=eaYoWIqf2OHYLa/CkVn+B4drg1Ea8BxPDG0rrR/d+c+1gBTYBoFJmWe3zTcwOffbevEvIo IWAFz+UDkLcaxy9Ab/OkmmxjW1ocL2RXU+X9dFholPcCai+EVhFF6koBH628bwnQpBId89 Q3KOE2RzjfpRRwwbIsbPj1NJBAxD4Bs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version: Content-Type; bh=zYiXtfVmofgN8fsdrL5yrW9lXdRUcGbVHp2UDjk1nZ8=; b=dLPinXuPgEe6AI8aBTctHDjuVzDHlIkw7Y7t3KW7888gS6fTH0di3jo719Ucdk 1Osv+Qd4Cc8FnLInprfnbTyRfAPnoi276PHj5ko+sfijV3lvfQEwsfLyMvl2dooc yWrJtmy7lUW4I0nSNGU9m2uNmcZYTAaYkkYby+8XW7Rf4= Received: from localhost.localdomain (unknown [124.90.109.239]) by zwqz-smtp-mta-g2-1 (Coremail) with SMTP id _____wB3n_NZlDZlHoE8BQ--.20243S2; Mon, 23 Oct 2023 23:42:17 +0800 (CST) From: wang wei To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: wang wei Subject: [PATCH] mm/page_alloc: fix the potential memory waste in page_frag_alloc_align Date: Mon, 23 Oct 2023 23:42:16 +0800 Message-Id: <20231023154216.376522-1-a929244872@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CM-TRANSID: _____wB3n_NZlDZlHoE8BQ--.20243S2 X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvj4R2Q6XDUUUU X-Originating-IP: [124.90.109.239] X-CM-SenderInfo: jdzsmjiuuylji6rwjhhfrp/xtbBFhoSplgi7YMtdAAAs1 X-Rspamd-Queue-Id: 0F77816001F X-Rspam-User: X-Stat-Signature: gw18ru9ye3oypbd4z8qmmwskgjk113cc X-Rspamd-Server: rspam01 X-HE-Tag: 1698075748-769017 X-HE-Meta: U2FsdGVkX18Xwsdrp3IBPydku3jaAk9N/joD5M+hfqYiGMPYFAzk+hM+H5tibFJvQgv5c1Afgi48CEk6QjZi+TcYe38JKnul6dGmvOLGLKfZsPuTKbqbCuHN7yFxrs7w+0bRCP+hAOgw5bio1gvfrfTSEgM6pEQGR2131bzmVzcKvV/2kkCsgXhJhgwW83ioR43ZL35DH98yq/Hz/TZM46oJQOhugFcE/7c5HQVXHN0hAYxgCPNeSDL7tn0PjCIvww7TbID0Wu+8oM/w7JY3zsgS8b4yIXauyEIi2tT9TdNtnXYvZjpdtDuoF9cHO8KiR8hSQQI9KEYDLasL1J2rAQLf5eDwoFeyOOtD415Q1c7NTh/JOoO2ESlGK9ocTiWHxEnBh9f7KvB6/36f0SsUjTObiGr2ixJD+dqgdinPtmL3eQX3Hwx95gxXVZNMcD2zmbpK9vyblpc6w3kH2vGHSMLLuUxffU66/3kiar6C293c6DZROd5s7REd5rCGVim9kBlmFkvhzGaQC6mjOPbW6SyXN86rBHvnoxeYNIM/AUCUn9q2xu0LWLmPxzZy4efHlAhCW93x2jh+prFthY/0StwLMiMaFYFNmHOW48zKjtgXETfKj8rKw8rSlLnpfDBzREcan1mQmw5rswJlaVSlzLSq28fUxsguoOzELZhmS/0k+RNRdoHe9oT18+mcn3F6va8VzRiyuUqnMvZPSSlPiHokEmApeiZBDTdUokO2eWgWrDJye5oxVQW+eUgqCLeKvF4wsdkGKE6KCeSatI8yqLNDPr9LvNJdNOWBWHDrgeL7fuDNEizCcPYjpPIJVddEywPJ6Moe/8jm9NjY/FaW2SqrCYIRuGoNndBUEyemd1t0+oSun8QFRDrpMfpbiONy/opFsa0ilNzSA1NyigTf5owPA4P5tFmD0gWKElg/fGG2Ew40yMQAe0FM8p8wPKJ0XChgwvr8qvzkJmEAqSi tLtw55p/ f7wFm950y/WbXE0C6IuXCQ4aBbcKDFupbCcO/JKXduZnlKWaZqD9sa2GKmLtQMEUQxPEjWuMM7AqjRXoF9Sq/6TM7m33hYLLPpkEsn9+21mQ61+e97H1H8+tJflfTFmHNvz1NpL1r0ezeiSLpd3Fax14OG34kp+7auT7wSBqZWxD5oRIMaKw/dZ9XE6s3Ls/YyTWwJNowznIKJKR0aLzcf2eWD60b6cA8Luu9DVYu6F4VfU3OQ7wRSlmBM0GtYfYa5WEt+fCGT/hgb6UdXQIvuJvLYxborOBGdWvx8HZ0FrRM3dqgtbX74P/0fh0thXK/+a4Ii13utFkTky0ee2bidQeCnRm1sC0MeGQhCW0mUByEsx/98PXJ5g1zuyZnmID8q5p9XnUHSXGDhnQOAgqcNSQhkWXqsK5M0w6gHkLO8Eh0blXaYqkRj8KU3ICAYjH5Cv5EAI5RPqfUEdc= 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: List-Subscribe: List-Unsubscribe: First step, allocating a memory fragment with size 1KB bytes uses page_frag_alloc_align. It will allocate PAGE_FRAG_CACHE_MAX_SIZE bytes by __page_frag_cache_refill, store the pointer at nc->va and then return the last 1KB memory fragment which address is nc->va + PAGE_FRAG_CACHE_MAX_SIZE - 1KB. The remaining PAGE_FRAG_CACHE_MAX_SIZE - 1KB bytes of memory can Meet future memory requests. Second step, if the caller requests a memory fragment with size more then PAGE_FRAG_CACHE_MAX_SIZE bytes, page_frag_alloc_align, it will also allocate PAGE_FRAG_CACHE_MAX_SIZE bytes by __page_frag_cache_refill, store the pointer at nc->va, and return NULL. this behavior makes the rest of PAGE_FRAG_CACHE_MAX_SIZE - 1KB bytes memory at First step are wasted(allocate from buddy system but not used). So we should check the size of memory requests. If it beyound PAGE_FRAG_CACHE_MAX_SIZE, we should return NULL directly. Signed-off-by: wang wei --- mm/page_alloc.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 8cf86d0c6aa8..3182c648e86e 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4757,6 +4757,9 @@ void *page_frag_alloc_align(struct page_frag_cache *nc, struct page *page; int offset; + if(unlikely(fragsz > PAGE_FRAG_CACHE_MAX_SIZE)) + return NULL; + if (unlikely(!nc->va)) { refill: page = __page_frag_cache_refill(nc, gfp_mask);