fallback.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 kill_all)
  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 (kill_all || !fw_priv->need_uevent)
  48. __fw_load_abort(fw_priv);
  49. }
  50. if (kill_all)
  51. fw_load_abort_all = true;
  52. mutex_unlock(&fw_lock);
  53. }
  54. /**
  55. * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
  56. * @fw_sysfs: firmware sysfs information for the firmware to load
  57. * @timeout: timeout to wait for the load
  58. *
  59. * In charge of constructing a sysfs fallback interface for firmware loading.
  60. **/
  61. static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
  62. {
  63. int retval = 0;
  64. struct device *f_dev = &fw_sysfs->dev;
  65. struct fw_priv *fw_priv = fw_sysfs->fw_priv;
  66. /* fall back on userspace loading */
  67. if (!fw_priv->data)
  68. fw_priv->is_paged_buf = true;
  69. dev_set_uevent_suppress(f_dev, true);
  70. retval = device_add(f_dev);
  71. if (retval) {
  72. dev_err(f_dev, "%s: device_register failed\n", __func__);
  73. goto err_put_dev;
  74. }
  75. mutex_lock(&fw_lock);
  76. if (fw_load_abort_all || fw_state_is_aborted(fw_priv)) {
  77. mutex_unlock(&fw_lock);
  78. retval = -EINTR;
  79. goto out;
  80. }
  81. list_add(&fw_priv->pending_list, &pending_fw_head);
  82. mutex_unlock(&fw_lock);
  83. if (fw_priv->opt_flags & FW_OPT_UEVENT) {
  84. fw_priv->need_uevent = true;
  85. dev_set_uevent_suppress(f_dev, false);
  86. dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
  87. kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
  88. } else {
  89. timeout = MAX_JIFFY_OFFSET;
  90. }
  91. retval = fw_sysfs_wait_timeout(fw_priv, timeout);
  92. if (retval < 0 && retval != -ENOENT) {
  93. mutex_lock(&fw_lock);
  94. fw_load_abort(fw_sysfs);
  95. mutex_unlock(&fw_lock);
  96. }
  97. if (fw_state_is_aborted(fw_priv)) {
  98. if (retval == -ERESTARTSYS)
  99. retval = -EINTR;
  100. } else if (fw_priv->is_paged_buf && !fw_priv->data)
  101. retval = -ENOMEM;
  102. out:
  103. device_del(f_dev);
  104. err_put_dev:
  105. put_device(f_dev);
  106. return retval;
  107. }
  108. static int fw_load_from_user_helper(struct firmware *firmware,
  109. const char *name, struct device *device,
  110. u32 opt_flags)
  111. {
  112. struct fw_sysfs *fw_sysfs;
  113. long timeout;
  114. int ret;
  115. timeout = firmware_loading_timeout();
  116. if (opt_flags & FW_OPT_NOWAIT) {
  117. timeout = usermodehelper_read_lock_wait(timeout);
  118. if (!timeout) {
  119. dev_dbg(device, "firmware: %s loading timed out\n",
  120. name);
  121. return -EBUSY;
  122. }
  123. } else {
  124. ret = usermodehelper_read_trylock();
  125. if (WARN_ON(ret)) {
  126. dev_err(device, "firmware: %s will not be loaded\n",
  127. name);
  128. return ret;
  129. }
  130. }
  131. fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
  132. if (IS_ERR(fw_sysfs)) {
  133. ret = PTR_ERR(fw_sysfs);
  134. goto out_unlock;
  135. }
  136. fw_sysfs->fw_priv = firmware->priv;
  137. ret = fw_load_sysfs_fallback(fw_sysfs, timeout);
  138. if (!ret)
  139. ret = assign_fw(firmware, device);
  140. out_unlock:
  141. usermodehelper_read_unlock();
  142. return ret;
  143. }
  144. static bool fw_force_sysfs_fallback(u32 opt_flags)
  145. {
  146. if (fw_fallback_config.force_sysfs_fallback)
  147. return true;
  148. if (!(opt_flags & FW_OPT_USERHELPER))
  149. return false;
  150. return true;
  151. }
  152. static bool fw_run_sysfs_fallback(u32 opt_flags)
  153. {
  154. int ret;
  155. if (fw_fallback_config.ignore_sysfs_fallback) {
  156. pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
  157. return false;
  158. }
  159. if ((opt_flags & FW_OPT_NOFALLBACK_SYSFS))
  160. return false;
  161. /* Also permit LSMs and IMA to fail firmware sysfs fallback */
  162. ret = security_kernel_load_data(LOADING_FIRMWARE, true);
  163. if (ret < 0)
  164. return false;
  165. return fw_force_sysfs_fallback(opt_flags);
  166. }
  167. /**
  168. * firmware_fallback_sysfs() - use the fallback mechanism to find firmware
  169. * @fw: pointer to firmware image
  170. * @name: name of firmware file to look for
  171. * @device: device for which firmware is being loaded
  172. * @opt_flags: options to control firmware loading behaviour, as defined by
  173. * &enum fw_opt
  174. * @ret: return value from direct lookup which triggered the fallback mechanism
  175. *
  176. * This function is called if direct lookup for the firmware failed, it enables
  177. * a fallback mechanism through userspace by exposing a sysfs loading
  178. * interface. Userspace is in charge of loading the firmware through the sysfs
  179. * loading interface. This sysfs fallback mechanism may be disabled completely
  180. * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
  181. * If this is false we check if the internal API caller set the
  182. * @FW_OPT_NOFALLBACK_SYSFS flag, if so it would also disable the fallback
  183. * mechanism. A system may want to enforce the sysfs fallback mechanism at all
  184. * times, it can do this by setting ignore_sysfs_fallback to false and
  185. * force_sysfs_fallback to true.
  186. * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
  187. * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
  188. **/
  189. int firmware_fallback_sysfs(struct firmware *fw, const char *name,
  190. struct device *device,
  191. u32 opt_flags,
  192. int ret)
  193. {
  194. if (!fw_run_sysfs_fallback(opt_flags))
  195. return ret;
  196. if (!(opt_flags & FW_OPT_NO_WARN))
  197. dev_warn(device, "Falling back to sysfs fallback for: %s\n",
  198. name);
  199. else
  200. dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
  201. name);
  202. return fw_load_from_user_helper(fw, name, device, opt_flags);
  203. }