From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752368AbbIOVVX (ORCPT ); Tue, 15 Sep 2015 17:21:23 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:46963 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752167AbbIOVVV (ORCPT ); Tue, 15 Sep 2015 17:21:21 -0400 Date: Tue, 15 Sep 2015 14:21:20 -0700 From: Andrew Morton To: Tejun Heo Cc: John Stultz , LKML , Ingo Molnar , "Steven Rostedt (Red Hat)" , Peter Zijlstra , Masami Hiramatsu , Michal Nazarewicz , Prarit Bhargava , Richard Cochran , Thomas Gleixner , "Theodore Ts'o" , Andreas Dilger , Dave Chinner , Joe Perches , Linus Torvalds Subject: Re: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values Message-Id: <20150915142120.7a2f7ac90b2d69b4879b68d7@linux-foundation.org> In-Reply-To: <20150915034632.GB25658@htj.duckdns.org> References: <1442279124-7309-1-git-send-email-john.stultz@linaro.org> <20150915014936.GA25658@htj.duckdns.org> <20150915034632.GB25658@htj.duckdns.org> X-Mailer: Sylpheed 3.4.1 (GTK+ 2.24.23; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 14 Sep 2015 23:46:32 -0400 Tejun Heo wrote: > Anyways, let's please get abs() working for all types, one way or the > other. That would be by far the best solution, of course. This seems to work OK: --- a/include/linux/kernel.h~a +++ a/include/linux/kernel.h @@ -207,8 +207,11 @@ extern int _cond_resched(void); * for those. */ #define abs(x) ({ \ - long ret; \ - if (sizeof(x) == sizeof(long)) { \ + s64 ret; \ + if (sizeof(x) == sizeof(s64)) { \ + s64 __x = (x); \ + ret = (__x < 0) ? -__x : __x; \ + } else if (sizeof(x) == sizeof(long)) { \ long __x = (x); \ ret = (__x < 0) ? -__x : __x; \ } else { \ Test case: --- /dev/null +++ a/lib/xx.c @@ -0,0 +1,33 @@ +#include + +#define newabs(x) ({ \ + s64 ret; \ + if (sizeof(x) == sizeof(s64)) { \ + s64 __x = (x); \ + ret = (__x < 0) ? -__x : __x; \ + } else if (sizeof(x) == sizeof(long)) { \ + long __x = (x); \ + ret = (__x < 0) ? -__x : __x; \ + } else { \ + int __x = (x); \ + ret = (__x < 0) ? -__x : __x; \ + } \ + ret; \ + }) + +#define oldabs(x) ({ \ + long ret; \ + if (sizeof(x) == sizeof(long)) { \ + long __x = (x); \ + ret = (__x < 0) ? -__x : __x; \ + } else { \ + int __x = (x); \ + ret = (__x < 0) ? -__x : __x; \ + } \ + ret; \ + }) + +int foo(int x) +{ + return oldabs(x); +} diff -puN lib/Makefile~b lib/Makefile --- a/lib/Makefile~b +++ a/lib/Makefile @@ -13,7 +13,7 @@ lib-y := ctype.o string.o vsprintf.o cmd sha1.o md5.o irq_regs.o argv_split.o \ proportions.o flex_proportions.o ratelimit.o show_mem.o \ is_single_threaded.o plist.o decompress.o kobject_uevent.o \ - earlycpio.o seq_buf.o nmi_backtrace.o + earlycpio.o seq_buf.o nmi_backtrace.o xx.o obj-$(CONFIG_ARCH_HAS_DEBUG_STRICT_USER_COPY_CHECKS) += usercopy.o lib-$(CONFIG_MMU) += ioremap.o on i386, xx.o's text is 68 bytes with either newabs() or oldabs(). lib/percpu_counter.o's text does get larger with newabs(). That's because __percpu_counter_compare() is doing abs() on an s64, doh.