mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: zhoufeng <zhoufeng.zf@bytedance.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: adobriyan@gmail.com, rppt@kernel.org,
	linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	songmuchun@bytedance.com, zhouchengming@bytedance.com,
	chenying.kernel@bytedance.com, zhengqi.arch@bytedance.com
Subject: Re: [External] Re: [PATCH] fs/proc/kcore.c: add mmap interface
Date: Tue, 1 Jun 2021 10:57:11 +0800	[thread overview]
Message-ID: <06da9f61-3003-078a-4e44-722e978cecd3@bytedance.com> (raw)
In-Reply-To: <20210531182344.e9692132981a5bf9bf6d4583@linux-foundation.org>



在 2021/6/1 上午9:23, Andrew Morton 写道:
> On Wed, 26 May 2021 15:51:42 +0800 Feng zhou <zhoufeng.zf@bytedance.com> wrote:
> 
>> From: ZHOUFENG <zhoufeng.zf@bytedance.com>
>>
>> When we do the kernel monitor, use the DRGN
>> (https://github.com/osandov/drgn) access to kernel data structures,
>> found that the system calls a lot. DRGN is implemented by reading
>> /proc/kcore. After looking at the kcore code, it is found that kcore
>> does not implement mmap, resulting in frequent context switching
>> triggered by read. Therefore, we want to add mmap interface to optimize
>> performance. Since vmalloc and module areas will change with allocation
>> and release, consistency cannot be guaranteed, so mmap interface only
>> maps KCORE_TEXT and KCORE_RAM.
>>
>> ...
>>
>> --- a/fs/proc/kcore.c
>> +++ b/fs/proc/kcore.c
>> @@ -573,11 +573,81 @@ static int release_kcore(struct inode *inode, struct file *file)
>>   	return 0;
>>   }
>>   
>> +static vm_fault_t mmap_kcore_fault(struct vm_fault *vmf)
>> +{
>> +	return VM_FAULT_SIGBUS;
>> +}
>> +
>> +static const struct vm_operations_struct kcore_mmap_ops = {
>> +	.fault = mmap_kcore_fault,
>> +};
>> +
>> +static int mmap_kcore(struct file *file, struct vm_area_struct *vma)
>> +{
>> +	size_t size = vma->vm_end - vma->vm_start;
>> +	u64 start, pfn;
>> +	int nphdr;
>> +	size_t data_offset;
>> +	size_t phdrs_len, notes_len;
>> +	struct kcore_list *m = NULL;
>> +	int ret = 0;
>> +
>> +	down_read(&kclist_lock);
>> +
>> +	get_kcore_size(&nphdr, &phdrs_len, &notes_len, &data_offset);
>> +
>> +	start = kc_offset_to_vaddr(((u64)vma->vm_pgoff << PAGE_SHIFT) -
>> +		((data_offset >> PAGE_SHIFT) << PAGE_SHIFT));
>> +
>> +	list_for_each_entry(m, &kclist_head, list) {
>> +		if (start >= m->addr && size <= m->size)
>> +			break;
>> +	}
>> +
>> +	if (&m->list == &kclist_head) {
>> +		ret = -EINVAL;
>> +		goto out;
>> +	}
>> +
>> +	if (vma->vm_flags & (VM_WRITE | VM_EXEC)) {
>> +		ret = -EPERM;
>> +		goto out;
>> +	}
>> +
>> +	vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
>> +	vma->vm_flags |= VM_MIXEDMAP;
>> +	vma->vm_ops = &kcore_mmap_ops;
>> +
>> +	if (kern_addr_valid(start)) {
>> +		if (m->type == KCORE_RAM || m->type == KCORE_REMAP)
>> +			pfn = __pa(start) >> PAGE_SHIFT;
>> +		else if (m->type == KCORE_TEXT)
>> +			pfn = __pa_symbol(start) >> PAGE_SHIFT;
>> +		else {
>> +			ret = -EFAULT;
>> +			goto out;
>> +		}
>> +
>> +		if (remap_pfn_range(vma, vma->vm_start, pfn, size,
>> +				vma->vm_page_prot)) {
>> +			ret = -EAGAIN;
> 
> EAGAIN seems a strange errno for this case.   The mmap manpage says
> 
>         EAGAIN The file has been locked, or too much  memory  has  been  locked
>                (see setrlimit(2)).
> 
> 
> remap_pfn_range() already returns an errno - why not return whatever
> that code was?
> 

   yes, that's a good idea.

>> +			goto out;
>> +		}
>> +	} else {
>> +		ret = -EFAULT;
>> +	}
>> +
>> +out:
>> +	up_read(&kclist_lock);
>> +	return ret;
>> +}
>> +
>>   static const struct proc_ops kcore_proc_ops = {
>>   	.proc_read	= read_kcore,
>>   	.proc_open	= open_kcore,
>>   	.proc_release	= release_kcore,
>>   	.proc_lseek	= default_llseek,
>> +	.proc_mmap	= mmap_kcore,
>>   };
>>   
>>   /* just remember that we have to update kcore */
> 
> Otherwise looks OK to me.  Please update the changelog to reflect the
> discussion thus far and send a v2?
> 
   OK, I am very happy to do so, I will send a v2 in two days.

      reply	other threads:[~2021-06-01  2:57 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-26  7:51 Feng zhou
2021-05-27  0:39 ` Andrew Morton
2021-05-27  6:37   ` [External] " zhoufeng
     [not found]   ` <d71a4ffa-f21e-62f5-7fa6-83ca14b3f05b@bytedance.com>
2021-05-27 22:30     ` Andrew Morton
2021-05-28  2:10       ` zhoufeng
2021-06-01  1:23 ` Andrew Morton
2021-06-01  2:57   ` zhoufeng [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=06da9f61-3003-078a-4e44-722e978cecd3@bytedance.com \
    --to=zhoufeng.zf@bytedance.com \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=chenying.kernel@bytedance.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rppt@kernel.org \
    --cc=songmuchun@bytedance.com \
    --cc=zhengqi.arch@bytedance.com \
    --cc=zhouchengming@bytedance.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®