From patchwork Wed Jul 12 01:31:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zheng Yejian X-Patchwork-Id: 118770 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp859218vqm; Tue, 11 Jul 2023 19:08:17 -0700 (PDT) X-Google-Smtp-Source: APBJJlEI02+XINT3WWCO3Lc+zmT2fFneVXI7x0ZPGa6lnobsceRAJ0pFxKJKYFVr7ZNzxyfo24JW X-Received: by 2002:aa7:8891:0:b0:681:3ed2:b493 with SMTP id z17-20020aa78891000000b006813ed2b493mr15289800pfe.26.1689127696747; Tue, 11 Jul 2023 19:08:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689127696; cv=none; d=google.com; s=arc-20160816; b=KarZ5aR4c8UWTV75/CQdPlmAvfwjVu0tEkDijXtUQS1ABRRD1l/qCYUuhLF2DZG2xW ZPmbFz8SgYVcS8pLYDBDzzT6HPJMzOLdlepdSuWMqnxuhHa9rd/cJqbFIX0ptLtwB+fU WBnYkD/IcSHoV0R7iA8jgSd8NC7w6BPi+qo14NMtTKR80b/6N04w7xUjSI3i6ywkdmhS qFnRPemMG2ur4lqaBG+lxn/oX9sTD5QZ1c2iF/CShcPVMQG6YKFuZJApqzZYZPEayN26 5/jIGpi+iM1GODSFwBJydpXw+lr+prRgzlVAxF/e1atI5xHrxmGO2GciVefRBY0ySs8P wjgw== 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=545xgirTgHo+plVALXOVskX8Lx1fd8R4yfZxn79sfGw=; fh=APhBgUjYXesXWOIgKm8aSjIctaOGtmXBvn0W6GCNYWk=; b=IIGFGWzlJT97HPQcpD0iYpoZLB7uguEEJj8m6RsXMbbxecmp/tu/EbVv9yiGfVLE7I 34thvKiORPSbTWxqV7EIz8fpJTv3QM2Dz1C0HDPVloC75pTF1iq1bz+WHQpOokZQwnVa 6MttPb6xoaeH1ebve7sDU/in/L87Q/+2lCHR8DiUfEmWcsEX4rzcVBWpgjngSfbNmCH1 jcjCv6/+fjLANWE3MvJdll0ZTKAQsEO/3jfngVAmXuAde/WFj8Qp54cE156vzZrG22gw 5pPgnpxS7GjjX3xbIVUb1HWhRIgLRBFxDT7Ggw4GVO/EY2PLLnboNNjmiC70Ynw8/5Ba 2eOw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fb6-20020a056a002d8600b0063d45a6d0e5si2475782pfb.17.2023.07.11.19.08.04; Tue, 11 Jul 2023 19:08:16 -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 S231355AbjGLBbO (ORCPT + 99 others); Tue, 11 Jul 2023 21:31:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230329AbjGLBbN (ORCPT ); Tue, 11 Jul 2023 21:31:13 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AFB4E69; Tue, 11 Jul 2023 18:31:11 -0700 (PDT) Received: from dggpeml500012.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4R10WP1bJQzPkGV; Wed, 12 Jul 2023 09:28:49 +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 09:31:08 +0800 From: Zheng Yejian To: CC: , , , Subject: [PATCH v4] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs() Date: Wed, 12 Jul 2023 09:31:03 +0800 Message-ID: <20230712013103.3021978-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 X-Originating-IP: [10.67.175.61] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500012.china.huawei.com (7.185.36.15) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771026112620047218 X-GMAIL-MSGID: 1771178763511186305 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 v3 [3] => v4: - Keep the upside-down-xmas-tree format as Steve suggested. Link: https://lore.kernel.org/all/20230711095802.71406422@gandalf.local.home/ Changes v2 [2] => v3: - Check NULL for 'pg->next' before assigning it to variable 'pg_unuse'. Changes v1 [1] => v2: - As Steve suggested, 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. Link: https://lore.kernel.org/all/20230710104625.421c851a@gandalf.local.home/ - 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/ kernel/trace/ftrace.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 3740aca79fe7..6fc238f6ef3e 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -6471,9 +6471,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 +6538,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 +6555,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 +6580,20 @@ 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); + while (pg_unuse) { + pg = pg_unuse; + pg_unuse = pg->next; + if (pg->records) { + free_pages((unsigned long)pg->records, pg->order); + ftrace_number_of_pages -= 1 << pg->order; + } + kfree(pg); + ftrace_number_of_groups--; + } + } return ret; }