Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1068569rda; Mon, 23 Oct 2023 01:03:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHpJnBi7eAX09039OPx9DBZJv0hYTjrf4QgdwEm7+QSWMqRWj0oHa4p4WhMfEkIU7xPAFJ7 X-Received: by 2002:a05:6870:b492:b0:1e9:87fc:d63a with SMTP id y18-20020a056870b49200b001e987fcd63amr11044484oap.56.1698048194961; Mon, 23 Oct 2023 01:03:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698048194; cv=none; d=google.com; s=arc-20160816; b=XzdWb+EYpITRomIEx8tqJGBPDPVyMk4rMGFQKMMse/m9FVdKwLab7G12/Sj9Q9ijpT VpYpMR4xodF7/SI90DCHzkvY7pADpXUoK3cLCa8IICkppbRWWpmxnz2xw7+Lg35HTsWM LJ3X0oOVMcbfSjvG7FfoaQPzeQ89Cn+LVwaUw+jhNeuFui5lfTWmPMONrqkKTgsQGUFX EG9ZKtwrgnECjCmugv9IQXmLWKrvc7oWKtHfgpP8yfUjcJJtyh83alToxH8TG9U0gwNc T2lLEqwK0dVYf9YeTq/8bHDHI51HORQa0ps9giKPSdeDZNPk9bLb6BOlbYsw/edREs4e h4+A== 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 :message-id:date:subject:cc:to:from; bh=7dMuyMAwDxLV6S04QknYW7S5P2S0aRIdMtWuC19frU8=; fh=MTsRbanq8aNVFbVOJ6kHgFcZMfpB+DDg1rHzw2/NCkE=; b=fCmYZAfsZ/gkRyh0gc2ccNB0yRGjaWFv132nx1ibSa38nUvXik6xsR1R6I9TzuJxse RlOMqtm+6yY6sjw21xqokj4C6q0lLEGuCrOQyme9S7A8E5mEzkY/3jVzd8z+qB/D7p+t Lmsxx1RNZ/r6aaWOG3kKbCicV+t9Xzp2nHD+voixS2YJCqjYalOmRB3cfm9w5cl0oF+J XUhu3YX+TG4xUIjk/o0zKXg/QdKkTN+QPL4Ggi/P08UeUQxFq+SRX9P9tQ6oCr2YXrJg oZQCBibnM9TC4VfEtrs+OlQCXWWc6qGxZKV1+UZdI6C7qx7uxjfNl7iVeQDuRPSDqmgX x+wg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=aspeedtech.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id m3-20020a656a03000000b0059b7f926c03si6158590pgu.771.2023.10.23.01.03.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 01:03:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=aspeedtech.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 4D471805B2D3; Mon, 23 Oct 2023 01:03:09 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229498AbjJWICs (ORCPT + 99 others); Mon, 23 Oct 2023 04:02:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjJWICr (ORCPT ); Mon, 23 Oct 2023 04:02:47 -0400 Received: from TWMBX02.aspeed.com (mail.aspeedtech.com [211.20.114.72]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75BB4A6 for ; Mon, 23 Oct 2023 01:02:43 -0700 (PDT) Received: from TWMBX02.aspeed.com (192.168.0.24) by TWMBX02.aspeed.com (192.168.0.24) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 23 Oct 2023 16:02:38 +0800 Received: from twmbx02.aspeed.com (192.168.10.10) by TWMBX02.aspeed.com (192.168.0.24) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Mon, 23 Oct 2023 16:02:38 +0800 From: Billy Tsai To: , , , CC: Subject: [PATCH v1] i3c/master/mipi-i3c-hci: Fix a kernel panic for accessing DAT_data. Date: Mon, 23 Oct 2023 16:02:37 +0800 Message-ID: <20231023080237.560936-1-billy_tsai@aspeedtech.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII Received-SPF: Fail (TWMBX02.aspeed.com: domain of billy_tsai@aspeedtech.com does not designate 192.168.10.10 as permitted sender) receiver=TWMBX02.aspeed.com; client-ip=192.168.10.10; helo=twmbx02.aspeed.com; X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 23 Oct 2023 01:03:09 -0700 (PDT) The `i3c_master_bus_init` function may attach the I2C devices before the I3C bus initialization. In this flow, the DAT `alloc_entry`` will be used before the DAT `init`. Additionally, if the `i3c_master_bus_init` fails, the DAT `cleanup` will execute before the device is detached, which will execue DAT `free_entry` function. The above scenario can cause the driver to use DAT_data when it is NULL. Signed-off-by: Billy Tsai --- drivers/i3c/master/mipi-i3c-hci/dat_v1.c | 29 ++++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/drivers/i3c/master/mipi-i3c-hci/dat_v1.c b/drivers/i3c/master/mipi-i3c-hci/dat_v1.c index 97bb49ff5b53..47b9b4d4ed3f 100644 --- a/drivers/i3c/master/mipi-i3c-hci/dat_v1.c +++ b/drivers/i3c/master/mipi-i3c-hci/dat_v1.c @@ -64,15 +64,17 @@ static int hci_dat_v1_init(struct i3c_hci *hci) return -EOPNOTSUPP; } - /* use a bitmap for faster free slot search */ - hci->DAT_data = bitmap_zalloc(hci->DAT_entries, GFP_KERNEL); - if (!hci->DAT_data) - return -ENOMEM; - - /* clear them */ - for (dat_idx = 0; dat_idx < hci->DAT_entries; dat_idx++) { - dat_w0_write(dat_idx, 0); - dat_w1_write(dat_idx, 0); + if (!hci->DAT_data) { + /* use a bitmap for faster free slot search */ + hci->DAT_data = bitmap_zalloc(hci->DAT_entries, GFP_KERNEL); + if (!hci->DAT_data) + return -ENOMEM; + + /* clear them */ + for (dat_idx = 0; dat_idx < hci->DAT_entries; dat_idx++) { + dat_w0_write(dat_idx, 0); + dat_w1_write(dat_idx, 0); + } } return 0; @@ -87,7 +89,13 @@ static void hci_dat_v1_cleanup(struct i3c_hci *hci) static int hci_dat_v1_alloc_entry(struct i3c_hci *hci) { unsigned int dat_idx; + int ret; + if (!hci->DAT_data) { + ret = hci_dat_v1_init(hci); + if (ret) + return ret; + } dat_idx = find_first_zero_bit(hci->DAT_data, hci->DAT_entries); if (dat_idx >= hci->DAT_entries) return -ENOENT; @@ -103,7 +111,8 @@ static void hci_dat_v1_free_entry(struct i3c_hci *hci, unsigned int dat_idx) { dat_w0_write(dat_idx, 0); dat_w1_write(dat_idx, 0); - __clear_bit(dat_idx, hci->DAT_data); + if (hci->DAT_data) + __clear_bit(dat_idx, hci->DAT_data); } static void hci_dat_v1_set_dynamic_addr(struct i3c_hci *hci, -- 2.25.1