Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3914443pxb; Mon, 1 Feb 2021 07:52:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJy3zGs5zPSJ0TZfJirVJjThxGRPXq+uwHuOzwgJgHnGuA+9UslJR6hFNM06Ay7WMdlGs7aG X-Received: by 2002:a17:906:470e:: with SMTP id y14mr18242551ejq.349.1612194749234; Mon, 01 Feb 2021 07:52:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612194749; cv=none; d=google.com; s=arc-20160816; b=JKei6iOSYYxZ1g10bQJZQkITdZDfcDElyB/wq2/giDsZOtWDiIk3+fJLV93SmGn3yH krzngmiV4eLMzr+rqUO5jsJGBCm50ieh2N11ZQGJqVmXm71S9BAMOwoKD8YsTLYId7ax h6VthCaXQrf4oh3KtGJxM9QMphOoh3ZYtvT8Qudih0zZ8s1KkOgbfYRjrtypbRjS47/n 6KR8BtxWGTyoIw/t6EfLquRnA6ZtsHxBkMtv6NO68DxLAJjrqh82zKYn2apPlxkjzq3L UmfIFvQDfo5Rp3b7ZwtAEwXy+dtADupk3Uw7GJnwT0wq+6ZLrQhtPZ57YiyBWN4L87wO vESA== 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=umfeuXW08c1qiq1OJKFy/yOM2bFxEDQdo0ZTFrpR6VM=; b=wsLlnTrDSjgUtZ8l8baWozZHrcBRvIKDg2D5NOmtSQuHrOWh9tZZSCMkHmXIi0PhWy Nngf6CfsyD/F4W9CFq9YUS5rcoq1OgfvbsFRJ59Lm9NOnd4Ig/vGW18Rx7AVo92leMzk wDlxwfufuxo8TzkiEoLv5UwtbtgOrPb6oqaX0HTnbJFzi5mJu+V0pe4zVzhmYsKF9ZXV 0WRW/zUCfro1LK6KFDOVpIIhE98WMz0xPQiDeSebC7EQQDo+/tq1cUeBjlooGKbnwOUK Pq4Z8T1DL6eFGeVQb7yGzuPG7P5JRG5KWz91934XssxfrbBs/4Gnxu1AOhcK6R0h51y8 hLqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=X55wTf8G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y6si12382242edp.196.2021.02.01.07.52.03; Mon, 01 Feb 2021 07:52:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=X55wTf8G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231720AbhBAPvG (ORCPT + 99 others); Mon, 1 Feb 2021 10:51:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231535AbhBAPu6 (ORCPT ); Mon, 1 Feb 2021 10:50:58 -0500 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D6E0C06174A for ; Mon, 1 Feb 2021 07:50:10 -0800 (PST) Received: by mail-ej1-x633.google.com with SMTP id bl23so25100155ejb.5 for ; Mon, 01 Feb 2021 07:50:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=umfeuXW08c1qiq1OJKFy/yOM2bFxEDQdo0ZTFrpR6VM=; b=X55wTf8G8OwgkjiABiwOVZmZuE1SSTejF01oX9CAb9n3QEZi3TNqI7CwHyRJVhrEFN GNGjSDM9cLnmzD3Ljm236PQMaCf7WUUu5qPVPIdHP6adoZqpKRYfk09ezhm8wQnU9pjs VmPIkmA8qM9eNVcqZfM14QUMGgmY5jV8xvSfhtWtmZPg/BQEuVXDFurbNT/YKXcdCFrk OS6FVb8PGTypAwqVecLQBN5X7A+SIQ7IHg/YOia0/iHGCHYs2KAQQgyWe5q84ZBUFijc /9mn8Z0960EE4ZxuyuzhI8Xy9EtxIJ2bRESkF0z2jtKJJpmT5ywjnHH+IBzTS2wbzBOj dZ7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=umfeuXW08c1qiq1OJKFy/yOM2bFxEDQdo0ZTFrpR6VM=; b=jHBuSwD6JXxCu5Sgd7KXgl7QkU4fHUhnnFz6m/LKpyPuJmD+nbTt85cC2kRdJ5N9ym u+yBdCLDEvWSDL6ydllmN4BapKgqKAj44hjuhwSGbcMEPxVBHh4Z8Lg8qYiMYPknMEWu gYgmQD366RH4l9HmMmJ+P8bURPGVkzRuiQZemfC8AF80b5rLTxHiAwlaDg/01yVqdaGT /3IWz/m5fXrtJDOAa65Ip4nXCaPim86R6W7+9scdHENoCwvBzEuqVQyphTizRHBlL94c E8gUnOKweM88LwfgN1qF1b37InRpjokR6L6v97Nv3kd8DegmcqrFU53iMMldlP6mQxGd ms6A== X-Gm-Message-State: AOAM531zPhWYd4ApfERlqiacxzYGm2Aw4idKG+oOP0+enWm2MgG8b/kI /f9K1KvO8PXsrzddUIRntGaoH/iEVD9sUomRtTGxsg== X-Received: by 2002:a17:906:19c3:: with SMTP id h3mr17993759ejd.429.1612194608986; Mon, 01 Feb 2021 07:50:08 -0800 (PST) MIME-Version: 1.0 References: <20210129081917.1808218-1-quanyang.wang@windriver.com> <02e614e9-fe2f-d7b2-6c47-2431475780ed@windriver.com> In-Reply-To: <02e614e9-fe2f-d7b2-6c47-2431475780ed@windriver.com> From: Bartosz Golaszewski Date: Mon, 1 Feb 2021 16:49:58 +0100 Message-ID: Subject: Re: [PATCH] gpiolib: free device name on error path to fix kmemleak To: "quanyang.wang" , Andy Shevchenko Cc: Linus Walleij , "open list:GPIO SUBSYSTEM" , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jan 30, 2021 at 4:45 AM quanyang.wang wrote: > > Hi Andy, > > On 1/30/21 1:26 AM, Andy Shevchenko wrote: > > On Fri, Jan 29, 2021 at 2:01 PM wrote: > >> From: Quanyang Wang > >> > >> In gpiochip_add_data_with_key, we should check the return value of > >> dev_set_name to ensure that device name is allocated successfully > >> and then add a label on the error path to free device name to fix > >> kmemleak as below: > > Thanks for the report. > > Unfortunately... > > > >> + ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id); > >> + if (ret) > >> + goto err_free_ida; > > ... > > > >> +err_free_dev_name: > >> + kfree(dev_name(&gdev->dev)); > > ...this approach seems to create a possible double free if I'm not mistaken. > Thanks for your comment. I didn't catch the double free. Would you > please point it out? > > > > The idea is that device name should be cleaned in kobject ->release() > > callback when device is put. > > Yes, the device name should be freed by calling put_device(&gdev->dev). > But int gpiochip_add_data_with_key, > > when running dev_set_name, "gdev->dev.release" hasn't been installed > until in the tail of gpiochip_add_data_with_key. > > So we couldn't call put_device here. > > Any suggestion is much appreciated. > > Thanks, > > Quanyang > > > Can you elaborate? > > Andy, gdev->dev.release is assigned as the very last step in gpiochip_add_data_with_key() so the patch looks correct to me. Do you still have objections? Maybe I'm not seeing something. Bart