From mboxrd@z Thu Jan 1 00:00:00 1970 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 89C59C10F15 for ; Wed, 24 Apr 2024 08:27:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0542B6B0255; Wed, 24 Apr 2024 04:27:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 004DE6B0257; Wed, 24 Apr 2024 04:27:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E0FA96B0258; Wed, 24 Apr 2024 04:27:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C10CA6B0255 for ; Wed, 24 Apr 2024 04:27:47 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 402E5A106F for ; Wed, 24 Apr 2024 08:27:47 +0000 (UTC) X-FDA: 82043746974.26.0F6AC26 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf09.hostedemail.com (Postfix) with ESMTP id 7FC7214002C for ; Wed, 24 Apr 2024 08:27:45 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ezfqpoXY; spf=pass (imf09.hostedemail.com: domain of ytcoode@gmail.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=ytcoode@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713947265; 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=UyVLkZK5IAK4Hkz5uf7S362I3IR+MhEiIFxo4lPf7kQ=; b=I7R4iI/QR16fgM0QFoBYjFkZSyxkhMXLc+G5PiKvLwlqq21WQXCCVXJYYsPkYh36+AWRpi Y95a3qVMsjVYNmCqbTW6kCla3bGNsV0NININVN+rGYpNKNNB9iRFFX/C9AECSFiOE/8AyL fBhci6Eg3QQJtiG0eJ0b/51ykonrdvM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713947265; a=rsa-sha256; cv=none; b=k/MzMc1FL4igyCmVya0OA4Ed/M/LZt6a7miCcoYsZqAZW7izG8eakYm0RcBkwe7eQ/i00b 0XrShVA5v8bmxj1zkooT4zrmZHHNk0yuNhb3m/zfoudIUBxRQAjYSIT1/RqIGIKoFRLFmA Q26PdSNeKxuFIxO2yr+jz4AgW6AzQW0= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ezfqpoXY; spf=pass (imf09.hostedemail.com: domain of ytcoode@gmail.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=ytcoode@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-5dbf7b74402so4027003a12.0 for ; Wed, 24 Apr 2024 01:27:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713947264; x=1714552064; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UyVLkZK5IAK4Hkz5uf7S362I3IR+MhEiIFxo4lPf7kQ=; b=ezfqpoXYXU835TTpI0OnB3Opkbjc7nc9/kEYk7L73EIE9eEtzY452Z/pN7zq9AtM1u VN9lKdorrgVFdlEQqglmzdjz2hTfq87XI0Xwv7mEWayFFtL3nLwl9/19BMiDyNjUI5Cs bxB2uc/RyYjs/s5XphGPsfSgSJy1k1uFjl3qcvcc9qRDY5FwEW4y1BGrdg5vLrJtj8cX f8o0oaqpNj8UBnBU3O4jwZlWqos+j1naQ+riN5ZMTL3JwsNbZP2j4B70lEWcGBoJ4oD+ JcQle8fTpRltdWLlRc7sulK5k0f6yZIJuz8w1r/Ws/Bv755eR+L/vWBttbm8tQrQcx9M mleg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713947264; x=1714552064; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UyVLkZK5IAK4Hkz5uf7S362I3IR+MhEiIFxo4lPf7kQ=; b=G8jdqxiePU3zcciTQo65R4u4tbkOce0U3/fVHyf8wPvaDQbZNFd2vI52GSuVrRhO8+ P6SoKjz+w1nydrIs4baG758PgOotatE4fKKRovMCJse9BUMpkL7H1pbcE9M0adavMFan lVOMHBcKxtjzl0maJN+iqxxFXTWAmARQUT1HRxUiak8R+pG70Fpq+T4pudBaiPJTGQjQ eLBniJYSZPbvK768mDj/YzXtjN6FVCCqc4BwaSPhz6qvAn307N1bSjxKuXVHf1T8LY0o P2uwD3kEZ2/RjYF52TrfPfGkipFZH3l0xsRnavGQJ7i/jJ48nQu7iP4ronTCAfLLC61l fAhw== X-Forwarded-Encrypted: i=1; AJvYcCWeJTF1VdIZ8kuhhIN93rVEN+h9QzRK/Mq1fxiN8AxC2jG8ycGku3Uf9C7PCJy8jDiL8FKlKvTu0NRzxHh8bG10Khg= X-Gm-Message-State: AOJu0YxeF12U37MjFUSlHHiFgOWCt4br76EKxHWLcHjLlSY4ToaLwVUE 4fynp3BlSnWheqnBmveKImvPqyxLpbHHa/GP1D+K4iebeaRrx49n X-Google-Smtp-Source: AGHT+IGTK7wGhjw3HCy3pFp/GPitVMREtq2Jpo9FuF9PO8CrTRQirInQGMN99/thrEL2mGoJVfYMcw== X-Received: by 2002:a17:90a:c909:b0:2a6:4293:f88c with SMTP id v9-20020a17090ac90900b002a64293f88cmr1555035pjt.16.1713947264144; Wed, 24 Apr 2024 01:27:44 -0700 (PDT) Received: from code.. ([144.202.108.46]) by smtp.gmail.com with ESMTPSA id p18-20020a17090adf9200b002aecaa2aebdsm2328200pjv.20.2024.04.24.01.27.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 01:27:43 -0700 (PDT) From: Yuntao Wang To: dennis@kernel.org Cc: akpm@linux-foundation.org, cl@linux.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, tj@kernel.org, ytcoode@gmail.com Subject: [PATCH v2] percpu: simplify the logic of pcpu_alloc_first_chunk() Date: Wed, 24 Apr 2024 16:27:30 +0800 Message-ID: <20240424082730.99945-1-ytcoode@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 7FC7214002C X-Rspam-User: X-Stat-Signature: q3sr67o3qgza4jcikmy491usp35anodk X-HE-Tag: 1713947265-687713 X-HE-Meta: U2FsdGVkX19E7qk09tiqpThmuR22tqRZccKKDRA8gXC5MX9bqKcMzlcgtG5bdIdKxNsVnGcfCdVWW6cXXSBVEzqRE18sCDWajYLnRAHR6OKxGCNnmNRzv3aYkkBXadavkdxuIEPUZeEDNkDr1w9FEy4/Bvba5Scrx2lWyZxUXjC6iFuegkxdDvciNK4LFwV1Nq27A6Gcu3o25oXosnKT9xmXeTdLNPhlzde/yxlTdklNTjMKjcqrYA8fQJpUxZO6qDk8aFEetZsWAxdUKYv4Ta6W236UXP17+t+NtyM0a4zOR8AG3lxm/IYWTtEm+S9RGT1ZDG/cH2OVtHhX4iMqPXRxUzyk/RT/BL2+axeeJKSCwQEK3em7jUoc934ynL+aBef1krnnePZE1ec/KhoObE7N7xw6innHoQlds1iF8tPUWkKVY2skWwTz/jVLiF1XXE6QmxiPcbbb0JSjJHlOwndP92vc1YObZdKW5PeSzM4WF2mdyTRqM/HJMK8/XM6jZjjsGsw/kZGRMNmw2H6Zis8HcD97x9XIFbZjV9G85HBTJATqB3YjB4oZlkUnMKRDFQKvI3biWeI6+z4SfJtHmS5yStRE4mPGJtM8At9n9Qtg8b7nvnWjloCAsXWSw1ig/tJqxpQJmiAFWlpDogHSHpPjxBMSGOOYBAVaA92F8djzoy0SM8nNxV/yz95wkcmKKLZpM10aFVASEOX4x9cIYJH/yivlKVA2EzN3bN++QfzHZY7S0XDz1pK+SCyvaabfihJHsKQf06kjVO2kFjwb4vwn4h23WeXuxw0fbz6VG43i2AV5Y+rfomks8skzxqOKsweTDbtjjf8PPIEgm/1L382kfhxKkk0vap45nYMpS6TxsLEDWO6CmGj2E4+aRc0MbMMtjeFA9a+D7LyH9Y/3dDlfbLNPm0d2cXwehxZpszOu1YOXNDs01A/1S+O8xghCxF34DVLetc3Znuy5dzZ NEqMvUyE 6UQUC0je7G/9JemRcZi+x/fMhtuqgBEYB/pCbuNbKybUK/Cic8Nfy7rH7rWxdk07pShFN7oaO5W/KxcTXZjUqFrbU/ib/NN28eWeLQWBf/pnInYFPiQdsun77H2zwnPYQFIs/MLhOfguAgcvztNmuV+OSeE5UaxcV9Twf X-Bogosity: Ham, tests=bogofilter, spamicity=0.122098, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In the logic for hiding the end region of the chunk, there are several places where the same value 'region_bits - offset_bits' is calculated over and over again using different methods. Eliminating these redundant calculations can improve code readability. Additionally, there is a lot of repetitive code when hiding the start and end regions of the chunk. We can consolidate this logic into a function, making the final code cleaner and more concise. Signed-off-by: Yuntao Wang --- v1 -> v2: Optimize the code based on Dennis's suggestion mm/percpu.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/mm/percpu.c b/mm/percpu.c index 4e11fc1e6def..d22b317f3d41 100644 --- a/mm/percpu.c +++ b/mm/percpu.c @@ -1329,6 +1329,15 @@ static void pcpu_init_md_blocks(struct pcpu_chunk *chunk) pcpu_init_md_block(md_block, PCPU_BITMAP_BLOCK_BITS); } +static void pcpu_chunk_hide_region(struct pcpu_chunk *chunk, int bit_off, + int bits) +{ + bitmap_set(chunk->alloc_map, bit_off, bits); + set_bit(bit_off, chunk->bound_map); + set_bit(bit_off + bits, chunk->bound_map); + pcpu_block_update_hint_alloc(chunk, bit_off, bits); +} + /** * pcpu_alloc_first_chunk - creates chunks that serve the first chunk * @tmp_addr: the start of the region served @@ -1409,27 +1418,15 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr, if (chunk->start_offset) { /* hide the beginning of the bitmap */ offset_bits = chunk->start_offset / PCPU_MIN_ALLOC_SIZE; - bitmap_set(chunk->alloc_map, 0, offset_bits); - set_bit(0, chunk->bound_map); - set_bit(offset_bits, chunk->bound_map); - chunk->chunk_md.first_free = offset_bits; - - pcpu_block_update_hint_alloc(chunk, 0, offset_bits); + pcpu_chunk_hide_region(chunk, 0, offset_bits); } if (chunk->end_offset) { /* hide the end of the bitmap */ offset_bits = chunk->end_offset / PCPU_MIN_ALLOC_SIZE; - bitmap_set(chunk->alloc_map, - pcpu_chunk_map_bits(chunk) - offset_bits, - offset_bits); - set_bit((start_offset + map_size) / PCPU_MIN_ALLOC_SIZE, - chunk->bound_map); - set_bit(region_bits, chunk->bound_map); - - pcpu_block_update_hint_alloc(chunk, pcpu_chunk_map_bits(chunk) - - offset_bits, offset_bits); + pcpu_chunk_hide_region(chunk, region_bits - offset_bits, + offset_bits); } return chunk; -- 2.44.0