From: Michal Hocko Subject: Re: Possible race in obj_stock_flush_required() vs drain_obj_stock() Date: Mon, 3 Oct 2022 16:27:00 +0200 Message-ID: References: <1664546131660.1777662787.1655319815@gmail.com> <821923d8-17c3-f1c2-4d6a-5653c88db3e8@gmail.com> Mime-Version: 1.0 Return-path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1664807221; h=from:from:reply-to: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=aLlkpgbxGZRMNBxuPIGalwtLBIi7tqsIbhglT3gkCtE=; b=ZaYAqscq9DPqSdfPYl7g0L7ZKm7z8h31kFJ9l89bcayffbVyeEqX/uXywgl2u7P8wyGleU mYDUrIrM26hJ63fFc0/NF9Cbj2lk1J4MtvjxAvdtWDg/GwD1BadGV4D6xpGzlFyy5RM6qS hn/LjFfv5NyNd0Tvc2nBDbpT+D7krOw= Content-Disposition: inline In-Reply-To: <821923d8-17c3-f1c2-4d6a-5653c88db3e8-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> List-ID: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Alexander Fedorov Cc: Roman Gushchin , Johannes Weiner , Shakeel Butt , Vladimir Davydov , Muchun Song , Sebastian Andrzej Siewior , cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org On Mon 03-10-22 17:09:15, Alexander Fedorov wrote: > On 03.10.2022 16:32, Michal Hocko wrote: > > On Mon 03-10-22 15:47:10, Alexander Fedorov wrote: > >> @@ -3197,17 +3197,30 @@ static void drain_obj_stock(struct memcg_stock_pcp *stock) > >> stock->nr_bytes = 0; > >> } > >> > >> - obj_cgroup_put(old); > >> + /* > >> + * Clear pointer before freeing memory so that > >> + * drain_all_stock() -> obj_stock_flush_required() > >> + * does not see a freed pointer. > >> + */ > >> stock->cached_objcg = NULL; > >> + obj_cgroup_put(old); > > > > Do we need barrier() or something else to ensure there is no reordering? > > I am not reallyu sure what kind of barriers are implied by the pcp ref > > counting. > > obj_cgroup_put() -> kfree_rcu() -> synchronize_rcu() should take care > of this: This is a very subtle guarantee. Also it would only apply if this is the last reference, right? Is there any reason to not use WRITE_ONCE(stock->cached_objcg, NULL); obj_cgroup_put(old); IIRC this should prevent any reordering. -- Michal Hocko SUSE Labs