Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1274349rwi; Wed, 26 Oct 2022 13:06:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5qmoIlznTMZ6syYMO1k464QvJz+1BQiR6WnmrULHUVS/fyuYdpQK8svE9s3Z8LpKesUpCp X-Received: by 2002:a17:907:60c8:b0:78d:bc8d:8457 with SMTP id hv8-20020a17090760c800b0078dbc8d8457mr38052590ejc.418.1666814797701; Wed, 26 Oct 2022 13:06:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666814797; cv=none; d=google.com; s=arc-20160816; b=y5PIY4hoduAoQX9s6dtuh5X+SnaqmLtBQ5VTwDs1vtF2OqWsBtJCff9Vr2tHu+jzXL ouOSoac8HCvZj6hAixZ/u2TbkwQMr2VdK4U7xXID5toTzRCvvsWKk5q4FLIHNKsiOwMT 5BOpYMHEfbTrJuCL2asltYZtSyH+4wb6XkZLSaLV3jK9UiGT13E65cPqsX3R4uPvgbSI 21xTd5L3IU8WTd3ZiqwEVLjo9BRUN29xzmLv0S7WO8H+f2xL6lrBqk6JGKqUdN8OZw8j DJEq28y5ntM12ztj4hgHYl4C1ypLmNmjRDN6YySxHGGaKZRP1CKgeTsU9NzahtLWZyeq DHng== 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:date:subject:cc:to:from :dkim-signature; bh=R2ZOrYe1VpBXYn04M+t8OPuDPgxBa02cJDnMZ+dKsEs=; b=FDAPxk6PEclPga/lv9ellb1hc3+WIxN6lMTfkG8pXQb94TP6inpeS35TLuImlGON0I +ONjTlXlESH2ZfI8hfqOKoHTl5Sm2dA0vm/VOnI6uSDCRntHKawOeqvG4UhrFjB6dbl7 8xmzr9+fmwuHbCsuZusFmGWI3jeUPyDNZywBuVIAt010fKtYajaSIBkvTzJUNNQvKzEr TE0RxTufSGQLCf+SRhBTJTw1QZjHl0oovol87NiOEG4efr3AuDpbhEq6EQpeNzLhGy6z pWqiLYL7JZiYzLxMs+IPSmuwL2pxUa18k9ynuMHMiF13cX6j7Sk2iuaC/sX4mFJ0LMmx wtGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b="M+kae/Jv"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hq28-20020a1709073f1c00b0078db137967dsi6798215ejc.566.2022.10.26.13.06.09; Wed, 26 Oct 2022 13:06:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b="M+kae/Jv"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234845AbiJZTlw (ORCPT + 99 others); Wed, 26 Oct 2022 15:41:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235013AbiJZTlj (ORCPT ); Wed, 26 Oct 2022 15:41:39 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE142B978C; Wed, 26 Oct 2022 12:41:38 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 88FCB1FD6C; Wed, 26 Oct 2022 19:41:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1666813297; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R2ZOrYe1VpBXYn04M+t8OPuDPgxBa02cJDnMZ+dKsEs=; b=M+kae/Jv/pCAjxtC8WxpkQ7bU1KNk/44f4tcVMgTefmZqA9KLxU89klgYcqoODkK0qtjpz jSLjSNjXCD+NmJ9kG0u5FVpzDouhF1ubeXUOq0hfL7ZCaMy9+bVOX+ETHzEl7V02sJlh6/ Dsj8wD1CE5ntP82y46AqOvMi80p2mMo= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id AC3E713A77; Wed, 26 Oct 2022 19:41:35 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id oFkcHG+NWWNlFwAAMHmgww (envelope-from ); Wed, 26 Oct 2022 19:41:35 +0000 From: Marcos Paulo de Souza To: linux-kernel@vger.kernel.org, live-patching@vger.kernel.org Cc: jpoimboe@redhat.com, joe.lawrence@redhat.com, pmladek@suse.com, Marcos Paulo de Souza Subject: [PATCH v2 1/4] livepatch/shadow: Separate code to get or use pre-allocated shadow variable Date: Wed, 26 Oct 2022 16:41:19 -0300 Message-Id: <20221026194122.11761-2-mpdesouza@suse.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221026194122.11761-1-mpdesouza@suse.com> References: <20221026194122.11761-1-mpdesouza@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Petr Mladek Separate code that is used in klp_shadow_get_or_alloc() under klp_mutex. It splits a long spaghetti function into two. Also it unifies the error handling. The old used a mix of duplicated code, direct returns, and goto. The new code has only one unlock, free, and return calls. It is code refactoring without any functional changes. Signed-off-by: Petr Mladek Signed-off-by: Marcos Paulo de Souza --- Changes from v1: * Change __klp_shadow_get_or_use to __klp_shadow_get_or_add_locked and add a comment about it's meaning (Petr) * Add lockdep_assert_held on __klp_shadow_get_or_add_locked * Reworked the commit message (Petr) * Added my SoB (Josh) kernel/livepatch/shadow.c | 78 ++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/kernel/livepatch/shadow.c b/kernel/livepatch/shadow.c index c2e724d97ddf..81ad7cbbd124 100644 --- a/kernel/livepatch/shadow.c +++ b/kernel/livepatch/shadow.c @@ -101,41 +101,22 @@ void *klp_shadow_get(void *obj, unsigned long id) } EXPORT_SYMBOL_GPL(klp_shadow_get); -static void *__klp_shadow_get_or_alloc(void *obj, unsigned long id, - size_t size, gfp_t gfp_flags, - klp_shadow_ctor_t ctor, void *ctor_data, - bool warn_on_exist) +/* Check if the variable exists. Otherwise, add the pre-allocated one. */ +static void *__klp_shadow_get_or_add_locked(void *obj, unsigned long id, + struct klp_shadow *new_shadow, + klp_shadow_ctor_t ctor, void *ctor_data, + bool *exist) { - struct klp_shadow *new_shadow; void *shadow_data; - unsigned long flags; - /* Check if the shadow variable already exists */ - shadow_data = klp_shadow_get(obj, id); - if (shadow_data) - goto exists; - - /* - * Allocate a new shadow variable. Fill it with zeroes by default. - * More complex setting can be done by @ctor function. But it is - * called only when the buffer is really used (under klp_shadow_lock). - */ - new_shadow = kzalloc(size + sizeof(*new_shadow), gfp_flags); - if (!new_shadow) - return NULL; + lockdep_assert_held(&klp_shadow_lock); - /* Look for again under the lock */ - spin_lock_irqsave(&klp_shadow_lock, flags); shadow_data = klp_shadow_get(obj, id); if (unlikely(shadow_data)) { - /* - * Shadow variable was found, throw away speculative - * allocation. - */ - spin_unlock_irqrestore(&klp_shadow_lock, flags); - kfree(new_shadow); - goto exists; + *exist = true; + return shadow_data; } + *exist = false; new_shadow->obj = obj; new_shadow->id = id; @@ -145,8 +126,6 @@ static void *__klp_shadow_get_or_alloc(void *obj, unsigned long id, err = ctor(obj, new_shadow->data, ctor_data); if (err) { - spin_unlock_irqrestore(&klp_shadow_lock, flags); - kfree(new_shadow); pr_err("Failed to construct shadow variable <%p, %lx> (%d)\n", obj, id, err); return NULL; @@ -156,12 +135,45 @@ static void *__klp_shadow_get_or_alloc(void *obj, unsigned long id, /* No found, so attach the newly allocated one */ hash_add_rcu(klp_shadow_hash, &new_shadow->node, (unsigned long)new_shadow->obj); - spin_unlock_irqrestore(&klp_shadow_lock, flags); return new_shadow->data; +} + +static void *__klp_shadow_get_or_alloc(void *obj, unsigned long id, + size_t size, gfp_t gfp_flags, + klp_shadow_ctor_t ctor, void *ctor_data, + bool warn_on_exist) +{ + struct klp_shadow *new_shadow; + void *shadow_data; + bool exist; + unsigned long flags; + + /* Check if the shadow variable already exists */ + shadow_data = klp_shadow_get(obj, id); + if (shadow_data) + return shadow_data; + + /* + * Allocate a new shadow variable. Fill it with zeroes by default. + * More complex setting can be done by @ctor function. But it is + * called only when the buffer is really used (under klp_shadow_lock). + */ + new_shadow = kzalloc(size + sizeof(*new_shadow), gfp_flags); + if (!new_shadow) + return NULL; + + /* Look for again under the lock */ + spin_lock_irqsave(&klp_shadow_lock, flags); + shadow_data = __klp_shadow_get_or_add_locked(obj, id, new_shadow, + ctor, ctor_data, &exist); + spin_unlock_irqrestore(&klp_shadow_lock, flags); + + /* Throw away unused speculative allocation. */ + if (!shadow_data || exist) + kfree(new_shadow); -exists: - if (warn_on_exist) { + if (exist && warn_on_exist) { WARN(1, "Duplicate shadow variable <%p, %lx>\n", obj, id); return NULL; } -- 2.37.3