Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2379812pxj; Sun, 16 May 2021 23:48:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzxLj09lOH5CXj9B5qL6XczSJdVkoYaaj10Arj/eIV1p0OTEr55jSlNlKFDf9A8tQsknY95 X-Received: by 2002:a02:8787:: with SMTP id t7mr46101518jai.53.1621234117683; Sun, 16 May 2021 23:48:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621234117; cv=none; d=google.com; s=arc-20160816; b=B0FOVrJhT7LJjFnLLZCKcWGIkoIbhf8jZuQwA4xGNHxb2EW8faBE8sPiczCLEA1R9C VSK5LSJdB6P7h7Nu7SnXtDQ/PKSyNa+DMg596VbgIEV1ly0fpA2RT5lMBS9lFU/XJt2D ma+i2rqTM90JeFGVkIKqJhc3fVleds16w/+3mdb/m+d76PHDFx8MvmNbRnV8iG5XYldK pxvaaH4PTUEJGswVzhyB+Hr00Dpt98EyupzrUxjSuHcMIREnu949Yt7+0FzZRss78eMO JD0zBnf7fJ/Hf1l65F0Q0N+hxbdtkg0B3IGQk6uwGHPd9FRjR76zQy8PwbZbSIBTFSd9 2B8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=+XIsOIw1Wz5POj16b/wLwnZ9zjeS6Y7s1OGp4Eye8rs=; b=NIN8sGa+4YJuNYV/iAYKJUwpg5wCRmUOJmlQ6Dx7YSBg56FfJIT40QSLyaqzKVHzxW 6MRMdhxoBMxVBTtZE+D566oYYKH8cU4Vh/IKm/DNNKsfTjl3mS6xH7RZY95wuybQ4YSV 6k6civsV0IOkXlc39pr/nEI0HiuV5Whfmw+V96qJUWy75mK3n9TqZft3tmLt88DXnVPy loMYWaXpjmZxOrFep+MYSz/Ew0tC6OwfbwNStIag+8Kr/+tsbM5Xe4Xqvp9jsCIn8EcZ 3ipIzeUs2hUsZZ3UGbcX3r3KtlpGpHxMBNLOI1Q/FQ6UnECKJcLkygOC5G6B09LVdR5U JU8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dznbhxtN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f10si4128003ilu.68.2021.05.16.23.48.25; Sun, 16 May 2021 23:48:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dznbhxtN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232040AbhEQGoi (ORCPT + 99 others); Mon, 17 May 2021 02:44:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:43526 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229527AbhEQGoh (ORCPT ); Mon, 17 May 2021 02:44:37 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 06B8860C40; Mon, 17 May 2021 06:43:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1621233801; bh=Auni3LFhqjW6BQy2QyQlRuaS4YtCakNMfUyiz2ozvTs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dznbhxtN4oNAsEmlJLL+kv6OEZRZElm7Ww7seTYYHSYjSPymeyi0d1vvtHuYd1T7Z 1uckVK7Kd6/b4OVdm45oH4GDKxIzIgzIqA6FD0OPbOVOqX2N5gON+bMliI+Zr/m10W nTBJzEAr/xTvZppZuRWf7i9YoMVxRMvnb02Y8sMIvBd7DOgEuV94HcesLCOZwBe5ta 5g4J0t+VESbo5ZrdFyLF57F2ooLlydHb3cxm3FpdLJFey0oEKsB8pGqQiu9ZV0Kppl hDvc31B3TgvcA2jLUyJRaxb85Ii92pev5BZYdW36wAeknUQr0Wiykel0L+VaRO4xZp U8VWF+3NL+SkQ== Date: Mon, 17 May 2021 09:43:08 +0300 From: Mike Rapoport To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, Andrew Morton , "Michael S. Tsirkin" , Jason Wang , Alexey Dobriyan , "Matthew Wilcox (Oracle)" , Oscar Salvador , Michal Hocko , Roman Gushchin , Alex Shi , Steven Price , Mike Kravetz , Aili Yao , Jiri Bohac , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Naoya Horiguchi , linux-hyperv@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v2 4/6] mm: introduce page_offline_(begin|end|freeze|thaw) to synchronize setting PageOffline() Message-ID: References: <20210514172247.176750-1-david@redhat.com> <20210514172247.176750-5-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210514172247.176750-5-david@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 14, 2021 at 07:22:45PM +0200, David Hildenbrand wrote: > A driver might set a page logically offline -- PageOffline() -- and > turn the page inaccessible in the hypervisor; after that, access to page > content can be fatal. One example is virtio-mem; while unplugged memory > -- marked as PageOffline() can currently be read in the hypervisor, this > will no longer be the case in the future; for example, when having > a virtio-mem device backed by huge pages in the hypervisor. > > Some special PFN walkers -- i.e., /proc/kcore -- read content of random > pages after checking PageOffline(); however, these PFN walkers can race > with drivers that set PageOffline(). > > Let's introduce page_offline_(begin|end|freeze|thaw) for > synchronizing. > > page_offline_freeze()/page_offline_thaw() allows for a subsystem to > synchronize with such drivers, achieving that a page cannot be set > PageOffline() while frozen. > > page_offline_begin()/page_offline_end() is used by drivers that care about > such races when setting a page PageOffline(). > > For simplicity, use a rwsem for now; neither drivers nor users are > performance sensitive. > > Acked-by: Michal Hocko > Signed-off-by: David Hildenbrand One nit below, otherwise Reviewed-by: Mike Rapoport > --- > include/linux/page-flags.h | 10 ++++++++++ > mm/util.c | 40 ++++++++++++++++++++++++++++++++++++++ > 2 files changed, 50 insertions(+) > > diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h > index daed82744f4b..ea2df9a247b3 100644 > --- a/include/linux/page-flags.h > +++ b/include/linux/page-flags.h > @@ -769,9 +769,19 @@ PAGE_TYPE_OPS(Buddy, buddy) > * relies on this feature is aware that re-onlining the memory block will > * require to re-set the pages PageOffline() and not giving them to the > * buddy via online_page_callback_t. > + * > + * There are drivers that mark a page PageOffline() and do not expect any Maybe "and expect there won't be any further access"... > + * further access to page content. PFN walkers that read content of random > + * pages should check PageOffline() and synchronize with such drivers using > + * page_offline_freeze()/page_offline_thaw(). > */ > PAGE_TYPE_OPS(Offline, offline) > > +extern void page_offline_freeze(void); > +extern void page_offline_thaw(void); > +extern void page_offline_begin(void); > +extern void page_offline_end(void); > + > /* > * Marks pages in use as page tables. > */ > diff --git a/mm/util.c b/mm/util.c > index a8bf17f18a81..a034525e7ba2 100644 > --- a/mm/util.c > +++ b/mm/util.c > @@ -1010,3 +1010,43 @@ void mem_dump_obj(void *object) > } > EXPORT_SYMBOL_GPL(mem_dump_obj); > #endif > + > +/* > + * A driver might set a page logically offline -- PageOffline() -- and > + * turn the page inaccessible in the hypervisor; after that, access to page > + * content can be fatal. > + * > + * Some special PFN walkers -- i.e., /proc/kcore -- read content of random > + * pages after checking PageOffline(); however, these PFN walkers can race > + * with drivers that set PageOffline(). > + * > + * page_offline_freeze()/page_offline_thaw() allows for a subsystem to > + * synchronize with such drivers, achieving that a page cannot be set > + * PageOffline() while frozen. > + * > + * page_offline_begin()/page_offline_end() is used by drivers that care about > + * such races when setting a page PageOffline(). > + */ > +static DECLARE_RWSEM(page_offline_rwsem); > + > +void page_offline_freeze(void) > +{ > + down_read(&page_offline_rwsem); > +} > + > +void page_offline_thaw(void) > +{ > + up_read(&page_offline_rwsem); > +} > + > +void page_offline_begin(void) > +{ > + down_write(&page_offline_rwsem); > +} > +EXPORT_SYMBOL(page_offline_begin); > + > +void page_offline_end(void) > +{ > + up_write(&page_offline_rwsem); > +} > +EXPORT_SYMBOL(page_offline_end); > -- > 2.31.1 > -- Sincerely yours, Mike.