From patchwork Tue Nov 29 15:58:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uladzislau Rezki X-Patchwork-Id: 27318 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp424080wrr; Tue, 29 Nov 2022 08:01:02 -0800 (PST) X-Google-Smtp-Source: AA0mqf7Y+7mNbhtxRTboxbYCsA4B6XsG/DgZJR9mAX3IOra7NkD+BCxonV7hM7vF3Dq5DhV7pC5u X-Received: by 2002:a63:f845:0:b0:478:1187:b58 with SMTP id v5-20020a63f845000000b0047811870b58mr10402173pgj.43.1669737662104; Tue, 29 Nov 2022 08:01:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669737662; cv=none; d=google.com; s=arc-20160816; b=wnZzCBKWnwsX9IbSLbGPYz3QqHABghvpl4vVIZE3mQlQv4V+S7EfbAtNIPPQ60aUCx mbw5Y5o86xHFO7EVBgbDtHBr3Ez+ZCIa8itUCqgyez1w/6XLH2BOHBc2dbjHQtav5aN3 l/rgU1e9RqfAZi5O7cLkgT8MTyQclrDrvYz8RFNy+Z57ccIaCThRthvpOel1SO8OZ20J 9TKEGctr8G2F0W95WfOkeGvSqS8PzZu4jVyXCGYAuDJuu8n1RdDSSVs+hALDBFns0eGX 1aMyzAxUibtT2b4DEfuOTjszNIEshfxOfGC5SCuKiELq0mrOKJcmCwW5Q8tOU0AG6ull b22Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=i1497nUJKqf7VUksi/0Y8xV/c/P+R8VIcAPELcOUy1w=; b=Epmk254BTkbEl86XcStgP8UPRXrSyg4YdE11jevgku7Qvf9+BEcbStPXKfFD4ajrK6 Mx7QGsGZ4mSNpfZp8pAVStGsIMUThgeRIVLZNSrZtRVQ/uuIk+w6hiQtmR4DL+lFB/J0 e1qLHfVSZd3YwJxAbQKHLJCpaybw29sGfa78DWUWzo0wSkDuW+gEsccN93K46N1yksFt jLAwVfFirwcIvw2UuTK69Di9Yb0IsDiSkgndNloSkWfw28UUpWZKb5kW12I5ublz55vX DUU5v7N6tJeueQWuf1Wm4tEx7gWx7JnW552kc0bqY5ElrquFR4lecVZBggln1O2yP/fy Yj1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="MhUMVf/m"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bd32-20020a056a0027a000b00565cb1ebdb2si16043494pfb.267.2022.11.29.08.00.47; Tue, 29 Nov 2022 08:01:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="MhUMVf/m"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232491AbiK2QAW (ORCPT + 99 others); Tue, 29 Nov 2022 11:00:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235992AbiK2QAA (ORCPT ); Tue, 29 Nov 2022 11:00:00 -0500 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E30136A775; Tue, 29 Nov 2022 07:58:33 -0800 (PST) Received: by mail-ed1-x52f.google.com with SMTP id b8so20345498edf.11; Tue, 29 Nov 2022 07:58:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=i1497nUJKqf7VUksi/0Y8xV/c/P+R8VIcAPELcOUy1w=; b=MhUMVf/m+igVldFa7ZmQ2pWdRqW1NuYkVc/IwSgq5NNNP5/jxn2sm/Z380OJj0htNf 1zgvtVPf/Iyd0t6kPCVPZY6sMgXuGdDwBiZ5bnanINNSI7kG5lHI5gbJocSnwtvo1QmR 9Xx+ilF4A4y1zoN4lPqZRtu52SbLop964AV/UxWTf7btSzSpFn7ZypYZarF+ikSibsHt shMpnQNClLljA49Qa+G1IOFvZyjNl4w2eX55G26YLHkR5mxU2bj57HKcXYAH69sUZ474 WDCv7DQqIoEWi6mqTfDL+h0wB/T+bD1qG88nFMtVEUwkL75icLW+xth/k5aLuxnNSspd PxFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=i1497nUJKqf7VUksi/0Y8xV/c/P+R8VIcAPELcOUy1w=; b=ziHLbvaz2aNl4ZGhvFL+JbfCmoTI6ufhwyyVu6EZpzALuwglE5Xnx6fosBrtfdAai0 qlpp96bYg1Qw1jUGnw37r0lEZP9EHqnRxPEI6CYlEWlvOnXfw5Yq9XCfpLClFAcvaRjg FHfehxQUHBefMz4vXrIM60YhO7KG4Gma1XyLI/pp9tYoqSCZhNs3XcUR4vrx+wCPKCf5 rcM3zOyKQ8//L79TJqAjUROiZH8qcAa+VTgaQzd0SL8tCbAVd2k7qIWYhxoHKdnwdcFe DgJnm1HTRIZqcL0IAbZBRqlHfXTnthOGOEptGHfOJSrQ0jywmTA6br9xFRZmQxXXZXl3 Xtqg== X-Gm-Message-State: ANoB5pkdWvsY4TMgBIHwnY2PbGNEr4rCAwWoflnC4JB+h+7J4I1DlFH7 uDaOHl7u94IePWBWqBR/ZPndJhR33IQ= X-Received: by 2002:a05:6402:1f89:b0:458:caec:8f1e with SMTP id c9-20020a0564021f8900b00458caec8f1emr51499032edc.280.1669737512122; Tue, 29 Nov 2022 07:58:32 -0800 (PST) Received: from pc638.lan ([155.137.26.201]) by smtp.gmail.com with ESMTPSA id u23-20020a056402065700b0046778ce5fdfsm6371059edx.10.2022.11.29.07.58.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 07:58:31 -0800 (PST) From: "Uladzislau Rezki (Sony)" To: LKML , RCU , "Paul E . McKenney" Cc: Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Uladzislau Rezki , Oleksiy Avramchenko Subject: [PATCH v2 2/4] rcu/kvfree: Move bulk/list reclaim to separate functions Date: Tue, 29 Nov 2022 16:58:20 +0100 Message-Id: <20221129155822.538434-3-urezki@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221129155822.538434-1-urezki@gmail.com> References: <20221129155822.538434-1-urezki@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750846839007701761?= X-GMAIL-MSGID: =?utf-8?q?1750846839007701761?= There are two different paths how a memory is reclaimed. Currently it is open-coded what makes it a bit messy and less easy to read. Introduce two separate functions kvfree_rcu_list() and kvfree_rcu_bulk() to cover two independent cases. Please note, this patch does not introduce any functional change it is only limited by refactoring of code. Signed-off-by: Uladzislau Rezki (Sony) --- kernel/rcu/tree.c | 114 ++++++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 49 deletions(-) diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index 1c79b244aac9..445f8c11a9a3 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -2893,6 +2893,65 @@ drain_page_cache(struct kfree_rcu_cpu *krcp) return freed; } +static void +kvfree_rcu_bulk(struct kfree_rcu_cpu *krcp, + struct kvfree_rcu_bulk_data *bnode, int idx) +{ + unsigned long flags; + int i; + + debug_rcu_bhead_unqueue(bnode); + + rcu_lock_acquire(&rcu_callback_map); + if (idx == 0) { // kmalloc() / kfree(). + trace_rcu_invoke_kfree_bulk_callback( + rcu_state.name, bnode->nr_records, + bnode->records); + + kfree_bulk(bnode->nr_records, bnode->records); + } else { // vmalloc() / vfree(). + for (i = 0; i < bnode->nr_records; i++) { + trace_rcu_invoke_kvfree_callback( + rcu_state.name, bnode->records[i], 0); + + vfree(bnode->records[i]); + } + } + rcu_lock_release(&rcu_callback_map); + + raw_spin_lock_irqsave(&krcp->lock, flags); + if (put_cached_bnode(krcp, bnode)) + bnode = NULL; + raw_spin_unlock_irqrestore(&krcp->lock, flags); + + if (bnode) + free_page((unsigned long) bnode); + + cond_resched_tasks_rcu_qs(); +} + +static void +kvfree_rcu_list(struct rcu_head *head) +{ + struct rcu_head *next; + + for (; head; head = next) { + void *ptr = (void *) head->func; + unsigned long offset = (void *) head - ptr; + + next = head->next; + debug_rcu_head_unqueue((struct rcu_head *)ptr); + rcu_lock_acquire(&rcu_callback_map); + trace_rcu_invoke_kvfree_callback(rcu_state.name, head, offset); + + if (!WARN_ON_ONCE(!__is_kvfree_rcu_offset(offset))) + kvfree(ptr); + + rcu_lock_release(&rcu_callback_map); + cond_resched_tasks_rcu_qs(); + } +} + /* * This function is invoked in workqueue context after a grace period. * It frees all the objects queued on ->bulk_head_free or ->head_free. @@ -2902,10 +2961,10 @@ static void kfree_rcu_work(struct work_struct *work) unsigned long flags; struct kvfree_rcu_bulk_data *bnode, *n; struct list_head bulk_head[FREE_N_CHANNELS]; - struct rcu_head *head, *next; + struct rcu_head *head; struct kfree_rcu_cpu *krcp; struct kfree_rcu_cpu_work *krwp; - int i, j; + int i; krwp = container_of(to_rcu_work(work), struct kfree_rcu_cpu_work, rcu_work); @@ -2922,38 +2981,9 @@ static void kfree_rcu_work(struct work_struct *work) raw_spin_unlock_irqrestore(&krcp->lock, flags); // Handle the first two channels. - for (i = 0; i < FREE_N_CHANNELS; i++) { - list_for_each_entry_safe(bnode, n, &bulk_head[i], list) { - debug_rcu_bhead_unqueue(bnode); - - rcu_lock_acquire(&rcu_callback_map); - if (i == 0) { // kmalloc() / kfree(). - trace_rcu_invoke_kfree_bulk_callback( - rcu_state.name, bnode->nr_records, - bnode->records); - - kfree_bulk(bnode->nr_records, bnode->records); - } else { // vmalloc() / vfree(). - for (j = 0; j < bnode->nr_records; j++) { - trace_rcu_invoke_kvfree_callback( - rcu_state.name, bnode->records[j], 0); - - vfree(bnode->records[j]); - } - } - rcu_lock_release(&rcu_callback_map); - - raw_spin_lock_irqsave(&krcp->lock, flags); - if (put_cached_bnode(krcp, bnode)) - bnode = NULL; - raw_spin_unlock_irqrestore(&krcp->lock, flags); - - if (bnode) - free_page((unsigned long) bnode); - - cond_resched_tasks_rcu_qs(); - } - } + for (i = 0; i < FREE_N_CHANNELS; i++) + list_for_each_entry_safe(bnode, n, &bulk_head[i], list) + kvfree_rcu_bulk(krcp, bnode, i); /* * This is used when the "bulk" path can not be used for the @@ -2962,21 +2992,7 @@ static void kfree_rcu_work(struct work_struct *work) * queued on a linked list through their rcu_head structures. * This list is named "Channel 3". */ - for (; head; head = next) { - void *ptr = (void *) head->func; - unsigned long offset = (void *) head - ptr; - - next = head->next; - debug_rcu_head_unqueue((struct rcu_head *)ptr); - rcu_lock_acquire(&rcu_callback_map); - trace_rcu_invoke_kvfree_callback(rcu_state.name, head, offset); - - if (!WARN_ON_ONCE(!__is_kvfree_rcu_offset(offset))) - kvfree(ptr); - - rcu_lock_release(&rcu_callback_map); - cond_resched_tasks_rcu_qs(); - } + kvfree_rcu_list(head); } static bool