From patchwork Wed Dec 9 05:03:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zhu Yanjun X-Patchwork-Id: 11957811 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00, DATE_IN_FUTURE_12_24,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA90CC4361B for ; Tue, 8 Dec 2020 07:40:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 97B8123A3C for ; Tue, 8 Dec 2020 07:40:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727193AbgLHHkF (ORCPT ); Tue, 8 Dec 2020 02:40:05 -0500 Received: from mga11.intel.com ([192.55.52.93]:50127 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726734AbgLHHkF (ORCPT ); Tue, 8 Dec 2020 02:40:05 -0500 IronPort-SDR: UzZ2G44LoUWFbwXEjYjw7MTwEPz+vnbTTQSUKq8km2oHKPTad3NrFPrAfHzGICm7viYZakOmIm 5ZiNM4sljeUQ== X-IronPort-AV: E=McAfee;i="6000,8403,9828"; a="170337875" X-IronPort-AV: E=Sophos;i="5.78,401,1599548400"; d="scan'208";a="170337875" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Dec 2020 23:39:20 -0800 IronPort-SDR: MUBL+F5W9P6LV33aAaKRMThtmIRsq2LlA4ii15Djhj1gQftAAjCp7lwt+6d75TOs0a1M3mR53d MG3hMjTXz3zg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.78,401,1599548400"; d="scan'208";a="363530493" Received: from unknown (HELO localhost.localdomain.bj.intel.com) ([10.238.154.69]) by orsmga008.jf.intel.com with ESMTP; 07 Dec 2020 23:39:19 -0800 From: Zhu Yanjun To: zyjzyj2000@gmail.com, bjorn.topel@intel.com, magnus.karlsson@intel.com, netdev@vger.kernel.org, jonathan.lemon@gmail.com Cc: Zhu Yanjun Subject: [PATCH v2 1/1] xdp: avoid calling kfree twice Date: Wed, 9 Dec 2020 00:03:15 -0500 Message-Id: <20201209050315.5864-1-yanjun.zhu@intel.com> X-Mailer: git-send-email 2.18.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org In the function xdp_umem_pin_pages, if npgs != umem->npgs and npgs >= 0, the function xdp_umem_unpin_pages is called. In this function, kfree is called to handle umem->pgs, and then in the function xdp_umem_pin_pages, kfree is called again to handle umem->pgs. Eventually, umem->pgs is freed twice. Acked-by: Björn Töpel Signed-off-by: Zhu Yanjun --- net/xdp/xdp_umem.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/net/xdp/xdp_umem.c b/net/xdp/xdp_umem.c index 56a28a686988..01b31c56cead 100644 --- a/net/xdp/xdp_umem.c +++ b/net/xdp/xdp_umem.c @@ -97,7 +97,6 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address) { unsigned int gup_flags = FOLL_WRITE; long npgs; - int err; umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs), GFP_KERNEL | __GFP_NOWARN); @@ -112,20 +111,14 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address) if (npgs != umem->npgs) { if (npgs >= 0) { umem->npgs = npgs; - err = -ENOMEM; - goto out_pin; + xdp_umem_unpin_pages(umem); + return -ENOMEM; } - err = npgs; - goto out_pgs; + kfree(umem->pgs); + umem->pgs = NULL; + return (int)npgs; } return 0; - -out_pin: - xdp_umem_unpin_pages(umem); -out_pgs: - kfree(umem->pgs); - umem->pgs = NULL; - return err; } static int xdp_umem_account_pages(struct xdp_umem *umem)