Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/ioremap: Simplify setup_data mapping variants

memremap_is_setup_data() and early_memremap_is_setup_data() share
completely the same process and handling, except for the differing
memremap/unmap invocations.

Add a helper __memremap_is_setup_data() extracting the common part and
simplify a lot of code while at it.

Mark __memremap_is_setup_data() as __ref to suppress this section
mismatch warning:

WARNING: modpost: vmlinux: section mismatch in reference: __memremap_is_setup_data+0x5f (section: .text) ->
early_memunmap (section: .init.text)

[ bp: Massage a bit. ]

Signed-off-by: Baoquan He <bhe@redhat.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Link: https://lore.kernel.org/r/20241123114221.149383-2-bhe@redhat.com

authored by

Baoquan He and committed by
Borislav Petkov (AMD)
095ac6fa 5daececd

+37 -73
+37 -73
arch/x86/mm/ioremap.c
··· 632 632 * Examine the physical address to determine if it is boot data by checking 633 633 * it against the boot params setup_data chain. 634 634 */ 635 - static bool memremap_is_setup_data(resource_size_t phys_addr, 636 - unsigned long size) 635 + static bool __ref __memremap_is_setup_data(resource_size_t phys_addr, bool early) 637 636 { 638 - struct setup_indirect *indirect; 639 - struct setup_data *data; 640 - u64 paddr, paddr_next; 641 - 642 - paddr = boot_params.hdr.setup_data; 643 - while (paddr) { 644 - unsigned int len; 645 - 646 - if (phys_addr == paddr) 647 - return true; 648 - 649 - data = memremap(paddr, sizeof(*data), 650 - MEMREMAP_WB | MEMREMAP_DEC); 651 - if (!data) { 652 - pr_warn("failed to memremap setup_data entry\n"); 653 - return false; 654 - } 655 - 656 - paddr_next = data->next; 657 - len = data->len; 658 - 659 - if ((phys_addr > paddr) && 660 - (phys_addr < (paddr + sizeof(struct setup_data) + len))) { 661 - memunmap(data); 662 - return true; 663 - } 664 - 665 - if (data->type == SETUP_INDIRECT) { 666 - memunmap(data); 667 - data = memremap(paddr, sizeof(*data) + len, 668 - MEMREMAP_WB | MEMREMAP_DEC); 669 - if (!data) { 670 - pr_warn("failed to memremap indirect setup_data\n"); 671 - return false; 672 - } 673 - 674 - indirect = (struct setup_indirect *)data->data; 675 - 676 - if (indirect->type != SETUP_INDIRECT) { 677 - paddr = indirect->addr; 678 - len = indirect->len; 679 - } 680 - } 681 - 682 - memunmap(data); 683 - 684 - if ((phys_addr > paddr) && (phys_addr < (paddr + len))) 685 - return true; 686 - 687 - paddr = paddr_next; 688 - } 689 - 690 - return false; 691 - } 692 - 693 - /* 694 - * Examine the physical address to determine if it is boot data by checking 695 - * it against the boot params setup_data chain (early boot version). 696 - */ 697 - static bool __init early_memremap_is_setup_data(resource_size_t phys_addr, 698 - unsigned long size) 699 - { 637 + unsigned int setup_data_sz = sizeof(struct setup_data); 700 638 struct setup_indirect *indirect; 701 639 struct setup_data *data; 702 640 u64 paddr, paddr_next; ··· 646 708 if (phys_addr == paddr) 647 709 return true; 648 710 649 - data = early_memremap_decrypted(paddr, sizeof(*data)); 711 + if (early) 712 + data = early_memremap_decrypted(paddr, setup_data_sz); 713 + else 714 + data = memremap(paddr, setup_data_sz, MEMREMAP_WB | MEMREMAP_DEC); 650 715 if (!data) { 651 - pr_warn("failed to early memremap setup_data entry\n"); 716 + pr_warn("failed to remap setup_data entry\n"); 652 717 return false; 653 718 } 654 719 655 - size = sizeof(*data); 720 + size = setup_data_sz; 656 721 657 722 paddr_next = data->next; 658 723 len = data->len; 659 724 660 725 if ((phys_addr > paddr) && 661 - (phys_addr < (paddr + sizeof(struct setup_data) + len))) { 662 - early_memunmap(data, sizeof(*data)); 726 + (phys_addr < (paddr + setup_data_sz + len))) { 727 + if (early) 728 + early_memunmap(data, setup_data_sz); 729 + else 730 + memunmap(data); 663 731 return true; 664 732 } 665 733 666 734 if (data->type == SETUP_INDIRECT) { 667 735 size += len; 668 - early_memunmap(data, sizeof(*data)); 669 - data = early_memremap_decrypted(paddr, size); 736 + if (early) { 737 + early_memunmap(data, setup_data_sz); 738 + data = early_memremap_decrypted(paddr, size); 739 + } else { 740 + memunmap(data); 741 + data = memremap(paddr, size, MEMREMAP_WB | MEMREMAP_DEC); 742 + } 670 743 if (!data) { 671 - pr_warn("failed to early memremap indirect setup_data\n"); 744 + pr_warn("failed to remap indirect setup_data\n"); 672 745 return false; 673 746 } 674 747 ··· 691 742 } 692 743 } 693 744 694 - early_memunmap(data, size); 745 + if (early) 746 + early_memunmap(data, size); 747 + else 748 + memunmap(data); 695 749 696 750 if ((phys_addr > paddr) && (phys_addr < (paddr + len))) 697 751 return true; ··· 703 751 } 704 752 705 753 return false; 754 + } 755 + 756 + static bool memremap_is_setup_data(resource_size_t phys_addr, 757 + unsigned long size) 758 + { 759 + return __memremap_is_setup_data(phys_addr, false); 760 + } 761 + 762 + static bool __init early_memremap_is_setup_data(resource_size_t phys_addr, 763 + unsigned long size) 764 + { 765 + return __memremap_is_setup_data(phys_addr, true); 706 766 } 707 767 708 768 /*