From patchwork Tue Oct 3 10:01:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 114685 Delivered-To: patch@linaro.org Received: by 10.140.22.163 with SMTP id 32csp1683831qgn; Tue, 3 Oct 2017 03:01:28 -0700 (PDT) X-Received: by 10.99.39.129 with SMTP id n123mr15125877pgn.240.1507024888060; Tue, 03 Oct 2017 03:01:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1507024888; cv=none; d=google.com; s=arc-20160816; b=WOl6iowQb0VB9duAwP5/ocy+CiBzDjigV6CWINJs7YYcWROrxwPbc5iqWyjSCLpQsT bwnhBm2uLsjA7FaxmkfaSheaufpTkwUrhp4KKdseq5cDnoRHYppAtkcv77yGBa94LYIS ofhCGERdx6eeVn+eay0+x++1IeGzBSNuzdT/uJT13pD2l6Pd1/BYvuIwmRTjOoVqSYnr 4ZO3lp/ZHb8JnD9IOofnlr24xJ6jLF9Rak/owCoLFHk4o++XRXiSEheRqR1CL/swn5pp xImNxXCUntEaoVFFPIzJ0xodeS6640956aG2MbfiY0blO2vWDuqGKYoe7fZMnO5YeUYf hCPw== 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=r/rt5bnHQ8k6hr8ZaoLk0yCTxOwU/RHlMly++v+19+A=; b=NnHdagaa/MZUXrRrcBj9nk9Cx05EE6+m0akq+1aN7W3OHJMSwtz0RCe1xn66fuFHZb ab5uizQEF9vnSuD5TAX8bD6XOWfYrC+F7C7RNqBE9WGluYEUqDAeaLu3+Wxd1xwL/pkU Xif4WUQHp8U/AN0UUFemWz/zLikg1gnKfA3LForvbnSK3MCN4gsyctIxSauPJZ71cL5y 1b3sIsQZprK2fO8keqVUCd0UCwAHRw5XqV5vsjufvdlLB5+2dgvnOae4Cm/6bB21tVlb TkRY4GGhj6a8Z2jXMTQgGGBG52z4+aJ8kQrMFq5d5LwTRqQPiabqSgNl6v170Tiks/aR ljRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=Qm08moeZ; 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 r6si10187028pls.298.2017.10.03.03.01.27; Tue, 03 Oct 2017 03:01:28 -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=Qm08moeZ; 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 S1751420AbdJCKB1 (ORCPT + 6 others); Tue, 3 Oct 2017 06:01:27 -0400 Received: from mail-lf0-f54.google.com ([209.85.215.54]:53146 "EHLO mail-lf0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751184AbdJCKB0 (ORCPT ); Tue, 3 Oct 2017 06:01:26 -0400 Received: by mail-lf0-f54.google.com with SMTP id b127so9030369lfe.9 for ; Tue, 03 Oct 2017 03:01: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=0Xj6FvHC7YKf4AesCZ72rOKHCyWbjLdgHLneh0CaQwA=; b=Qm08moeZz+L19hFYuC6rw0jr31sQ3KZKIwstsTQF0NDM2c5un6nDcu7hO6OEjfFDkl dmvVMpJ4kxzm+xyeRqpj/8KXWCUNEq7mNvdZg+UtgZ19Swse5GVZgqFQl9QA/m+E0ilE 2xPzQnq/UmAqIRX8mK+7HGhz38WjNbm9gzQ1w= 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=0Xj6FvHC7YKf4AesCZ72rOKHCyWbjLdgHLneh0CaQwA=; b=ZlpFSDY1bbmE2Bov5IlJPHKxO0819O2buGtTsFu6fh5NzeLaOeEL94SBjpHl070hIZ ED6meVjNOpURzTpteDpWDybJRjBJraow2dllVSay3ybkK2+ytKrOy0Lpg7raW/IGYD+k ZX1/+2h71zmSsRxwwuxdCDdTWsGR3m7pHtXd+f1bj7/Ub6KHgx2UNVFbPR7Xa+/hM5K8 8ZiotzlAUJJ9Ci432EurM/IwYcqU3rCJQ2tR/nuLWr4lQh1zPvhPKSTe1a1oHLKKRP1x /fHcB0tDFxkXQOmbjlkFVU9dcbwsu75pnCRkvkHesbN9ThwQlBCReDuwyR/3fgUug7uc uYSQ== X-Gm-Message-State: AHPjjUjEIxdH1a0D053hNOgRznBUo920EJtmMyRyGCzlnlpZ+XS1e/5s vgDBkxHJarqEU4gr381oiDOlitRGFCk= X-Google-Smtp-Source: AOwi7QB8D83c5WgOAr89AbaBR9H/RnTcFzPU+q24mZrYUAxUKsdrB4GjivBEBW12OILK9qNj5jWBaQ== X-Received: by 10.46.41.214 with SMTP id p83mr6084561ljp.173.1507024884777; Tue, 03 Oct 2017 03:01:24 -0700 (PDT) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id s26sm2813980ljd.77.2017.10.03.03.01.23 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 03 Oct 2017 03:01:24 -0700 (PDT) From: Linus Walleij To: linux-mmc@vger.kernel.org, Ulf Hansson Cc: Adrian Hunter , Linus Walleij Subject: [PATCH v2] mmc: block: Fix bug when removing RPMB chardev Date: Tue, 3 Oct 2017 12:01:19 +0200 Message-Id: <20171003100119.15418-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 Fixes: 0ab0c7c0e65a ("mmc: block: Convert RPMB to a character device") Reported-by: Adrian Hunter Signed-off-by: Linus Walleij --- 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 | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 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..4a79c11e2069 100644 --- a/drivers/mmc/core/block.c +++ b/drivers/mmc/core/block.c @@ -2329,6 +2329,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 +2354,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 +2368,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 +2378,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 +2393,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