From patchwork Wed Oct 4 09:10:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 9984071 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7BF8560365 for ; Wed, 4 Oct 2017 09:10:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6C7A828AD6 for ; Wed, 4 Oct 2017 09:10:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5E2F828ADD; Wed, 4 Oct 2017 09:10:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C98EC28AD6 for ; Wed, 4 Oct 2017 09:10:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751945AbdJDJKQ (ORCPT ); Wed, 4 Oct 2017 05:10:16 -0400 Received: from mail-lf0-f41.google.com ([209.85.215.41]:55750 "EHLO mail-lf0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751925AbdJDJKO (ORCPT ); Wed, 4 Oct 2017 05:10:14 -0400 Received: by mail-lf0-f41.google.com with SMTP id p184so12363080lfe.12 for ; Wed, 04 Oct 2017 02:10:14 -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=SPi401xsojPEn5B89pbgZOkChjXmOSxjb8LCjcN18os=; b=REshNF0S9oEOyIrpeP5ApoHOqVJz21YWq+zPIhGDbQ7kbTjABjRd6acRH6IuESbLY4 2b5RKrc0a4maPZcsWWeZIVA0/XWG0I6ZyOYr2nRovsJxVs7jkLghiCP3eaPg0Zj/BZz5 DkfPbYXZdEaKyzqdiJYtT5qqaiAOGlpzcVop4= 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=SPi401xsojPEn5B89pbgZOkChjXmOSxjb8LCjcN18os=; b=s75RNkXJY4p+XkqoBZN6Fiwp5jMlTMysP2l9HANnqbHW6ODxjadejs5rbzamceTVM8 EhFlSKBdaea4x6Yp5lkcARXtoBHLTB7FdAI+hLiek17KjlW/uX9WwAiePMlz+XdlWnxh na1McTwElic2lcSChhnLk5inxsqJekJtzlLKooB/qO818vtAiO0vPh2oCukPPJR613ch xhbvsB9KiKYfzUwtrLmofHd6zd8kEZjHkmGzENT+5En9ExWI218YwQhaYdEqWb4aST8U DR8KgOD45p36b+WQ6pV8Jm+cDH7i+bAaHqWiPX6jnFblIoJp3knR9kmaGQC9dFxwGab6 TxCA== X-Gm-Message-State: AMCzsaVwHtIlsDR6kwMB7XjtSuAI9elguigul3opfLq7aCZBlHhD3qKI 5WyWJIazWnH454KMe+TvleCyUUGfj24= X-Google-Smtp-Source: AOwi7QBudQdWs/NvBcY0zriJvDqtircs5VSsd1idSVQQgigChCybhr6j0zS71Y6mSMnF2XQO45HIdw== X-Received: by 10.25.28.148 with SMTP id c142mr6966411lfc.34.1507108212914; Wed, 04 Oct 2017 02:10:12 -0700 (PDT) Received: from genomnajs.bredbandsbolaget.se (c-367171d5.014-348-6c756e10.cust.bredbandsbolaget.se. [213.113.113.54]) by smtp.gmail.com with ESMTPSA id v198sm2390242lfa.34.2017.10.04.02.10.11 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 04 Oct 2017 02:10:12 -0700 (PDT) From: Linus Walleij To: linux-mmc@vger.kernel.org, Ulf Hansson Cc: Adrian Hunter , Linus Walleij Subject: [PATCH v4] mmc: block: Fix bug when removing RPMB chardev Date: Wed, 4 Oct 2017 11:10:07 +0200 Message-Id: <20171004091007.26729-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 X-Virus-Scanned: ClamAV using ClamSMTP 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 Fixes: 0ab0c7c0e65a ("mmc: block: Convert RPMB to a character device") Reported-by: Adrian Hunter Signed-off-by: Linus Walleij Acked-by: Adrian Hunter --- ChangeLog v3->v4: - Also skip the lock now that we take it inside mmc_blk_get()/ mmc_blk_put() ChangeLog v2->v3: - Fix the block device reference counting: instead of manipulating md->usage directly, call the proper refcounting functions mmc_blk_get() and mmc_blk_put(). ChangeLog v1->v2: - Drop the print from removing the RPMB partition - Refactor the error path of the RPMB to use refcounting - Drop the verbose comment on refcounts --- drivers/mmc/core/block.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c index c29dbcec7c61..ea80ff4cd7f9 100644 --- a/drivers/mmc/core/block.c +++ b/drivers/mmc/core/block.c @@ -2298,9 +2298,7 @@ static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp) get_device(&rpmb->dev); filp->private_data = rpmb; - mutex_lock(&open_lock); - rpmb->md->usage++; - mutex_unlock(&open_lock); + mmc_blk_get(rpmb->md->disk); return nonseekable_open(inode, filp); } @@ -2311,9 +2309,7 @@ static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp) struct mmc_rpmb_data, chrdev); put_device(&rpmb->dev); - mutex_lock(&open_lock); - rpmb->md->usage--; - mutex_unlock(&open_lock); + mmc_blk_put(rpmb->md); return 0; } @@ -2329,6 +2325,13 @@ 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); + + 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, @@ -2347,8 +2350,10 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, return devidx; rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL); - if (!rpmb) + if (!rpmb) { + ida_simple_remove(&mmc_rpmb_ida, devidx); return -ENOMEM; + } snprintf(rpmb_name, sizeof(rpmb_name), "mmcblk%u%s", card->host->index, subname ? subname : ""); @@ -2359,6 +2364,7 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, rpmb->dev.bus = &mmc_rpmb_bus_type; rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id); rpmb->dev.parent = &card->dev; + rpmb->dev.release = mmc_blk_rpmb_device_release; device_initialize(&rpmb->dev); dev_set_drvdata(&rpmb->dev, rpmb); rpmb->md = md; @@ -2368,7 +2374,7 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev); if (ret) { pr_err("%s: could not add character device\n", rpmb_name); - goto out_remove_ida; + goto out_put_device; } list_add(&rpmb->node, &md->rpmbs); @@ -2383,18 +2389,16 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, return 0; -out_remove_ida: - ida_simple_remove(&mmc_rpmb_ida, rpmb->id); - kfree(rpmb); +out_put_device: + put_device(&rpmb->dev); 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); + put_device(&rpmb->dev); } /* MMC Physical partitions consist of two boot partitions and