diff mbox series

[v2] KVM: leverage change to adjust slots->used_slots in update_memslots()

Message ID 20180822135711.7389-1-richard.weiyang@gmail.com (mailing list archive)
State New, archived
Headers show
Series [v2] KVM: leverage change to adjust slots->used_slots in update_memslots() | expand

Commit Message

Wei Yang Aug. 22, 2018, 1:57 p.m. UTC
update_memslots() is only called by __kvm_set_memory_region(), in which
"change" is calculated and indicates how to adjust slots->used_slots

  * increase by one if it is KVM_MR_CREATE
  * decrease by one if it is KVM_MR_DELETE
  * not change for others

This patch adjusts slots->used_slots in update_memslots() based on "change"
value instead of re-calculate those states again.

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

---
v2: use switch on change instead of encode the adjustment in it.
    add warning in case the status is not correct

---
 virt/kvm/kvm_main.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

Comments

Wei Yang Sept. 4, 2018, 3:26 p.m. UTC | #1
Any comment on this one?

On Wed, Aug 22, 2018 at 09:57:11PM +0800, Wei Yang wrote:
>update_memslots() is only called by __kvm_set_memory_region(), in which
>"change" is calculated and indicates how to adjust slots->used_slots
>
>  * increase by one if it is KVM_MR_CREATE
>  * decrease by one if it is KVM_MR_DELETE
>  * not change for others
>
>This patch adjusts slots->used_slots in update_memslots() based on "change"
>value instead of re-calculate those states again.
>
>Signed-off-by: Wei Yang <richard.weiyang@gmail.com>
>
>---
>v2: use switch on change instead of encode the adjustment in it.
>    add warning in case the status is not correct
>
>---
> virt/kvm/kvm_main.c | 23 ++++++++++++++---------
> 1 file changed, 14 insertions(+), 9 deletions(-)
>
>diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
>index 0df592c4f09f..692885c1457f 100644
>--- a/virt/kvm/kvm_main.c
>+++ b/virt/kvm/kvm_main.c
>@@ -802,20 +802,25 @@ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
>  * sorted array and known changed memslot position.
>  */
> static void update_memslots(struct kvm_memslots *slots,
>-			    struct kvm_memory_slot *new)
>+			    struct kvm_memory_slot *new,
>+			    enum kvm_mr_change change)
> {
> 	int id = new->id;
> 	int i = slots->id_to_index[id];
> 	struct kvm_memory_slot *mslots = slots->memslots;
> 
> 	WARN_ON(mslots[i].id != id);
>-	if (!new->npages) {
>-		WARN_ON(!mslots[i].npages);
>-		if (mslots[i].npages)
>-			slots->used_slots--;
>-	} else {
>-		if (!mslots[i].npages)
>-			slots->used_slots++;
>+	switch (change) {
>+	case KVM_MR_CREATE:
>+		slots->used_slots++;
>+		WARN_ON(mslots[i].npages || !new->npages);
>+		break;
>+	case KVM_MR_DELETE:
>+		slots->used_slots--;
>+		WARN_ON(new->npages || !mslots[i].npages);
>+		break;
>+	default:
>+		break;
> 	}
> 
> 	while (i < KVM_MEM_SLOTS_NUM - 1 &&
>@@ -1051,7 +1056,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
> 		memset(&new.arch, 0, sizeof(new.arch));
> 	}
> 
>-	update_memslots(slots, &new);
>+	update_memslots(slots, &new, change);
> 	old_memslots = install_new_memslots(kvm, as_id, slots);
> 
> 	kvm_arch_commit_memory_region(kvm, mem, &old, &new, change);
>-- 
>2.15.1
Wei Yang Sept. 16, 2018, 1:24 a.m. UTC | #2
Hi, Paolo

Do you like this one?

On Tue, Sep 04, 2018 at 11:26:03PM +0800, Wei Yang wrote:
>Any comment on this one?
>
>On Wed, Aug 22, 2018 at 09:57:11PM +0800, Wei Yang wrote:
>>update_memslots() is only called by __kvm_set_memory_region(), in which
>>"change" is calculated and indicates how to adjust slots->used_slots
>>
>>  * increase by one if it is KVM_MR_CREATE
>>  * decrease by one if it is KVM_MR_DELETE
>>  * not change for others
>>
>>This patch adjusts slots->used_slots in update_memslots() based on "change"
>>value instead of re-calculate those states again.
>>
>>Signed-off-by: Wei Yang <richard.weiyang@gmail.com>
>>
>>---
>>v2: use switch on change instead of encode the adjustment in it.
>>    add warning in case the status is not correct
>>
>>---
>> virt/kvm/kvm_main.c | 23 ++++++++++++++---------
>> 1 file changed, 14 insertions(+), 9 deletions(-)
>>
>>diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
>>index 0df592c4f09f..692885c1457f 100644
>>--- a/virt/kvm/kvm_main.c
>>+++ b/virt/kvm/kvm_main.c
>>@@ -802,20 +802,25 @@ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
>>  * sorted array and known changed memslot position.
>>  */
>> static void update_memslots(struct kvm_memslots *slots,
>>-			    struct kvm_memory_slot *new)
>>+			    struct kvm_memory_slot *new,
>>+			    enum kvm_mr_change change)
>> {
>> 	int id = new->id;
>> 	int i = slots->id_to_index[id];
>> 	struct kvm_memory_slot *mslots = slots->memslots;
>> 
>> 	WARN_ON(mslots[i].id != id);
>>-	if (!new->npages) {
>>-		WARN_ON(!mslots[i].npages);
>>-		if (mslots[i].npages)
>>-			slots->used_slots--;
>>-	} else {
>>-		if (!mslots[i].npages)
>>-			slots->used_slots++;
>>+	switch (change) {
>>+	case KVM_MR_CREATE:
>>+		slots->used_slots++;
>>+		WARN_ON(mslots[i].npages || !new->npages);
>>+		break;
>>+	case KVM_MR_DELETE:
>>+		slots->used_slots--;
>>+		WARN_ON(new->npages || !mslots[i].npages);
>>+		break;
>>+	default:
>>+		break;
>> 	}
>> 
>> 	while (i < KVM_MEM_SLOTS_NUM - 1 &&
>>@@ -1051,7 +1056,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
>> 		memset(&new.arch, 0, sizeof(new.arch));
>> 	}
>> 
>>-	update_memslots(slots, &new);
>>+	update_memslots(slots, &new, change);
>> 	old_memslots = install_new_memslots(kvm, as_id, slots);
>> 
>> 	kvm_arch_commit_memory_region(kvm, mem, &old, &new, change);
>>-- 
>>2.15.1
>
>-- 
>Wei Yang
>Help you, Help me
Paolo Bonzini Oct. 3, 2018, 4:07 p.m. UTC | #3
On 22/08/2018 15:57, Wei Yang wrote:
> update_memslots() is only called by __kvm_set_memory_region(), in which
> "change" is calculated and indicates how to adjust slots->used_slots
> 
>   * increase by one if it is KVM_MR_CREATE
>   * decrease by one if it is KVM_MR_DELETE
>   * not change for others
> 
> This patch adjusts slots->used_slots in update_memslots() based on "change"
> value instead of re-calculate those states again.
> 
> Signed-off-by: Wei Yang <richard.weiyang@gmail.com>
> 
> ---
> v2: use switch on change instead of encode the adjustment in it.
>     add warning in case the status is not correct
> 
> ---
>  virt/kvm/kvm_main.c | 23 ++++++++++++++---------
>  1 file changed, 14 insertions(+), 9 deletions(-)
> 
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 0df592c4f09f..692885c1457f 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -802,20 +802,25 @@ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
>   * sorted array and known changed memslot position.
>   */
>  static void update_memslots(struct kvm_memslots *slots,
> -			    struct kvm_memory_slot *new)
> +			    struct kvm_memory_slot *new,
> +			    enum kvm_mr_change change)
>  {
>  	int id = new->id;
>  	int i = slots->id_to_index[id];
>  	struct kvm_memory_slot *mslots = slots->memslots;
>  
>  	WARN_ON(mslots[i].id != id);
> -	if (!new->npages) {
> -		WARN_ON(!mslots[i].npages);
> -		if (mslots[i].npages)
> -			slots->used_slots--;
> -	} else {
> -		if (!mslots[i].npages)
> -			slots->used_slots++;
> +	switch (change) {
> +	case KVM_MR_CREATE:
> +		slots->used_slots++;
> +		WARN_ON(mslots[i].npages || !new->npages);
> +		break;
> +	case KVM_MR_DELETE:
> +		slots->used_slots--;
> +		WARN_ON(new->npages || !mslots[i].npages);
> +		break;
> +	default:
> +		break;
>  	}
>  
>  	while (i < KVM_MEM_SLOTS_NUM - 1 &&
> @@ -1051,7 +1056,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
>  		memset(&new.arch, 0, sizeof(new.arch));
>  	}
>  
> -	update_memslots(slots, &new);
> +	update_memslots(slots, &new, change);
>  	old_memslots = install_new_memslots(kvm, as_id, slots);
>  
>  	kvm_arch_commit_memory_region(kvm, mem, &old, &new, change);
> 

Queued, thanks.

Paolo
diff mbox series

Patch

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 0df592c4f09f..692885c1457f 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -802,20 +802,25 @@  static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
  * sorted array and known changed memslot position.
  */
 static void update_memslots(struct kvm_memslots *slots,
-			    struct kvm_memory_slot *new)
+			    struct kvm_memory_slot *new,
+			    enum kvm_mr_change change)
 {
 	int id = new->id;
 	int i = slots->id_to_index[id];
 	struct kvm_memory_slot *mslots = slots->memslots;
 
 	WARN_ON(mslots[i].id != id);
-	if (!new->npages) {
-		WARN_ON(!mslots[i].npages);
-		if (mslots[i].npages)
-			slots->used_slots--;
-	} else {
-		if (!mslots[i].npages)
-			slots->used_slots++;
+	switch (change) {
+	case KVM_MR_CREATE:
+		slots->used_slots++;
+		WARN_ON(mslots[i].npages || !new->npages);
+		break;
+	case KVM_MR_DELETE:
+		slots->used_slots--;
+		WARN_ON(new->npages || !mslots[i].npages);
+		break;
+	default:
+		break;
 	}
 
 	while (i < KVM_MEM_SLOTS_NUM - 1 &&
@@ -1051,7 +1056,7 @@  int __kvm_set_memory_region(struct kvm *kvm,
 		memset(&new.arch, 0, sizeof(new.arch));
 	}
 
-	update_memslots(slots, &new);
+	update_memslots(slots, &new, change);
 	old_memslots = install_new_memslots(kvm, as_id, slots);
 
 	kvm_arch_commit_memory_region(kvm, mem, &old, &new, change);