kcore.c 17 KB

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