fallback.c 6.3 KB

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