From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 61F7DA0554; Wed, 16 Nov 2022 11:22:09 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3C92540DFB; Wed, 16 Nov 2022 11:22:09 +0100 (CET) Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) by mails.dpdk.org (Postfix) with ESMTP id 8501C40DFD for ; Tue, 15 Nov 2022 13:35:47 +0100 (CET) Received: by mail-pj1-f48.google.com with SMTP id w3-20020a17090a460300b00218524e8877so308114pjg.1 for ; Tue, 15 Nov 2022 04:35:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=PLvx+i6iNBKKlzfhCBVkQmaKWG/lq9zlOnq5g2T2EMg=; b=m2xnya+ITLJv3zW2/XqU1rdvIuaLuun7IdANV9EpxEBWIsftsm+q9ul3z0dkGtCJOw aVRnf8dPyO87cGRxNzVl/tlsCdDFPdPygO+/tuCt4PNh2V/TUV1UjBhHCGKrYEjR6+7g ZgJ+X75STPP/qdwQrWabY6eZyWiAzj4wcgz/sVDk/3rdX5wsGqbNiocNbsU/bxUb48Yu Z9CqkHCB0xnh5vUojgOg7HmfPnOIfLwXSOA+rrkBWP4/X1gxqCMHv4O+g3nRylD+zfQY 1rfwmkev72jjpMsKOBWj4vkRxLq0blPm7oY6AWLsmPAbUqphFRX1xAcH1PIGXyNvjWVY bfkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PLvx+i6iNBKKlzfhCBVkQmaKWG/lq9zlOnq5g2T2EMg=; b=nbbYx2qS4bgLBRAA23YGzaISoiik2SqvYlB6GJyMP8sDE6qR8158GAdKDQUrktJPsH Ksme8nfi0maL8Ak4A2Ub5v5t9hv+7YvZYZaSoxth4GVnYzuunZcOzeNzO38A8e1YZigg Jqri0pL0TjPy92Ab1TFHSAM30YzwrkX9XXm2KRdt0hUyO18AmHxFQ8d5enrBM2mqgfRt +fLLLBOZtl92zkmaTNmoER35K8AwkGhiQRmJg5T5/QDTFXhCCZxJgCLTSCo50TE+kFm6 EqMw+SRoyH2bUNv3N76q0wmJhBLo5bv+Ss4fYZ6HkLvSewF2/XGtfUAqXnxXOtdUajJe IzUg== X-Gm-Message-State: ANoB5pnyt0A/XOfw/a+XNmjnr+hpaMbdQ13F19gY56EXM0VXQc80Co1K qRZTQK7+FUE3HW6cJAiY3ZCYNA== X-Google-Smtp-Source: AA0mqf5Gz8zllNvTJOJkAQRKHNwQdnbXX53tbgWOWjevRw3xxGSEA2cZxQo1pVsjCZOfkeefPfDxTg== X-Received: by 2002:a17:90a:d18d:b0:214:1df2:b566 with SMTP id fu13-20020a17090ad18d00b002141df2b566mr2100584pjb.147.1668515746684; Tue, 15 Nov 2022 04:35:46 -0800 (PST) Received: from HTW5T2C6VL.bytedance.net ([2404:9dc0:cd01::15]) by smtp.gmail.com with ESMTPSA id a17-20020a170902ee9100b001888a46e2d0sm9766062pld.162.2022.11.15.04.35.43 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 15 Nov 2022 04:35:46 -0800 (PST) From: Fengnan Chang To: david.marchand@redhat.com, olivier.matz@6wind.com, mb@smartsharesystems.com, dev@dpdk.org Cc: Fengnan Chang Subject: [PATCH v2] mempool: fix rte_mempool_avail_count may segment fault when used in multiprocess Date: Tue, 15 Nov 2022 20:35:02 +0800 Message-Id: <20221115123502.12560-1-changfengnan@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailman-Approved-At: Wed, 16 Nov 2022 11:22:07 +0100 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org rte_mempool_create put tailq entry into rte_mempool_tailq list before populate, and pool_data set when populate. So in multi process, if process A create mempool, and process B can get mempool through rte_mempool_lookup before pool_data set, if B call rte_mempool_avail_count, it will cause segment fault. Fix this by put tailq entry into rte_mempool_tailq after populate. Signed-off-by: Fengnan Chang --- lib/mempool/rte_mempool.c | 43 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/mempool/rte_mempool.c b/lib/mempool/rte_mempool.c index 4c78071a34..b3a6572fc8 100644 --- a/lib/mempool/rte_mempool.c +++ b/lib/mempool/rte_mempool.c @@ -155,6 +155,27 @@ get_min_page_size(int socket_id) return wa.min == SIZE_MAX ? (size_t) rte_mem_page_size() : wa.min; } +static int +add_mempool_to_list(struct rte_mempool *mp) +{ + struct rte_mempool_list *mempool_list; + struct rte_tailq_entry *te = NULL; + + /* try to allocate tailq entry */ + te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0); + if (te == NULL) { + RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n"); + return -ENOMEM; + } + + te->data = mp; + mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list); + rte_mcfg_tailq_write_lock(); + TAILQ_INSERT_TAIL(mempool_list, te, next); + rte_mcfg_tailq_write_unlock(); + + return 0; +} static void mempool_add_elem(struct rte_mempool *mp, __rte_unused void *opaque, @@ -304,6 +325,9 @@ mempool_ops_alloc_once(struct rte_mempool *mp) if (ret != 0) return ret; mp->flags |= RTE_MEMPOOL_F_POOL_CREATED; + ret = add_mempool_to_list(mp); + if (ret != 0) + return ret; } return 0; } @@ -798,9 +822,7 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, int socket_id, unsigned flags) { char mz_name[RTE_MEMZONE_NAMESIZE]; - struct rte_mempool_list *mempool_list; struct rte_mempool *mp = NULL; - struct rte_tailq_entry *te = NULL; const struct rte_memzone *mz = NULL; size_t mempool_size; unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY; @@ -820,8 +842,6 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, RTE_CACHE_LINE_MASK) != 0); #endif - mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list); - /* asked for zero items */ if (n == 0) { rte_errno = EINVAL; @@ -866,14 +886,6 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, private_data_size = (private_data_size + RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK); - - /* try to allocate tailq entry */ - te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0); - if (te == NULL) { - RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n"); - goto exit_unlock; - } - mempool_size = RTE_MEMPOOL_HEADER_SIZE(mp, cache_size); mempool_size += private_data_size; mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN); @@ -923,20 +935,13 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, cache_size); } - te->data = mp; - - rte_mcfg_tailq_write_lock(); - TAILQ_INSERT_TAIL(mempool_list, te, next); - rte_mcfg_tailq_write_unlock(); rte_mcfg_mempool_write_unlock(); - rte_mempool_trace_create_empty(name, n, elt_size, cache_size, private_data_size, flags, mp); return mp; exit_unlock: rte_mcfg_mempool_write_unlock(); - rte_free(te); rte_mempool_free(mp); return NULL; } -- 2.37.0 (Apple Git-136)