From patchwork Fri Feb 12 13:15:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Hansson X-Patchwork-Id: 381827 Delivered-To: patches@linaro.org Received: by 2002:a02:b18a:0:0:0:0:0 with SMTP id t10csp2922292jah; Fri, 12 Feb 2021 05:15:52 -0800 (PST) X-Received: by 2002:a05:6512:344a:: with SMTP id j10mr1573834lfr.317.1613135752459; Fri, 12 Feb 2021 05:15:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613135752; cv=none; d=google.com; s=arc-20160816; b=yKC537KH24sNeSa2lUQS4h4ZqWKKfQD+WZF6iZ+/AtwtZSCBfnqATOPU5bW1eHkyyd UB/4+jE483PalVp4NGAQIlVglB/asSdfnMuTgbTW0YU4cwWmIjB993rvvO2DZwzuhf8E MIiJRI3xlIL1UxuZUQcljQBVOCz+eQHTmYZXZzetP2BBa/HMZzhrZ3u6n3kn4AXGtGbt BhQoNwGkKTxO9sdRHs1Ds8GzwjKmh410hpCsZsbne3h0nzClTSjA05ndNmUsZ4Y+cTMT beyY0ulQYbXsda9zUz3AgClR7pycGc3ueEFUru8xvwmeT0i5VqudVA1rVDeJfQ2wPHTE nlDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=kNfUMwXJsoKHQeXptuNd1laxxutYEsENQDplczxMvTQ=; b=edpzzU+73QdlCums77Tee39UiKyRbB2GdCYgVkEt+XRh+IVppZfvp5TIXDOOh7UGpb 5Frg6mayIr3cvsKj8luvGG6O/wtwZ/uBjgSkwAGEx2s/b7pQC8sgxeb7iTOexPuSqaIo FsK8qFxgWuxCvNhmGiNrqJFalU9Dt/PPlaKno6A3FK/VrqBSaIu+4HCJ36/IjMUhVbqB b7ks/8vUaZ74Yscufcq2rKXKDj73iOYoDuOYarSm7vq7t8wBSogzS0Gt/haS5C4sDq+j kyj7aeS2zfCNN3xPHPkfy+gpFK5pUmrraMUX1BPFFJNTeB1s7fp1fSe/3OYo7oK9Mw0O F4Zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=U9kMLvw3; spf=pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=ulf.hansson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id w9sor2030611lfd.3.2021.02.12.05.15.52 for (Google Transport Security); Fri, 12 Feb 2021 05:15:52 -0800 (PST) Received-SPF: pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=U9kMLvw3; spf=pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=ulf.hansson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kNfUMwXJsoKHQeXptuNd1laxxutYEsENQDplczxMvTQ=; b=U9kMLvw3+66zo/Cb7/7B/Wi9DPJ1U99GxpEBsXoOmEJprfaYXcEgHpkmqKQ0rr6d2y 0j2RpI96LKVXj13MN/JKUC/Z54BVszxQMLKZp/kXlAf9Q+Ty8/TwO52jwvaK9MEUSbjw evQ8U9UqkwwsH3cO2AlIIkPDN6558cjFCzMhdcn8bRVOfdN+MO19R4qG0nBrklS/ghbq admRir1yUYAxvX5JugAavEEUJdDCMvYlu9J7AcgPD9gjd6TNFWrHlxFuQPuboPKabObG FOvbvsizTmCJETdlac9jL34X41zzPtMpdgE5H8UsqaiVeeb7FtnwmZVrc5/PwbB47hPc T5Gw== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=kNfUMwXJsoKHQeXptuNd1laxxutYEsENQDplczxMvTQ=; b=f8q/UFLvZY5qwxtG/HA+KcRy2WNNS3wE+3F0sHh4ieJ7rhKn2l9hW8DSI4iywwvKlO 7VUHvae+fjT8yZacD5HFnDduf2QKhsaCiov6TdgapB4x2JMQSZVsr2RdHgJdBSg0lJif +VU0OOKuJVLpWPqxn24dyuIAloJQJKvdy/d3v1dSICwLfQb/BtQZt6DiQvyK44igCE2+ /VH0g+q9cx3A/QkY28aBazvC8oUh/Jp7szUIACRI9KaREhAKt8/ILHpmTDnzYESvrE3c gww91uI1/LbNTU/+a5M3Njt9jtn9NSOs5zK2ortSfSVXZ8Jq1hjNf2lHsrILHAmg0HHq UcoQ== X-Gm-Message-State: AOAM532pLnZ5faUOocgZiQdmNNG4UZZqXscBV+NU1Zs2ZQY3ymujcYnB i+vzRH0EazZatQ+k0HUcxSTdLiz1 X-Google-Smtp-Source: ABdhPJyxYldTkDjFUhavPJDFmo5SZNs+btDvyxYX7E1iHc4y/uxolj8FTbGLfyHIHdOMV46UosYMog== X-Received: by 2002:a19:78a:: with SMTP id 132mr1505818lfh.382.1613135752096; Fri, 12 Feb 2021 05:15:52 -0800 (PST) Return-Path: Received: from localhost.localdomain (h-98-128-181-28.NA.cust.bahnhof.se. [98.128.181.28]) by smtp.gmail.com with ESMTPSA id w21sm971448lfu.132.2021.02.12.05.15.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Feb 2021 05:15:50 -0800 (PST) From: Ulf Hansson To: linux-mmc@vger.kernel.org, Ulf Hansson Cc: Adrian Hunter , Linus Walleij , Wolfram Sang , Pierre Ossman , Russell King , linux-kernel@vger.kernel.org Subject: [PATCH 1/2] mmc: core: Drop superfluous validations in mmc_hw|sw_reset() Date: Fri, 12 Feb 2021 14:15:32 +0100 Message-Id: <20210212131532.236775-1-ulf.hansson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 The mmc_hw|sw_reset() APIs are designed to be called solely from upper layers, which means drivers that operates on top of the struct mmc_card, like the mmc block device driver and an SDIO functional driver. Additionally, as long as the struct mmc_host has a valid pointer to a struct mmc_card, the corresponding host->bus_ops pointer stays valid and assigned. For these reasons, let's drop the superfluous reference counting and the redundant validations in mmc_hw|sw_reset(). Signed-off-by: Ulf Hansson --- Note, ideally mmc_hw|sw_reset() shall be converted to take a struct mmc_card* as an in-parameter, rather than the current struct mmc_host*, but that is left to a future/separate change. --- drivers/mmc/core/block.c | 2 +- drivers/mmc/core/core.c | 21 +-------------------- 2 files changed, 2 insertions(+), 21 deletions(-) -- 2.25.1 Reviewed-by: Adrian Hunter diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c index b877f62df366..b7d3f5a73388 100644 --- a/drivers/mmc/core/block.c +++ b/drivers/mmc/core/block.c @@ -947,7 +947,7 @@ static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, md->reset_done |= type; err = mmc_hw_reset(host); /* Ensure we switch back to the correct partition */ - if (err != -EOPNOTSUPP) { + if (err) { struct mmc_blk_data *main_md = dev_get_drvdata(&host->card->dev); int part_err; diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c index 1136b859ddd8..2e41fa598bd6 100644 --- a/drivers/mmc/core/core.c +++ b/drivers/mmc/core/core.c @@ -2080,18 +2080,7 @@ int mmc_hw_reset(struct mmc_host *host) { int ret; - if (!host->card) - return -EINVAL; - - mmc_bus_get(host); - if (!host->bus_ops || host->bus_dead || !host->bus_ops->hw_reset) { - mmc_bus_put(host); - return -EOPNOTSUPP; - } - ret = host->bus_ops->hw_reset(host); - mmc_bus_put(host); - if (ret < 0) pr_warn("%s: tried to HW reset card, got error %d\n", mmc_hostname(host), ret); @@ -2104,18 +2093,10 @@ int mmc_sw_reset(struct mmc_host *host) { int ret; - if (!host->card) - return -EINVAL; - - mmc_bus_get(host); - if (!host->bus_ops || host->bus_dead || !host->bus_ops->sw_reset) { - mmc_bus_put(host); + if (!host->bus_ops->sw_reset) return -EOPNOTSUPP; - } ret = host->bus_ops->sw_reset(host); - mmc_bus_put(host); - if (ret) pr_warn("%s: tried to SW reset card, got error %d\n", mmc_hostname(host), ret); From patchwork Fri Feb 12 13:16:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Hansson X-Patchwork-Id: 381828 Delivered-To: patches@linaro.org Received: by 2002:a02:b18a:0:0:0:0:0 with SMTP id t10csp2922596jah; Fri, 12 Feb 2021 05:16:18 -0800 (PST) X-Received: by 2002:ac2:420e:: with SMTP id y14mr1496917lfh.370.1613135778079; Fri, 12 Feb 2021 05:16:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613135778; cv=none; d=google.com; s=arc-20160816; b=dZ7Qhb+YcVdpVLb9HafJuJ/VfYEiNGnT5oM4hhnKrGguFMDTHBcfgOyUm2IHP258LD 7dKBTpBxtxPsCcDh+PMT3CPDwOLi+HIO/zquXkWFioEW1p9lHFSvSOHRSCQp96muZbO7 /azqCVdFN7kiYw5n56dy5X2hCbbaSxjtdoqEnMZfwU3roTkslfBd6DMC2HM5odBzi3ez oXpJoJDegmJh93Q30t314XeqAkLW6V+nNXrt8e1Zo+Q9EhtUUoIduNjitSb1tWA9OcF2 mWHQ5fHgeUh/yvwzDZ7LLOEnEqjzbIQzjVIBEiGyK56Qd9jrCDDQboVxhxb3fHLVqZ1H zbmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=NTDYqyjjzX5B3Ni0UXMher++mMIetga5ni7QQVe5LLQ=; b=b0bOu13FVQre2eqzC3Oo/Fo2nMWaa51AK7XCl7mAm2RxhBc4Yfc4zBzszkML2jomdk uZShe3+PWQZgM0AefSufeacnXUTieGEUrhVW6Y7kIWFQZCXSrcGH5f0Iv6Hs5axJu99B yvPe+jcg0QtrLLUOKmf3Qb1EMb0Q5ApHTsfQm7a8TVDY3o5PJopMsuKNCp5G4I7Bkt1z a+Gujthh6gAc6hNwDL+1yjpUJ4o8yMeH2UDG3JYT1zeqzNv6cFs+hSl7pCKYpSGmHTgN 4iY/9g9t0QcjjxJVsBCqKvR/I368g3ofQ6g0VmGKih7PQ5TKudywJQJrItN5mBc8+Jip WrGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="CxT2M/AK"; spf=pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=ulf.hansson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id i11sor2060159lfu.93.2021.02.12.05.16.17 for (Google Transport Security); Fri, 12 Feb 2021 05:16:18 -0800 (PST) Received-SPF: pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="CxT2M/AK"; spf=pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=ulf.hansson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=NTDYqyjjzX5B3Ni0UXMher++mMIetga5ni7QQVe5LLQ=; b=CxT2M/AKPqAnl6tNXWKDY2gTbCh+bJPOmwa8X8SKUvIT/zp5cH5dDK3H4VHT5e5LCn /lc9k1evmF0fL8Jcpj1Qp/Ux72GNdfMTvgxT4+hEyOzfnCBjyayiZXpO6IE/csdLclWE RTmm9vBTEUeGcqQhmTIvPaSY9DQMtWUXGohYQfQfzze9UYxxS1i9EHg0V0MMcwriIfxB 7YiCgOjeTaUBUKtvowua1PPsX4QH2utYJc158zSIsxErpmoaMR/r7zFZJsmb+xyIRgJU 0eX9T05rzz6q9AauSsascFK7vZkx7+eYP7S38QcLG/Q3LoZPs8Qy7zAYTyeg/B4GcgVM Imtw== 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:mime-version :content-transfer-encoding; bh=NTDYqyjjzX5B3Ni0UXMher++mMIetga5ni7QQVe5LLQ=; b=PrRjetp4AiUu969SBYaO3nyHJIxjEzoUluDZw+Os4C0jcjjSl/71bfjuv41pyp+zYP WOg88rCKtkR+Of5Erb3upnHsByVyxMyzAqn+Eish/IMY0CmL1Sp5nS76kpC+i4z94W2u DwyNtBaN6vVu1HuY0l/9eSA7kCWBCJGIevmklXOwgxHLtGE81mzz2u+MqubSdyXjPq+N CgRwGXGwEV4wD0JO6wwvbVVqwXsoa1TXixJ7arMLtiMKpmkv+AQw3Eh4rEOOy6RDo+zM QImZw170HPJIVMGV8lqTNJGOjB5kAP5u9xv+BaG6S7kCYLH0x4n14Tqzj7y7AYtkoJxE doxw== X-Gm-Message-State: AOAM532ewSkO2EKpRnnwrP6gGSELrbfBJ/hB8ZhgkDCMawWR0BzVZuVN AfDEPoCsDBd3RQM1Rf+OjhndEnGO X-Google-Smtp-Source: ABdhPJzU5T31V+T4DlryUo3Y3NHW1UjZTtDXaf6onT/F3qTvmZQ7ZUHxdWdPrG6VzWVbLUhYWzhetg== X-Received: by 2002:ac2:58fb:: with SMTP id v27mr1523937lfo.371.1613135777664; Fri, 12 Feb 2021 05:16:17 -0800 (PST) Return-Path: Received: from localhost.localdomain (h-98-128-181-28.NA.cust.bahnhof.se. [98.128.181.28]) by smtp.gmail.com with ESMTPSA id a25sm1314400ljk.35.2021.02.12.05.16.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Feb 2021 05:16:16 -0800 (PST) From: Ulf Hansson To: linux-mmc@vger.kernel.org, Ulf Hansson Cc: Adrian Hunter , Linus Walleij , Wolfram Sang , Pierre Ossman , Russell King , linux-kernel@vger.kernel.org Subject: [PATCH 2/2] mmc: core: Drop reference counting of the bus_ops Date: Fri, 12 Feb 2021 14:16:10 +0100 Message-Id: <20210212131610.236843-1-ulf.hansson@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 When the mmc_rescan work is enabled for execution (host->rescan_disable), it's the only instance per mmc host that is allowed to set/clear the host->bus_ops pointer. Besides the mmc_rescan work, there are a couple of scenarios when the host->bus_ops pointer may be accessed. Typically, those can be described as as below: *) Upper mmc driver layers (like the mmc block device driver or an SDIO functional driver) needs to execute a host->bus_ops callback. This can be considered as safe without having to use some special locking mechanism, because they operate on top of the struct mmc_card. As long as there is a card to operate upon, the mmc core guarantees that there is a host->bus_ops assigned as well. Note that, upper layer mmc drivers are of course responsible to clean up from themselves from their ->remove() callbacks, otherwise things would fall apart anyways. **) Via the mmc host instance, we may need to force a removal of an inserted mmc card. This happens when a mmc host driver gets unbind, for example. In this case, we protect the host->bus_ops pointer from concurrent accesses, by disabling the mmc_rescan work upfront (host->rescan_disable). See mmc_stop_host() for example. This said, it seems like the reference counting of the host->bus_ops pointer at some point have become superfluous. As this is an old mechanism of the mmc core, it a bit difficult to digest the history of when that could have happened. However, let's drop the reference counting to avoid unnecessary code-paths and lockings. Cc: Pierre Ossman Cc: Russell King Signed-off-by: Ulf Hansson --- drivers/mmc/core/core.c | 89 ++-------------------------------------- include/linux/mmc/host.h | 2 - 2 files changed, 4 insertions(+), 87 deletions(-) -- 2.25.1 Reviewed-by: Adrian Hunter diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c index 2e41fa598bd6..860995eb548b 100644 --- a/drivers/mmc/core/core.c +++ b/drivers/mmc/core/core.c @@ -1377,63 +1377,13 @@ void mmc_power_cycle(struct mmc_host *host, u32 ocr) mmc_power_up(host, ocr); } -/* - * Cleanup when the last reference to the bus operator is dropped. - */ -static void __mmc_release_bus(struct mmc_host *host) -{ - WARN_ON(!host->bus_dead); - - host->bus_ops = NULL; -} - -/* - * Increase reference count of bus operator - */ -static inline void mmc_bus_get(struct mmc_host *host) -{ - unsigned long flags; - - spin_lock_irqsave(&host->lock, flags); - host->bus_refs++; - spin_unlock_irqrestore(&host->lock, flags); -} - -/* - * Decrease reference count of bus operator and free it if - * it is the last reference. - */ -static inline void mmc_bus_put(struct mmc_host *host) -{ - unsigned long flags; - - spin_lock_irqsave(&host->lock, flags); - host->bus_refs--; - if ((host->bus_refs == 0) && host->bus_ops) - __mmc_release_bus(host); - spin_unlock_irqrestore(&host->lock, flags); -} - /* * Assign a mmc bus handler to a host. Only one bus handler may control a * host at any given time. */ void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops) { - unsigned long flags; - - WARN_ON(!host->claimed); - - spin_lock_irqsave(&host->lock, flags); - - WARN_ON(host->bus_ops); - WARN_ON(host->bus_refs); - host->bus_ops = ops; - host->bus_refs = 1; - host->bus_dead = 0; - - spin_unlock_irqrestore(&host->lock, flags); } /* @@ -1441,18 +1391,7 @@ void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops) */ void mmc_detach_bus(struct mmc_host *host) { - unsigned long flags; - - WARN_ON(!host->claimed); - WARN_ON(!host->bus_ops); - - spin_lock_irqsave(&host->lock, flags); - - host->bus_dead = 1; - - spin_unlock_irqrestore(&host->lock, flags); - - mmc_bus_put(host); + host->bus_ops = NULL; } void _mmc_detect_change(struct mmc_host *host, unsigned long delay, bool cd_irq) @@ -2244,32 +2183,15 @@ void mmc_rescan(struct work_struct *work) host->trigger_card_event = false; } - mmc_bus_get(host); - /* Verify a registered card to be functional, else remove it. */ - if (host->bus_ops && !host->bus_dead) + if (host->bus_ops) host->bus_ops->detect(host); host->detect_change = 0; - /* - * Let mmc_bus_put() free the bus/bus_ops if we've found that - * the card is no longer present. - */ - mmc_bus_put(host); - mmc_bus_get(host); - /* if there still is a card present, stop here */ - if (host->bus_ops != NULL) { - mmc_bus_put(host); + if (host->bus_ops != NULL) goto out; - } - - /* - * Only we can add a new handler, so it's safe to - * release the lock here. - */ - mmc_bus_put(host); mmc_claim_host(host); if (mmc_card_is_removable(host) && host->ops->get_cd && @@ -2332,18 +2254,15 @@ void mmc_stop_host(struct mmc_host *host) /* clear pm flags now and let card drivers set them as needed */ host->pm_flags = 0; - mmc_bus_get(host); - if (host->bus_ops && !host->bus_dead) { + if (host->bus_ops) { /* Calling bus_ops->remove() with a claimed host can deadlock */ host->bus_ops->remove(host); mmc_claim_host(host); mmc_detach_bus(host); mmc_power_off(host); mmc_release_host(host); - mmc_bus_put(host); return; } - mmc_bus_put(host); mmc_claim_host(host); mmc_power_off(host); diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h index 927ba7566617..7b5555c705fd 100644 --- a/include/linux/mmc/host.h +++ b/include/linux/mmc/host.h @@ -423,7 +423,6 @@ struct mmc_host { /* group bitfields together to minimize padding */ unsigned int use_spi_crc:1; unsigned int claimed:1; /* host exclusively claimed */ - unsigned int bus_dead:1; /* bus has been released */ unsigned int doing_init_tune:1; /* initial tuning in progress */ unsigned int can_retune:1; /* re-tuning can be used */ unsigned int doing_retune:1; /* re-tuning in progress */ @@ -455,7 +454,6 @@ struct mmc_host { struct mmc_slot slot; const struct mmc_bus_ops *bus_ops; /* current bus driver */ - unsigned int bus_refs; /* reference counter */ unsigned int sdio_irqs; struct task_struct *sdio_irq_thread;