Files
geramyloveless 79036bbc8f SysfsWrite: the performance level and power profile, through upstream's store()
On Linux, root writes power_dpm_force_performance_level and
pp_power_profile_mode, and amdgpu_pm.c's store() applies them. This
driver had no sysfs write path: its observer client only reads.

Selector 89 (SysfsWrite) takes an attribute id and a value and runs that
attribute's own store(), through linuxu_sysfs_write (as kernfs, one
store() of a NUL-terminated page). It is limited to:
- An allowlist in session_state.h: power_dpm_force_performance_level
  with auto, low, high or profile_peak, and pp_power_profile_mode with a
  bare profile index, 0-99. Manual, the other profile_* levels and custom
  profile parameters are not offered.
- Observer clients that carry the session-release entitlement, which is
  the host app's, as for Retire and the quarantine release.
- Synchronous and bounded on the observer reads' driver thread, never the
  delivery thread, with admission as for SysfsRead.
store()'s result comes back as the bytes taken or a negative Linux errno.
A value outside the allowlist is NotPermitted, a missing entitlement is
NotPrivileged, and a store() still running at 250 ms is Timeout.

Tests: test-sysfs-read writes through a device attribute's store(), and
checks the NUL-terminated buffer, store()'s own errno, and the read-only,
missing and empty cases. test-session-shutdown checks the observer
policy, that the call is synchronous but not on the delivery thread, the
paths, and the value allowlist (every offered level and index, with and
without a newline, and the levels, spellings and parameters it refuses).
2026-10-06 09:27:35 -07:00

57 lines
2.9 KiB
C

#ifndef LINUXU_RT_SYSFS_H
#define LINUXU_RT_SYSFS_H
#include <stddef.h>
/* Shared with DriverKit and C++ callers: no kernel types. */
#ifdef __cplusplus
extern "C" {
#endif
struct kobject;
/* Own an opaque in-memory directory for a registered kobject. These functions
* perform no filesystem publication and never invoke attribute callbacks. */
int linuxu_sysfs_add(struct kobject *kobj);
void linuxu_sysfs_remove(struct kobject *kobj);
/* Cached metadata only, for offline checks and in-process diagnostics. */
int linuxu_sysfs_has_file(struct kobject *kobj, const char *group, const char *name);
size_t linuxu_sysfs_count(struct kobject *kobj);
/* Paths are relative to @root's directory, or to the top-level kobjects
* when @root is NULL: '/'-separated names with no empty, "." or ".."
* component, at most LINUXU_SYSFS_PATH_MAX bytes. A name resolves to an
* entry of the current directory (attribute file, named group directory,
* link, which is followed) or else to a child kobject's directory. */
#define LINUXU_SYSFS_PATH_MAX 1024
/* read(2) at @pos of a freshly opened sysfs file, as fs/sysfs/file.c does
* it: an attribute file runs ktype->sysfs_ops->show() once into a zeroed
* PAGE_SIZE buffer, a bin_attribute runs its read() for at most a page.
* Returns the bytes copied to @buf or a negative Linux errno: show()'s or
* read()'s own error, -ENOENT, -ENOTDIR, -EISDIR, -EACCES (no read
* permission or no show operation), -EINVAL or -ENAMETOOLONG for a bad
* path. @length, when given, receives the attribute's full length (show()'s
* count, or the bin_attribute size, 0 when unknown).
*
* The callback runs on the caller's thread and may sleep and take upstream
* locks, like a Linux sysfs read. Removing the file or its kobject waits
* until the callback has returned. */
long linuxu_sysfs_read(struct kobject *root, const char *path, void *buf, size_t count,
long long pos, size_t *length);
/* readdir of a directory: one line per entry, "<type> <name>\n", sorted by
* name, with type 'f' (attribute or bin file), 'd' (group or kobject
* directory) or 'l' (link). Copies the bytes at @pos like
* linuxu_sysfs_read; @length receives the whole listing's size. No
* attribute callback runs. */
long linuxu_sysfs_list(struct kobject *root, const char *path, void *buf, size_t count,
long long pos, size_t *length);
/* A write of an attribute file, as a Linux sysfs write: its store() runs
* once with @count bytes (less than a page) in a zeroed, NUL-terminated
* page. Returns store()'s result (the bytes taken, or its negative errno),
* -EACCES for a file without write permission or store operation, or a
* binary attribute, -E2BIG for a page or more, and the path errors of
* linuxu_sysfs_read. Runs on the caller's thread; may sleep. */
long linuxu_sysfs_write(struct kobject *root, const char *path, const void *buf, size_t count);
#ifdef __cplusplus
}
#endif
#endif