From patchwork Mon Jan 10 18:47:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Kaehlcke X-Patchwork-Id: 12709051 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03CE1C433FE for ; Mon, 10 Jan 2022 18:47:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241606AbiAJSrp (ORCPT ); Mon, 10 Jan 2022 13:47:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239534AbiAJSrp (ORCPT ); Mon, 10 Jan 2022 13:47:45 -0500 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EEB65C061748 for ; Mon, 10 Jan 2022 10:47:44 -0800 (PST) Received: by mail-pl1-x631.google.com with SMTP id t18so3786065plg.9 for ; Mon, 10 Jan 2022 10:47:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=8rLNNTam0GGSuTPyyVz1PzDH77pSBcTTKBVr91Cfs40=; b=Uoh7XeUXB53SMI5MmhJjyywZINMKK+fW95UVEhlKaD5KIHCB0cJoNpiiMzAUhu72hI qAgNgBt2717VdpCPmaynmdVRlg0Jitx8iIolBGyoC+C4K0jDCKtPlwT6uxCBmpWsleDW RJUMlJw1vM09Dz2ZxU4WSuFUnJEZ3UBW1QD24= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=8rLNNTam0GGSuTPyyVz1PzDH77pSBcTTKBVr91Cfs40=; b=3Xs5Tv6oNaoDnvG40uRPDSwWUOW1pV085dftZl8m0JDNjAtZ11VP2MbqSumhrXVstS ZcqncWn92VcGfN49giZFu9+B6S2b3W4+JYadzdfLZ4Y9gh27V1M09MiNoADmI4UheXG6 TO/6ljIJgDUO7JfVeBQsCygF7FosAiCHz1bV7tJkd0o7crLA8xcTQccUWDHrHCYXrQej yu4emMNOVUZBXxnCwYDutvCIGHLn065VCLGkmELn5W/ua+zcNcMquy7SIE/pXQQeEY/n eBn7rhNs6CpnW1KFaNSJDYFDdnGQuvB4fpfAQOgwwWB2r78CFQU89WFKQZYU3AN7O08J U6WQ== X-Gm-Message-State: AOAM532mN9pxAXiT8+H+hdGa3tioa6JQtAS0SY3kU2whF3CAHcNzGwMN ms4n1+IqgX5DVBMC83MHHuKJ5A== X-Google-Smtp-Source: ABdhPJzGcI/pzbdJgE6UpbLi6Ea8+bFNov9DkpzFKV0156JTZbqgDhJiHNNy99sc8Fv2zLcrMKIdJg== X-Received: by 2002:a05:6a00:1249:b0:4bb:4a31:1e0a with SMTP id u9-20020a056a00124900b004bb4a311e0amr858625pfi.81.1641840464396; Mon, 10 Jan 2022 10:47:44 -0800 (PST) Received: from localhost ([2620:15c:202:201:470e:3451:3aa4:4b43]) by smtp.gmail.com with UTF8SMTPSA id l2sm7332363pfe.189.2022.01.10.10.47.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 Jan 2022 10:47:43 -0800 (PST) From: Matthias Kaehlcke To: Andy Gross , Ohad Ben-Cohen Cc: Stephen Boyd , linux-kernel@vger.kernel.org, linux-remoteproc@vger.kernel.org, Sibi Sankar , Sujit Kautkar , Bjorn Andersson , Matthias Kaehlcke , Mathieu Poirier Subject: [PATCH v6 1/2] rpmsg: char: Fix race between the release of rpmsg_ctrldev and cdev Date: Mon, 10 Jan 2022 10:47:36 -0800 Message-Id: <20220110104706.v6.1.Iaac908f3e3149a89190ce006ba166e2d3fd247a3@changeid> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-remoteproc@vger.kernel.org From: Sujit Kautkar struct rpmsg_ctrldev contains a struct cdev. The current code frees the rpmsg_ctrldev struct in rpmsg_ctrldev_release_device(), but the cdev is a managed object, therefore its release is not predictable and the rpmsg_ctrldev could be freed before the cdev is entirely released, as in the backtrace below. [ 93.625603] ODEBUG: free active (active state 0) object type: timer_list hint: delayed_work_timer_fn+0x0/0x7c [ 93.636115] WARNING: CPU: 0 PID: 12 at lib/debugobjects.c:488 debug_print_object+0x13c/0x1b0 [ 93.644799] Modules linked in: veth xt_cgroup xt_MASQUERADE rfcomm algif_hash algif_skcipher af_alg uinput ip6table_nat fuse uvcvideo videobuf2_vmalloc venus_enc venus_dec videobuf2_dma_contig hci_uart btandroid btqca snd_soc_rt5682_i2c bluetooth qcom_spmi_temp_alarm snd_soc_rt5682v [ 93.715175] CPU: 0 PID: 12 Comm: kworker/0:1 Tainted: G B 5.4.163-lockdep #26 [ 93.723855] Hardware name: Google Lazor (rev3 - 8) with LTE (DT) [ 93.730055] Workqueue: events kobject_delayed_cleanup [ 93.735271] pstate: 60c00009 (nZCv daif +PAN +UAO) [ 93.740216] pc : debug_print_object+0x13c/0x1b0 [ 93.744890] lr : debug_print_object+0x13c/0x1b0 [ 93.749555] sp : ffffffacf5bc7940 [ 93.752978] x29: ffffffacf5bc7940 x28: dfffffd000000000 [ 93.758448] x27: ffffffacdb11a800 x26: dfffffd000000000 [ 93.763916] x25: ffffffd0734f856c x24: dfffffd000000000 [ 93.769389] x23: 0000000000000000 x22: ffffffd0733c35b0 [ 93.774860] x21: ffffffd0751994a0 x20: ffffffd075ec27c0 [ 93.780338] x19: ffffffd075199100 x18: 00000000000276e0 [ 93.785814] x17: 0000000000000000 x16: dfffffd000000000 [ 93.791291] x15: ffffffffffffffff x14: 6e6968207473696c [ 93.796768] x13: 0000000000000000 x12: ffffffd075e2b000 [ 93.802244] x11: 0000000000000001 x10: 0000000000000000 [ 93.807723] x9 : d13400dff1921900 x8 : d13400dff1921900 [ 93.813200] x7 : 0000000000000000 x6 : 0000000000000000 [ 93.818676] x5 : 0000000000000080 x4 : 0000000000000000 [ 93.824152] x3 : ffffffd0732a0fa4 x2 : 0000000000000001 [ 93.829628] x1 : ffffffacf5bc7580 x0 : 0000000000000061 [ 93.835104] Call trace: [ 93.837644] debug_print_object+0x13c/0x1b0 [ 93.841963] __debug_check_no_obj_freed+0x25c/0x3c0 [ 93.846987] debug_check_no_obj_freed+0x18/0x20 [ 93.851669] slab_free_freelist_hook+0xbc/0x1e4 [ 93.856346] kfree+0xfc/0x2f4 [ 93.859416] rpmsg_ctrldev_release_device+0x78/0xb8 [ 93.864445] device_release+0x84/0x168 [ 93.868310] kobject_cleanup+0x12c/0x298 [ 93.872356] kobject_delayed_cleanup+0x10/0x18 [ 93.876948] process_one_work+0x578/0x92c [ 93.881086] worker_thread+0x804/0xcf8 [ 93.884963] kthread+0x2a8/0x314 [ 93.888303] ret_from_fork+0x10/0x18 The cdev_device_add/del() API was created to address this issue (see commit 233ed09d7fda), use it instead of cdev add/del(). Fixes: c0cdc19f84a4 ("rpmsg: Driver for user space endpoint interface") Signed-off-by: Sujit Kautkar Signed-off-by: Matthias Kaehlcke Reviewed-by: Mathieu Poirier Reviewed-by: Bjorn Andersson Reviewed-by: Stephen Boyd --- Changes in v6: - none Changes in v5: - added 'Fixes' tag - added 'Reviewed-by' tags from Mathieu and Bjorn Changes in v4: - call cdev_device_del() from rpmsg_chrdev_remove() instead of rpmsg_ctrldev_release_device() - updated subject (was: "rpmsg: glink: Update cdev add/del API in rpmsg_ctrldev_release_device()") - updated commit message - replaced backtrace in commit message with one that doesn't have a dump_backtrace() call Changes in v3: - Remove unecessary error check as per Matthias's comment Changes in v2: - Fix typo in commit message drivers/rpmsg/rpmsg_char.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c index 2bebc9b2d163..ba85f5d11960 100644 --- a/drivers/rpmsg/rpmsg_char.c +++ b/drivers/rpmsg/rpmsg_char.c @@ -461,7 +461,6 @@ static void rpmsg_ctrldev_release_device(struct device *dev) ida_simple_remove(&rpmsg_ctrl_ida, dev->id); ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt)); - cdev_del(&ctrldev->cdev); kfree(ctrldev); } @@ -496,19 +495,13 @@ static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev) dev->id = ret; dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", ret); - ret = cdev_add(&ctrldev->cdev, dev->devt, 1); + ret = cdev_device_add(&ctrldev->cdev, &ctrldev->dev); if (ret) goto free_ctrl_ida; /* We can now rely on the release function for cleanup */ dev->release = rpmsg_ctrldev_release_device; - ret = device_add(dev); - if (ret) { - dev_err(&rpdev->dev, "device_add failed: %d\n", ret); - put_device(dev); - } - dev_set_drvdata(&rpdev->dev, ctrldev); return ret; @@ -534,7 +527,7 @@ static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev) if (ret) dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret); - device_del(&ctrldev->dev); + cdev_device_del(&ctrldev->cdev, &ctrldev->dev); put_device(&ctrldev->dev); } From patchwork Mon Jan 10 18:47:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Kaehlcke X-Patchwork-Id: 12709052 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF6B4C433F5 for ; Mon, 10 Jan 2022 18:47:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242220AbiAJSrs (ORCPT ); Mon, 10 Jan 2022 13:47:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241560AbiAJSrr (ORCPT ); Mon, 10 Jan 2022 13:47:47 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D80FC061748 for ; Mon, 10 Jan 2022 10:47:47 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id y16-20020a17090a6c9000b001b13ffaa625so61132pjj.2 for ; Mon, 10 Jan 2022 10:47:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ShvwMaotgmHYVdyRN67h92I9DP3zj/wpQ1GkBlk/Io0=; b=bY3Jr6clk/F1DeLvYYTFQrOpmv3r+/HIgddPZvgbu9aU7WONBEcBhaxAx3gTkoKgUW rZEn23pieFdWmQP/CjGjD1akaeFOmUriPWVKAoxKRxh0H3k53xsZqK1leMpg9z8kxXeK BlWU1lQNk2TTML9iyGAIAXnvBh7NZvUJsg09M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ShvwMaotgmHYVdyRN67h92I9DP3zj/wpQ1GkBlk/Io0=; b=hFlwzdaQyGKLJDhrAf8R8/Hb9R9008S+HJU7ggr6Ia9u1bJLBlOyWgMxgUNfHdl0Pb 3DFBEyz+rnYqduwqQ1Wv7ZWNR8/DLmPGuFTzI/n+/Vz9+2dfNdGwaLmlNfAOPeih+967 oQRQ03ot3BWBNGK1jcSKDtQjjAw0A4EVIu7TuyqXYl0+bO39xg0OATpZHq1b/OvlGIh/ ozqWP6z4YAyCY25tJmwRaLjGxg4CEsMstU+We0BkgGd4gDCPHume0F2x9r6HJFkKn9OT HMR6CZKyL5JHkGKjZAWVdmyS+wdies4WLTpdwn1WdgnEpXePXRcXrNMsVabas7X8vXDr Xk6A== X-Gm-Message-State: AOAM530UrOfCpERv2En1lLi13XXPcnzCLPwDWlmxbGnMXJ8faIc1GBvx m1JcbUXdkRpD+NqFdnIxB/Y4vw== X-Google-Smtp-Source: ABdhPJxderP7otW8Zajj9tFh0nOWHilY6JOnS9jnc7szLXg+tzFHs2CFgUsgiuNM5SOx2QsQuKyMnA== X-Received: by 2002:a17:90b:1e50:: with SMTP id pi16mr1104987pjb.118.1641840466651; Mon, 10 Jan 2022 10:47:46 -0800 (PST) Received: from localhost ([2620:15c:202:201:470e:3451:3aa4:4b43]) by smtp.gmail.com with UTF8SMTPSA id s35sm7393137pfw.193.2022.01.10.10.47.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 Jan 2022 10:47:45 -0800 (PST) From: Matthias Kaehlcke To: Andy Gross , Ohad Ben-Cohen Cc: Stephen Boyd , linux-kernel@vger.kernel.org, linux-remoteproc@vger.kernel.org, Sibi Sankar , Sujit Kautkar , Bjorn Andersson , Matthias Kaehlcke , Mathieu Poirier Subject: [PATCH v6 2/2] rpmsg: char: Fix race between the release of rpmsg_eptdev and cdev Date: Mon, 10 Jan 2022 10:47:37 -0800 Message-Id: <20220110104706.v6.2.Idde68b05b88d4a2e6e54766c653f3a6d9e419ce6@changeid> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog In-Reply-To: <20220110104706.v6.1.Iaac908f3e3149a89190ce006ba166e2d3fd247a3@changeid> References: <20220110104706.v6.1.Iaac908f3e3149a89190ce006ba166e2d3fd247a3@changeid> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-remoteproc@vger.kernel.org struct rpmsg_eptdev contains a struct cdev. The current code frees the rpmsg_eptdev struct in rpmsg_eptdev_destroy(), but the cdev is a managed object, therefore its release is not predictable and the rpmsg_eptdev could be freed before the cdev is entirely released. The cdev_device_add/del() API was created to address this issue (see commit 233ed09d7fda), use it instead of cdev add/del(). Fixes: c0cdc19f84a4 ("rpmsg: Driver for user space endpoint interface") Suggested-by: Bjorn Andersson Signed-off-by: Matthias Kaehlcke Reviewed-by: Mathieu Poirier Reviewed-by: Bjorn Andersson Reviewed-by: Stephen Boyd --- Changes in v6: - remove cdev_del() from rpmsg_eptdev_release_device() - added 'Reviewed-by' tag from Mathieu and 'Suggested-by' tag Changes in v5: - patch added to the series drivers/rpmsg/rpmsg_char.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c index ba85f5d11960..49dd5a200998 100644 --- a/drivers/rpmsg/rpmsg_char.c +++ b/drivers/rpmsg/rpmsg_char.c @@ -92,7 +92,7 @@ static int rpmsg_eptdev_destroy(struct device *dev, void *data) /* wake up any blocked readers */ wake_up_interruptible(&eptdev->readq); - device_del(&eptdev->dev); + cdev_device_del(&eptdev->cdev, &eptdev->dev); put_device(&eptdev->dev); return 0; @@ -335,7 +335,6 @@ static void rpmsg_eptdev_release_device(struct device *dev) ida_simple_remove(&rpmsg_ept_ida, dev->id); ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt)); - cdev_del(&eptdev->cdev); kfree(eptdev); } @@ -380,19 +379,13 @@ static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, dev->id = ret; dev_set_name(dev, "rpmsg%d", ret); - ret = cdev_add(&eptdev->cdev, dev->devt, 1); + ret = cdev_device_add(&eptdev->cdev, &eptdev->dev); if (ret) goto free_ept_ida; /* We can now rely on the release function for cleanup */ dev->release = rpmsg_eptdev_release_device; - ret = device_add(dev); - if (ret) { - dev_err(dev, "device_add failed: %d\n", ret); - put_device(dev); - } - return ret; free_ept_ida: