2015-06-01 09:33:26

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH 12/15] KVM: MTRR: introduce mtrr_for_each_mem_type



On 30/05/2015 12:59, Xiao Guangrong wrote:
> +struct mtrr_looker {
> + /* input fields. */
> + struct kvm_mtrr *mtrr_state;
> + u64 start;
> + u64 end;

s/looker/iter/ or s/looker/lookup/

> +static void mtrr_lookup_start(struct mtrr_looker *looker)
> +{
> + looker->mem_type = -1;
> +
> + if (!looker->mtrr_state->mtrr_enabled) {
> + looker->partial_map = true;
> + return;
> + }
> +
> + if (!mtrr_lookup_fixed_start(looker))
> + mtrr_lookup_var_start(looker);
> +}
> +

Separating mtrr_lookup_start and mtrr_lookup_init is weird.

There are common parts of mtrr_lookup_*_start and mtrr_lookup_*_next.
For example this:

> + looker->mem_type = looker->mtrr_state->fixed_ranges[index];
> + looker->start = fixed_mtrr_range_end_addr(seg, index);
> + return true;

in mtrr_lookup_fixed_start is the same as this:

>
> + end = fixed_mtrr_range_end_addr(looker->seg, looker->index);
> +
> + /* switch to next segment. */
> + if (end >= eseg->end) {
> + looker->seg++;
> + looker->index = 0;
> +
> + /* have looked up for all fixed MTRRs. */
> + if (looker->seg >= ARRAY_SIZE(fixed_seg_table))
> + return mtrr_lookup_var_start(looker);
> +
> + end = fixed_mtrr_range_end_addr(looker->seg, looker->index);
> + }
> +
> + looker->mem_type = mtrr_state->fixed_ranges[looker->index];
> + looker->start = end;

in mtrr_lookup_fixed_next. Can you try to make them more common?

Basically you should have

+#define mtrr_for_each_mem_type(_looker_, _mtrr_, _gpa_start_, _gpa_end_) \
+ for (mtrr_lookup_init(_looker_, _mtrr_, _gpa_start_, _gpa_end_); \
+ !mtrr_lookup_end(_looker_); mtrr_lookup_next(_looker_))

Paolo


2015-06-01 14:26:29

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH 12/15] KVM: MTRR: introduce mtrr_for_each_mem_type



On 01/06/2015 11:33, Paolo Bonzini wrote:
>> + looker->mem_type = looker->mtrr_state->fixed_ranges[index];
>> > + looker->start = fixed_mtrr_range_end_addr(seg, index);
>> > + return true;
> in mtrr_lookup_fixed_start is the same as this:
>
>> >
>> > + end = fixed_mtrr_range_end_addr(looker->seg, looker->index);
>> > +
>> > + /* switch to next segment. */
>> > + if (end >= eseg->end) {
>> > + looker->seg++;
>> > + looker->index = 0;
>> > +
>> > + /* have looked up for all fixed MTRRs. */
>> > + if (looker->seg >= ARRAY_SIZE(fixed_seg_table))
>> > + return mtrr_lookup_var_start(looker);
>> > +
>> > + end = fixed_mtrr_range_end_addr(looker->seg, looker->index);
>> > + }
>> > +
>> > + looker->mem_type = mtrr_state->fixed_ranges[looker->index];
>> > + looker->start = end;
> in mtrr_lookup_fixed_next. Can you try to make them more common?
>
> Basically you should have
>
> +#define mtrr_for_each_mem_type(_looker_, _mtrr_, _gpa_start_, _gpa_end_) \
> + for (mtrr_lookup_init(_looker_, _mtrr_, _gpa_start_, _gpa_end_); \
> + !mtrr_lookup_end(_looker_); mtrr_lookup_next(_looker_))

... where the above code I quoted would be part of mtrr_lookup_end.

Paolo

2015-06-03 02:44:45

by Xiao Guangrong

[permalink] [raw]
Subject: Re: [PATCH 12/15] KVM: MTRR: introduce mtrr_for_each_mem_type



On 06/01/2015 05:33 PM, Paolo Bonzini wrote:
>
>
> On 30/05/2015 12:59, Xiao Guangrong wrote:
>> +struct mtrr_looker {
>> + /* input fields. */
>> + struct kvm_mtrr *mtrr_state;
>> + u64 start;
>> + u64 end;
>
> s/looker/iter/ or s/looker/lookup/

Good to me.

>
>> +static void mtrr_lookup_start(struct mtrr_looker *looker)
>> +{
>> + looker->mem_type = -1;
>> +
>> + if (!looker->mtrr_state->mtrr_enabled) {
>> + looker->partial_map = true;
>> + return;
>> + }
>> +
>> + if (!mtrr_lookup_fixed_start(looker))
>> + mtrr_lookup_var_start(looker);
>> +}
>> +
>
> Separating mtrr_lookup_start and mtrr_lookup_init is weird.

Agreed, will fold mtrr_lookup_start() into mtrr_lookup_init().

>
> There are common parts of mtrr_lookup_*_start and mtrr_lookup_*_next.
> For example this:
>
>> + looker->mem_type = looker->mtrr_state->fixed_ranges[index];
>> + looker->start = fixed_mtrr_range_end_addr(seg, index);
>> + return true;
>
> in mtrr_lookup_fixed_start is the same as this:
>
>>
>> + end = fixed_mtrr_range_end_addr(looker->seg, looker->index);
>> +
>> + /* switch to next segment. */
>> + if (end >= eseg->end) {
>> + looker->seg++;
>> + looker->index = 0;
>> +
>> + /* have looked up for all fixed MTRRs. */
>> + if (looker->seg >= ARRAY_SIZE(fixed_seg_table))
>> + return mtrr_lookup_var_start(looker);
>> +
>> + end = fixed_mtrr_range_end_addr(looker->seg, looker->index);
>> + }
>> +
>> + looker->mem_type = mtrr_state->fixed_ranges[looker->index];
>> + looker->start = end;
>
> in mtrr_lookup_fixed_next. Can you try to make them more common?

Yes, i will check them carefully and introduce common functions to do
it.

>
> Basically you should have
>
> +#define mtrr_for_each_mem_type(_looker_, _mtrr_, _gpa_start_, _gpa_end_) \
> + for (mtrr_lookup_init(_looker_, _mtrr_, _gpa_start_, _gpa_end_); \
> + !mtrr_lookup_end(_looker_); mtrr_lookup_next(_looker_))

Good idea, will do it in v2.

2015-06-03 03:01:35

by Xiao Guangrong

[permalink] [raw]
Subject: Re: [PATCH 12/15] KVM: MTRR: introduce mtrr_for_each_mem_type



On 06/01/2015 10:26 PM, Paolo Bonzini wrote:
>
>
> On 01/06/2015 11:33, Paolo Bonzini wrote:
>>> + looker->mem_type = looker->mtrr_state->fixed_ranges[index];
>>>> + looker->start = fixed_mtrr_range_end_addr(seg, index);
>>>> + return true;
>> in mtrr_lookup_fixed_start is the same as this:
>>
>>>>
>>>> + end = fixed_mtrr_range_end_addr(looker->seg, looker->index);
>>>> +
>>>> + /* switch to next segment. */
>>>> + if (end >= eseg->end) {
>>>> + looker->seg++;
>>>> + looker->index = 0;
>>>> +
>>>> + /* have looked up for all fixed MTRRs. */
>>>> + if (looker->seg >= ARRAY_SIZE(fixed_seg_table))
>>>> + return mtrr_lookup_var_start(looker);
>>>> +
>>>> + end = fixed_mtrr_range_end_addr(looker->seg, looker->index);
>>>> + }
>>>> +
>>>> + looker->mem_type = mtrr_state->fixed_ranges[looker->index];
>>>> + looker->start = end;
>> in mtrr_lookup_fixed_next. Can you try to make them more common?
>>
>> Basically you should have
>>
>> +#define mtrr_for_each_mem_type(_looker_, _mtrr_, _gpa_start_, _gpa_end_) \
>> + for (mtrr_lookup_init(_looker_, _mtrr_, _gpa_start_, _gpa_end_); \
>> + !mtrr_lookup_end(_looker_); mtrr_lookup_next(_looker_))
>
> ... where the above code I quoted would be part of mtrr_lookup_end.

Okay, will do. :)