Skip to content

Commit a07804c

Browse files
wangbaolin719arndb
authored andcommitted
ALSA: Avoid using timespec for struct snd_timer_status
struct snd_timer_status uses 'timespec' type variables to record timestamp, which will be changed to an incompatible layout with updated user space using 64-bit time_t. To handle both the old and the new layout on 32-bit architectures, this patch introduces 'struct snd_timer_status32' and 'struct snd_timer_status64' to handle 32bit time_t and 64bit time_t in native mode and compat mode, which replaces timespec with s64 type. When glibc changes time_t to 64-bit, any recompiled program will issue ioctl commands that the kernel does not understand without this patch. In the public uapi header, snd_timer_status is now guarded by an #ifndef __KERNEL__ to avoid referencing 'struct timespec'. The timespec definition will be removed from the kernel to prevent new y2038 bugs and to avoid the conflict with an incompatible libc type of the same name. Signed-off-by: Baolin Wang <baolin.wang@linaro.org> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
1 parent fcae40c commit a07804c

3 files changed

Lines changed: 64 additions & 57 deletions

File tree

‎include/uapi/sound/asound.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ struct snd_timer_params {
761761
unsigned char reserved[60]; /* reserved */
762762
};
763763

764+
#ifndef __KERNEL__
764765
struct snd_timer_status {
765766
struct timespec tstamp; /* Timestamp - last update */
766767
unsigned int resolution; /* current period resolution in ns */
@@ -769,6 +770,7 @@ struct snd_timer_status {
769770
unsigned int queue; /* used queue size */
770771
unsigned char reserved[64]; /* reserved */
771772
};
773+
#endif
772774

773775
#define SNDRV_TIMER_IOCTL_PVERSION _IOR('T', 0x00, int)
774776
#define SNDRV_TIMER_IOCTL_NEXT_DEVICE _IOWR('T', 0x01, struct snd_timer_id)

‎sound/core/timer.c‎

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,30 @@ struct snd_timer_user {
6565
struct mutex ioctl_lock;
6666
};
6767

68+
struct snd_timer_status32 {
69+
s32 tstamp_sec; /* Timestamp - last update */
70+
s32 tstamp_nsec;
71+
unsigned int resolution; /* current period resolution in ns */
72+
unsigned int lost; /* counter of master tick lost */
73+
unsigned int overrun; /* count of read queue overruns */
74+
unsigned int queue; /* used queue size */
75+
unsigned char reserved[64]; /* reserved */
76+
};
77+
78+
#define SNDRV_TIMER_IOCTL_STATUS32 _IOR('T', 0x14, struct snd_timer_status32)
79+
80+
struct snd_timer_status64 {
81+
s64 tstamp_sec; /* Timestamp - last update */
82+
s64 tstamp_nsec;
83+
unsigned int resolution; /* current period resolution in ns */
84+
unsigned int lost; /* counter of master tick lost */
85+
unsigned int overrun; /* count of read queue overruns */
86+
unsigned int queue; /* used queue size */
87+
unsigned char reserved[64]; /* reserved */
88+
};
89+
90+
#define SNDRV_TIMER_IOCTL_STATUS64 _IOR('T', 0x14, struct snd_timer_status64)
91+
6892
/* list of timers */
6993
static LIST_HEAD(snd_timer_list);
7094

@@ -1875,17 +1899,41 @@ static int snd_timer_user_params(struct file *file,
18751899
return err;
18761900
}
18771901

1878-
static int snd_timer_user_status(struct file *file,
1879-
struct snd_timer_status __user *_status)
1902+
static int snd_timer_user_status32(struct file *file,
1903+
struct snd_timer_status32 __user *_status)
1904+
{
1905+
struct snd_timer_user *tu;
1906+
struct snd_timer_status32 status;
1907+
1908+
tu = file->private_data;
1909+
if (!tu->timeri)
1910+
return -EBADFD;
1911+
memset(&status, 0, sizeof(status));
1912+
status.tstamp_sec = tu->tstamp.tv_sec;
1913+
status.tstamp_nsec = tu->tstamp.tv_nsec;
1914+
status.resolution = snd_timer_resolution(tu->timeri);
1915+
status.lost = tu->timeri->lost;
1916+
status.overrun = tu->overrun;
1917+
spin_lock_irq(&tu->qlock);
1918+
status.queue = tu->qused;
1919+
spin_unlock_irq(&tu->qlock);
1920+
if (copy_to_user(_status, &status, sizeof(status)))
1921+
return -EFAULT;
1922+
return 0;
1923+
}
1924+
1925+
static int snd_timer_user_status64(struct file *file,
1926+
struct snd_timer_status64 __user *_status)
18801927
{
18811928
struct snd_timer_user *tu;
1882-
struct snd_timer_status status;
1929+
struct snd_timer_status64 status;
18831930

18841931
tu = file->private_data;
18851932
if (!tu->timeri)
18861933
return -EBADFD;
18871934
memset(&status, 0, sizeof(status));
1888-
status.tstamp = timespec64_to_timespec(tu->tstamp);
1935+
status.tstamp_sec = tu->tstamp.tv_sec;
1936+
status.tstamp_nsec = tu->tstamp.tv_nsec;
18891937
status.resolution = snd_timer_resolution(tu->timeri);
18901938
status.lost = tu->timeri->lost;
18911939
status.overrun = tu->overrun;
@@ -2009,8 +2057,10 @@ static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
20092057
return snd_timer_user_info(file, argp);
20102058
case SNDRV_TIMER_IOCTL_PARAMS:
20112059
return snd_timer_user_params(file, argp);
2012-
case SNDRV_TIMER_IOCTL_STATUS:
2013-
return snd_timer_user_status(file, argp);
2060+
case SNDRV_TIMER_IOCTL_STATUS32:
2061+
return snd_timer_user_status32(file, argp);
2062+
case SNDRV_TIMER_IOCTL_STATUS64:
2063+
return snd_timer_user_status64(file, argp);
20142064
case SNDRV_TIMER_IOCTL_START:
20152065
case SNDRV_TIMER_IOCTL_START_OLD:
20162066
return snd_timer_user_start(file);

‎sound/core/timer_compat.c‎

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -69,54 +69,11 @@ static int snd_timer_user_info_compat(struct file *file,
6969
return 0;
7070
}
7171

72-
struct snd_timer_status32 {
73-
struct compat_timespec tstamp;
74-
u32 resolution;
75-
u32 lost;
76-
u32 overrun;
77-
u32 queue;
78-
unsigned char reserved[64];
79-
};
80-
81-
static int snd_timer_user_status_compat(struct file *file,
82-
struct snd_timer_status32 __user *_status)
83-
{
84-
struct snd_timer_user *tu;
85-
struct snd_timer_status32 status;
86-
87-
tu = file->private_data;
88-
if (!tu->timeri)
89-
return -EBADFD;
90-
memset(&status, 0, sizeof(status));
91-
status.tstamp.tv_sec = tu->tstamp.tv_sec;
92-
status.tstamp.tv_nsec = tu->tstamp.tv_nsec;
93-
status.resolution = snd_timer_resolution(tu->timeri);
94-
status.lost = tu->timeri->lost;
95-
status.overrun = tu->overrun;
96-
spin_lock_irq(&tu->qlock);
97-
status.queue = tu->qused;
98-
spin_unlock_irq(&tu->qlock);
99-
if (copy_to_user(_status, &status, sizeof(status)))
100-
return -EFAULT;
101-
return 0;
102-
}
103-
104-
#ifdef CONFIG_X86_X32
105-
/* X32 ABI has the same struct as x86-64 */
106-
#define snd_timer_user_status_x32(file, s) \
107-
snd_timer_user_status(file, s)
108-
#endif /* CONFIG_X86_X32 */
109-
110-
/*
111-
*/
112-
11372
enum {
11473
SNDRV_TIMER_IOCTL_GPARAMS32 = _IOW('T', 0x04, struct snd_timer_gparams32),
11574
SNDRV_TIMER_IOCTL_INFO32 = _IOR('T', 0x11, struct snd_timer_info32),
116-
SNDRV_TIMER_IOCTL_STATUS32 = _IOW('T', 0x14, struct snd_timer_status32),
117-
#ifdef CONFIG_X86_X32
118-
SNDRV_TIMER_IOCTL_STATUS_X32 = _IOW('T', 0x14, struct snd_timer_status),
119-
#endif /* CONFIG_X86_X32 */
75+
SNDRV_TIMER_IOCTL_STATUS_COMPAT32 = _IOW('T', 0x14, struct snd_timer_status32),
76+
SNDRV_TIMER_IOCTL_STATUS_COMPAT64 = _IOW('T', 0x14, struct snd_timer_status64),
12077
};
12178

12279
static long __snd_timer_user_ioctl_compat(struct file *file, unsigned int cmd,
@@ -145,12 +102,10 @@ static long __snd_timer_user_ioctl_compat(struct file *file, unsigned int cmd,
145102
return snd_timer_user_gparams_compat(file, argp);
146103
case SNDRV_TIMER_IOCTL_INFO32:
147104
return snd_timer_user_info_compat(file, argp);
148-
case SNDRV_TIMER_IOCTL_STATUS32:
149-
return snd_timer_user_status_compat(file, argp);
150-
#ifdef CONFIG_X86_X32
151-
case SNDRV_TIMER_IOCTL_STATUS_X32:
152-
return snd_timer_user_status_x32(file, argp);
153-
#endif /* CONFIG_X86_X32 */
105+
case SNDRV_TIMER_IOCTL_STATUS_COMPAT32:
106+
return snd_timer_user_status32(file, argp);
107+
case SNDRV_TIMER_IOCTL_STATUS_COMPAT64:
108+
return snd_timer_user_status64(file, argp);
154109
}
155110
return -ENOIOCTLCMD;
156111
}

0 commit comments

Comments
 (0)