From patchwork Fri Nov 15 19:02:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Gushchin X-Patchwork-Id: 13876707 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 590E9D68BCF for ; Fri, 15 Nov 2024 19:02:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9F6766B009F; Fri, 15 Nov 2024 14:02:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A5C16B00A1; Fri, 15 Nov 2024 14:02:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 86DC86B00A3; Fri, 15 Nov 2024 14:02:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6942F6B009F for ; Fri, 15 Nov 2024 14:02:50 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id ECE1A1A1BF9 for ; Fri, 15 Nov 2024 19:02:49 +0000 (UTC) X-FDA: 82789249872.23.551DEF5 Received: from out-170.mta1.migadu.com (out-170.mta1.migadu.com [95.215.58.170]) by imf26.hostedemail.com (Postfix) with ESMTP id 7A30914002D for ; Fri, 15 Nov 2024 19:02:13 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Rf8SisqS; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf26.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.170 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731697188; a=rsa-sha256; cv=none; b=IKzlwTm6p6tpDmgEePzAkI58CRvecYwtSQKNDKqd510fcmKEzEZgS0NVCcLX9RuSnychyM dKTeyEQw21LpNvd4PS7Kpu1qrLHv7+iAtww7RAx7Y1hDwoxfBN1oTAs0+XS7HaFkq76a9b MVkwiOd0xOBG1RGXM8dBdoq0HB/lTCk= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Rf8SisqS; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf26.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.170 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731697188; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=VzlgiHC0yz0XkmBlZ14ltqdVdQSzRi2uwbEBaytUOF4=; b=llhsJHYIJZ2I41rrKh1thMa+bh/e8BUDwpdqZ7lody2TEaOdvO/Tqq51aMD+1RV+C5Bu4F NRCUrt1c9ZoKuNfs0UMzwC5uHhikJHlEVOJPU9kelnB/feML/VW09LMTASIxNxB7ODDeSY OyppqXkuriMLjh5ZFfn2gpvfSLB8lUI= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1731697365; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=VzlgiHC0yz0XkmBlZ14ltqdVdQSzRi2uwbEBaytUOF4=; b=Rf8SisqSTA/YQBQdIRdZLiQVOG/HVTGj0EDAPzd0PZMPWCxQvcQnN3lG9nrHSBGhTFQSg9 dWMhNjkT3upesKaai2op5x8//92pP50cZkYFWMq2QnDTzR2LvyL+S6Ez9OXJFf75VIofA7 N7u44jPUek/fY+RtLeWqrM6HK7DeFsI= From: Roman Gushchin To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Shakeel Butt , Michal Hocko , Johannes Weiner , Roman Gushchin Subject: [PATCH 1/2] mm: swap_cgroup: allocate swap_cgroup map using vcalloc() Date: Fri, 15 Nov 2024 19:02:28 +0000 Message-ID: <20241115190229.676440-1-roman.gushchin@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: pwse4kku9ebenqygstejg4xkmgniorak X-Rspamd-Queue-Id: 7A30914002D X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1731697333-529456 X-HE-Meta: U2FsdGVkX18757PUnCuoObkPKRH5ALYgI3DW/xAkQh1s+53DxpXcP4wzfiaYe0ihjIf7VKIdvojsDF7jQFElmtRffG6UZJM006qJpnGJjtbmkW5b/2QLWPcDeSIK6/JTWqrt/yeltvKDPDNdzrDbr0Wo3q8rsq2D6jUZHlQtBDIry4xxEB4FEovwK6iXc/Gbtrc9RJgUu4rEjIzMDhWaQKGVfWt7yagx63v79gXkYIz6Tbvg1EQulLnKR8KjsO9FJ/kZUqGjUpZkExr2MScZdw4/2um6oRQKd3RE02gLqcu7b5eh0IK8vGWalFo6PT/ccnaZuaj4Ae1Aht0cJSkxY1FnL0eBTaPuVWPG96qBVfT4bvUIz1eBAuLM4OI/EH6hfvCq9c2iD/BbZfuRurBC9+2XBnu5dCIcmBjYgevcAAGFwEhR4iPOPV0wV+XDdyBzu1WcEGS9et9fHXS7Q/xPsedrbZ4CdYbyBU4kjAib9jxqxZkOT7sAR64RF1+MNY7L4lWeHiY+KbAWbFsKwS8wPKHOP4dvFZA74gKfIxwOz97gOO7xe1ahUVc8iY3mearrHcungJZHmo8xXRAOg2sves3WfqdHulfknubWq8mRQRpSxpHtZ6TxNBNHQ+HFThqCEMphR/dln/qtlFQEDENA2e097VyCyKr/xPUsLrg7aqgH5mbRahThVYN/FuN/VTQEtXZYdNiWi50+tRPxT5XRGz8dKBS9V+Y6yhfPwFDKcmHvTDSXL/yLwZoQbRuZsQmhIXe3juHXaGJSd5aLxy93XOUQWdW8Nffvwajml53vUvxiZUI1WELNFNy3a5JMos37YCaGDtDtycSFfyZz9ETe8NOCsVTJVFPZyLMKD8tBwFCz3/g8fopZgqufsBnbQV8tGG/rZ9Gr5jB8LUPPFLwUf7p2QTUcBwVxMXkkeuFjPSGBTpYWQxUD27bGlxmiyAZ0fJCsIvBrhB+ErgrCcQE i1h8aypa BD8MAPSO4xcNtylJT/N91LXnzAXUiy/Ijfkx6KRCskU7Hh2VOpGM/2f0lIo/qByJ0bvU4uMP/pruTr82HgpaVjFR7MyvuAvjk8Rtm0QHE8gGirmUF2k6tbwvK9XmxR+WJxrXh6h7Y033WFZOmqeqyYYqtQHAEgpMemCRlNmtHx5Lw6KU55YmEJkGlgw== 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: Currently swap_cgroup's map is constructed as a vmalloc()'s-based array of pointers to individual struct pages. This brings an unnecessary complexity into the code. This patch turns the swap_cgroup's map into a single space allocated by vcalloc(). Signed-off-by: Roman Gushchin Acked-by: Shakeel Butt --- mm/swap_cgroup.c | 83 ++++++++---------------------------------------- 1 file changed, 13 insertions(+), 70 deletions(-) diff --git a/mm/swap_cgroup.c b/mm/swap_cgroup.c index da1278f0563b..18de498c84a4 100644 --- a/mm/swap_cgroup.c +++ b/mm/swap_cgroup.c @@ -6,17 +6,18 @@ #include /* depends on mm.h include */ static DEFINE_MUTEX(swap_cgroup_mutex); + +struct swap_cgroup { + unsigned short id; +}; + struct swap_cgroup_ctrl { - struct page **map; - unsigned long length; + struct swap_cgroup *map; spinlock_t lock; }; static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES]; -struct swap_cgroup { - unsigned short id; -}; #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup)) /* @@ -33,44 +34,10 @@ struct swap_cgroup { * TODO: we can push these buffers out to HIGHMEM. */ -/* - * allocate buffer for swap_cgroup. - */ -static int swap_cgroup_prepare(int type) -{ - struct page *page; - struct swap_cgroup_ctrl *ctrl; - unsigned long idx, max; - - ctrl = &swap_cgroup_ctrl[type]; - - for (idx = 0; idx < ctrl->length; idx++) { - page = alloc_page(GFP_KERNEL | __GFP_ZERO); - if (!page) - goto not_enough_page; - ctrl->map[idx] = page; - - if (!(idx % SWAP_CLUSTER_MAX)) - cond_resched(); - } - return 0; -not_enough_page: - max = idx; - for (idx = 0; idx < max; idx++) - __free_page(ctrl->map[idx]); - - return -ENOMEM; -} - static struct swap_cgroup *__lookup_swap_cgroup(struct swap_cgroup_ctrl *ctrl, pgoff_t offset) { - struct page *mappage; - struct swap_cgroup *sc; - - mappage = ctrl->map[offset / SC_PER_PAGE]; - sc = page_address(mappage); - return sc + offset % SC_PER_PAGE; + return &ctrl->map[offset]; } static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent, @@ -168,32 +135,20 @@ unsigned short lookup_swap_cgroup_id(swp_entry_t ent) int swap_cgroup_swapon(int type, unsigned long max_pages) { - void *array; - unsigned long length; + struct swap_cgroup *map; struct swap_cgroup_ctrl *ctrl; if (mem_cgroup_disabled()) return 0; - length = DIV_ROUND_UP(max_pages, SC_PER_PAGE); - - array = vcalloc(length, sizeof(void *)); - if (!array) + map = vcalloc(max_pages, sizeof(struct swap_cgroup)); + if (!map) goto nomem; ctrl = &swap_cgroup_ctrl[type]; mutex_lock(&swap_cgroup_mutex); - ctrl->length = length; - ctrl->map = array; + ctrl->map = map; spin_lock_init(&ctrl->lock); - if (swap_cgroup_prepare(type)) { - /* memory shortage */ - ctrl->map = NULL; - ctrl->length = 0; - mutex_unlock(&swap_cgroup_mutex); - vfree(array); - goto nomem; - } mutex_unlock(&swap_cgroup_mutex); return 0; @@ -205,8 +160,7 @@ int swap_cgroup_swapon(int type, unsigned long max_pages) void swap_cgroup_swapoff(int type) { - struct page **map; - unsigned long i, length; + struct swap_cgroup *map; struct swap_cgroup_ctrl *ctrl; if (mem_cgroup_disabled()) @@ -215,19 +169,8 @@ void swap_cgroup_swapoff(int type) mutex_lock(&swap_cgroup_mutex); ctrl = &swap_cgroup_ctrl[type]; map = ctrl->map; - length = ctrl->length; ctrl->map = NULL; - ctrl->length = 0; mutex_unlock(&swap_cgroup_mutex); - if (map) { - for (i = 0; i < length; i++) { - struct page *page = map[i]; - if (page) - __free_page(page); - if (!(i % SWAP_CLUSTER_MAX)) - cond_resched(); - } - vfree(map); - } + kvfree(map); }