All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCHv2 0/3] Enable no_cache flag to driver_data
@ 2017-05-20  6:46 yi1.li
  2017-05-20  6:46 ` [PATCHv2 1/3] firmware_class: move NO_CACHE from private to driver_data_req_params yi1.li
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: yi1.li @ 2017-05-20  6:46 UTC (permalink / raw)
  To: mcgrof, atull, gregkh, wagi, dwmw2, rafal, arend.vanspriel, rjw,
	moritz.fischer, pmladek, johannes.berg, emmanuel.grumbach,
	luciano.coelho, kvalo, luto, takahiro.akashi, dhowells, pjones
  Cc: linux-kernel, linux-fpga, Yi Li

From: Yi Li <yi1.li@linux.intel.com>

Changes in v2:

  - Rebase to Luis R. Rodriguez's 20170501-driver-data-try2
    branch 
  - Expose DRIVER_DATA_REQ_NO_CACHE flag to public 
    driver_data_req_params structure, so upper drivers can ask
    driver_data driver to bypass the internal caching mechanism.
    This will be used for streaming and other drivers maintains
    their own caching like iwlwifi. 
  - Add self test cases.


Yi Li (3):
  firmware_class: move NO_CACHE from private to driver_data_req_params
  iwlwifi: use DRIVER_DATA_REQ_NO_CACHE for driver_data
  test: add no_cache to driver_data load tester

 drivers/base/firmware_class.c                   | 16 ++++-----
 drivers/net/wireless/intel/iwlwifi/iwl-drv.c    |  2 ++
 include/linux/driver_data.h                     |  4 +++
 lib/test_driver_data.c                          | 43 +++++++++++++++++++++++--
 tools/testing/selftests/firmware/driver_data.sh | 36 +++++++++++++++++++++
 5 files changed, 89 insertions(+), 12 deletions(-)

-- 
2.7.4

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCHv2 1/3] firmware_class: move NO_CACHE from private to driver_data_req_params
  2017-05-20  6:46 [PATCHv2 0/3] Enable no_cache flag to driver_data yi1.li
@ 2017-05-20  6:46 ` yi1.li
  2017-05-20  6:46 ` [PATCHv2 2/3] iwlwifi: use DRIVER_DATA_REQ_NO_CACHE for driver_data yi1.li
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 14+ messages in thread
From: yi1.li @ 2017-05-20  6:46 UTC (permalink / raw)
  To: mcgrof, atull, gregkh, wagi, dwmw2, rafal, arend.vanspriel, rjw,
	moritz.fischer, pmladek, johannes.berg, emmanuel.grumbach,
	luciano.coelho, kvalo, luto, takahiro.akashi, dhowells, pjones
  Cc: linux-kernel, linux-fpga, Yi Li

From: Yi Li <yi1.li@linux.intel.com>

This adds DRIVER_DATA_REQ_NO_CACHE flag with .req flag under struct
driver_data_req_params. When this flag is set, the driver_data driver
will bypass the internal caching mechanism, its used by streaming
case and other drivers which implement their own cache thing.

Signed-off-by: Yi Li <yi1.li@linux.intel.com>
---
 drivers/base/firmware_class.c | 16 +++++++---------
 include/linux/driver_data.h   |  4 ++++
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c
index 7af430a..444c9a8 100644
--- a/drivers/base/firmware_class.c
+++ b/drivers/base/firmware_class.c
@@ -72,14 +72,10 @@ enum driver_data_mode {
  * 	issue a uevent to userspace. Userspace in turn is expected to be
  * 	monitoring for uevents for the firmware_class and will use the
  * 	exposted sysfs interface to upload the driver data for the caller.
- * @DRIVER_DATA_PRIV_REQ_NO_CACHE: indicates that the driver data request
- * 	should not set up and use the internal caching mechanism to assist
- * 	drivers from fetching driver data at resume time after suspend.
  */
 enum driver_data_priv_reqs {
 	DRIVER_DATA_PRIV_REQ_FALLBACK			= 1 << 0,
 	DRIVER_DATA_PRIV_REQ_FALLBACK_UEVENT		= 1 << 1,
-	DRIVER_DATA_PRIV_REQ_NO_CACHE			= 1 << 2,
 };
 
 /**
@@ -151,10 +147,12 @@ struct driver_data_params {
 	}
 
 #define __DATA_REQ_FIRMWARE_BUF(buf, size)				\
+	.req_params = {							\
+		.reqs = DRIVER_DATA_REQ_NO_CACHE,			\
+	},								\
 	.priv_params = {						\
 		.priv_reqs = DRIVER_DATA_PRIV_REQ_FALLBACK |		\
-			     DRIVER_DATA_PRIV_REQ_FALLBACK_UEVENT |	\
-			     DRIVER_DATA_PRIV_REQ_NO_CACHE,		\
+			     DRIVER_DATA_PRIV_REQ_FALLBACK_UEVENT,	\
 		.alloc_buf = buf,					\
 		.alloc_buf_size = size,					\
 	}
@@ -186,7 +184,7 @@ struct driver_data_params {
 #define driver_data_param_uevent(params)	\
 	(!!((params)->priv_reqs & DRIVER_DATA_PRIV_REQ_FALLBACK_UEVENT))
 #define driver_data_param_nocache(params)	\
-	(!!((params)->priv_reqs & DRIVER_DATA_PRIV_REQ_NO_CACHE))
+	(!!((params)->reqs & DRIVER_DATA_REQ_NO_CACHE))
 
 #define driver_data_param_optional(params)	\
 	(!!((params)->reqs & DRIVER_DATA_REQ_OPTIONAL))
@@ -793,14 +791,14 @@ static int assign_firmware_buf(struct firmware *fw, struct device *device,
 	/* don't cache firmware handled without uevent */
 	if (device &&
 	    driver_data_param_uevent(&data_params->priv_params) &&
-	    !driver_data_param_nocache(&data_params->priv_params))
+	    !driver_data_param_nocache(&data_params->req_params))
 		fw_add_devm_name(device, buf->fw_id);
 
 	/*
 	 * After caching firmware image is started, let it piggyback
 	 * on request firmware.
 	 */
-	if (!driver_data_param_nocache(&data_params->priv_params) &&
+	if (!driver_data_param_nocache(&data_params->req_params) &&
 	    buf->fwc->state == FW_LOADER_START_CACHE) {
 		if (fw_cache_piggyback_on_request(buf->fw_id))
 			kref_get(&buf->ref);
diff --git a/include/linux/driver_data.h b/include/linux/driver_data.h
index bf51e0b..b6ef5ee 100644
--- a/include/linux/driver_data.h
+++ b/include/linux/driver_data.h
@@ -117,11 +117,15 @@ union driver_data_cbs {
  *	file to be present given the API range, it is only required for one
  *	file in the API range to be present.  If the %DRIVER_DATA_REQ_OPTIONAL
  *	flag is also enabled then all files are treated as optional.
+ * @DRIVER_DATA_REQ_NO_CACHE: indicates that the driver data request
+ *	should not set up and use the internal caching mechanism to assist
+ *	drivers from fetching driver data at resume time after suspend.
  */
 enum driver_data_reqs {
 	DRIVER_DATA_REQ_OPTIONAL			= 1 << 0,
 	DRIVER_DATA_REQ_KEEP				= 1 << 1,
 	DRIVER_DATA_REQ_USE_API_VERSIONING		= 1 << 2,
+	DRIVER_DATA_REQ_NO_CACHE			= 1 << 3,
 };
 
 /**
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCHv2 2/3] iwlwifi: use DRIVER_DATA_REQ_NO_CACHE for driver_data
  2017-05-20  6:46 [PATCHv2 0/3] Enable no_cache flag to driver_data yi1.li
  2017-05-20  6:46 ` [PATCHv2 1/3] firmware_class: move NO_CACHE from private to driver_data_req_params yi1.li
@ 2017-05-20  6:46 ` yi1.li
  2017-05-20  6:46 ` [PATCHv2 3/3] test: add no_cache to driver_data load tester yi1.li
  2017-05-24 19:03 ` [PATCHv2 0/3] Enable no_cache flag to driver_data Luis R. Rodriguez
  3 siblings, 0 replies; 14+ messages in thread
From: yi1.li @ 2017-05-20  6:46 UTC (permalink / raw)
  To: mcgrof, atull, gregkh, wagi, dwmw2, rafal, arend.vanspriel, rjw,
	moritz.fischer, pmladek, johannes.berg, emmanuel.grumbach,
	luciano.coelho, kvalo, luto, takahiro.akashi, dhowells, pjones
  Cc: linux-kernel, linux-fpga, Yi Li

From: Yi Li <yi1.li@linux.intel.com>

Set DRIVER_DATA_REQ_NO_CACHE flag to disable driver_data driver caching
mechanism, iwlwifi has its own firmware cache management.

Signed-off-by: Yi Li <yi1.li@linux.intel.com>
---
 drivers/net/wireless/intel/iwlwifi/iwl-drv.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/net/wireless/intel/iwlwifi/iwl-drv.c b/drivers/net/wireless/intel/iwlwifi/iwl-drv.c
index 028854d3..db4d6fc 100644
--- a/drivers/net/wireless/intel/iwlwifi/iwl-drv.c
+++ b/drivers/net/wireless/intel/iwlwifi/iwl-drv.c
@@ -229,6 +229,8 @@ static int iwl_request_firmware(struct iwl_drv *drv)
 	const struct driver_data_req_params req_params = {
 		DRIVER_DATA_API_CB(iwl_req_fw_callback, drv),
 		DRIVER_DATA_API(cfg->ucode_api_min, cfg->ucode_api_max, ".ucode"),
+		.reqs = DRIVER_DATA_REQ_NO_CACHE |
+			DRIVER_DATA_REQ_USE_API_VERSIONING,
 	};
 
 	return driver_data_request_async(name_pre,
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCHv2 3/3] test: add no_cache to driver_data load tester
  2017-05-20  6:46 [PATCHv2 0/3] Enable no_cache flag to driver_data yi1.li
  2017-05-20  6:46 ` [PATCHv2 1/3] firmware_class: move NO_CACHE from private to driver_data_req_params yi1.li
  2017-05-20  6:46 ` [PATCHv2 2/3] iwlwifi: use DRIVER_DATA_REQ_NO_CACHE for driver_data yi1.li
@ 2017-05-20  6:46 ` yi1.li
  2017-05-24 19:03 ` [PATCHv2 0/3] Enable no_cache flag to driver_data Luis R. Rodriguez
  3 siblings, 0 replies; 14+ messages in thread
From: yi1.li @ 2017-05-20  6:46 UTC (permalink / raw)
  To: mcgrof, atull, gregkh, wagi, dwmw2, rafal, arend.vanspriel, rjw,
	moritz.fischer, pmladek, johannes.berg, emmanuel.grumbach,
	luciano.coelho, kvalo, luto, takahiro.akashi, dhowells, pjones
  Cc: linux-kernel, linux-fpga, Yi Li

From: Yi Li <yi1.li@linux.intel.com>

This adds a no_cache flag to simple sync and async test.

Signed-off-by: Yi Li <yi1.li@linux.intel.com>
---
 lib/test_driver_data.c                          | 43 +++++++++++++++++++++++--
 tools/testing/selftests/firmware/driver_data.sh | 36 +++++++++++++++++++++
 2 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/lib/test_driver_data.c b/lib/test_driver_data.c
index 488cc6e..fa7f52b 100644
--- a/lib/test_driver_data.c
+++ b/lib/test_driver_data.c
@@ -64,6 +64,10 @@ int num_test_devs;
  *	struct driver_data_reg_params @optional field for more information.
  * @keep: whether or not we wish to free the driver_data on our own, refer to
  *	the struct driver_data_req_params @keep field for more information.
+ * @no_cache: whether or not we wish to use the internal caching mechanism
+ *      to assist drivers from fetching driver data at resume time after
+ *      suspend, refer to the struct driver_data_req_params .req
+ *      DRIVER_DATA_REQ_NO_CACHE for more information.
  * @enable_opt_cb: whether or not the optional callback should be set
  *	on a trigger. There is no equivalent setting on the struct
  *	driver_data_req_params as this is implementation specific, and in
@@ -112,6 +116,7 @@ struct test_config {
 	bool async;
 	bool optional;
 	bool keep;
+	bool no_cache;
 	bool enable_opt_cb;
 	bool use_api_versioning;
 	u8 api_min;
@@ -337,6 +342,9 @@ static ssize_t config_show(struct device *dev,
 	len += snprintf(buf+len, PAGE_SIZE,
 			"keep:\t\t%s\n",
 			config->keep ? "true" : "false");
+	len += snprintf(buf+len, PAGE_SIZE,
+			"no_cache:\t\t%s\n",
+			config->no_cache ? "true" : "false");
 
 	mutex_unlock(&test_dev->config_mutex);
 
@@ -452,7 +460,8 @@ static int trigger_config_sync(struct driver_data_test_device *test_dev)
 		DRIVER_DATA_SYNC_OPT_CB(config_sync_req_default_cb,
 					     test_dev),
 		.reqs = (config->optional ? DRIVER_DATA_REQ_OPTIONAL : 0) |
-			(config->keep ? DRIVER_DATA_REQ_KEEP : 0),
+			(config->keep ? DRIVER_DATA_REQ_KEEP : 0) |
+			(config->no_cache ? DRIVER_DATA_REQ_NO_CACHE : 0),
 	};
 	const struct driver_data_req_params *req_params;
 
@@ -518,19 +527,22 @@ static int trigger_config_async(struct driver_data_test_device *test_dev)
 	const struct driver_data_req_params req_params_default = {
 		DRIVER_DATA_DEFAULT_ASYNC(config_async_req_cb, test_dev),
 		.reqs = (config->optional ? DRIVER_DATA_REQ_OPTIONAL : 0) |
-			(config->keep ? DRIVER_DATA_REQ_KEEP : 0),
+			(config->keep ? DRIVER_DATA_REQ_KEEP : 0) |
+			(config->no_cache ? DRIVER_DATA_REQ_NO_CACHE : 0),
 	};
 	const struct driver_data_req_params req_params_opt_cb = {
 		DRIVER_DATA_DEFAULT_ASYNC(config_async_req_cb, test_dev),
 		DRIVER_DATA_ASYNC_OPT_CB(config_async_req_default_cb, test_dev),
 		.reqs = (config->optional ? DRIVER_DATA_REQ_OPTIONAL : 0) |
-			(config->keep ? DRIVER_DATA_REQ_KEEP : 0),
+			(config->keep ? DRIVER_DATA_REQ_KEEP : 0) |
+			(config->no_cache ? DRIVER_DATA_REQ_NO_CACHE : 0),
 	};
 	const struct driver_data_req_params req_params_api = {
 		DRIVER_DATA_API_CB(config_async_req_api_cb, test_dev),
 		DRIVER_DATA_API(config->api_min, config->api_max, config->api_name_postfix),
 		.reqs = (config->optional ? DRIVER_DATA_REQ_OPTIONAL : 0) |
 			(config->keep ? DRIVER_DATA_REQ_KEEP : 0) |
+			(config->no_cache ? DRIVER_DATA_REQ_NO_CACHE : 0) |
 			(config->use_api_versioning ? DRIVER_DATA_REQ_USE_API_VERSIONING : 0),
 	};
 	const struct driver_data_req_params *req_params;
@@ -656,6 +668,7 @@ static int __driver_data_config_init(struct test_config *config)
 	config->async = false;
 	config->optional = false;
 	config->keep = false;
+	config->no_cache = false;
 	config->enable_opt_cb = false;
 	config->use_api_versioning = false;
 	config->api_min = 0;
@@ -975,6 +988,29 @@ static ssize_t config_keep_show(struct device *dev,
 }
 static DEVICE_ATTR(config_keep, 0644, config_keep_show, config_keep_store);
 
+static ssize_t config_no_cache_store(struct device *dev,
+				 struct device_attribute *attr,
+				 const char *buf, size_t count)
+{
+	struct driver_data_test_device *test_dev = dev_to_test_dev(dev);
+	struct test_config *config = &test_dev->config;
+
+	return test_dev_config_update_bool(test_dev, buf, count,
+					   &config->no_cache);
+}
+
+static ssize_t config_no_cache_show(struct device *dev,
+				struct device_attribute *attr,
+				char *buf)
+{
+	struct driver_data_test_device *test_dev = dev_to_test_dev(dev);
+	struct test_config *config = &test_dev->config;
+
+	return test_dev_config_show_bool(test_dev, buf, config->no_cache);
+}
+static DEVICE_ATTR(config_no_cache, 0644, config_no_cache_show,
+		   config_no_cache_store);
+
 static ssize_t config_enable_opt_cb_store(struct device *dev,
 					  struct device_attribute *attr,
 					  const char *buf, size_t count)
@@ -1132,6 +1168,7 @@ static struct attribute *test_dev_attrs[] = {
 	TEST_DRIVER_DATA_DEV_ATTR(config_async),
 	TEST_DRIVER_DATA_DEV_ATTR(config_optional),
 	TEST_DRIVER_DATA_DEV_ATTR(config_keep),
+	TEST_DRIVER_DATA_DEV_ATTR(config_no_cache),
 	TEST_DRIVER_DATA_DEV_ATTR(config_use_api_versioning),
 	TEST_DRIVER_DATA_DEV_ATTR(config_enable_opt_cb),
 	TEST_DRIVER_DATA_DEV_ATTR(config_api_min),
diff --git a/tools/testing/selftests/firmware/driver_data.sh b/tools/testing/selftests/firmware/driver_data.sh
index c830d04..82fae0a 100755
--- a/tools/testing/selftests/firmware/driver_data.sh
+++ b/tools/testing/selftests/firmware/driver_data.sh
@@ -212,6 +212,22 @@ config_disable_keep()
 	fi
 }
 
+config_set_no_cache()
+{
+	if ! echo -n 1 >$DIR/config_no_cache; then
+		echo "$0: Unable to set to no_cache" >&2
+		exit 1
+	fi
+}
+
+config_disable_no_cache()
+{
+	if ! echo -n 0 >$DIR/config_no_cache; then
+		echo "$0: Unable to disable no_cache option" >&2
+		exit 1
+	fi
+}
+
 config_enable_opt_cb()
 {
 	if ! echo -n 1 >$DIR/config_enable_opt_cb; then
@@ -525,10 +541,30 @@ driver_data_test_0004a()
 	config_expect_result ${FUNCNAME[0]} SUCCESS
 }
 
+driver_data_test_0004s_no_cache()
+{
+	driver_data_set_sync_defaults
+	config_set_no_cache
+	config_trigger ${FUNCNAME[0]}
+	config_file_should_match ${FUNCNAME[0]}
+	config_expect_result ${FUNCNAME[0]} SUCCESS
+}
+
+driver_data_test_0004a_no_cache()
+{
+	driver_data_set_async_defaults
+	config_set_no_cache
+	config_trigger ${FUNCNAME[0]}
+	config_file_should_match ${FUNCNAME[0]}
+	config_expect_result ${FUNCNAME[0]} SUCCESS
+}
+
 driver_data_test_0004()
 {
 	driver_data_test_0004s
 	driver_data_test_0004a
+	driver_data_test_0004s_no_cache
+	driver_data_test_0004a_no_cache
 }
 
 driver_data_test_0005s()
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCHv2 0/3] Enable no_cache flag to driver_data
  2017-05-20  6:46 [PATCHv2 0/3] Enable no_cache flag to driver_data yi1.li
                   ` (2 preceding siblings ...)
  2017-05-20  6:46 ` [PATCHv2 3/3] test: add no_cache to driver_data load tester yi1.li
@ 2017-05-24 19:03 ` Luis R. Rodriguez
  2017-05-24 20:32   ` Luis R. Rodriguez
                     ` (2 more replies)
  3 siblings, 3 replies; 14+ messages in thread
From: Luis R. Rodriguez @ 2017-05-24 19:03 UTC (permalink / raw)
  To: yi1.li
  Cc: mcgrof, atull, gregkh, wagi, dwmw2, rafal, arend.vanspriel, rjw,
	moritz.fischer, pmladek, johannes.berg, emmanuel.grumbach,
	luciano.coelho, kvalo, luto, takahiro.akashi, dhowells, pjones,
	linux-kernel, linux-fpga

On Sat, May 20, 2017 at 01:46:56AM -0500, yi1.li@linux.intel.com wrote:
> From: Yi Li <yi1.li@linux.intel.com>
> 
> Changes in v2:
> 
>   - Rebase to Luis R. Rodriguez's 20170501-driver-data-try2
>     branch 
>   - Expose DRIVER_DATA_REQ_NO_CACHE flag to public 
>     driver_data_req_params structure, so upper drivers can ask
>     driver_data driver to bypass the internal caching mechanism.
>     This will be used for streaming and other drivers maintains
>     their own caching like iwlwifi. 
>   - Add self test cases.
> 
> 
> Yi Li (3):
>   firmware_class: move NO_CACHE from private to driver_data_req_params
>   iwlwifi: use DRIVER_DATA_REQ_NO_CACHE for driver_data
>   test: add no_cache to driver_data load tester
> 
>  drivers/base/firmware_class.c                   | 16 ++++-----
>  drivers/net/wireless/intel/iwlwifi/iwl-drv.c    |  2 ++
>  include/linux/driver_data.h                     |  4 +++
>  lib/test_driver_data.c                          | 43 +++++++++++++++++++++++--
>  tools/testing/selftests/firmware/driver_data.sh | 36 +++++++++++++++++++++
>  5 files changed, 89 insertions(+), 12 deletions(-)
> 

Good stuff, this series is looking good and very easy to read !  Only thing
though -- the cache test is just setting up the cache and ensuring it gets set,
it doesn't really *test* the cache is functional. Can you devise a test which
does ensure the cache is functional ?

We use the cache upon suspend to cache the firmware so that upon resume a
request will use that cache, to avoid the file lookup on disk. Doing a test
with qemu suspend + resume is possible but that requires having access to
the qemu monitor interface and doing something like this to trigger a wakeup:

echo system_wakeup | socat - /dev/pts/7,raw,echo=0,crnl

where /dev/pts/7 is assumed to be the PTY. This is rather complex for ksefltest,
so another option is to add a debug mode debugfs interface for firmware_class where
we can force-enable the cache mechanism without actually this being prompted by a
suspend/hibernate. Then we can just toggle this bit from a testing perspective and
excercise the caching mechanism.

So if you look at fw_pm_notify()

        switch (mode) {                                                         
        case PM_HIBERNATION_PREPARE:                                            
        case PM_SUSPEND_PREPARE:                                                
        case PM_RESTORE_PREPARE:                                                
                /*                                                              
                 * kill pending fallback requests with a custom fallback        
                 * to avoid stalling suspend.                                   
                 */                                                             
                kill_pending_fw_fallback_reqs(true);                            
                device_cache_fw_images();                                       
                disable_firmware();                                             
                break;    


kill_pending_fw_fallback_reqs(true), device_cache_fw_images(), and
disable_firmware() could be folder into a helper, then a debugfs interface
could kick that into action to put us cache mode as the
device_cache_fw_images() changes the cache state to FW_LOADER_START_CACHE, and
when this is done you'll notice that assign_firmware_buf() does:

        /*                                                                      
         * After caching firmware image is started, let it piggyback            
         * on request firmware.                                                 
         */                                                                     
        if (!driver_data_param_nocache(&data_params->priv_params) &&            
            buf->fwc->state == FW_LOADER_START_CACHE) {                         
                if (fw_cache_piggyback_on_request(buf->fw_id))                  
                        kref_get(&buf->ref);                                    
        }    

Which adds an incoming request to the cache. The first request that adds this cache
entry would be triggered by device_cache_fw_images() after the cache state is enabled:

static void device_cache_fw_images(void) 
{
	...
        fwc->state = FW_LOADER_START_CACHE;                                     
        dpm_for_each_dev(NULL, dev_cache_fw_image);  
	...
}

Subsequent requests then lookup for the cache through _request_firmware_prepare()
when fw_lookup_and_allocate_buf() is called. __fw_lookup_buf() really should be
renamed to something that reflects this is a cache lookup. In fact if you find
anything else that needs renaming to make it clear please feel free to send patches
for it.

We want to test that when caching is enabled, the cache is actually used.

Note that disable_firmware() above on the notifier *does* disable subsequent firmware
lookups but this is only *if* the lookup fails with _request_firmware_prepare():

_request_firmware(const struct firmware **firmware_p, const char *name,         
                  struct driver_data_params *data_params,                       
                  struct device *device)                                        
{                                                                               
        struct firmware *fw = NULL;                                             
        int ret;                                                                
                                                                                
        if (!firmware_p)                                                        
                return -EINVAL;                                                 
                                                                                
        if (!name || name[0] == '\0') {                                         
                ret = -EINVAL;                                                  
                goto out;                                                       
        }                                                                       
                                                                                
        ret = _request_firmware_prepare(&fw, name, device, data_params);        
        if (ret <= 0) /* error or already assigned */                           
                goto out;                                                       
                                                                                
        if (!firmware_enabled()) {                                              
                WARN(1, "firmware request while host is not available\n");      
                ret = -EHOSTDOWN;                                               
                goto out;                                                       
        }                                                 
	...
}

The idea is that _request_firmware_prepare() will have picked up the cache and
enabled use of that while the infrastructure for disk lookups is disabled. The
caching effect is lifted later on the same notifier fw_pm_notify() and it also
schedules a clearing of the cached firmwares with device_uncache_fw_images_delay().

To me this last part smells like a possible source of issue (not sure) if we might
suspend/resume a lot in short period of time, this theory could be tested by toggling
on/of this debugfs interface I suggested while having requests of different types
blast in.

This is the sort of testing which would really help here.

Likewise, if you are extending functionality please consider ways to break it :)
and test against it. Please think about these things carefully, its what will
change the stability for the better long term of our loader infrastructure.

  Luis

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCHv2 0/3] Enable no_cache flag to driver_data
  2017-05-24 19:03 ` [PATCHv2 0/3] Enable no_cache flag to driver_data Luis R. Rodriguez
@ 2017-05-24 20:32   ` Luis R. Rodriguez
  2017-05-25 22:30   ` Li, Yi
  2017-06-06 19:31   ` Li, Yi
  2 siblings, 0 replies; 14+ messages in thread
From: Luis R. Rodriguez @ 2017-05-24 20:32 UTC (permalink / raw)
  To: Luis R. Rodriguez, John Ewalt
  Cc: yi1.li, atull, gregkh, wagi, dwmw2, rafal, arend.vanspriel, rjw,
	moritz.fischer, pmladek, johannes.berg, emmanuel.grumbach,
	luciano.coelho, kvalo, luto, takahiro.akashi, dhowells, pjones,
	linux-kernel, linux-fpga

On Wed, May 24, 2017 at 09:03:57PM +0200, Luis R. Rodriguez wrote:
> __fw_lookup_buf() really should be
> renamed to something that reflects this is a cache lookup.

Actually I take this back, other than the cache, note that when we
fw_lookup_and_allocate_buf() we first __fw_lookup_buf() but if the buf is not
there we allocate a new one and list_add() it to the cache regardless of
whether or not the cache thing has been enabled.

What this does then *also* other than caching for suspend/resume (which we
should document more formally) is to gather up contending lookups together
with one buf, and share the final status of just one lookup. If a buf is
found we fw_state_wait() on _request_firmware_prepare() until the buf clears.

John Ewalt recently reported some issues with loadng multiple files at the
same time. He also provided a patch. The swake_up() fix seems sensible
and would seem to have been caused by the swait transformation, but in
inspecting the other proposed changes it would seem we have had tons of
other lingering bugs which have probably existed for ages.

For instance, if there are pending requests for a leader request to send back
info, and one is about to complete but in the last moment on
assign_firmware_buf() fails, all the error paths lack a wake up call. As such
all pending requests may just wait and linger, and since none of these have
a timeout I would expect these to just linger forever. I'm not even sure we
kref_get() properly on the buf for pending requests when we are waiting for
a serialized request, ie, we might be able to take a buf underneath the nose
of a waiter.

Although some are new bugs, some seem to be really old bugs.

These are the sorts of issues I wish for a test driver to be able to uncover,
test and ensure we never regress again. This is also why I am being careful
about enabling a feature, we should *really* think things through well before
enabling on the new API.

[0] https://bugzilla.kernel.org/show_bug.cgi?id=195477

  Luis

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCHv2 0/3] Enable no_cache flag to driver_data
  2017-05-24 19:03 ` [PATCHv2 0/3] Enable no_cache flag to driver_data Luis R. Rodriguez
  2017-05-24 20:32   ` Luis R. Rodriguez
@ 2017-05-25 22:30   ` Li, Yi
  2017-05-25 22:43     ` Luis R. Rodriguez
  2017-06-06 19:31   ` Li, Yi
  2 siblings, 1 reply; 14+ messages in thread
From: Li, Yi @ 2017-05-25 22:30 UTC (permalink / raw)
  To: Luis R. Rodriguez
  Cc: atull, gregkh, wagi, dwmw2, rafal, arend.vanspriel, rjw,
	moritz.fischer, pmladek, johannes.berg, emmanuel.grumbach,
	luciano.coelho, kvalo, luto, takahiro.akashi, dhowells, pjones,
	linux-kernel, linux-fpga

hi Luis


On 5/24/2017 2:03 PM, Luis R. Rodriguez wrote:
> On Sat, May 20, 2017 at 01:46:56AM -0500, yi1.li@linux.intel.com wrote:
>> From: Yi Li <yi1.li@linux.intel.com>
>>
>> Changes in v2:
>>
>>    - Rebase to Luis R. Rodriguez's 20170501-driver-data-try2
>>      branch
>>    - Expose DRIVER_DATA_REQ_NO_CACHE flag to public
>>      driver_data_req_params structure, so upper drivers can ask
>>      driver_data driver to bypass the internal caching mechanism.
>>      This will be used for streaming and other drivers maintains
>>      their own caching like iwlwifi.
>>    - Add self test cases.
>>
>>
>> Yi Li (3):
>>    firmware_class: move NO_CACHE from private to driver_data_req_params
>>    iwlwifi: use DRIVER_DATA_REQ_NO_CACHE for driver_data
>>    test: add no_cache to driver_data load tester
>>
>>   drivers/base/firmware_class.c                   | 16 ++++-----
>>   drivers/net/wireless/intel/iwlwifi/iwl-drv.c    |  2 ++
>>   include/linux/driver_data.h                     |  4 +++
>>   lib/test_driver_data.c                          | 43 +++++++++++++++++++++++--
>>   tools/testing/selftests/firmware/driver_data.sh | 36 +++++++++++++++++++++
>>   5 files changed, 89 insertions(+), 12 deletions(-)
>>
> Good stuff, this series is looking good and very easy to read !  Only thing
> though -- the cache test is just setting up the cache and ensuring it gets set,
> it doesn't really *test* the cache is functional. Can you devise a test which
> does ensure the cache is functional ?

This patch is for "disabling the cache" for streaming and iwlwifi case, 
adding the test to verify the cache function should be a separate patch, 
right? I can look more into the cache part.

Yi
>
> We use the cache upon suspend to cache the firmware so that upon resume a
> request will use that cache, to avoid the file lookup on disk. Doing a test
> with qemu suspend + resume is possible but that requires having access to
> the qemu monitor interface and doing something like this to trigger a wakeup:
>
> echo system_wakeup | socat - /dev/pts/7,raw,echo=0,crnl
>
> where /dev/pts/7 is assumed to be the PTY. This is rather complex for ksefltest,
> so another option is to add a debug mode debugfs interface for firmware_class where
> we can force-enable the cache mechanism without actually this being prompted by a
> suspend/hibernate. Then we can just toggle this bit from a testing perspective and
> excercise the caching mechanism.
>
> So if you look at fw_pm_notify()
>
>          switch (mode) {
>          case PM_HIBERNATION_PREPARE:
>          case PM_SUSPEND_PREPARE:
>          case PM_RESTORE_PREPARE:
>                  /*
>                   * kill pending fallback requests with a custom fallback
>                   * to avoid stalling suspend.
>                   */
>                  kill_pending_fw_fallback_reqs(true);
>                  device_cache_fw_images();
>                  disable_firmware();
>                  break;
>
>
> kill_pending_fw_fallback_reqs(true), device_cache_fw_images(), and
> disable_firmware() could be folder into a helper, then a debugfs interface
> could kick that into action to put us cache mode as the
> device_cache_fw_images() changes the cache state to FW_LOADER_START_CACHE, and
> when this is done you'll notice that assign_firmware_buf() does:
>
>          /*
>           * After caching firmware image is started, let it piggyback
>           * on request firmware.
>           */
>          if (!driver_data_param_nocache(&data_params->priv_params) &&
>              buf->fwc->state == FW_LOADER_START_CACHE) {
>                  if (fw_cache_piggyback_on_request(buf->fw_id))
>                          kref_get(&buf->ref);
>          }
>
> Which adds an incoming request to the cache. The first request that adds this cache
> entry would be triggered by device_cache_fw_images() after the cache state is enabled:
>
> static void device_cache_fw_images(void)
> {
> 	...
>          fwc->state = FW_LOADER_START_CACHE;
>          dpm_for_each_dev(NULL, dev_cache_fw_image);
> 	...
> }
>
> Subsequent requests then lookup for the cache through _request_firmware_prepare()
> when fw_lookup_and_allocate_buf() is called. __fw_lookup_buf() really should be
> renamed to something that reflects this is a cache lookup. In fact if you find
> anything else that needs renaming to make it clear please feel free to send patches
> for it.
>
> We want to test that when caching is enabled, the cache is actually used.
>
> Note that disable_firmware() above on the notifier *does* disable subsequent firmware
> lookups but this is only *if* the lookup fails with _request_firmware_prepare():
>
> _request_firmware(const struct firmware **firmware_p, const char *name,
>                    struct driver_data_params *data_params,
>                    struct device *device)
> {
>          struct firmware *fw = NULL;
>          int ret;
>                                                                                  
>          if (!firmware_p)
>                  return -EINVAL;
>                                                                                  
>          if (!name || name[0] == '\0') {
>                  ret = -EINVAL;
>                  goto out;
>          }
>                                                                                  
>          ret = _request_firmware_prepare(&fw, name, device, data_params);
>          if (ret <= 0) /* error or already assigned */
>                  goto out;
>                                                                                  
>          if (!firmware_enabled()) {
>                  WARN(1, "firmware request while host is not available\n");
>                  ret = -EHOSTDOWN;
>                  goto out;
>          }
> 	...
> }
>
> The idea is that _request_firmware_prepare() will have picked up the cache and
> enabled use of that while the infrastructure for disk lookups is disabled. The
> caching effect is lifted later on the same notifier fw_pm_notify() and it also
> schedules a clearing of the cached firmwares with device_uncache_fw_images_delay().
>
> To me this last part smells like a possible source of issue (not sure) if we might
> suspend/resume a lot in short period of time, this theory could be tested by toggling
> on/of this debugfs interface I suggested while having requests of different types
> blast in.
>
> This is the sort of testing which would really help here.
>
> Likewise, if you are extending functionality please consider ways to break it :)
> and test against it. Please think about these things carefully, its what will
> change the stability for the better long term of our loader infrastructure.
>
>    Luis
> --
> To unsubscribe from this list: send the line "unsubscribe linux-fpga" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCHv2 0/3] Enable no_cache flag to driver_data
  2017-05-25 22:30   ` Li, Yi
@ 2017-05-25 22:43     ` Luis R. Rodriguez
  2017-05-26 21:05       ` Li, Yi
  0 siblings, 1 reply; 14+ messages in thread
From: Luis R. Rodriguez @ 2017-05-25 22:43 UTC (permalink / raw)
  To: Li, Yi
  Cc: atull, Greg Kroah-Hartman, Daniel Wagner, David Woodhouse, rafal,
	Arend Van Spriel, Rafael J. Wysocki, Moritz Fischer, Petr Mladek,
	Johannes Berg, Emmanuel Grumbach, Luciano Coelho, Kalle Valo,
	Andy Lutomirski, AKASHI, Takahiro, David Howells, Peter Jones,
	linux-kernel, linux-fpga

On Thu, May 25, 2017 at 3:30 PM, Li, Yi <yi1.li@linux.intel.com> wrote:
> This patch is for "disabling the cache" for streaming and iwlwifi case,
> adding the test to verify the cache function should be a separate patch,
> right? I can look more into the cache part.

How can we know cache was disabled without first testing what having
cache enabled could be like ?

  Luis

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCHv2 0/3] Enable no_cache flag to driver_data
  2017-05-25 22:43     ` Luis R. Rodriguez
@ 2017-05-26 21:05       ` Li, Yi
  2017-05-26 21:13         ` Luis R. Rodriguez
  0 siblings, 1 reply; 14+ messages in thread
From: Li, Yi @ 2017-05-26 21:05 UTC (permalink / raw)
  To: Luis R. Rodriguez
  Cc: atull, Greg Kroah-Hartman, Daniel Wagner, David Woodhouse, rafal,
	Arend Van Spriel, Rafael J. Wysocki, Moritz Fischer, Petr Mladek,
	Johannes Berg, Emmanuel Grumbach, Luciano Coelho, Kalle Valo,
	Andy Lutomirski, AKASHI, Takahiro, David Howells, Peter Jones,
	linux-kernel, linux-fpga

hi Luis


On 5/25/2017 5:43 PM, Luis R. Rodriguez wrote:
> On Thu, May 25, 2017 at 3:30 PM, Li, Yi <yi1.li@linux.intel.com> wrote:
>> This patch is for "disabling the cache" for streaming and iwlwifi case,
>> adding the test to verify the cache function should be a separate patch,
>> right? I can look more into the cache part.
> How can we know cache was disabled without first testing what having
> cache enabled could be like ?

Understand the point, adding the test for cache enabled still cannot 
cover the disable cache part though:-). Please give me couple days to 
think about it. Maybe will first have a new patch to test the cache 
part, likely through the debugfs as you suggested; then amend this 
series to complete the cache disabling test part. Thanks for the insight.

Yi
>
>    Luis
> --
> To unsubscribe from this list: send the line "unsubscribe linux-fpga" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCHv2 0/3] Enable no_cache flag to driver_data
  2017-05-26 21:05       ` Li, Yi
@ 2017-05-26 21:13         ` Luis R. Rodriguez
  0 siblings, 0 replies; 14+ messages in thread
From: Luis R. Rodriguez @ 2017-05-26 21:13 UTC (permalink / raw)
  To: Li, Yi
  Cc: Luis R. Rodriguez, atull, Greg Kroah-Hartman, Daniel Wagner,
	David Woodhouse, rafal, Arend Van Spriel, Rafael J. Wysocki,
	Moritz Fischer, Petr Mladek, Johannes Berg, Emmanuel Grumbach,
	Luciano Coelho, Kalle Valo, Andy Lutomirski, AKASHI, Takahiro,
	David Howells, Peter Jones, linux-kernel, linux-fpga

On Fri, May 26, 2017 at 04:05:43PM -0500, Li, Yi wrote:
> hi Luis
> 
> 
> On 5/25/2017 5:43 PM, Luis R. Rodriguez wrote:
> > On Thu, May 25, 2017 at 3:30 PM, Li, Yi <yi1.li@linux.intel.com> wrote:
> > > This patch is for "disabling the cache" for streaming and iwlwifi case,
> > > adding the test to verify the cache function should be a separate patch,
> > > right? I can look more into the cache part.
> > How can we know cache was disabled without first testing what having
> > cache enabled could be like ?
> 
> Understand the point, adding the test for cache enabled still cannot cover
> the disable cache part though:-). Please give me couple days to think about
> it. Maybe will first have a new patch to test the cache part, likely through
> the debugfs as you suggested; then amend this series to complete the cache
> disabling test part. Thanks for the insight.

Sounds good!

  Luis

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCHv2 0/3] Enable no_cache flag to driver_data
  2017-05-24 19:03 ` [PATCHv2 0/3] Enable no_cache flag to driver_data Luis R. Rodriguez
  2017-05-24 20:32   ` Luis R. Rodriguez
  2017-05-25 22:30   ` Li, Yi
@ 2017-06-06 19:31   ` Li, Yi
  2017-06-07 17:59     ` Luis R. Rodriguez
  2 siblings, 1 reply; 14+ messages in thread
From: Li, Yi @ 2017-06-06 19:31 UTC (permalink / raw)
  To: Luis R. Rodriguez
  Cc: atull, gregkh, wagi, dwmw2, rafal, arend.vanspriel, rjw,
	moritz.fischer, pmladek, johannes.berg, emmanuel.grumbach,
	luciano.coelho, kvalo, luto, takahiro.akashi, dhowells, pjones,
	linux-kernel, linux-fpga

Hi Luis,


On 5/24/2017 2:03 PM, Luis R. Rodriguez wrote:
> On Sat, May 20, 2017 at 01:46:56AM -0500, yi1.li@linux.intel.com wrote:
>> From: Yi Li <yi1.li@linux.intel.com>
>>
>> Changes in v2:
>>
>>    - Rebase to Luis R. Rodriguez's 20170501-driver-data-try2
>>      branch
>>    - Expose DRIVER_DATA_REQ_NO_CACHE flag to public
>>      driver_data_req_params structure, so upper drivers can ask
>>      driver_data driver to bypass the internal caching mechanism.
>>      This will be used for streaming and other drivers maintains
>>      their own caching like iwlwifi.
>>    - Add self test cases.
>>
>>
>> Yi Li (3):
>>    firmware_class: move NO_CACHE from private to driver_data_req_params
>>    iwlwifi: use DRIVER_DATA_REQ_NO_CACHE for driver_data
>>    test: add no_cache to driver_data load tester
>>
>>   drivers/base/firmware_class.c                   | 16 ++++-----
>>   drivers/net/wireless/intel/iwlwifi/iwl-drv.c    |  2 ++
>>   include/linux/driver_data.h                     |  4 +++
>>   lib/test_driver_data.c                          | 43 +++++++++++++++++++++++--
>>   tools/testing/selftests/firmware/driver_data.sh | 36 +++++++++++++++++++++
>>   5 files changed, 89 insertions(+), 12 deletions(-)
>>
> Good stuff, this series is looking good and very easy to read !  Only thing
> though -- the cache test is just setting up the cache and ensuring it gets set,
> it doesn't really *test* the cache is functional. Can you devise a test which
> does ensure the cache is functional ?
>
> We use the cache upon suspend to cache the firmware so that upon resume a
> request will use that cache, to avoid the file lookup on disk. Doing a test
> with qemu suspend + resume is possible but that requires having access to
> the qemu monitor interface and doing something like this to trigger a wakeup:
>
> echo system_wakeup | socat - /dev/pts/7,raw,echo=0,crnl
>
> where /dev/pts/7 is assumed to be the PTY. This is rather complex for ksefltest,
> so another option is to add a debug mode debugfs interface for firmware_class where
> we can force-enable the cache mechanism without actually this being prompted by a
> suspend/hibernate. Then we can just toggle this bit from a testing perspective and
> excercise the caching mechanism.
>
> So if you look at fw_pm_notify()
>
>          switch (mode) {
>          case PM_HIBERNATION_PREPARE:
>          case PM_SUSPEND_PREPARE:
>          case PM_RESTORE_PREPARE:
>                  /*
>                   * kill pending fallback requests with a custom fallback
>                   * to avoid stalling suspend.
>                   */
>                  kill_pending_fw_fallback_reqs(true);
>                  device_cache_fw_images();
>                  disable_firmware();
>                  break;
>
I am studying at the firmware caching codes and have to say it's very 
complicated. :-( Here are some questions:

1. Since device_cache_fw_images invokes dev_cache_fw_image through 
dpm_for_each_dev, adding a debugfs driver to kick it can only cache 
firmware for those associated with devices which has PM enabled, which 
do not include the driver_data_test_device. Any suggestions?

2. Look into dev_cache_fw_image function, devres_for_each_res will walk through the firmware have been loaded before (through assign_firmware_buf -> fw_add_devm_name) and add to the todo list, eventually it will create the fw_names list. So in the test driver, we need to load the firmware once before calling the kick?
dev_cache_fw_image(struct device *dev, void *data)

{

LIST_HEAD(todo);

struct fw_cache_entry *fce;

struct fw_cache_entry *fce_next;

struct firmware_cache *fwc = &fw_cache;

devres_for_each_res(dev, fw_name_devm_release,

devm_name_match, &fw_cache,

dev_create_fw_entry, &todo);

list_for_each_entry_safe(fce, fce_next, &todo, list) {

list_del(&fce->list);

spin_lock(&fwc->name_lock);

/* only one cache entry for one firmware */

if (!__fw_entry_found(fce->name)) {

list_add(&fce->list, &fwc->fw_names);

} else {

free_fw_cache_entry(fce);

           ...
}
> kill_pending_fw_fallback_reqs(true), device_cache_fw_images(), and
> disable_firmware() could be folder into a helper, then a debugfs interface
> could kick that into action to put us cache mode as the
> device_cache_fw_images() changes the cache state to FW_LOADER_START_CACHE, and
> when this is done you'll notice that assign_firmware_buf() does:
>
>          /*
>           * After caching firmware image is started, let it piggyback
>           * on request firmware.
>           */
>          if (!driver_data_param_nocache(&data_params->priv_params) &&
>              buf->fwc->state == FW_LOADER_START_CACHE) {
>                  if (fw_cache_piggyback_on_request(buf->fw_id))
>                          kref_get(&buf->ref);
>          }
>
> Which adds an incoming request to the cache. The first request that adds this cache
> entry would be triggered by device_cache_fw_images() after the cache state is enabled:
>
> static void device_cache_fw_images(void)
> {
> 	...
>          fwc->state = FW_LOADER_START_CACHE;
>          dpm_for_each_dev(NULL, dev_cache_fw_image);
> 	...
> }
This only applies to the devices have PM enabled.
> Subsequent requests then lookup for the cache through _request_firmware_prepare()
> when fw_lookup_and_allocate_buf() is called. __fw_lookup_buf() really should be
> renamed to something that reflects this is a cache lookup. In fact if you find
> anything else that needs renaming to make it clear please feel free to send patches
> for it.
>
> We want to test that when caching is enabled, the cache is actually used.
>
> Note that disable_firmware() above on the notifier *does* disable subsequent firmware
> lookups but this is only *if* the lookup fails with _request_firmware_prepare():
>
> _request_firmware(const struct firmware **firmware_p, const char *name,
>                    struct driver_data_params *data_params,
>                    struct device *device)
> {
>          struct firmware *fw = NULL;
>          int ret;
>                                                                                  
>          if (!firmware_p)
>                  return -EINVAL;
>                                                                                  
>          if (!name || name[0] == '\0') {
>                  ret = -EINVAL;
>                  goto out;
>          }
>                                                                                  
>          ret = _request_firmware_prepare(&fw, name, device, data_params);
>          if (ret <= 0) /* error or already assigned */
>                  goto out;
>                                                                                  
>          if (!firmware_enabled()) {
>                  WARN(1, "firmware request while host is not available\n");
>                  ret = -EHOSTDOWN;
>                  goto out;
>          }
> 	...
> }
>
> The idea is that _request_firmware_prepare() will have picked up the cache and
> enabled use of that while the infrastructure for disk lookups is disabled. The
> caching effect is lifted later on the same notifier fw_pm_notify() and it also
> schedules a clearing of the cached firmwares with device_uncache_fw_images_delay().

device_uncache_fw_images_delay() will be called for PM_POST_RESTORE, which means the hibernation restore got error. How about the successful restore case, calling driver will free its firmware_buf after loading?

> To me this last part smells like a possible source of issue (not sure) if we might
> suspend/resume a lot in short period of time, this theory could be tested by toggling
> on/of this debugfs interface I suggested while having requests of different types
> blast in.
Agree, it might create an issue if the system is get into 
restore_prepare again before the device_uncache_fw_images_delay clear 
the cache, why we need the 10 * MSEC_PER_SEC delay? In theory, 
fwc->name_lock should protect the case though.
>
> This is the sort of testing which would really help here.
>
> Likewise, if you are extending functionality please consider ways to break it :)
Understand the need to test the firmware caching part. For non-caching 
test case, will it be enough if we can test that the noncache setting 
will ban the firmware name be added to the fwc->fw_names list?
> and test against it. Please think about these things carefully, its what will
> change the stability for the better long term of our loader infrastructure.
>
>    Luis
> --
> To unsubscribe from this list: send the line "unsubscribe linux-fpga" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCHv2 0/3] Enable no_cache flag to driver_data
  2017-06-06 19:31   ` Li, Yi
@ 2017-06-07 17:59     ` Luis R. Rodriguez
  2017-06-07 21:00       ` Li, Yi
  0 siblings, 1 reply; 14+ messages in thread
From: Luis R. Rodriguez @ 2017-06-07 17:59 UTC (permalink / raw)
  To: Li, Yi
  Cc: Luis R. Rodriguez, atull, gregkh, wagi, dwmw2, rafal,
	arend.vanspriel, rjw, moritz.fischer, pmladek, johannes.berg,
	emmanuel.grumbach, luciano.coelho, kvalo, luto, takahiro.akashi,
	dhowells, pjones, linux-kernel, linux-fpga

On Tue, Jun 06, 2017 at 02:31:54PM -0500, Li, Yi wrote:
> > We use the cache upon suspend to cache the firmware so that upon resume a
> > request will use that cache, to avoid the file lookup on disk. Doing a test
> > with qemu suspend + resume is possible but that requires having access to
> > the qemu monitor interface and doing something like this to trigger a wakeup:
> > 
> > echo system_wakeup | socat - /dev/pts/7,raw,echo=0,crnl

BTW if it helps for testing:

https://github.com/mcgrof/kvm-boot

> I am studying at the firmware caching codes and have to say it's very
> complicated. :-( Here are some questions:

It is! For this reason I tried to document it, have you read this:

https://www.kernel.org/doc/html/latest/driver-api/firmware/firmware_cache.html

This is certainly missing the notes about how some of this is loosely re-used
for possible duplicate requests though, but I think I've mentioned this on
my review of your patches so far.

> 1. Since device_cache_fw_images invokes dev_cache_fw_image through
> dpm_for_each_dev, adding a debugfs driver to kick it can only cache firmware
> for those associated with devices which has PM enabled, which do not include
> the driver_data_test_device. Any suggestions?

Ah, consider adding PM to driver_data_test_device ?

May be worth updating the documentation bout this requirement too!

> 2. Look into dev_cache_fw_image function, devres_for_each_res will walk
> through the firmware have been loaded before (through assign_firmware_buf ->
> fw_add_devm_name) and add to the todo list, eventually it will create the
> fw_names list. So in the test driver, we need to load the firmware once
> before calling the kick?

Yes, makes sense, given its a firmware cache, it would only make sense to
cache firmware for requests already made.

An important note I made in the documentation which you did not mention 
but should be important for your own sanity:

"The firmware devres entry is maintained throughout the lifetime of the device.
This means that even if you release_firmware() the firmware cache will still be
used on resume from suspend."

This means the cache will be tried even if the driver did use release_firmware()
after request_firmware().

> > static void device_cache_fw_images(void)
> > {
> > 	...
> >          fwc->state = FW_LOADER_START_CACHE;
> >          dpm_for_each_dev(NULL, dev_cache_fw_image);
> > 	...
> > }
> This only applies to the devices have PM enabled.

Makes sense!

> 
> device_uncache_fw_images_delay() will be called for PM_POST_RESTORE, which
> means the hibernation restore got error. How about the successful restore
> case, calling driver will free its firmware_buf after loading?

As it is on my latest 20170605-driver-data [0] (but should be just as yours if you
used a recent branch of mine), fw_pm_notify() uses:

static int fw_pm_notify(struct notifier_block *notify_block,
			unsigned long mode, void *unused)
{
	switch (mode) {
	...
	case PM_POST_SUSPEND:
	case PM_POST_HIBERNATION:
	case PM_POST_RESTORE:
		/*
		 * In case that system sleep failed and syscore_suspend is
		 * not called.
		 */
		mutex_lock(&fw_lock);
		fw_cache.state = FW_LOADER_NO_CACHE;
		mutex_unlock(&fw_lock);
		enable_firmware();

		device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
		break;
        } 
}

So it uses also PM_POST_RESTORE. I think that covers it all ? As it
is today we handle the firmware cache for all drivers, it should be
transparent to drivers.

[0] https://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux-next.git/log/?h=20170605-driver-data

> > To me this last part smells like a possible source of issue (not sure) if we might
> > suspend/resume a lot in short period of time, this theory could be tested by toggling
> > on/of this debugfs interface I suggested while having requests of different types
> > blast in.
> Agree, it might create an issue if the system is get into restore_prepare
> again before the device_uncache_fw_images_delay clear the cache, why we need
> the 10 * MSEC_PER_SEC delay? In theory, fwc->name_lock should protect the
> case though.

One possible solution may be to cancel_delayed_work(&fw_cache.work)
or cancel_delayed_work_sync(&fw_cache.work) on suspend, and then
force it to run *right away* so we clear any pending old cache.

One performance issue with this is we are clearing some possible cache we are
about to re-create, so mod_delayed_work() seems more efficient -- provided we
have accounting notes to know no new firmware requests have trickled in since
the last cache build. This seems rather complicated so a first initial approach
to just kill all known cache before suspend seems easy and fair.

BTW such a fix might be a stable candidate if you find a real behavioural
issue with the kernel !

> > This is the sort of testing which would really help here.
> > 
> > Likewise, if you are extending functionality please consider ways to break it :)
> Understand the need to test the firmware caching part. For non-caching test
> case, will it be enough if we can test that the noncache setting will ban
> the firmware name be added to the fwc->fw_names list?

Good question. Let's see...  in the future a device might have two requests
with the same firmware, one with a cache enabled and one without it -- I suppose
for now we should perhaps disable a non-cache request if one already exists
with a cache request ? The code for that probably is not there... But other than
this corner case...

Yeah I think that's a reasonable test case. Such a code seems may need a debug
export symbol to allow drivers to query for this info, if so feel free to a
respective debug kconfig entry for it.

  Luis

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCHv2 0/3] Enable no_cache flag to driver_data
  2017-06-07 17:59     ` Luis R. Rodriguez
@ 2017-06-07 21:00       ` Li, Yi
  2017-06-07 23:02         ` Luis R. Rodriguez
  0 siblings, 1 reply; 14+ messages in thread
From: Li, Yi @ 2017-06-07 21:00 UTC (permalink / raw)
  To: Luis R. Rodriguez
  Cc: atull, gregkh, wagi, dwmw2, rafal, arend.vanspriel, rjw,
	moritz.fischer, pmladek, johannes.berg, emmanuel.grumbach,
	luciano.coelho, kvalo, luto, takahiro.akashi, dhowells, pjones,
	linux-kernel, linux-fpga

On 6/7/2017 12:59 PM, Luis R. Rodriguez wrote:
> On Tue, Jun 06, 2017 at 02:31:54PM -0500, Li, Yi wrote:
>>> We use the cache upon suspend to cache the firmware so that upon resume a
>>> request will use that cache, to avoid the file lookup on disk. Doing a test
>>> with qemu suspend + resume is possible but that requires having access to
>>> the qemu monitor interface and doing something like this to trigger a wakeup:
>>>
>>> echo system_wakeup | socat - /dev/pts/7,raw,echo=0,crnl
>
> BTW if it helps for testing:
>
> https://github.com/mcgrof/kvm-boot

Thanks, will explore that.

>
>> I am studying at the firmware caching codes and have to say it's very
>> complicated. :-( Here are some questions:
>
> It is! For this reason I tried to document it, have you read this:
>
> https://www.kernel.org/doc/html/latest/driver-api/firmware/firmware_cache.html

Good info, thanks.

>
> This is certainly missing the notes about how some of this is loosely re-used
> for possible duplicate requests though, but I think I've mentioned this on
> my review of your patches so far.
>
>> 1. Since device_cache_fw_images invokes dev_cache_fw_image through
>> dpm_for_each_dev, adding a debugfs driver to kick it can only cache firmware
>> for those associated with devices which has PM enabled, which do not include
>> the driver_data_test_device. Any suggestions?
>
> Ah, consider adding PM to driver_data_test_device ?
>

That's what I am looking now. But per my understanding, the misc_device 
does not have the hook to add PM support like those platform device 
drivers do. Please let me know if there is a way to do that.

> May be worth updating the documentation bout this requirement too!
>
>> 2. Look into dev_cache_fw_image function, devres_for_each_res will walk
>> through the firmware have been loaded before (through assign_firmware_buf ->
>> fw_add_devm_name) and add to the todo list, eventually it will create the
>> fw_names list. So in the test driver, we need to load the firmware once
>> before calling the kick?
>
> Yes, makes sense, given its a firmware cache, it would only make sense to
> cache firmware for requests already made.
>
> An important note I made in the documentation which you did not mention
> but should be important for your own sanity:
>
> "The firmware devres entry is maintained throughout the lifetime of the device.
> This means that even if you release_firmware() the firmware cache will still be
> used on resume from suspend."
>
> This means the cache will be tried even if the driver did use release_firmware()
> after request_firmware().

Yes, it makes sense and good to know and it's explained in the 
kernel.org link you pointed out earlier. The devres entry records all 
the firmware have been loaded before, it will save restore prepare time 
for all the drivers to mark their firmware "no cache" if there is no 
need to reload the firmware during restore.

>
>>> static void device_cache_fw_images(void)
>>> {
>>> 	...
>>>          fwc->state = FW_LOADER_START_CACHE;
>>>          dpm_for_each_dev(NULL, dev_cache_fw_image);
>>> 	...
>>> }
>> This only applies to the devices have PM enabled.
>
> Makes sense!
>
>>
>> device_uncache_fw_images_delay() will be called for PM_POST_RESTORE, which
>> means the hibernation restore got error. How about the successful restore
>> case, calling driver will free its firmware_buf after loading?
>
> As it is on my latest 20170605-driver-data [0] (but should be just as yours if you
> used a recent branch of mine), fw_pm_notify() uses:
>
> static int fw_pm_notify(struct notifier_block *notify_block,
> 			unsigned long mode, void *unused)
> {
> 	switch (mode) {
> 	...
> 	case PM_POST_SUSPEND:
> 	case PM_POST_HIBERNATION:
> 	case PM_POST_RESTORE:
> 		/*
> 		 * In case that system sleep failed and syscore_suspend is
> 		 * not called.
> 		 */
> 		mutex_lock(&fw_lock);
> 		fw_cache.state = FW_LOADER_NO_CACHE;
> 		mutex_unlock(&fw_lock);
> 		enable_firmware();
>
> 		device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
> 		break;
>         }
> }
>
> So it uses also PM_POST_RESTORE. I think that covers it all ? As it
> is today we handle the firmware cache for all drivers, it should be
> transparent to drivers.

Ah, the comment of "sleep failed" mislead me. :)

>
> [0] https://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux-next.git/log/?h=20170605-driver-data
>
>>> To me this last part smells like a possible source of issue (not sure) if we might
>>> suspend/resume a lot in short period of time, this theory could be tested by toggling
>>> on/of this debugfs interface I suggested while having requests of different types
>>> blast in.
>> Agree, it might create an issue if the system is get into restore_prepare
>> again before the device_uncache_fw_images_delay clear the cache, why we need
>> the 10 * MSEC_PER_SEC delay? In theory, fwc->name_lock should protect the
>> case though.
>
> One possible solution may be to cancel_delayed_work(&fw_cache.work)
> or cancel_delayed_work_sync(&fw_cache.work) on suspend, and then
> force it to run *right away* so we clear any pending old cache.
>
> One performance issue with this is we are clearing some possible cache we are
> about to re-create, so mod_delayed_work() seems more efficient -- provided we
> have accounting notes to know no new firmware requests have trickled in since
> the last cache build. This seems rather complicated so a first initial approach
> to just kill all known cache before suspend seems easy and fair.
>
> BTW such a fix might be a stable candidate if you find a real behavioural
> issue with the kernel !
>
>>> This is the sort of testing which would really help here.
>>>
>>> Likewise, if you are extending functionality please consider ways to break it :)
>> Understand the need to test the firmware caching part. For non-caching test
>> case, will it be enough if we can test that the noncache setting will ban
>> the firmware name be added to the fwc->fw_names list?
>
> Good question. Let's see...  in the future a device might have two requests
> with the same firmware, one with a cache enabled and one without it -- I suppose
> for now we should perhaps disable a non-cache request if one already exists
> with a cache request ? The code for that probably is not there... But other than
> this corner case...
>
> Yeah I think that's a reasonable test case. Such a code seems may need a debug
> export symbol to allow drivers to query for this info, if so feel free to a
> respective debug kconfig entry for it.

Sounds good, thanks!

>
>   Luis
> --
> To unsubscribe from this list: send the line "unsubscribe linux-fpga" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCHv2 0/3] Enable no_cache flag to driver_data
  2017-06-07 21:00       ` Li, Yi
@ 2017-06-07 23:02         ` Luis R. Rodriguez
  0 siblings, 0 replies; 14+ messages in thread
From: Luis R. Rodriguez @ 2017-06-07 23:02 UTC (permalink / raw)
  To: Li, Yi
  Cc: Luis R. Rodriguez, atull, gregkh, wagi, dwmw2, rafal,
	arend.vanspriel, rjw, moritz.fischer, pmladek, johannes.berg,
	emmanuel.grumbach, luciano.coelho, kvalo, luto, takahiro.akashi,
	dhowells, pjones, linux-kernel, linux-fpga

On Wed, Jun 07, 2017 at 04:00:42PM -0500, Li, Yi wrote:
> On 6/7/2017 12:59 PM, Luis R. Rodriguez wrote:
> > Ah, consider adding PM to driver_data_test_device ?
> 
> That's what I am looking now. But per my understanding, the misc_device does
> not have the hook to add PM support like those platform device drivers do.
> Please let me know if there is a way to do that.

Ah, hrm. Yeah I'd have to look into it, why does misc devs not have support?

Otherwise you could see if you can just modify the test driver to be a platform
driver, these are easy to register, see. For a simple example see
platform_device_register_simple() use on net/wireless/reg.c.

> > > device_uncache_fw_images_delay() will be called for PM_POST_RESTORE, which
> > > means the hibernation restore got error. How about the successful restore
> > > case, calling driver will free its firmware_buf after loading?
> > 
> > As it is on my latest 20170605-driver-data [0] (but should be just as yours if you
> > used a recent branch of mine), fw_pm_notify() uses:
> > 
> > static int fw_pm_notify(struct notifier_block *notify_block,
> > 			unsigned long mode, void *unused)
> > {
> > 	switch (mode) {
> > 	...
> > 	case PM_POST_SUSPEND:
> > 	case PM_POST_HIBERNATION:
> > 	case PM_POST_RESTORE:
> > 		/*
> > 		 * In case that system sleep failed and syscore_suspend is
> > 		 * not called.
> > 		 */
> > 		mutex_lock(&fw_lock);
> > 		fw_cache.state = FW_LOADER_NO_CACHE;
> > 		mutex_unlock(&fw_lock);
> > 		enable_firmware();
> > 
> > 		device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
> > 		break;
> >         }
> > }
> > 
> > So it uses also PM_POST_RESTORE. I think that covers it all ? As it
> > is today we handle the firmware cache for all drivers, it should be
> > transparent to drivers.
> 
> Ah, the comment of "sleep failed" mislead me. :)

Ah no that comment is about more of a brain fuck with the internals of suspend
on Linux, this may be very confusing. Its for this reason why I recently
submitted a patch which helps explain all this more, the patch was recently
merged by Greg KH, it expands on the documentation of fw_pm_notify() so let me
paste the diagram which is relevant form the documentation added:

/**
 * fw_pm_notify - notifier for suspend/resume
 * @notify_block: unused
 * @mode: mode we are switching to
 * @unused: unused
 *
 * Used to modify the firmware_class state as we move in between states.
 * The firmware_class implements a firmware cache to enable device driver
 * to fetch firmware upon resume before the root filesystem is ready. We
 * disable API calls which do not use the built-in firmware or the firmware
 * cache when we know these calls will not work.
 *
 * The inner logic behind all this is a bit complex so it is worth summarizing
 * the kernel's own suspend/resume process with context and focus on how this
 * can impact the firmware API.
 *
 * First a review on how we go to suspend::
 *
 *	pm_suspend() --> enter_state() -->
 *	sys_sync()
 *	suspend_prepare() -->
 *		__pm_notifier_call_chain(PM_SUSPEND_PREPARE, ...);
 *		suspend_freeze_processes() -->
 *			freeze_processes() -->
 *				__usermodehelper_set_disable_depth(UMH_DISABLED);
 *				freeze all tasks ...
 *			freeze_kernel_threads()
 *	suspend_devices_and_enter() -->
 *		dpm_suspend_start() -->
 *				dpm_prepare()
 *				dpm_suspend()
 *		suspend_enter()  -->
 *			platform_suspend_prepare()
 *			dpm_suspend_late()
 *			freeze_enter()
 *			syscore_suspend()
 *
 * When we resume we bail out of a loop from suspend_devices_and_enter() and
 * unwind back out to the caller enter_state() where we were before as follows::
 *
 * 	enter_state() -->
 *	suspend_devices_and_enter() --> (bail from loop)
 *		dpm_resume_end() -->
 *			dpm_resume()
 *			dpm_complete()
 *	suspend_finish() -->
 *		suspend_thaw_processes() -->
 *			thaw_processes() -->
 *				__usermodehelper_set_disable_depth(UMH_FREEZING);
 *				thaw_workqueues();
 *				thaw all processes ...
 *				usermodehelper_enable();
 *		pm_notifier_call_chain(PM_POST_SUSPEND);
 *
 * fw_pm_notify() works through pm_notifier_call_chain().
 */

The key is that even if we fail at syscore_suspend() we will bail out of the
loop on suspend_devices_and_enter() and eventually get our notifier called
with PM_POST_SUSPEND, in the case of suspend/resume. The reason the
comment is there is that even though we have the notifier we also have
the fw_syscore_ops, and our fw_suspend(). If our fw_suspend() fails to
get called then this does not happen:

static int fw_suspend(void)
{
	fw_cache.state = FW_LOADER_NO_CACHE;
	return 0;                                                               
}

  Luis

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2017-06-07 23:02 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-20  6:46 [PATCHv2 0/3] Enable no_cache flag to driver_data yi1.li
2017-05-20  6:46 ` [PATCHv2 1/3] firmware_class: move NO_CACHE from private to driver_data_req_params yi1.li
2017-05-20  6:46 ` [PATCHv2 2/3] iwlwifi: use DRIVER_DATA_REQ_NO_CACHE for driver_data yi1.li
2017-05-20  6:46 ` [PATCHv2 3/3] test: add no_cache to driver_data load tester yi1.li
2017-05-24 19:03 ` [PATCHv2 0/3] Enable no_cache flag to driver_data Luis R. Rodriguez
2017-05-24 20:32   ` Luis R. Rodriguez
2017-05-25 22:30   ` Li, Yi
2017-05-25 22:43     ` Luis R. Rodriguez
2017-05-26 21:05       ` Li, Yi
2017-05-26 21:13         ` Luis R. Rodriguez
2017-06-06 19:31   ` Li, Yi
2017-06-07 17:59     ` Luis R. Rodriguez
2017-06-07 21:00       ` Li, Yi
2017-06-07 23:02         ` Luis R. Rodriguez

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.