Allows userspace to query the actual limit and set a new limit for
 the maximum guest memory size. The limit will be rounded up to
 2048 MB, 4096 GB, 8192 TB respectively, as this limit is governed by
-the number of page table levels.
+the number of page table levels. In the case that there is no limit we will set
+the limit to KVM_S390_NO_MEM_LIMIT (U64_MAX).
 
 2. GROUP: KVM_S390_VM_CPU_MODEL
 Architectures: s390
 
        case KVM_S390_VM_MEM_LIMIT_SIZE:
                ret = 0;
                VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
-                        kvm->arch.gmap->asce_end);
-               if (put_user(kvm->arch.gmap->asce_end, (u64 __user *)attr->addr))
+                        kvm->arch.mem_limit);
+               if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
                        ret = -EFAULT;
                break;
        default:
                if (get_user(new_limit, (u64 __user *)attr->addr))
                        return -EFAULT;
 
-               if (new_limit > kvm->arch.gmap->asce_end)
+               if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
+                   new_limit > kvm->arch.mem_limit)
                        return -E2BIG;
 
+               if (!new_limit)
+                       return -EINVAL;
+
+               /* gmap_alloc takes last usable address */
+               if (new_limit != KVM_S390_NO_MEM_LIMIT)
+                       new_limit -= 1;
+
                ret = -EBUSY;
                mutex_lock(&kvm->lock);
                if (atomic_read(&kvm->online_vcpus) == 0) {
                        }
                }
                mutex_unlock(&kvm->lock);
-               VM_EVENT(kvm, 3, "SET: max guest memory: %lu bytes", new_limit);
+               VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
+               VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
+                        (void *) kvm->arch.gmap->asce);
                break;
        }
        default:
 
        if (type & KVM_VM_S390_UCONTROL) {
                kvm->arch.gmap = NULL;
+               kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
        } else {
-               kvm->arch.gmap = gmap_alloc(current->mm, (1UL << 44) - 1);
+               kvm->arch.mem_limit = TASK_MAX_SIZE;
+               kvm->arch.gmap = gmap_alloc(current->mm, kvm->arch.mem_limit - 1);
                if (!kvm->arch.gmap)
                        goto out_err;
                kvm->arch.gmap->private = kvm;
        if (mem->memory_size & 0xffffful)
                return -EINVAL;
 
+       if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
+               return -EINVAL;
+
        return 0;
 }
 
 
 /**
  * gmap_alloc - allocate a guest address space
  * @mm: pointer to the parent mm_struct
- * @limit: maximum size of the gmap address space
+ * @limit: maximum address of the gmap address space
  *
  * Returns a guest address space structure.
  */
        if ((from | to | len) & (PMD_SIZE - 1))
                return -EINVAL;
        if (len == 0 || from + len < from || to + len < to ||
-           from + len > TASK_MAX_SIZE || to + len > gmap->asce_end)
+           from + len - 1 > TASK_MAX_SIZE || to + len - 1 > gmap->asce_end)
                return -EINVAL;
 
        flush = 0;