hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/base/firmware_loader/fallback.c
....@@ -9,6 +9,7 @@
99 #include <linux/umh.h>
1010 #include <linux/sysctl.h>
1111 #include <linux/vmalloc.h>
12
+#include <linux/module.h>
1213
1314 #include "fallback.h"
1415 #include "firmware.h"
....@@ -16,6 +17,8 @@
1617 /*
1718 * firmware fallback mechanism
1819 */
20
+
21
+MODULE_IMPORT_NS(FIRMWARE_LOADER_PRIVATE);
1922
2023 extern struct firmware_fallback_config fw_fallback_config;
2124
....@@ -120,7 +123,7 @@
120123 static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
121124 char *buf)
122125 {
123
- return sprintf(buf, "%d\n", __firmware_loading_timeout());
126
+ return sysfs_emit(buf, "%d\n", __firmware_loading_timeout());
124127 }
125128
126129 /**
....@@ -215,21 +218,7 @@
215218 loading = fw_sysfs_loading(fw_sysfs->fw_priv);
216219 mutex_unlock(&fw_lock);
217220
218
- return sprintf(buf, "%d\n", loading);
219
-}
220
-
221
-/* one pages buffer should be mapped/unmapped only once */
222
-static int map_fw_priv_pages(struct fw_priv *fw_priv)
223
-{
224
- if (!fw_priv->is_paged_buf)
225
- return 0;
226
-
227
- vunmap(fw_priv->data);
228
- fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
229
- PAGE_KERNEL_RO);
230
- if (!fw_priv->data)
231
- return -ENOMEM;
232
- return 0;
221
+ return sysfs_emit(buf, "%d\n", loading);
233222 }
234223
235224 /**
....@@ -253,7 +242,6 @@
253242 struct fw_priv *fw_priv;
254243 ssize_t written = count;
255244 int loading = simple_strtol(buf, NULL, 10);
256
- int i;
257245
258246 mutex_lock(&fw_lock);
259247 fw_priv = fw_sysfs->fw_priv;
....@@ -264,12 +252,7 @@
264252 case 1:
265253 /* discarding any previous partial load */
266254 if (!fw_sysfs_done(fw_priv)) {
267
- for (i = 0; i < fw_priv->nr_pages; i++)
268
- __free_page(fw_priv->pages[i]);
269
- vfree(fw_priv->pages);
270
- fw_priv->pages = NULL;
271
- fw_priv->page_array_size = 0;
272
- fw_priv->nr_pages = 0;
255
+ fw_free_paged_buf(fw_priv);
273256 fw_state_start(fw_priv);
274257 }
275258 break;
....@@ -283,14 +266,14 @@
283266 * see the mapped 'buf->data' once the loading
284267 * is completed.
285268 * */
286
- rc = map_fw_priv_pages(fw_priv);
269
+ rc = fw_map_paged_buf(fw_priv);
287270 if (rc)
288271 dev_err(dev, "%s: map pages failed\n",
289272 __func__);
290273 else
291
- rc = security_kernel_post_read_file(NULL,
292
- fw_priv->data, fw_priv->size,
293
- READING_FIRMWARE);
274
+ rc = security_kernel_post_load_data(fw_priv->data,
275
+ fw_priv->size,
276
+ LOADING_FIRMWARE, "blob");
294277
295278 /*
296279 * Same logic as fw_load_abort, only the DONE bit
....@@ -304,10 +287,10 @@
304287 }
305288 break;
306289 }
307
- /* fallthrough */
290
+ fallthrough;
308291 default:
309292 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
310
- /* fallthrough */
293
+ fallthrough;
311294 case -1:
312295 fw_load_abort(fw_sysfs);
313296 break;
....@@ -387,40 +370,13 @@
387370
388371 static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
389372 {
390
- struct fw_priv *fw_priv= fw_sysfs->fw_priv;
391
- int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
373
+ int err;
392374
393
- /* If the array of pages is too small, grow it... */
394
- if (fw_priv->page_array_size < pages_needed) {
395
- int new_array_size = max(pages_needed,
396
- fw_priv->page_array_size * 2);
397
- struct page **new_pages;
398
-
399
- new_pages = vmalloc(array_size(new_array_size, sizeof(void *)));
400
- if (!new_pages) {
401
- fw_load_abort(fw_sysfs);
402
- return -ENOMEM;
403
- }
404
- memcpy(new_pages, fw_priv->pages,
405
- fw_priv->page_array_size * sizeof(void *));
406
- memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
407
- (new_array_size - fw_priv->page_array_size));
408
- vfree(fw_priv->pages);
409
- fw_priv->pages = new_pages;
410
- fw_priv->page_array_size = new_array_size;
411
- }
412
-
413
- while (fw_priv->nr_pages < pages_needed) {
414
- fw_priv->pages[fw_priv->nr_pages] =
415
- alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
416
-
417
- if (!fw_priv->pages[fw_priv->nr_pages]) {
418
- fw_load_abort(fw_sysfs);
419
- return -ENOMEM;
420
- }
421
- fw_priv->nr_pages++;
422
- }
423
- return 0;
375
+ err = fw_grow_paged_buf(fw_sysfs->fw_priv,
376
+ PAGE_ALIGN(min_size) >> PAGE_SHIFT);
377
+ if (err)
378
+ fw_load_abort(fw_sysfs);
379
+ return err;
424380 }
425381
426382 /**
....@@ -505,7 +461,7 @@
505461
506462 static struct fw_sysfs *
507463 fw_create_instance(struct firmware *firmware, const char *fw_name,
508
- struct device *device, enum fw_opt opt_flags)
464
+ struct device *device, u32 opt_flags)
509465 {
510466 struct fw_sysfs *fw_sysfs;
511467 struct device *f_dev;
....@@ -532,13 +488,11 @@
532488 /**
533489 * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
534490 * @fw_sysfs: firmware sysfs information for the firmware to load
535
- * @opt_flags: flags of options, FW_OPT_*
536491 * @timeout: timeout to wait for the load
537492 *
538493 * In charge of constructing a sysfs fallback interface for firmware loading.
539494 **/
540
-static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs,
541
- enum fw_opt opt_flags, long timeout)
495
+static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
542496 {
543497 int retval = 0;
544498 struct device *f_dev = &fw_sysfs->dev;
....@@ -565,7 +519,7 @@
565519 list_add(&fw_priv->pending_list, &pending_fw_head);
566520 mutex_unlock(&fw_lock);
567521
568
- if (opt_flags & FW_OPT_UEVENT) {
522
+ if (fw_priv->opt_flags & FW_OPT_UEVENT) {
569523 fw_priv->need_uevent = true;
570524 dev_set_uevent_suppress(f_dev, false);
571525 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
....@@ -596,7 +550,7 @@
596550
597551 static int fw_load_from_user_helper(struct firmware *firmware,
598552 const char *name, struct device *device,
599
- enum fw_opt opt_flags)
553
+ u32 opt_flags)
600554 {
601555 struct fw_sysfs *fw_sysfs;
602556 long timeout;
....@@ -626,10 +580,10 @@
626580 }
627581
628582 fw_sysfs->fw_priv = firmware->priv;
629
- ret = fw_load_sysfs_fallback(fw_sysfs, opt_flags, timeout);
583
+ ret = fw_load_sysfs_fallback(fw_sysfs, timeout);
630584
631585 if (!ret)
632
- ret = assign_fw(firmware, device, opt_flags);
586
+ ret = assign_fw(firmware, device);
633587
634588 out_unlock:
635589 usermodehelper_read_unlock();
....@@ -637,7 +591,7 @@
637591 return ret;
638592 }
639593
640
-static bool fw_force_sysfs_fallback(enum fw_opt opt_flags)
594
+static bool fw_force_sysfs_fallback(u32 opt_flags)
641595 {
642596 if (fw_fallback_config.force_sysfs_fallback)
643597 return true;
....@@ -646,7 +600,7 @@
646600 return true;
647601 }
648602
649
-static bool fw_run_sysfs_fallback(enum fw_opt opt_flags)
603
+static bool fw_run_sysfs_fallback(u32 opt_flags)
650604 {
651605 int ret;
652606
....@@ -655,11 +609,11 @@
655609 return false;
656610 }
657611
658
- if ((opt_flags & FW_OPT_NOFALLBACK))
612
+ if ((opt_flags & FW_OPT_NOFALLBACK_SYSFS))
659613 return false;
660614
661615 /* Also permit LSMs and IMA to fail firmware sysfs fallback */
662
- ret = security_kernel_load_data(LOADING_FIRMWARE);
616
+ ret = security_kernel_load_data(LOADING_FIRMWARE, true);
663617 if (ret < 0)
664618 return false;
665619
....@@ -671,31 +625,33 @@
671625 * @fw: pointer to firmware image
672626 * @name: name of firmware file to look for
673627 * @device: device for which firmware is being loaded
674
- * @opt_flags: options to control firmware loading behaviour
628
+ * @opt_flags: options to control firmware loading behaviour, as defined by
629
+ * &enum fw_opt
675630 * @ret: return value from direct lookup which triggered the fallback mechanism
676631 *
677632 * This function is called if direct lookup for the firmware failed, it enables
678633 * a fallback mechanism through userspace by exposing a sysfs loading
679
- * interface. Userspace is in charge of loading the firmware through the syfs
680
- * loading interface. This syfs fallback mechanism may be disabled completely
634
+ * interface. Userspace is in charge of loading the firmware through the sysfs
635
+ * loading interface. This sysfs fallback mechanism may be disabled completely
681636 * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
682
- * If this false we check if the internal API caller set the @FW_OPT_NOFALLBACK
683
- * flag, if so it would also disable the fallback mechanism. A system may want
684
- * to enfoce the sysfs fallback mechanism at all times, it can do this by
685
- * setting ignore_sysfs_fallback to false and force_sysfs_fallback to true.
637
+ * If this is false we check if the internal API caller set the
638
+ * @FW_OPT_NOFALLBACK_SYSFS flag, if so it would also disable the fallback
639
+ * mechanism. A system may want to enforce the sysfs fallback mechanism at all
640
+ * times, it can do this by setting ignore_sysfs_fallback to false and
641
+ * force_sysfs_fallback to true.
686642 * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
687643 * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
688644 **/
689645 int firmware_fallback_sysfs(struct firmware *fw, const char *name,
690646 struct device *device,
691
- enum fw_opt opt_flags,
647
+ u32 opt_flags,
692648 int ret)
693649 {
694650 if (!fw_run_sysfs_fallback(opt_flags))
695651 return ret;
696652
697653 if (!(opt_flags & FW_OPT_NO_WARN))
698
- dev_warn(device, "Falling back to syfs fallback for: %s\n",
654
+ dev_warn(device, "Falling back to sysfs fallback for: %s\n",
699655 name);
700656 else
701657 dev_dbg(device, "Falling back to sysfs fallback for: %s\n",