Linux Audio

Check our new training course

Loading...
  1// SPDX-License-Identifier: GPL-2.0
  2
  3#include <linux/types.h>
  4#include <linux/kconfig.h>
  5#include <linux/list.h>
  6#include <linux/security.h>
  7#include <linux/umh.h>
  8#include <linux/sysctl.h>
  9#include <linux/module.h>
 10
 11#include "fallback.h"
 12#include "firmware.h"
 13
 14/*
 15 * firmware fallback mechanism
 16 */
 17
 18/*
 19 * use small loading timeout for caching devices' firmware because all these
 20 * firmware images have been loaded successfully at lease once, also system is
 21 * ready for completing firmware loading now. The maximum size of firmware in
 22 * current distributions is about 2M bytes, so 10 secs should be enough.
 23 */
 24void fw_fallback_set_cache_timeout(void)
 25{
 26	fw_fallback_config.old_timeout = __firmware_loading_timeout();
 27	__fw_fallback_set_timeout(10);
 28}
 29
 30/* Restores the timeout to the value last configured during normal operation */
 31void fw_fallback_set_default_timeout(void)
 32{
 33	__fw_fallback_set_timeout(fw_fallback_config.old_timeout);
 34}
 35
 36static long firmware_loading_timeout(void)
 37{
 38	return __firmware_loading_timeout() > 0 ?
 39		__firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET;
 40}
 41
 42static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv,  long timeout)
 43{
 44	return __fw_state_wait_common(fw_priv, timeout);
 45}
 46
 47static LIST_HEAD(pending_fw_head);
 48
 49void kill_pending_fw_fallback_reqs(bool only_kill_custom)
 50{
 51	struct fw_priv *fw_priv;
 52	struct fw_priv *next;
 53
 54	mutex_lock(&fw_lock);
 55	list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
 56				 pending_list) {
 57		if (!fw_priv->need_uevent || !only_kill_custom)
 58			 __fw_load_abort(fw_priv);
 59	}
 
 
 
 
 60	mutex_unlock(&fw_lock);
 61}
 62
 63/**
 64 * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
 65 * @fw_sysfs: firmware sysfs information for the firmware to load
 66 * @timeout: timeout to wait for the load
 67 *
 68 * In charge of constructing a sysfs fallback interface for firmware loading.
 69 **/
 70static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
 71{
 72	int retval = 0;
 73	struct device *f_dev = &fw_sysfs->dev;
 74	struct fw_priv *fw_priv = fw_sysfs->fw_priv;
 75
 76	/* fall back on userspace loading */
 77	if (!fw_priv->data)
 78		fw_priv->is_paged_buf = true;
 79
 80	dev_set_uevent_suppress(f_dev, true);
 81
 82	retval = device_add(f_dev);
 83	if (retval) {
 84		dev_err(f_dev, "%s: device_register failed\n", __func__);
 85		goto err_put_dev;
 86	}
 87
 88	mutex_lock(&fw_lock);
 89	if (fw_state_is_aborted(fw_priv)) {
 90		mutex_unlock(&fw_lock);
 91		retval = -EINTR;
 92		goto out;
 93	}
 94	list_add(&fw_priv->pending_list, &pending_fw_head);
 95	mutex_unlock(&fw_lock);
 96
 97	if (fw_priv->opt_flags & FW_OPT_UEVENT) {
 98		fw_priv->need_uevent = true;
 99		dev_set_uevent_suppress(f_dev, false);
100		dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
101		kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
102	} else {
103		timeout = MAX_JIFFY_OFFSET;
104	}
105
106	retval = fw_sysfs_wait_timeout(fw_priv, timeout);
107	if (retval < 0 && retval != -ENOENT) {
108		mutex_lock(&fw_lock);
109		fw_load_abort(fw_sysfs);
110		mutex_unlock(&fw_lock);
111	}
112
113	if (fw_state_is_aborted(fw_priv)) {
114		if (retval == -ERESTARTSYS)
115			retval = -EINTR;
116	} else if (fw_priv->is_paged_buf && !fw_priv->data)
117		retval = -ENOMEM;
118
119out:
120	device_del(f_dev);
121err_put_dev:
122	put_device(f_dev);
123	return retval;
124}
125
126static int fw_load_from_user_helper(struct firmware *firmware,
127				    const char *name, struct device *device,
128				    u32 opt_flags)
129{
130	struct fw_sysfs *fw_sysfs;
131	long timeout;
132	int ret;
133
134	timeout = firmware_loading_timeout();
135	if (opt_flags & FW_OPT_NOWAIT) {
136		timeout = usermodehelper_read_lock_wait(timeout);
137		if (!timeout) {
138			dev_dbg(device, "firmware: %s loading timed out\n",
139				name);
140			return -EBUSY;
141		}
142	} else {
143		ret = usermodehelper_read_trylock();
144		if (WARN_ON(ret)) {
145			dev_err(device, "firmware: %s will not be loaded\n",
146				name);
147			return ret;
148		}
149	}
150
151	fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
152	if (IS_ERR(fw_sysfs)) {
153		ret = PTR_ERR(fw_sysfs);
154		goto out_unlock;
155	}
156
157	fw_sysfs->fw_priv = firmware->priv;
158	ret = fw_load_sysfs_fallback(fw_sysfs, timeout);
159
160	if (!ret)
161		ret = assign_fw(firmware, device);
162
163out_unlock:
164	usermodehelper_read_unlock();
165
166	return ret;
167}
168
169static bool fw_force_sysfs_fallback(u32 opt_flags)
170{
171	if (fw_fallback_config.force_sysfs_fallback)
172		return true;
173	if (!(opt_flags & FW_OPT_USERHELPER))
174		return false;
175	return true;
176}
177
178static bool fw_run_sysfs_fallback(u32 opt_flags)
179{
180	int ret;
181
182	if (fw_fallback_config.ignore_sysfs_fallback) {
183		pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
184		return false;
185	}
186
187	if ((opt_flags & FW_OPT_NOFALLBACK_SYSFS))
188		return false;
189
190	/* Also permit LSMs and IMA to fail firmware sysfs fallback */
191	ret = security_kernel_load_data(LOADING_FIRMWARE, true);
192	if (ret < 0)
193		return false;
194
195	return fw_force_sysfs_fallback(opt_flags);
196}
197
198/**
199 * firmware_fallback_sysfs() - use the fallback mechanism to find firmware
200 * @fw: pointer to firmware image
201 * @name: name of firmware file to look for
202 * @device: device for which firmware is being loaded
203 * @opt_flags: options to control firmware loading behaviour, as defined by
204 *	       &enum fw_opt
205 * @ret: return value from direct lookup which triggered the fallback mechanism
206 *
207 * This function is called if direct lookup for the firmware failed, it enables
208 * a fallback mechanism through userspace by exposing a sysfs loading
209 * interface. Userspace is in charge of loading the firmware through the sysfs
210 * loading interface. This sysfs fallback mechanism may be disabled completely
211 * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
212 * If this is false we check if the internal API caller set the
213 * @FW_OPT_NOFALLBACK_SYSFS flag, if so it would also disable the fallback
214 * mechanism. A system may want to enforce the sysfs fallback mechanism at all
215 * times, it can do this by setting ignore_sysfs_fallback to false and
216 * force_sysfs_fallback to true.
217 * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
218 * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
219 **/
220int firmware_fallback_sysfs(struct firmware *fw, const char *name,
221			    struct device *device,
222			    u32 opt_flags,
223			    int ret)
224{
225	if (!fw_run_sysfs_fallback(opt_flags))
226		return ret;
227
228	if (!(opt_flags & FW_OPT_NO_WARN))
229		dev_warn(device, "Falling back to sysfs fallback for: %s\n",
230				 name);
231	else
232		dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
233				name);
234	return fw_load_from_user_helper(fw, name, device, opt_flags);
235}
  1// SPDX-License-Identifier: GPL-2.0
  2
  3#include <linux/types.h>
  4#include <linux/kconfig.h>
  5#include <linux/list.h>
  6#include <linux/security.h>
  7#include <linux/umh.h>
  8#include <linux/sysctl.h>
  9#include <linux/module.h>
 10
 11#include "fallback.h"
 12#include "firmware.h"
 13
 14/*
 15 * firmware fallback mechanism
 16 */
 17
 18/*
 19 * use small loading timeout for caching devices' firmware because all these
 20 * firmware images have been loaded successfully at lease once, also system is
 21 * ready for completing firmware loading now. The maximum size of firmware in
 22 * current distributions is about 2M bytes, so 10 secs should be enough.
 23 */
 24void fw_fallback_set_cache_timeout(void)
 25{
 26	fw_fallback_config.old_timeout = __firmware_loading_timeout();
 27	__fw_fallback_set_timeout(10);
 28}
 29
 30/* Restores the timeout to the value last configured during normal operation */
 31void fw_fallback_set_default_timeout(void)
 32{
 33	__fw_fallback_set_timeout(fw_fallback_config.old_timeout);
 34}
 35
 36static long firmware_loading_timeout(void)
 37{
 38	return __firmware_loading_timeout() > 0 ?
 39		__firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET;
 40}
 41
 42static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv,  long timeout)
 43{
 44	return __fw_state_wait_common(fw_priv, timeout);
 45}
 46
 47static LIST_HEAD(pending_fw_head);
 48
 49void kill_pending_fw_fallback_reqs(bool kill_all)
 50{
 51	struct fw_priv *fw_priv;
 52	struct fw_priv *next;
 53
 54	mutex_lock(&fw_lock);
 55	list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
 56				 pending_list) {
 57		if (kill_all || !fw_priv->need_uevent)
 58			 __fw_load_abort(fw_priv);
 59	}
 60
 61	if (kill_all)
 62		fw_load_abort_all = true;
 63
 64	mutex_unlock(&fw_lock);
 65}
 66
 67/**
 68 * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
 69 * @fw_sysfs: firmware sysfs information for the firmware to load
 70 * @timeout: timeout to wait for the load
 71 *
 72 * In charge of constructing a sysfs fallback interface for firmware loading.
 73 **/
 74static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
 75{
 76	int retval = 0;
 77	struct device *f_dev = &fw_sysfs->dev;
 78	struct fw_priv *fw_priv = fw_sysfs->fw_priv;
 79
 80	/* fall back on userspace loading */
 81	if (!fw_priv->data)
 82		fw_priv->is_paged_buf = true;
 83
 84	dev_set_uevent_suppress(f_dev, true);
 85
 86	retval = device_add(f_dev);
 87	if (retval) {
 88		dev_err(f_dev, "%s: device_register failed\n", __func__);
 89		goto err_put_dev;
 90	}
 91
 92	mutex_lock(&fw_lock);
 93	if (fw_load_abort_all || fw_state_is_aborted(fw_priv)) {
 94		mutex_unlock(&fw_lock);
 95		retval = -EINTR;
 96		goto out;
 97	}
 98	list_add(&fw_priv->pending_list, &pending_fw_head);
 99	mutex_unlock(&fw_lock);
100
101	if (fw_priv->opt_flags & FW_OPT_UEVENT) {
102		fw_priv->need_uevent = true;
103		dev_set_uevent_suppress(f_dev, false);
104		dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
105		kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
106	} else {
107		timeout = MAX_JIFFY_OFFSET;
108	}
109
110	retval = fw_sysfs_wait_timeout(fw_priv, timeout);
111	if (retval < 0 && retval != -ENOENT) {
112		mutex_lock(&fw_lock);
113		fw_load_abort(fw_sysfs);
114		mutex_unlock(&fw_lock);
115	}
116
117	if (fw_state_is_aborted(fw_priv)) {
118		if (retval == -ERESTARTSYS)
119			retval = -EINTR;
120	} else if (fw_priv->is_paged_buf && !fw_priv->data)
121		retval = -ENOMEM;
122
123out:
124	device_del(f_dev);
125err_put_dev:
126	put_device(f_dev);
127	return retval;
128}
129
130static int fw_load_from_user_helper(struct firmware *firmware,
131				    const char *name, struct device *device,
132				    u32 opt_flags)
133{
134	struct fw_sysfs *fw_sysfs;
135	long timeout;
136	int ret;
137
138	timeout = firmware_loading_timeout();
139	if (opt_flags & FW_OPT_NOWAIT) {
140		timeout = usermodehelper_read_lock_wait(timeout);
141		if (!timeout) {
142			dev_dbg(device, "firmware: %s loading timed out\n",
143				name);
144			return -EBUSY;
145		}
146	} else {
147		ret = usermodehelper_read_trylock();
148		if (WARN_ON(ret)) {
149			dev_err(device, "firmware: %s will not be loaded\n",
150				name);
151			return ret;
152		}
153	}
154
155	fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
156	if (IS_ERR(fw_sysfs)) {
157		ret = PTR_ERR(fw_sysfs);
158		goto out_unlock;
159	}
160
161	fw_sysfs->fw_priv = firmware->priv;
162	ret = fw_load_sysfs_fallback(fw_sysfs, timeout);
163
164	if (!ret)
165		ret = assign_fw(firmware, device);
166
167out_unlock:
168	usermodehelper_read_unlock();
169
170	return ret;
171}
172
173static bool fw_force_sysfs_fallback(u32 opt_flags)
174{
175	if (fw_fallback_config.force_sysfs_fallback)
176		return true;
177	if (!(opt_flags & FW_OPT_USERHELPER))
178		return false;
179	return true;
180}
181
182static bool fw_run_sysfs_fallback(u32 opt_flags)
183{
184	int ret;
185
186	if (fw_fallback_config.ignore_sysfs_fallback) {
187		pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
188		return false;
189	}
190
191	if ((opt_flags & FW_OPT_NOFALLBACK_SYSFS))
192		return false;
193
194	/* Also permit LSMs and IMA to fail firmware sysfs fallback */
195	ret = security_kernel_load_data(LOADING_FIRMWARE, true);
196	if (ret < 0)
197		return false;
198
199	return fw_force_sysfs_fallback(opt_flags);
200}
201
202/**
203 * firmware_fallback_sysfs() - use the fallback mechanism to find firmware
204 * @fw: pointer to firmware image
205 * @name: name of firmware file to look for
206 * @device: device for which firmware is being loaded
207 * @opt_flags: options to control firmware loading behaviour, as defined by
208 *	       &enum fw_opt
209 * @ret: return value from direct lookup which triggered the fallback mechanism
210 *
211 * This function is called if direct lookup for the firmware failed, it enables
212 * a fallback mechanism through userspace by exposing a sysfs loading
213 * interface. Userspace is in charge of loading the firmware through the sysfs
214 * loading interface. This sysfs fallback mechanism may be disabled completely
215 * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
216 * If this is false we check if the internal API caller set the
217 * @FW_OPT_NOFALLBACK_SYSFS flag, if so it would also disable the fallback
218 * mechanism. A system may want to enforce the sysfs fallback mechanism at all
219 * times, it can do this by setting ignore_sysfs_fallback to false and
220 * force_sysfs_fallback to true.
221 * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
222 * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
223 **/
224int firmware_fallback_sysfs(struct firmware *fw, const char *name,
225			    struct device *device,
226			    u32 opt_flags,
227			    int ret)
228{
229	if (!fw_run_sysfs_fallback(opt_flags))
230		return ret;
231
232	if (!(opt_flags & FW_OPT_NO_WARN))
233		dev_warn(device, "Falling back to sysfs fallback for: %s\n",
234				 name);
235	else
236		dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
237				name);
238	return fw_load_from_user_helper(fw, name, device, opt_flags);
239}