From patchwork Mon Oct 2 11:59:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 114578 Delivered-To: patch@linaro.org Received: by 10.140.22.163 with SMTP id 32csp593160qgn; Mon, 2 Oct 2017 04:59:27 -0700 (PDT) X-Received: by 10.98.42.77 with SMTP id q74mr14068205pfq.272.1506945567577; Mon, 02 Oct 2017 04:59:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1506945567; cv=none; d=google.com; s=arc-20160816; b=IQNBcEa0nGgxXLxrzSNr5nYxylaggGmwCVGzxKePxaiOj3vQATfMjmryYf9vwsMu20 GCMhePS0z/Rix5BzG9WTgQezKOgUPHXpAujobEJvf6gUr9oOwFCeOTbKJh6ubOJdn3JP cZdo3jytTTr8pnGe+hLjzGiUYKtJrG+431T6qQG94LoOGdi1h7G7THPnlk5YyrZV3wTO wlJg2Lw7Oc1fUz7VPYRyHm3535ZkBWJ2s5/TTO/3J7eRue/Msw2mrdh/sdPFk4li3dor NES/AMGyCkfC5Pr/1h/h5xyzkGk9tIPK3DLeqC1yl/LqpHLBZJbtrbsiwONUD/vYFQ7W J++w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=4+b/xzh7+iZTnoyHIyI7iQq48jRoZR0H6cJSOgDc5x0=; b=tK0lamsXfY34Ei3teHUUCABvVUzBuMKpgtCaG/MXEEgRom87wf9Ew9rX4+haDdMM1z Qqh9lIb8XRu69BAHoV3D16cS2vJEW4wQuUcE+wd8CdkjCyP9fQ0bzytZYJIaPLvNapU2 UTESwIbh8KmvBY3mAc3BYTZ05UBd0sZI5sHygjtiTGrZFdHhewaB7TTZO7Gci/uyWKbc EdFAkNWF1/iWfPbLc9FGwhU07ERn3vPt7GgOwfU/Ab8JlRLjwjuoSOTAoO2X0mml0E/l PTxzd+JpxS2UV9CA3rjtikIdRAF+BmEz/5oLV4MTJeVBOKs0K0JBAuLBXANwceqpEjMG QtDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=E3YHUf/H; spf=pass (google.com: best guess record for domain of linux-mmc-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-mmc-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u3si402548plb.302.2017.10.02.04.59.26; Mon, 02 Oct 2017 04:59:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-mmc-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=E3YHUf/H; spf=pass (google.com: best guess record for domain of linux-mmc-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-mmc-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750878AbdJBL70 (ORCPT + 6 others); Mon, 2 Oct 2017 07:59:26 -0400 Received: from mail-lf0-f52.google.com ([209.85.215.52]:44669 "EHLO mail-lf0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750788AbdJBL7Z (ORCPT ); Mon, 2 Oct 2017 07:59:25 -0400 Received: by mail-lf0-f52.google.com with SMTP id l196so5756262lfl.1 for ; Mon, 02 Oct 2017 04:59:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=PQs+4UktTSxe+rReF1rfY5PzZ5fjLa6lnfIM3vOesWY=; b=E3YHUf/HkxRR3x7TCkduMENYiBaTpAbY8SS/6yyd8X/M7C+9Nf0SiiEM66fn7IMTXa Hs/o72CkC1uscTLFFDUEosvVunI+f+RIVY9jv2JH06W4V7bsCxiYJAC2mwIWQSw2pAdT YjZDiK/R12SGZ9RcvT6/ayEwnfy7VHAMB89/0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=PQs+4UktTSxe+rReF1rfY5PzZ5fjLa6lnfIM3vOesWY=; b=QNgaaQrAqdmuUn8/nnrtREjX71UfQ/ZCx/FVOe2Ffk6xzIqX/jEFTZP/mFmMogbteg HrBu+grwjm5kZgNi2ASatpq+Mr9Nnj8y0K/pxdvhZAeCVSpcXvOBRuK/SOEt4iCm/bFZ 32SnaofELFQgLp9K70ZODhbkzsrWBWDyPPL8jsj+iYlgHj1qCd7HnQ/iK8OeRm8DN+SH 9TRlIXVdZ9T2MZRt0HvBpReP2hOxQld612KLo2XddkipcFeuHSaKRCJk3dGV1cSMuHbz rt3hRdGBkLdsbufdWI5/A9hNkV1/DOSlv3md8dV9KNlLePKdpCkJEWrNlVC9UyhKN+fW QL5w== X-Gm-Message-State: AHPjjUh4N/yisq3AWr54XscYbGv7PaBtlUfB05Pqeqz3MAHCXX9ldfou 6myA0yZqxd2Ea2pifdBcuiu878oRp7o= X-Google-Smtp-Source: AOwi7QBjfcIS+ys0CYBmhSftLb3dQU1WZNMWVmDaRbmQ/feffRFDdWuCL2QJ94w+UQVPvISXJgrITA== X-Received: by 10.25.37.81 with SMTP id l78mr3805092lfl.35.1506945563805; Mon, 02 Oct 2017 04:59:23 -0700 (PDT) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id g141sm1593616lfg.70.2017.10.02.04.59.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 02 Oct 2017 04:59:23 -0700 (PDT) From: Linus Walleij To: linux-mmc@vger.kernel.org, Ulf Hansson Cc: Adrian Hunter , Linus Walleij Subject: [PATCH] mmc: block: Fix bug when removing RPMB chardev Date: Mon, 2 Oct 2017 13:59:18 +0200 Message-Id: <20171002115918.11035-1-linus.walleij@linaro.org> X-Mailer: git-send-email 2.13.5 Sender: linux-mmc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org I forgot to account for the fact that the device core holds a reference to a device added with device_initialize() that need to be released with a corresponding put_device() to reach a 0 refcount at the end of the lifecycle. This led to a NULL pointer reference when freeing the device when e.g. unbidning the host device in sysfs. Fix this and use the device .release() callback to free the IDA and free:ing the memory used by the RPMB device. Before this patch: /sys/bus/amba/drivers/mmci-pl18x$ echo 80114000.sdi4_per2 > unbind [ 29.797332] mmc3: card 0001 removed [ 29.810791] Unable to handle kernel NULL pointer dereference at virtual address 00000050 [ 29.818878] pgd = de70c000 [ 29.821624] [00000050] *pgd=1e70a831, *pte=00000000, *ppte=00000000 [ 29.827911] Internal error: Oops: 17 [#1] PREEMPT SMP ARM [ 29.833282] Modules linked in: [ 29.836334] CPU: 1 PID: 154 Comm: sh Not tainted 4.14.0-rc3-00039-g83318e309566-dirty #736 [ 29.844604] Hardware name: ST-Ericsson Ux5x0 platform (Device Tree Support) [ 29.851562] task: de572700 task.stack: de742000 [ 29.856079] PC is at kernfs_find_ns+0x8/0x100 [ 29.860443] LR is at kernfs_find_and_get_ns+0x30/0x48 After this patch: /sys/bus/amba/drivers/mmci-pl18x$ echo 80005000.sdi4_per2 > unbind [ 20.623382] mmc3: card 0001 removed [ 20.627288] mmc_rpmb mmcblk3rpmb: removed Fixes: 0ab0c7c0e65a ("mmc: block: Convert RPMB to a character device") Reported-by: Adrian Hunter Signed-off-by: Linus Walleij --- drivers/mmc/core/block.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) -- 2.13.5 -- To unsubscribe from this list: send the line "unsubscribe linux-mmc" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c index c29dbcec7c61..2033a71bacc3 100644 --- a/drivers/mmc/core/block.c +++ b/drivers/mmc/core/block.c @@ -2329,6 +2329,14 @@ static const struct file_operations mmc_rpmb_fileops = { #endif }; +static void mmc_blk_rpmb_device_release(struct device *dev) +{ + struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev); + + dev_info(dev, "removed\n"); + ida_simple_remove(&mmc_rpmb_ida, rpmb->id); + kfree(rpmb); +} static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, struct mmc_blk_data *md, @@ -2371,6 +2379,8 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, goto out_remove_ida; } + /* From this point, the .release() function cleans up the device */ + rpmb->dev.release = mmc_blk_rpmb_device_release; list_add(&rpmb->node, &md->rpmbs); string_get_size((u64)size, 512, STRING_UNITS_2, @@ -2384,17 +2394,22 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, return 0; out_remove_ida: + put_device(&rpmb->dev); ida_simple_remove(&mmc_rpmb_ida, rpmb->id); kfree(rpmb); return ret; } static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb) + { cdev_device_del(&rpmb->chrdev, &rpmb->dev); - device_del(&rpmb->dev); - ida_simple_remove(&mmc_rpmb_ida, rpmb->id); - kfree(rpmb); + /* + * Unless something is holding a reference to the device, this + * drops the last reference and triggers the device to cleanup + * and calls the .remove() callback. + */ + put_device(&rpmb->dev); } /* MMC Physical partitions consist of two boot partitions and