kcore.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/proc/kcore.c kernel ELF core dumper
  4. *
  5. * Modelled on fs/exec.c:aout_core_dump()
  6. * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  7. * ELF version written by David Howells <David.Howells@nexor.co.uk>
  8. * Modified and incorporated into 2.3.x by Tigran Aivazian <tigran@veritas.com>
  9. * Support to dump vmalloc'd areas (ELF only), Tigran Aivazian <tigran@veritas.com>
  10. * Safe accesses to vmalloc/direct-mapped discontiguous areas, Kanoj Sarcar <kanoj@sgi.com>
  11. */
  12. #include <linux/vmcore_info.h>
  13. #include <linux/mm.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/kcore.h>
  16. #include <linux/user.h>
  17. #include <linux/capability.h>
  18. #include <linux/elf.h>
  19. #include <linux/elfcore.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/highmem.h>
  22. #include <linux/printk.h>
  23. #include <linux/memblock.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/uio.h>
  27. #include <asm/io.h>
  28. #include <linux/list.h>
  29. #include <linux/ioport.h>
  30. #include <linux/memory.h>
  31. #include <linux/sched/task.h>
  32. #include <linux/security.h>
  33. #include <asm/sections.h>
  34. #include "internal.h"
  35. #ifndef ELF_CORE_EFLAGS
  36. #define ELF_CORE_EFLAGS 0
  37. #endif
  38. static struct proc_dir_entry *proc_root_kcore;
  39. #ifndef kc_vaddr_to_offset
  40. #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
  41. #endif
  42. #ifndef kc_offset_to_vaddr
  43. #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
  44. #endif
  45. #ifndef kc_xlate_dev_mem_ptr
  46. #define kc_xlate_dev_mem_ptr kc_xlate_dev_mem_ptr
  47. static inline void *kc_xlate_dev_mem_ptr(phys_addr_t phys)
  48. {
  49. return __va(phys);
  50. }
  51. #endif
  52. #ifndef kc_unxlate_dev_mem_ptr
  53. #define kc_unxlate_dev_mem_ptr kc_unxlate_dev_mem_ptr
  54. static inline void kc_unxlate_dev_mem_ptr(phys_addr_t phys, void *virt)
  55. {
  56. }
  57. #endif
  58. static LIST_HEAD(kclist_head);
  59. static int kcore_nphdr;
  60. static size_t kcore_phdrs_len;
  61. static size_t kcore_notes_len;
  62. static size_t kcore_data_offset;
  63. DEFINE_STATIC_PERCPU_RWSEM(kclist_lock);
  64. static int kcore_need_update = 1;
  65. /*
  66. * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
  67. * Same as oldmem_pfn_is_ram in vmcore
  68. */
  69. static int (*mem_pfn_is_ram)(unsigned long pfn);
  70. int __init register_mem_pfn_is_ram(int (*fn)(unsigned long pfn))
  71. {
  72. if (mem_pfn_is_ram)
  73. return -EBUSY;
  74. mem_pfn_is_ram = fn;
  75. return 0;
  76. }
  77. static int pfn_is_ram(unsigned long pfn)
  78. {
  79. if (mem_pfn_is_ram)
  80. return mem_pfn_is_ram(pfn);
  81. else
  82. return 1;
  83. }
  84. /* This doesn't grab kclist_lock, so it should only be used at init time. */
  85. void __init kclist_add(struct kcore_list *new, void *addr, size_t size,
  86. int type)
  87. {
  88. new->addr = (unsigned long)addr;
  89. new->size = size;
  90. new->type = type;
  91. list_add_tail(&new->list, &kclist_head);
  92. }
  93. static void update_kcore_size(void)
  94. {
  95. size_t try, size;
  96. struct kcore_list *m;
  97. kcore_nphdr = 1; /* PT_NOTE */
  98. size = 0;
  99. list_for_each_entry(m, &kclist_head, list) {
  100. try = kc_vaddr_to_offset((size_t)m->addr + m->size);
  101. if (try > size)
  102. size = try;
  103. kcore_nphdr++;
  104. }
  105. kcore_phdrs_len = kcore_nphdr * sizeof(struct elf_phdr);
  106. kcore_notes_len = (4 * sizeof(struct elf_note) +
  107. ALIGN(sizeof(NN_PRSTATUS), 4) +
  108. ALIGN(sizeof(NN_PRPSINFO), 4) +
  109. ALIGN(sizeof(NN_TASKSTRUCT), 4) +
  110. VMCOREINFO_NOTE_NAME_BYTES +
  111. ALIGN(sizeof(struct elf_prstatus), 4) +
  112. ALIGN(sizeof(struct elf_prpsinfo), 4) +
  113. ALIGN(arch_task_struct_size, 4) +
  114. ALIGN(vmcoreinfo_size, 4));
  115. kcore_data_offset = PAGE_ALIGN(sizeof(struct elfhdr) + kcore_phdrs_len +
  116. kcore_notes_len);
  117. proc_root_kcore->size = kcore_data_offset + size;
  118. }
  119. #ifdef CONFIG_HIGHMEM
  120. /*
  121. * If no highmem, we can assume [0...max_low_pfn) continuous range of memory
  122. * because memory hole is not as big as !HIGHMEM case.
  123. * (HIGHMEM is special because part of memory is _invisible_ from the kernel.)
  124. */
  125. static int kcore_ram_list(struct list_head *head)
  126. {
  127. struct kcore_list *ent;
  128. ent = kmalloc_obj(*ent);
  129. if (!ent)
  130. return -ENOMEM;
  131. ent->addr = (unsigned long)__va(0);
  132. ent->size = max_low_pfn << PAGE_SHIFT;
  133. ent->type = KCORE_RAM;
  134. list_add(&ent->list, head);
  135. return 0;
  136. }
  137. #else /* !CONFIG_HIGHMEM */
  138. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  139. /* calculate vmemmap's address from given system ram pfn and register it */
  140. static int
  141. get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
  142. {
  143. unsigned long pfn = __pa(ent->addr) >> PAGE_SHIFT;
  144. unsigned long nr_pages = ent->size >> PAGE_SHIFT;
  145. unsigned long start, end;
  146. struct kcore_list *vmm, *tmp;
  147. start = ((unsigned long)pfn_to_page(pfn)) & PAGE_MASK;
  148. end = ((unsigned long)pfn_to_page(pfn + nr_pages)) - 1;
  149. end = PAGE_ALIGN(end);
  150. /* overlap check (because we have to align page */
  151. list_for_each_entry(tmp, head, list) {
  152. if (tmp->type != KCORE_VMEMMAP)
  153. continue;
  154. if (start < tmp->addr + tmp->size)
  155. if (end > tmp->addr)
  156. end = tmp->addr;
  157. }
  158. if (start < end) {
  159. vmm = kmalloc_obj(*vmm);
  160. if (!vmm)
  161. return 0;
  162. vmm->addr = start;
  163. vmm->size = end - start;
  164. vmm->type = KCORE_VMEMMAP;
  165. list_add_tail(&vmm->list, head);
  166. }
  167. return 1;
  168. }
  169. #else
  170. static int
  171. get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
  172. {
  173. return 1;
  174. }
  175. #endif
  176. static int
  177. kclist_add_private(unsigned long pfn, unsigned long nr_pages, void *arg)
  178. {
  179. struct list_head *head = (struct list_head *)arg;
  180. struct kcore_list *ent;
  181. struct page *p;
  182. if (!pfn_valid(pfn))
  183. return 1;
  184. p = pfn_to_page(pfn);
  185. ent = kmalloc_obj(*ent);
  186. if (!ent)
  187. return -ENOMEM;
  188. ent->addr = (unsigned long)page_to_virt(p);
  189. ent->size = nr_pages << PAGE_SHIFT;
  190. if (!virt_addr_valid((void *)ent->addr))
  191. goto free_out;
  192. /* cut not-mapped area. ....from ppc-32 code. */
  193. if (ULONG_MAX - ent->addr < ent->size)
  194. ent->size = ULONG_MAX - ent->addr;
  195. /*
  196. * We've already checked virt_addr_valid so we know this address
  197. * is a valid pointer, therefore we can check against it to determine
  198. * if we need to trim
  199. */
  200. if (VMALLOC_START > ent->addr) {
  201. if (VMALLOC_START - ent->addr < ent->size)
  202. ent->size = VMALLOC_START - ent->addr;
  203. }
  204. ent->type = KCORE_RAM;
  205. list_add_tail(&ent->list, head);
  206. if (!get_sparsemem_vmemmap_info(ent, head)) {
  207. list_del(&ent->list);
  208. goto free_out;
  209. }
  210. return 0;
  211. free_out:
  212. kfree(ent);
  213. return 1;
  214. }
  215. static int kcore_ram_list(struct list_head *list)
  216. {
  217. int nid, ret;
  218. unsigned long end_pfn;
  219. /* Not initialized....update now */
  220. /* find out "max pfn" */
  221. end_pfn = 0;
  222. for_each_node_state(nid, N_MEMORY) {
  223. unsigned long node_end;
  224. node_end = node_end_pfn(nid);
  225. if (end_pfn < node_end)
  226. end_pfn = node_end;
  227. }
  228. /* scan 0 to max_pfn */
  229. ret = walk_system_ram_range(0, end_pfn, list, kclist_add_private);
  230. if (ret)
  231. return -ENOMEM;
  232. return 0;
  233. }
  234. #endif /* CONFIG_HIGHMEM */
  235. static int kcore_update_ram(void)
  236. {
  237. LIST_HEAD(list);
  238. LIST_HEAD(garbage);
  239. struct kcore_list *tmp, *pos;
  240. int ret = 0;
  241. percpu_down_write(&kclist_lock);
  242. if (!xchg(&kcore_need_update, 0))
  243. goto out;
  244. ret = kcore_ram_list(&list);
  245. if (ret) {
  246. /* Couldn't get the RAM list, try again next time. */
  247. WRITE_ONCE(kcore_need_update, 1);
  248. list_splice_tail(&list, &garbage);
  249. goto out;
  250. }
  251. list_for_each_entry_safe(pos, tmp, &kclist_head, list) {
  252. if (pos->type == KCORE_RAM || pos->type == KCORE_VMEMMAP)
  253. list_move(&pos->list, &garbage);
  254. }
  255. list_splice_tail(&list, &kclist_head);
  256. update_kcore_size();
  257. out:
  258. percpu_up_write(&kclist_lock);
  259. list_for_each_entry_safe(pos, tmp, &garbage, list) {
  260. list_del(&pos->list);
  261. kfree(pos);
  262. }
  263. return ret;
  264. }
  265. static void append_kcore_note(char *notes, size_t *i, const char *name,
  266. unsigned int type, const void *desc,
  267. size_t descsz)
  268. {
  269. struct elf_note *note = (struct elf_note *)&notes[*i];
  270. note->n_namesz = strlen(name) + 1;
  271. note->n_descsz = descsz;
  272. note->n_type = type;
  273. *i += sizeof(*note);
  274. memcpy(&notes[*i], name, note->n_namesz);
  275. *i = ALIGN(*i + note->n_namesz, 4);
  276. memcpy(&notes[*i], desc, descsz);
  277. *i = ALIGN(*i + descsz, 4);
  278. }
  279. static ssize_t read_kcore_iter(struct kiocb *iocb, struct iov_iter *iter)
  280. {
  281. struct file *file = iocb->ki_filp;
  282. char *buf = file->private_data;
  283. loff_t *fpos = &iocb->ki_pos;
  284. size_t phdrs_offset, notes_offset;
  285. size_t page_offline_frozen = 1;
  286. struct kcore_list *m;
  287. size_t tsz;
  288. unsigned long start;
  289. size_t buflen = iov_iter_count(iter);
  290. size_t orig_buflen = buflen;
  291. int ret = 0;
  292. percpu_down_read(&kclist_lock);
  293. /*
  294. * Don't race against drivers that set PageOffline() and expect no
  295. * further page access.
  296. */
  297. page_offline_freeze();
  298. phdrs_offset = sizeof(struct elfhdr);
  299. notes_offset = phdrs_offset + kcore_phdrs_len;
  300. /* ELF file header. */
  301. if (buflen && *fpos < sizeof(struct elfhdr)) {
  302. struct elfhdr ehdr = {
  303. .e_ident = {
  304. [EI_MAG0] = ELFMAG0,
  305. [EI_MAG1] = ELFMAG1,
  306. [EI_MAG2] = ELFMAG2,
  307. [EI_MAG3] = ELFMAG3,
  308. [EI_CLASS] = ELF_CLASS,
  309. [EI_DATA] = ELF_DATA,
  310. [EI_VERSION] = EV_CURRENT,
  311. [EI_OSABI] = ELF_OSABI,
  312. },
  313. .e_type = ET_CORE,
  314. .e_machine = ELF_ARCH,
  315. .e_version = EV_CURRENT,
  316. .e_phoff = sizeof(struct elfhdr),
  317. .e_flags = ELF_CORE_EFLAGS,
  318. .e_ehsize = sizeof(struct elfhdr),
  319. .e_phentsize = sizeof(struct elf_phdr),
  320. .e_phnum = kcore_nphdr,
  321. };
  322. tsz = min_t(size_t, buflen, sizeof(struct elfhdr) - *fpos);
  323. if (copy_to_iter((char *)&ehdr + *fpos, tsz, iter) != tsz) {
  324. ret = -EFAULT;
  325. goto out;
  326. }
  327. buflen -= tsz;
  328. *fpos += tsz;
  329. }
  330. /* ELF program headers. */
  331. if (buflen && *fpos < phdrs_offset + kcore_phdrs_len) {
  332. struct elf_phdr *phdrs, *phdr;
  333. phdrs = kzalloc(kcore_phdrs_len, GFP_KERNEL);
  334. if (!phdrs) {
  335. ret = -ENOMEM;
  336. goto out;
  337. }
  338. phdrs[0].p_type = PT_NOTE;
  339. phdrs[0].p_offset = notes_offset;
  340. phdrs[0].p_filesz = kcore_notes_len;
  341. phdr = &phdrs[1];
  342. list_for_each_entry(m, &kclist_head, list) {
  343. phdr->p_type = PT_LOAD;
  344. phdr->p_flags = PF_R | PF_W | PF_X;
  345. phdr->p_offset = kc_vaddr_to_offset(m->addr)
  346. + kcore_data_offset;
  347. phdr->p_vaddr = (size_t)m->addr;
  348. if (m->type == KCORE_RAM)
  349. phdr->p_paddr = __pa(m->addr);
  350. else if (m->type == KCORE_TEXT)
  351. phdr->p_paddr = __pa_symbol(m->addr);
  352. else
  353. phdr->p_paddr = (elf_addr_t)-1;
  354. phdr->p_filesz = phdr->p_memsz = m->size;
  355. phdr->p_align = PAGE_SIZE;
  356. phdr++;
  357. }
  358. tsz = min_t(size_t, buflen,
  359. phdrs_offset + kcore_phdrs_len - *fpos);
  360. if (copy_to_iter((char *)phdrs + *fpos - phdrs_offset, tsz,
  361. iter) != tsz) {
  362. kfree(phdrs);
  363. ret = -EFAULT;
  364. goto out;
  365. }
  366. kfree(phdrs);
  367. buflen -= tsz;
  368. *fpos += tsz;
  369. }
  370. /* ELF note segment. */
  371. if (buflen && *fpos < notes_offset + kcore_notes_len) {
  372. struct elf_prstatus prstatus = {};
  373. struct elf_prpsinfo prpsinfo = {
  374. .pr_sname = 'R',
  375. .pr_fname = "vmlinux",
  376. };
  377. char *notes;
  378. size_t i = 0;
  379. strscpy(prpsinfo.pr_psargs, saved_command_line,
  380. sizeof(prpsinfo.pr_psargs));
  381. notes = kzalloc(kcore_notes_len, GFP_KERNEL);
  382. if (!notes) {
  383. ret = -ENOMEM;
  384. goto out;
  385. }
  386. append_kcore_note(notes, &i, NN_PRSTATUS, NT_PRSTATUS, &prstatus,
  387. sizeof(prstatus));
  388. append_kcore_note(notes, &i, NN_PRPSINFO, NT_PRPSINFO, &prpsinfo,
  389. sizeof(prpsinfo));
  390. append_kcore_note(notes, &i, NN_TASKSTRUCT, NT_TASKSTRUCT, current,
  391. arch_task_struct_size);
  392. /*
  393. * vmcoreinfo_size is mostly constant after init time, but it
  394. * can be changed by crash_save_vmcoreinfo(). Racing here with a
  395. * panic on another CPU before the machine goes down is insanely
  396. * unlikely, but it's better to not leave potential buffer
  397. * overflows lying around, regardless.
  398. */
  399. append_kcore_note(notes, &i, VMCOREINFO_NOTE_NAME, 0,
  400. vmcoreinfo_data,
  401. min(vmcoreinfo_size, kcore_notes_len - i));
  402. tsz = min_t(size_t, buflen,
  403. notes_offset + kcore_notes_len - *fpos);
  404. if (copy_to_iter(notes + *fpos - notes_offset, tsz, iter) != tsz) {
  405. kfree(notes);
  406. ret = -EFAULT;
  407. goto out;
  408. }
  409. kfree(notes);
  410. buflen -= tsz;
  411. *fpos += tsz;
  412. }
  413. /*
  414. * Check to see if our file offset matches with any of
  415. * the addresses in the elf_phdr on our list.
  416. */
  417. start = kc_offset_to_vaddr(*fpos - kcore_data_offset);
  418. if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
  419. tsz = buflen;
  420. m = NULL;
  421. while (buflen) {
  422. struct page *page;
  423. unsigned long pfn;
  424. phys_addr_t phys;
  425. void *__start;
  426. /*
  427. * If this is the first iteration or the address is not within
  428. * the previous entry, search for a matching entry.
  429. */
  430. if (!m || start < m->addr || start >= m->addr + m->size) {
  431. struct kcore_list *pos;
  432. m = NULL;
  433. list_for_each_entry(pos, &kclist_head, list) {
  434. if (start >= pos->addr &&
  435. start < pos->addr + pos->size) {
  436. m = pos;
  437. break;
  438. }
  439. }
  440. }
  441. if (page_offline_frozen++ % MAX_ORDER_NR_PAGES == 0) {
  442. page_offline_thaw();
  443. cond_resched();
  444. page_offline_freeze();
  445. }
  446. if (!m) {
  447. if (iov_iter_zero(tsz, iter) != tsz) {
  448. ret = -EFAULT;
  449. goto out;
  450. }
  451. goto skip;
  452. }
  453. switch (m->type) {
  454. case KCORE_VMALLOC:
  455. {
  456. const char *src = (char *)start;
  457. size_t read = 0, left = tsz;
  458. /*
  459. * vmalloc uses spinlocks, so we optimistically try to
  460. * read memory. If this fails, fault pages in and try
  461. * again until we are done.
  462. */
  463. while (true) {
  464. read += vread_iter(iter, src, left);
  465. if (read == tsz)
  466. break;
  467. src += read;
  468. left -= read;
  469. if (fault_in_iov_iter_writeable(iter, left)) {
  470. ret = -EFAULT;
  471. goto out;
  472. }
  473. }
  474. break;
  475. }
  476. case KCORE_USER:
  477. /* User page is handled prior to normal kernel page: */
  478. if (copy_to_iter((char *)start, tsz, iter) != tsz) {
  479. ret = -EFAULT;
  480. goto out;
  481. }
  482. break;
  483. case KCORE_RAM:
  484. phys = __pa(start);
  485. pfn = phys >> PAGE_SHIFT;
  486. page = pfn_to_online_page(pfn);
  487. /*
  488. * Don't read offline sections, logically offline pages
  489. * (e.g., inflated in a balloon), hwpoisoned pages,
  490. * and explicitly excluded physical ranges.
  491. */
  492. if (!page || PageOffline(page) ||
  493. is_page_hwpoison(page) || !pfn_is_ram(pfn) ||
  494. pfn_is_unaccepted_memory(pfn)) {
  495. if (iov_iter_zero(tsz, iter) != tsz) {
  496. ret = -EFAULT;
  497. goto out;
  498. }
  499. break;
  500. }
  501. fallthrough;
  502. case KCORE_VMEMMAP:
  503. case KCORE_TEXT:
  504. if (m->type == KCORE_RAM) {
  505. __start = kc_xlate_dev_mem_ptr(phys);
  506. if (!__start) {
  507. ret = -ENOMEM;
  508. if (iov_iter_zero(tsz, iter) != tsz)
  509. ret = -EFAULT;
  510. goto out;
  511. }
  512. } else {
  513. __start = (void *)start;
  514. }
  515. /*
  516. * Sadly we must use a bounce buffer here to be able to
  517. * make use of copy_from_kernel_nofault(), as these
  518. * memory regions might not always be mapped on all
  519. * architectures.
  520. */
  521. ret = copy_from_kernel_nofault(buf, __start, tsz);
  522. if (m->type == KCORE_RAM)
  523. kc_unxlate_dev_mem_ptr(phys, __start);
  524. if (ret) {
  525. if (iov_iter_zero(tsz, iter) != tsz) {
  526. ret = -EFAULT;
  527. goto out;
  528. }
  529. ret = 0;
  530. /*
  531. * We know the bounce buffer is safe to copy from, so
  532. * use _copy_to_iter() directly.
  533. */
  534. } else if (_copy_to_iter(buf, tsz, iter) != tsz) {
  535. ret = -EFAULT;
  536. goto out;
  537. }
  538. break;
  539. default:
  540. pr_warn_once("Unhandled KCORE type: %d\n", m->type);
  541. if (iov_iter_zero(tsz, iter) != tsz) {
  542. ret = -EFAULT;
  543. goto out;
  544. }
  545. }
  546. skip:
  547. buflen -= tsz;
  548. *fpos += tsz;
  549. start += tsz;
  550. tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
  551. }
  552. out:
  553. page_offline_thaw();
  554. percpu_up_read(&kclist_lock);
  555. if (ret)
  556. return ret;
  557. return orig_buflen - buflen;
  558. }
  559. static int open_kcore(struct inode *inode, struct file *filp)
  560. {
  561. int ret = security_locked_down(LOCKDOWN_KCORE);
  562. if (!capable(CAP_SYS_RAWIO))
  563. return -EPERM;
  564. if (ret)
  565. return ret;
  566. filp->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
  567. if (!filp->private_data)
  568. return -ENOMEM;
  569. if (kcore_need_update)
  570. kcore_update_ram();
  571. if (i_size_read(inode) != proc_root_kcore->size) {
  572. inode_lock(inode);
  573. i_size_write(inode, proc_root_kcore->size);
  574. inode_unlock(inode);
  575. }
  576. return 0;
  577. }
  578. static int release_kcore(struct inode *inode, struct file *file)
  579. {
  580. kfree(file->private_data);
  581. return 0;
  582. }
  583. static const struct proc_ops kcore_proc_ops = {
  584. .proc_flags = PROC_ENTRY_PERMANENT,
  585. .proc_read_iter = read_kcore_iter,
  586. .proc_open = open_kcore,
  587. .proc_release = release_kcore,
  588. .proc_lseek = default_llseek,
  589. };
  590. /* just remember that we have to update kcore */
  591. static int __meminit kcore_callback(struct notifier_block *self,
  592. unsigned long action, void *arg)
  593. {
  594. switch (action) {
  595. case MEM_ONLINE:
  596. case MEM_OFFLINE:
  597. kcore_need_update = 1;
  598. break;
  599. }
  600. return NOTIFY_OK;
  601. }
  602. static struct kcore_list kcore_vmalloc;
  603. #ifdef CONFIG_ARCH_PROC_KCORE_TEXT
  604. static struct kcore_list kcore_text;
  605. /*
  606. * If defined, special segment is used for mapping kernel text instead of
  607. * direct-map area. We need to create special TEXT section.
  608. */
  609. static void __init proc_kcore_text_init(void)
  610. {
  611. kclist_add(&kcore_text, _text, _end - _text, KCORE_TEXT);
  612. }
  613. #else
  614. static void __init proc_kcore_text_init(void)
  615. {
  616. }
  617. #endif
  618. #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
  619. /*
  620. * MODULES_VADDR has no intersection with VMALLOC_ADDR.
  621. */
  622. static struct kcore_list kcore_modules;
  623. static void __init add_modules_range(void)
  624. {
  625. if (MODULES_VADDR != VMALLOC_START && MODULES_END != VMALLOC_END) {
  626. kclist_add(&kcore_modules, (void *)MODULES_VADDR,
  627. MODULES_END - MODULES_VADDR, KCORE_VMALLOC);
  628. }
  629. }
  630. #else
  631. static void __init add_modules_range(void)
  632. {
  633. }
  634. #endif
  635. static int __init proc_kcore_init(void)
  636. {
  637. proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &kcore_proc_ops);
  638. if (!proc_root_kcore) {
  639. pr_err("couldn't create /proc/kcore\n");
  640. return 0; /* Always returns 0. */
  641. }
  642. /* Store text area if it's special */
  643. proc_kcore_text_init();
  644. /* Store vmalloc area */
  645. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  646. VMALLOC_END - VMALLOC_START, KCORE_VMALLOC);
  647. add_modules_range();
  648. /* Store direct-map area from physical memory map */
  649. kcore_update_ram();
  650. hotplug_memory_notifier(kcore_callback, DEFAULT_CALLBACK_PRI);
  651. return 0;
  652. }
  653. fs_initcall(proc_kcore_init);