powerpc/pseries/vas: Close windows with DLPAR core removal
authorHaren Myneni <haren@linux.ibm.com>
Tue, 1 Mar 2022 01:14:28 +0000 (17:14 -0800)
committerMichael Ellerman <mpe@ellerman.id.au>
Mon, 7 Mar 2022 13:04:55 +0000 (00:04 +1100)
The hypervisor assigns vas credits (windows) for each LPAR based
on the number of cores configured in that system. The OS is
expected to release credits when cores are removed, and may
allocate more when cores are added. So there is a possibility of
using excessive credits (windows) in the LPAR and the hypervisor
expects the system to close the excessive windows so that NX load
can be equally distributed across all LPARs in the system.

When the OS closes the excessive windows in the hypervisor,
it sets the window status inactive and invalidates window
virtual address mapping. The user space receives paste instruction
failure if any NX requests are issued on the inactive window.
Then the user space can use with the available open windows or
retry NX requests until this window active again.

This patch also adds the notifier for core removal/add to close
windows in the hypervisor if the system lost credits (core
removal) and reopen windows in the hypervisor when the previously
lost credits are available.

Signed-off-by: Haren Myneni <haren@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/108928f9c00a48cc6a722315d482d07cf66acf5a.camel@linux.ibm.com
arch/powerpc/include/asm/vas.h
arch/powerpc/platforms/pseries/vas.c
arch/powerpc/platforms/pseries/vas.h

index 27251af..6baf7b9 100644 (file)
@@ -34,6 +34,8 @@
  */
 #define VAS_WIN_ACTIVE         0x0     /* Used in platform independent */
                                        /* vas mmap() */
+/* Window is closed in the hypervisor due to lost credit */
+#define VAS_WIN_NO_CRED_CLOSE  0x00000001
 
 /*
  * Get/Set bit fields
index 1035446..a297720 100644 (file)
@@ -370,13 +370,28 @@ static struct vas_window *vas_allocate_window(int vas_id, u64 flags,
        if (rc)
                goto out_free;
 
-       vas_user_win_add_mm_context(&txwin->vas_win.task_ref);
        txwin->win_type = cop_feat_caps->win_type;
        mutex_lock(&vas_pseries_mutex);
-       list_add(&txwin->win_list, &caps->list);
+       /*
+        * Possible to lose the acquired credit with DLPAR core
+        * removal after the window is opened. So if there are any
+        * closed windows (means with lost credits), do not give new
+        * window to user space. New windows will be opened only
+        * after the existing windows are reopened when credits are
+        * available.
+        */
+       if (!caps->nr_close_wins) {
+               list_add(&txwin->win_list, &caps->list);
+               caps->nr_open_windows++;
+               mutex_unlock(&vas_pseries_mutex);
+               vas_user_win_add_mm_context(&txwin->vas_win.task_ref);
+               return &txwin->vas_win;
+       }
        mutex_unlock(&vas_pseries_mutex);
 
-       return &txwin->vas_win;
+       put_vas_user_win_ref(&txwin->vas_win.task_ref);
+       rc = -EBUSY;
+       pr_err("No credit is available to allocate window\n");
 
 out_free:
        /*
@@ -439,14 +454,24 @@ static int vas_deallocate_window(struct vas_window *vwin)
 
        caps = &vascaps[win->win_type].caps;
        mutex_lock(&vas_pseries_mutex);
-       rc = deallocate_free_window(win);
-       if (rc) {
-               mutex_unlock(&vas_pseries_mutex);
-               return rc;
-       }
+       /*
+        * VAS window is already closed in the hypervisor when
+        * lost the credit. So just remove the entry from
+        * the list, remove task references and free vas_window
+        * struct.
+        */
+       if (win->vas_win.status & VAS_WIN_NO_CRED_CLOSE) {
+               rc = deallocate_free_window(win);
+               if (rc) {
+                       mutex_unlock(&vas_pseries_mutex);
+                       return rc;
+               }
+       } else
+               vascaps[win->win_type].nr_close_wins--;
 
        list_del(&win->win_list);
        atomic_dec(&caps->nr_used_credits);
+       vascaps[win->win_type].nr_open_windows--;
        mutex_unlock(&vas_pseries_mutex);
 
        put_vas_user_win_ref(&vwin->task_ref);
@@ -501,6 +526,7 @@ static int __init get_vas_capabilities(u8 feat, enum vas_cop_feat_type type,
        memset(vcaps, 0, sizeof(*vcaps));
        INIT_LIST_HEAD(&vcaps->list);
 
+       vcaps->feat = feat;
        caps = &vcaps->caps;
 
        rc = h_query_vas_capabilities(H_QUERY_VAS_CAPABILITIES, feat,
@@ -539,6 +565,168 @@ static int __init get_vas_capabilities(u8 feat, enum vas_cop_feat_type type,
        return 0;
 }
 
+/*
+ * The hypervisor reduces the available credits if the LPAR lost core. It
+ * means the excessive windows should not be active and the user space
+ * should not be using these windows to send compression requests to NX.
+ * So the kernel closes the excessive windows and unmap the paste address
+ * such that the user space receives paste instruction failure. Then up to
+ * the user space to fall back to SW compression and manage with the
+ * existing windows.
+ */
+static int reconfig_close_windows(struct vas_caps *vcap, int excess_creds)
+{
+       struct pseries_vas_window *win, *tmp;
+       struct vas_user_win_ref *task_ref;
+       struct vm_area_struct *vma;
+       int rc = 0;
+
+       list_for_each_entry_safe(win, tmp, &vcap->list, win_list) {
+               /*
+                * This window is already closed due to lost credit
+                * before. Go for next window.
+                */
+               if (win->vas_win.status & VAS_WIN_NO_CRED_CLOSE)
+                       continue;
+
+               task_ref = &win->vas_win.task_ref;
+               mutex_lock(&task_ref->mmap_mutex);
+               vma = task_ref->vma;
+               /*
+                * Number of available credits are reduced, So select
+                * and close windows.
+                */
+               win->vas_win.status |= VAS_WIN_NO_CRED_CLOSE;
+
+               mmap_write_lock(task_ref->mm);
+               /*
+                * vma is set in the original mapping. But this mapping
+                * is done with mmap() after the window is opened with ioctl.
+                * so we may not see the original mapping if the core remove
+                * is done before the original mmap() and after the ioctl.
+                */
+               if (vma)
+                       zap_page_range(vma, vma->vm_start,
+                                       vma->vm_end - vma->vm_start);
+
+               mmap_write_unlock(task_ref->mm);
+               mutex_unlock(&task_ref->mmap_mutex);
+               /*
+                * Close VAS window in the hypervisor, but do not
+                * free vas_window struct since it may be reused
+                * when the credit is available later (DLPAR with
+                * adding cores). This struct will be used
+                * later when the process issued with close(FD).
+                */
+               rc = deallocate_free_window(win);
+               if (rc)
+                       return rc;
+
+               vcap->nr_close_wins++;
+
+               if (!--excess_creds)
+                       break;
+       }
+
+       return 0;
+}
+
+/*
+ * Get new VAS capabilities when the core add/removal configuration
+ * changes. Reconfig window configurations based on the credits
+ * availability from this new capabilities.
+ */
+static int vas_reconfig_capabilties(u8 type)
+{
+       struct hv_vas_cop_feat_caps *hv_caps;
+       struct vas_cop_feat_caps *caps;
+       int old_nr_creds, new_nr_creds;
+       struct vas_caps *vcaps;
+       int rc = 0, nr_active_wins;
+
+       if (type >= VAS_MAX_FEAT_TYPE) {
+               pr_err("Invalid credit type %d\n", type);
+               return -EINVAL;
+       }
+
+       vcaps = &vascaps[type];
+       caps = &vcaps->caps;
+
+       hv_caps = kmalloc(sizeof(*hv_caps), GFP_KERNEL);
+       if (!hv_caps)
+               return -ENOMEM;
+
+       mutex_lock(&vas_pseries_mutex);
+       rc = h_query_vas_capabilities(H_QUERY_VAS_CAPABILITIES, vcaps->feat,
+                                     (u64)virt_to_phys(hv_caps));
+       if (rc)
+               goto out;
+
+       new_nr_creds = be16_to_cpu(hv_caps->target_lpar_creds);
+
+       old_nr_creds = atomic_read(&caps->nr_total_credits);
+
+       atomic_set(&caps->nr_total_credits, new_nr_creds);
+       /*
+        * The total number of available credits may be decreased or
+        * inceased with DLPAR operation. Means some windows have to be
+        * closed / reopened. Hold the vas_pseries_mutex so that the
+        * the user space can not open new windows.
+        */
+       if (old_nr_creds >  new_nr_creds) {
+               /*
+                * # active windows is more than new LPAR available
+                * credits. So close the excessive windows.
+                * On pseries, each window will have 1 credit.
+                */
+               nr_active_wins = vcaps->nr_open_windows - vcaps->nr_close_wins;
+               if (nr_active_wins > new_nr_creds)
+                       rc = reconfig_close_windows(vcaps,
+                                       nr_active_wins - new_nr_creds);
+       }
+
+out:
+       mutex_unlock(&vas_pseries_mutex);
+       kfree(hv_caps);
+       return rc;
+}
+/*
+ * Total number of default credits available (target_credits)
+ * in LPAR depends on number of cores configured. It varies based on
+ * whether processors are in shared mode or dedicated mode.
+ * Get the notifier when CPU configuration is changed with DLPAR
+ * operation so that get the new target_credits (vas default capabilities)
+ * and then update the existing windows usage if needed.
+ */
+static int pseries_vas_notifier(struct notifier_block *nb,
+                               unsigned long action, void *data)
+{
+       struct of_reconfig_data *rd = data;
+       struct device_node *dn = rd->dn;
+       const __be32 *intserv = NULL;
+       int len, rc = 0;
+
+       if ((action == OF_RECONFIG_ATTACH_NODE) ||
+               (action == OF_RECONFIG_DETACH_NODE))
+               intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s",
+                                         &len);
+       /*
+        * Processor config is not changed
+        */
+       if (!intserv)
+               return NOTIFY_OK;
+
+       rc = vas_reconfig_capabilties(VAS_GZIP_DEF_FEAT_TYPE);
+       if (rc)
+               pr_err("Failed reconfig VAS capabilities with DLPAR\n");
+
+       return rc;
+}
+
+static struct notifier_block pseries_vas_nb = {
+       .notifier_call = pseries_vas_notifier,
+};
+
 static int __init pseries_vas_init(void)
 {
        struct hv_vas_cop_feat_caps *hv_cop_caps;
@@ -592,6 +780,9 @@ static int __init pseries_vas_init(void)
                        goto out_cop;
        }
 
+       if (copypaste_feat && firmware_has_feature(FW_FEATURE_LPAR))
+               of_reconfig_notifier_register(&pseries_vas_nb);
+
        pr_info("GZIP feature is available\n");
 
 out_cop:
index 2872532..701363c 100644 (file)
@@ -83,6 +83,9 @@ struct vas_cop_feat_caps {
 struct vas_caps {
        struct vas_cop_feat_caps caps;
        struct list_head list;  /* List of open windows */
+       int nr_close_wins;      /* closed windows in the hypervisor for DLPAR */
+       int nr_open_windows;    /* Number of successful open windows */
+       u8 feat;                /* Feature type */
 };
 
 /*