diff mbox

[i-g-t,2/3] tests/drv_module_reload_basic: Convert sh script to C version.

Message ID 20161020193649.11701-3-marius.c.vlad@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Marius Vlad Oct. 20, 2016, 7:36 p.m. UTC
Signed-off-by: Marius Vlad <marius.c.vlad@intel.com>
---
 tests/Makefile.sources          |   2 +-
 tests/drv_module_reload_basic   |  97 -----------------------
 tests/drv_module_reload_basic.c | 166 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 167 insertions(+), 98 deletions(-)
 delete mode 100755 tests/drv_module_reload_basic
 create mode 100644 tests/drv_module_reload_basic.c

Comments

Chris Wilson Oct. 20, 2016, 7:52 p.m. UTC | #1
On Thu, Oct 20, 2016 at 10:36:48PM +0300, Marius Vlad wrote:
> +static const char *tests[] = {
> +	"gem_alive", "gem_exec_store"
> +};

gem_alive is just a single ioctl query, simpler and move obvious to do
it inline. Then remove tests/gem_alive.c, but it may live on as
tools/gem_alive.c (or better yet tools/gem_info.c).

gem_exec_store is a couple of ioctls...

A rewritten C test should not be i915 specific if we can help it. The
core of it can be driver agnostic (same steps required to unbind from
console and reload after all).

> +
> +static int
> +reload(const char *opts_i915)
> +{
> +	kick_fbcon(0);
> +
> +	if (opts_i915)
> +		igt_info("Reloading i915 with %s\n\n", opts_i915);
> +
> +	if (igt_lsmod_has_module("snd_hda_intel")) {
> +		if (igt_pkill(SIGTERM, "alsactl") == -1) {
> +			return IGT_EXIT_FAILURE;
> +		}
> +		if (igt_rmmod("snd_hda_intel", false) == -1)
> +			return IGT_EXIT_FAILURE;
> +	}
> +
> +	/* gen5 */
> +	if (igt_lsmod_has_module("intel_ips")) {
> +		igt_rmmod("intel_ips", false);
> +	}
> +
> +	if (igt_rmmod("i915", false) == -1) {
> +		return IGT_EXIT_SKIP;
> +	}

Ugh. These names leave much to be desired.

igt_kmod_load()
igt_kmod_unload()
igt_kmod_is_loaded() (can return refcnt >= 0 and -1 for unloaded)

> +
> +	igt_info("i915.ko has been unloaded!\n");
> +
> +	if (igt_lsmod_has_module("intel-gtt")) {
> +		igt_rmmod("intel-gtt", false);
> +	}
> +
> +	igt_rmmod("drm_kms_helper", false);
> +	igt_rmmod("drm", false);
> +
> +	if (igt_lsmod_has_module("i915")) {
> +		igt_info("WARNING: i915.ko still loaded!\n");
> +		return IGT_EXIT_FAILURE;
> +	} else {
> +		igt_info("module successfully unloaded\n");
> +	}
> +
> +	/* we do not have automatic loading of dependencies */
> +	igt_insmod("drm", NULL);
> +	igt_insmod("drm_kms_helper", NULL);
> +
> +	if (igt_insmod("i915", opts_i915) == -1) {
> +		igt_info("Could not load i915\n");
> +		return IGT_EXIT_FAILURE;
> +	}
> +
> +	kick_fbcon(1);
> +
> +	if (igt_insmod("snd_hda_intel", NULL) == -1)
> +		return IGT_EXIT_FAILURE;
> +
> +	return IGT_EXIT_SUCCESS;
> +}
> +
> +static void
> +igt_execv(char **argv)
> +{
> +	igt_fork(child, 1) {
> +		if (execv(argv[0], argv) < 0) {
> +			igt_info("Failed to exec %s\n",
> +					argv[0]);
> +			exit(IGT_EXIT_FAILURE);
> +		}
> +	}
> +	igt_waitchildren();
> +}
> +
> +static void
> +finish_load(char *dirname)
> +{
> +	char buf[PATH_MAX];
> +	char *__argv[2] = { buf, NULL };
> +
> +	memset(buf, 0, PATH_MAX);
> +	snprintf(buf, sizeof(buf), "%s/%s", dirname, tests[0]);
> +
> +	igt_execv(__argv);
> +
> +	memset(buf, 0, sizeof(buf));
> +	snprintf(buf, sizeof(buf), "%s/%s", dirname, tests[1]);
> +
> +	igt_execv(__argv);
> +}
> +
> +int main(int argc, char *argv[])

igt_main
{
Petri Latvala Oct. 21, 2016, 9:39 a.m. UTC | #2
On 10/20/2016 10:36 PM, Marius Vlad wrote:
> Signed-off-by: Marius Vlad <marius.c.vlad@intel.com>
> ---
>   tests/Makefile.sources          |   2 +-
>   tests/drv_module_reload_basic   |  97 -----------------------
>   tests/drv_module_reload_basic.c | 166 ++++++++++++++++++++++++++++++++++++++++
>   3 files changed, 167 insertions(+), 98 deletions(-)
>   delete mode 100755 tests/drv_module_reload_basic
>   create mode 100644 tests/drv_module_reload_basic.c
>
> diff --git a/tests/Makefile.sources b/tests/Makefile.sources
> index 6d081c3..c35ea11 100644
> --- a/tests/Makefile.sources
> +++ b/tests/Makefile.sources
> @@ -211,6 +211,7 @@ TESTS_progs = \
>   	kms_pwrite_crc \
>   	kms_sink_crc_basic \
>   	prime_udl \
> +	drv_module_reload_basic \
>   	$(NULL)
>   
>   # IMPORTANT: The ZZ_ tests need to be run last!
> @@ -221,7 +222,6 @@ TESTS_scripts_M = \
>   TESTS_scripts = \
>   	debugfs_emon_crash \
>   	drv_debugfs_reader \
> -	drv_module_reload_basic \
>   	kms_sysfs_edid_timing \
>   	sysfs_l3_parity \
>   	test_rte_check \
> diff --git a/tests/drv_module_reload_basic b/tests/drv_module_reload_basic
> deleted file mode 100755
> index a8d628d..0000000
> --- a/tests/drv_module_reload_basic
> +++ /dev/null
> @@ -1,97 +0,0 @@
> -#!/bin/bash
> -#
> -# Testcase: Reload the drm module
> -#
> -# ... we've broken this way too often :(
> -#
> -
> -SOURCE_DIR="$( dirname "${BASH_SOURCE[0]}" )"
> -. $SOURCE_DIR/drm_lib.sh
> -
> -# no other drm service should be running, so we can just unbind
> -
> -# return 0 if module by name $1 is loaded according to lsmod
> -function mod_loaded()
> -{
> -	lsmod | grep -w "^$1" &> /dev/null
> -}
> -
> -function reload() {
> -	local snd_hda_intel_unloaded
> -
> -	echo Reloading i915.ko with $*
> -
> -	# we must kick away fbcon (but only fbcon)
> -	for vtcon in /sys/class/vtconsole/vtcon*/ ; do
> -		if grep "frame buffer device" $vtcon/name > /dev/null ; then
> -			echo unbinding $vtcon: `cat $vtcon/name`
> -			echo 0 > $vtcon/bind
> -		fi
> -	done
> -
> -	# The sound driver uses our power well
> -	pkill alsactl
> -	snd_hda_intel_unloaded=0
> -	if mod_loaded snd_hda_intel; then
> -		rmmod snd_hda_intel && snd_hda_intel_unloaded=1
> -	fi
> -
> -	# gen5 only
> -	if mod_loaded intel_ips; then
> -		rmmod intel_ips
> -	fi
> -	rmmod i915 || return $IGT_EXIT_SKIP
> -	#ignore errors in intel-gtt, often built-in
> -	rmmod intel-gtt &> /dev/null
> -	# drm may be used by other devices (nouveau, radeon, udl, etc)
> -	rmmod drm_kms_helper &> /dev/null
> -	rmmod drm &> /dev/null
> -
> -	if mod_loaded i915; then
> -		echo WARNING: i915.ko still loaded!
> -		return $IGT_EXIT_FAILURE
> -	else
> -		echo module successfully unloaded
> -	fi
> -
> -	modprobe i915 $*
> -
> -	if [ -f /sys/class/vtconsole/vtcon1/bind ]; then
> -		echo 1 > /sys/class/vtconsole/vtcon1/bind
> -	fi
> -
> -	modprobe -q snd_hda_intel || return $snd_hda_intel_unloaded
> -}
> -
> -function finish_load() {
> -	# does the device exist?
> -	if $SOURCE_DIR/gem_alive > /dev/null ; then
> -		echo "module successfully loaded again"
> -	else
> -		echo "failed to reload module successfully"
> -		return $IGT_EXIT_FAILURE
> -	fi
> -
> -	# then try to run something
> -	if ! $SOURCE_DIR/gem_exec_store > /dev/null ; then
> -		echo "failed to execute a simple batch after reload"
> -		return $IGT_EXIT_FAILURE
> -	fi
> -
> -	return $IGT_EXIT_SUCCESS
> -}
> -
> -hda_dynamic_debug_enable

I don't see the equivalent of hda_dynamic_debug_enable in the C version.


> -
> -reload || exit $?
> -finish_load || exit $?
> -
> -# Repeat the module reload trying to to generate faults
> -for i in $(seq 1 4); do
> -	reload inject_load_failure=$i
> -done
> -
> -reload || exit $?
> -finish_load
> -
> -exit $?
> diff --git a/tests/drv_module_reload_basic.c b/tests/drv_module_reload_basic.c
> new file mode 100644
> index 0000000..d36afde
> --- /dev/null
> +++ b/tests/drv_module_reload_basic.c
> @@ -0,0 +1,166 @@
> +/*
> + * Copyright © 2016 Intel Corporation
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the next
> + * paragraph) shall be included in all copies or substantial portions of the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> + * IN THE SOFTWARE.
> + */
> +#include "igt.h"
> +#include "igt_debugfs.h"
> +#include "igt_aux.h"
> +#include "igt_sysfs.h"
> +#include "igt_core.h"
> +
> +#include <dirent.h>
> +#include <sys/utsname.h>
> +#include <linux/limits.h>
> +#include <signal.h>
> +#include <libgen.h>
> +
> +#include <libkmod.h>
> +#include <proc/readproc.h>
> +
> +static const char *tests[] = {
> +	"gem_alive", "gem_exec_store"
> +};
> +
> +static int
> +reload(const char *opts_i915)
> +{
> +	kick_fbcon(0);
> +
> +	if (opts_i915)
> +		igt_info("Reloading i915 with %s\n\n", opts_i915);
> +
> +	if (igt_lsmod_has_module("snd_hda_intel")) {
> +		if (igt_pkill(SIGTERM, "alsactl") == -1) {
> +			return IGT_EXIT_FAILURE;
> +		}
> +		if (igt_rmmod("snd_hda_intel", false) == -1)
> +			return IGT_EXIT_FAILURE;
> +	}
> +
> +	/* gen5 */
> +	if (igt_lsmod_has_module("intel_ips")) {
> +		igt_rmmod("intel_ips", false);
> +	}
> +
> +	if (igt_rmmod("i915", false) == -1) {
> +		return IGT_EXIT_SKIP;
> +	}
> +
> +	igt_info("i915.ko has been unloaded!\n");
> +
> +	if (igt_lsmod_has_module("intel-gtt")) {
> +		igt_rmmod("intel-gtt", false);
> +	}
> +
> +	igt_rmmod("drm_kms_helper", false);
> +	igt_rmmod("drm", false);
> +
> +	if (igt_lsmod_has_module("i915")) {
> +		igt_info("WARNING: i915.ko still loaded!\n");
> +		return IGT_EXIT_FAILURE;
> +	} else {
> +		igt_info("module successfully unloaded\n");
> +	}
> +
> +	/* we do not have automatic loading of dependencies */
> +	igt_insmod("drm", NULL);
> +	igt_insmod("drm_kms_helper", NULL);
> +
> +	if (igt_insmod("i915", opts_i915) == -1) {
> +		igt_info("Could not load i915\n");
> +		return IGT_EXIT_FAILURE;
> +	}
> +
> +	kick_fbcon(1);
> +
> +	if (igt_insmod("snd_hda_intel", NULL) == -1)
> +		return IGT_EXIT_FAILURE;
> +
> +	return IGT_EXIT_SUCCESS;
> +}
> +
> +static void
> +igt_execv(char **argv)
> +{
> +	igt_fork(child, 1) {
> +		if (execv(argv[0], argv) < 0) {
> +			igt_info("Failed to exec %s\n",
> +					argv[0]);
> +			exit(IGT_EXIT_FAILURE);
> +		}
> +	}
> +	igt_waitchildren();
> +}
> +
> +static void
> +finish_load(char *dirname)
> +{
> +	char buf[PATH_MAX];
> +	char *__argv[2] = { buf, NULL };
> +
> +	memset(buf, 0, PATH_MAX);
> +	snprintf(buf, sizeof(buf), "%s/%s", dirname, tests[0]);
> +
> +	igt_execv(__argv);
> +
> +	memset(buf, 0, sizeof(buf));
> +	snprintf(buf, sizeof(buf), "%s/%s", dirname, tests[1]);
> +
> +	igt_execv(__argv);
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +	int i, err;
> +	char buf[64];
> +	char *prg, *dir;
> +
> +	prg = strdup(argv[0]);
> +	igt_subtest_init_parse_opts(&argc, argv, "", NULL,
> +				    NULL, NULL, NULL);
> +
> +
> +	igt_subtest("basic-reload") {
> +		if ((err = reload(NULL)))
> +			igt_fail(err);
> +	}
> +
> +	igt_subtest("basic-exec") {
> +		dir = dirname(prg);
> +		finish_load(dir);
> +	}
> +
> +	igt_subtest("basic-reload-inject") {
> +		for (i = 0; i < 4; i++) {
> +			memset(buf, 0, sizeof(buf));
> +			snprintf(buf, sizeof(buf), "inject_load_failure=%d", i);
> +			reload(buf);
> +		}
> +	}
> +
> +	igt_subtest("basic-reload-final")
> +		if ((err = reload(NULL)))
> +			igt_fail(err);
> +

An accompanying change to tests/intel-ci/fast-feedback.testlist in the 
same commit required.
Marius Vlad Oct. 24, 2016, 6:05 p.m. UTC | #3
On Thu, Oct 20, 2016 at 08:52:23PM +0100, Chris Wilson wrote:
> On Thu, Oct 20, 2016 at 10:36:48PM +0300, Marius Vlad wrote:
> > +static const char *tests[] = {
> > +	"gem_alive", "gem_exec_store"
> > +};
> 
> gem_alive is just a single ioctl query, simpler and move obvious to do
> it inline. Then remove tests/gem_alive.c, but it may live on as
> tools/gem_alive.c (or better yet tools/gem_info.c).
> 
> gem_exec_store is a couple of ioctls...
Initially I tried embeddeding them directly. Problem is the at exit drm
fd handler:

if (is_i915_device(fd)) {
	if (__sync_fetch_and_add(&open_count, 1) == 0) {
		gem_quiescent_gpu(fd);

		at_exit_drm_fd = __drm_open_driver(chipset); <-- leaked until igt_exit, at_exit(), exit.
		igt_install_exit_handler(quiescent_gpu_at_exit);
	}
}

A drm_open_driver() w/o that opened fd allows reloading the driver after
running those basic tests/subtests.

> 
> A rewritten C test should not be i915 specific if we can help it. The
> core of it can be driver agnostic (same steps required to unbind from
> console and reload after all).
> 
> > +
> > +static int
> > +reload(const char *opts_i915)
> > +{
> > +	kick_fbcon(0);
> > +
> > +	if (opts_i915)
> > +		igt_info("Reloading i915 with %s\n\n", opts_i915);
> > +
> > +	if (igt_lsmod_has_module("snd_hda_intel")) {
> > +		if (igt_pkill(SIGTERM, "alsactl") == -1) {
> > +			return IGT_EXIT_FAILURE;
> > +		}
> > +		if (igt_rmmod("snd_hda_intel", false) == -1)
> > +			return IGT_EXIT_FAILURE;
> > +	}
> > +
> > +	/* gen5 */
> > +	if (igt_lsmod_has_module("intel_ips")) {
> > +		igt_rmmod("intel_ips", false);
> > +	}
> > +
> > +	if (igt_rmmod("i915", false) == -1) {
> > +		return IGT_EXIT_SKIP;
> > +	}
> 
> Ugh. These names leave much to be desired.
> 
> igt_kmod_load()
> igt_kmod_unload()
> igt_kmod_is_loaded() (can return refcnt >= 0 and -1 for unloaded)
> 
> > +
> > +	igt_info("i915.ko has been unloaded!\n");
> > +
> > +	if (igt_lsmod_has_module("intel-gtt")) {
> > +		igt_rmmod("intel-gtt", false);
> > +	}
> > +
> > +	igt_rmmod("drm_kms_helper", false);
> > +	igt_rmmod("drm", false);
> > +
> > +	if (igt_lsmod_has_module("i915")) {
> > +		igt_info("WARNING: i915.ko still loaded!\n");
> > +		return IGT_EXIT_FAILURE;
> > +	} else {
> > +		igt_info("module successfully unloaded\n");
> > +	}
> > +
> > +	/* we do not have automatic loading of dependencies */
> > +	igt_insmod("drm", NULL);
> > +	igt_insmod("drm_kms_helper", NULL);
> > +
> > +	if (igt_insmod("i915", opts_i915) == -1) {
> > +		igt_info("Could not load i915\n");
> > +		return IGT_EXIT_FAILURE;
> > +	}
> > +
> > +	kick_fbcon(1);
> > +
> > +	if (igt_insmod("snd_hda_intel", NULL) == -1)
> > +		return IGT_EXIT_FAILURE;
> > +
> > +	return IGT_EXIT_SUCCESS;
> > +}
> > +
> > +static void
> > +igt_execv(char **argv)
> > +{
> > +	igt_fork(child, 1) {
> > +		if (execv(argv[0], argv) < 0) {
> > +			igt_info("Failed to exec %s\n",
> > +					argv[0]);
> > +			exit(IGT_EXIT_FAILURE);
> > +		}
> > +	}
> > +	igt_waitchildren();
> > +}
> > +
> > +static void
> > +finish_load(char *dirname)
> > +{
> > +	char buf[PATH_MAX];
> > +	char *__argv[2] = { buf, NULL };
> > +
> > +	memset(buf, 0, PATH_MAX);
> > +	snprintf(buf, sizeof(buf), "%s/%s", dirname, tests[0]);
> > +
> > +	igt_execv(__argv);
> > +
> > +	memset(buf, 0, sizeof(buf));
> > +	snprintf(buf, sizeof(buf), "%s/%s", dirname, tests[1]);
> > +
> > +	igt_execv(__argv);
> > +}
> > +
> > +int main(int argc, char *argv[])
> 
> igt_main
> {
> 
> -- 
> Chris Wilson, Intel Open Source Technology Centre
Marius Vlad Oct. 24, 2016, 6:06 p.m. UTC | #4
On Fri, Oct 21, 2016 at 12:39:22PM +0300, Petri Latvala wrote:
> 
> 
> On 10/20/2016 10:36 PM, Marius Vlad wrote:
> > Signed-off-by: Marius Vlad <marius.c.vlad@intel.com>
> > ---
> >   tests/Makefile.sources          |   2 +-
> >   tests/drv_module_reload_basic   |  97 -----------------------
> >   tests/drv_module_reload_basic.c | 166 ++++++++++++++++++++++++++++++++++++++++
> >   3 files changed, 167 insertions(+), 98 deletions(-)
> >   delete mode 100755 tests/drv_module_reload_basic
> >   create mode 100644 tests/drv_module_reload_basic.c
> > 
> > diff --git a/tests/Makefile.sources b/tests/Makefile.sources
> > index 6d081c3..c35ea11 100644
> > --- a/tests/Makefile.sources
> > +++ b/tests/Makefile.sources
> > @@ -211,6 +211,7 @@ TESTS_progs = \
> >   	kms_pwrite_crc \
> >   	kms_sink_crc_basic \
> >   	prime_udl \
> > +	drv_module_reload_basic \
> >   	$(NULL)
> >   # IMPORTANT: The ZZ_ tests need to be run last!
> > @@ -221,7 +222,6 @@ TESTS_scripts_M = \
> >   TESTS_scripts = \
> >   	debugfs_emon_crash \
> >   	drv_debugfs_reader \
> > -	drv_module_reload_basic \
> >   	kms_sysfs_edid_timing \
> >   	sysfs_l3_parity \
> >   	test_rte_check \
> > diff --git a/tests/drv_module_reload_basic b/tests/drv_module_reload_basic
> > deleted file mode 100755
> > index a8d628d..0000000
> > --- a/tests/drv_module_reload_basic
> > +++ /dev/null
> > @@ -1,97 +0,0 @@
> > -#!/bin/bash
> > -#
> > -# Testcase: Reload the drm module
> > -#
> > -# ... we've broken this way too often :(
> > -#
> > -
> > -SOURCE_DIR="$( dirname "${BASH_SOURCE[0]}" )"
> > -. $SOURCE_DIR/drm_lib.sh
> > -
> > -# no other drm service should be running, so we can just unbind
> > -
> > -# return 0 if module by name $1 is loaded according to lsmod
> > -function mod_loaded()
> > -{
> > -	lsmod | grep -w "^$1" &> /dev/null
> > -}
> > -
> > -function reload() {
> > -	local snd_hda_intel_unloaded
> > -
> > -	echo Reloading i915.ko with $*
> > -
> > -	# we must kick away fbcon (but only fbcon)
> > -	for vtcon in /sys/class/vtconsole/vtcon*/ ; do
> > -		if grep "frame buffer device" $vtcon/name > /dev/null ; then
> > -			echo unbinding $vtcon: `cat $vtcon/name`
> > -			echo 0 > $vtcon/bind
> > -		fi
> > -	done
> > -
> > -	# The sound driver uses our power well
> > -	pkill alsactl
> > -	snd_hda_intel_unloaded=0
> > -	if mod_loaded snd_hda_intel; then
> > -		rmmod snd_hda_intel && snd_hda_intel_unloaded=1
> > -	fi
> > -
> > -	# gen5 only
> > -	if mod_loaded intel_ips; then
> > -		rmmod intel_ips
> > -	fi
> > -	rmmod i915 || return $IGT_EXIT_SKIP
> > -	#ignore errors in intel-gtt, often built-in
> > -	rmmod intel-gtt &> /dev/null
> > -	# drm may be used by other devices (nouveau, radeon, udl, etc)
> > -	rmmod drm_kms_helper &> /dev/null
> > -	rmmod drm &> /dev/null
> > -
> > -	if mod_loaded i915; then
> > -		echo WARNING: i915.ko still loaded!
> > -		return $IGT_EXIT_FAILURE
> > -	else
> > -		echo module successfully unloaded
> > -	fi
> > -
> > -	modprobe i915 $*
> > -
> > -	if [ -f /sys/class/vtconsole/vtcon1/bind ]; then
> > -		echo 1 > /sys/class/vtconsole/vtcon1/bind
> > -	fi
> > -
> > -	modprobe -q snd_hda_intel || return $snd_hda_intel_unloaded
> > -}
> > -
> > -function finish_load() {
> > -	# does the device exist?
> > -	if $SOURCE_DIR/gem_alive > /dev/null ; then
> > -		echo "module successfully loaded again"
> > -	else
> > -		echo "failed to reload module successfully"
> > -		return $IGT_EXIT_FAILURE
> > -	fi
> > -
> > -	# then try to run something
> > -	if ! $SOURCE_DIR/gem_exec_store > /dev/null ; then
> > -		echo "failed to execute a simple batch after reload"
> > -		return $IGT_EXIT_FAILURE
> > -	fi
> > -
> > -	return $IGT_EXIT_SUCCESS
> > -}
> > -
> > -hda_dynamic_debug_enable
> 
> I don't see the equivalent of hda_dynamic_debug_enable in the C version.
Indeed, this got updated prior to me looking at it.
> 
> 
> > -
> > -reload || exit $?
> > -finish_load || exit $?
> > -
> > -# Repeat the module reload trying to to generate faults
> > -for i in $(seq 1 4); do
> > -	reload inject_load_failure=$i
> > -done
> > -
> > -reload || exit $?
> > -finish_load
> > -
> > -exit $?
> > diff --git a/tests/drv_module_reload_basic.c b/tests/drv_module_reload_basic.c
> > new file mode 100644
> > index 0000000..d36afde
> > --- /dev/null
> > +++ b/tests/drv_module_reload_basic.c
> > @@ -0,0 +1,166 @@
> > +/*
> > + * Copyright © 2016 Intel Corporation
> > + *
> > + * Permission is hereby granted, free of charge, to any person obtaining a
> > + * copy of this software and associated documentation files (the "Software"),
> > + * to deal in the Software without restriction, including without limitation
> > + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> > + * and/or sell copies of the Software, and to permit persons to whom the
> > + * Software is furnished to do so, subject to the following conditions:
> > + *
> > + * The above copyright notice and this permission notice (including the next
> > + * paragraph) shall be included in all copies or substantial portions of the
> > + * Software.
> > + *
> > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> > + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> > + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> > + * IN THE SOFTWARE.
> > + */
> > +#include "igt.h"
> > +#include "igt_debugfs.h"
> > +#include "igt_aux.h"
> > +#include "igt_sysfs.h"
> > +#include "igt_core.h"
> > +
> > +#include <dirent.h>
> > +#include <sys/utsname.h>
> > +#include <linux/limits.h>
> > +#include <signal.h>
> > +#include <libgen.h>
> > +
> > +#include <libkmod.h>
> > +#include <proc/readproc.h>
> > +
> > +static const char *tests[] = {
> > +	"gem_alive", "gem_exec_store"
> > +};
> > +
> > +static int
> > +reload(const char *opts_i915)
> > +{
> > +	kick_fbcon(0);
> > +
> > +	if (opts_i915)
> > +		igt_info("Reloading i915 with %s\n\n", opts_i915);
> > +
> > +	if (igt_lsmod_has_module("snd_hda_intel")) {
> > +		if (igt_pkill(SIGTERM, "alsactl") == -1) {
> > +			return IGT_EXIT_FAILURE;
> > +		}
> > +		if (igt_rmmod("snd_hda_intel", false) == -1)
> > +			return IGT_EXIT_FAILURE;
> > +	}
> > +
> > +	/* gen5 */
> > +	if (igt_lsmod_has_module("intel_ips")) {
> > +		igt_rmmod("intel_ips", false);
> > +	}
> > +
> > +	if (igt_rmmod("i915", false) == -1) {
> > +		return IGT_EXIT_SKIP;
> > +	}
> > +
> > +	igt_info("i915.ko has been unloaded!\n");
> > +
> > +	if (igt_lsmod_has_module("intel-gtt")) {
> > +		igt_rmmod("intel-gtt", false);
> > +	}
> > +
> > +	igt_rmmod("drm_kms_helper", false);
> > +	igt_rmmod("drm", false);
> > +
> > +	if (igt_lsmod_has_module("i915")) {
> > +		igt_info("WARNING: i915.ko still loaded!\n");
> > +		return IGT_EXIT_FAILURE;
> > +	} else {
> > +		igt_info("module successfully unloaded\n");
> > +	}
> > +
> > +	/* we do not have automatic loading of dependencies */
> > +	igt_insmod("drm", NULL);
> > +	igt_insmod("drm_kms_helper", NULL);
> > +
> > +	if (igt_insmod("i915", opts_i915) == -1) {
> > +		igt_info("Could not load i915\n");
> > +		return IGT_EXIT_FAILURE;
> > +	}
> > +
> > +	kick_fbcon(1);
> > +
> > +	if (igt_insmod("snd_hda_intel", NULL) == -1)
> > +		return IGT_EXIT_FAILURE;
> > +
> > +	return IGT_EXIT_SUCCESS;
> > +}
> > +
> > +static void
> > +igt_execv(char **argv)
> > +{
> > +	igt_fork(child, 1) {
> > +		if (execv(argv[0], argv) < 0) {
> > +			igt_info("Failed to exec %s\n",
> > +					argv[0]);
> > +			exit(IGT_EXIT_FAILURE);
> > +		}
> > +	}
> > +	igt_waitchildren();
> > +}
> > +
> > +static void
> > +finish_load(char *dirname)
> > +{
> > +	char buf[PATH_MAX];
> > +	char *__argv[2] = { buf, NULL };
> > +
> > +	memset(buf, 0, PATH_MAX);
> > +	snprintf(buf, sizeof(buf), "%s/%s", dirname, tests[0]);
> > +
> > +	igt_execv(__argv);
> > +
> > +	memset(buf, 0, sizeof(buf));
> > +	snprintf(buf, sizeof(buf), "%s/%s", dirname, tests[1]);
> > +
> > +	igt_execv(__argv);
> > +}
> > +
> > +int main(int argc, char *argv[])
> > +{
> > +	int i, err;
> > +	char buf[64];
> > +	char *prg, *dir;
> > +
> > +	prg = strdup(argv[0]);
> > +	igt_subtest_init_parse_opts(&argc, argv, "", NULL,
> > +				    NULL, NULL, NULL);
> > +
> > +
> > +	igt_subtest("basic-reload") {
> > +		if ((err = reload(NULL)))
> > +			igt_fail(err);
> > +	}
> > +
> > +	igt_subtest("basic-exec") {
> > +		dir = dirname(prg);
> > +		finish_load(dir);
> > +	}
> > +
> > +	igt_subtest("basic-reload-inject") {
> > +		for (i = 0; i < 4; i++) {
> > +			memset(buf, 0, sizeof(buf));
> > +			snprintf(buf, sizeof(buf), "inject_load_failure=%d", i);
> > +			reload(buf);
> > +		}
> > +	}
> > +
> > +	igt_subtest("basic-reload-final")
> > +		if ((err = reload(NULL)))
> > +			igt_fail(err);
> > +
> 
> An accompanying change to tests/intel-ci/fast-feedback.testlist in the same
> commit required.
Will do.
> 
> 
> -- 
> Petri Latvala
> 
> 
> > +	free(prg);
> > +
> > +	igt_exit();
> > +}
>
Chris Wilson Oct. 24, 2016, 8:34 p.m. UTC | #5
On Mon, Oct 24, 2016 at 09:05:28PM +0300, Marius Vlad wrote:
> On Thu, Oct 20, 2016 at 08:52:23PM +0100, Chris Wilson wrote:
> > On Thu, Oct 20, 2016 at 10:36:48PM +0300, Marius Vlad wrote:
> > > +static const char *tests[] = {
> > > +	"gem_alive", "gem_exec_store"
> > > +};
> > 
> > gem_alive is just a single ioctl query, simpler and move obvious to do
> > it inline. Then remove tests/gem_alive.c, but it may live on as
> > tools/gem_alive.c (or better yet tools/gem_info.c).
> > 
> > gem_exec_store is a couple of ioctls...
> Initially I tried embeddeding them directly. Problem is the at exit drm
> fd handler:
> 
> if (is_i915_device(fd)) {
> 	if (__sync_fetch_and_add(&open_count, 1) == 0) {
> 		gem_quiescent_gpu(fd);
> 
> 		at_exit_drm_fd = __drm_open_driver(chipset); <-- leaked until igt_exit, at_exit(), exit.
> 		igt_install_exit_handler(quiescent_gpu_at_exit);
> 	}
> }
> 
> A drm_open_driver() w/o that opened fd allows reloading the driver after
> running those basic tests/subtests.

Ah, yes, for module reload in C, you have to use __drm_open_driver() to
avoid that stray open device filehandle.
-Chris
diff mbox

Patch

diff --git a/tests/Makefile.sources b/tests/Makefile.sources
index 6d081c3..c35ea11 100644
--- a/tests/Makefile.sources
+++ b/tests/Makefile.sources
@@ -211,6 +211,7 @@  TESTS_progs = \
 	kms_pwrite_crc \
 	kms_sink_crc_basic \
 	prime_udl \
+	drv_module_reload_basic \
 	$(NULL)
 
 # IMPORTANT: The ZZ_ tests need to be run last!
@@ -221,7 +222,6 @@  TESTS_scripts_M = \
 TESTS_scripts = \
 	debugfs_emon_crash \
 	drv_debugfs_reader \
-	drv_module_reload_basic \
 	kms_sysfs_edid_timing \
 	sysfs_l3_parity \
 	test_rte_check \
diff --git a/tests/drv_module_reload_basic b/tests/drv_module_reload_basic
deleted file mode 100755
index a8d628d..0000000
--- a/tests/drv_module_reload_basic
+++ /dev/null
@@ -1,97 +0,0 @@ 
-#!/bin/bash
-#
-# Testcase: Reload the drm module
-#
-# ... we've broken this way too often :(
-#
-
-SOURCE_DIR="$( dirname "${BASH_SOURCE[0]}" )"
-. $SOURCE_DIR/drm_lib.sh
-
-# no other drm service should be running, so we can just unbind
-
-# return 0 if module by name $1 is loaded according to lsmod
-function mod_loaded()
-{
-	lsmod | grep -w "^$1" &> /dev/null
-}
-
-function reload() {
-	local snd_hda_intel_unloaded
-
-	echo Reloading i915.ko with $*
-
-	# we must kick away fbcon (but only fbcon)
-	for vtcon in /sys/class/vtconsole/vtcon*/ ; do
-		if grep "frame buffer device" $vtcon/name > /dev/null ; then
-			echo unbinding $vtcon: `cat $vtcon/name`
-			echo 0 > $vtcon/bind
-		fi
-	done
-
-	# The sound driver uses our power well
-	pkill alsactl
-	snd_hda_intel_unloaded=0
-	if mod_loaded snd_hda_intel; then
-		rmmod snd_hda_intel && snd_hda_intel_unloaded=1
-	fi
-
-	# gen5 only
-	if mod_loaded intel_ips; then
-		rmmod intel_ips
-	fi
-	rmmod i915 || return $IGT_EXIT_SKIP
-	#ignore errors in intel-gtt, often built-in
-	rmmod intel-gtt &> /dev/null
-	# drm may be used by other devices (nouveau, radeon, udl, etc)
-	rmmod drm_kms_helper &> /dev/null
-	rmmod drm &> /dev/null
-
-	if mod_loaded i915; then
-		echo WARNING: i915.ko still loaded!
-		return $IGT_EXIT_FAILURE
-	else
-		echo module successfully unloaded
-	fi
-
-	modprobe i915 $*
-
-	if [ -f /sys/class/vtconsole/vtcon1/bind ]; then
-		echo 1 > /sys/class/vtconsole/vtcon1/bind
-	fi
-
-	modprobe -q snd_hda_intel || return $snd_hda_intel_unloaded
-}
-
-function finish_load() {
-	# does the device exist?
-	if $SOURCE_DIR/gem_alive > /dev/null ; then
-		echo "module successfully loaded again"
-	else
-		echo "failed to reload module successfully"
-		return $IGT_EXIT_FAILURE
-	fi
-
-	# then try to run something
-	if ! $SOURCE_DIR/gem_exec_store > /dev/null ; then
-		echo "failed to execute a simple batch after reload"
-		return $IGT_EXIT_FAILURE
-	fi
-
-	return $IGT_EXIT_SUCCESS
-}
-
-hda_dynamic_debug_enable
-
-reload || exit $?
-finish_load || exit $?
-
-# Repeat the module reload trying to to generate faults
-for i in $(seq 1 4); do
-	reload inject_load_failure=$i
-done
-
-reload || exit $?
-finish_load
-
-exit $?
diff --git a/tests/drv_module_reload_basic.c b/tests/drv_module_reload_basic.c
new file mode 100644
index 0000000..d36afde
--- /dev/null
+++ b/tests/drv_module_reload_basic.c
@@ -0,0 +1,166 @@ 
+/*
+ * Copyright © 2016 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+#include "igt.h"
+#include "igt_debugfs.h"
+#include "igt_aux.h"
+#include "igt_sysfs.h"
+#include "igt_core.h"
+
+#include <dirent.h>
+#include <sys/utsname.h>
+#include <linux/limits.h>
+#include <signal.h>
+#include <libgen.h>
+
+#include <libkmod.h>
+#include <proc/readproc.h>
+
+static const char *tests[] = {
+	"gem_alive", "gem_exec_store"
+};
+
+static int
+reload(const char *opts_i915)
+{
+	kick_fbcon(0);
+
+	if (opts_i915)
+		igt_info("Reloading i915 with %s\n\n", opts_i915);
+
+	if (igt_lsmod_has_module("snd_hda_intel")) {
+		if (igt_pkill(SIGTERM, "alsactl") == -1) {
+			return IGT_EXIT_FAILURE;
+		}
+		if (igt_rmmod("snd_hda_intel", false) == -1)
+			return IGT_EXIT_FAILURE;
+	}
+
+	/* gen5 */
+	if (igt_lsmod_has_module("intel_ips")) {
+		igt_rmmod("intel_ips", false);
+	}
+
+	if (igt_rmmod("i915", false) == -1) {
+		return IGT_EXIT_SKIP;
+	}
+
+	igt_info("i915.ko has been unloaded!\n");
+
+	if (igt_lsmod_has_module("intel-gtt")) {
+		igt_rmmod("intel-gtt", false);
+	}
+
+	igt_rmmod("drm_kms_helper", false);
+	igt_rmmod("drm", false);
+
+	if (igt_lsmod_has_module("i915")) {
+		igt_info("WARNING: i915.ko still loaded!\n");
+		return IGT_EXIT_FAILURE;
+	} else {
+		igt_info("module successfully unloaded\n");
+	}
+
+	/* we do not have automatic loading of dependencies */
+	igt_insmod("drm", NULL);
+	igt_insmod("drm_kms_helper", NULL);
+
+	if (igt_insmod("i915", opts_i915) == -1) {
+		igt_info("Could not load i915\n");
+		return IGT_EXIT_FAILURE;
+	}
+
+	kick_fbcon(1);
+
+	if (igt_insmod("snd_hda_intel", NULL) == -1)
+		return IGT_EXIT_FAILURE;
+
+	return IGT_EXIT_SUCCESS;
+}
+
+static void
+igt_execv(char **argv)
+{
+	igt_fork(child, 1) {
+		if (execv(argv[0], argv) < 0) {
+			igt_info("Failed to exec %s\n",
+					argv[0]);
+			exit(IGT_EXIT_FAILURE);
+		}
+	}
+	igt_waitchildren();
+}
+
+static void
+finish_load(char *dirname)
+{
+	char buf[PATH_MAX];
+	char *__argv[2] = { buf, NULL };
+
+	memset(buf, 0, PATH_MAX);
+	snprintf(buf, sizeof(buf), "%s/%s", dirname, tests[0]);
+
+	igt_execv(__argv);
+
+	memset(buf, 0, sizeof(buf));
+	snprintf(buf, sizeof(buf), "%s/%s", dirname, tests[1]);
+
+	igt_execv(__argv);
+}
+
+int main(int argc, char *argv[])
+{
+	int i, err;
+	char buf[64];
+	char *prg, *dir;
+
+	prg = strdup(argv[0]);
+	igt_subtest_init_parse_opts(&argc, argv, "", NULL,
+				    NULL, NULL, NULL);
+
+
+	igt_subtest("basic-reload") {
+		if ((err = reload(NULL)))
+			igt_fail(err);
+	}
+
+	igt_subtest("basic-exec") {
+		dir = dirname(prg);
+		finish_load(dir);
+	}
+
+	igt_subtest("basic-reload-inject") {
+		for (i = 0; i < 4; i++) {
+			memset(buf, 0, sizeof(buf));
+			snprintf(buf, sizeof(buf), "inject_load_failure=%d", i);
+			reload(buf);
+		}
+	}
+
+	igt_subtest("basic-reload-final")
+		if ((err = reload(NULL)))
+			igt_fail(err);
+
+	free(prg);
+
+	igt_exit();
+}