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); } From patchwork Fri Nov 15 19:02:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Gushchin X-Patchwork-Id: 13876708 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 7503BD68BCD for ; Fri, 15 Nov 2024 19:02:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1BB136B00A1; Fri, 15 Nov 2024 14:02:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 169D26B00A3; Fri, 15 Nov 2024 14:02:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 056B56B00A5; Fri, 15 Nov 2024 14:02:50 -0500 (EST) 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 DF3476B00A1 for ; Fri, 15 Nov 2024 14:02:50 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 90CAB41C0B for ; Fri, 15 Nov 2024 19:02:50 +0000 (UTC) X-FDA: 82789248108.17.208F7CF Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) by imf06.hostedemail.com (Postfix) with ESMTP id 747C418001A for ; Fri, 15 Nov 2024 19:02:16 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=PS2jU747; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf06.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.180 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731697304; a=rsa-sha256; cv=none; b=xhX0SDApv8vGwvl6y8r3Jn6H2DgRIe/bmUdEA9ye5jWuu0asez9sN1fLHlexKs1Karw2s6 4M9q/cmvXuEQmQTTSxG2mN9P7+gGlSw44U7/vCQUU5XFVgqeHOyTHGL/7oLG2pbP47EBMA kJdb4tKEL6nLSgWUwZk22j9A/tkD9W8= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=PS2jU747; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf06.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.180 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=1731697304; 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:in-reply-to:references:references:dkim-signature; bh=cChRt8Y/7rNJ+UE7EiKYAmqcqG+mD3DTpu3d12HPnTc=; b=3vm2JlhDi5J7xa4soq/9Q5tt+TGb7Ak8fKkgab5gyPmVyTYXDJ0e2PjjQt9oiZ5/oOF52N JO7JFPpuM4S8VlOqVRSJzjVJKYpArObitb9U/iZrdp9uq0r3In/eX5es/lFT4/oKAv+shW wmBCLg0jtFl9UU73kJTPTsue8AI2fGY= 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=1731697367; 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: in-reply-to:in-reply-to:references:references; bh=cChRt8Y/7rNJ+UE7EiKYAmqcqG+mD3DTpu3d12HPnTc=; b=PS2jU747h5/TqBqugYikH8pMT78fDosPtIf0M28FYdx2T7/Gr6zIEe3lht9AD5mbdmazLO S/zp1C2BigugXatQzPfouGIr828SdBJx3dPkpETc6WcAlS9TlVhmpjTy4kiwR9wm69Woz+ rtBwb/L6PPRRVYlYQGVLrRotXjfdrbw= 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 2/2] mm: swap_cgroup: get rid of __lookup_swap_cgroup() Date: Fri, 15 Nov 2024 19:02:29 +0000 Message-ID: <20241115190229.676440-2-roman.gushchin@linux.dev> In-Reply-To: <20241115190229.676440-1-roman.gushchin@linux.dev> References: <20241115190229.676440-1-roman.gushchin@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Queue-Id: 747C418001A X-Rspamd-Server: rspam11 X-Stat-Signature: dwg79ykhnzrx3oa63c317wbdfdympkrr X-HE-Tag: 1731697336-960174 X-HE-Meta: U2FsdGVkX1+UVhQgqrHSY4PIy5Itn2E4RszFHY08RaIspWgC4pYF0I/72c44y1un4FxCsmVpT6GkIJupjo7ONr4wKIl6/6cr4GbEM92e23aE+p1PiAq3330YSx/Rn8R5mGF/7ZwO0QUX9Hl+tf0+YrxUkgvBYAxImvkFmtN7x3VjJYrpzmoK400skwUOrOZktXttLzI1Ohc78W7fudpYgN9uf2TlK8le9ywJn9/M4fkCTGSPXLDKLFcI4GjuNHNygfSCA9S88nKSzq1k8ETSPFX/UKDnRQkSVlrv6BR0djSkmvr3CaewZ37Ae/ug9J7U4k91LBx+vSZc1mdADCrqpaBSsODu3OuXDCzD693xSU7J4TYDa5xopfCLVrlcb7e85RHgHux5Hu9xSHDPRSLRXqbd+7ziZW+kGcArodkAOAeUy2MPHP4gPL4qcQik4BDkfyH6ShKgT3wMDpRIlfYRDo4KOi64EW/oM0ULIN7CGzFJ52+vgtLOGCixDEHg85S+j/4i09YCrwLf628sVKPmanQpMPpVn9OQgnb8OM5YOmkgUFV7BARRi4RRe7LRU7mwcyaFQwDqkc3JN/Ze42i9dqeRyVREvAJSTendEAH7/2pzV1rtqmHRTpKsQxKKuAv8LnR/ZzzoBOI+R8r2diwG6BwSeGaxhJDlvImp1lharzr9HCOo9RCT/0awslWg9zawumSbYy8TKnee12cXC9QCxyXu7UIOLbBvdChppNhEDxe0jlZdT+cVird1TAgFSQcwTjmW4/pxh+bY8wkkn4pShJeSInRw5ZctIn5JDoTreA4WtRoDJKk4ioyLRPEZZsPaDQTW45Yfb8OYbwdw/liyFhX8rz+WBZDgJbjBOISyQjFjTlXFXBJ3EcYfHy9NGjs6PVBOm3X8XNeRgV/+YkhTlLZtqS3+p7modsjJTHsSwTPaaS4ymMmsSwkW9JP58owm69BgI9zjW0cwWCBcWsY VrMfJzaL Lm/uxDKpBNKwFeJOcwU/UlobfxQcejAUDnzWSA08Rd78l+Jdhjp/8VLGTDYCI1nhzzkLgugKHxyQU9NEgNrnQdSYnIwPjvSbolNzpP9vnJM6RS4pLRkrE3NKoUNR0t6Iy1GqgnRjgwdZMOfSuJL86rHTJkvghA1E4egbtXKYF9goAiFEiBZhkVvbkDA== 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: Because swap_cgroup map is now virtually contiguous, swap_cgroup_record() can be simplified, which eliminates a need to use __lookup_swap_cgroup(). Now as __lookup_swap_cgroup() is really trivial and is used only once, it can be inlined. Signed-off-by: Roman Gushchin Acked-by: Shakeel Butt --- mm/swap_cgroup.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/mm/swap_cgroup.c b/mm/swap_cgroup.c index 18de498c84a4..0db907308c94 100644 --- a/mm/swap_cgroup.c +++ b/mm/swap_cgroup.c @@ -33,13 +33,6 @@ static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES]; * * TODO: we can push these buffers out to HIGHMEM. */ - -static struct swap_cgroup *__lookup_swap_cgroup(struct swap_cgroup_ctrl *ctrl, - pgoff_t offset) -{ - return &ctrl->map[offset]; -} - static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent, struct swap_cgroup_ctrl **ctrlp) { @@ -49,7 +42,7 @@ static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent, ctrl = &swap_cgroup_ctrl[swp_type(ent)]; if (ctrlp) *ctrlp = ctrl; - return __lookup_swap_cgroup(ctrl, offset); + return &ctrl->map[offset]; } /** @@ -104,16 +97,9 @@ unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id, spin_lock_irqsave(&ctrl->lock, flags); old = sc->id; - for (;;) { + for (; offset < end; offset++, sc++) { VM_BUG_ON(sc->id != old); sc->id = id; - offset++; - if (offset == end) - break; - if (offset % SC_PER_PAGE) - sc++; - else - sc = __lookup_swap_cgroup(ctrl, offset); } spin_unlock_irqrestore(&ctrl->lock, flags);