From patchwork Thu Aug 24 11:55:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 716543 Delivered-To: patch@linaro.org Received: by 2002:adf:f747:0:b0:317:ecd7:513f with SMTP id z7csp1289047wrp; Thu, 24 Aug 2023 04:55:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEvgjQ4OLw6Iu7cjhqO7clmmj7OwAyfcTml2H60fJ2iWpi6d9tRiN/v4uOdNXDypUQKCDkH X-Received: by 2002:adf:e888:0:b0:315:a1f9:8667 with SMTP id d8-20020adfe888000000b00315a1f98667mr10306045wrm.23.1692878116267; Thu, 24 Aug 2023 04:55:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692878116; cv=none; d=google.com; s=arc-20160816; b=q5EuYynyvGtoRygkPj/WFZTYxMmbXE8aBVdcaXBpjh+9wndytxdN/DDkuL2gBfY8vr s4plbOqkdw6AGoPdGfo50y5HalxNZyODjtzEK6sCCpRtMEpSohyqdoKxJ2JokfwBnPRY lgpOzZ57irDGrRCK+oPCVIdbcDPHUjk56Mt7E4fo8u+ndnk2I7hJQUtYqy2gukBNVnE2 MjoRnXsj2w6uTQzmYN8Mjlzn5Y3XszoPK83ssXTItgHEgMRBzdWlnuw7IRChwyId/jx+ Y2A7ISbYNTw4X+aVe/hfCp3Img/av98v9Ckzu7Jbv9sFKIxchQ+QI9jJ6Su5UhVC6SCB vPng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=+4nZBQREroHNf1yQ61IgQrl73fmHFFgSo3+NiWRMbew=; fh=djVjLtnhQth8aWbABmos+pGtahnhRSYGFFo6J4sVH/o=; b=iwCl1N3cm09SuVlzzGPvdMgD5dZBUxIY2FYCXvo3iuznyhVv8CxjI8fC0YZHAG05u5 TJU0P7YXtm7ncM/XBknMq0Vy1myVYrdprR5NEOylCYukl69yVXfQWkXisJcq4JLJaztI m/O5wd3Mu50Mj1nyMRYDB3qbLQOb7pnU4yIfkAHC7PGjir7C0PTWX1P+GNBSQkIIh3Iu 83AG/W6/osc0zBoTFduXurlXCSVMXZC6L3gLJdY4oybx+g+jX12gq+JBlA1YnPqJTC3Z rQmn9xA1FcA/DdkmFI3sstunaU3ow1NuQ4yyzWD64UwFHZlEWRKP2skaPmNzJ3vK6/L1 CwUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WrmccGLL; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id w2-20020adfec42000000b0031972d4f3ccsi7208496wrn.983.2023.08.24.04.55.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Aug 2023 04:55:16 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WrmccGLL; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2001B8657C; Thu, 24 Aug 2023 13:55:15 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="WrmccGLL"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 807F5865A6; Thu, 24 Aug 2023 13:55:13 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C3D69864B7 for ; Thu, 24 Aug 2023 13:55:10 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ilias.apalodimas@linaro.org Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-3fef3c3277bso35243575e9.1 for ; Thu, 24 Aug 2023 04:55:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692878110; x=1693482910; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=+4nZBQREroHNf1yQ61IgQrl73fmHFFgSo3+NiWRMbew=; b=WrmccGLLlRND1BdGKOkDmb+R8otsnjiL6jp10dUFlpHW3RJ1nRC6BWuwxartSQyT9y DpzKv7aTziwwUjvGLFZ8BxuXY/YPnitqAA1wuqtM2E1KkballxqXSbUTLvaWHcxacGCE NcUrEWJrPWXde9RZEEGgVorxJQt4f5d2PddlbEOsWZ350zO1n62+XQ1Kg7NIt3Jhv7eI qNjH5CLBBmcjIh47930djHXDzJHJZeJxFL5/jvnv+WpOxixhKKUtoAFdG1LEvUsdwjF8 rlP+qmbeAE9k1cK7D5KuzNqoq6kMUFoSLWyXBxZljrkmu5PO17+3hfxySgSYoYEFKYqO ipag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692878110; x=1693482910; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+4nZBQREroHNf1yQ61IgQrl73fmHFFgSo3+NiWRMbew=; b=h49nFgtX/MHIbEx9yl+2VADrK/f10HMWMvobY3xq7i64DvKGUEwz7d9yFoJiMeccZU RMlAWjIC9s60uHSZNLAjiFI6+Fd3eh0bkSiTjRAKE4CYlthp8GF7TvWS5OGY7m9RUM6y v1MSuF+rx4RO6oh2IEq8T0s/2FC90xXS5EVkLqMGDfy9+sGBGdQqcXuieZ7llIrLMgYm 0tVf7eGpVGfllYPC+NS7NoIGQVtkkttMUEEDb8zOa92YJds2HNYnC2I5TdMzBD/ktTPk Cob9gURQ7+ZDSgibSb2tdSWtGL5wZd5UACeO7Mj13P6B9ELAEBVcBTMV4HUqqUQo3EIC B00A== X-Gm-Message-State: AOJu0YynMHybfBqHRhW0H1PrUIBD6N0zwFsXtwVqa2okkdpdUDCu7kdq buR+DJn5BqvyxuZXMQ1Qwszowt7b3JKPQRtFuQb++A== X-Received: by 2002:a7b:cbcd:0:b0:3fb:fa9f:5292 with SMTP id n13-20020a7bcbcd000000b003fbfa9f5292mr11500243wmi.25.1692878110226; Thu, 24 Aug 2023 04:55:10 -0700 (PDT) Received: from hades.. ([2a02:85f:fcb3:de74:3efd:feff:fe6b:c5ca]) by smtp.gmail.com with ESMTPSA id k25-20020a05600c0b5900b003feea62440bsm2389901wmr.43.2023.08.24.04.55.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Aug 2023 04:55:09 -0700 (PDT) From: Ilias Apalodimas To: u-boot@lists.denx.de, xypron.glpk@gmx.de Cc: Ilias Apalodimas Subject: [PATCH] efi_loader: delete handle from events when a protocol is uninstalled Date: Thu, 24 Aug 2023 14:55:00 +0300 Message-Id: <20230824115500.2388636-1-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean When a notification event is registered for a protocol the handle of the protocol is added in our event notification list. When all the protocols of the handle are uninstalled we delete the handle but we do not remove it from the event notification list. Clean up the protocol removal functions and add a wrapper which - Removes the to-be deleted handle from any lists it participates - Remove the handle if no more protocols are present Signed-off-by: Ilias Apalodimas --- lib/efi_loader/efi_boottime.c | 80 ++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c index 0e89c8505b11..96950790ba2a 100644 --- a/lib/efi_loader/efi_boottime.c +++ b/lib/efi_loader/efi_boottime.c @@ -61,7 +61,7 @@ static volatile gd_t *efi_gd, *app_gd; static efi_status_t efi_uninstall_protocol (efi_handle_t handle, const efi_guid_t *protocol, - void *protocol_interface); + void *protocol_interface, bool preserve); /* 1 if inside U-Boot code, 0 if inside EFI payload code */ static int entry_count = 1; @@ -207,6 +207,36 @@ static bool efi_event_is_queued(struct efi_event *event) return !!event->queue_link.next; } +/** + * efi_purge_handle() - Clean the deleted handle from the various lists + * @handle: handle to remove + * + * Return: status code + */ +static efi_status_t efi_purge_handle(efi_handle_t handle) +{ + struct efi_register_notify_event *item, *next; + + if (!list_empty(&handle->protocols)) + return EFI_ACCESS_DENIED; + /* The handle is about to be freed. Remove it from events */ + list_for_each_entry_safe(item, next, &efi_register_notify_events, link) { + struct efi_protocol_notification *hitem, *hnext; + + list_for_each_entry_safe(hitem, hnext, &item->handles, link) { + if (handle == hitem->handle) { + list_del(&hitem->link); + free(hitem); + } + } + } + /* The last protocol has been removed, delete the handle. */ + list_del(&handle->link); + free(handle); + + return EFI_SUCCESS; +} + /** * efi_process_event_queue() - process event queue */ @@ -615,7 +645,7 @@ static efi_status_t efi_remove_all_protocols(const efi_handle_t handle) efi_status_t ret; ret = efi_uninstall_protocol(handle, &protocol->guid, - protocol->protocol_interface); + protocol->protocol_interface, true); if (ret != EFI_SUCCESS) return ret; } @@ -639,10 +669,7 @@ efi_status_t efi_delete_handle(efi_handle_t handle) return ret; } - list_del(&handle->link); - free(handle); - - return ret; + return efi_purge_handle(handle); } /** @@ -1356,6 +1383,8 @@ reconnect: * @handle: handle from which the protocol shall be removed * @protocol: GUID of the protocol to be removed * @protocol_interface: interface to be removed + * @preserve: preserve or delete the handle and remove it from any + * list it participates if no protocols remain * * This function DOES NOT delete a handle without installed protocol. * @@ -1363,7 +1392,7 @@ reconnect: */ static efi_status_t efi_uninstall_protocol (efi_handle_t handle, const efi_guid_t *protocol, - void *protocol_interface) + void *protocol_interface, bool preserve) { struct efi_handler *handler; struct efi_open_protocol_info_item *item; @@ -1397,6 +1426,14 @@ static efi_status_t efi_uninstall_protocol goto out; } r = efi_remove_protocol(handle, protocol, protocol_interface); + if (r != EFI_SUCCESS) + return r; + /* + * We don't care about the return value here since the + * handle might have more protocols installed + */ + if (!preserve) + efi_purge_handle(handle); out: return r; } @@ -1422,15 +1459,10 @@ static efi_status_t EFIAPI efi_uninstall_protocol_interface EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface); - ret = efi_uninstall_protocol(handle, protocol, protocol_interface); + ret = efi_uninstall_protocol(handle, protocol, protocol_interface, false); if (ret != EFI_SUCCESS) goto out; - /* If the last protocol has been removed, delete the handle. */ - if (list_empty(&handle->protocols)) { - list_del(&handle->link); - free(handle); - } out: return EFI_EXIT(ret); } @@ -2785,7 +2817,7 @@ static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle, efi_va_list argptr) { - const efi_guid_t *protocol; + const efi_guid_t *protocol, *next_protocol; void *protocol_interface; efi_status_t ret = EFI_SUCCESS; size_t i = 0; @@ -2795,25 +2827,25 @@ efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle, return EFI_INVALID_PARAMETER; efi_va_copy(argptr_copy, argptr); + protocol = efi_va_arg(argptr, efi_guid_t*); for (;;) { - protocol = efi_va_arg(argptr, efi_guid_t*); + bool preserve = true; + if (!protocol) break; protocol_interface = efi_va_arg(argptr, void*); + next_protocol = efi_va_arg(argptr, efi_guid_t*); + if (!next_protocol) + preserve = false; ret = efi_uninstall_protocol(handle, protocol, - protocol_interface); + protocol_interface, preserve); if (ret != EFI_SUCCESS) break; i++; + protocol = next_protocol; } - if (ret == EFI_SUCCESS) { - /* If the last protocol has been removed, delete the handle. */ - if (list_empty(&handle->protocols)) { - list_del(&handle->link); - free(handle); - } + if (ret == EFI_SUCCESS) goto out; - } /* If an error occurred undo all changes. */ for (; i; --i) { @@ -3712,7 +3744,7 @@ static efi_status_t EFIAPI efi_reinstall_protocol_interface( new_interface); /* Uninstall protocol but do not delete handle */ - ret = efi_uninstall_protocol(handle, protocol, old_interface); + ret = efi_uninstall_protocol(handle, protocol, old_interface, true); if (ret != EFI_SUCCESS) goto out;