| Jiakai Zhang | 7d29222 | 2024-01-18 17:27:42 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python | 
|  | 2 | # | 
|  | 3 | # Copyright (C) 2024 The Android Open Source Project | 
|  | 4 | # | 
|  | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | # you may not use this file except in compliance with the License. | 
|  | 7 | # You may obtain a copy of the License at | 
|  | 8 | # | 
|  | 9 | #      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | # | 
|  | 11 | # Unless required by applicable law or agreed to in writing, software | 
|  | 12 | # distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | # See the License for the specific language governing permissions and | 
|  | 15 | # limitations under the License. | 
|  | 16 | # | 
|  | 17 | """Utils to determine whether to enable UFFD GC.""" | 
|  | 18 |  | 
|  | 19 | import re | 
|  | 20 | import sys | 
|  | 21 |  | 
|  | 22 |  | 
|  | 23 | def should_enable_uffd_gc(kernel_version_file): | 
|  | 24 | with open(kernel_version_file, 'r') as f: | 
|  | 25 | kernel_version = f.read().strip() | 
|  | 26 | return should_enable_uffd_gc_impl(kernel_version) | 
|  | 27 |  | 
|  | 28 | def should_enable_uffd_gc_impl(kernel_version): | 
|  | 29 | # See https://source.android.com/docs/core/architecture/kernel/gki-versioning#determine-release | 
|  | 30 | p = r"^(?P<w>\d+)[.](?P<x>\d+)[.](?P<y>\d+)(-android(?P<z>\d+)-(?P<k>\d+).*$)?" | 
|  | 31 | m = re.match(p, kernel_version) | 
|  | 32 | if m is not None: | 
|  | 33 | if m.group('z') is not None: | 
|  | 34 | android_release = int(m.group('z')) | 
|  | 35 | # No need to check w, x, y because all Android 12 kernels have backports. | 
|  | 36 | return android_release >= 12 | 
|  | 37 | else: | 
|  | 38 | # Old kernel or non-GKI kernel. | 
|  | 39 | version = int(m.group('w')) | 
|  | 40 | patch_level = int(m.group('x')) | 
|  | 41 | if version < 5: | 
|  | 42 | # Old kernel. | 
|  | 43 | return False | 
|  | 44 | elif (version == 5 and patch_level >= 7) or version >= 6: | 
|  | 45 | # New non-GKI kernel. 5.7 supports MREMAP_DONTUNMAP without the need for | 
|  | 46 | # backports. | 
|  | 47 | return True | 
|  | 48 | else: | 
|  | 49 | # Non-GKI kernel between 5 and 5.6. It may have backports. | 
|  | 50 | raise exit_with_error(kernel_version) | 
|  | 51 | elif kernel_version == '<unknown-kernel>': | 
|  | 52 | # The kernel information isn't available to the build system, probably | 
|  | 53 | # because PRODUCT_OTA_ENFORCE_VINTF_KERNEL_REQUIREMENTS is set to false. We | 
|  | 54 | # assume that the kernel supports UFFD GC because it is the case for most of | 
|  | 55 | # the products today and it is the future. | 
|  | 56 | return True | 
|  | 57 | else: | 
|  | 58 | # Unrecognizable non-GKI kernel. | 
|  | 59 | raise exit_with_error(kernel_version) | 
|  | 60 |  | 
|  | 61 | def exit_with_error(kernel_version): | 
|  | 62 | sys.exit(f""" | 
|  | 63 | Unable to determine UFFD GC flag for kernel version "{kernel_version}". | 
|  | 64 | You can fix this by explicitly setting PRODUCT_ENABLE_UFFD_GC to "true" or | 
|  | 65 | "false" based on the kernel version. | 
|  | 66 | 1. Set PRODUCT_ENABLE_UFFD_GC to "true" if the kernel supports userfaultfd(2) | 
|  | 67 | and MREMAP_DONTUNMAP. | 
|  | 68 | 2. Set PRODUCT_ENABLE_UFFD_GC to "false" otherwise.""") |