From patchwork Fri Apr 20 12:05:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 133871 Delivered-To: patch@linaro.org Received: by 10.46.66.142 with SMTP id h14csp198154ljf; Fri, 20 Apr 2018 05:07:38 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/B0I6K+1jssbXw8EzYvYDaJkGK/+MRU3LgsnjsiKNHJYp0IJmBXJdl21jLDQ2cfit6iiQh X-Received: by 10.99.37.196 with SMTP id l187mr8447342pgl.221.1524226058728; Fri, 20 Apr 2018 05:07:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524226058; cv=none; d=google.com; s=arc-20160816; b=gKSEY87cKFD34mF1s1njfdb+z5TSdfO0ew4eQycndDeoAfRzAQ23IJVSLz6j0MoiR2 /oQ3kw1gVk2tBPII3RNldYExSXf5oXl/00df8DQSloxRtc8zXXBAXHFhg+di+5akk4sV VPZ/TP5cJLxtch6aB3nfDbWzy1AbjnbsFxExYIa3UrPvTEQSitHYwGRQKlWeDCup8I4s n6P5q9e+d1rI/NfdD20eqafG6EzQ7b+2mkElBexcCdIUsT21bvlgBw1MC4Hf/UT4/iYf TD+qlQRoagKkyduZS+sUVlwXov7ncgdH/dvsDeUvD4T2L+xBxdnWfkJQP50OLFo9GAOw pjww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=CaZ+lbQ0l4bL9J9SaktDIsD6mbZ4DDscAxex7IW+NKY=; b=LcrkGwNg/FZtKbOSuouJfZ6SQBxuVUeTl2lMqG2rS/9rSx5a+L+Q0HTFUJs2dJdo5w thIUyf33lPyEwQS1UfTCsihzmChdTTRG3nRwCz4VVu+DJ9GbpvzAPRIkdbi5iPFUTjvj 0/b8FPW8M8Ht/OBZ/OTpd1FhRGVM97yofnSi6/VtJeIJkfrs6YosZJdGIqnJ9CuQlVyM BKRJzPTwkEU3+pLkK076S0OVZ5qS038D9q6hmtUb9le6EJYMNCVCTKNuxApnCHsPkK7W pSw9T5rgWkEcOy7a28nwwkIM0OlOlk3EhIHJcoB57tsjy5EVnftzJaLfB0mnZV4yl8nQ 1e2A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l4si4883497pgc.374.2018.04.20.05.07.38; Fri, 20 Apr 2018 05:07:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754730AbeDTMHf (ORCPT + 29 others); Fri, 20 Apr 2018 08:07:35 -0400 Received: from mout.kundenserver.de ([212.227.126.187]:50873 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754673AbeDTMHb (ORCPT ); Fri, 20 Apr 2018 08:07:31 -0400 Received: from wuerfel.lan ([95.208.111.237]) by mrelayeu.kundenserver.de (mreue006 [212.227.15.129]) with ESMTPA (Nemesis) id 0LqaYA-1eWI6b49Io-00eMqe; Fri, 20 Apr 2018 14:06:44 +0200 From: Arnd Bergmann To: y2038@lists.linaro.org Cc: linux-kernel@vger.kernel.org, x86@kernel.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, Arnd Bergmann , Paul Eggert , "Eric W . Biederman" , Richard Henderson , Ivan Kokshaysky , Matt Turner , Al Viro , Dominik Brodowski , Thomas Gleixner , Andrew Morton , linux-alpha@vger.kernel.org Subject: [PATCH v2 1/2] y2038: rusage: Use __kernel_old_timeval for process times Date: Fri, 20 Apr 2018 14:05:13 +0200 Message-Id: <20180420120605.1612248-1-arnd@arndb.de> X-Mailer: git-send-email 2.9.0 X-Provags-ID: V03:K1:0SNFXZmFStKcH5/8A4BPPkveCyAk32QpJigBXVo4F7YQWwpeUFS YVWvXTrGRJg2blaKLn2PhWh/cQQIIWp6/H9+pocsn+bRgyVDvA8bE5dHVpmIdven1UWJkjd AnBiCuEo9xd2qjP4VNFl2Wq9jloJs7r6AibkLCOcgOM2SqYdyLFY4C38APSKgwodCq++lOi hcExeyh3dw9UDhN1/Q5Gg== X-UI-Out-Filterresults: notjunk:1; V01:K0:1gcBgKKlDZo=:Q+6FinLZilEVDee+qz6neT h1gRz00XaggIMUnZEYcrZfH30sFfmzW3cYltBx6jvvYuKFEaGQYqSzN3vA4ufhlaCcs3djAxj z8tTvYZyKn/x32p9BAjG8EqHxM2qrb5J9eJ6UASjBDgn+ilD7rtz2W7VKRSh76D5YNum/mxN5 L+cBxM4JR23T+QBZizDRzm3kaeA4uK67jzmoKwcbK5whou6KJXuDvyQZrRTCF52thVzlA5GKg YzBKGF5LEntKNIm7pygiIq7v92M8rN1+Dm+6s8x4YSm7unTfX5Z8xs5H5D6okCbOgW1A4o4aV ziGZYWjEsBoyMFPzsnJq9eO8Ih+lQ9YMtxEtVWrpp5HaAnyoB4FHirDdpx2305o86aQHT1qFw Q6Z1acgsKpbTZEugFHnHdZdBUyQw+vM2LruB7UOizaagx89lfoT7CaJTqqaufnuimgOKdQsfr EN2jtSE6IBshoxMspPzSUzh5YVL2Kxf7x5oCgMbUtChqxbYBAf2h02o+UaSwbvey1gEyLYK+P 3ilgc84pzqFayJTZSOxIvDyWJKNucHzRMZXgIM5bxa/ohxIorIaZ0pQ8DCt1pLjxx6dqlbm5I IGeFPZWVUs5eSMLB8wSivASC87aU58+YqD3Q9J438+fcOR5IYAGREQ0LC8NbnJW9YJ+uiujOZ pBSIro1UihWmryRRdpPJmoIKvuaa/kUgU0KmEEOibC1isNU905lmxJsqylKD4h4SKH8vLDDEf L114s0xht4EhYNbGOaJQJu2M1vdfxxSkWpegpA== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 'struct rusage' contains the run times of a process in 'timeval' format and is accessed through the wait4() and getrusage() system calls. This is not a problem for y2038 safety by itself, but causes an issue when the C library starts using 64-bit time_t on 32-bit architectures because the structure layout becomes incompatible. There are three possible ways of dealing with this: a) deprecate the wait4() and getrusage() system calls, and create a set of kernel interfaces based around a newly defined structure that could solve multiple problems at once, e.g. provide more fine-grained timestamps. The C library could then implement the posix interfaces on top of the new system calls. b) Extend the approach taken by the x32 ABI, and use the 64-bit native structure layout for rusage on all architectures with new system calls that is otherwise compatible. A downside of this is that it requires a number of ugly hacks to deal with all the other fields of the structure also becoming 64 bit wide. Especially on big-endian architectures, we can't easily use the union trick from glibc. c) Change the definition of struct rusage to be independent of time_t. This is the easiest change, as it does not involve new system call entry points, but it requires the C library to convert between the kernel format of the structure and the user space definition. d) Add a new ABI variant of 'struct rusage' that corresponds to the current layout with 32-bit counters but 64-bit time_t. This would minimize the libc changes but require additional kernel code to handle a third binary layout on 64-bit kernels. I'm picking approach c) for its simplicity. As pointed out by reviewers, simply using the kernel structure in user space would not be POSIX compliant, but I have verified that none of the usual C libraries (glibc, musl, uclibc-ng, newlib) do that. Instead, they all provide their own definition of 'struct rusage' to applications in sys/resource.h. To be on the safe side, I'm only changing the definition inside of the kernel and for user space with an updated 'time_t'. All existing users will see the traditional layout that is compatible with what the C libraries export. A 32-bit application that includes linux/resource.h but uses an update C library with 64-bit time_t will now see the low-level kernel structure that corresponds to the getrusage() system call interface but that will be different from one defined in sys/resource.h for the getrusage library interface. Link: https://patchwork.kernel.org/patch/10077527/ Cc: Paul Eggert Cc: Eric W. Biederman Signed-off-by: Arnd Bergmann --- arch/alpha/kernel/osf_sys.c | 15 +++++++++------ include/uapi/linux/resource.h | 14 ++++++++++++-- kernel/sys.c | 4 ++-- 3 files changed, 23 insertions(+), 10 deletions(-) -- 2.9.0 diff --git a/arch/alpha/kernel/osf_sys.c b/arch/alpha/kernel/osf_sys.c index 89faa6f4de47..cad03ee445b3 100644 --- a/arch/alpha/kernel/osf_sys.c +++ b/arch/alpha/kernel/osf_sys.c @@ -1184,6 +1184,7 @@ SYSCALL_DEFINE4(osf_wait4, pid_t, pid, int __user *, ustatus, int, options, struct rusage32 __user *, ur) { unsigned int status = 0; + struct rusage32 r32; struct rusage r; long err = kernel_wait4(pid, &status, options, &r); if (err <= 0) @@ -1192,12 +1193,14 @@ SYSCALL_DEFINE4(osf_wait4, pid_t, pid, int __user *, ustatus, int, options, return -EFAULT; if (!ur) return err; - if (put_tv_to_tv32(&ur->ru_utime, &r.ru_utime)) - return -EFAULT; - if (put_tv_to_tv32(&ur->ru_stime, &r.ru_stime)) - return -EFAULT; - if (copy_to_user(&ur->ru_maxrss, &r.ru_maxrss, - sizeof(struct rusage32) - offsetof(struct rusage32, ru_maxrss))) + r32.ru_utime.tv_sec = r.ru_utime.tv_sec; + r32.ru_utime.tv_usec = r.ru_utime.tv_usec; + r32.ru_stime.tv_sec = r.ru_stime.tv_sec; + r32.ru_stime.tv_usec = r.ru_stime.tv_usec; + memcpy(&r32.ru_maxrss, &r.ru_maxrss, + sizeof(struct rusage32) - offsetof(struct rusage32, ru_maxrss)); + + if (copy_to_user(ur, &r32, sizeof(r32))) return -EFAULT; return err; } diff --git a/include/uapi/linux/resource.h b/include/uapi/linux/resource.h index cc00fd079631..611d3745c70a 100644 --- a/include/uapi/linux/resource.h +++ b/include/uapi/linux/resource.h @@ -22,8 +22,18 @@ #define RUSAGE_THREAD 1 /* only the calling thread */ struct rusage { - struct timeval ru_utime; /* user time used */ - struct timeval ru_stime; /* system time used */ +#if (__BITS_PER_LONG != 32 || !defined(__USE_TIME_BITS64)) && !defined(__KERNEL__) + struct timeval ru_utime; /* user time used */ + struct timeval ru_stime; /* system time used */ +#else + /* + * For 32-bit user space with 64-bit time_t, the binary layout + * in these fields is incompatible with 'struct timeval', so the + * C library has to translate this into the POSIX compatible layout. + */ + struct __kernel_old_timeval ru_utime; + struct __kernel_old_timeval ru_stime; +#endif __kernel_long_t ru_maxrss; /* maximum resident set size */ __kernel_long_t ru_ixrss; /* integral shared memory size */ __kernel_long_t ru_idrss; /* integral unshared data size */ diff --git a/kernel/sys.c b/kernel/sys.c index ad692183dfe9..1de538f622e8 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -1769,8 +1769,8 @@ void getrusage(struct task_struct *p, int who, struct rusage *r) unlock_task_sighand(p, &flags); out: - r->ru_utime = ns_to_timeval(utime); - r->ru_stime = ns_to_timeval(stime); + r->ru_utime = ns_to_kernel_old_timeval(utime); + r->ru_stime = ns_to_kernel_old_timeval(stime); if (who != RUSAGE_CHILDREN) { struct mm_struct *mm = get_task_mm(p);