Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1352832pxy; Thu, 29 Apr 2021 05:28:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxURTWwdLUWP8Gw3QwAMCxl6mtZY0sn1IXNlz4xTvFSw8LcyBTEvee4+LeRvdJB4GELVLvQ X-Received: by 2002:a17:90a:a60b:: with SMTP id c11mr37993896pjq.125.1619699285413; Thu, 29 Apr 2021 05:28:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619699285; cv=none; d=google.com; s=arc-20160816; b=Exz+LScHuD6Mp5QnSaKUuILExEIzzgGdLtq7B1T4nWJ38jWHheoriSwCgeINMKuVf2 KlUI7rC5IvSJbTVuGZtNvNFV4siogTWLqP4IfHJAky+3ETG/iDb2BeO83LMnk/mITcXs MJz8IcrJ/GVSFuAy2e889CpLdc0MTEjKDLr1++MK8ieCoaEn6pj1SMphvM3QO9Vd+us6 DUVHLpXShCvhiaKdnQDzBt96PLksgHPhqDhgglFxuCX6qArAW+SOC/FECb+zoUyfF82E RqsIQZkouJarTYL5sDbbxxY+sUtMbcA3dAg//x1Jo7NUUinsncHMpoDTsg6yXrvwSEiL yJIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OqK47sG85aZfcKTSSN25XorwOXhWot6GcaTYJAm0h7w=; b=b5GR0Spp7jXelvGV3J9C83EsyUAdgSbWB6iX+6Ikp5HTWvGMpUPaYLpEsPrFyYywln EQDyhNsyerGPDPyRST/5EU8R1yh/dV215rkCqa2APr1bnOg4gxw4zHyEgrWzA1P5U7c3 1ZCGL4t//jF21Dt1bZd3kNZ7s1su/4rC+lHVbE7j1dXQioHK60LDeX5JnB7q/9Jp5AwJ h2d1uvH6jQylzKPkD7Yn3nA9n9LvtEZoLCLY39FbS3Bvq6h3S/pPWDpSNoA3B8/hTkah zC4BohR53rcdO8hMR6VhtcwGzAhP1c/sBZmjN0Nz0QP66Map9x2DRJMwxZcdO1dqlggy hyuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BMUB1aYE; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h13si3401640plr.95.2021.04.29.05.27.52; Thu, 29 Apr 2021 05:28:05 -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=@redhat.com header.s=mimecast20190719 header.b=BMUB1aYE; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231343AbhD2M11 (ORCPT + 99 others); Thu, 29 Apr 2021 08:27:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:56370 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231564AbhD2M10 (ORCPT ); Thu, 29 Apr 2021 08:27:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619699199; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OqK47sG85aZfcKTSSN25XorwOXhWot6GcaTYJAm0h7w=; b=BMUB1aYEJXWkmgtPRK/lvs/unrJ2bPdaBzYuCWVwX0d8MoyVX5/5pLvoRnA3PMdVvzXkRn G5UuDRcdd6AeCE6k+lhy5QdE7pmWi2p28wu0A+RWrGht8XGpxDFIBi2DsKg+03+O7RXLD2 ol6HFHLNpKsGkeMAuLxDBNIjm4KlFXM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-262-BOB6ZMW4OPaam5VXHtscdg-1; Thu, 29 Apr 2021 08:26:37 -0400 X-MC-Unique: BOB6ZMW4OPaam5VXHtscdg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0763910054F6; Thu, 29 Apr 2021 12:26:35 +0000 (UTC) Received: from t480s.redhat.com (ovpn-114-50.ams2.redhat.com [10.36.114.50]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4B8A118796; Thu, 29 Apr 2021 12:26:15 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: David Hildenbrand , Andrew Morton , "Michael S. Tsirkin" , Jason Wang , Alexey Dobriyan , Mike Rapoport , "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: [PATCH v1 5/7] mm: introduce page_offline_(begin|end|freeze|unfreeze) to synchronize setting PageOffline() Date: Thu, 29 Apr 2021 14:25:17 +0200 Message-Id: <20210429122519.15183-6-david@redhat.com> In-Reply-To: <20210429122519.15183-1-david@redhat.com> References: <20210429122519.15183-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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|unfreeze) for synchronizing. page_offline_freeze()/page_offline_unfreeze() 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. Signed-off-by: David Hildenbrand --- include/linux/page-flags.h | 5 +++++ mm/util.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index b8c56672a588..e3d00c72f459 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -767,6 +767,11 @@ PAGE_TYPE_OPS(Buddy, buddy) */ PAGE_TYPE_OPS(Offline, offline) +extern void page_offline_freeze(void); +extern void page_offline_unfreeze(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 54870226cea6..95395d4e4209 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1013,3 +1013,41 @@ void mem_dump_obj(void *object) } pr_cont(" non-slab/vmalloc memory.\n"); } + +/* + * 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_unfreeze() 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_unfreeze(void) +{ + up_read(&page_offline_rwsem); +} + +void page_offline_begin(void) +{ + down_write(&page_offline_rwsem); +} + +void page_offline_end(void) +{ + up_write(&page_offline_rwsem); +} -- 2.30.2