Skip to content

Commit 9ec4ece

Browse files
AKASHI Takahirotorvalds
authored andcommitted
kexec_file,x86,powerpc: factor out kexec_file_ops functions
As arch_kexec_kernel_image_{probe,load}(), arch_kimage_file_post_load_cleanup() and arch_kexec_kernel_verify_sig() are almost duplicated among architectures, they can be commonalized with an architecture-defined kexec_file_ops array. So let's factor them out. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: AKASHI Takahiro <[email protected]> Acked-by: Dave Young <[email protected]> Tested-by: Dave Young <[email protected]> Cc: Vivek Goyal <[email protected]> Cc: Baoquan He <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Thiago Jung Bauermann <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent b799a09 commit 9ec4ece

File tree

8 files changed

+71
-94
lines changed

8 files changed

+71
-94
lines changed

arch/powerpc/include/asm/kexec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static inline bool kdump_in_progress(void)
9595
}
9696

9797
#ifdef CONFIG_KEXEC_FILE
98-
extern struct kexec_file_ops kexec_elf64_ops;
98+
extern const struct kexec_file_ops kexec_elf64_ops;
9999

100100
#ifdef CONFIG_IMA_KEXEC
101101
#define ARCH_HAS_KIMAGE_ARCH

arch/powerpc/kernel/kexec_elf_64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
657657
return ret ? ERR_PTR(ret) : fdt;
658658
}
659659

660-
struct kexec_file_ops kexec_elf64_ops = {
660+
const struct kexec_file_ops kexec_elf64_ops = {
661661
.probe = elf64_probe,
662662
.load = elf64_load,
663663
};

arch/powerpc/kernel/machine_kexec_file_64.c

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,52 +31,19 @@
3131

3232
#define SLAVE_CODE_SIZE 256
3333

34-
static struct kexec_file_ops *kexec_file_loaders[] = {
34+
const struct kexec_file_ops * const kexec_file_loaders[] = {
3535
&kexec_elf64_ops,
36+
NULL
3637
};
3738

3839
int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
3940
unsigned long buf_len)
4041
{
41-
int i, ret = -ENOEXEC;
42-
struct kexec_file_ops *fops;
43-
4442
/* We don't support crash kernels yet. */
4543
if (image->type == KEXEC_TYPE_CRASH)
4644
return -EOPNOTSUPP;
4745

48-
for (i = 0; i < ARRAY_SIZE(kexec_file_loaders); i++) {
49-
fops = kexec_file_loaders[i];
50-
if (!fops || !fops->probe)
51-
continue;
52-
53-
ret = fops->probe(buf, buf_len);
54-
if (!ret) {
55-
image->fops = fops;
56-
return ret;
57-
}
58-
}
59-
60-
return ret;
61-
}
62-
63-
void *arch_kexec_kernel_image_load(struct kimage *image)
64-
{
65-
if (!image->fops || !image->fops->load)
66-
return ERR_PTR(-ENOEXEC);
67-
68-
return image->fops->load(image, image->kernel_buf,
69-
image->kernel_buf_len, image->initrd_buf,
70-
image->initrd_buf_len, image->cmdline_buf,
71-
image->cmdline_buf_len);
72-
}
73-
74-
int arch_kimage_file_post_load_cleanup(struct kimage *image)
75-
{
76-
if (!image->fops || !image->fops->cleanup)
77-
return 0;
78-
79-
return image->fops->cleanup(image->image_loader_data);
46+
return kexec_image_probe_default(image, buf, buf_len);
8047
}
8148

8249
/**

arch/x86/include/asm/kexec-bzimage64.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
#ifndef _ASM_KEXEC_BZIMAGE64_H
33
#define _ASM_KEXEC_BZIMAGE64_H
44

5-
extern struct kexec_file_ops kexec_bzImage64_ops;
5+
extern const struct kexec_file_ops kexec_bzImage64_ops;
66

77
#endif /* _ASM_KEXE_BZIMAGE64_H */

arch/x86/kernel/kexec-bzimage64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)
538538
}
539539
#endif
540540

541-
struct kexec_file_ops kexec_bzImage64_ops = {
541+
const struct kexec_file_ops kexec_bzImage64_ops = {
542542
.probe = bzImage64_probe,
543543
.load = bzImage64_load,
544544
.cleanup = bzImage64_cleanup,

arch/x86/kernel/machine_kexec_64.c

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
#include <asm/set_memory.h>
3131

3232
#ifdef CONFIG_KEXEC_FILE
33-
static struct kexec_file_ops *kexec_file_loaders[] = {
33+
const struct kexec_file_ops * const kexec_file_loaders[] = {
3434
&kexec_bzImage64_ops,
35+
NULL
3536
};
3637
#endif
3738

@@ -364,27 +365,6 @@ void arch_crash_save_vmcoreinfo(void)
364365
/* arch-dependent functionality related to kexec file-based syscall */
365366

366367
#ifdef CONFIG_KEXEC_FILE
367-
int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
368-
unsigned long buf_len)
369-
{
370-
int i, ret = -ENOEXEC;
371-
struct kexec_file_ops *fops;
372-
373-
for (i = 0; i < ARRAY_SIZE(kexec_file_loaders); i++) {
374-
fops = kexec_file_loaders[i];
375-
if (!fops || !fops->probe)
376-
continue;
377-
378-
ret = fops->probe(buf, buf_len);
379-
if (!ret) {
380-
image->fops = fops;
381-
return ret;
382-
}
383-
}
384-
385-
return ret;
386-
}
387-
388368
void *arch_kexec_kernel_image_load(struct kimage *image)
389369
{
390370
vfree(image->arch.elf_headers);
@@ -399,27 +379,6 @@ void *arch_kexec_kernel_image_load(struct kimage *image)
399379
image->cmdline_buf_len);
400380
}
401381

402-
int arch_kimage_file_post_load_cleanup(struct kimage *image)
403-
{
404-
if (!image->fops || !image->fops->cleanup)
405-
return 0;
406-
407-
return image->fops->cleanup(image->image_loader_data);
408-
}
409-
410-
#ifdef CONFIG_KEXEC_VERIFY_SIG
411-
int arch_kexec_kernel_verify_sig(struct kimage *image, void *kernel,
412-
unsigned long kernel_len)
413-
{
414-
if (!image->fops || !image->fops->verify_sig) {
415-
pr_debug("kernel loader does not support signature verification.");
416-
return -EKEYREJECTED;
417-
}
418-
419-
return image->fops->verify_sig(kernel, kernel_len);
420-
}
421-
#endif
422-
423382
/*
424383
* Apply purgatory relocations.
425384
*

include/linux/kexec.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ struct kexec_file_ops {
135135
#endif
136136
};
137137

138+
extern const struct kexec_file_ops * const kexec_file_loaders[];
139+
140+
int kexec_image_probe_default(struct kimage *image, void *buf,
141+
unsigned long buf_len);
142+
138143
/**
139144
* struct kexec_buf - parameters for finding a place for a buffer in memory
140145
* @image: kexec image in which memory to search.
@@ -209,7 +214,7 @@ struct kimage {
209214
unsigned long cmdline_buf_len;
210215

211216
/* File operations provided by image loader */
212-
struct kexec_file_ops *fops;
217+
const struct kexec_file_ops *fops;
213218

214219
/* Image loader handling the kernel can store a pointer here */
215220
void *image_loader_data;
@@ -273,12 +278,6 @@ int crash_shrink_memory(unsigned long new_size);
273278
size_t crash_get_memory_size(void);
274279
void crash_free_reserved_phys_range(unsigned long begin, unsigned long end);
275280

276-
int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
277-
unsigned long buf_len);
278-
void * __weak arch_kexec_kernel_image_load(struct kimage *image);
279-
int __weak arch_kimage_file_post_load_cleanup(struct kimage *image);
280-
int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
281-
unsigned long buf_len);
282281
int __weak arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr,
283282
Elf_Shdr *sechdrs, unsigned int relsec);
284283
int __weak arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,

kernel/kexec_file.c

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,80 @@
2828

2929
static int kexec_calculate_store_digests(struct kimage *image);
3030

31+
/*
32+
* Currently this is the only default function that is exported as some
33+
* architectures need it to do additional handlings.
34+
* In the future, other default functions may be exported too if required.
35+
*/
36+
int kexec_image_probe_default(struct kimage *image, void *buf,
37+
unsigned long buf_len)
38+
{
39+
const struct kexec_file_ops * const *fops;
40+
int ret = -ENOEXEC;
41+
42+
for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
43+
ret = (*fops)->probe(buf, buf_len);
44+
if (!ret) {
45+
image->fops = *fops;
46+
return ret;
47+
}
48+
}
49+
50+
return ret;
51+
}
52+
3153
/* Architectures can provide this probe function */
3254
int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
3355
unsigned long buf_len)
3456
{
35-
return -ENOEXEC;
57+
return kexec_image_probe_default(image, buf, buf_len);
58+
}
59+
60+
static void *kexec_image_load_default(struct kimage *image)
61+
{
62+
if (!image->fops || !image->fops->load)
63+
return ERR_PTR(-ENOEXEC);
64+
65+
return image->fops->load(image, image->kernel_buf,
66+
image->kernel_buf_len, image->initrd_buf,
67+
image->initrd_buf_len, image->cmdline_buf,
68+
image->cmdline_buf_len);
3669
}
3770

3871
void * __weak arch_kexec_kernel_image_load(struct kimage *image)
3972
{
40-
return ERR_PTR(-ENOEXEC);
73+
return kexec_image_load_default(image);
74+
}
75+
76+
static int kexec_image_post_load_cleanup_default(struct kimage *image)
77+
{
78+
if (!image->fops || !image->fops->cleanup)
79+
return 0;
80+
81+
return image->fops->cleanup(image->image_loader_data);
4182
}
4283

4384
int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
4485
{
45-
return -EINVAL;
86+
return kexec_image_post_load_cleanup_default(image);
4687
}
4788

4889
#ifdef CONFIG_KEXEC_VERIFY_SIG
90+
static int kexec_image_verify_sig_default(struct kimage *image, void *buf,
91+
unsigned long buf_len)
92+
{
93+
if (!image->fops || !image->fops->verify_sig) {
94+
pr_debug("kernel loader does not support signature verification.\n");
95+
return -EKEYREJECTED;
96+
}
97+
98+
return image->fops->verify_sig(buf, buf_len);
99+
}
100+
49101
int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
50102
unsigned long buf_len)
51103
{
52-
return -EKEYREJECTED;
104+
return kexec_image_verify_sig_default(image, buf, buf_len);
53105
}
54106
#endif
55107

0 commit comments

Comments
 (0)