From a35d4814f8444ec71c97377e1c276945fa31fc1b Mon Sep 17 00:00:00 2001 From: Mr NeilBrown Date: Mon, 4 May 2020 11:43:58 -0400 Subject: [PATCH] LU-9859 libcfs: replace memory_presure functions by standard interfaces 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 Signed-off-by: Greg Kroah-Hartman Reviewed-on: https://review.whamcloud.com/38211 Tested-by: jenkins Reviewed-by: Andreas Dilger Tested-by: Maloo --- libcfs/autoconf/lustre-libcfs.m4 | 18 +++++++++++++++++ libcfs/include/libcfs/libcfs_prim.h | 31 ----------------------------- libcfs/include/libcfs/linux/linux-mem.h | 19 ++++++++++++++++++ libcfs/libcfs/tracefile.c | 4 ++-- lnet/klnds/gnilnd/gnilnd_cb.c | 10 +++++++--- lnet/klnds/socklnd/socklnd_cb.c | 35 ++++++++++++++++++++------------- lnet/lnet/lib-move.c | 2 +- lustre/ldlm/ldlm_lockd.c | 10 +++++++--- lustre/osc/osc_cache.c | 2 +- lustre/osc/osc_request.c | 9 +++++---- lustre/ptlrpc/niobuf.c | 5 +++-- lustre/target/tgt_handler.c | 10 ++++++++-- 12 files changed, 92 insertions(+), 63 deletions(-) diff --git a/libcfs/autoconf/lustre-libcfs.m4 b/libcfs/autoconf/lustre-libcfs.m4 index 4346553..e933bc6 100644 --- a/libcfs/autoconf/lustre-libcfs.m4 +++ b/libcfs/autoconf/lustre-libcfs.m4 @@ -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 +],[ + 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 diff --git a/libcfs/include/libcfs/libcfs_prim.h b/libcfs/include/libcfs/libcfs_prim.h index 50ed7b9..c753ab5 100644 --- a/libcfs/include/libcfs/libcfs_prim.h +++ b/libcfs/include/libcfs/libcfs_prim.h @@ -50,35 +50,4 @@ # 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 diff --git a/libcfs/include/libcfs/linux/linux-mem.h b/libcfs/include/libcfs/linux/linux-mem.h index b822173..16ff239 100644 --- a/libcfs/include/libcfs/linux/linux-mem.h +++ b/libcfs/include/libcfs/linux/linux-mem.h @@ -44,6 +44,25 @@ #ifdef HAVE_MM_INLINE # include #endif +#include +#ifdef HAVE_SCHED_HEADERS +#include +#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 diff --git a/libcfs/libcfs/tracefile.c b/libcfs/libcfs/tracefile.c index 580c315..7ab2371 100644 --- a/libcfs/libcfs/tracefile.c +++ b/libcfs/libcfs/tracefile.c @@ -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); diff --git a/lnet/klnds/gnilnd/gnilnd_cb.c b/lnet/klnds/gnilnd/gnilnd_cb.c index 3ec2e55..a4d8f50 100644 --- a/lnet/klnds/gnilnd/gnilnd_cb.c +++ b/lnet/klnds/gnilnd/gnilnd_cb.c @@ -27,6 +27,9 @@ #include #include #include + +#include + #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; } diff --git a/lnet/klnds/socklnd/socklnd_cb.c b/lnet/klnds/socklnd/socklnd_cb.c index 23a406d..1417c13 100644 --- a/lnet/klnds/socklnd/socklnd_cb.c +++ b/lnet/klnds/socklnd/socklnd_cb.c @@ -24,6 +24,7 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#include #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); diff --git a/lnet/lnet/lib-move.c b/lnet/lnet/lib-move.c index 9dee2f9..8099a27 100644 --- a/lnet/lnet/lib-move.c +++ b/lnet/lnet/lib-move.c @@ -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); diff --git a/lustre/ldlm/ldlm_lockd.c b/lustre/ldlm/ldlm_lockd.c index 25a1892..bfe424b 100644 --- a/lustre/ldlm/ldlm_lockd.c +++ b/lustre/ldlm/ldlm_lockd.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -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)); diff --git a/lustre/osc/osc_cache.c b/lustre/osc/osc_cache.c index 3214339..327f938 100644 --- a/lustre/osc/osc_cache.c +++ b/lustre/osc/osc_cache.c @@ -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; diff --git a/lustre/osc/osc_request.c b/lustre/osc/osc_request.c index 00cc441..0ea2ad6 100644 --- a/lustre/osc/osc_request.c +++ b/lustre/osc/osc_request.c @@ -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); diff --git a/lustre/ptlrpc/niobuf.c b/lustre/ptlrpc/niobuf.c index 1ffaf43..41badd7 100644 --- a/lustre/ptlrpc/niobuf.c +++ b/lustre/ptlrpc/niobuf.c @@ -31,6 +31,7 @@ */ #define DEBUG_SUBSYSTEM S_RPC +#include #include #include #include @@ -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; } diff --git a/lustre/target/tgt_handler.c b/lustre/target/tgt_handler.c index 221342a..31ca748 100644 --- a/lustre/target/tgt_handler.c +++ b/lustre/target/tgt_handler.c @@ -38,6 +38,7 @@ #include #include +#include #include #include #include @@ -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); -- 1.8.3.1