diff mbox series

[v1,06/14] s390x: introduce s390_get_memory_limit()

Message ID 20240910175809.2135596-7-david@redhat.com (mailing list archive)
State New
Headers show
Series s390x: virtio-mem support | expand

Commit Message

David Hildenbrand Sept. 10, 2024, 5:58 p.m. UTC
Let's add s390_get_memory_limit(), to query what has been successfully
set via s390_set_memory_limit(). Allow setting the limit only once.

Signed-off-by: David Hildenbrand <david@redhat.com>
---
 target/s390x/cpu-sysemu.c | 19 +++++++++++++++++--
 target/s390x/cpu.h        |  1 +
 2 files changed, 18 insertions(+), 2 deletions(-)

Comments

Thomas Huth Sept. 12, 2024, 8:10 a.m. UTC | #1
On 10/09/2024 19.58, David Hildenbrand wrote:
> Let's add s390_get_memory_limit(), to query what has been successfully
> set via s390_set_memory_limit(). Allow setting the limit only once.
> 
> Signed-off-by: David Hildenbrand <david@redhat.com>
> ---
>   target/s390x/cpu-sysemu.c | 19 +++++++++++++++++--
>   target/s390x/cpu.h        |  1 +
>   2 files changed, 18 insertions(+), 2 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>
Nina Schoetterl-Glausch Sept. 16, 2024, 1:20 p.m. UTC | #2
On Tue, 2024-09-10 at 19:58 +0200, David Hildenbrand wrote:
> Let's add s390_get_memory_limit(), to query what has been successfully
> set via s390_set_memory_limit(). Allow setting the limit only once.
> 
> Signed-off-by: David Hildenbrand <david@redhat.com>

Reviewed-by: Nina Schoetterl-Glausch <nsg@linux.ibm.com>

Comment below.
> ---
>  target/s390x/cpu-sysemu.c | 19 +++++++++++++++++--
>  target/s390x/cpu.h        |  1 +
>  2 files changed, 18 insertions(+), 2 deletions(-)
> 
> diff --git a/target/s390x/cpu-sysemu.c b/target/s390x/cpu-sysemu.c
> index 1cd30c1d84..1915567b3a 100644
> --- a/target/s390x/cpu-sysemu.c
> +++ b/target/s390x/cpu-sysemu.c
> @@ -255,12 +255,27 @@ unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
>      return s390_count_running_cpus();
>  }
>  
> +static uint64_t memory_limit;
> +
>  int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
>  {
> +    int ret = 0;
> +
> +    if (memory_limit) {
> +        return -EBUSY;
> +    }
>      if (kvm_enabled()) {
> -        return kvm_s390_set_mem_limit(new_limit, hw_limit);
> +        ret = kvm_s390_set_mem_limit(new_limit, hw_limit);
> +    }
> +    if (!ret) {
> +        memory_limit = new_limit;
>      }
> -    return 0;
> +    return ret;
> +}
> +
> +uint64_t s390_get_memory_limit(void)
> +{

Might be nice to guard/warn against s390_set_memory_limit not having been called before.

> +    return memory_limit;
>  }
>  
>  void s390_set_max_pagesize(uint64_t pagesize, Error **errp)
> diff --git a/target/s390x/cpu.h b/target/s390x/cpu.h
> index d6b75ad0e0..7a51b606ed 100644
> --- a/target/s390x/cpu.h
> +++ b/target/s390x/cpu.h
> @@ -895,6 +895,7 @@ static inline void s390_do_cpu_load_normal(CPUState *cs, run_on_cpu_data arg)
>  /* cpu.c */
>  void s390_crypto_reset(void);
>  int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit);
> +uint64_t s390_get_memory_limit(void);
>  void s390_set_max_pagesize(uint64_t pagesize, Error **errp);
>  void s390_cmma_reset(void);
>  void s390_enable_css_support(S390CPU *cpu);
David Hildenbrand Sept. 17, 2024, 11:23 a.m. UTC | #3
On 16.09.24 15:20, Nina Schoetterl-Glausch wrote:
> On Tue, 2024-09-10 at 19:58 +0200, David Hildenbrand wrote:
>> Let's add s390_get_memory_limit(), to query what has been successfully
>> set via s390_set_memory_limit(). Allow setting the limit only once.
>>
>> Signed-off-by: David Hildenbrand <david@redhat.com>
> 
> Reviewed-by: Nina Schoetterl-Glausch <nsg@linux.ibm.com>
> 
> Comment below.
>> ---
>>   target/s390x/cpu-sysemu.c | 19 +++++++++++++++++--
>>   target/s390x/cpu.h        |  1 +
>>   2 files changed, 18 insertions(+), 2 deletions(-)
>>
>> diff --git a/target/s390x/cpu-sysemu.c b/target/s390x/cpu-sysemu.c
>> index 1cd30c1d84..1915567b3a 100644
>> --- a/target/s390x/cpu-sysemu.c
>> +++ b/target/s390x/cpu-sysemu.c
>> @@ -255,12 +255,27 @@ unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
>>       return s390_count_running_cpus();
>>   }
>>   
>> +static uint64_t memory_limit;
>> +
>>   int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
>>   {
>> +    int ret = 0;
>> +
>> +    if (memory_limit) {
>> +        return -EBUSY;
>> +    }
>>       if (kvm_enabled()) {
>> -        return kvm_s390_set_mem_limit(new_limit, hw_limit);
>> +        ret = kvm_s390_set_mem_limit(new_limit, hw_limit);
>> +    }
>> +    if (!ret) {
>> +        memory_limit = new_limit;
>>       }
>> -    return 0;
>> +    return ret;
>> +}
>> +
>> +uint64_t s390_get_memory_limit(void)
>> +{
> 
> Might be nice to guard/warn against s390_set_memory_limit not having been called before.
> 

What about the following on top:

diff --git a/target/s390x/cpu-sysemu.c b/target/s390x/cpu-sysemu.c
index 1915567b3a..07cb85103a 100644
--- a/target/s390x/cpu-sysemu.c
+++ b/target/s390x/cpu-sysemu.c
@@ -263,6 +263,8 @@ int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
  
      if (memory_limit) {
          return -EBUSY;
+    } else if (!new_limit) {
+        return -EINVAL;
      }
      if (kvm_enabled()) {
          ret = kvm_s390_set_mem_limit(new_limit, hw_limit);
@@ -275,6 +277,8 @@ int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
  
  uint64_t s390_get_memory_limit(void)
  {
+    /* We expect to be called after s390_set_memory_limit(). */
+    assert(memory_limit);
      return memory_limit;
  }
Nina Schoetterl-Glausch Sept. 17, 2024, 12:48 p.m. UTC | #4
On Tue, 2024-09-17 at 13:23 +0200, David Hildenbrand wrote:
> On 16.09.24 15:20, Nina Schoetterl-Glausch wrote:
> > On Tue, 2024-09-10 at 19:58 +0200, David Hildenbrand wrote:
> > > Let's add s390_get_memory_limit(), to query what has been successfully
> > > set via s390_set_memory_limit(). Allow setting the limit only once.
> > > 
> > > Signed-off-by: David Hildenbrand <david@redhat.com>
> > 
> > Reviewed-by: Nina Schoetterl-Glausch <nsg@linux.ibm.com>
> > 
> > Comment below.
> > > ---
> > >   target/s390x/cpu-sysemu.c | 19 +++++++++++++++++--
> > >   target/s390x/cpu.h        |  1 +
> > >   2 files changed, 18 insertions(+), 2 deletions(-)

[...]

> > > +
> > > +uint64_t s390_get_memory_limit(void)
> > > +{
> > 
> > Might be nice to guard/warn against s390_set_memory_limit not having been called before.
> > 
> 
> What about the following on top:
> 
> diff --git a/target/s390x/cpu-sysemu.c b/target/s390x/cpu-sysemu.c
> index 1915567b3a..07cb85103a 100644
> --- a/target/s390x/cpu-sysemu.c
> +++ b/target/s390x/cpu-sysemu.c
> @@ -263,6 +263,8 @@ int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
>   
>       if (memory_limit) {
>           return -EBUSY;
> +    } else if (!new_limit) {
> +        return -EINVAL;
>       }
>       if (kvm_enabled()) {
>           ret = kvm_s390_set_mem_limit(new_limit, hw_limit);
> @@ -275,6 +277,8 @@ int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
>   
>   uint64_t s390_get_memory_limit(void)
>   {
> +    /* We expect to be called after s390_set_memory_limit(). */
> +    assert(memory_limit);
>       return memory_limit;
>   }

Looks good.

Looking at the patch again I'm wondering if using globals in qemu is still encouraged.
I know it's a common pattern today, but seeing efforts like the multiarch binary or Unicorn
I'm wondering if there is aspirations to do things more "cleanly", in general, for far out benefits?
I.e. memory_limit could be a machine property instead.
diff mbox series

Patch

diff --git a/target/s390x/cpu-sysemu.c b/target/s390x/cpu-sysemu.c
index 1cd30c1d84..1915567b3a 100644
--- a/target/s390x/cpu-sysemu.c
+++ b/target/s390x/cpu-sysemu.c
@@ -255,12 +255,27 @@  unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
     return s390_count_running_cpus();
 }
 
+static uint64_t memory_limit;
+
 int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
 {
+    int ret = 0;
+
+    if (memory_limit) {
+        return -EBUSY;
+    }
     if (kvm_enabled()) {
-        return kvm_s390_set_mem_limit(new_limit, hw_limit);
+        ret = kvm_s390_set_mem_limit(new_limit, hw_limit);
+    }
+    if (!ret) {
+        memory_limit = new_limit;
     }
-    return 0;
+    return ret;
+}
+
+uint64_t s390_get_memory_limit(void)
+{
+    return memory_limit;
 }
 
 void s390_set_max_pagesize(uint64_t pagesize, Error **errp)
diff --git a/target/s390x/cpu.h b/target/s390x/cpu.h
index d6b75ad0e0..7a51b606ed 100644
--- a/target/s390x/cpu.h
+++ b/target/s390x/cpu.h
@@ -895,6 +895,7 @@  static inline void s390_do_cpu_load_normal(CPUState *cs, run_on_cpu_data arg)
 /* cpu.c */
 void s390_crypto_reset(void);
 int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit);
+uint64_t s390_get_memory_limit(void);
 void s390_set_max_pagesize(uint64_t pagesize, Error **errp);
 void s390_cmma_reset(void);
 void s390_enable_css_support(S390CPU *cpu);