Skip to content

Conversation

@libinyang
Copy link

…format count

Sanity check available_fmt->input_pin_fmts and available_fmt->output_pin_fmts before using them as some modules may not have input or output pins.

Signed-off-by: Libin Yang libin.yang@intel.com

@libinyang libinyang changed the title fixup! ASoC: SOF: ipc4-topology: Add new tokens for input/output pin … Sanity check available_fmt->input_pin_fmts and available_fmt->output_pin_fmts before using them Feb 10, 2023
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not group all the available_fmt->input_pin_fmts under one if? Along with the dev_dbg() few lines up?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have asked this in the original PR, but the comment got lost @ranj063:
Let's say we have a module with 2 input and 1 output pin.
sof_ipc4_init_audio_fmt() is called with the input_pin_fmts and num_input_formats, we find the second pin_fmt as a match (i=1).
output_pin_fmts is allocated for one pin, no? We access data outside (output_pin_fmts[1])?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 2 set of params: num_input_pins/num_output_pins and num_input_formats/num_output_formats. For WoV, there is no output pin. So it means, the num_output_pins and num_output_formats should be 0. In current WoV tplg, I set num_output_pins = 0 and num_output_formats = 1. This can work without this PR. (num_output_formats = 1)

However, I still think we need this patch for the sanity check in case someone set the num_input_formats or num_output_formats to 0 in tplg, which will cause kernel panic. Kernel should handle such exception.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@libinyang, if there is no output at all, then obs might be better to set to 0?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@libinyang, if there is no output at all, then obs might be better to set to 0?

The default value is 0 as we are using kzalloc()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, and what if we have 1 input pin and the i is 2? What if we have 1 output pin and the i is 2?

Signed-off-by: Libin Yang <libin.yang@intel.com>
@libinyang
Copy link
Author

PR is updated based on @ujfalusi 's comments.

return -EINVAL;
}

/* copy input format */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@libinyang, the commit subject and message is not too verbose...

sof_ipc4_dbg_audio_format(sdev->dev, &available_fmt->input_pin_fmts[i], 1);
/* set base_cfg obs */
if (available_fmt->output_pin_fmts)
base_config->obs = available_fmt->output_pin_fmts[i].buffer_size;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non existent output_pin_fmts does not warrant similar print as the input_pin_fmts?

dev_dbg(sdev->dev, "init input audio formats for %s\n", swidget->widget->name);
sof_ipc4_dbg_audio_format(sdev->dev, &available_fmt->input_pin_fmts[i], 1);
} else {
dev_dbg(sdev->dev, "No init input audio formats for %s\n", swidget->widget->name);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is printed when no input pin is defined, sure it means that there is no input format, but...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add this printed info in case some one will check the input audio formats. So he can easily use grep "init input audio formats" to get all input formats (also for this reason, I changed "Init" to "init" to print the same keyword in 2 conditions). Or we don't print anything for no input audio format scenario?

sizeof(struct sof_ipc4_audio_format));
if (available_fmt->output_pin_fmts)
memcpy(&process->output_format, &available_fmt->output_pin_fmts[ret].audio_fmt,
sizeof(struct sof_ipc4_audio_format));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's say you got a match with the 2nd input format in sof_ipc4_init_audio_fmt(), that will make ret==1, let's say you have one output pin. available_fmt->output_pin_fmts[1] is out of bound.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's say you got a match with the 2nd input format in sof_ipc4_init_audio_fmt(), that will make ret==1, let's say you have one output pin. available_fmt->output_pin_fmts[1] is out of bound.

Yes, I mentioned a similar question in #4163 (comment). The output is not 1:1 mapping to input. Sometimes input number may be larger than output number. This may cause issue. What I understand is we will limit it in tplg based on @ranj063 's comments?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @ujfalusi raise this issue, I still want to recommend that we can add an index in the format structure. Let say:

 struct sof_ipc4_pin_format {
        u32 pin_index;
+       u32 format_index;
        u32 buffer_size;
        struct sof_ipc4_audio_format audio_fmt;
 };

When there is output format mapped to an input format, they have the same format_index. So we don't use ret as the output fmt idx. This can help us to avoid such out of bound accessing.

@ranj063
Copy link
Collaborator

ranj063 commented Feb 14, 2023

@libinyang @ujfalusi i've added the fixups in my PR #4185. Lets continue the discussion there

@libinyang
Copy link
Author

Close this PR as Ranjani merged this fix in PR #4185.

@libinyang libinyang closed this Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants