[PATCH v2 1/8] power: supply: Pack power_supply_desc to eliminate holes
From: Waqar Hameed
Date: Tue Sep 01 2026 - 15:45:17 EST
`pahole` reports that there are two holes in `struct power_supply_desc`:
struct power_supply_desc {
const char * name; /* 0 4 */
enum power_supply_type type; /* 4 4 */
u8 charge_behaviours; /* 8 1 */
/* XXX 3 bytes hole, try to pack */
u32 charge_types; /* 12 4 */
u32 usb_types; /* 16 4 */
const enum power_supply_property * properties; /* 20 4 */
size_t num_properties; /* 24 4 */
int (*get_property)(...); /* 28 4 */
int (*set_property)(...); /* 32 4 */
int (*property_is_writeable)(...); /* 36 4 */
void (*external_power_changed)(...); /* 40 4 */
int (*init)(struct power_supply *); /* 44 4 */
bool no_thermal; /* 48 1 */
/* XXX 3 bytes hole, try to pack */
int use_for_apm; /* 52 4 */
/* size: 56, cachelines: 1, members: 14 */
/* sum members: 50, holes: 2, sum holes: 6 */
/* last cacheline: 56 bytes */
};
This can be optimized by moving `u8 charge_behaviours` to the end and
swapping `int use_for_apm` with `bool no_thermal`:
struct power_supply_desc {
const char * name; /* 0 4 */
enum power_supply_type type; /* 4 4 */
u32 charge_types; /* 8 4 */
u32 usb_types; /* 12 4 */
const enum power_supply_property * properties; /* 16 4 */
size_t num_properties; /* 20 4 */
int (*get_property)(...); /* 24 4 */
int (*set_property)(...); /* 28 4 */
int (*property_is_writeable)(...); /* 32 4 */
void (*external_power_changed)(...); /* 36 4 */
int (*init)(struct power_supply *); /* 40 4 */
int use_for_apm; /* 44 4 */
bool no_thermal; /* 48 1 */
u8 charge_behaviours; /* 49 1 */
/* size: 52, cachelines: 1, members: 14 */
/* padding: 2 */
/* last cacheline: 52 bytes */
};
Do this in order to save 4 bytes. This will also help when adding new
members in the future to this `struct`.
Signed-off-by: Waqar Hameed <waqar.hameed@xxxxxxxx>
---
include/linux/power_supply.h | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index e749d21893352..fcd05f4a88833 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -259,7 +259,6 @@ struct power_supply_config {
struct power_supply_desc {
const char *name;
enum power_supply_type type;
- u8 charge_behaviours;
u32 charge_types;
u32 usb_types;
const enum power_supply_property *properties;
@@ -295,14 +294,17 @@ struct power_supply_desc {
*/
int (*init)(struct power_supply *psy);
+ /* For APM emulation, think legacy userspace. */
+ int use_for_apm;
+
/*
* Set if thermal zone should not be created for this power supply.
* For example for virtual supplies forwarding calls to actual
* sensors or other supplies.
*/
bool no_thermal;
- /* For APM emulation, think legacy userspace. */
- int use_for_apm;
+
+ u8 charge_behaviours;
};
struct power_supply_ext {
--
2.43.0