From patchwork Thu Apr 30 09:16:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Hansson X-Patchwork-Id: 185883 Delivered-To: patches@linaro.org Received: by 2002:a92:3d9a:0:0:0:0:0 with SMTP id k26csp1839099ilf; Thu, 30 Apr 2020 02:17:01 -0700 (PDT) X-Received: by 2002:a2e:9bc4:: with SMTP id w4mr1678284ljj.178.1588238221701; Thu, 30 Apr 2020 02:17:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588238221; cv=none; d=google.com; s=arc-20160816; b=AMyPvLYYRg/XwCEDGSoorM1DOj83usyl02SKKX0CXA1+yVDSn7vfRvFEoRaW2GxB0t 0QgRYzYmCbYJCVSIWEyhl5csDq+I27rSMPrinzzH1gGjkxBhtwMOHtoRrknHFTE6L9G4 BQgX/lRwletxzE5iKbOM4NjU1CRKGVluPb5kQ7LpTJ6k4YjUd7Oe9MLXkPcFTBQpy77v +mcWV6nbc5xFCS4eFSYa8Pi/kE1LEbRZd1UIgQ/oSCVOjIN/0ZKPqNemWCwoWelPjak4 /BphjhRtp25F1FCdO/vF5NT3RQxUxVkCA1FFPaW2bqeyo5KLsRxKN0QV/T32HDfzElGj Z5kw== 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=89RWgm3Up3ro7S0Yk5yseNEuoMg10aAHaWqeBrHIt/g=; b=S+bcfNqjhXPlFnMui70GRqG+xxyIxe0G3uBiEBn90OyRBXn+x2V8m38tVS3YRlBcHO r1yFc5XlfelT067Qu3oX+PCchiG7PjMqQgqlWrwd2pa9M4BN4TGNmFZQrR1pnTjTuJKR 7pr6gTkbTysjK8kUH2+1j8a6DWy4FAe0OdGmyOldUROM4m3601v33g8oSTaw+pweP3o+ IOa0/uExVXYhDgjyLSt1LDkySxJPOVDJcogNjhJYAgqZcaJa/kFeaGn4hNlLJXNlnDnt OFR3VuaTRZRo2V+l7kkc5IFTJkToLkJUN4nE4d04Pbyp9VcyTXR6nXdzmkSpWZJtjKer HuTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Gy1jjxtW; spf=pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.65 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-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id b16sor3662509ljk.10.2020.04.30.02.17.01 for (Google Transport Security); Thu, 30 Apr 2020 02:17:01 -0700 (PDT) Received-SPF: pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Gy1jjxtW; spf=pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.65 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=89RWgm3Up3ro7S0Yk5yseNEuoMg10aAHaWqeBrHIt/g=; b=Gy1jjxtW2kqnP8wl1z0ZePoqRxHvAmq/BvkprWs+HY3KgER1UNhmbkrUE7RD0LPgKY uQtnZBz1J2mSeu+eT0etLN/FfcwGvsrKL4Nrx5zOfWwpjtrdUrOmR5LvHJTno8WFY1k3 x5OrsN21NVJFhFWc6Jc1u0idkK5MhkHDkviaR36/JUS11GwLtlMbbWzWdLn1mqqlHkxk qoKHmX++K4izXqqhMg9SiOPnBduBWKoAjdSwopuGIeT+I7EmZMA9t0pQAKvTM/7TrJkQ 9akLeyXPv/jMDk5GbMhZIhuk1XGs8Ikdd5/BrlBTuB01ck/qt+eY5t8JYwesIAbi7Twf 47fA== 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=89RWgm3Up3ro7S0Yk5yseNEuoMg10aAHaWqeBrHIt/g=; b=uWek7etsA1qTXxGaGC8QXwboV/yRJR73W5qQfo+we8JZ9ha3nesuPCqZVWKph9pxZI 8Rescu7GmY5spOdfLdHlPkd9KR+PePWGN9VkqvRJxTssiX3dqGeLBRj3Unr7VTb59sZQ Tq/C7RriOSk7WaTS0ENzlVOK62IwwcIyW4NTz0Utqijktnqdq2Fgx3Z2TPnIHrVU7GoR +codoDXgEXgs4NIsTM7WuaeRYHCJH3SxshbzcW3w6BSEtAIatbb1dOQviOqfJmDIuuG9 Ej86TTT5suln02g+h3wOqUfUfTzIBouNc+FDIONpY+md44yG8IB1/GUsweU6Pb2bWU/0 0AgA== X-Gm-Message-State: AGi0PuYPMwBlYI0AD0pp1SVhXuYnAPr+T8YDJz7+yP5aUWWv0of0kzG7 VGpA+YATlzu/1R/6E5CTlGspiuoS X-Google-Smtp-Source: APiQypJmzs15jBI6elZWL1N97UEwVf7+JRqsPJCAid1U0Zizuu6vh6vZY2dgafQFqaaUTqSk+T5ZsA== X-Received: by 2002:a05:651c:50e:: with SMTP id o14mr1644456ljp.52.1588238220685; Thu, 30 Apr 2020 02:17:00 -0700 (PDT) Return-Path: Received: from localhost.localdomain (h-98-128-181-7.NA.cust.bahnhof.se. [98.128.181.7]) by smtp.gmail.com with ESMTPSA id x29sm4417818lfn.64.2020.04.30.02.16.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Apr 2020 02:17:00 -0700 (PDT) From: Ulf Hansson To: linux-mmc@vger.kernel.org, Ulf Hansson Cc: Adrian Hunter , Douglas Anderson , Matthias Kaehlcke , Shawn Lin , Yong Mao , Chaotian Jing , stable@vger.kernel.org Subject: [PATCH 2/4] mmc: sdio: Fix several potential memory leaks in mmc_sdio_init_card() Date: Thu, 30 Apr 2020 11:16:38 +0200 Message-Id: <20200430091640.455-3-ulf.hansson@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200430091640.455-1-ulf.hansson@linaro.org> References: <20200430091640.455-1-ulf.hansson@linaro.org> MIME-Version: 1.0 Over the years, the code in mmc_sdio_init_card() has grown to become quite messy. Unfortunate this has also lead to that several paths are leaking memory in form of an allocated struct mmc_card, which includes additional data, such as initialized struct device for example. Unfortunate, it's a too complex task find each offending commit. Therefore, this change fixes all memory leaks at once. Cc: Signed-off-by: Ulf Hansson --- drivers/mmc/core/sdio.c | 58 +++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 31 deletions(-) -- 2.20.1 diff --git a/drivers/mmc/core/sdio.c b/drivers/mmc/core/sdio.c index d35679e6e6aa..20eed28ea60d 100644 --- a/drivers/mmc/core/sdio.c +++ b/drivers/mmc/core/sdio.c @@ -584,7 +584,7 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, */ err = mmc_send_io_op_cond(host, ocr, &rocr); if (err) - goto err; + return err; /* * For SPI, enable CRC as appropriate. @@ -592,17 +592,15 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, if (mmc_host_is_spi(host)) { err = mmc_spi_set_crc(host, use_spi_crc); if (err) - goto err; + return err; } /* * Allocate card structure. */ card = mmc_alloc_card(host, NULL); - if (IS_ERR(card)) { - err = PTR_ERR(card); - goto err; - } + if (IS_ERR(card)) + return PTR_ERR(card); if ((rocr & R4_MEMORY_PRESENT) && mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) { @@ -610,19 +608,15 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO || memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) { - mmc_remove_card(card); - pr_debug("%s: Perhaps the card was replaced\n", - mmc_hostname(host)); - return -ENOENT; + err = -ENOENT; + goto mismatch; } } else { card->type = MMC_TYPE_SDIO; if (oldcard && oldcard->type != MMC_TYPE_SDIO) { - mmc_remove_card(card); - pr_debug("%s: Perhaps the card was replaced\n", - mmc_hostname(host)); - return -ENOENT; + err = -ENOENT; + goto mismatch; } } @@ -677,7 +671,7 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, if (!oldcard && card->type == MMC_TYPE_SD_COMBO) { err = mmc_sd_get_csd(host, card); if (err) - return err; + goto remove; mmc_decode_cid(card); } @@ -704,7 +698,12 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, mmc_set_timing(card->host, MMC_TIMING_SD_HS); } - goto finish; + if (oldcard) + mmc_remove_card(card); + else + host->card = card; + + return 0; } /* @@ -730,16 +729,14 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, goto remove; if (oldcard) { - int same = (card->cis.vendor == oldcard->cis.vendor && - card->cis.device == oldcard->cis.device); - mmc_remove_card(card); - if (!same) { - pr_debug("%s: Perhaps the card was replaced\n", - mmc_hostname(host)); - return -ENOENT; + if (card->cis.vendor == oldcard->cis.vendor && + card->cis.device == oldcard->cis.device) { + mmc_remove_card(card); + card = oldcard; + } else { + err = -ENOENT; + goto mismatch; } - - card = oldcard; } card->ocr = ocr_card; mmc_fixup_device(card, sdio_fixup_methods); @@ -800,16 +797,15 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, err = -EINVAL; goto remove; } -finish: - if (!oldcard) - host->card = card; + + host->card = card; return 0; +mismatch: + pr_debug("%s: Perhaps the card was replaced\n", mmc_hostname(host)); remove: - if (!oldcard) + if (oldcard != card) mmc_remove_card(card); - -err: return err; }