From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7866C004D4 for ; Thu, 19 Jan 2023 12:48:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DD7D86B0072; Thu, 19 Jan 2023 07:48:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D60DA6B0073; Thu, 19 Jan 2023 07:48:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDA2E6B0074; Thu, 19 Jan 2023 07:48:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A6EB86B0072 for ; Thu, 19 Jan 2023 07:48:54 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 637ED803F2 for ; Thu, 19 Jan 2023 12:48:54 +0000 (UTC) X-FDA: 80371528188.30.86D5FB9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf09.hostedemail.com (Postfix) with ESMTP id BD8CA140003 for ; Thu, 19 Jan 2023 12:48:52 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WslKRf06; spf=pass (imf09.hostedemail.com: domain of bhe@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=bhe@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674132532; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YAKRFMXRp2ApOVKi5cAZjE7qcay8g+VNvLJ9cfYcxHU=; b=t8rIzOImN7ru/7uBm6RF4QenFKwRP5NEbVzccXdFjuKVDe4QsT5IVinQ2Ty5eWQQCGQujT 87PkZq7+UCwgAJn/MICcTlNyNm7rrVEUTjAQ+vVLbPiZfCqM392ze/9DJ84VaVCu9IAP21 mtI6kRnUYv6vpywXYXSLM9foZwczz24= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WslKRf06; spf=pass (imf09.hostedemail.com: domain of bhe@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=bhe@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674132532; a=rsa-sha256; cv=none; b=7PeUTKQ/soAXfHeL1+R6hUSZBbIP7M23Z0OI9SizuwRZV0nCGrRQ/pi5iZ6xlnVx2IMiSB RnSDGh+BXEt+jU9IJDZtd8r0mnydlE9ahAxXCmbLYXw97Xw7HPgVLEYxrW0zeycUc9n5S8 ltXuFrm6MLBkssnfdln7CnaYLB4wjU8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674132532; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=YAKRFMXRp2ApOVKi5cAZjE7qcay8g+VNvLJ9cfYcxHU=; b=WslKRf06WqiHcXSCSdSG10+Ow7vN6ze4wmFW5u7zqgnJcwjwVTeCbYkC/oGJmNBQmbQux8 MWbSFpfsm4DXMvGvrRxIc6vJg8Jia9l5ED56rEiilZbFrxH32e4OmwLRcOcEcjDaKTwg/9 Ku5VaCovG4Y5LYXmwgQuLGDJ174Gwi4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-674-P25k8TtnNPWS2eseBHZwiA-1; Thu, 19 Jan 2023 07:48:48 -0500 X-MC-Unique: P25k8TtnNPWS2eseBHZwiA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 608A1183B3C0; Thu, 19 Jan 2023 12:48:48 +0000 (UTC) Received: from localhost (ovpn-13-24.pek2.redhat.com [10.72.13.24]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 853622026D68; Thu, 19 Jan 2023 12:48:47 +0000 (UTC) Date: Thu, 19 Jan 2023 20:48:43 +0800 From: Baoquan He To: Uladzislau Rezki Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, lstoakes@gmail.com, stephen.s.brennan@oracle.com, willy@infradead.org, akpm@linux-foundation.org, hch@infradead.org Subject: Re: [PATCH v3 3/7] mm/vmalloc.c: allow vread() to read out vm_map_ram areas Message-ID: References: <20230113031921.64716-1-bhe@redhat.com> <20230113031921.64716-4-bhe@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: BD8CA140003 X-Rspam-User: X-Stat-Signature: r5qy8ep5hgc1wy9k7agfhh5gww13uwmf X-HE-Tag: 1674132532-657862 X-HE-Meta: U2FsdGVkX192oBZKOkutEGdjNcyJsf0Ywo93b4Rx6eG6fCwV27Lizl1a2/nY0yjsaW/+W2AfJV/iuxeANZi0/YMeKQyc+f3AjMFMMf86FVAYdWj2o1xz980J+vyaZbrw/in1YnvyvSVGoSdfb4JAhOBjHQjGeZ8B/K4/vlbNgym74qn9fCUALx8XdMlx9y+VRwjQ4VI1j11XXqV0lZsrdE39VQuWvamFvEQmrYW+MeRzAFu+L7JUci6ON8bycPRB7wtJ56KcfeTAIhOogWKtshOsaJ0iV1BimWILB8QGSheGC1FiJqrA+vhhbZFw1EiiKl40lwz8ye7uQgZ6JLNtAnLWr0h/+YrsX3Ci28jDuZy30417W9CxWDLRU3V3msBmRIc7hm6oy1usi8WuklwQLtver6dU0QJ3Dqh3SG1dBJ3l9p8R32o+J8AY4emh6miNfLGfvDtj7mKz7OxCvtwbfrHjEa6uUwvbmPwbsLFra/pTy7f0v9bS7zo0w7FsG4ITQ1GT/6P/M41ov49uDUaCTrR0GE57wnghG5Zpiy/P9ZwpQMD05nHBVNtMraBtsSL/bo3UC/ffIuCkA8uNP//twz2UOPP06i3y4hY5EuVwkx3SHZLBteCSXE572vbjZb7G0uxM5MxgtXaIm+35eWC2UcTCjaqZD5pYy0efiP8NAUucfT1Qr0gQrbC9urwh51BpeG2BNGZsXhNDHntO/qC0KN5E0r19y/RPR6TwvoLSeo0Bmah2cO91X4a9n3Oa6K1twfwcORSrwU8WluIe+sJi6PxYWCNiDimGcLGz/pmf7czXLEdOmlqLBI4lYzO1VpNNfkqda2k/AuqsPi82O149UZBncmrfnEHahFeT28RAultQoxn4kxeJo2YOCWEFGpUhaVudW5edO2TEmaBE2m1+AW4FslT95NBA8WlGRL6vflo= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 01/19/23 at 05:52pm, Baoquan He wrote: > On 01/16/23 at 12:50pm, Uladzislau Rezki wrote: > > On Fri, Jan 13, 2023 at 11:19:17AM +0800, Baoquan He wrote: > > > Currently, vread can read out vmalloc areas which is associated with > > > a vm_struct. While this doesn't work for areas created by vm_map_ram() > > > interface because it doesn't have an associated vm_struct. Then in vread(), > > > these areas are all skipped. > > > > > > Here, add a new function vmap_ram_vread() to read out vm_map_ram areas. > > > The area created with vmap_ram_vread() interface directly can be handled > > > like the other normal vmap areas with aligned_vread(). While areas > > > which will be further subdivided and managed with vmap_block need > > > carefully read out page-aligned small regions and zero fill holes. > > > > > > Signed-off-by: Baoquan He > > > --- > > > mm/vmalloc.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++----- > > > 1 file changed, 73 insertions(+), 7 deletions(-) > > > > > > diff --git a/mm/vmalloc.c b/mm/vmalloc.c > > > index ab4825050b5c..13875bc41e27 100644 > > > --- a/mm/vmalloc.c > > > +++ b/mm/vmalloc.c > > > @@ -3544,6 +3544,65 @@ static int aligned_vread(char *buf, char *addr, unsigned long count) > > > return copied; > > > } > > > > > > +static void vmap_ram_vread(char *buf, char *addr, int count, unsigned long flags) > > > +{ > > > + char *start; > > > + struct vmap_block *vb; > > > + unsigned long offset; > > > + unsigned int rs, re, n; > > > + > > > + /* > > > + * If it's area created by vm_map_ram() interface directly, but > > > + * not further subdividing and delegating management to vmap_block, > > > + * handle it here. > > > + */ > > > + if (!(flags & VMAP_BLOCK)) { > > > + aligned_vread(buf, addr, count); > > > + return; > > > + } > > > + > > > + /* > > > + * Area is split into regions and tracked with vmap_block, read out > > > + * each region and zero fill the hole between regions. > > > + */ > > > + vb = xa_load(&vmap_blocks, addr_to_vb_idx((unsigned long)addr)); > > > + > > > + spin_lock(&vb->lock); > > > + if (bitmap_empty(vb->used_map, VMAP_BBMAP_BITS)) { > > > > > CPU-X invokes free_vmap_block() whereas we take the vb->lock and do > > some manipulations with vb that might be already freed over RCU-core. > > > > Should we protect it by the rcu_read_lock() also here? > > Just go over the vb and vbq code again, seems we don't need the > rcu_read_lock() here. The rcu lock is needed when operating on the > vmap_block_queue->free list. I don't see race between the vb accessing > here and those list adding or removing on vmap_block_queue->free with > rcu. If I miss some race windows between them, please help point out. > > However, when I check free_vmap_block(), I do find a risk. As you said, Forgot to add details about why there's no race between free_vmap_block() and vmap_ram_vread() because we have taken vmap_area_lock at the beginning of vread(). So, except of the missing checking on returned value from xa_load(), free_vmap_block() either is blocked to wait for vmap_area_lock before calling unlink_va(), or finishes calling unlink_va() to remove the vmap from vmap_area_root tree. In both cases, no race happened. > CPU-x invokes free_vmap_block() and executed xa_erase() to remove the vb > from vmap_blocks tree. Then vread() comes into vmap_ram_vread() and call > xa_load(), it would be null. I should check the returned vb in > free_vmap_block(). > > > static void vmap_ram_vread(char *buf, char *addr, int count, unsigned long flags) > { > ...... > if (!(flags & VMAP_BLOCK)) { > aligned_vread(buf, addr, count); > return; > } > > /* > * Area is split into regions and tracked with vmap_block, read out > * each region and zero fill the hole between regions. > */ > vb = xa_load(&vmap_blocks, addr_to_vb_idx((unsigned long)addr)); > if (!vb) <-- vb need be checked here to avoid accessing erased vb from vmap_blocks tree > memset(buf, 0, count); > ...... > } >