Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1081887ybe; Thu, 5 Sep 2019 10:02:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqz17J9/zLFC9lLbNMFbsuQjdufTxO/cEoTJ6Mll3VhyXYa5m/rDb+amo4leLeTrKMLd5Dpr X-Received: by 2002:a17:902:6b88:: with SMTP id p8mr4435540plk.95.1567702922500; Thu, 05 Sep 2019 10:02:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567702922; cv=none; d=google.com; s=arc-20160816; b=BBIaBoYdWI8fCoeiJU96OV8v7zxnhN4pAQMj10N3d7nwePSDxMLlz22UtUu/5ZR+IZ jVQLkfw5wOCaZsiFxD3kZB8rOk2Q3R9uxtiQSlMudmZDYpPEkD/8OL/3qnJI3pIynum6 +ewCdfNJ9EXHhPq0oM72B9kQh95drotOwgyyIb1PFbkFsLEFtQQ7vo7xK57xeE+QI5DT ODnMArImDuCVUNWDQgrGtN0+VtihfE6NCiHPuuCvBaWWkXWFZOZZ5ovDOAmCUMOyVmG3 yXk1wkg7mCbZlb3JJ2O+L1zycuJ3oTTLIaX4FBvBd7uQ/fGo4l8lEQlz+fv20bglybEC ziSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=WYPdKgjYfyDe15dl+gbbGmxISp1OqCaRgEnJrFhfdqU=; b=YLpMWkebbzd2QdFgqjNDQK7msV2Z8xNdt3bqkpXhQF3oePXvu/fb04CtxdoEa0EwnF qBoSlTzK3uwL1rKyksuDmOMnQyaHJ5pLQ7iD4KC5bLQf7MU3ZTMQWU6rUAxsgBWP7pu4 dVGb31NfqNxUOVJ6yGKhVqoWvISU2kYEX2VfIe8ljLO5yI366HX2wcxiRvdGlgstR4Z0 oHAB4K3K74JnFZEJ9ILG4xbcuJy7iT6usF9dZj+5ACYG7qHnuVIoamCWcrykEv5DCxEv M4V5UgYci14Q5ls0O1w1UT6XS4d9CNx27qV6Y2LvC57tvRTLRoLlbNrTt16fIryFXH7B ipFQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n190si2249357pga.33.2019.09.05.10.01.45; Thu, 05 Sep 2019 10:02:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389510AbfIEMp0 (ORCPT + 99 others); Thu, 5 Sep 2019 08:45:26 -0400 Received: from mx2.suse.de ([195.135.220.15]:40678 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1731492AbfIEMpT (ORCPT ); Thu, 5 Sep 2019 08:45:19 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 42A42B658; Thu, 5 Sep 2019 12:45:17 +0000 (UTC) From: Miroslav Benes To: jikos@kernel.org, jpoimboe@redhat.com, pmladek@suse.com Cc: joe.lawrence@redhat.com, nstange@suse.de, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Miroslav Benes Subject: [RFC PATCH v2 2/3] livepatch: Unify functions for writing and clearing object relocations Date: Thu, 5 Sep 2019 14:45:13 +0200 Message-Id: <20190905124514.8944-3-mbenes@suse.cz> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190905124514.8944-1-mbenes@suse.cz> References: <20190905124514.8944-1-mbenes@suse.cz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Functions klp_write_object_relocations() and klp_clear_object_relocations() share a lot of code. Take the code out to a common function and provide the specific actions through callbacks. Signed-off-by: Miroslav Benes --- kernel/livepatch/core.c | 84 +++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 50 deletions(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index f0b380d2a17a..023c9333c276 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -245,8 +245,11 @@ static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod) return 0; } -static int klp_write_object_relocations(struct module *pmod, - struct klp_object *obj) +typedef int (reloc_update_fn_t)(struct module *, unsigned int); + +static int klp_update_object_relocations(struct module *pmod, + struct klp_object *obj, + reloc_update_fn_t reloc_update_fn) { int i, cnt, ret = 0; const char *objname, *secname; @@ -281,13 +284,7 @@ static int klp_write_object_relocations(struct module *pmod, if (strcmp(objname, sec_objname)) continue; - ret = klp_resolve_symbols(sec, pmod); - if (ret) - break; - - ret = apply_relocate_add(pmod->klp_info->sechdrs, - pmod->core_kallsyms.strtab, - pmod->klp_info->symndx, i, pmod); + ret = reloc_update_fn(pmod, i); if (ret) break; } @@ -295,45 +292,6 @@ static int klp_write_object_relocations(struct module *pmod, return ret; } -static void klp_clear_object_relocations(struct module *pmod, - struct klp_object *obj) -{ - int i, cnt; - const char *objname, *secname; - char sec_objname[MODULE_NAME_LEN]; - Elf_Shdr *sec; - - objname = klp_is_module(obj) ? obj->name : "vmlinux"; - - /* For each klp relocation section */ - for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) { - sec = pmod->klp_info->sechdrs + i; - secname = pmod->klp_info->secstrings + sec->sh_name; - if (!(sec->sh_flags & SHF_RELA_LIVEPATCH)) - continue; - - /* - * Format: .klp.rela.sec_objname.section_name - * See comment in klp_resolve_symbols() for an explanation - * of the selected field width value. - */ - secname = pmod->klp_info->secstrings + sec->sh_name; - cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname); - if (cnt != 1) { - pr_err("section %s has an incorrectly formatted name\n", - secname); - continue; - } - - if (strcmp(objname, sec_objname)) - continue; - - clear_relocate_add(pmod->klp_info->sechdrs, - pmod->core_kallsyms.strtab, - pmod->klp_info->symndx, i, pmod); - } -} - /* * Sysfs Interface * @@ -751,6 +709,21 @@ void __weak arch_klp_init_object_loaded(struct klp_patch *patch, { } +static int klp_write_relocations(struct module *pmod, + unsigned int relsec) +{ + int ret; + + ret = klp_resolve_symbols(pmod->klp_info->sechdrs + relsec, pmod); + if (ret) + return ret; + + ret = apply_relocate_add(pmod->klp_info->sechdrs, + pmod->core_kallsyms.strtab, + pmod->klp_info->symndx, relsec, pmod); + return ret; +} + /* parts of the initialization that is done only when the object is loaded */ static int klp_init_object_loaded(struct klp_patch *patch, struct klp_object *obj) @@ -761,7 +734,8 @@ static int klp_init_object_loaded(struct klp_patch *patch, mutex_lock(&text_mutex); module_disable_ro(patch->mod); - ret = klp_write_object_relocations(patch->mod, obj); + ret = klp_update_object_relocations(patch->mod, obj, + klp_write_relocations); if (ret) { module_enable_ro(patch->mod, true); mutex_unlock(&text_mutex); @@ -1111,6 +1085,15 @@ void klp_discard_nops(struct klp_patch *new_patch) klp_free_objects_dynamic(klp_transition_patch); } +static int klp_clear_relocations(struct module *pmod, + unsigned int relsec) +{ + clear_relocate_add(pmod->klp_info->sechdrs, + pmod->core_kallsyms.strtab, + pmod->klp_info->symndx, relsec, pmod); + return 0; +} + /* * Remove parts of patches that touch a given kernel module. The list of * patches processed might be limited. When limit is NULL, all patches @@ -1141,7 +1124,8 @@ static void klp_cleanup_module_patches_limited(struct module *mod, mutex_lock(&text_mutex); module_disable_ro(patch->mod); - klp_clear_object_relocations(patch->mod, obj); + klp_update_object_relocations(patch->mod, obj, + klp_clear_relocations); module_enable_ro(patch->mod, true); mutex_unlock(&text_mutex); -- 2.23.0