public inbox for alsa-devel@alsa-project.org
 help / color / mirror / Atom feed
From: "Péter Ujfalusi" <peter.ujfalusi@linux.intel.com>
To: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: pierre-louis.bossart@linux.intel.com,
	alsa-devel@alsa-project.org, kai.vehmanen@linux.intel.com,
	cujomalainey@google.com, ranjani.sridharan@linux.intel.com,
	lgirdwood@gmail.com, broonie@kernel.org
Subject: Re: [PATCH v2] ASoC: SOF: ipc3-topology: Correct get_control_data for non bytes payload
Date: Wed, 27 Apr 2022 15:08:32 +0300	[thread overview]
Message-ID: <03a5d9ee-90ef-e4b9-5117-e59e81407453@linux.intel.com> (raw)
In-Reply-To: <YmkvBAgBrxRAMUcO@google.com>



On 27/04/2022 14:54, Sergey Senozhatsky wrote:
> On (22/04/27 14:33), Péter Ujfalusi wrote:
>>> @@ -2100,6 +2101,7 @@ static int sof_get_control_data(struct snd_soc_component *scomp,
>>>  				size_t *size)
>>>  {
>>>  	const struct snd_kcontrol_new *kc;
>>> +	struct sof_ipc_ctrl_data *cdata;
>>>  	struct soc_mixer_control *sm;
>>>  	struct soc_bytes_ext *sbe;
>>>  	struct soc_enum *se;
>>> @@ -2136,16 +2138,28 @@ static int sof_get_control_data(struct snd_soc_component *scomp,
>>>  			return -EINVAL;
>>>  		}
>>>  
>>> -		wdata[i].pdata = wdata[i].control->control_data->data;
>>> -		if (!wdata[i].pdata)
>>> -			return -EINVAL;
>>> +		cdata = wdata[i].control->control_data;
>>> +		if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES) {
>>> +			if ((void *)cdata->data == NULL)
>>
>> Is there a need for casting it to void*?
> 
> clang appears to be unhappy otherwise.
> 
> 	error: comparison of array 'cdata->data' equal to a null pointer is always false
> 
> Changing this into `if (!cdata->data)` is a little bit better as now
> 'always false' becomes 'always true'
> 
> 	error: address of array 'cdata->data' will always evaluate to 'true'

Hrm, uhm. clang is right. The check is (and was) bogus...

cdata->data is a pointer (to cdata->data[0]) which is always:
cdata + sizeof(struct sof_ipc_ctrl_data).
Checking if it is NULL or not is irrelevant and wrong. If we do not have
additional data then cdata->data points to memory which is outside of
the struct and it can be random data (might be 0, might not be).

I think we can just drop this check as we would not be here if
additional data was not allocated for the payload prior?

> 
>>> +				return -EINVAL;
>>>  
>>> -		/* make sure data is valid - data can be updated at runtime */
>>> -		if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES &&
>>> -		    wdata[i].pdata->magic != SOF_ABI_MAGIC)
>>> -			return -EINVAL;
>>> +			if (cdata->data->magic != SOF_ABI_MAGIC)
>>> +				return -EINVAL;
>>> +
>>> +			wdata[i].pdata = cdata->data;
>>
>> you want to save the cdata->data->data, so w/o the abi header stuff.
> 
> Oh... good catch!
> 
> I used `wdata[i].control->control_data->data` for tests, converted this
> to cdata the very last moment.
> 
> So something like this then
> 
> ---
>  sound/soc/sof/topology.c | 42 +++++++++++++++++++++++++++-------------
>  1 file changed, 29 insertions(+), 13 deletions(-)
> 
> diff --git a/sound/soc/sof/topology.c b/sound/soc/sof/topology.c
> index c1fc7bcf4eb5..60b4b0053e98 100644
> --- a/sound/soc/sof/topology.c
> +++ b/sound/soc/sof/topology.c
> @@ -50,7 +50,8 @@
>  struct sof_widget_data {
>  	int ctrl_type;
>  	int ipc_cmd;
> -	struct sof_abi_hdr *pdata;
> +	void *pdata;
> +	size_t pdata_size;
>  	struct snd_sof_control *control;
>  };
>  
> @@ -2100,6 +2101,7 @@ static int sof_get_control_data(struct snd_soc_component *scomp,
>  				size_t *size)
>  {
>  	const struct snd_kcontrol_new *kc;
> +	struct sof_ipc_ctrl_data *cdata;
>  	struct soc_mixer_control *sm;
>  	struct soc_bytes_ext *sbe;
>  	struct soc_enum *se;
> @@ -2136,16 +2138,28 @@ static int sof_get_control_data(struct snd_soc_component *scomp,
>  			return -EINVAL;
>  		}
>  
> -		wdata[i].pdata = wdata[i].control->control_data->data;
> -		if (!wdata[i].pdata)
> -			return -EINVAL;
> +		cdata = wdata[i].control->control_data;
> +		if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES) {
> +			if ((void *)cdata->data == NULL)
> +				return -EINVAL;
>  
> -		/* make sure data is valid - data can be updated at runtime */
> -		if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES &&
> -		    wdata[i].pdata->magic != SOF_ABI_MAGIC)
> -			return -EINVAL;
> +			if (cdata->data->magic != SOF_ABI_MAGIC)
> +				return -EINVAL;
> +
> +			wdata[i].pdata = cdata->data->data;
> +			wdata[i].pdata_size = cdata->data->size;
> +		} else {
> +			/* points to the control data union */
> +			wdata[i].pdata = cdata->chanv;
> +			/*
> +			 * wdata[i].control->size is calculated with struct_size
> +			 * and includes the size of struct sof_ipc_ctrl_data
> +			 */
> +			wdata[i].pdata_size = wdata[i].control->size -
> +					      sizeof(struct sof_ipc_ctrl_data);
> +		}
>  
> -		*size += wdata[i].pdata->size;
> +		*size += wdata[i].pdata_size;
>  
>  		/* get data type */
>  		switch (wdata[i].control->cmd) {
> @@ -2236,10 +2250,12 @@ static int sof_process_load(struct snd_soc_component *scomp, int index,
>  	 */
>  	if (ipc_data_size) {
>  		for (i = 0; i < widget->num_kcontrols; i++) {
> -			memcpy(&process->data + offset,
> -			       wdata[i].pdata->data,
> -			       wdata[i].pdata->size);
> -			offset += wdata[i].pdata->size;
> +			if (!wdata[i].pdata_size)
> +				continue;
> +
> +			memcpy(&process->data[offset], wdata[i].pdata,
> +			       wdata[i].pdata_size);
> +			offset += wdata[i].pdata_size;
>  		}
>  	}
>  

-- 
Péter

  reply	other threads:[~2022-04-27 12:09 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-27 10:52 [PATCH v2] ASoC: SOF: ipc3-topology: Correct get_control_data for non bytes payload Peter Ujfalusi
2022-04-27 11:20 ` Sergey Senozhatsky
2022-04-27 11:33   ` Péter Ujfalusi
2022-04-27 11:54     ` Sergey Senozhatsky
2022-04-27 12:08       ` Péter Ujfalusi [this message]
2022-04-27 12:31         ` Sergey Senozhatsky
2022-04-27 12:35           ` Péter Ujfalusi
2022-04-27 12:41             ` Sergey Senozhatsky

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=03a5d9ee-90ef-e4b9-5117-e59e81407453@linux.intel.com \
    --to=peter.ujfalusi@linux.intel.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=broonie@kernel.org \
    --cc=cujomalainey@google.com \
    --cc=kai.vehmanen@linux.intel.com \
    --cc=lgirdwood@gmail.com \
    --cc=pierre-louis.bossart@linux.intel.com \
    --cc=ranjani.sridharan@linux.intel.com \
    --cc=senozhatsky@chromium.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