Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp6875985ybc; Thu, 28 Nov 2019 07:01:18 -0800 (PST) X-Google-Smtp-Source: APXvYqyl8JILPDKU8XOnrMJy3lKqn5i7VjQh76ldISzjRsWpgLhVhRHiOAAMyGujkt6DkJMVrF+U X-Received: by 2002:a17:906:b289:: with SMTP id q9mr55145005ejz.183.1574953278644; Thu, 28 Nov 2019 07:01:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574953278; cv=none; d=google.com; s=arc-20160816; b=VK7RftoeRdaFxf1D42pU/R/Yrdkq7CahRvQ69zh6l0hSzcad2I9Z9HE9+GlZelFtWu 8+nyTEwLVCR0c16dF9QYfKaLmhXx+VEIe6vxl2o93RMRbmaGGKFCexLFgTioMTc0Af7m fdj7tUIBFP8crLpFgWdH7t+jG3z0h+wkVjfasY9A0fUODptHc0KVR3GXp77jfawfCGdk oKdPlQc92SMLjxUBm8tGjACdxBkjPprSwJcCZlokuewemxin8vFeCFgGTO07XupH3xqX HryCpgi0bKU4dP6ob2J1UV8qhERO7WEhwFLcn05DqzpuJyHP7u38gTZFDk5tk6dxvro0 cc3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=p72ig8otN7OnmiLAjVP1RCBOnmxplTNtC1X7TZOVWpw=; b=PdBtMKvylWATyCRcogbp/2qmf+2bz0Z0+b4jtWI/UK9mPZVHWHY1UGvsaXw4Dp6jcZ iBJJ2pOhGaquYzjyCRkuNHYz7u09zGzkkxYw2tZAVGOE2/M6IM6sJsafKoyRtZeWQVBr sDygvKHQtHzkgzZdoup1TvqRuczmwSMf23MnkauOaifRF589TXjZQCKilRbt8JgjkbNv hp8cysVhhO3kmpzgEo+cI7GvXlYYqHk4bwW0XFyR03qCyTEHwNPW/w4kXy4xh9TId3zd TzJA9noEki2CrwEBNfTv+WhrJmSiEnWPSqedRLuo8RDBWNFYbG4Bx8ERgSLAJ9F2kgGp Wdvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=OQQ2h+Re; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x12si12797582edr.217.2019.11.28.07.00.52; Thu, 28 Nov 2019 07:01:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=OQQ2h+Re; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727028AbfK1O54 (ORCPT + 99 others); Thu, 28 Nov 2019 09:57:56 -0500 Received: from mail-lj1-f196.google.com ([209.85.208.196]:41901 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727484AbfK1O5y (ORCPT ); Thu, 28 Nov 2019 09:57:54 -0500 Received: by mail-lj1-f196.google.com with SMTP id m4so28805659ljj.8 for ; Thu, 28 Nov 2019 06:57:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=p72ig8otN7OnmiLAjVP1RCBOnmxplTNtC1X7TZOVWpw=; b=OQQ2h+RevdyQm5rInmg2I8t2k1UuIX7x88JoTaMJl0mP3FKso/s85V9nZHyzR4ZhWk Bm+YZX5gRUEPF6MaHPscUB1QkrGXjl5izfcDhTmeqGqbsHLacR5wZ0c0MI3xxwYsCY/A Vkupe+rAVPCpON95rrklzc/93kyPsSxYsbMfs= 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=p72ig8otN7OnmiLAjVP1RCBOnmxplTNtC1X7TZOVWpw=; b=Nke70ut8o0u2jUSoa5ohor98XPbxd2X7Ch8MJfTOZIP/dJIM9APYd9KC+AU9vvqOwW 1RM3Zem+UyBx9vde9BxA54/qIgaNgiYjTQ7HbLSmx3E5/iNBIloagEoByITudaPLcdzk 3s+l45YKseD2kbb8gsM2Lx6nQZ2iVVG+KUIlypdiMfxbR8Sydo7APkhQKlQlDX9fUplf a28trHkbk6NdWVQI8PEiYlDBCnjlYX2m3MEknTgXo1cDy4fWYQ8uRexX95B8UuiAiimm 6DPxFqhjMeYD3w++moWYsuX6nECDo54S33gOGfcXe0q5IyeM8xelLbCjQ3Mstl+peco6 8veQ== X-Gm-Message-State: APjAAAWj2dh1VvbN9QHtbAS8hHqloMKCvorNPlqMstIWVVaUEWufYBLT MRTBpbd71jBXlyLqDl+Q5zYejA== X-Received: by 2002:a2e:6e07:: with SMTP id j7mr3590986ljc.18.1574953071630; Thu, 28 Nov 2019 06:57:51 -0800 (PST) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id u2sm2456803lfl.18.2019.11.28.06.57.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Nov 2019 06:57:51 -0800 (PST) From: Rasmus Villemoes To: Qiang Zhao , Li Yang , Christophe Leroy Cc: linuxppc-dev@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Scott Wood , Timur Tabi , Rasmus Villemoes Subject: [PATCH v6 35/49] soc: fsl: qe: change return type of cpm_muram_alloc() to s32 Date: Thu, 28 Nov 2019 15:55:40 +0100 Message-Id: <20191128145554.1297-36-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191128145554.1297-1-linux@rasmusvillemoes.dk> References: <20191128145554.1297-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are a number of problems with cpm_muram_alloc() and its callers. Most callers assign the return value to some variable and then use IS_ERR_VALUE to check for allocation failure. However, when that variable is not sizeof(long), this leads to warnings - and it is indeed broken to do e.g. u32 foo = cpm_muram_alloc(); if (IS_ERR_VALUE(foo)) on a 64-bit platform, since the condition foo >= (unsigned long)-ENOMEM is tautologically false. There are also callers that ignore the possibility of error, and then there are those that check for error by comparing the return value to 0... One could fix that by changing all callers to store the return value temporarily in an "unsigned long" and test that. However, use of IS_ERR_VALUE() is error-prone and should be restricted to things which are inherently long-sized (stuff in pt_regs etc.). Instead, let's aim for changing to the standard kernel style int foo = cpm_muram_alloc(); if (foo < 0) deal_with_it() some->where = foo; Changing the return type from unsigned long to s32 (aka signed int) doesn't change the value that gets stored into any of the callers' variables except if the caller was storing the result in a u64 _and_ the allocation failed, so in itself this patch should be a no-op. Another problem with cpm_muram_alloc() is that it can certainly validly return 0 - and except if some cpm_muram_alloc_fixed() call interferes, the very first cpm_muram_alloc() call will return just that. But that shows that both ucc_slow_free() and ucc_fast_free() are buggy, since they assume that a value of 0 means "that field was never allocated". We'll later change cpm_muram_free() to accept (and ignore) a negative offset, so callers can use a sentinel of -1 instead of 0 and just unconditionally call cpm_muram_free(). Reviewed-by: Timur Tabi Signed-off-by: Rasmus Villemoes --- drivers/soc/fsl/qe/qe_common.c | 29 ++++++++++++++++------------- include/soc/fsl/qe/qe.h | 16 ++++++++-------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/drivers/soc/fsl/qe/qe_common.c b/drivers/soc/fsl/qe/qe_common.c index 83e85e61669f..84c90105e588 100644 --- a/drivers/soc/fsl/qe/qe_common.c +++ b/drivers/soc/fsl/qe/qe_common.c @@ -32,7 +32,7 @@ static phys_addr_t muram_pbase; struct muram_block { struct list_head head; - unsigned long start; + s32 start; int size; }; @@ -110,13 +110,14 @@ int cpm_muram_init(void) * @algo: algorithm for alloc. * @data: data for genalloc's algorithm. * - * This function returns an offset into the muram area. + * This function returns a non-negative offset into the muram area, or + * a negative errno on failure. */ -static unsigned long cpm_muram_alloc_common(unsigned long size, - genpool_algo_t algo, void *data) +static s32 cpm_muram_alloc_common(unsigned long size, + genpool_algo_t algo, void *data) { struct muram_block *entry; - unsigned long start; + s32 start; if (!muram_pool && cpm_muram_init()) goto out2; @@ -137,7 +138,7 @@ static unsigned long cpm_muram_alloc_common(unsigned long size, out1: gen_pool_free(muram_pool, start, size); out2: - return (unsigned long)-ENOMEM; + return -ENOMEM; } /* @@ -145,13 +146,14 @@ static unsigned long cpm_muram_alloc_common(unsigned long size, * @size: number of bytes to allocate * @align: requested alignment, in bytes * - * This function returns an offset into the muram area. + * This function returns a non-negative offset into the muram area, or + * a negative errno on failure. * Use cpm_dpram_addr() to get the virtual address of the area. * Use cpm_muram_free() to free the allocation. */ -unsigned long cpm_muram_alloc(unsigned long size, unsigned long align) +s32 cpm_muram_alloc(unsigned long size, unsigned long align) { - unsigned long start; + s32 start; unsigned long flags; struct genpool_data_align muram_pool_data; @@ -168,7 +170,7 @@ EXPORT_SYMBOL(cpm_muram_alloc); * cpm_muram_free - free a chunk of multi-user ram * @offset: The beginning of the chunk as returned by cpm_muram_alloc(). */ -int cpm_muram_free(unsigned long offset) +int cpm_muram_free(s32 offset) { unsigned long flags; int size; @@ -194,13 +196,14 @@ EXPORT_SYMBOL(cpm_muram_free); * cpm_muram_alloc_fixed - reserve a specific region of multi-user ram * @offset: offset of allocation start address * @size: number of bytes to allocate - * This function returns an offset into the muram area + * This function returns @offset if the area was available, a negative + * errno otherwise. * Use cpm_dpram_addr() to get the virtual address of the area. * Use cpm_muram_free() to free the allocation. */ -unsigned long cpm_muram_alloc_fixed(unsigned long offset, unsigned long size) +s32 cpm_muram_alloc_fixed(unsigned long offset, unsigned long size) { - unsigned long start; + s32 start; unsigned long flags; struct genpool_data_fixed muram_pool_data_fixed; diff --git a/include/soc/fsl/qe/qe.h b/include/soc/fsl/qe/qe.h index 521fa3a177e0..f589ae3f1216 100644 --- a/include/soc/fsl/qe/qe.h +++ b/include/soc/fsl/qe/qe.h @@ -98,26 +98,26 @@ static inline void qe_reset(void) {} int cpm_muram_init(void); #if defined(CONFIG_CPM) || defined(CONFIG_QUICC_ENGINE) -unsigned long cpm_muram_alloc(unsigned long size, unsigned long align); -int cpm_muram_free(unsigned long offset); -unsigned long cpm_muram_alloc_fixed(unsigned long offset, unsigned long size); +s32 cpm_muram_alloc(unsigned long size, unsigned long align); +int cpm_muram_free(s32 offset); +s32 cpm_muram_alloc_fixed(unsigned long offset, unsigned long size); void __iomem *cpm_muram_addr(unsigned long offset); unsigned long cpm_muram_offset(void __iomem *addr); dma_addr_t cpm_muram_dma(void __iomem *addr); #else -static inline unsigned long cpm_muram_alloc(unsigned long size, - unsigned long align) +static inline s32 cpm_muram_alloc(unsigned long size, + unsigned long align) { return -ENOSYS; } -static inline int cpm_muram_free(unsigned long offset) +static inline int cpm_muram_free(s32 offset) { return -ENOSYS; } -static inline unsigned long cpm_muram_alloc_fixed(unsigned long offset, - unsigned long size) +static inline s32 cpm_muram_alloc_fixed(unsigned long offset, + unsigned long size) { return -ENOSYS; } -- 2.23.0