From patchwork Tue Sep 10 13:15:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Calabrese X-Patchwork-Id: 3420 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-lf1-f69.google.com (mail-lf1-f69.google.com [209.85.167.69]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 5ECCD3F263 for ; Tue, 10 Sep 2024 15:25:03 +0200 (CEST) Received: by mail-lf1-f69.google.com with SMTP id 2adb3069b0e04-5356a05fcd8sf4346988e87.0 for ; Tue, 10 Sep 2024 06:25:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1725974702; cv=pass; d=google.com; s=arc-20240605; b=R678Yx91W8rEz2UgtLQG6JPVW09NnFYuDdtX63TY+1SkWD2DfHPzStPjQLnqlFKV1K O5jZ7y6gBQm7jhUco1m/fCSWtLibE0PGNkhIbK0YpM5G8iSHtf8ssRxwR5yyaUJ/V5IB yzoxWJceFxHnRZ7l14soOiFPnxVD2roQiZ1kpxVBVMg7Nfsbp2jnwdYwlYjQsGMmL+2O O5L3DyX3RoHL7vJ6MESqK01q3Yfpde96k+hUVuCt/9CnctDpzALMmAuBg1nSFXDubl1Q o+va/U4SuDP8rx5HwLgyJ1+AVlEpQgOrkmpiLBcHK13XNsGurSItIPhp8TtxL9DzA1lm 6n+w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:message-id:date:subject:cc:to :from:dkim-signature; bh=4jLFO4Zlr6hR1ST7FwsPeBjTzwaK17MbdQihWQb67Rg=; fh=MiaX+/l12glI/o/2kInkZw9ojTu7eyL2Z/Qsu/Gps3k=; b=TJy/MCAcr9NG3R/4BWCLXt736mJEs6TXtOZRX7+4924R1NTPJ2wceuas3MEKcNn9VY lPorrJiSMQI7d3KqqwQtYgJ3xExHET3Xl2B+INrk2AcrDnNhzF1x1jTtHZ3hJyzbwXYB J9ofROet6pWUCpe8rfbX8ep8K2S5sJONNuoswbxaVBdoyJDRezhCO/y9uN6tX6Ia8KCO sGvibePBt8xPypatjFr2lq/3QmOqVkz4u7xJgLNBsJghCiRg4eLaRMeXlD7zWtAcNlJX rGGiEJFxwja4PRaS76zN/9djIBCpnlmmvY3sQ2LgI7vRzmO/VQotCZ71rQ+pgJfBpHmr yLrg==; darn=patchwork.amarulasolutions.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=hEF7U9p1; spf=pass (google.com: domain of andrea.calabrese@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=andrea.calabrese@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=pass header.i=@amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; t=1725974702; x=1726579502; darn=patchwork.amarulasolutions.com; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:message-id:date:subject:cc:to:from :from:to:cc:subject:date:message-id:reply-to; bh=4jLFO4Zlr6hR1ST7FwsPeBjTzwaK17MbdQihWQb67Rg=; b=GOiCkArlYPnJGNJ2EsV2tqXQGdtvDHB3j1TZLnVVimgn6xiXeOxn9W2oLvctR4J+lQ 5IbhOtp4tzcFgZQPL0t9c6sO6uSESS26kHq8QLwqBwmwEmQqrdR7nHMHY6Fcon4oz8J6 JpWX4GUxRaxsMFzyOynRc55khXzTpGDGchde0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725974702; x=1726579502; h=list-unsubscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :message-id:date:subject:cc:to:from:x-beenthere:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=4jLFO4Zlr6hR1ST7FwsPeBjTzwaK17MbdQihWQb67Rg=; b=aBjkn2HrgVbVenvkWyEUPrMIYk1eRLqCBxSUjUUaY05JUw8L7HRiT0+Wl8Qa/vNPw9 3y+b+3tdgqSvT+s9REZzALgPP0zdi25Hh1r/4BdcPAq6QJdarK4vmyalD4R+q9mO1vnJ zLN9vvqt3VcF8+XT83nXwJpy++bjxDrf5W8hZPYkcV2BgZ4F2lBRtEuyJN5yPMUeV3lS b64GvGOVoG++Z2CXK+He+bZVQJ+D64NAVDny1LlI0XRc9LiWa37RPULXXclEL7Or3FTe +oR5sH8viLtF6KNU/iUY1P9XjIdPAy1P2DgeI7n7RUfq0YZ9+crGp8T4HLx0retyXy54 +NWw== X-Forwarded-Encrypted: i=2; AJvYcCWWMCLRcKxnGzNrOHEsHSxxxz06xyMFwWL/5rhY6K87AoIVvTeuD6ffNtYtUr1+UOrELp9ujUQ5OPDYtlM6@patchwork.amarulasolutions.com X-Gm-Message-State: AOJu0Yx5Zy4WIjn6j0BJSO7l+L6If4tMm6yUPfZoFBrDkU0Nr2atPjJK Om6gSTPry7wJ/4d4QtFOfEYYbCFyIpnUckDq3J0iuH+I2s5wYkhiFqXnCsQVfzpBNg== X-Google-Smtp-Source: AGHT+IGjwWTQLo2ctwtKax4JN60M0DGaDlXFPyHNzKmhT1e0zEvqUC4c/J1Bd51v/q0EYdaGa+RAVA== X-Received: by 2002:a05:651c:2209:b0:2f7:5890:b15a with SMTP id 38308e7fff4ca-2f75890b4ffmr83392221fa.38.1725974702119; Tue, 10 Sep 2024 06:25:02 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a2e:2a83:0:b0:2f3:ee66:7ce with SMTP id 38308e7fff4ca-2f75111133fls9325701fa.1.-pod-prod-04-eu; Tue, 10 Sep 2024 06:25:00 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUogqtTc6dyrbgb5mYutOFgUOw47gQCIqxJqD0XStp9aklUyl5O5UPbi/RJUtAPUnWqX/RjP4iEYfhnKrMb@amarulasolutions.com X-Received: by 2002:a05:6512:3d1a:b0:535:6bb6:e7bd with SMTP id 2adb3069b0e04-536587b8c50mr10444430e87.28.1725974699882; Tue, 10 Sep 2024 06:24:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1725974699; cv=none; d=google.com; s=arc-20160816; b=05zKBzpNFcpjbVfEwC3zvdwvT/eCz7U27/3PSdo7FZr8tYP5uLHQya+/Mle7J071F3 0uUjVjn7XBTp9V53QxE71KtP+YaIizRaB8YFe1WjfA7PD+DR/zbFKtEE3vu6SQ6wvEYg k/FiBe3qYoKuud6p9Wl6RSFTPQh1rYh9lB3zfO48sCVsMaV1esNbu8kPIP7DTfK/gxIW akoDWObZLI30b5gHiISN7wnVlugMiCRSC7cUe6HtSh2OVqStP1NXETQkSkYvLcP+plvH cOKgwuHtvXIMP2xdhpbUwMK505LIU42HFuIfqvSDvIPoPywvuyvhr8U8jc9SchQ6kalw EFNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=eGZtZlbcVTuqfHipICSHcU4nVCOOqo16078EY8ojHwg=; fh=v30553OEZ37YEVP4WOCO3zYCqaXy053l+faP/v3Q6sg=; b=Vjqmmw8t/ZK4umHiehMXE0bUmyXo38pyVDAyMe13K1WhH3kzkdPGF0NOBpRlTKlw2r UoFViM/xTEmcwhFQmjAZMy1yui629xIXcE7i1hW1rw7j5xN8vtuXa9EMZxfDR5EvkX/L Bt2UPFCfe+xTo/7JDlUiioqyWOD9v599iQnrMIV62I8nk2XlOtPA6P/NJxvPgZZ/ULs0 Ndd9N8ylf/xnTU6zIq5TSxfYuUcK9Lw2jmKWW3i5qIngzC0dyNjNzJ+x1u9hBnmCf3i/ VKpNehx++32q7tN3+3Kw3g9UfOJ9Yi/3+Io5HWJNZySlWYootMgEk20gd3I/xDIxAYUx unKQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=hEF7U9p1; spf=pass (google.com: domain of andrea.calabrese@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=andrea.calabrese@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=pass header.i=@amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id 2adb3069b0e04-5365f867eecsor2009270e87.3.2024.09.10.06.24.59 for (Google Transport Security); Tue, 10 Sep 2024 06:24:59 -0700 (PDT) Received-SPF: pass (google.com: domain of andrea.calabrese@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Forwarded-Encrypted: i=1; AJvYcCWMpuytc6eXYrL/wDERE/2S85X1nnelZ0OEZARmHXQRRJmXqJwiEoMinq4FhKfL/AfmnCBOXz+NkRqLpl2R@amarulasolutions.com X-Received: by 2002:a05:6512:1283:b0:533:4820:275a with SMTP id 2adb3069b0e04-536588130c0mr11624002e87.52.1725974699152; Tue, 10 Sep 2024 06:24:59 -0700 (PDT) Received: from andrea-amarula.homenet.telecomitalia.it ([95.236.207.79]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c3ebd76ef0sm4274900a12.63.2024.09.10.06.24.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Sep 2024 06:24:58 -0700 (PDT) From: Andrea Calabrese To: gregkh@linuxfoundation.org, rafael@kernel.org, dri-devel@lists.freedesktop.org, l.stach@pengutronix.de Cc: trivial@kernel.org, linux-amarula@amarulasolutions.com, andrea.calabrese@amarulasolutions.com Subject: [RESEND PATCH v4] devres: Refactor using guards Date: Tue, 10 Sep 2024 15:15:21 +0200 Message-ID: <20240910131520.150610-2-andrea.calabrese@amarulasolutions.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Original-Sender: andrea.calabrese@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=hEF7U9p1; spf=pass (google.com: domain of andrea.calabrese@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=andrea.calabrese@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=pass header.i=@amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Code refactoring using the recent guard and scoped_guard macros for automatic cleanup of the spinlocks. This does not change the effective behaviour of the kernel, but guarantees a cleaned-up exit from each lock, automatically avoiding potential deadlocks. Signed-off-by: Andrea Calabrese --- Diff from V3: as Greg KH and Lucas Stach noticed, there was a behavioural change between the two versions: I used guard(spinlock), while the expected behaviour should have come from a spinlock_irqsave guard. This has been fixed. diff --git a/drivers/base/devres.c b/drivers/base/devres.c index 3df0025d12aa..4872756426dd 100644 --- a/drivers/base/devres.c +++ b/drivers/base/devres.c @@ -194,14 +194,12 @@ void devres_for_each_res(struct device *dev, dr_release_t release, { struct devres_node *node; struct devres_node *tmp; - unsigned long flags; if (!fn) return; - spin_lock_irqsave(&dev->devres_lock, flags); - list_for_each_entry_safe_reverse(node, tmp, - &dev->devres_head, entry) { + guard(spinlock_irqsave)(&dev->devres_lock); + list_for_each_entry_safe_reverse(node, tmp, &dev->devres_head, entry) { struct devres *dr = container_of(node, struct devres, node); if (node->release != release) @@ -210,7 +208,6 @@ void devres_for_each_res(struct device *dev, dr_release_t release, continue; fn(dev, dr->data, data); } - spin_unlock_irqrestore(&dev->devres_lock, flags); } EXPORT_SYMBOL_GPL(devres_for_each_res); @@ -243,11 +240,9 @@ EXPORT_SYMBOL_GPL(devres_free); void devres_add(struct device *dev, void *res) { struct devres *dr = container_of(res, struct devres, data); - unsigned long flags; - spin_lock_irqsave(&dev->devres_lock, flags); + guard(spinlock_irqsave)(&dev->devres_lock); add_dr(dev, &dr->node); - spin_unlock_irqrestore(&dev->devres_lock, flags); } EXPORT_SYMBOL_GPL(devres_add); @@ -287,11 +282,8 @@ void * devres_find(struct device *dev, dr_release_t release, dr_match_t match, void *match_data) { struct devres *dr; - unsigned long flags; - - spin_lock_irqsave(&dev->devres_lock, flags); + guard(spinlock_irqsave)(&dev->devres_lock); dr = find_dr(dev, release, match, match_data); - spin_unlock_irqrestore(&dev->devres_lock, flags); if (dr) return dr->data; @@ -318,16 +310,14 @@ void * devres_get(struct device *dev, void *new_res, { struct devres *new_dr = container_of(new_res, struct devres, data); struct devres *dr; - unsigned long flags; - - spin_lock_irqsave(&dev->devres_lock, flags); - dr = find_dr(dev, new_dr->node.release, match, match_data); - if (!dr) { - add_dr(dev, &new_dr->node); - dr = new_dr; - new_res = NULL; + scoped_guard(spinlock_irqsave, &dev->devres_lock) { + dr = find_dr(dev, new_dr->node.release, match, match_data); + if (!dr) { + add_dr(dev, &new_dr->node); + dr = new_dr; + new_res = NULL; + } } - spin_unlock_irqrestore(&dev->devres_lock, flags); devres_free(new_res); return dr->data; @@ -353,15 +343,13 @@ void * devres_remove(struct device *dev, dr_release_t release, dr_match_t match, void *match_data) { struct devres *dr; - unsigned long flags; - - spin_lock_irqsave(&dev->devres_lock, flags); - dr = find_dr(dev, release, match, match_data); - if (dr) { - list_del_init(&dr->node.entry); - devres_log(dev, &dr->node, "REM"); + scoped_guard(spinlock_irqsave, &dev->devres_lock) { + dr = find_dr(dev, release, match, match_data); + if (dr) { + list_del_init(&dr->node.entry); + devres_log(dev, &dr->node, "REM"); + } } - spin_unlock_irqrestore(&dev->devres_lock, flags); if (dr) return dr->data; @@ -516,7 +504,6 @@ static void release_nodes(struct device *dev, struct list_head *todo) */ int devres_release_all(struct device *dev) { - unsigned long flags; LIST_HEAD(todo); int cnt; @@ -528,9 +515,9 @@ int devres_release_all(struct device *dev) if (list_empty(&dev->devres_head)) return 0; - spin_lock_irqsave(&dev->devres_lock, flags); - cnt = remove_nodes(dev, dev->devres_head.next, &dev->devres_head, &todo); - spin_unlock_irqrestore(&dev->devres_lock, flags); + scoped_guard(spinlock_irqsave, &dev->devres_lock) { + cnt = remove_nodes(dev, dev->devres_head.next, &dev->devres_head, &todo); + } release_nodes(dev, &todo); return cnt; @@ -552,7 +539,6 @@ int devres_release_all(struct device *dev) void * devres_open_group(struct device *dev, void *id, gfp_t gfp) { struct devres_group *grp; - unsigned long flags; grp = kmalloc(sizeof(*grp), gfp); if (unlikely(!grp)) @@ -568,9 +554,8 @@ void * devres_open_group(struct device *dev, void *id, gfp_t gfp) if (id) grp->id = id; - spin_lock_irqsave(&dev->devres_lock, flags); + guard(spinlock_irqsave)(&dev->devres_lock); add_dr(dev, &grp->node[0]); - spin_unlock_irqrestore(&dev->devres_lock, flags); return grp->id; } EXPORT_SYMBOL_GPL(devres_open_group); @@ -609,17 +594,14 @@ static struct devres_group * find_group(struct device *dev, void *id) void devres_close_group(struct device *dev, void *id) { struct devres_group *grp; - unsigned long flags; - spin_lock_irqsave(&dev->devres_lock, flags); + guard(spinlock_irqsave)(&dev->devres_lock); grp = find_group(dev, id); if (grp) add_dr(dev, &grp->node[1]); else WARN_ON(1); - - spin_unlock_irqrestore(&dev->devres_lock, flags); } EXPORT_SYMBOL_GPL(devres_close_group); @@ -635,19 +617,16 @@ EXPORT_SYMBOL_GPL(devres_close_group); void devres_remove_group(struct device *dev, void *id) { struct devres_group *grp; - unsigned long flags; - - spin_lock_irqsave(&dev->devres_lock, flags); - - grp = find_group(dev, id); - if (grp) { - list_del_init(&grp->node[0].entry); - list_del_init(&grp->node[1].entry); - devres_log(dev, &grp->node[0], "REM"); - } else - WARN_ON(1); - spin_unlock_irqrestore(&dev->devres_lock, flags); + scoped_guard(spinlock_irqsave, &dev->devres_lock) { + grp = find_group(dev, id); + if (grp) { + list_del_init(&grp->node[0].entry); + list_del_init(&grp->node[1].entry); + devres_log(dev, &grp->node[0], "REM"); + } else + WARN_ON(1); + } kfree(grp); } @@ -668,11 +647,10 @@ EXPORT_SYMBOL_GPL(devres_remove_group); int devres_release_group(struct device *dev, void *id) { struct devres_group *grp; - unsigned long flags; LIST_HEAD(todo); int cnt = 0; - spin_lock_irqsave(&dev->devres_lock, flags); + guard(spinlock_irqsave)(&dev->devres_lock); grp = find_group(dev, id); if (grp) { @@ -683,12 +661,9 @@ int devres_release_group(struct device *dev, void *id) end = grp->node[1].entry.next; cnt = remove_nodes(dev, first, end, &todo); - spin_unlock_irqrestore(&dev->devres_lock, flags); - release_nodes(dev, &todo); } else { WARN_ON(1); - spin_unlock_irqrestore(&dev->devres_lock, flags); } return cnt; @@ -860,7 +835,6 @@ void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp) { size_t total_new_size, total_old_size; struct devres *old_dr, *new_dr; - unsigned long flags; if (unlikely(!new_size)) { devm_kfree(dev, ptr); @@ -906,20 +880,17 @@ void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp) * The spinlock protects the linked list against concurrent * modifications but not the resource itself. */ - spin_lock_irqsave(&dev->devres_lock, flags); + scoped_guard(spinlock_irqsave, &dev->devres_lock) { + old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr); + if (!old_dr) { + kfree(new_dr); + WARN(1, "Memory chunk not managed or managed by a different device."); + return NULL; + } - old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr); - if (!old_dr) { - spin_unlock_irqrestore(&dev->devres_lock, flags); - kfree(new_dr); - WARN(1, "Memory chunk not managed or managed by a different device."); - return NULL; + replace_dr(dev, &old_dr->node, &new_dr->node); } - replace_dr(dev, &old_dr->node, &new_dr->node); - - spin_unlock_irqrestore(&dev->devres_lock, flags); - /* * We can copy the memory contents after releasing the lock as we're * no longer modifying the list links.