Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp371768rwb; Thu, 10 Nov 2022 01:56:52 -0800 (PST) X-Google-Smtp-Source: AA0mqf5nHfjfyFzhWbB/DjUbT2j6+v0WdxXmjUkBb31b1bvWu5tcmrTXDoDTH7JM1lAQMK8rSQJz X-Received: by 2002:a17:907:3f9d:b0:7ae:587e:73aa with SMTP id hr29-20020a1709073f9d00b007ae587e73aamr21771003ejc.289.1668074212670; Thu, 10 Nov 2022 01:56:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668074212; cv=none; d=google.com; s=arc-20160816; b=XcJ081IgE3KUUg6vxXQ4qA7oN3n3j8/A/0psj4S/oe2it3isK0b3aqAd/sZqeNmKs6 9RkEPfzmwsqegoQ4IB+p/xe6TI281tbyOtGe+j8jSADdm9NgGkMnwMqpIY6LAOPDn/PK j9R7W3JexXKy4+zFaS1ZyiNA8rhIwPbSTUBHbdEIXEwtQ6C08Oljjk23c20fyC+hOBha sg/HTQyzovKDxgWh5Z69uOwEYZKPR+m2zjTQmCpR9qYIOAoMGwAxlxIAX3S5HISu+l2m YbzotimqXiCAXoaWzPJRWDzdKkfHJZM9ZO4DJrtnrCgJvpBlHlGd40VUEOb2M5+Lu9Em 3UJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Xcao/PqbCDd3mW4vvxXtcV3kVRYEc6SH+zTFTbxv0YQ=; b=lefE5rTYwUv/WE1b964EDdZgw7iz1vDJh6jkBAW2451GnCD990AEx0N6jaAGcobzUp LJ5TUprw+873YPAbR3XrYxj1WUas+ZXsBrZ7ElWaJcjfjVymNCu29+Jj79u1pgCpTSKK ytaj7IeNt5HgvYCcXCmUfUVmMhhn3C1EbLpKsKCGcPHGLmp5CwyBPvgOvrQ6JxHm0uV9 dLtFHrb5jmYrc1L8n8Ux7weO/P3Xd5ctbAHtBkW5/cOmfK2BasMf/jnthhb+ADdGWWHY Z92ZawoeexXDd/nf6R3QO1J/6FCbmbLgvj5BM3p/WkGPDB0DVcoCR80iWKwY6CjiQiEz lhow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Zi1EYc3/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y8-20020a50bb08000000b0044e987f3a34si18187473ede.489.2022.11.10.01.56.30; Thu, 10 Nov 2022 01:56:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Zi1EYc3/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229843AbiKJJPb (ORCPT + 92 others); Thu, 10 Nov 2022 04:15:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229785AbiKJJP0 (ORCPT ); Thu, 10 Nov 2022 04:15:26 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF36C682AE for ; Thu, 10 Nov 2022 01:14:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668071670; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Xcao/PqbCDd3mW4vvxXtcV3kVRYEc6SH+zTFTbxv0YQ=; b=Zi1EYc3/GAIALG6zuBSCTkpq0rPJBARTWUCoxibiVTwJIE93jEe8NdjsdO3UCeNPQocWyf jOFDYlox3IUjJBDkvpZmYnpLk3XON7+iq0PPfoESiwpMm6KNPUasuG9yJfJ6GOy/G6El7P H8fCkNSazXItZdMK9T0L8i3JiEVjpD4= Received: from mail-oa1-f71.google.com (mail-oa1-f71.google.com [209.85.160.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-209-GpG3E-IqOAmRLLiZJa6XEw-1; Thu, 10 Nov 2022 04:14:26 -0500 X-MC-Unique: GpG3E-IqOAmRLLiZJa6XEw-1 Received: by mail-oa1-f71.google.com with SMTP id 586e51a60fabf-13b88262940so756678fac.15 for ; Thu, 10 Nov 2022 01:14:25 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Xcao/PqbCDd3mW4vvxXtcV3kVRYEc6SH+zTFTbxv0YQ=; b=V2iRGL+E0VvQ4Y8LGWRf/4b9xgg3t/qa8JHWbcFNTsnlB9D6nyMJ7iuOnbyDwKkquc Jz03uwdaXKd9zIr6IsyaVepJgv6TWXTfxXAaY1CJJthIyYlDH3zjK/o0mgYegOg++KYk vt/5nSc/7vED5ZN37jLgrCWlk1w6Zvc9keAf5rex+6iRZmIRrclpmrjnjHuLr70LBZjJ 7ym/jGtc7reVuPA01rHm6Hmr9FYyeXL6CdeThvyBzu8Ucynybdtce1o5TGm1YGJFYkbv kXa+KO+3s/VP2+TbWOIADRuCFr/R+w2Q3xQGdxyqCm1QdwKrCyWwuPsmjD8GK/ki1lVL NuzA== X-Gm-Message-State: ACrzQf1uPkGsA3/wql3RVuUbX+aYKVP1mP7TAYs2WCgb9sEOvfuCQXSe XyUkDkJucJlkkRBkoYm5320lgi65Szo2ImvJ4TfB+Op1vMyes0QmVSenuiaxQS0qa1JM1CP/LWI 9Z0QHSCaBJ/LuQ2CN+KwbMwvapjs3+Ujgd6WHu6uT X-Received: by 2002:a05:6870:9595:b0:132:7b3:29ac with SMTP id k21-20020a056870959500b0013207b329acmr1260434oao.35.1668071664157; Thu, 10 Nov 2022 01:14:24 -0800 (PST) X-Received: by 2002:a05:6870:9595:b0:132:7b3:29ac with SMTP id k21-20020a056870959500b0013207b329acmr1260423oao.35.1668071663899; Thu, 10 Nov 2022 01:14:23 -0800 (PST) MIME-Version: 1.0 References: <20221110082348.4105476-1-ruanjinjie@huawei.com> In-Reply-To: <20221110082348.4105476-1-ruanjinjie@huawei.com> From: Jason Wang Date: Thu, 10 Nov 2022 17:14:12 +0800 Message-ID: Subject: Re: [PATCH v3] vdpa_sim: fix possible memory leak in vdpasim_net_init() and vdpasim_blk_init() To: ruanjinjie Cc: mst@redhat.com, sgarzare@redhat.com, eperezma@redhat.com, gautam.dawar@xilinx.com, elic@nvidia.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 10, 2022 at 4:27 PM ruanjinjie wrote: > > Inject fault while probing module, if device_register() fails in > vdpasim_net_init() or vdpasim_blk_init(), but the refcount of kobject is > not decreased to 0, the name allocated in dev_set_name() is leaked. > Fix this by calling put_device(), so that name can be freed in > callback function kobject_cleanup(). > > (vdpa_sim_net) > unreferenced object 0xffff88807eebc370 (size 16): > comm "modprobe", pid 3848, jiffies 4362982860 (age 18.153s) > hex dump (first 16 bytes): > 76 64 70 61 73 69 6d 5f 6e 65 74 00 6b 6b 6b a5 vdpasim_net.kkk. > backtrace: > [] __kmalloc_node_track_caller+0x4e/0x150 > [] kstrdup+0x33/0x60 > [] kobject_set_name_vargs+0x41/0x110 > [] dev_set_name+0xab/0xe0 > [] device_add+0xe3/0x1a80 > [] 0xffffffffa0270013 > [] do_one_initcall+0x87/0x2e0 > [] do_init_module+0x1ab/0x640 > [] load_module+0x5d00/0x77f0 > [] __do_sys_finit_module+0x110/0x1b0 > [] do_syscall_64+0x35/0x80 > [] entry_SYSCALL_64_after_hwframe+0x46/0xb0 > > (vdpa_sim_blk) > unreferenced object 0xffff8881070c1250 (size 16): > comm "modprobe", pid 6844, jiffies 4364069319 (age 17.572s) > hex dump (first 16 bytes): > 76 64 70 61 73 69 6d 5f 62 6c 6b 00 6b 6b 6b a5 vdpasim_blk.kkk. > backtrace: > [] __kmalloc_node_track_caller+0x4e/0x150 > [] kstrdup+0x33/0x60 > [] kobject_set_name_vargs+0x41/0x110 > [] dev_set_name+0xab/0xe0 > [] device_add+0xe3/0x1a80 > [] 0xffffffffa0220013 > [] do_one_initcall+0x87/0x2e0 > [] do_init_module+0x1ab/0x640 > [] load_module+0x5d00/0x77f0 > [] __do_sys_finit_module+0x110/0x1b0 > [] do_syscall_64+0x35/0x80 > [] entry_SYSCALL_64_after_hwframe+0x46/0xb0 > > Fixes: 899c4d187f6a ("vdpa_sim_blk: add support for vdpa management tool") > Fixes: a3c06ae158dd ("vdpa_sim_net: Add support for user supported devices") > > Signed-off-by: ruanjinjie > Reviewed-by: Stefano Garzarella Acked-by: Jason Wang Thanks > --- > v3: > - add fix tag > v2: > - add fault inject message > --- > drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 4 +++- > drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 4 +++- > 2 files changed, 6 insertions(+), 2 deletions(-) > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c > index c6db1a1baf76..f745926237a8 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c > @@ -427,8 +427,10 @@ static int __init vdpasim_blk_init(void) > int ret; > > ret = device_register(&vdpasim_blk_mgmtdev); > - if (ret) > + if (ret) { > + put_device(&vdpasim_blk_mgmtdev); > return ret; > + } > > ret = vdpa_mgmtdev_register(&mgmt_dev); > if (ret) > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c > index c3cb225ea469..11f5a121df24 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c > @@ -305,8 +305,10 @@ static int __init vdpasim_net_init(void) > int ret; > > ret = device_register(&vdpasim_net_mgmtdev); > - if (ret) > + if (ret) { > + put_device(&vdpasim_net_mgmtdev); > return ret; > + } > > ret = vdpa_mgmtdev_register(&mgmt_dev); > if (ret) > -- > 2.25.1 >