linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Qiang Zhao <qiang.zhao@nxp.com>, Li Yang <leoyang.li@nxp.com>,
	Christophe Leroy <christophe.leroy@c-s.fr>
Cc: linuxppc-dev@lists.ozlabs.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, Scott Wood <oss@buserror.net>,
	Timur Tabi <timur@kernel.org>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>
Subject: [PATCH v5 39/48] soc: fsl: qe: refactor cpm_muram_alloc_common to prevent BUG on error path
Date: Mon, 18 Nov 2019 12:23:15 +0100	[thread overview]
Message-ID: <20191118112324.22725-40-linux@rasmusvillemoes.dk> (raw)
In-Reply-To: <20191118112324.22725-1-linux@rasmusvillemoes.dk>

If the kmalloc() fails, we try to undo the gen_pool allocation we've
just done. Unfortunately, start has already been modified to subtract
the GENPOOL_OFFSET bias, so we're freeing something that very likely
doesn't exist in the gen_pool, meaning we hit the

 kernel BUG at lib/genalloc.c:399!
 Internal error: Oops - BUG: 0 [#1] PREEMPT SMP ARM
 ...
 [<803fd0e8>] (gen_pool_free) from [<80426bc8>] (cpm_muram_alloc_common+0xb0/0xc8)
 [<80426bc8>] (cpm_muram_alloc_common) from [<80426c28>] (cpm_muram_alloc+0x48/0x80)
 [<80426c28>] (cpm_muram_alloc) from [<80428214>] (ucc_slow_init+0x110/0x4f0)
 [<80428214>] (ucc_slow_init) from [<8044a718>] (qe_uart_request_port+0x3c/0x1d8)

(this was tested by just injecting a random failure by adding
"|| (get_random_int()&7) == 0" to the "if (!entry)" condition).

Refactor the code so we do the kmalloc() first, meaning that's the
thing that needs undoing in case gen_pool_alloc_algo() then
fails. This allows a later cleanup to move the locking from the
callers into the _common function, keeping the kmalloc() out of the
critical region and then, hopefully (if all the muram_alloc callers
allow) change it to a GFP_KERNEL allocation.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 drivers/soc/fsl/qe/qe_common.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/drivers/soc/fsl/qe/qe_common.c b/drivers/soc/fsl/qe/qe_common.c
index dcb267567d76..a81a1a79f1ca 100644
--- a/drivers/soc/fsl/qe/qe_common.c
+++ b/drivers/soc/fsl/qe/qe_common.c
@@ -119,23 +119,21 @@ static s32 cpm_muram_alloc_common(unsigned long size,
 	struct muram_block *entry;
 	s32 start;
 
+	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
+	if (!entry)
+		return -ENOMEM;
 	start = gen_pool_alloc_algo(muram_pool, size, algo, data);
-	if (!start)
-		goto out2;
+	if (!start) {
+		kfree(entry);
+		return -ENOMEM;
+	}
 	start = start - GENPOOL_OFFSET;
 	memset_io(cpm_muram_addr(start), 0, size);
-	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
-	if (!entry)
-		goto out1;
 	entry->start = start;
 	entry->size = size;
 	list_add(&entry->head, &muram_block_list);
 
 	return start;
-out1:
-	gen_pool_free(muram_pool, start, size);
-out2:
-	return -ENOMEM;
 }
 
 /*
-- 
2.23.0


  parent reply	other threads:[~2019-11-18 11:24 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-18 11:22 [PATCH v5 00/48] QUICC Engine support on ARM, ARM64, PPC64 Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 01/48] soc: fsl: qe: remove space-before-tab Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 02/48] soc: fsl: qe: drop volatile qualifier of struct qe_ic::regs Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 03/48] soc: fsl: qe: rename qe_(clr/set/clrset)bit* helpers Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 04/48] soc: fsl: qe: introduce qe_io{read,write}* wrappers Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 05/48] soc: fsl: qe: avoid ppc-specific io accessors Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 06/48] soc: fsl: qe: replace spin_event_timeout by readx_poll_timeout_atomic Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 07/48] soc: fsl: qe: qe.c: guard use of pvr_version_is() with CONFIG_PPC32 Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 08/48] soc: fsl: qe: drop unneeded #includes Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 09/48] soc: fsl: qe: drop assign-only high_active in qe_ic_init Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 10/48] soc: fsl: qe: remove pointless sysfs registration in qe_ic.c Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 11/48] soc: fsl: qe: use qe_ic_cascade_{low,high}_mpic also on 83xx Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 12/48] soc: fsl: qe: move calls of qe_ic_init out of arch/powerpc/ Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 13/48] powerpc/83xx: remove mpc83xx_ipic_and_qe_init_IRQ Rasmus Villemoes
2019-11-20 17:59   ` Li Yang
2019-11-22  7:23     ` Scott Wood
2019-11-18 11:22 ` [PATCH v5 14/48] powerpc/85xx: remove mostly pointless mpc85xx_qe_init() Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 15/48] soc: fsl: qe: move qe_ic_cascade_* functions to qe_ic.c Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 16/48] soc: fsl: qe: rename qe_ic_cascade_low_mpic -> qe_ic_cascade_low Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 17/48] soc: fsl: qe: remove unused qe_ic_set_* functions Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 18/48] soc: fsl: qe: don't use NO_IRQ in qe_ic.c Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 19/48] soc: fsl: qe: make qe_ic_get_{low,high}_irq static Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 20/48] soc: fsl: qe: simplify qe_ic_init() Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 21/48] soc: fsl: qe: merge qe_ic.h headers into qe_ic.c Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 22/48] soc: fsl: qe: qe.c: use of_property_read_* helpers Rasmus Villemoes
2019-11-18 11:22 ` [PATCH v5 23/48] soc: fsl: qe: qe_io.c: don't open-code of_parse_phandle() Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 24/48] soc: fsl: qe: qe_io.c: access device tree property using be32_to_cpu Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 25/48] soc: fsl: qe: qe_io.c: use of_property_read_u32() in par_io_init() Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 26/48] soc: fsl: move cpm.h from powerpc/include/asm to include/soc/fsl Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 27/48] soc/fsl/qe/qe.h: update include path for cpm.h Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 28/48] serial: ucc_uart: explicitly include soc/fsl/cpm.h Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 29/48] serial: ucc_uart: replace ppc-specific IO accessors Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 30/48] serial: ucc_uart: factor out soft_uart initialization Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 31/48] serial: ucc_uart: stub out soft_uart_init for !CONFIG_PPC32 Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 32/48] serial: ucc_uart: use of_property_read_u32() in ucc_uart_probe() Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 33/48] serial: ucc_uart: limit brg-frequency workaround to PPC32 Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 34/48] serial: ucc_uart: access __be32 field using be32_to_cpu Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 35/48] soc: fsl: qe: change return type of cpm_muram_alloc() to s32 Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 36/48] soc: fsl: qe: make cpm_muram_free() return void Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 37/48] soc: fsl: qe: make cpm_muram_free() ignore a negative offset Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 38/48] soc: fsl: qe: drop broken lazy call of cpm_muram_init() Rasmus Villemoes
2019-11-18 11:23 ` Rasmus Villemoes [this message]
2019-11-18 11:23 ` [PATCH v5 40/48] soc: fsl: qe: avoid IS_ERR_VALUE in ucc_slow.c Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 41/48] soc: fsl: qe: drop use of IS_ERR_VALUE in qe_sdma_init() Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 42/48] soc: fsl: qe: drop pointless check " Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 43/48] soc: fsl: qe: avoid IS_ERR_VALUE in ucc_fast.c Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 44/48] net/wan/fsl_ucc_hdlc: avoid use of IS_ERR_VALUE() Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 45/48] net/wan/fsl_ucc_hdlc: fix reading of __be16 registers Rasmus Villemoes
2019-11-20 17:52   ` Li Yang
2019-11-18 11:23 ` [PATCH v5 46/48] net/wan/fsl_ucc_hdlc: reject muram offsets above 64K Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 47/48] net: ethernet: freescale: make UCC_GETH explicitly depend on PPC32 Rasmus Villemoes
2019-11-18 11:23 ` [PATCH v5 48/48] soc: fsl: qe: remove PPC32 dependency from CONFIG_QUICC_ENGINE Rasmus Villemoes
2019-11-20  3:55 ` [PATCH v5 00/48] QUICC Engine support on ARM, ARM64, PPC64 Timur Tabi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191118112324.22725-40-linux@rasmusvillemoes.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=christophe.leroy@c-s.fr \
    --cc=leoyang.li@nxp.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=oss@buserror.net \
    --cc=qiang.zhao@nxp.com \
    --cc=timur@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).