Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933244AbcDER0x (ORCPT ); Tue, 5 Apr 2016 13:26:53 -0400 Received: from mail-db3on0129.outbound.protection.outlook.com ([157.55.234.129]:3661 "EHLO emea01-db3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1756707AbcDER0t (ORCPT ); Tue, 5 Apr 2016 13:26:49 -0400 X-Greylist: delayed 12771 seconds by postgrey-1.27 at vger.kernel.org; Tue, 05 Apr 2016 13:26:48 EDT Authentication-Results: suse.cz; dkim=none (message not signed) header.d=none;suse.cz; dmarc=none action=none header.from=virtuozzo.com; Subject: Re: Bug with paravirt ops and livepatches To: Miroslav Benes , Josh Poimboeuf References: <20160329120518.GA21252@canonical.com> <20160401190704.GB7837@canonical.com> <20160404161428.3qap2i4vpgda66iw@treble.redhat.com> CC: Jiri Kosina , Chris J Arges , , , Linux Kernel Mailing List , From: Evgenii Shatokhin Message-ID: <5703C36D.4090306@virtuozzo.com> Date: Tue, 5 Apr 2016 16:53:49 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [195.214.232.10] X-ClientProxiedBy: AM3PR03CA012.eurprd03.prod.outlook.com (2a01:111:e400:8829::12) To AM3PR08MB0722.eurprd08.prod.outlook.com (2a01:111:e400:c409::28) X-MS-Office365-Filtering-Correlation-Id: 1fecf8d2-cda0-4e7b-6b67-08d35d59b996 X-Microsoft-Exchange-Diagnostics: 1;AM3PR08MB0722;2:RsF4d6ot0alFvJh5ZPD9VPAXOenBirqe0Y42+IbGiveCZkWaqwte+568Ya6v02iIe0eU94iRZOHYdgTGSqJsAi8n9s0esG+MEBohSmtiCpOahM8504yztb9+QLArcyrdS+KWFHmMeH1KTkC1lZPJSNPRwdY4gdQxnwfZ7aRAee26HuTWA+ikV100dA87aZZz;3:H5jqEXCBjDro3Sk2j9FQgm7SfyusppxAcGqkzEooPPOEZmMRiPuiD1gpwmg7lxz2uSrrkGpefDdp8NTCWoAFJ26D5CNqmv9DvvvJ4dRzaPRZtN2y+U+3V93vtaeVHUWo X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:AM3PR08MB0722; X-Microsoft-Exchange-Diagnostics: 1;AM3PR08MB0722;25:+NxlkxshZs3ATdGVwRIVERNLbVoVCn357mMpgtRWbSDF0HsshDfn3C3yDa/1Mv/oKcnCFzSJO8iJXO9I6LE5YPckpGGSmw1ELcmQN5ZP7zQVnZLy235ZAnipvSL4Qq6/PrNMKF8AIItOONHEp2Bsznkuu0Do94qX0Wa5Uvl2RSI0UxGr7qsj38V6pQqMZ7egLTaRC4S9i26jJ8ZgJemxCxQdK+NjyTSnMVa5SqbgfzzWCG3KD728Z1+E7whNrm6hS5maYgKCbRnYbP8Z1t6ZNPfJ9s30gy92TxLxvABUXwr8vG940cMKrCI8JlMA4CkvVFikvhEwmetFjPd91mPXihDpl+PQWpuQR5VxeJoE889cJNCkSBXlK1S9Y5QzDs7OGyOPdAlHQw8+z2+ob6njc5gpgDjkXmHZyNwBK5iiNRVK8zsf17NmaD2TwhXH248oDAp9ParRP1IqeLXTymiW/J+IHS9m5xoPLLjXIQ27lrbm8g+MlyIWKlsyH20g4KjDDYmc4eMX0e7iAm12XuHo7UZu+53/tIHhNVy6TnvfzVuXUf7Yfbkhopn7PQ2g8EIxG/HB9PKfAHV4wpBOzC1wRXFrx+ME2/467dF4f0J0lrPdC5bTfBp0tVJyjmdxkLfNzLUw87AAnjg7OE66sbMa6xiyoM2582dOWXis4R4GTGg0fTvYkTYlqai/CdlgjFPo X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040046)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001)(6041046)(6043046);SRVR:AM3PR08MB0722;BCL:0;PCL:0;RULEID:;SRVR:AM3PR08MB0722; X-Microsoft-Exchange-Diagnostics: 1;AM3PR08MB0722;4:inzcgKy79J2VbR7JSBd41M/ROGX/nL80HV46uwC2TPmuwWsbZHbPENwD4gAyeyv6IYkibR8vY0bv67jRcXeHxEEiBmSH2a9kHOPRrKbqgGBaKY733JgqzKmHUlImOsIyRUtp7NIj6bHFcgarIU4a5+vPuwXfKQWoTXbyy6rkLndCZlyuJUXx8bmN9VKATM0m/SWvARqpstMvFGcMl0m38/eUB5cs9SUNsKFLgxO75FKvLFACZirFc28Yun0ysVYgfEgbZDgOHJTtd7E+1LBwSbYeaQz9wL5ju4yQceSzYM5DynHoy4aUYglPrfGlpCgYgfNGEtEyOiANZS4TB64Vx8+S+W3GyY6rrzTsLYHx9Djr/3jRueeqoyTCnWTIdyptkOzWcOqXeXnxpMpBvxut+Pd437sI2gYJeo4/shWWfb4= X-Forefront-PRVS: 0903DD1D85 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4630300001)(6049001)(6009001)(24454002)(51444003)(19580395003)(66066001)(93886004)(86362001)(15975445007)(65806001)(64126003)(47776003)(2950100001)(50466002)(33656002)(80316001)(77096005)(189998001)(4326007)(2906002)(5004730100002)(42186005)(5008740100001)(54356999)(2870700001)(36756003)(1096002)(6116002)(586003)(5001770100001)(65816999)(92566002)(3846002)(87266999)(81166005)(23676002)(76176999)(50986999);DIR:OUT;SFP:1102;SCL:1;SRVR:AM3PR08MB0722;H:[10.30.16.145];FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtBTTNQUjA4TUIwNzIyOzIzOlc3dFRib050OExJWXc3OEMxdFdoSkQrUWJa?= =?utf-8?B?QWJJdDc5bU9HT2xWOXRpWVVQTTIzdWZXQ0lIR1FlMW9LVFV6ZnVQaU8yMkwy?= =?utf-8?B?ZWgwbnBQVm5NZjYrVVFsN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lNVh5eDlEZmc4RzE4c2xPL0RrT3c3aFMzdEROVkJoTWRpcVJI?= =?utf-8?B?bVd1OEh5WGxnRWhGdWQwTlk2R3JUdFV0emhJbHN4dHFqWi9wRFJEU0RFc3JK?= =?utf-8?B?Nk00dUhvc2lCREE2Y2UzUWEzaElraXV4Z25iZzViU09JYzlUUlowK1VSNWFq?= =?utf-8?B?UkNXaVJuSkwrUTdMUjNQSFFmRXNMYVVjZ0JBc09vMDVMK1M2REd6c0xEV3V4?= =?utf-8?B?MW9GVlc2UGFTdzZqbTBERkFOc1diQ2R2OFlITStGbFpyQ29KRVRMVHFrSkUr?= =?utf-8?B?QmNXQlVxRktybk90dGFVQVhMSjFyMGZOT2JUazJNQU8wVDR6WXRDTXRqeGxl?= =?utf-8?B?UHp5by9FTVBQWU1ZWkNxbmliVTlSS3N4RHhJY1hRPT0=?= X-Microsoft-Exchange-Diagnostics: 1;AM3PR08MB0722;5:NCysMAVLSNEbWzRFXXZyBI2/J+JQr+jB3tu2xIWy3/+Foxu6Ddig+kqKCS6pJrn56VOw40ofE1ESwekf66wuDIK0bWv7akmUkoMpl/OBIJfQ/BBOXaMwM/Q5ZiF6bhmUY0B8NKpsekIKNapafh5MHw==;24:lgl/k3WFIVnVj1iteXMa8/KYxozQl0AmgobvDnvld7t/SR/N4XMs5d0mIBPPADFdo/Hia099SQ+bSLPLWRkD4aGm61+A2Q60BnzhkZJHeO0=;20:dKiLs3EoARpU/MIjvM/E4Gm7jPGd6VZpDTHH0tx2hQEBceR+vl1xHr51m6Ivj8uR3oS8fZfhFOFjI2ei7l72UrkJ52b8Z6PIZE2ouVTS1nhFHiMODNAZRh2fElHf+hTEY40T2auWiGIPeCHzqOSnNEp0kpHmPamMMr27f01XfMA= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Apr 2016 13:53:53.1330 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM3PR08MB0722 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 6647 Lines: 137 05.04.2016 16:07, Miroslav Benes пишет: > On Mon, 4 Apr 2016, Josh Poimboeuf wrote: > >> So I think this doesn't fix the problem. Dynamic relocations are >> applied to the "patch module", whereas the above code deals with the >> initialization order of the "patched module". This distinction >> originally confused me as well, until Jessica set me straight. >> >> Let me try to illustrate the problem with an example. Imagine you have >> a patch module P which applies a patch to module M. P replaces M's >> function F with a new function F', which uses paravirt ops. >> >> 1) Patch P is loaded before module M. P's new function F' has an >> instruction which is patched by apply_paravirt(), even though the >> patch hasn't been applied yet. >> >> 2) Module M is loaded. Before applying the patch, livepatch tries to >> apply a klp_reloc to the instruction in F' which was already patched >> by apply_paravirt() in step 1. This results in undefined behavior >> because it tries to patch the original instruction but instead >> patches the new paravirt instruction. >> >> So the above patch makes no difference because the paravirt module >> loading order doesn't really matter. > > Hi, > > we are trying really hard to understand the actual culprit here and as it > is quite confusing I have several questions/comments... > > 1. can you provide dynrela sections of the patch module from > https://github.com/dynup/kpatch/issues/580? What is interesting is that > kvm_arch_vm_ioctl() function contains rela records only for trivial (== > exported) symbols from the first look. The problem should be there only if > you want to patch a function which reference some paravirt_ops unexported > symbol. For that symbol dynrela should be created. As far as I understand it, create-diff-object does not check if a symbol is exported or not when a patch for a module rather than for vmlinux is being prepared. In such cases, it only checks if the referenced global symbol is defined somewhere in that module and if not, creates a dynrela for it. The code is in kpatch_create_dynamic_rela_sections() from https://github.com/dynup/kpatch/blob/master/kpatch-build/create-diff-object.c. > > 2. I am almost 100 percent sure that the second problem Chris mentions in > github post is something different. If he patches only kvm_arch_vm_ioctl() > so that it calls his exported __kvm_arch_vm_ioctl() duplicate there is no > problem. Because it is a trivial livepatching case. There are no dynrelas > and no alternatives in the patch module. The crash is suspicious and we > have a problem somewhere. Chris, can you please provide more info about > that? That is how exactly you used kallsyms_lookup_name() and so on... > > 3. Now I'll try to explain what really happens here... because of 1. and > the way the alternatives and relocations are implemented the only > problematic case is when one wants to patch a module which introduces its > own alternatives. Which is probably the case of KVM. Why? > > When alternative is used, the call to some pv_*_ops.function is placed > somewhere. The address of this location is stored in a separate elf > section and when apply_paravirt() is called it takes the address and > place the new code there (or it does not, it depends :)). When the > alternative is in some module and pv_*_ops is exported, which is the > usual case, there is no problem. No dynrela needs to be used when a user > wants to patch such function with the alternative. > > The only problem is when the function uses unexported pv_*_ops (or some > other alternative symbol) from its own object file. When the user wants to > patch this one there is a need for dynrela. > > So what really happens is that we load a patch module, we do not apply > our relocations but we do apply alternatives to the patch module, which is > problematic because there is a reference to something which is not yet > resolved (that is unexported pv_*_ops). When a patched module arrives we > happily resolve relocations but since we do not apply alternatives again > there is a rubbish in the patching code. > > Is this all correct? > >> Jessica proposed some novel fixes here: >> >> https://github.com/dynup/kpatch/issues/580#issuecomment-183001652 > > Yes, I think that fix should be the same we have for relocations. To > postpone the alternatives applications. Maybe it is even possible to do it > in a similar way. And yes on one hand it is gonna be ugly, on the other > hand it is gonna be consistent with relocations. > >> I think the *real* problem here (and one that we've seen before) is that >> we have a feature which allows you to load a patch to a module before >> loading the module itself. That really goes against the grain of how >> module dependencies work. It has already given us several headaches and >> it makes the livepatch code a lot more complex. >> >> I really think we need to take another hard look about whether it's >> really worth it. My current feeling is that it's not. > > I can only say that maybe about 1/3 of kgraft patches we currently have > are for modules (1/3 is probably not correct but it is definitely > non-trivial number). It would be really unfortunate to load all the > to-be-patched modules when a patch module is applied. > > This does not mean that we cannot solve it in another way as you propose > below. I have to think about it. > > Miroslav > >> If we were able to get rid of that "feature", yes, the livepatch code >> would be simpler, but there might be another awesome benefit: I suspect >> we'd also be able to get rid of the need for specialized patch creation >> tooling like kpatch-build. Instead I think we could just specify >> klp_relocs info in the source code of the patch, and just use kbuild to >> build the patch module. Not only would the livepatch code be simpler >> (and much easier to wrap your head around), but the user space tooling >> could be *vastly* simpler. >> >> Of course, removing that feature might create some headaches for the >> user. It is nice to be able to load a big cumulative patch without >> having to load all the dependencies first. But maybe there are things >> we could do to make the dependency problem more manageable. e.g., >> splitting up patch modules to be per-object? requiring the user to load >> modules they don't need? patching or replacing the module on disk? >> copying the new module to a new locaiton and telling modprobe where to >> find it? >> >> I don't have all the answers but I think we should take a hard look at >> some of these other approaches. >> >> -- >> Josh >> Regards, Evgenii