diff mbox

[i-g-t,2/4,v6] lib/igt_gvt: Make use of libkmod helpers and fix reading gvt parameter.

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

Commit Message

Marius Vlad Nov. 23, 2016, 12:35 p.m. UTC
v2:
- use igt_sysfs_get_boolean() to get gvt status (Chris Wilson)
- do not hard-fail when i915 module could not be loaded/unloaded (Chris
Wilson)

Signed-off-by: Marius Vlad <marius.c.vlad@intel.com>
---
 lib/igt_gvt.c     | 37 ++++++++++++++++++-------------------
 tests/gvt_basic.c |  2 +-
 2 files changed, 19 insertions(+), 20 deletions(-)

Comments

Chris Wilson Nov. 23, 2016, 1:31 p.m. UTC | #1
On Wed, Nov 23, 2016 at 02:35:10PM +0200, Marius Vlad wrote:
> v2:
> - use igt_sysfs_get_boolean() to get gvt status (Chris Wilson)
> - do not hard-fail when i915 module could not be loaded/unloaded (Chris
> Wilson)
> 
> Signed-off-by: Marius Vlad <marius.c.vlad@intel.com>
> ---
>  lib/igt_gvt.c     | 37 ++++++++++++++++++-------------------
>  tests/gvt_basic.c |  2 +-
>  2 files changed, 19 insertions(+), 20 deletions(-)
> 
> diff --git a/lib/igt_gvt.c b/lib/igt_gvt.c
> index 8bbf9bd..4ab7433 100644
> --- a/lib/igt_gvt.c
> +++ b/lib/igt_gvt.c
> @@ -24,35 +24,30 @@
>  #include "igt.h"
>  #include "igt_gvt.h"
>  #include "igt_sysfs.h"
> +#include "igt_kmod.h"
>  
> +#include <signal.h>
>  #include <dirent.h>
>  #include <unistd.h>
>  #include <fcntl.h>
>  
>  static bool is_gvt_enabled(void)
>  {
> -	FILE *file;
> -	int value;
>  	bool enabled = false;
> +	int dir, fd;
>  
> -	file = fopen("/sys/module/i915/parameters/enable_gvt", "r");
> -	if (!file)
> +	fd = __drm_open_driver(DRIVER_INTEL);
> +	dir = igt_sysfs_open_parameters(fd);
> +	if (dir < 0)
>  		return false;
>  
> -	if (fscanf(file, "%d", &value) == 1)
> -		enabled = value;
> -	fclose(file);
> +	enabled = igt_sysfs_get_boolean(dir, "enable_gvt");
>  
> -	errno = 0;
> -	return enabled;
> -}
> +	close(dir);
> +	close(fd);
>  
> -static void unload_i915(void)
> -{
> -	kick_fbcon(false);
> -	/* pkill alsact */
> +	return enabled;
>  
> -	igt_ignore_warn(system("/sbin/modprobe -s -r i915"));
>  }
>  
>  bool igt_gvt_load_module(void)
> @@ -60,8 +55,11 @@ bool igt_gvt_load_module(void)
>  	if (is_gvt_enabled())
>  		return true;
>  
> -	unload_i915();
> -	igt_ignore_warn(system("/sbin/modprobe -s i915 enable_gvt=1"));
> +	if (igt_i915_driver_unload())
> +		return false;
> +
> +	if (igt_i915_driver_load("enable_gvt=1"))
> +		return false;
>  
>  	return is_gvt_enabled();
>  }
> @@ -71,8 +69,9 @@ void igt_gvt_unload_module(void)
>  	if (!is_gvt_enabled())
>  		return;
>  
> -	unload_i915();
> -	igt_ignore_warn(system("/sbin/modprobe -s i915 enable_gvt=0"));
> +	igt_i915_driver_unload();
> +
> +	igt_i915_driver_load(NULL);
>  
>  	igt_assert(!is_gvt_enabled());
>  }
> diff --git a/tests/gvt_basic.c b/tests/gvt_basic.c
> index 48b853a..4e909a5 100644
> --- a/tests/gvt_basic.c
> +++ b/tests/gvt_basic.c
> @@ -32,7 +32,7 @@ igt_main
>  
>  	igt_fixture {
>  		igt_require(igt_gvt_load_module());
> -		fd = drm_open_driver(DRIVER_INTEL);
> +		fd = __drm_open_driver(DRIVER_INTEL);

I couldn't work out why this needed to switch to the non-checking, bare
call to open_driver().
-Chris
Marius Vlad Nov. 24, 2016, 12:04 p.m. UTC | #2
On Wed, Nov 23, 2016 at 01:31:21PM +0000, Chris Wilson wrote:
> On Wed, Nov 23, 2016 at 02:35:10PM +0200, Marius Vlad wrote:
> > v2:
> > - use igt_sysfs_get_boolean() to get gvt status (Chris Wilson)
> > - do not hard-fail when i915 module could not be loaded/unloaded (Chris
> > Wilson)
> > 
> > Signed-off-by: Marius Vlad <marius.c.vlad@intel.com>
> > ---
> >  lib/igt_gvt.c     | 37 ++++++++++++++++++-------------------
> >  tests/gvt_basic.c |  2 +-
> >  2 files changed, 19 insertions(+), 20 deletions(-)
> > 
> > diff --git a/lib/igt_gvt.c b/lib/igt_gvt.c
> > index 8bbf9bd..4ab7433 100644
> > --- a/lib/igt_gvt.c
> > +++ b/lib/igt_gvt.c
> > @@ -24,35 +24,30 @@
> >  #include "igt.h"
> >  #include "igt_gvt.h"
> >  #include "igt_sysfs.h"
> > +#include "igt_kmod.h"
> >  
> > +#include <signal.h>
> >  #include <dirent.h>
> >  #include <unistd.h>
> >  #include <fcntl.h>
> >  
> >  static bool is_gvt_enabled(void)
> >  {
> > -	FILE *file;
> > -	int value;
> >  	bool enabled = false;
> > +	int dir, fd;
> >  
> > -	file = fopen("/sys/module/i915/parameters/enable_gvt", "r");
> > -	if (!file)
> > +	fd = __drm_open_driver(DRIVER_INTEL);
> > +	dir = igt_sysfs_open_parameters(fd);
> > +	if (dir < 0)
> >  		return false;
> >  
> > -	if (fscanf(file, "%d", &value) == 1)
> > -		enabled = value;
> > -	fclose(file);
> > +	enabled = igt_sysfs_get_boolean(dir, "enable_gvt");
> >  
> > -	errno = 0;
> > -	return enabled;
> > -}
> > +	close(dir);
> > +	close(fd);
> >  
> > -static void unload_i915(void)
> > -{
> > -	kick_fbcon(false);
> > -	/* pkill alsact */
> > +	return enabled;
> >  
> > -	igt_ignore_warn(system("/sbin/modprobe -s -r i915"));
> >  }
> >  
> >  bool igt_gvt_load_module(void)
> > @@ -60,8 +55,11 @@ bool igt_gvt_load_module(void)
> >  	if (is_gvt_enabled())
> >  		return true;
> >  
> > -	unload_i915();
> > -	igt_ignore_warn(system("/sbin/modprobe -s i915 enable_gvt=1"));
> > +	if (igt_i915_driver_unload())
> > +		return false;
> > +
> > +	if (igt_i915_driver_load("enable_gvt=1"))
> > +		return false;
> >  
> >  	return is_gvt_enabled();
> >  }
> > @@ -71,8 +69,9 @@ void igt_gvt_unload_module(void)
> >  	if (!is_gvt_enabled())
> >  		return;
> >  
> > -	unload_i915();
> > -	igt_ignore_warn(system("/sbin/modprobe -s i915 enable_gvt=0"));
> > +	igt_i915_driver_unload();
> > +
> > +	igt_i915_driver_load(NULL);
> >  
> >  	igt_assert(!is_gvt_enabled());
> >  }
> > diff --git a/tests/gvt_basic.c b/tests/gvt_basic.c
> > index 48b853a..4e909a5 100644
> > --- a/tests/gvt_basic.c
> > +++ b/tests/gvt_basic.c
> > @@ -32,7 +32,7 @@ igt_main
> >  
> >  	igt_fixture {
> >  		igt_require(igt_gvt_load_module());
> > -		fd = drm_open_driver(DRIVER_INTEL);
> > +		fd = __drm_open_driver(DRIVER_INTEL);
> 
> I couldn't work out why this needed to switch to the non-checking, bare
> call to open_driver().

The last fixture unloads (actually reloads with enable_gvt=0) the driver
and will fail to do so due the stray fd in drm_open_driver():

	at_exit_drm_fd = __drm_open_driver(chipset);

which is closed atexit by the quiescent_gpu_at_exit().

Maybe I should document this in lib/igt_kmod that __drm_open_driver() is
required when reloading/unloading the driver.

There's also igt_set_module_param() which should allow setting
parameters dynamically without the need of reloading but I don't know if
there's support for that.



> -Chris
> 
> -- 
> Chris Wilson, Intel Open Source Technology Centre
Chris Wilson Nov. 24, 2016, 12:06 p.m. UTC | #3
On Thu, Nov 24, 2016 at 02:04:38PM +0200, Marius Vlad wrote:
> On Wed, Nov 23, 2016 at 01:31:21PM +0000, Chris Wilson wrote:
> > On Wed, Nov 23, 2016 at 02:35:10PM +0200, Marius Vlad wrote:
> > > v2:
> > > - use igt_sysfs_get_boolean() to get gvt status (Chris Wilson)
> > > - do not hard-fail when i915 module could not be loaded/unloaded (Chris
> > > Wilson)
> > > 
> > > Signed-off-by: Marius Vlad <marius.c.vlad@intel.com>
> > > ---
> > >  lib/igt_gvt.c     | 37 ++++++++++++++++++-------------------
> > >  tests/gvt_basic.c |  2 +-
> > >  2 files changed, 19 insertions(+), 20 deletions(-)
> > > 
> > > diff --git a/lib/igt_gvt.c b/lib/igt_gvt.c
> > > index 8bbf9bd..4ab7433 100644
> > > --- a/lib/igt_gvt.c
> > > +++ b/lib/igt_gvt.c
> > > @@ -24,35 +24,30 @@
> > >  #include "igt.h"
> > >  #include "igt_gvt.h"
> > >  #include "igt_sysfs.h"
> > > +#include "igt_kmod.h"
> > >  
> > > +#include <signal.h>
> > >  #include <dirent.h>
> > >  #include <unistd.h>
> > >  #include <fcntl.h>
> > >  
> > >  static bool is_gvt_enabled(void)
> > >  {
> > > -	FILE *file;
> > > -	int value;
> > >  	bool enabled = false;
> > > +	int dir, fd;
> > >  
> > > -	file = fopen("/sys/module/i915/parameters/enable_gvt", "r");
> > > -	if (!file)
> > > +	fd = __drm_open_driver(DRIVER_INTEL);
> > > +	dir = igt_sysfs_open_parameters(fd);
> > > +	if (dir < 0)
> > >  		return false;
> > >  
> > > -	if (fscanf(file, "%d", &value) == 1)
> > > -		enabled = value;
> > > -	fclose(file);
> > > +	enabled = igt_sysfs_get_boolean(dir, "enable_gvt");
> > >  
> > > -	errno = 0;
> > > -	return enabled;
> > > -}
> > > +	close(dir);
> > > +	close(fd);
> > >  
> > > -static void unload_i915(void)
> > > -{
> > > -	kick_fbcon(false);
> > > -	/* pkill alsact */
> > > +	return enabled;
> > >  
> > > -	igt_ignore_warn(system("/sbin/modprobe -s -r i915"));
> > >  }
> > >  
> > >  bool igt_gvt_load_module(void)
> > > @@ -60,8 +55,11 @@ bool igt_gvt_load_module(void)
> > >  	if (is_gvt_enabled())
> > >  		return true;
> > >  
> > > -	unload_i915();
> > > -	igt_ignore_warn(system("/sbin/modprobe -s i915 enable_gvt=1"));
> > > +	if (igt_i915_driver_unload())
> > > +		return false;
> > > +
> > > +	if (igt_i915_driver_load("enable_gvt=1"))
> > > +		return false;
> > >  
> > >  	return is_gvt_enabled();
> > >  }
> > > @@ -71,8 +69,9 @@ void igt_gvt_unload_module(void)
> > >  	if (!is_gvt_enabled())
> > >  		return;
> > >  
> > > -	unload_i915();
> > > -	igt_ignore_warn(system("/sbin/modprobe -s i915 enable_gvt=0"));
> > > +	igt_i915_driver_unload();
> > > +
> > > +	igt_i915_driver_load(NULL);
> > >  
> > >  	igt_assert(!is_gvt_enabled());
> > >  }
> > > diff --git a/tests/gvt_basic.c b/tests/gvt_basic.c
> > > index 48b853a..4e909a5 100644
> > > --- a/tests/gvt_basic.c
> > > +++ b/tests/gvt_basic.c
> > > @@ -32,7 +32,7 @@ igt_main
> > >  
> > >  	igt_fixture {
> > >  		igt_require(igt_gvt_load_module());
> > > -		fd = drm_open_driver(DRIVER_INTEL);
> > > +		fd = __drm_open_driver(DRIVER_INTEL);
> > 
> > I couldn't work out why this needed to switch to the non-checking, bare
> > call to open_driver().
> 
> The last fixture unloads (actually reloads with enable_gvt=0) the driver
> and will fail to do so due the stray fd in drm_open_driver():
> 
> 	at_exit_drm_fd = __drm_open_driver(chipset);
> 
> which is closed atexit by the quiescent_gpu_at_exit().
> 
> Maybe I should document this in lib/igt_kmod that __drm_open_driver() is
> required when reloading/unloading the driver.

Hmm, indeed. Better perhaps might be

fd = igt_open_module(DRIVER_INTEL, "module options") ?

Time will tell.

> There's also igt_set_module_param() which should allow setting
> parameters dynamically without the need of reloading but I don't know if
> there's support for that.

Not all (most in fact do not) support runtime changes. And the ones that
we do change, taint the kernel. Sigh.
-Chris
diff mbox

Patch

diff --git a/lib/igt_gvt.c b/lib/igt_gvt.c
index 8bbf9bd..4ab7433 100644
--- a/lib/igt_gvt.c
+++ b/lib/igt_gvt.c
@@ -24,35 +24,30 @@ 
 #include "igt.h"
 #include "igt_gvt.h"
 #include "igt_sysfs.h"
+#include "igt_kmod.h"
 
+#include <signal.h>
 #include <dirent.h>
 #include <unistd.h>
 #include <fcntl.h>
 
 static bool is_gvt_enabled(void)
 {
-	FILE *file;
-	int value;
 	bool enabled = false;
+	int dir, fd;
 
-	file = fopen("/sys/module/i915/parameters/enable_gvt", "r");
-	if (!file)
+	fd = __drm_open_driver(DRIVER_INTEL);
+	dir = igt_sysfs_open_parameters(fd);
+	if (dir < 0)
 		return false;
 
-	if (fscanf(file, "%d", &value) == 1)
-		enabled = value;
-	fclose(file);
+	enabled = igt_sysfs_get_boolean(dir, "enable_gvt");
 
-	errno = 0;
-	return enabled;
-}
+	close(dir);
+	close(fd);
 
-static void unload_i915(void)
-{
-	kick_fbcon(false);
-	/* pkill alsact */
+	return enabled;
 
-	igt_ignore_warn(system("/sbin/modprobe -s -r i915"));
 }
 
 bool igt_gvt_load_module(void)
@@ -60,8 +55,11 @@  bool igt_gvt_load_module(void)
 	if (is_gvt_enabled())
 		return true;
 
-	unload_i915();
-	igt_ignore_warn(system("/sbin/modprobe -s i915 enable_gvt=1"));
+	if (igt_i915_driver_unload())
+		return false;
+
+	if (igt_i915_driver_load("enable_gvt=1"))
+		return false;
 
 	return is_gvt_enabled();
 }
@@ -71,8 +69,9 @@  void igt_gvt_unload_module(void)
 	if (!is_gvt_enabled())
 		return;
 
-	unload_i915();
-	igt_ignore_warn(system("/sbin/modprobe -s i915 enable_gvt=0"));
+	igt_i915_driver_unload();
+
+	igt_i915_driver_load(NULL);
 
 	igt_assert(!is_gvt_enabled());
 }
diff --git a/tests/gvt_basic.c b/tests/gvt_basic.c
index 48b853a..4e909a5 100644
--- a/tests/gvt_basic.c
+++ b/tests/gvt_basic.c
@@ -32,7 +32,7 @@  igt_main
 
 	igt_fixture {
 		igt_require(igt_gvt_load_module());
-		fd = drm_open_driver(DRIVER_INTEL);
+		fd = __drm_open_driver(DRIVER_INTEL);
 	}
 
 	igt_subtest_f("invalid-placeholder-test");