From 0be74214c03b5df8a4e85d7b59e829a27cdfcbe5 Mon Sep 17 00:00:00 2001 From: "Isaac J. Manjarres" Date: Wed, 2 Jul 2025 22:46:26 +0000 Subject: [PATCH] ANDROID: ashmem_rust: Add support for querying the size of an ashmem region Given a pointer to a file structure that represents an ashmem region, return the size of the region. This should only be used for dumping information about ashmem regions in debug scenarios. Bug: 193397560 Change-Id: I24e6a2f7a469d667dc37ec8714e591b911043ee4 Signed-off-by: Isaac J. Manjarres --- drivers/staging/android/ashmem.h | 1 + drivers/staging/android/ashmem_rust.rs | 17 +++++++++++++++++ drivers/staging/android/ashmem_rust_exports.c | 1 + 3 files changed, 19 insertions(+) diff --git a/drivers/staging/android/ashmem.h b/drivers/staging/android/ashmem.h index ad64cf791f2e..06f7f91ac22b 100644 --- a/drivers/staging/android/ashmem.h +++ b/drivers/staging/android/ashmem.h @@ -33,5 +33,6 @@ enum { bool is_ashmem_file(struct file *file); int ashmem_area_name(struct file *file, char *name); +long ashmem_area_size(struct file *file); #endif /* _LINUX_ASHMEM_H */ diff --git a/drivers/staging/android/ashmem_rust.rs b/drivers/staging/android/ashmem_rust.rs index b147e524f88f..30c886c63246 100644 --- a/drivers/staging/android/ashmem_rust.rs +++ b/drivers/staging/android/ashmem_rust.rs @@ -707,3 +707,20 @@ unsafe extern "C" fn ashmem_area_name( Err(err) => err.to_errno() as c_int, } } + +/// # Safety +/// +/// The caller must ensure that `file` is valid for the duration of this function. +#[no_mangle] +unsafe extern "C" fn ashmem_area_size(file: *mut bindings::file) -> isize { + // SAFETY: file is valid for the duration of this function. + let ashmem = match unsafe { get_ashmem_area(file) } { + Ok(area) => area, + Err(_err) => return 0, + }; + + match ashmem.get_size() { + Ok(size) => size, + Err(_err) => 0, + } +} diff --git a/drivers/staging/android/ashmem_rust_exports.c b/drivers/staging/android/ashmem_rust_exports.c index 95546739bd6f..3957a3b208c0 100644 --- a/drivers/staging/android/ashmem_rust_exports.c +++ b/drivers/staging/android/ashmem_rust_exports.c @@ -18,3 +18,4 @@ */ EXPORT_SYMBOL_GPL(is_ashmem_file); EXPORT_SYMBOL_GPL(ashmem_area_name); +EXPORT_SYMBOL_GPL(ashmem_area_size);