Re: Converting struct timer_list callback argument to struct timer_list *

From: Kees Cook
Date: Fri Sep 01 2017 - 12:24:28 EST


On Fri, Sep 1, 2017 at 3:44 AM, Christoph Hellwig <hch@xxxxxxxxxxxxx> wrote:
> On Fri, Sep 01, 2017 at 12:21:46PM +0200, Thomas Gleixner wrote:
>> On Fri, 1 Sep 2017, Christoph Hellwig wrote:
>>
>> > Good work!
>> >
>> > I just think that the TIMER_CONTAINER name is revolting.
>> >
>> > The usual name for such a helper fitting other uses like lists
>> > and rbtrees would be timer_entry, and that also reads much better.
>>
>> I think the plan is to remove that thing afterward, because then the
>> callback function is:
>>
>> void func(struct timer_list *timer)
>>
>> So I don't mind the ugly name as it should be simply removed once the tree
>> is converted over.

The removed casts would be the (TIMER_FUNC_TYPE) and (TIMER_DATA_TYPE)
casts. TIMER_CONTAINER is ugly, yes. I can rework that.

> Well, we can't just remove it, we could just replace it with
> container_of(). lists and rbtrees just keep their list_entry and
> rb_entry wrappers for timer_of, so we could save us the additional
> churn by naming it timer_entry and just keeping it.

These examples are:

#define list_entry(ptr, type, member) container_of(ptr, type, member)
#define rb_entry(ptr, type, member) container_of(ptr, type, member)

The use of a "timer_entry()" at the start of callbacks repeats the
struct name, which I find irritating (and it usually results in split
lines). For example:

#define timer_entry(ptr, type, member) container_of(ptr, type, member)

-static void snd_card_asihpi_timer_function(unsigned long data)
+static void snd_card_asihpi_timer_function(struct timer_list *t)
{
- struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
+ struct snd_card_asihpi_pcm *dpcm =
+ timer_entry(t, struct
snd_card_asihpi_pcm, timer);

I prefer to tie this directly to the variable, so how about renaming
TIMER_CONTAINER to timer_of():

#define timer_of(var, callback_timer, timer_fieldname) \
container_of(callback_timer, typeof(*var), timer_fieldname)

-static void snd_card_asihpi_timer_function(unsigned long data)
+static void snd_card_asihpi_timer_function(struct timer_list *t)
{
- struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
+ struct snd_card_asihpi_pcm *dpcm = timer_of(dpcm, t, timer);

If not, I can just go the timer_entry() way, it just means I have to
split a lot of lines.

-Kees

--
Kees Cook
Pixel Security