Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp2401890ybc; Wed, 20 Nov 2019 13:41:07 -0800 (PST) X-Google-Smtp-Source: APXvYqwHPj76J1QzGEjDMFP3l8spoIA5dqzQPUtuIMzYLVjUd+He1IbyGq4xuqchfi70W15/8q14 X-Received: by 2002:a17:906:751:: with SMTP id z17mr8027481ejb.313.1574286066880; Wed, 20 Nov 2019 13:41:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574286066; cv=none; d=google.com; s=arc-20160816; b=YBQs4Z6BbwrkmfwVhtXYAXpmHeIjzQFlahdJKGf2CPkcKIY8Dg25y5fhkAIevHOQl3 I2g4PvoiozFeffjZt4WPqtQa6kYAp/ACymxVGr3KT0lPwphqEweZ6ov1fPs9AdBFV44N ukkBX9vuALEqNMLPdJ3sSMAN5+Ogl9mItQceUNaxnIZum3a1b7ARDfizVaFB3bOwTaFB LPT+hZJ4UpmeVsBYm0VreHeQomuVCasotT8UNue5R1EvhSnURBbnAT6tQNX5Ux3h8Fix PsMSzlaAEClSPcrmU0H6VWa2LNGHq3TdiLNeaxmCj9YUh09NuNBGuVJtIbkjb1bJ2pPz qK7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=F4UOOlylYKA4x5XwS/IcSHBpAnEOvDZz0DmXXFZBpcA=; b=fM9Wi2rwEsyakGiuVUKoLwxzNi39WkumElpEh96RJKXuNUvRUt21Z4j3BKwcHOLydW SO6mz3lS3eVuB++DNvOEa5DzkMfs9RIweWHn46rGxK8yxFzudvy46558B54E9B4JxYxQ oYzR7xiTXNmBIwK3QjmfibTf28JVpCgnzdEHgTTvRxm/DonIQgmnMjHlMHeKmykfoJYw lfOpeMVnnjNyzOIk6h1Hc/HMzozjy1C2HA+NYn2YrrqSXTfFpTnlR4oApT2Gcsf4J+18 YHgSNU+El95/3Q6YkKwHmrGF/10Qm0BiVhVgJdV2K82LNWA7pPgBjVd1kQM/E0rRCrYF Pnpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=HWlZvj1c; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o1si282943eje.298.2019.11.20.13.40.42; Wed, 20 Nov 2019 13:41:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=HWlZvj1c; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726396AbfKTVje (ORCPT + 99 others); Wed, 20 Nov 2019 16:39:34 -0500 Received: from mail-qt1-f194.google.com ([209.85.160.194]:41087 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725854AbfKTVje (ORCPT ); Wed, 20 Nov 2019 16:39:34 -0500 Received: by mail-qt1-f194.google.com with SMTP id o3so1214079qtj.8 for ; Wed, 20 Nov 2019 13:39:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=F4UOOlylYKA4x5XwS/IcSHBpAnEOvDZz0DmXXFZBpcA=; b=HWlZvj1c8bNqbGRODv0AgUEGYJvuTzXv3y+PxWcWyLnvLfcKURUZIzxXfU266k3473 zbm+2dXSBVQG2y2z1Oz1GVrmV3K4GqVzfEG7zivXUO+Tam1zvCqDHyjQpjdWw5WCQwLm lzbmvkCSZzNFsiSddiHgU/kP3GzsDD8kMA6FBzb/GPXKxoZTvK43OXZwWhrLYABsdeLv GZzAl8OT/xf1vbkEt4gyPza/zcf80oiyWvZOoFYYtZA3z2QPhwDT+f6UtVa8Ol7zZ6Xd Z9ETSaZqziR1SUsWpnqyTYIPJuX9pTSwaZV0jCr+JJoXoHoF6WNgYWqzHywVVMTJWeDm 6YrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=F4UOOlylYKA4x5XwS/IcSHBpAnEOvDZz0DmXXFZBpcA=; b=eESyPz90dxI5rgg/1zM27yVc9d87YU5MmM/isBTAK4XOHH8PQBfth8NWhynEVKPDjx ko6Y2YrM2/LqDy4gZ1aul7kykKZIU9Qzus25p/5aullXnsibMh0T7SfQCnA36F1Zvzpw WOmqyNwYUys+I1cEXo5Iy6nO7M/nAGdgQK3BktZZQkeDdwCyT+d9iqjwmMgsLXhuAf78 b/ilgiXmd8Y6gKP/wgIlSys4Fc/ov/RQTFVadzYvHEHMQ6KXlpzhqFvBOq7wGwqYFm0n 8L+FuQLO8VqRU7BKdbS1zIzuPNprvADR2To5qO8RGFppEONJV2opXz90dek4MxAij7Wk 8d7w== X-Gm-Message-State: APjAAAXu7bTqBwBB6Rw6Y+gwys4DEGioFz9ilCs6pgwOl4xxeg8dO1yz sVWGxB04H8ldO4Rjppv99Q4WuA== X-Received: by 2002:ac8:6697:: with SMTP id d23mr4730012qtp.32.1574285973134; Wed, 20 Nov 2019 13:39:33 -0800 (PST) Received: from localhost ([2620:10d:c091:500::6900]) by smtp.gmail.com with ESMTPSA id l11sm199918qtq.20.2019.11.20.13.39.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2019 13:39:32 -0800 (PST) Date: Wed, 20 Nov 2019 16:39:31 -0500 From: Johannes Weiner To: Shakeel Butt Cc: Andrew Morton , Hugh Dickins , Michal Hocko , Alex Shi , Roman Gushchin , Linux MM , Cgroups , LKML , Kernel Team Subject: Re: [PATCH] mm: fix unsafe page -> lruvec lookups with cgroup charge migration Message-ID: <20191120213931.GB428283@cmpxchg.org> References: <20191120165847.423540-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.12.2 (2019-09-21) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 20, 2019 at 12:31:06PM -0800, Shakeel Butt wrote: > On Wed, Nov 20, 2019 at 8:58 AM Johannes Weiner wrote: > > > > While reviewing the "per lruvec lru_lock for memcg" series, Hugh and I > > noticed two places in the existing code where the page -> memcg -> > > lruvec lookup can result in a use-after-free bug. This affects cgroup1 > > setups that have charge migration enabled. > > > > To pin page->mem_cgroup, callers need to either have the page locked, > > an exclusive refcount (0), or hold the lru_lock and "own" PageLRU > > (either ensure it's set, or be the one to hold the page in isolation) > > to make cgroup migration fail the isolation step. > > I think we should add the above para in the comments for better visibility. Good idea. I'm attaching a delta patch below. > > Reported-by: Hugh Dickins > > Signed-off-by: Johannes Weiner > > Reviewed-by: Shakeel Butt Thanks! --- From 73b58ce09009cce668ea97d9e047611c60e95bd6 Mon Sep 17 00:00:00 2001 From: Johannes Weiner Date: Wed, 20 Nov 2019 16:36:03 -0500 Subject: [PATCH] mm: fix unsafe page -> lruvec lookups with cgroup charge migration fix Better document the mem_cgroup_page_lruvec() caller requirements. Suggested-by: Shakeel Butt Signed-off-by: Johannes Weiner --- mm/memcontrol.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 50f5bc55fcec..2d700fa0d7f4 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1202,9 +1202,18 @@ int mem_cgroup_scan_tasks(struct mem_cgroup *memcg, * @page: the page * @pgdat: pgdat of the page * - * This function is only safe when following the LRU page isolation - * and putback protocol: the LRU lock must be held, and the page must - * either be PageLRU() or the caller must have isolated/allocated it. + * NOTE: The returned lruvec is only stable if the calling context has + * the page->mem_cgroup pinned! This is accomplished by satisfying one + * of the following criteria: + * + * a) have the @page locked + * b) have an exclusive reference to @page (e.g. refcount 0) + * c) hold the lru_lock and "own" the PageLRU (meaning either ensure + * it's set, or be the one to hold the page in isolation) + * + * Otherwise, the page could be freed or moved out of the memcg, + * thereby releasing its reference on the memcg and potentially + * freeing it and its lruvecs in the process. */ struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgdat) { -- 2.24.0