public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Baoquan He <bhe@redhat.com>
To: Chao Fan <fanc.fnst@cn.fujitsu.com>
Cc: linux-kernel@vger.kernel.org, x86@kernel.org, bp@alien8.de,
	tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com,
	keescook@chromium.org, msys.mizuma@gmail.com,
	indou.takao@jp.fujitsu.com, caoj.fnst@cn.fujitsu.com
Subject: Re: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Date: Wed, 12 Dec 2018 15:46:15 +0800	[thread overview]
Message-ID: <20181212074615.GO17340@MiWiFi-R3L-srv> (raw)
In-Reply-To: <20181212031053.1815-2-fanc.fnst@cn.fujitsu.com>

On 12/12/18 at 11:10am, Chao Fan wrote:
> Introduce kstrtoull() from lib/kstrtox.c to boot directory so that code

It's not introducing kstrtoull(), just copying kstrtoull() from
lib/kstrtox.c to boot.

> in boot/ can use kstrtoull() and the old simple_strtoull() can be
> replaced.
> 
> Signed-off-by: Chao Fan <fanc.fnst@cn.fujitsu.com>
> ---
>  arch/x86/boot/string.c | 137 +++++++++++++++++++++++++++++++++++++++++
>  arch/x86/boot/string.h |   2 +
>  2 files changed, 139 insertions(+)
> 
> diff --git a/arch/x86/boot/string.c b/arch/x86/boot/string.c
> index c4428a176973..e02405f20f98 100644
> --- a/arch/x86/boot/string.c
> +++ b/arch/x86/boot/string.c
> @@ -12,7 +12,10 @@
>   * Very basic string functions
>   */
>  
> +#define _LINUX_CTYPE_H
>  #include <linux/types.h>
> +#include <linux/kernel.h>
> +#include <linux/errno.h>
>  #include <asm/asm.h>
>  #include "ctype.h"
>  #include "string.h"
> @@ -187,3 +190,137 @@ char *strchr(const char *s, int c)
>  			return NULL;
>  	return (char *)s;
>  }
> +
> +static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
> +{
> +	union {
> +		u64 v64;
> +		u32 v32[2];
> +	} d = { dividend };
> +	u32 upper;
> +
> +	upper = d.v32[1];
> +	d.v32[1] = 0;
> +	if (upper >= divisor) {
> +		d.v32[1] = upper / divisor;
> +		upper %= divisor;
> +	}
> +	asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) :
> +		"rm" (divisor), "0" (d.v32[0]), "1" (upper));
> +	return d.v64;
> +}
> +
> +static inline u64 div_u64(u64 dividend, u32 divisor)
> +{
> +	u32 remainder;
> +	return div_u64_rem(dividend, divisor, &remainder);
> +}
> +
> +static inline char _tolower(const char c)
> +{
> +	return c | 0x20;
> +}
> +
> +const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
> +{
> +	if (*base == 0) {
> +		if (s[0] == '0') {
> +			if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
> +				*base = 16;
> +			else
> +				*base = 8;
> +		} else
> +			*base = 10;
> +	}
> +	if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
> +		s += 2;
> +	return s;
> +}
> +
> +/*
> + * Convert non-negative integer string representation in explicitly given radix
> + * to an integer.
> + * Return number of characters consumed maybe or-ed with overflow bit.
> + * If overflow occurs, result integer (incorrect) is still returned.
> + *
> + * Don't you dare use this function.
> + */
> +unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
> +{
> +	unsigned long long res;
> +	unsigned int rv;
> +
> +	res = 0;
> +	rv = 0;
> +	while (1) {
> +		unsigned int c = *s;
> +		unsigned int lc = c | 0x20; /* don't tolower() this line */
> +		unsigned int val;
> +
> +		if ('0' <= c && c <= '9')
> +			val = c - '0';
> +		else if ('a' <= lc && lc <= 'f')
> +			val = lc - 'a' + 10;
> +		else
> +			break;
> +
> +		if (val >= base)
> +			break;
> +		/*
> +		 * Check for overflow only if we are within range of
> +		 * it in the max base we support (16)
> +		 */
> +		if (unlikely(res & (~0ull << 60))) {
> +			if (res > div_u64(ULLONG_MAX - val, base))
> +				rv |= KSTRTOX_OVERFLOW;
> +		}
> +		res = res * base + val;
> +		rv++;
> +		s++;
> +	}
> +	*p = res;
> +	return rv;
> +}
> +
> +static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
> +{
> +	unsigned long long _res;
> +	unsigned int rv;
> +
> +	s = _parse_integer_fixup_radix(s, &base);
> +	rv = _parse_integer(s, base, &_res);
> +	if (rv & KSTRTOX_OVERFLOW)
> +		return -ERANGE;
> +	if (rv == 0)
> +		return -EINVAL;
> +	s += rv;
> +	if (*s == '\n')
> +		s++;
> +	if (*s)
> +		return -EINVAL;
> +	*res = _res;
> +	return 0;
> +}
> +
> +/**
> + * kstrtoull - convert a string to an unsigned long long
> + * @s: The start of the string. The string must be null-terminated, and may also
> + *  include a single newline before its terminating null. The first character
> + *  may also be a plus sign, but not a minus sign.
> + * @base: The number base to use. The maximum supported base is 16. If base is
> + *  given as 0, then the base of the string is automatically detected with the
> + *  conventional semantics - If it begins with 0x the number will be parsed as a
> + *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
> + *  parsed as an octal number. Otherwise it will be parsed as a decimal.
> + * @res: Where to write the result of the conversion on success.
> + *
> + * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
> + * Used as a replacement for the obsolete simple_strtoull. Return code must
> + * be checked.
> + */
> +int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
> +{
> +	if (s[0] == '+')
> +		s++;
> +	return _kstrtoull(s, base, res);
> +}
> diff --git a/arch/x86/boot/string.h b/arch/x86/boot/string.h
> index 3d78e27077f4..171007c99acc 100644
> --- a/arch/x86/boot/string.h
> +++ b/arch/x86/boot/string.h
> @@ -29,4 +29,6 @@ extern unsigned int atou(const char *s);
>  extern unsigned long long simple_strtoull(const char *cp, char **endp,
>  					  unsigned int base);
>  
> +int kstrtoull(const char *s, unsigned int base, unsigned long long *res);
> +#define KSTRTOX_OVERFLOW       (1U << 31)
>  #endif /* BOOT_STRING_H */
> -- 
> 2.19.2
> 
> 
> 

  parent reply	other threads:[~2018-12-12  7:46 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-12  3:10 [PATCH v13 0/6] x86/boot/KASLR: Parse ACPI table and limit KASLR to choosing immovable memory Chao Fan
2018-12-12  3:10 ` [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull() Chao Fan
2018-12-12  4:12   ` Chao Fan
2018-12-12  8:03     ` Baoquan He
2018-12-13 13:26       ` Borislav Petkov
2018-12-14  1:34         ` Chao Fan
2018-12-14 10:38           ` Borislav Petkov
2018-12-17  1:27         ` Chao Fan
2018-12-17 15:45           ` Borislav Petkov
2018-12-18  3:20             ` Chao Fan
2018-12-12  7:46   ` Baoquan He [this message]
2018-12-12  8:10     ` Chao Fan
2018-12-13 12:50   ` Borislav Petkov
2018-12-14  2:59     ` Chao Fan
2018-12-14 11:57       ` Borislav Petkov
2018-12-16 19:22   ` kbuild test robot
2018-12-16 20:31     ` Borislav Petkov
2018-12-12  3:10 ` [PATCH v13 2/6] x86/boot: Introduce get_acpi_rsdp() to parse RSDP in cmdline from KEXEC Chao Fan
2018-12-13 19:25   ` Masayoshi Mizuma
2018-12-13 19:29     ` Borislav Petkov
2018-12-13 19:38       ` Masayoshi Mizuma
2018-12-14  1:32     ` Chao Fan
2018-12-13 19:42   ` Borislav Petkov
2018-12-14  1:31     ` Chao Fan
2018-12-12  3:10 ` [PATCH v13 3/6] x86/boot: Introduce efi_get_rsdp_addr() to find RSDP from EFI table Chao Fan
2018-12-13 20:23   ` Borislav Petkov
2018-12-14  1:28     ` Chao Fan
2018-12-17 17:16   ` Masayoshi Mizuma
2018-12-12  3:10 ` [PATCH v13 4/6] x86/boot: Introduce bios_get_rsdp_addr() to search RSDP in memory Chao Fan
2018-12-12  3:10 ` [PATCH v13 5/6] x86/boot: Parse SRAT from RSDP and store immovable memory Chao Fan
2018-12-12  3:10 ` [PATCH v13 6/6] x86/boot/KASLR: Limit KASLR to extracting kernel in " Chao Fan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181212074615.GO17340@MiWiFi-R3L-srv \
    --to=bhe@redhat.com \
    --cc=bp@alien8.de \
    --cc=caoj.fnst@cn.fujitsu.com \
    --cc=fanc.fnst@cn.fujitsu.com \
    --cc=hpa@zytor.com \
    --cc=indou.takao@jp.fujitsu.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=msys.mizuma@gmail.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox