mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alex Elder <elder@linaro.org>
To: Jakub Kicinski <kuba@kernel.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>,
	Johannes Berg <johannes@sipsolutions.net>,
	Arnd Bergmann <arnd@arndb.de>,
	Masahiro Yamada <masahiroy@kernel.org>,
	Bjorn Andersson <bjorn.andersson@linaro.org>,
	lkml <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] bitfield.h: add FIELD_MAX() and field_max()
Date: Sat, 29 Feb 2020 07:10:45 -0600	[thread overview]
Message-ID: <be229ebb-53a5-e048-9c68-1b4c7cc2ab9d@linaro.org> (raw)
In-Reply-To: <d6bf67ba-3546-c582-21a6-30cbd4edd984@linaro.org>

On 2/28/20 12:04 PM, Alex Elder wrote:
> On 2/28/20 11:56 AM, Jakub Kicinski wrote:
>> On Fri, 28 Feb 2020 10:53:43 -0600 Alex Elder wrote:
>>> Define FIELD_MAX(), which supplies the maximum value that can be
>>> represented by a field value.  Define field_max() as well, to go
>>> along with the lower-case forms of the field mask functions.
>>>
>>> Signed-off-by: Alex Elder <elder@linaro.org>
>>> ---
>>>
>>>  NOTE:	I'm not entirely sure who owns/maintains this file so
>>> 	I'm sending it to those who have committed things to it.
>>> 	I hope someone will just take it in after review; I use
>>> 	field_max() in some code I'm about to send out.
>>
>> Could you give us an example use?
>>
>> Is it that you find the current macros misnamed or there's something
>> you can't do? Or are you trying to set fields to max?
> 
> I'm trying to validate variable values are in range before attempting
> to use them in a bitfield.

I should have actually checked my code before I sent this.  Yes
I am using the macro as I described, to see if something fits.
But I'm also using it this way:

	foo = u32_get_bits(register, FOO_COUNT_FMASK);
	if (foo == field_max(FOO_COUNT_MASK))
		;	/* This has special meaning */

And another way:

	size_limit = field_max(FOO_COUNT_MASK) * sizeof(struct foo);

So field_max() is really what I need here.  It does imply a
signed interpretation of the field value, but that's true
for all of the lower-case bitfield functions.

I understand the value of FIELD_FIT() but I think field_max()
(and FIELD_MAX()) serves a purpose.  In fact one could argue it
makes FIELD_FIT() unnecessary (compare against field_max(mask)
instead) but I won't propose removing it.

So after further consideration I believe the original patch
is fine.  What are your thoughts?

					-Alex

> I find field_max() to be a good name for what I'm looking for.
> 
> 					-Alex
> 
> #define FOO_FMASK	0x000ff000
> 
> static u32 register = 0x12345678;
> 
> int foo(u32 value)
> {
> 	if (value > field_max(FOO_FMASK))
> 		return -EINVAL;
> 
> 	u32_replace_bits(&register, value, FOO_FMASK);
> 	
> 	return 0;
> }
> 


  parent reply	other threads:[~2020-02-29 13:11 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-28 16:53 Alex Elder
2020-02-28 17:56 ` Jakub Kicinski
2020-02-28 18:04   ` Alex Elder
2020-02-28 18:06     ` Alex Elder
2020-02-28 18:33       ` Jakub Kicinski
2020-02-28 18:37         ` Alex Elder
2020-02-28 19:01           ` Jakub Kicinski
2020-02-29 13:10     ` Alex Elder [this message]
2020-02-29 20:17       ` Jakub Kicinski

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=be229ebb-53a5-e048-9c68-1b4c7cc2ab9d@linaro.org \
    --to=elder@linaro.org \
    --cc=arnd@arndb.de \
    --cc=bjorn.andersson@linaro.org \
    --cc=johannes@sipsolutions.net \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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

all inboxes | Powered by JetHome®