Skip to contentSkip to conversation

Linux-kernel-mentees Digest, Vol 51, Issue 5

DISCUSSION Ankita Pareek 1 participant 1 message Jun 7, 2023 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): mailman.17.1686139204.22992.linux-kernel-mentees@lists.linuxfoundation.org

#1 lore raw

unsubscribe

Quoted 595 lines · On Wed, Jun 7, 2023 at 5:30 PM < [email protected]…

Send Linux-kernel-mentees mailing list submissions to
[email protected]

To subscribe or unsubscribe via the World Wide Web, visit

https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

or, via email, send a message with subject or body 'help' to
[email protected]

You can reach the person managing the list at
[email protected]

When replying, please edit your Subject line so it is more specific than "Re: Contents of Linux-kernel-mentees digest..."

Today's Topics:

1. [PATCH v4 3/3] selftests: ALSA: Add test for the 'pcmtest' driver (Ivan Orlov)
2. Re: [PATCH v2] selftests: rtc: Improves rtctest error handling. (Alexandre Belloni)

----------------------------------------------------------------------

Message: 1
Date: Tue, 6 Jun 2023 23:32:54 +0400
From: Ivan Orlov <[email protected]>
To: [email protected], [email protected], [email protected],
[email protected], [email protected]
Cc: [email protected], [email protected],
[email protected], [email protected], [email protected]
Subject: [PATCH v4 3/3] selftests: ALSA: Add test for the 'pcmtest'
driver
Message-ID: <[email protected]>

This test covers the new Virtual PCM Test Driver, including the capturing, playback and ioctl redefinition functionalities for both interleaved and non-interleaved access modes. This test is also helpful as an usage example of the 'pcmtest' driver.

We have a lot of different virtual media drivers, which can be used for testing of the userspace applications and media subsystem middle layer. However, all of them are aimed at testing the video functionality and simulating the video devices. For audio devices we have only snd-dummy module, which is good in simulating the correct behavior of an ALSA device. I decided to write a tool, which would help to test the userspace ALSA programs (and the PCM middle layer as well) under unusual circumstances to figure out how they would behave. So I came up with this Virtual PCM Test Driver.

This new Virtual PCM Test Driver has several features which can be useful during the userspace ALSA applications testing/fuzzing, or testing/fuzzing of the PCM middle layer. Not all of them can be implemented using the existing virtual drivers (like dummy or loopback). Here is what can this driver do:

- Simulate both capture and playback processes
- Generate random or pattern-based capture data
- Check the playback stream for containing the looped pattern
- Inject delays into the playback and capturing processes
- Inject errors during the PCM callbacks

Also, this driver can check the playback stream for containing the predefined pattern, which is used in the corresponding selftest to check the PCM middle layer data transferring functionality. Additionally, this driver redefines the default RESET ioctl, and the selftest covers this PCM API functionality as well.

The driver supports both interleaved and non-interleaved access modes, and have separate pattern buffers for each channel. The driver supports up to 4 channels and up to 8 substreams.

---
V1 -> V2:

- Rename the driver from from 'valsa' to 'pcmtest'.
- Implement support for interleaved and non-interleaved access modes
- Add support for 8 substreams and 4 channels
- Extend supported formats
- Extend and rewrite in C the selftest for the driver

V2 -> V3:

- Add separate pattern buffers for each channel
- Speed up the capture data generation when using interleaved access mode
- Extend the corresponding selftest to cover the multiple channels capturing and playback functionalities when using interleaved access mode.
- Fix documentation issues

V3 -> V4:

- Fix issue in the selftest: there was a typo in the fscanf argument.

 tools/testing/selftests/alsa/Makefile         |   2 +-
 .../selftests/alsa/test-pcmtest-driver.c      | 333 ++++++++++++++++++
 2 files changed, 334 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/alsa/test-pcmtest-driver.c
tools/testing/selftests/alsa/Makefile +1 -1
index 901949db80ad..5af9ba8a4645 100644
@@ -12,7 +12,7 @@ LDLIBS+=-lpthread
 
 OVERRIDE_TARGETS = 1
 
-TEST_GEN_PROGS := mixer-test pcm-test
+TEST_GEN_PROGS := mixer-test pcm-test test-pcmtest-driver
 
 TEST_GEN_PROGS_EXTENDED := libatest.so
 
tools/testing/selftests/alsa/test-pcmtest-driver.c new file +41 -0
new file mode 100644
index 000000000000..71931b240a83
@@ -0,0 +1,333 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * This is the test which covers PCM middle layer data transferring using
+ * the virtual pcm test driver (snd-pcmtest).
+ *
+ * Copyright 2023 Ivan Orlov <[email protected]>
+ */
+#include <string.h>
+#include <alsa/asoundlib.h>
+#include "../kselftest_harness.h"
+
+#define CH_NUM 4
+
+struct pattern_buf {
+       char buf[1024];
+       int len;
+};
+
+struct pattern_buf patterns[CH_NUM];
+
+struct pcmtest_test_params {
+       unsigned long buffer_size;
+       unsigned long period_size;
+       unsigned long channels;
+       unsigned int rate;
+       snd_pcm_access_t access;
+       size_t sec_buf_len;
+       size_t sample_size;
+       int time;
+       snd_pcm_format_t format;
+};
+
+static int read_patterns(void)
+{
+       FILE *fp, *fpl;
+       int i;
+       char pf[64];
+       char plf[64];
+
+       for (i = 0; i < CH_NUM; i++) {
+               sprintf(plf,

"/sys/kernel/debug/pcmtest/fill_pattern%d_len", i);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +39 -0
+               fpl = fopen(plf, "r");
+               if (!fpl)
+                       return -1;
+               fscanf(fpl, "%u", &patterns[i].len);
+               fclose(fpl);
+
+               sprintf(pf, "/sys/kernel/debug/pcmtest/fill_pattern%d", i);
+               fp = fopen(pf, "r");
+               if (!fp) {
+                       fclose(fpl);
+                       return -1;
+               }
+               fread(patterns[i].buf, 1, patterns[i].len, fp);
+               fclose(fp);
+       }
+
+       return 0;
+}
+
+static int get_test_results(char *debug_name)
+{
+       int result;
+       FILE *f;
+       char fname[128];
+
+       sprintf(fname, "/sys/kernel/debug/pcmtest/%s", debug_name);
+
+       f = fopen(fname, "r");
+       if (!f) {
+               printf("Failed to open file\n");
+               return -1;
+       }
+       fscanf(f, "%d", &result);
+       fclose(f);
+
+       return result;
+}
+
+static size_t get_sec_buf_len(unsigned int rate, unsigned long channels,

snd_pcm_format_t format)

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +6 -0
+{
+       return rate * channels * snd_pcm_format_physical_width(format) / 8;
+}
+
+static int setup_handle(snd_pcm_t **handle, snd_pcm_sw_params_t *swparams,
+                       snd_pcm_hw_params_t *hwparams, struct

pcmtest_test_params *params,

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +14 -0
+                       int card, snd_pcm_stream_t stream)
+{
+       char pcm_name[32];
+       int err;
+
+       sprintf(pcm_name, "hw:%d,0,0", card);
+       err = snd_pcm_open(handle, pcm_name, stream, 0);
+       if (err < 0)
+               return err;
+       snd_pcm_hw_params_any(*handle, hwparams);
+       snd_pcm_hw_params_set_rate_resample(*handle, hwparams, 0);
+       snd_pcm_hw_params_set_access(*handle, hwparams, params->access);
+       snd_pcm_hw_params_set_format(*handle, hwparams, params->format);
+       snd_pcm_hw_params_set_channels(*handle, hwparams,

params->channels);
+ snd_pcm_hw_params_set_rate_near(*handle, hwparams, &params->rate,
0);
+ snd_pcm_hw_params_set_period_size_near(*handle, hwparams,
&params->period_size, 0);
+ snd_pcm_hw_params_set_buffer_size_near(*handle, hwparams,
&params->buffer_size);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +5 -0
+       snd_pcm_hw_params(*handle, hwparams);
+       snd_pcm_sw_params_current(*handle, swparams);
+
+       snd_pcm_hw_params_set_rate_resample(*handle, hwparams, 0);
+       snd_pcm_sw_params_set_avail_min(*handle, swparams,

params->period_size);
+ snd_pcm_hw_params_set_buffer_size_near(*handle, hwparams,
&params->buffer_size);
+ snd_pcm_hw_params_set_period_size_near(*handle, hwparams,
&params->period_size, 0);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +26 -0
+       snd_pcm_sw_params(*handle, swparams);
+       snd_pcm_hw_params(*handle, hwparams);
+
+       return 0;
+}
+
+FIXTURE(pcmtest) {
+       int card;
+       snd_pcm_sw_params_t *swparams;
+       snd_pcm_hw_params_t *hwparams;
+       struct pcmtest_test_params params;
+};
+
+FIXTURE_TEARDOWN(pcmtest) {
+}
+
+FIXTURE_SETUP(pcmtest) {
+       char *card_name;
+       int err;
+
+       if (geteuid())
+               SKIP(exit(-1), "This test needs root to run!");
+
+       err = read_patterns();
+       if (err)
+               SKIP(exit(-1), "Can't read patterns. Probably, module

isn't loaded");

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +11 -0
+
+       card_name = malloc(127);
+       ASSERT_NE(card_name, NULL);
+       self->params.buffer_size = 16384;
+       self->params.period_size = 4096;
+       self->params.channels = CH_NUM;
+       self->params.rate = 8000;
+       self->params.access = SND_PCM_ACCESS_RW_INTERLEAVED;
+       self->params.format = SND_PCM_FORMAT_S16_LE;
+       self->card = -1;
+       self->params.sample_size =

snd_pcm_format_physical_width(self->params.format) / 8;

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +2 -0
+
+       self->params.sec_buf_len = get_sec_buf_len(self->params.rate,

self->params.channels,

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +16 -0
+                                                  self->params.format);
+       self->params.time = 4;
+
+       while (snd_card_next(&self->card) >= 0) {
+               if (self->card == -1)
+                       break;
+               snd_card_get_name(self->card, &card_name);
+               if (!strcmp(card_name, "PCM-Test"))
+                       break;
+       }
+       free(card_name);
+       ASSERT_NE(self->card, -1);
+}
+
+/*
+ * Here we are trying to send the looped monotonically increasing

sequence of bytes to the driver.
+ * If our data isn't corrupted, the driver will set the content of 'pc_test' debugfs file to '1'
+ */

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +16 -0
+TEST_F(pcmtest, playback) {
+       snd_pcm_t *handle;
+       unsigned char *it;
+       size_t write_res;
+       int test_results;
+       int i, cur_ch, pos_in_ch;
+       void *samples;
+       struct pcmtest_test_params *params = &self->params;
+
+       samples = calloc(self->params.sec_buf_len * self->params.time, 1);
+       ASSERT_NE(samples, NULL);
+
+       snd_pcm_sw_params_alloca(&self->swparams);
+       snd_pcm_hw_params_alloca(&self->hwparams);
+
+       ASSERT_EQ(setup_handle(&handle, self->swparams, self->hwparams,

params,

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +3 -0
+                              self->card, SND_PCM_STREAM_PLAYBACK), 0);
+       snd_pcm_format_set_silence(params->format, samples,
+                                  params->rate * params->channels *

params->time);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +4 -0
+       it = samples;
+       for (i = 0; i < self->params.sec_buf_len * params->time; i++) {
+               cur_ch = (i / params->sample_size) % CH_NUM;
+               pos_in_ch = i / params->sample_size / CH_NUM *

params->sample_size

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +2 -0
+                           + (i % params->sample_size);
+               it[i] = patterns[cur_ch].buf[pos_in_ch %

patterns[cur_ch].len];

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +2 -0
+       }
+       write_res = snd_pcm_writei(handle, samples, params->rate *

params->time);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +10 -0
+       ASSERT_GE(write_res, 0);
+
+       snd_pcm_close(handle);
+       free(samples);
+       test_results = get_test_results("pc_test");
+       ASSERT_EQ(test_results, 1);
+}
+
+/*
+ * Here we test that the virtual alsa driver returns looped and

monotonically increasing sequence
+ * of bytes. In the interleaved mode the buffer will contain samples in the following order:
+ * C0, C1, C2, C3, C0, C1, ...
+ */

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +16 -0
+TEST_F(pcmtest, capture) {
+       snd_pcm_t *handle;
+       unsigned char *it;
+       size_t read_res;
+       int i, cur_ch, pos_in_ch;
+       void *samples;
+       struct pcmtest_test_params *params = &self->params;
+
+       samples = calloc(self->params.sec_buf_len * self->params.time, 1);
+       ASSERT_NE(samples, NULL);
+
+       snd_pcm_sw_params_alloca(&self->swparams);
+       snd_pcm_hw_params_alloca(&self->hwparams);
+
+       ASSERT_EQ(setup_handle(&handle, self->swparams, self->hwparams,
+                              params, self->card,

SND_PCM_STREAM_CAPTURE), 0);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +2 -0
+       snd_pcm_format_set_silence(params->format, samples,
+                                  params->rate * params->channels *

params->time);
+ read_res = snd_pcm_readi(handle, samples, params->rate *
params->time);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +4 -0
+       ASSERT_GE(read_res, 0);
+       snd_pcm_close(handle);
+       it = (unsigned char *)samples;
+       for (i = 0; i < self->params.sec_buf_len * self->params.time; i++)

{

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +2 -0
+               cur_ch = (i / params->sample_size) % CH_NUM;
+               pos_in_ch = i / params->sample_size / CH_NUM *

params->sample_size

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +2 -0
+                           + (i % params->sample_size);
+               ASSERT_EQ(it[i], patterns[cur_ch].buf[pos_in_ch %

patterns[cur_ch].len]);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +5 -0
+       }
+       free(samples);
+}
+
+// Test capture in the non-interleaved access mode. The are buffers for

each recorded channel

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +16 -0
+TEST_F(pcmtest, ni_capture) {
+       snd_pcm_t *handle;
+       struct pcmtest_test_params params = self->params;
+       char **chan_samples;
+       size_t i, j, read_res;
+
+       chan_samples = calloc(CH_NUM, sizeof(*chan_samples));
+       ASSERT_NE(chan_samples, NULL);
+
+       snd_pcm_sw_params_alloca(&self->swparams);
+       snd_pcm_hw_params_alloca(&self->hwparams);
+
+       params.access = SND_PCM_ACCESS_RW_NONINTERLEAVED;
+
+       ASSERT_EQ(setup_handle(&handle, self->swparams, self->hwparams,
+                              &params, self->card,

SND_PCM_STREAM_CAPTURE), 0);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +3 -0
+
+       for (i = 0; i < CH_NUM; i++)
+               chan_samples[i] = calloc(params.sec_buf_len * params.time,

1);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +3 -0
+
+       for (i = 0; i < 1; i++) {
+               read_res = snd_pcm_readn(handle, (void **)chan_samples,

params.rate * params.time);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +7 -0
+               ASSERT_GE(read_res, 0);
+       }
+       snd_pcm_close(handle);
+
+       for (i = 0; i < CH_NUM; i++) {
+               for (j = 0; j < params.rate * params.time; j++)
+                       ASSERT_EQ(chan_samples[i][j], patterns[i].buf[j %

patterns[i].len]);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +22 -0
+               free(chan_samples[i]);
+       }
+       free(chan_samples);
+}
+
+TEST_F(pcmtest, ni_playback) {
+       snd_pcm_t *handle;
+       struct pcmtest_test_params params = self->params;
+       char **chan_samples;
+       size_t i, j, read_res;
+       int test_res;
+
+       chan_samples = calloc(CH_NUM, sizeof(*chan_samples));
+       ASSERT_NE(chan_samples, NULL);
+
+       snd_pcm_sw_params_alloca(&self->swparams);
+       snd_pcm_hw_params_alloca(&self->hwparams);
+
+       params.access = SND_PCM_ACCESS_RW_NONINTERLEAVED;
+
+       ASSERT_EQ(setup_handle(&handle, self->swparams, self->hwparams,
+                              &params, self->card,

SND_PCM_STREAM_PLAYBACK), 0);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +3 -0
+
+       for (i = 0; i < CH_NUM; i++) {
+               chan_samples[i] = calloc(params.sec_buf_len * params.time,

1);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +2 -0
+               for (j = 0; j < params.sec_buf_len * params.time; j++)
+                       chan_samples[i][j] = patterns[i].buf[j %

patterns[i].len];

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +4 -0
+       }
+
+       for (i = 0; i < 1; i++) {
+               read_res = snd_pcm_writen(handle, (void **)chan_samples,

params.rate * params.time);

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +14 -0
+               ASSERT_GE(read_res, 0);
+       }
+
+       snd_pcm_close(handle);
+       test_res = get_test_results("pc_test");
+       ASSERT_EQ(test_res, 1);
+
+       for (i = 0; i < CH_NUM; i++)
+               free(chan_samples[i]);
+       free(chan_samples);
+}
+
+/*
+ * Here we are testing the custom ioctl definition inside the virtual

driver. If it triggers
+ * successfully, the driver sets the content of 'ioctl_test' debugfs file to '1'.
+ */

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +10 -0
+TEST_F(pcmtest, reset_ioctl) {
+       snd_pcm_t *handle;
+       unsigned char *it;
+       int test_res;
+       struct pcmtest_test_params *params = &self->params;
+
+       snd_pcm_sw_params_alloca(&self->swparams);
+       snd_pcm_hw_params_alloca(&self->hwparams);
+
+       ASSERT_EQ(setup_handle(&handle, self->swparams, self->hwparams,

params,

tools/testing/selftests/alsa/test-pcmtest-driver.c new file +8 -1
+                              self->card, SND_PCM_STREAM_CAPTURE), 0);
+       snd_pcm_reset(handle);
+       test_res = get_test_results("ioctl_test");
+       ASSERT_EQ(test_res, 1);
+       snd_pcm_close(handle);
+}
+
+TEST_HARNESS_MAIN
--

2.34.1

------------------------------

Message: 2
Date: Tue, 6 Jun 2023 23:50:19 +0200
From: Alexandre Belloni <[email protected]>
To: Atul Kumar Pant <[email protected]>
Cc: [email protected], [email protected],
[email protected]
Subject: Re: [PATCH v2] selftests: rtc: Improves rtctest error
handling.
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Quoted 4 lines · Atul Kumar Pant

When running the rtctest if we pass wrong rtc device file as an argument the test fails expectedly, but prints the logs that are not useful to point out the issue.
To handle this, the patch adds a checks to verify if the rtc_file is

valid.

Quoted 4 lines

---

Review

Quoted 8 lines

changes since v1:
Removed check for uid=0
If rtc file is invalid, then exit the test.

 tools/testing/selftests/rtc/rtctest.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/tools/testing/selftests/rtc/rtctest.c

Quoted 21 lines

index 2b9d929a24ed..07f7dcd47102 100644

tools/testing/selftests/rtc/rtctest.c +3 -0
@@ -17,6 +17,7 @@
 #include <unistd.h>
 
 #include "../kselftest_harness.h"
+#include "../kselftest.h"
 
 #define NUM_UIE 3
 #define ALARM_DELTA 3
@@ -388,6 +389,8 @@ __constructor_order_last(void)
 
 int main(int argc, char **argv)
 {
+     int ret = -1;
+
      switch (argc) {
      case 2:
              rtc_file = argv[1];

tools/testing/selftests/rtc/rtctest.c int main(int argc, char **argv) · line 405

Quoted 5 lines
tools/testing/selftests/rtc/rtctest.c +1 -1
@@ -399,5 +402,11 @@ int main(int argc, char **argv)
              return 1;
      }
 
-     return test_harness_run(argc, argv);
+     // Run the test if rtc_file is valid

if access to rtc_file is valid

tools/testing/selftests/rtc/rtctest.c int main(int argc, char **argv) · line 409

Quoted 4 lines
tools/testing/selftests/rtc/rtctest.c +4 -0
+     if (access(rtc_file, F_OK) == 0)
+             ret = test_harness_run(argc, argv);
+     else
+             ksft_exit_fail_msg("[ERROR]: %s : File does not exists -

Exiting\n", rtc_file);

It doesn't necessarily means the file does not exist but that you don't have access to it.

Quoted 6 lines
tools/testing/selftests/rtc/rtctest.c +2 -1
+
+     return ret;
 }
--

2.25.1

--
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

------------------------------

Subject: Digest Footer

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

Quoted 5 lines

------------------------------

End of Linux-kernel-mentees Digest, Vol 51, Issue 5
***************************************************

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees