Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp601230lqt; Fri, 19 Apr 2024 05:30:36 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXew0w+D9t5Kuo8MfNIGFt44OhmiyHnwVGHrFSb0HQY6PqEmAczEkcMMWtOu1Tsdsm6cFGcLYHQrV/XfLawO2oWHoyLFew+oZmoXsZ8qw== X-Google-Smtp-Source: AGHT+IGaqFyrbgx9PTk/sU1W041IcskqDtCI64wmeBuf5/wGe5rTkBBDD4KyHee5FIuboIGU+mUl X-Received: by 2002:a05:620a:8107:b0:78d:6398:d684 with SMTP id os7-20020a05620a810700b0078d6398d684mr2097855qkn.64.1713529836349; Fri, 19 Apr 2024 05:30:36 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713529836; cv=pass; d=google.com; s=arc-20160816; b=tMdL0V/9ZIK0zORWh95Dh1SxGBtQU82DPT1KWajgTxquZBWqTLmVamjeoQFWvFvISA VWEf74/W7/hqt8B5qPHhOgjuqYmLISgP7G//hGxJtZuIPCqVTp4mORXXdUhVrmfSeVUT X9geD7bjmMQRCkn+rMXqQ5GHRaG04p6h3tligrycxrE55Pmt4duBkEjCyLoHzQq2ZBTq A3VEZVmjaBTRnF6euJ2cIf4t7fa6t6sEUiElb1dNWq+7seN1NhVS3+dDFaCcPnWxcYrn f6kRMOMXbC2pXuOGudIQfsgzpSq9twNZnvrL7rby/r4qU1t5V4OcwpGkdZ67R3vfAd/K GNKQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=xUl9xI+kv1Ecrl0dN5hTZc3O707nonguLWXio7bCIq4=; fh=Hqc64R2rTnsyXF/GKeQsU0Jq363HX00byTMxNcqm2Sk=; b=pNyP8xbJ+uACeoA01glnpGSToDfIYR+0La1JKKr4vaGrvoakv/4iSj50pOuOzZFKrb b3psJQwQU/AxtgwPGyCTR4vab9ayk9Bmhi7On+02+tp3sZ0LioWyOtXZYSPD2JyCqsIl QI/n+XNQnUtvhqLCAp+L1QJsO2LvLJ+KU9lHzLTq6OHhYBrgnG7oFN0DIWfVRKzrDy4N CcRvjjF9520x4KKoQvCkO369G/r+PSpvgrevlA9+WIRiWyMklv3Xp6Nd+/knZuMr+y29 TGTfXdmc0xMpUv9FKGc9ybAvE/jo325Qs/DsQtxtGJNrFMAqe+ZhG5yPIlR1ZphmWtcE rl8w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YaAw2RNs; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-151408-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151408-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id j15-20020ae9c20f000000b0078ee0c12e34si3710048qkg.429.2024.04.19.05.30.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 05:30:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-151408-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YaAw2RNs; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-151408-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151408-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0A3111C20D58 for ; Fri, 19 Apr 2024 12:30:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 86B518565F; Fri, 19 Apr 2024 12:30:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YaAw2RNs" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E5A68563B for ; Fri, 19 Apr 2024 12:30:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713529823; cv=none; b=YCW3MSo0WcXJxZ3nHVM+++8ZLV0LKthRPOPlCMffMFp3tH5sOk7z4bEv2jDADId2gwJ0NGx3XYcQs9JiaHr9NZgNemRfaZQ06VR1MWh6r8q4KAnVTzSWSjD2HsoCqWl4qn5iVAcwAPBaCGJFX2sSfp504eSpjuxovTxBDsl87DA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713529823; c=relaxed/simple; bh=Y0qzuaLHp7jYNZt/yJHBqCPkPqeNLCNBhwMVULVwpAI=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=iijd3QB9nA4GfDsxuDN1ZK3tB8khxXlsOjdVj6C48RZdAbKmOw1XcDVdbgHa8r+n8oMizJlU+UjFBb1RiWSPyqPfIGs1H8UqTvsI9bSZvB3r7SYEhCrB+ppqx0G4dblMIWHhBagS2i2bf/tSWSAU9feFhLsp162m+Oygy9WkH8A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=YaAw2RNs; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713529821; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xUl9xI+kv1Ecrl0dN5hTZc3O707nonguLWXio7bCIq4=; b=YaAw2RNs4xRsISLfLz+296Nf0ShbW60WUPb349jnd73Nr7h4CShlDmwPhRouLX3ViPZkCm lCMuVE50V078McFi3+ZG7rIx1oQuEfZrYnIQYRM7F13a5HLyRWkVYWYylgi8MjW/C9w7u8 Zj/fvVXlopo4X0THRMa0v2rFtVUzqxc= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-9-iux10_i5OjiKSBAZBkX17g-1; Fri, 19 Apr 2024 08:30:19 -0400 X-MC-Unique: iux10_i5OjiKSBAZBkX17g-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-434e545b504so26912551cf.1 for ; Fri, 19 Apr 2024 05:30:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713529819; x=1714134619; h=content-transfer-encoding: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=xUl9xI+kv1Ecrl0dN5hTZc3O707nonguLWXio7bCIq4=; b=aIMVUNIBVU4f7ItdRdAbZYdv7giE7W1Tcp0Q9d9Kfx/pJO2TXk/0NZcSPJ1opzpJci aDaJ6+cKAyzqnXRk/U6aCuMUg+eqnhQAxkCUxZ9EMnMWLb9a1/S5ZLDy7gvZ1DBeByQ8 5DjQbADohpdddCMEVPzanegOAbDIdiOCxSml356WZExLeLVjBIRXqTNqI2Ae86YcSIqq BHa7/z6iLG0SU6ZlaK3S1HQZDXKo0Oj7uH1D/AzrxT3E1m/Fvg7+jvcCXHN3iG8fGt1V cgv7iALhIPQiqzIm7FXx/R3gp492HT/C83CNYDfgWO6vEK0W8r5PN2Vmaki29VKbnjwk +mOg== X-Forwarded-Encrypted: i=1; AJvYcCVWGUvud4SBErcs1Dv+SHhGx624X9IQ3da6EThk752VItTyjrL5nhR91DbkuMG8kcbzXplqD+632RIR3XIpMonAgnpQYYq/E8CiR6fu X-Gm-Message-State: AOJu0YzgD4hRubrg5UcgK9sJ7eTso7O+tTCE/hQ9QO/XsyVHavBlVgy6 l3xDgXn+w4dD3XklNs/GrcBClGxhBW0XVnD0wG27Fp8htLCb7abCz29jKm3Xete/JLwQIr2kkN9 sLtk0FqjHFDhLJVcNgvUY7w36J55jc8TqS24rdWxb/lZBqESug3YNlsoRU8X76DimHK4oMp5adZ 1Q3U0ujZSm6bU5uYC8cm7kcH88P/pF8TlaG3HI X-Received: by 2002:ac8:58c5:0:b0:436:9fc4:6146 with SMTP id u5-20020ac858c5000000b004369fc46146mr2525511qta.57.1713529819033; Fri, 19 Apr 2024 05:30:19 -0700 (PDT) X-Received: by 2002:ac8:58c5:0:b0:436:9fc4:6146 with SMTP id u5-20020ac858c5000000b004369fc46146mr2525485qta.57.1713529818801; Fri, 19 Apr 2024 05:30:18 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240418210236.194190-1-wander@redhat.com> <20240418210236.194190-3-wander@redhat.com> In-Reply-To: From: Wander Lairson Costa Date: Fri, 19 Apr 2024 09:30:06 -0300 Message-ID: Subject: Re: [PATCH v3 2/2] kunit: avoid memory leak on device register error To: David Gow Cc: Brendan Higgins , Rae Moar , Greg Kroah-Hartman , Shuah Khan , Maxime Ripard , Matti Vaittinen , "open list:KERNEL UNIT TESTING FRAMEWORK (KUnit)" , "open list:KERNEL UNIT TESTING FRAMEWORK (KUnit)" , open list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Apr 19, 2024 at 1:59=E2=80=AFAM David Gow wro= te: > > On Fri, 19 Apr 2024 at 05:02, Wander Lairson Costa wr= ote: > > > > If the device register fails, free the allocated memory before > > returning. > > > > Signed-off-by: Wander Lairson Costa > > Fixes: d03c720e03bd ("kunit: Add APIs for managing devices") > > --- > > Thanks. > > I'm not sure this is correct, though... Shouldn't put_device() free this = for us? > > The documentation for device_register() says to never free a device > after device_register() has been called, even if it fails: > https://docs.kernel.org/driver-api/infrastructure.html#c.device_register > > Or am I missing something? > I am not freeing the device object passed to device_register, but its parent structure. As a side note, the behavior of device_register() seems counterintuitive and error-prone, IMO. If the function returns an error, it should ensure it leaks no resource and shouldn't require the caller to do any cleanup. > Cheers, > -- David > > > > lib/kunit/device.c | 11 ++++++----- > > 1 file changed, 6 insertions(+), 5 deletions(-) > > > > diff --git a/lib/kunit/device.c b/lib/kunit/device.c > > index 25c81ed465fb..bc2e2032e505 100644 > > --- a/lib/kunit/device.c > > +++ b/lib/kunit/device.c > > @@ -119,10 +119,8 @@ static struct kunit_device *kunit_device_register_= internal(struct kunit *test, > > kunit_dev->owner =3D test; > > > > err =3D dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name= ); > > - if (err) { > > - kfree(kunit_dev); > > - return ERR_PTR(err); > > - } > > + if (err) > > + goto error; > > > > kunit_dev->dev.release =3D kunit_device_release; > > kunit_dev->dev.bus =3D &kunit_bus_type; > > @@ -131,7 +129,7 @@ static struct kunit_device *kunit_device_register_i= nternal(struct kunit *test, > > err =3D device_register(&kunit_dev->dev); > > if (err) { > > put_device(&kunit_dev->dev); > > - return ERR_PTR(err); > > + goto error; > > } > > > > kunit_dev->dev.dma_mask =3D &kunit_dev->dev.coherent_dma_mask; > > @@ -140,6 +138,9 @@ static struct kunit_device *kunit_device_register_i= nternal(struct kunit *test, > > kunit_add_action(test, device_unregister_wrapper, &kunit_dev->d= ev); > > > > return kunit_dev; > > +error: > > + kfree(kunit_dev); > > + return ERR_PTR(err); > > } > > > > /* > > -- > > 2.44.0 > >