Whamcloud - gitweb
LU-9859 libcfs: replace memory_presure functions by standard interfaces 11/38211/9
authorMr NeilBrown <neilb@suse.de>
Mon, 4 May 2020 15:43:58 +0000 (11:43 -0400)
committerOleg Drokin <green@whamcloud.com>
Thu, 14 May 2020 05:40:24 +0000 (05:40 +0000)
Use memalloc_noreclaim_save() and memalloc_noreclaim_restore(),
and for testing, just directly test the flag in current->flags

Linux-commit: fa399093620b867afe7686be47f197c8f17908c5

Change-Id: Id8f68164a7532ee4a816c71aff922f484c04877d
Signed-off-by: Mr NeilBrown <neilb@suse.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-on: https://review.whamcloud.com/38211
Tested-by: jenkins <devops@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
12 files changed:
libcfs/autoconf/lustre-libcfs.m4
libcfs/include/libcfs/libcfs_prim.h
libcfs/include/libcfs/linux/linux-mem.h
libcfs/libcfs/tracefile.c
lnet/klnds/gnilnd/gnilnd_cb.c
lnet/klnds/socklnd/socklnd_cb.c
lnet/lnet/lib-move.c
lustre/ldlm/ldlm_lockd.c
lustre/osc/osc_cache.c
lustre/osc/osc_request.c
lustre/ptlrpc/niobuf.c
lustre/target/tgt_handler.c

index 4346553..e933bc6 100644 (file)
@@ -763,6 +763,23 @@ EXTRA_KCFLAGS="$tmp_flags"
 ]) # LIBCFS_REFCOUNT_T
 
 #
+# Kernel version 4.12 commit 499118e966f1d2150bd66647c8932343c4e9a0b8
+# introduce memalloc_noreclaim_{save,restore}
+#
+AC_DEFUN([LIBCFS_MEMALLOC_NORECLAIM], [
+LB_CHECK_COMPILE([if memalloc_noreclaim_{save,restore} exist],
+memalloc_noreclaim, [
+       #include <linux/sched/mm.h>
+],[
+       int flag = memalloc_noreclaim_save();
+       memalloc_noreclaim_restore(flag);
+],[
+       AC_DEFINE(HAVE_MEMALLOC_RECLAIM, 1,
+               [memalloc_noreclaim_{save,restore}() is supported])
+])
+]) # LIBCFS_MEMALLOC_NORECLAIM
+
+#
 # LIBCFS_SCHED_HEADERS
 #
 # 4.11 has broken up sched.h into more headers.
@@ -1318,6 +1335,7 @@ LIBCFS_RHT_BUCKET_VAR
 # 4.12
 LIBCFS_HAVE_PROCESSOR_HEADER
 LIBCFS_HAVE_WAIT_BIT_HEADER
+LIBCFS_MEMALLOC_NORECLAIM
 LIBCFS_WAIT_QUEUE_TASK_LIST_RENAME
 LIBCFS_CPUS_READ_LOCK
 LIBCFS_UUID_T
index 50ed7b9..c753ab5 100644 (file)
 # define NUM_CACHEPAGES cfs_totalram_pages()
 #endif
 
-static inline unsigned int memory_pressure_get(void)
-{
-       return current->flags & PF_MEMALLOC;
-}
-
-static inline void memory_pressure_set(void)
-{
-       current->flags |= PF_MEMALLOC;
-}
-
-static inline void memory_pressure_clr(void)
-{
-       current->flags &= ~PF_MEMALLOC;
-}
-
-static inline int cfs_memory_pressure_get_and_set(void)
-{
-       int old = memory_pressure_get();
-
-       if (!old)
-               memory_pressure_set();
-       return old;
-}
-
-static inline void cfs_memory_pressure_restore(int old)
-{
-       if (old)
-               memory_pressure_set();
-       else
-               memory_pressure_clr();
-}
 #endif
index b822173..16ff239 100644 (file)
 #ifdef HAVE_MM_INLINE
 # include <linux/mm_inline.h>
 #endif
+#include <linux/sched.h>
+#ifdef HAVE_SCHED_HEADERS
+#include <linux/sched/mm.h>
+#endif
+
+#ifndef HAVE_MEMALLOC_RECLAIM
+static inline unsigned int memalloc_noreclaim_save(void)
+{
+       unsigned int flags = current->flags & PF_MEMALLOC;
+
+       current->flags |= PF_MEMALLOC;
+       return flags;
+}
+
+static inline void memalloc_noreclaim_restore(unsigned int flags)
+{
+       current->flags = (current->flags & ~PF_MEMALLOC) | flags;
+}
+#endif /* !HAVE_MEMALLOC_RECLAIM */
 
 /*
  * Shrinker
index 580c315..7ab2371 100644 (file)
@@ -75,7 +75,7 @@ static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
        struct cfs_trace_page *tage;
 
        /* My caller is trying to free memory */
-       if (!in_interrupt() && memory_pressure_get())
+       if (!in_interrupt() && (current->flags & PF_MEMALLOC))
                return NULL;
 
        /*
@@ -159,7 +159,7 @@ cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
                } else {
                        tage = cfs_tage_alloc(GFP_ATOMIC);
                        if (unlikely(tage == NULL)) {
-                               if ((!memory_pressure_get() ||
+                               if ((!(current->flags & PF_MEMALLOC) ||
                                     in_interrupt()) && printk_ratelimit())
                                        pr_warn("Lustre: cannot allocate a tage (%ld)\n",
                                                tcd->tcd_cur_pages);
index 3ec2e55..a4d8f50 100644 (file)
@@ -27,6 +27,9 @@
 #include <asm/page.h>
 #include <linux/nmi.h>
 #include <linux/pagemap.h>
+
+#include <libcfs/linux/linux-mem.h>
+
 #include "gnilnd.h"
 
 /* this is useful when needed to debug wire corruption. */
@@ -2125,7 +2128,8 @@ kgnilnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
        kgn_net_t        *net = ni->ni_data;
        kgn_tx_t         *tx;
        int               rc = 0;
-       int               mpflag = 0;
+       /* '1' for consistency with code that checks !mpflag to restore */
+       unsigned int mpflag = 1;
        int               reverse_rdma_flag = *kgnilnd_tunables.kgn_reverse_rdma;
 
        /* NB 'private' is different depending on what we're sending.... */
@@ -2140,7 +2144,7 @@ kgnilnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
                "lntmsg %p niov %d\n", lntmsg, niov);
 
        if (msg_vmflush)
-               mpflag = cfs_memory_pressure_get_and_set();
+               mpflag = memalloc_noreclaim_save();
 
        switch (type) {
        default:
@@ -2266,7 +2270,7 @@ kgnilnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
 out:
        /* use stored value as we could have already finalized lntmsg here from a failed launch */
        if (msg_vmflush)
-               cfs_memory_pressure_restore(mpflag);
+               memalloc_noreclaim_restore(mpflag);
        return rc;
 }
 
index 23a406d..1417c13 100644 (file)
@@ -24,6 +24,7 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#include <libcfs/linux/linux-mem.h>
 #include "socklnd.h"
 
 struct ksock_tx *
@@ -982,7 +983,8 @@ ksocknal_launch_packet(struct lnet_ni *ni, struct ksock_tx *tx,
 int
 ksocknal_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
 {
-       int mpflag = 1;
+       /* '1' for consistency with code that checks !mpflag to restore */
+       unsigned int mpflag = 1;
        int type = lntmsg->msg_type;
        struct lnet_process_id target = lntmsg->msg_target;
        unsigned int payload_niov = lntmsg->msg_niov;
@@ -1007,15 +1009,16 @@ ksocknal_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
                             tx_frags.paged.kiov[payload_niov]);
 
         if (lntmsg->msg_vmflush)
-                mpflag = cfs_memory_pressure_get_and_set();
-        tx = ksocknal_alloc_tx(KSOCK_MSG_LNET, desc_size);
-        if (tx == NULL) {
-                CERROR("Can't allocate tx desc type %d size %d\n",
-                       type, desc_size);
-                if (lntmsg->msg_vmflush)
-                        cfs_memory_pressure_restore(mpflag);
-                return (-ENOMEM);
-        }
+               mpflag = memalloc_noreclaim_save();
+
+       tx = ksocknal_alloc_tx(KSOCK_MSG_LNET, desc_size);
+       if (tx == NULL) {
+               CERROR("Can't allocate tx desc type %d size %d\n",
+                      type, desc_size);
+               if (lntmsg->msg_vmflush)
+                       memalloc_noreclaim_restore(mpflag);
+               return -ENOMEM;
+       }
 
        tx->tx_conn = NULL;                     /* set when assigned a conn */
        tx->tx_lnetmsg = lntmsg;
@@ -1035,10 +1038,14 @@ ksocknal_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
        tx->tx_msg.ksm_zc_cookies[0] = 0;
        tx->tx_msg.ksm_zc_cookies[1] = 0;
 
-        /* The first fragment will be set later in pro_pack */
-        rc = ksocknal_launch_packet(ni, tx, target);
-        if (!mpflag)
-                cfs_memory_pressure_restore(mpflag);
+       /* The first fragment will be set later in pro_pack */
+       rc = ksocknal_launch_packet(ni, tx, target);
+       /*
+        * We can't test lntsmg->msg_vmflush again as lntmsg may
+        * have been freed.
+        */
+       if (!mpflag)
+               memalloc_noreclaim_restore(mpflag);
 
         if (rc == 0)
                 return (0);
index 9dee2f9..8099a27 100644 (file)
@@ -4737,7 +4737,7 @@ LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
                       libcfs_id2str(target));
                return -ENOMEM;
        }
-       msg->msg_vmflush = !!memory_pressure_get();
+       msg->msg_vmflush = !!(current->flags & PF_MEMALLOC);
 
        cpt = lnet_cpt_of_cookie(mdh.cookie);
 
index 25a1892..bfe424b 100644 (file)
@@ -40,6 +40,7 @@
 #include <linux/kthread.h>
 #include <linux/list.h>
 #include <libcfs/libcfs.h>
+#include <libcfs/linux/linux-mem.h>
 #include <lustre_errno.h>
 #include <lustre_dlm.h>
 #include <obd_class.h>
@@ -2143,7 +2144,7 @@ static inline void init_blwi(struct ldlm_bl_work_item *blwi,
        init_completion(&blwi->blwi_comp);
        INIT_LIST_HEAD(&blwi->blwi_head);
 
-       if (memory_pressure_get())
+       if (current->flags & PF_MEMALLOC)
                blwi->blwi_mem_pressure = 1;
 
        blwi->blwi_ns = ns;
@@ -2805,6 +2806,9 @@ static int ldlm_bl_thread_need_create(struct ldlm_bl_pool *blp,
 static int ldlm_bl_thread_blwi(struct ldlm_bl_pool *blp,
                               struct ldlm_bl_work_item *blwi)
 {
+       /* '1' for consistency with code that checks !mpflag to restore */
+       unsigned int mpflags = 1;
+
        ENTRY;
 
        if (blwi->blwi_ns == NULL)
@@ -2812,7 +2816,7 @@ static int ldlm_bl_thread_blwi(struct ldlm_bl_pool *blp,
                RETURN(LDLM_ITER_STOP);
 
        if (blwi->blwi_mem_pressure)
-               memory_pressure_set();
+               mpflags = memalloc_noreclaim_save();
 
        OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL2, 4);
 
@@ -2834,7 +2838,7 @@ static int ldlm_bl_thread_blwi(struct ldlm_bl_pool *blp,
                                        blwi->blwi_lock);
        }
        if (blwi->blwi_mem_pressure)
-               memory_pressure_clr();
+               memalloc_noreclaim_restore(mpflags);
 
        if (blwi->blwi_flags & LCF_ASYNC)
                OBD_FREE(blwi, sizeof(*blwi));
index 3214339..327f938 100644 (file)
@@ -2552,7 +2552,7 @@ int osc_flush_async_page(const struct lu_env *env, struct cl_io *io,
        oap->oap_async_flags |= ASYNC_READY|ASYNC_URGENT;
        spin_unlock(&oap->oap_lock);
 
-       if (memory_pressure_get())
+       if (current->flags & PF_MEMALLOC)
                ext->oe_memalloc = 1;
 
        ext->oe_urgent = 1;
index 00cc441..0ea2ad6 100644 (file)
@@ -2178,7 +2178,8 @@ int osc_build_rpc(const struct lu_env *env, struct client_obd *cli,
        struct cl_req_attr              *crattr = NULL;
        loff_t                          starting_offset = OBD_OBJECT_EOF;
        loff_t                          ending_offset = 0;
-       int                             mpflag = 0;
+       /* '1' for consistency with code that checks !mpflag to restore */
+       int mpflag = 1;
        int                             mem_tight = 0;
        int                             page_count = 0;
        bool                            soft_sync = false;
@@ -2205,7 +2206,7 @@ int osc_build_rpc(const struct lu_env *env, struct client_obd *cli,
 
        soft_sync = osc_over_unstable_soft_limit(cli);
        if (mem_tight)
-               mpflag = cfs_memory_pressure_get_and_set();
+               mpflag = memalloc_noreclaim_save();
 
        OBD_ALLOC_PTR_ARRAY(pga, page_count);
        if (pga == NULL)
@@ -2327,8 +2328,8 @@ int osc_build_rpc(const struct lu_env *env, struct client_obd *cli,
        EXIT;
 
 out:
-       if (mem_tight != 0)
-               cfs_memory_pressure_restore(mpflag);
+       if (mem_tight)
+               memalloc_noreclaim_restore(mpflag);
 
        if (rc != 0) {
                LASSERT(req == NULL);
index 1ffaf43..41badd7 100644 (file)
@@ -31,6 +31,7 @@
  */
 
 #define DEBUG_SUBSYSTEM S_RPC
+#include <libcfs/linux/linux-mem.h>
 #include <obd_support.h>
 #include <lustre_net.h>
 #include <lustre_lib.h>
@@ -785,7 +786,7 @@ int ptl_send_rpc(struct ptlrpc_request *request, int noreply)
                        request->rq_resend_cb(request, &request->rq_async_args);
        }
        if (request->rq_memalloc)
-               mpflag = cfs_memory_pressure_get_and_set();
+               mpflag = memalloc_noreclaim_save();
 
        rc = sptlrpc_cli_wrap_request(request);
        if (rc)
@@ -940,7 +941,7 @@ int ptl_send_rpc(struct ptlrpc_request *request, int noreply)
        }
 
        if (request->rq_memalloc)
-               cfs_memory_pressure_restore(mpflag);
+               memalloc_noreclaim_restore(mpflag);
 
        return rc;
 }
index 221342a..31ca748 100644 (file)
@@ -38,6 +38,7 @@
 #include <linux/delay.h>
 #include <linux/uidgid.h>
 
+#include <libcfs/linux/linux-mem.h>
 #include <obd.h>
 #include <obd_class.h>
 #include <obd_cksum.h>
@@ -2477,6 +2478,8 @@ int tgt_brw_write(struct tgt_session_info *tsi)
        struct tgt_thread_big_cache *tbc = req->rq_svc_thread->t_data;
        bool wait_sync = false;
        const char *obd_name = exp->exp_obd->obd_name;
+       /* '1' for consistency with code that checks !mpflag to restore */
+       unsigned int mpflags = 1;
 
        ENTRY;
 
@@ -2535,7 +2538,7 @@ int tgt_brw_write(struct tgt_session_info *tsi)
 
        if ((remote_nb[0].rnb_flags & OBD_BRW_MEMALLOC) &&
            ptlrpc_connection_is_local(exp->exp_connection))
-               memory_pressure_set();
+               mpflags = memalloc_noreclaim_save();
 
        req_capsule_set_size(&req->rq_pill, &RMF_RCS, RCL_SERVER,
                             niocount * sizeof(*rcs));
@@ -2723,7 +2726,10 @@ out:
                                      obd_uuid2str(&exp->exp_client_uuid),
                                      obd_export_nid2str(exp), rc);
        }
-       memory_pressure_clr();
+
+       if (mpflags)
+               memalloc_noreclaim_restore(mpflags);
+
        RETURN(rc);
 }
 EXPORT_SYMBOL(tgt_brw_write);