From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F14FF4DD6F8 for ; Tue, 19 May 2026 12:46:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779194781; cv=none; b=O2g42gWaQbHak2p1yNPmbvkKRTEehp1brjqPn+j9bHtydKv/hdk7MF2BMh7WHJ9mhp3e/eWEunPqUCqlQWc3VQZ3U3wr8fCwHiQ0qV7/QVFaddKSyrU2CqpOHW3jAi+4fA2sOiL72eZxhfv0iv8ftxlavjO/4l2Nt57ttumhmGs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779194781; c=relaxed/simple; bh=84beUtLLQ8PcshZOsdAjwjxa5oHqBAhexXLxGk9uJuQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=mgF4vlWDXv3d/Ms2it91QtGPxZbAjI4SeEj1oXytk5cd6UGpf2IajX28B3pXrTI5HM3ABf7Y8xAQkyqnCkvzdDtRa3Wx1NHcHWMQHJ+ETWGE85VHgqW9fxBPLmVCv6nOJrjkeW4Kifzo9k0jDDabUwj3Rn8QqR+9kv8BfU2mnX0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=h2xeRbfc; arc=none smtp.client-ip=209.85.221.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="h2xeRbfc" Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-44985f4ab0fso1832881f8f.0 for ; Tue, 19 May 2026 05:46:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1779194778; x=1779799578; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=n5zxrJzhz4zdnbyw+j6OnckVHPyDpmxi7LLakKPc0g0=; b=h2xeRbfccUqcwnPtqb4FLc7jgbS2e6HnXBPMW+AYv7rVq/lKYSanmlzl5glGLYTQ+v b9HqB0UtmeXkwZJh5kVXC7mh9/obmeyKHF/U479x2/HxMMSejXRaP8wi8UW06z7Y5Lrp FftiA9aDy9MdmeaPU8/gTqVXoYka7sz5/qAPLAHtYQSd/VWd3+URNV2DCi/HPVX0tUY0 ncqq5xCadUAlNDpRvjM17Qoe/ZXlCw2Wk8GGnF+Cnldv6Gw3RIS0esgDycmTve6pajeZ TMaTEkFLQltjhUIHf8oEtuG+5E265wdvyhWSULe8Bt7/yWW0T1ieKE51Qc7tbT/whd+p bJ6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779194778; x=1779799578; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=n5zxrJzhz4zdnbyw+j6OnckVHPyDpmxi7LLakKPc0g0=; b=EgOYrmKw5u8+VG+fH9umfWbmAgkvfkSfyRFjvT5pmPjYMfvpAzopD59XOXHJMw5BXA K81+pNy1eHoRFPY2O+30i+QIoJH5fKSzERS6pX4p5vc7YIBHgYKQSDFJJ5TnwuIZeiau p4kYjlVSEwVewznS5Y3KqHIxxHv8xKhMS72sETwYM5BmfS+0hCQImxrF9sDXFALUuHf0 odyFS25CYW1Br6nzYz6f1sojn7rZYsJE8w2/K5haIVJiziAI8O3u5C/cUEiFK3QkQckn Yl+swlBE7ULtqjOZuU4DhFNih8An/eAejQ22+K1VtUiimuzfbkK2L19RkbkwMRa3dPXv I8iw== X-Forwarded-Encrypted: i=1; AFNElJ9BwMlSmpPk3bgt8aT6hXRon3p1np7zHi05r+ktIF6jtchtwi3K5aXRHSZ3a53mzgzz2pNwdP49GPqNzpw=@vger.kernel.org X-Gm-Message-State: AOJu0YyqP3Uwk+emDkf6YCQ+O/ouTSXpbyJ+eD64SYFdKzKgcLnQM7jA DeULaNLnk6ZXOx/kQNGAjE24OckJkSEyKLVL34Kvtt+NMRBDjcWbHJptoku4XVj3yTA= X-Gm-Gg: Acq92OHlrmVtU6+ZU6q4qUBhMHtCpDgkJEjc9HMzmmrt05+3V9m94MnegYt0GbDS+jV oEwala0fgzlRMZn9kLDXL8Z5njj17hrK8ZrlyGufyBb8JRG9ON3GM+EViSQOiT/znKbX3+937Rl jAycFXbz3oVwhHm4ha68EcAxmq1PLNueefQCZiszTzFz0Uy3xlciOPZ+LxwrGh6xRX7VjnVVHi9 Vmp0MC/kj/zo7qA5kPIKxAcU3Ew4QC0qCpdwki9+lVt6my41jD900jNt2kmpwBu9BpZwULZAthS h3uvp8cc5LJYDxZjtidbY+05AA0cvTlWaF1QuAe+RhEWyF6T3cHArwcMEm+TIXsNCuJI3j3Wll2 o/NWeMNRlzuiPEsMgxMwa9lpBnlo+FKDtW9xdWCH4j9Q9gXTP60HguXND/O1V6BjZHQe9cJjoJT WDLgE4gSl3ZhGS16abJwtCg3MresXa7uWqsqBuy7c= X-Received: by 2002:a5d:49d2:0:b0:45e:739b:3e3b with SMTP id ffacd0b85a97d-45e739b3ed0mr14584891f8f.0.1779194778350; Tue, 19 May 2026 05:46:18 -0700 (PDT) Received: from [192.168.1.3] ([185.48.77.170]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45da0fe13a7sm47525460f8f.29.2026.05.19.05.46.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 19 May 2026 05:46:17 -0700 (PDT) Message-ID: <6925146f-7250-40be-908d-9a210472654f@linaro.org> Date: Tue, 19 May 2026 13:46:16 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] coresight: platform: defer connection counter increment until alloc succeeds To: Jie Gan Cc: coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Suzuki K Poulose , Mike Leach , Leo Yan , Alexander Shishkin , Tingwei Zhang References: <20260511-fix-ref-count-issue-v1-1-99d647810d3c@oss.qualcomm.com> Content-Language: en-US From: James Clark In-Reply-To: <20260511-fix-ref-count-issue-v1-1-99d647810d3c@oss.qualcomm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 11/05/2026 5:19 am, Jie Gan wrote: > coresight_add_out_conn() increments nr_outconns before calling > devm_krealloc_array() and again before devm_kmalloc(). If either > allocation fails, the counter is already bumped while the corresponding > array entry is NULL or uninitialized garbage. > > coresight_add_in_conn() has the same problem with nr_inconns and > devm_krealloc_array(). > > In both cases the probe returns -ENOMEM, which causes > coresight_get_platform_data() to call coresight_release_platform_data() > for cleanup. That function iterates up to nr_outconns (or nr_inconns) > entries and dereferences each pointer unconditionally, hitting the NULL > or garbage entry and panicking instead of failing gracefully. > > Fix by moving the counter increments to after all allocations succeed, > so the struct is always consistent on any error path. > > Fixes: 3d4ff657e454 ("coresight: Dynamically add connections") > Fixes: e3f4e68797a9 ("coresight: Store in-connections as well as out-connections") > Signed-off-by: Jie Gan Reviewed-by: James Clark > --- > drivers/hwtracing/coresight/coresight-platform.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/drivers/hwtracing/coresight/coresight-platform.c b/drivers/hwtracing/coresight/coresight-platform.c > index e337b6e2bf32..93c2d075cad6 100644 > --- a/drivers/hwtracing/coresight/coresight-platform.c > +++ b/drivers/hwtracing/coresight/coresight-platform.c > @@ -45,9 +45,8 @@ coresight_add_out_conn(struct device *dev, > } > } > > - pdata->nr_outconns++; > pdata->out_conns = > - devm_krealloc_array(dev, pdata->out_conns, pdata->nr_outconns, > + devm_krealloc_array(dev, pdata->out_conns, pdata->nr_outconns + 1, > sizeof(*pdata->out_conns), GFP_KERNEL); > if (!pdata->out_conns) > return ERR_PTR(-ENOMEM); > @@ -63,7 +62,8 @@ coresight_add_out_conn(struct device *dev, > * used right away. > */ > *conn = *new_conn; > - pdata->out_conns[pdata->nr_outconns - 1] = conn; > + pdata->out_conns[pdata->nr_outconns] = conn; > + pdata->nr_outconns++; > return conn; > } > EXPORT_SYMBOL_GPL(coresight_add_out_conn); > @@ -86,13 +86,13 @@ int coresight_add_in_conn(struct coresight_connection *out_conn) > return 0; > } > > - pdata->nr_inconns++; > pdata->in_conns = > - devm_krealloc_array(dev, pdata->in_conns, pdata->nr_inconns, > + devm_krealloc_array(dev, pdata->in_conns, pdata->nr_inconns + 1, > sizeof(*pdata->in_conns), GFP_KERNEL); > if (!pdata->in_conns) > return -ENOMEM; > - pdata->in_conns[pdata->nr_inconns - 1] = out_conn; > + pdata->in_conns[pdata->nr_inconns] = out_conn; > + pdata->nr_inconns++; > return 0; > } > EXPORT_SYMBOL_GPL(coresight_add_in_conn); > > --- > base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83 > change-id: 20260511-fix-ref-count-issue-7c44ce39700f > > Best regards,