Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp1784619rdh; Sat, 25 Nov 2023 03:05:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IHrsgNrkwDRLgQjxNSrz5okld9B2K9FXMTFmZmKZytbv+Sh0MvCiQo/Ri+Hl1ETVBhQQ4JF X-Received: by 2002:a05:6808:4d9:b0:3b2:e799:97f6 with SMTP id a25-20020a05680804d900b003b2e79997f6mr5471721oie.59.1700910315017; Sat, 25 Nov 2023 03:05:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700910314; cv=none; d=google.com; s=arc-20160816; b=LiVxXwOoI0gx2N1kkQT5ovA9pkKc7CwSP8Y96Un3X54f2H2c+x1CsHg9KIWmAPvkP9 EGfD0F/vtvL6G+vAhj+A4DSIcvTyWozaznil+Qkf51YFpLpkU9is6IjC/ln8YK5Z7Xy3 Iiq+D8zEDdu3fpe+NSMBJ63wHJKdCtm+aaks5/MWLS4PPpParuYJaidflMAbPfWhyQJE bT3HAwiIsceXT22afAB06Nm/7vx7uhq9GLMxT/ye6J1m0cnqQMH4lxMur+2IrXhEN2NO 2XUqXImXJVFnOUOn2EgS4rtZis99dRNamtV2WCPGRvsKfOfHfalszfAyt8aFWB9VvRky 5rqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=K+Oy7Jg/cMoukGdFIf+aH3GcFiZ7ePa90gsJBk8Sf/s=; fh=Fyc7PmDmxc/7Qw8vVZ5SBPqcc20Wno8iNMaKIF2blGQ=; b=MxJ5HUhi6cCWHDikbdIkuJcTJDGu47rXWh5k6mD5EjEMDLwzOqZiFOq2qs1oRteKu1 J2jZb+XY5ZA1+Tdss8pmHjPNBI6pnGycz0b/dI5HJA4dN7fJXqLc4V3qgvOpITQz61q6 gSDH0LGP6/q4lUfJ10cqReVNwKR/CPZYdJAwH5XR8TCN7eu/C+ptDqJGYhwl7HV3SR0E JVyVnaayiRQBfuDVP+eI3xJsWhhzrBTo3efTPVWP8MLczl+UAf/NuijiZ0wYtkRopUNv v26N0/UNCJUhBUcQy+dWL0/CEa8OKSgYoL1pA6uqJrEb3Jub4efL5PE3cQwal3Ze7rcL VjLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PXFUqihI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id n18-20020a634d52000000b0055fd1bfb109si5327502pgl.679.2023.11.25.03.05.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Nov 2023 03:05:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PXFUqihI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 21B808022A9C; Sat, 25 Nov 2023 03:05:12 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231802AbjKYLEg (ORCPT + 99 others); Sat, 25 Nov 2023 06:04:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229483AbjKYLEf (ORCPT ); Sat, 25 Nov 2023 06:04:35 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68C65D41 for ; Sat, 25 Nov 2023 03:04:41 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B0394C433C8; Sat, 25 Nov 2023 11:04:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700910280; bh=iHtkiWIlP9Suyz7+kfNszP0CzobcR8FxFyBaHHZgiYE=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=PXFUqihIY59L6NIWcHYWEn6ohGfZtRjl66YkDrSyhNaET4i2wW274KWckT+KBH2h0 n0DN3281ENFy+tGUECdGgBjvD2PJ6rg1jVn7pNdA2ZygSb6Lso/Ds0l2jt+MeKRTxm kw2Dj/buKwQcH74k8eBjwmC0sDqj9VtEv4a4Du+vGE0D3iW2YmE3Ek0G2tJMnrwCER VWmzgLeKnD4W8xPypwo7IA3d1c8Jc1xg5/NNsmZdnjnml3kOxOmYnY8cDdGHiHmfxD BLc6eyhUp2wcKYRehbZge92OSkDy7Qfq4i8tbNeyIGqoLam7wkUMeN5RX2Cv/K7ley WL6i5Qhhe7SBg== Date: Sat, 25 Nov 2023 11:04:31 +0000 From: Jonathan Cameron To: George Stark Cc: Andy Shevchenko , , , , , , , , , , Subject: Re: [PATCH 0/8] devm_led_classdev_register() usage problem Message-ID: <20231125110431.0bfebe0d@jic23-huawei> In-Reply-To: <13cd5524-0d40-4f07-b542-002b79b37533@salutedevices.com> References: <20231025130737.2015468-1-gnstark@salutedevices.com> <13cd5524-0d40-4f07-b542-002b79b37533@salutedevices.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Sat, 25 Nov 2023 03:05:12 -0800 (PST) On Sat, 25 Nov 2023 03:47:41 +0300 George Stark wrote: > Hello Andy > > Thanks for the review. > > On 11/24/23 18:28, Andy Shevchenko wrote: > > On Wed, Oct 25, 2023 at 04:07:29PM +0300, George Stark wrote: > >> Lots of drivers use devm_led_classdev_register() to register their led objects > >> and let the kernel free those leds at the driver's remove stage. > >> It can lead to a problem due to led_classdev_unregister() > >> implementation calls led_set_brightness() to turn off the led. > >> led_set_brightness() may call one of the module's brightness_set callbacks. > >> If that callback uses module's resources allocated without using devm funcs() > >> then those resources will be already freed at module's remove() callback and > >> we may have use-after-free situation. > >> > >> Here is an example: > >> > >> module_probe() > >> { > >> devm_led_classdev_register(module_brightness_set_cb); > >> mutex_init(&mutex); > >> } > >> > >> module_brightness_set_cb() > >> { > >> mutex_lock(&mutex); > >> do_set_brightness(); > >> mutex_unlock(&mutex); > >> } > >> > >> module_remove() > >> { > >> mutex_destroy(&mutex); > >> } > >> > >> at rmmod: > >> module_remove() > >> ->mutex_destroy(&mutex); > >> devres_release_all() > >> ->led_classdev_unregister(); > >> ->led_set_brightness(); > >> ->module_brightness_set_cb(); > >> ->mutex_lock(&mutex); /* use-after-free */ > >> > >> I think it's an architectural issue and should be discussed thoroughly. > >> Some thoughts about fixing it as a start: > >> 1) drivers can use devm_led_classdev_unregister() to explicitly free leds before > >> dependend resources are freed. devm_led_classdev_register() remains being useful > >> to simplify probe implementation. > >> As a proof of concept I examined all drivers from drivers/leds and prepared > >> patches where it's needed. Sometimes it was not as clean as just calling > >> devm_led_classdev_unregister() because several drivers do not track > >> their leds object at all - they can call devm_led_classdev_register() and drop the > >> returned pointer. In that case I used devres group API. > >> > >> Drivers outside drivers/leds should be checked too after discussion. > >> > >> 2) remove led_set_brightness from led_classdev_unregister() and force the drivers > >> to turn leds off at shutdown. May be add check that led's brightness is 0 > >> at led_classdev_unregister() and put a warning to dmesg if it's not. > >> Actually in many cases it doesn't really need to turn off the leds manually one-by-one > >> if driver shutdowns whole led controller. For the last case to disable the warning > >> new flag can be brought in e.g LED_AUTO_OFF_AT_SHUTDOWN (similar to LED_RETAIN_AT_SHUTDOWN). > > > > NAK. > > > > Just fix the drivers by wrapping mutex_destroy() into devm, There are many > > doing so. You may be brave enough to introduce devm_mutex_init() somewhere > > in include/linux/device* > > > > Just one thing about mutex_destroy(). It seems like there's no single > opinion on should it be called in 100% cases e.g. in remove() paths. > For example in iio subsystem Jonathan suggests it can be dropped in > simple cases: https://www.spinics.net/lists/linux-iio/msg73423.html > > So the question is can we just drop mutex_destroy() in module's remove() > callback here if that mutex is needed for devm subsequent callbacks? I've never considered it remotely critical. The way IIO works means that things have gone pretty horribly wrong in the core if you managed to access a mutex after the unwind of devm_iio_device_register() has completed but sure, add a devm_mutex_init() and I'd happily see that adopted in IIO for consistency and to avoid answering questions on whether it is necessary to call mutex_destroy() My arguement has always eben that if line after(ish) a mutex_destroy() is going to either free the memory it's in, or make it otherwise inaccessible (IIO is proxying accesses via chardevs if there are open so should ensure they never hit the driver) then it's pointless and messy to call mutex_destroy(). devm_mutex_init() gets rid of that mess.. Jonathan >