Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp37902698rwd; Tue, 11 Jul 2023 23:30:53 -0700 (PDT) X-Google-Smtp-Source: APBJJlHRKX8ueuHN6UzYLmmtUzqzTPS+2qT7bCVHAc0qfQM2wxDmpFfjlWlZy3Y/nlPIx7OO65yH X-Received: by 2002:a17:907:9251:b0:992:ef60:aae0 with SMTP id kb17-20020a170907925100b00992ef60aae0mr16184101ejb.54.1689143453163; Tue, 11 Jul 2023 23:30:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689143453; cv=none; d=google.com; s=arc-20160816; b=ghLZ1ZOVqBXPBWbXbTMm7qBvjpvxbLPtO3kFvYwm7VyRWa5XChTtOw4mOFYwJGjTbh S6tzrHcfAv8Id8ieEXwLcG9scrh1dpbowAQ7RVFmVKO4p52wgFDYeaTRvdjtD/Mwub0t 6lJN+zhu40FoDyMhmzQ54psVZ1hErJ2HXsGI3IvtTpd1inJqaqnBTbFj95rRtb+dDKTz PL1e/OkJ/5WFRyQ5QRVa4WkrKdz/oDJLmKf4TBwEFcCugpAuZIFT9amOoVuG6ag+fVHY VSf7IxSbJdmmjR0KTQXujtJEBAJvDMcOhxWYptRWvuwjoBOPHSSlUCw1ohbMD0o8RIfM +mmw== 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; bh=B5a+73NuPutjvA9O1l7iDtsnSyYk4L6WZvPBtc0BunI=; fh=APhBgUjYXesXWOIgKm8aSjIctaOGtmXBvn0W6GCNYWk=; b=0VPNsA0hS8ZGwDXbjtf7fzZmup+avwdaSkceMnVE4PKq7GfzM4ukOlGS+6iVXsHI+o BDe465bxs15Y4Rb+MiTFJkBYZsmbwYo/0sLSL5dwGyYFH06hoDtlQMK8lAnyhgA02tUp nZJ8+QwQFhDHNe65mhBFF1uDZ2maqnyjEozjCbOT4DVFceo0BJbZ6oezlDpPG2FEHHnd RdWxbxrRCdfzkmoMHopCKoSELNQGhN5mFxtvX9QZlapuV2FBJxr5Q7Y7fu86CI1jEXOQ v4J35zDrmcuT02s/JykkBW0361IfnImdD4nv3Xa9hyHQ5hjhIWgC29h0CZvuySnW3LUu yBgw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bl1-20020a170906c24100b009889b69c1edsi3922669ejb.697.2023.07.11.23.30.29; Tue, 11 Jul 2023 23:30:53 -0700 (PDT) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230398AbjGLGFC (ORCPT + 99 others); Wed, 12 Jul 2023 02:05:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230308AbjGLGFA (ORCPT ); Wed, 12 Jul 2023 02:05:00 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A2BFC0; Tue, 11 Jul 2023 23:04:59 -0700 (PDT) Received: from dggpeml500012.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4R16bK2nTbzPkFr; Wed, 12 Jul 2023 14:02:37 +0800 (CST) Received: from localhost.localdomain (10.67.175.61) by dggpeml500012.china.huawei.com (7.185.36.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Wed, 12 Jul 2023 14:04:57 +0800 From: Zheng Yejian To: CC: , , , Subject: [PATCH v5] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs() Date: Wed, 12 Jul 2023 14:04:52 +0800 Message-ID: <20230712060452.3175675-1-zhengyejian1@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230711095802.71406422@gandalf.local.home> References: <20230711095802.71406422@gandalf.local.home> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.67.175.61] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500012.china.huawei.com (7.185.36.15) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 As comments in ftrace_process_locs(), there may be NULL pointers in mcount_loc section: > Some architecture linkers will pad between > the different mcount_loc sections of different > object files to satisfy alignments. > Skip any NULL pointers. After commit 20e5227e9f55 ("ftrace: allow NULL pointers in mcount_loc"), NULL pointers will be accounted when allocating ftrace pages but skipped before adding into ftrace pages, this may result in some pages not being used. Then after commit 706c81f87f84 ("ftrace: Remove extra helper functions"), warning may occur at: WARN_ON(pg->next); To fix it, only warn for case that no pointers skipped but pages not used up, then free those unused pages after releasing ftrace_lock. Fixes: 706c81f87f84 ("ftrace: Remove extra helper functions") Suggested-by: Steven Rostedt Signed-off-by: Zheng Yejian --- Changes v4 [6] => v5: - Extract page free logic in ftrace_allocate_pages() as ftrace_free_pages(), then call it to free unused pages in ftrace_process_locs(). Changes v3 [3] => v4: - Keep the upside-down-xmas-tree format as Steve suggested [5]. Changes v2 [2] => v3: - Check NULL for 'pg->next' before assigning it to variable 'pg_unuse'. Changes v1 [1] => v2: - As Steve suggested [4], only warn for case that no pointers skipped but pages not used up then free those unused pages. But I move the free process after releasing ftrace_lock. - Update commit messages about the new solution. [1] https://lore.kernel.org/all/20230710212958.274126-1-zhengyejian1@huawei.com/ [2] https://lore.kernel.org/all/20230711201630.1837109-1-zhengyejian1@huawei.com/ [3] https://lore.kernel.org/all/20230711112752.2595316-1-zhengyejian1@huawei.com/ [4] https://lore.kernel.org/all/20230710104625.421c851a@gandalf.local.home/ [5] https://lore.kernel.org/all/20230711095802.71406422@gandalf.local.home/ [6] https://lore.kernel.org/all/20230712013103.3021978-1-zhengyejian1@huawei.com/ kernel/trace/ftrace.c | 45 +++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 3740aca79fe7..05c0024815bf 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -3305,6 +3305,22 @@ static int ftrace_allocate_records(struct ftrace_page *pg, int count) return cnt; } +static void ftrace_free_pages(struct ftrace_page *pages) +{ + struct ftrace_page *pg = pages; + + while (pg) { + if (pg->records) { + free_pages((unsigned long)pg->records, pg->order); + ftrace_number_of_pages -= 1 << pg->order; + } + pages = pg->next; + kfree(pg); + pg = pages; + ftrace_number_of_groups--; + } +} + static struct ftrace_page * ftrace_allocate_pages(unsigned long num_to_init) { @@ -3343,17 +3359,7 @@ ftrace_allocate_pages(unsigned long num_to_init) return start_pg; free_pages: - pg = start_pg; - while (pg) { - if (pg->records) { - free_pages((unsigned long)pg->records, pg->order); - ftrace_number_of_pages -= 1 << pg->order; - } - start_pg = pg->next; - kfree(pg); - pg = start_pg; - ftrace_number_of_groups--; - } + ftrace_free_pages(start_pg); pr_info("ftrace: FAILED to allocate memory for functions\n"); return NULL; } @@ -6471,9 +6477,11 @@ static int ftrace_process_locs(struct module *mod, unsigned long *start, unsigned long *end) { + struct ftrace_page *pg_unuse = NULL; struct ftrace_page *start_pg; struct ftrace_page *pg; struct dyn_ftrace *rec; + unsigned long skipped = 0; unsigned long count; unsigned long *p; unsigned long addr; @@ -6536,8 +6544,10 @@ static int ftrace_process_locs(struct module *mod, * object files to satisfy alignments. * Skip any NULL pointers. */ - if (!addr) + if (!addr) { + skipped++; continue; + } end_offset = (pg->index+1) * sizeof(pg->records[0]); if (end_offset > PAGE_SIZE << pg->order) { @@ -6551,8 +6561,10 @@ static int ftrace_process_locs(struct module *mod, rec->ip = addr; } - /* We should have used all pages */ - WARN_ON(pg->next); + if (pg->next) { + pg_unuse = pg->next; + pg->next = NULL; + } /* Assign the last page to ftrace_pages */ ftrace_pages = pg; @@ -6574,6 +6586,11 @@ static int ftrace_process_locs(struct module *mod, out: mutex_unlock(&ftrace_lock); + /* We should have used all pages unless we skipped some */ + if (pg_unuse) { + WARN_ON(!skipped); + ftrace_free_pages(pg_unuse); + } return ret; } -- 2.25.1