All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wei Yang <richard.weiyang@gmail.com>
To: David Hildenbrand <david@redhat.com>
Cc: Wei Yang <richard.weiyang@gmail.com>,
	Oscar Salvador <osalvador@suse.de>,
	linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [PATCH v1 2/2] mm/highmem: make nr_free_highpages() return "unsigned long"
Date: Wed, 12 Jun 2024 07:34:35 +0000	[thread overview]
Message-ID: <20240612073435.52fch2aut5lxeyhf@master> (raw)
In-Reply-To: <2d0df7ca-2720-4a0c-95c5-9b665a567e5f@redhat.com>

On Wed, Jun 12, 2024 at 09:22:25AM +0200, David Hildenbrand wrote:
>On 12.06.24 09:01, Wei Yang wrote:
>> On Tue, Jun 11, 2024 at 11:20:00AM +0200, David Hildenbrand wrote:
>> > On 11.06.24 02:56, Wei Yang wrote:
>> > > On Mon, Jun 10, 2024 at 10:22:49AM +0200, David Hildenbrand wrote:
>> > > > On 10.06.24 05:40, Oscar Salvador wrote:
>> > > > > On Fri, Jun 07, 2024 at 10:37:11AM +0200, David Hildenbrand wrote:
>> > > > > > It looks rather weird that totalhigh_pages() returns an
>> > > > > > "unsigned long" but nr_free_highpages() returns an "unsigned int".
>> > > > > > 
>> > > > > > Let's return an "unsigned long" from nr_free_highpages() to be
>> > > > > > consistent.
>> > > > > > 
>> > > > > > While at it, use a plain "0" instead of a "0UL" in the !CONFIG_HIGHMEM
>> > > > > > totalhigh_pages() implementation, to make these look alike as well.
>> > > > > > 
>> > > > > > Signed-off-by: David Hildenbrand <david@redhat.com>
>> > > > > ...
>> > > > > > -static inline unsigned int nr_free_highpages(void) { return 0; }
>> > > > > > -static inline unsigned long totalhigh_pages(void) { return 0UL; }
>> > > > > > +static inline unsigned long nr_free_highpages(void) { return 0; }
>> > > > > > +static inline unsigned long totalhigh_pages(void) { return 0; }
>> > > > > 
>> > > > > Although I doubt it has any consequences, I would just leave them both with UL,
>> > > > > so the return type is consistent with what we are returning.
>> > > > 
>> > > > These suffixes are only required when using constants that would not fit
>> > > > into the native (int) type, or converting from that native (int) type to
>> > > > something else automatically by the compiler would mess things up (for example,
>> > > > undesired sign extension). For 0 that is certainly impossible :)
>> > > > 
>> > > > 
>> > > > That's also the reason why in include/linux we now have:
>> > > > 
>> > > > t14s: ~/git/linux/include/linux $ git grep "return 0UL;"
>> > > > skbuff.h:       return 0UL;
>> > > > uaccess.h:static inline unsigned long user_access_save(void) { return 0UL; }
>> > > > t14s: ~/git/linux/include/linux $ git grep "0UL;"
>> > > > bitmap.h:               *dst = ~0UL;
>> > > > dax.h:          return ~0UL;
>> > > > mtd/map.h:                      r.x[i] = ~0UL;
>> > > > netfilter.h:    return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
>> > > > skbuff.h:       return 0UL;
>> > > > uaccess.h:static inline unsigned long user_access_save(void) { return 0UL; }
>> > > > 
>> > > > 
>> > > > ... compared to a long list if "unsigned long" functions that simply "return 0;"
>> > > > 
>> > > 
>> > > Seems this is the current status.
>> > > 
>> > > Then my question is do we have a guide line for this? Or 0 is the special
>> > > case? Sounds positive value has no sign extension problem. If we need to
>> > > return 1, we suppose to use 1 or 1UL? I found myself confused.
>> > > 
>> > > I grepped "return 1" and do find some cases without UL:
>> > > 
>> > > backing-dev.h: wb_stat_error() return 1 for unsigned long.
>> > > pgtable.h: pte_batch_hint() return 1 for unsigned int.
>> > > 
>> > > So the guide line is for positive value, it is not necessary to use UL?
>> > 
>> > I think when returning simple values (0/1/-1), we really don't need these
>> > suffices at all. The standard says "The type of an integer constant is the
>> > first of the corresponding list in which its value can be represented.". I
>> > thought it would always use an "int", but that is not the case.
>> > 
>> > So, if we use "-1", the compiler will use an "int", and sign extension to
>> > "unsigned" long will do the right thing.
>> > 
>> > Simple test:
>> > 
>> > -1 results in: 0xffffffffffffffff
>> > -1U results in: 0xffffffff
>> > -1UL results in: 0xffffffffffffffff
>> > 0xffffffff results in: 0xffffffff
>> > 0xffffffffU results in: 0xffffffff
>> > 0xffffffffUL results in: 0xffffffff
>> > ~0xffffffff results in: 0x0
>> > ~0xffffffffU results in: 0x0
>> > ~0xffffffffUL results in: 0xffffffff00000000
>> > 0xffffffffffffffff results in: 0xffffffffffffffff
>> > 0xffffffffffffffffU results in: 0xffffffffffffffff
>> 
>> I expect this to be 0xffffffff. Why this extend it to a UL?
>
>Apparently, the "U" only restricts the set of types to "unsigned ones".
>
>https://en.cppreference.com/w/cpp/language/integer_literal
>
>So the compiler will use the first "unsigned" type that can hold that value.
>

Interesting, thanks for the reference.

Reviewed-by: Wei Yang <richard.weiyang@gmail.com>

>-- 
>Cheers,
>
>David / dhildenb

-- 
Wei Yang
Help you, Help me


  reply	other threads:[~2024-06-12  7:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-07  8:37 [PATCH v1 0/2] mm/highmem: don't track highmem pages manually David Hildenbrand
2024-06-07  8:37 ` [PATCH v1 1/2] mm/highmem: reimplement totalhigh_pages() by walking zones David Hildenbrand
2024-06-07 21:04   ` David Hildenbrand
2024-06-08  0:48   ` Wei Yang
2024-06-10  3:23   ` Oscar Salvador
2024-06-07  8:37 ` [PATCH v1 2/2] mm/highmem: make nr_free_highpages() return "unsigned long" David Hildenbrand
2024-06-08  0:51   ` Wei Yang
2024-06-10  8:23     ` David Hildenbrand
2024-06-10  3:40   ` Oscar Salvador
2024-06-10  8:22     ` David Hildenbrand
2024-06-11  0:56       ` Wei Yang
2024-06-11  9:20         ` David Hildenbrand
2024-06-12  7:01           ` Wei Yang
2024-06-12  7:22             ` David Hildenbrand
2024-06-12  7:34               ` Wei Yang [this message]
2024-06-08  0:45 ` [PATCH v1 0/2] mm/highmem: don't track highmem pages manually Wei Yang

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=20240612073435.52fch2aut5lxeyhf@master \
    --to=richard.weiyang@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=osalvador@suse.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.