From 6f497d1833dca4412a9f75846ad0ab318e92a565 Mon Sep 17 00:00:00 2001 From: Michael Butler Date: Fri, 11 Feb 2022 16:48:37 -0800 Subject: [PATCH] Clamp NNAPI HAL Version to runtime version -- hal The NNAPI is a Mainline Module, and its runtime version is determined by an Android Feature Flag to remotely rollout and rollback Feature Levels as needed. This change propagates the maximum feature level version allowed by the runtime to the HAL utility code, and clamps the utility code's version to the version allowed by the runtime. Bug: N/A Test: mma Test: CtsNNAPITestCases Test: NeuralNetworksTest_static Test: logged driver version, set current_feature_level, and verified the driver version was clamped by current_feature_level Change-Id: Ibaa895f8e35d36b2ddf9432b3ef9468e5886075f --- .../aidl/utils/include/nnapi/hal/aidl/Service.h | 3 ++- neuralnetworks/aidl/utils/src/Service.cpp | 10 ++++++---- .../utils/service/include/nnapi/hal/Service.h | 13 ++++++++++++- neuralnetworks/utils/service/src/Service.cpp | 11 +++++++---- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Service.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Service.h index cb6ff4b1cd..f229165b59 100644 --- a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Service.h +++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Service.h @@ -25,7 +25,8 @@ namespace aidl::android::hardware::neuralnetworks::utils { -::android::nn::GeneralResult<::android::nn::SharedDevice> getDevice(const std::string& name); +::android::nn::GeneralResult<::android::nn::SharedDevice> getDevice( + const std::string& name, ::android::nn::Version::Level maxFeatureLevelAllowed); } // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/src/Service.cpp b/neuralnetworks/aidl/utils/src/Service.cpp index e48593c38e..24fbb5309b 100644 --- a/neuralnetworks/aidl/utils/src/Service.cpp +++ b/neuralnetworks/aidl/utils/src/Service.cpp @@ -55,11 +55,12 @@ nn::GeneralResult getAidlServiceFeatureLevel(IDevice* service) { } // namespace -nn::GeneralResult getDevice(const std::string& instanceName) { +nn::GeneralResult getDevice( + const std::string& instanceName, ::android::nn::Version::Level maxFeatureLevelAllowed) { auto fullName = std::string(IDevice::descriptor) + "/" + instanceName; hal::utils::ResilientDevice::Factory makeDevice = - [instanceName, - name = std::move(fullName)](bool blocking) -> nn::GeneralResult { + [instanceName, name = std::move(fullName), + maxFeatureLevelAllowed](bool blocking) -> nn::GeneralResult { std::add_pointer_t getService; if (blocking) { if (__builtin_available(android __NNAPI_AIDL_MIN_ANDROID_API__, *)) { @@ -79,7 +80,8 @@ nn::GeneralResult getDevice(const std::string& instanceName) { << " returned nullptr"; } ABinderProcess_startThreadPool(); - const auto featureLevel = NN_TRY(getAidlServiceFeatureLevel(service.get())); + auto featureLevel = NN_TRY(getAidlServiceFeatureLevel(service.get())); + featureLevel.level = std::min(featureLevel.level, maxFeatureLevelAllowed); return Device::create(instanceName, std::move(service), featureLevel); }; diff --git a/neuralnetworks/utils/service/include/nnapi/hal/Service.h b/neuralnetworks/utils/service/include/nnapi/hal/Service.h index 2fd523728d..e8b9c10d77 100644 --- a/neuralnetworks/utils/service/include/nnapi/hal/Service.h +++ b/neuralnetworks/utils/service/include/nnapi/hal/Service.h @@ -29,7 +29,18 @@ struct SharedDeviceAndUpdatability { bool isDeviceUpdatable = false; }; -std::vector getDevices(bool includeUpdatableDrivers); +/** + * @brief Get the NNAPI sAIDL and HIDL services declared in the VINTF. + * + * @pre maxFeatureLevelAllowed >= Version::Level::FEATURE_LEVEL_5 + * + * @param includeUpdatableDrivers Allow updatable drivers to be used. + * @param maxFeatureLevelAllowed Maximum version of driver allowed to be used. Any driver version + * exceeding this must be clamped to `maxFeatureLevelAllowed`. + * @return A list of devices and whether each device is updatable or not. + */ +std::vector getDevices(bool includeUpdatableDrivers, + nn::Version::Level maxFeatureLevelAllowed); } // namespace android::hardware::neuralnetworks::service diff --git a/neuralnetworks/utils/service/src/Service.cpp b/neuralnetworks/utils/service/src/Service.cpp index 2286288c41..e0d5f8240c 100644 --- a/neuralnetworks/utils/service/src/Service.cpp +++ b/neuralnetworks/utils/service/src/Service.cpp @@ -74,7 +74,7 @@ void getHidlDevicesForVersion(const std::string& descriptor, getDeviceFn getDevi void getAidlDevices(std::vector* devices, std::unordered_set* registeredDevices, - bool includeUpdatableDrivers) { + bool includeUpdatableDrivers, nn::Version::Level maxFeatureLevelAllowed) { CHECK(devices != nullptr); CHECK(registeredDevices != nullptr); @@ -100,7 +100,7 @@ void getAidlDevices(std::vector* devices, continue; } if (const auto [it, unregistered] = registeredDevices->insert(name); unregistered) { - auto maybeDevice = aidl_hal::utils::getDevice(name); + auto maybeDevice = aidl_hal::utils::getDevice(name, maxFeatureLevelAllowed); if (maybeDevice.has_value()) { auto device = std::move(maybeDevice).value(); CHECK(device != nullptr); @@ -116,11 +116,14 @@ void getAidlDevices(std::vector* devices, } // namespace -std::vector getDevices(bool includeUpdatableDrivers) { +std::vector getDevices(bool includeUpdatableDrivers, + nn::Version::Level maxFeatureLevelAllowed) { std::vector devices; std::unordered_set registeredDevices; - getAidlDevices(&devices, ®isteredDevices, includeUpdatableDrivers); + CHECK_GE(maxFeatureLevelAllowed, nn::Version::Level::FEATURE_LEVEL_5); + + getAidlDevices(&devices, ®isteredDevices, includeUpdatableDrivers, maxFeatureLevelAllowed); getHidlDevicesForVersion(V1_3::IDevice::descriptor, &V1_3::utils::getDevice, &devices, ®isteredDevices);