Skip to contentSkip to conversation

rtser_config and compat userspace

PATCH Richard Weinberger 3 participants 5 messages Nov 14, 2022

lore
Share & tools
lore
#0 lore raw

Hi!

I noticed that a 32-bits x86 application cannot use RTSER_RTIOC_SET_CONFIG and friends when the kernel runs in 64-bits mode.
The problem is that struct rtser_config members have different alignment.

On 32bits:
struct rtser_config {

        int                        config_mask;          /*     0     4 */
        int                        baud_rate;            /*     4     4 */
        int                        parity;               /*     8     4 */
        int                        data_bits;            /*    12     4 */
        int                        stop_bits;            /*    16     4 */
        int                        handshake;            /*    20     4 */
        int                        fifo_depth;           /*    24     4 */
        int                        reserved;             /*    28     4 */
        nanosecs_rel_t             rx_timeout;           /*    32     8 */
        nanosecs_rel_t             tx_timeout;           /*    40     8 */
        nanosecs_rel_t             event_timeout;        /*    48     8 */
        int                        timestamp_history;    /*    56     4 */
        int                        event_mask;           /*    60     4 */
        /* --- cacheline 1 boundary (64 bytes) --- */
        int                        rs485;                /*    64     4 */
        
        /* size: 68, cachelines: 2, members: 14 */
        /* last cacheline: 4 bytes */
} __attribute__((__packed__));

Note: I don't know why pahole reports this struct as packed. It is not.

and 64:
struct rtser_config {

        int                        config_mask;          /*     0     4 */
        int                        baud_rate;            /*     4     4 */
        int                        parity;               /*     8     4 */
        int                        data_bits;            /*    12     4 */
        int                        stop_bits;            /*    16     4 */
        int                        handshake;            /*    20     4 */
        int                        fifo_depth;           /*    24     4 */
        int                        reserved;             /*    28     4 */
        nanosecs_rel_t             rx_timeout;           /*    32     8 */
        nanosecs_rel_t             tx_timeout;           /*    40     8 */
        nanosecs_rel_t             event_timeout;        /*    48     8 */
        int                        timestamp_history;    /*    56     4 */
        int                        event_mask;           /*    60     4 */
        /* --- cacheline 1 boundary (64 bytes) --- */
        int                        rs485;                /*    64     4 */

        /* size: 72, cachelines: 2, members: 14 */
        /* padding: 4 */
        /* last cacheline: 8 bytes */
};

While nanosecs_rel_t is in both case int64_t, gcc adds different tail padding to the struct.

I propose the following fix:

include/rtdm/uapi/serial.h +2 -0
index 9ac691b723a0..3c798de13985 100644
@@ -240,6 +240,8 @@ typedef struct rtser_config {
 
        /** enable RS485 mode, see @ref RTSER_RS485_xxx */
        int             rs485;
+
+       int             padding0;
 } rtser_config_t;
 
 /**

But I'm not sure whether you can accept this since it changes the ABI and existing Xenomai applications will have to be recompiled.

Thanks,
//richard

#1 lore raw
Quoted 72 lines · Richard Weinberger

Hi!

I noticed that a 32-bits x86 application cannot use RTSER_RTIOC_SET_CONFIG and friends when the kernel runs in 64-bits mode.
The problem is that struct rtser_config members have different alignment.

On 32bits:
struct rtser_config {

        int                        config_mask;          /*     0     4 */
        int                        baud_rate;            /*     4     4 */
        int                        parity;               /*     8     4 */
        int                        data_bits;            /*    12     4 */
        int                        stop_bits;            /*    16     4 */
        int                        handshake;            /*    20     4 */
        int                        fifo_depth;           /*    24     4 */
        int                        reserved;             /*    28     4 */
        nanosecs_rel_t             rx_timeout;           /*    32     8 */
        nanosecs_rel_t             tx_timeout;           /*    40     8 */
        nanosecs_rel_t             event_timeout;        /*    48     8 */
        int                        timestamp_history;    /*    56     4 */
        int                        event_mask;           /*    60     4 */
        /* --- cacheline 1 boundary (64 bytes) --- */
        int                        rs485;                /*    64     4 */
        
        /* size: 68, cachelines: 2, members: 14 */
        /* last cacheline: 4 bytes */
} __attribute__((__packed__));

Note: I don't know why pahole reports this struct as packed. It is not.

and 64:
struct rtser_config {

        int                        config_mask;          /*     0     4 */
        int                        baud_rate;            /*     4     4 */
        int                        parity;               /*     8     4 */
        int                        data_bits;            /*    12     4 */
        int                        stop_bits;            /*    16     4 */
        int                        handshake;            /*    20     4 */
        int                        fifo_depth;           /*    24     4 */
        int                        reserved;             /*    28     4 */
        nanosecs_rel_t             rx_timeout;           /*    32     8 */
        nanosecs_rel_t             tx_timeout;           /*    40     8 */
        nanosecs_rel_t             event_timeout;        /*    48     8 */
        int                        timestamp_history;    /*    56     4 */
        int                        event_mask;           /*    60     4 */
        /* --- cacheline 1 boundary (64 bytes) --- */
        int                        rs485;                /*    64     4 */

        /* size: 72, cachelines: 2, members: 14 */
        /* padding: 4 */
        /* last cacheline: 8 bytes */
};

While nanosecs_rel_t is in both case int64_t, gcc adds different tail padding to the struct.

I propose the following fix:

include/rtdm/uapi/serial.h +2 -0
index 9ac691b723a0..3c798de13985 100644
@@ -240,6 +240,8 @@ typedef struct rtser_config {
 
        /** enable RS485 mode, see @ref RTSER_RS485_xxx */
        int             rs485;
+
+       int             padding0;
 } rtser_config_t;
 
 /**
Quoted 3 lines

But I'm not sure whether you can accept this since it changes the ABI and existing Xenomai applications will have to be recompiled.

There are two unmodifiable truths right now: 64-on-64 bit and 32-on-32 bit. The 64-bit kernel must present the same ABI to a 32-bit application than the 32-bit kernel does. Only that will permit existing 32-bit apps to run over 64-bit. Data structures used for that need to be modeled accordingly. That may mean defining a special in-kernel compat version of the structure that models 32-bit layout (as expected by 32-bit userspace) on 64-bit kernels.

Jan

Signature · 2 lines
Siemens AG, Technology
Competence Center Embedded Linux
#2 lore raw

----- Ursprüngliche Mail -----

Quoted 8 lines

Von: "Jan Kiszka" <[email protected]>
There are two unmodifiable truths right now: 64-on-64 bit and 32-on-32 bit. The 64-bit kernel must present the same ABI to a 32-bit application than the 32-bit kernel does. Only that will permit existing 32-bit apps to run over 64-bit. Data structures used for that need to be modeled accordingly. That may mean defining a special in-kernel compat version of the structure that models 32-bit layout (as expected by 32-bit userspace) on 64-bit kernels.

So Xenomai follows the rules of Linux and does not break ABI. :-)

Thanks,
//richard

#3 lore raw
In reply to Richard Weinberger
Quoted 71 lines · Richard Weinberger

Hi!

I noticed that a 32-bits x86 application cannot use RTSER_RTIOC_SET_CONFIG and friends when the kernel runs in 64-bits mode.
The problem is that struct rtser_config members have different alignment.>
On 32bits:
struct rtser_config {

        int                        config_mask;          /*     0     4 */
        int                        baud_rate;            /*     4     4 */
        int                        parity;               /*     8     4 */
        int                        data_bits;            /*    12     4 */
        int                        stop_bits;            /*    16     4 */
        int                        handshake;            /*    20     4 */
        int                        fifo_depth;           /*    24     4 */
        int                        reserved;             /*    28     4 */
        nanosecs_rel_t             rx_timeout;           /*    32     8 */
        nanosecs_rel_t             tx_timeout;           /*    40     8 */
        nanosecs_rel_t             event_timeout;        /*    48     8 */
        int                        timestamp_history;    /*    56     4 */
        int                        event_mask;           /*    60     4 */
        /* --- cacheline 1 boundary (64 bytes) --- */
        int                        rs485;                /*    64     4 */
        
        /* size: 68, cachelines: 2, members: 14 */
        /* last cacheline: 4 bytes */
} __attribute__((__packed__));

Note: I don't know why pahole reports this struct as packed. It is not.

and 64:
struct rtser_config {

        int                        config_mask;          /*     0     4 */
        int                        baud_rate;            /*     4     4 */
        int                        parity;               /*     8     4 */
        int                        data_bits;            /*    12     4 */
        int                        stop_bits;            /*    16     4 */
        int                        handshake;            /*    20     4 */
        int                        fifo_depth;           /*    24     4 */
        int                        reserved;             /*    28     4 */
        nanosecs_rel_t             rx_timeout;           /*    32     8 */
        nanosecs_rel_t             tx_timeout;           /*    40     8 */
        nanosecs_rel_t             event_timeout;        /*    48     8 */
        int                        timestamp_history;    /*    56     4 */
        int                        event_mask;           /*    60     4 */
        /* --- cacheline 1 boundary (64 bytes) --- */
        int                        rs485;                /*    64     4 */

        /* size: 72, cachelines: 2, members: 14 */
        /* padding: 4 */
        /* last cacheline: 8 bytes */
};

While nanosecs_rel_t is in both case int64_t, gcc adds different tail padding to the struct.

I propose the following fix:

include/rtdm/uapi/serial.h +2 -0
index 9ac691b723a0..3c798de13985 100644
@@ -240,6 +240,8 @@ typedef struct rtser_config {
 
        /** enable RS485 mode, see @ref RTSER_RS485_xxx */
        int             rs485;
+
+       int             padding0;
 } rtser_config_t;
 
 /**
Quoted 2 lines

But I'm not sure whether you can accept this since it changes the ABI and existing Xenomai applications will have to be recompiled.

Which Xenomai version are you targeting? Maybe the following arguments can help. I hope they are correct, it's getting late here.

A 64 bit application can only be served by a 64 bit kernel and for such applications sizeof() and offsets will remain the same. No need to recompile as not affected.

A 32 bit application can only be served by a 64 bit kernel as well as we no longer support 32 bit kernels. That should apply to all stable trees but I'm not 100% sure about 3.0 and 3.1. No need to recompile the kernel as it is already 64 bit. 32 bit/compat applications are broken so we need to recompile anyhow to get them fixed. Should be acceptable, no?

Btw: My understanding is that a 64 bit kernel would always expect 72 bytes for this struct. If userspace only delivers 68 we should report that (-EINVAL) quite early. Did that happen? If not: We are missing a validation somewhere.

Florian

Quoted 4 lines

Thanks,
//richard

#4 lore raw
Quoted 82 lines · Florian Bezdeka
Quoted 71 lines · Richard Weinberger

Hi!

I noticed that a 32-bits x86 application cannot use RTSER_RTIOC_SET_CONFIG and friends when the kernel runs in 64-bits mode.
The problem is that struct rtser_config members have different alignment.>
On 32bits:
struct rtser_config {

        int                        config_mask;          /*     0     4 */
        int                        baud_rate;            /*     4     4 */
        int                        parity;               /*     8     4 */
        int                        data_bits;            /*    12     4 */
        int                        stop_bits;            /*    16     4 */
        int                        handshake;            /*    20     4 */
        int                        fifo_depth;           /*    24     4 */
        int                        reserved;             /*    28     4 */
        nanosecs_rel_t             rx_timeout;           /*    32     8 */
        nanosecs_rel_t             tx_timeout;           /*    40     8 */
        nanosecs_rel_t             event_timeout;        /*    48     8 */
        int                        timestamp_history;    /*    56     4 */
        int                        event_mask;           /*    60     4 */
        /* --- cacheline 1 boundary (64 bytes) --- */
        int                        rs485;                /*    64     4 */
        
        /* size: 68, cachelines: 2, members: 14 */
        /* last cacheline: 4 bytes */
} __attribute__((__packed__));

Note: I don't know why pahole reports this struct as packed. It is not.

and 64:
struct rtser_config {

        int                        config_mask;          /*     0     4 */
        int                        baud_rate;            /*     4     4 */
        int                        parity;               /*     8     4 */
        int                        data_bits;            /*    12     4 */
        int                        stop_bits;            /*    16     4 */
        int                        handshake;            /*    20     4 */
        int                        fifo_depth;           /*    24     4 */
        int                        reserved;             /*    28     4 */
        nanosecs_rel_t             rx_timeout;           /*    32     8 */
        nanosecs_rel_t             tx_timeout;           /*    40     8 */
        nanosecs_rel_t             event_timeout;        /*    48     8 */
        int                        timestamp_history;    /*    56     4 */
        int                        event_mask;           /*    60     4 */
        /* --- cacheline 1 boundary (64 bytes) --- */
        int                        rs485;                /*    64     4 */

        /* size: 72, cachelines: 2, members: 14 */
        /* padding: 4 */
        /* last cacheline: 8 bytes */
};

While nanosecs_rel_t is in both case int64_t, gcc adds different tail padding to the struct.

I propose the following fix:

include/rtdm/uapi/serial.h +2 -0
index 9ac691b723a0..3c798de13985 100644
@@ -240,6 +240,8 @@ typedef struct rtser_config {
 
        /** enable RS485 mode, see @ref RTSER_RS485_xxx */
        int             rs485;
+
+       int             padding0;
 } rtser_config_t;
 
 /**
Quoted 2 lines

But I'm not sure whether you can accept this since it changes the ABI and existing Xenomai applications will have to be recompiled.

Which Xenomai version are you targeting? Maybe the following arguments can help. I hope they are correct, it's getting late here.

A 64 bit application can only be served by a 64 bit kernel and for such applications sizeof() and offsets will remain the same. No need to recompile as not affected.

Quoted 5 lines

A 32 bit application can only be served by a 64 bit kernel as well as we no longer support 32 bit kernels. That should apply to all stable trees but I'm not 100% sure about 3.0 and 3.1. No need to recompile the kernel as it is already 64 bit. 32 bit/compat applications are broken so we need to recompile anyhow to get them fixed. Should be acceptable, no?

I missed the 32 bit application on 32 bit kernel here, as Jan already mentioned in his answer. That is working now, but would break existing applications if they are not updated in lockstep with the kernel.

Instead of implementing the full compat stuff with all the necessary
changes: Can we hide that behind some API-revision bump? Forcing users to recompile? I assume that would reduce complexity a lot.

Quoted 13 lines

Btw: My understanding is that a 64 bit kernel would always expect 72 bytes for this struct. If userspace only delivers 68 we should report that (-EINVAL) quite early. Did that happen? If not: We are missing a validation somewhere.

Florian

Quoted 4 lines

Thanks,
//richard