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 EACECC02194 for ; Thu, 6 Feb 2025 10:42:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 600406B008C; Thu, 6 Feb 2025 05:42:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5B076280002; Thu, 6 Feb 2025 05:42:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3DC986B0093; Thu, 6 Feb 2025 05:42:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 1FCE56B008C for ; Thu, 6 Feb 2025 05:42:19 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 740911A106D for ; Thu, 6 Feb 2025 10:42:02 +0000 (UTC) X-FDA: 83089179684.22.4826AD1 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf07.hostedemail.com (Postfix) with ESMTP id E34DB4004E for ; Thu, 6 Feb 2025 10:41:59 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=EF+WvkQq; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=ylF0STyd; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=EF+WvkQq; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=ylF0STyd; spf=pass (imf07.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738838520; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9nAH5O3dKn0ela33TBo40pW/2FZBzDM9roViKwIVSsc=; b=kD8f0a+s0GCFpYTdCnG3VbavMteX3sQu4viKitoJDJdV/NlUPHgNtj45jHUvGaSkURRknr n7w3wEc1EG/NP4G6YV2hy17ayiHpRzgEKlno95FInHMN7e3Bx/j29YvBUpxpJ8n4WldsMt m5PQqxnE0AJdePXuQBk5zA+qWA0BJbg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738838520; a=rsa-sha256; cv=none; b=eJG27YuCfYMyrZVflFaoJJwYi598UltGFJwygZoKi68+9lTj09Z3tnwDVUJfpP7LTZ73gX OOxfHtI6vK/eiSSTjil5x6N7aXY1MVIKBe371I6u1jwfIrdegGb4AXtpZPBPXFt+yTE8Gr XwHYVOZbv4bXhIa84UQAANO9Hd86S+Y= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=EF+WvkQq; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=ylF0STyd; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=EF+WvkQq; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=ylF0STyd; spf=pass (imf07.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 3DC5E21163; Thu, 6 Feb 2025 10:41:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1738838518; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=9nAH5O3dKn0ela33TBo40pW/2FZBzDM9roViKwIVSsc=; b=EF+WvkQqx0DT1Ro/yrSMO2X5MkW2SmGGMo/f1j4Zx/IF8HCOcQACTIUU4X60PHx34Im/OP VilMyPykC5x0E6sC01/QG1RBsV4BUn4ASQXq0Hv3zmh/zn50kD8FJYloURCessGvGxAa1Y cJYCIDejPsqQIf72jThgu6umMd/aE+E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1738838518; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=9nAH5O3dKn0ela33TBo40pW/2FZBzDM9roViKwIVSsc=; b=ylF0STydj7lcDT4Z8Ch27M0XE2RLGw72IGfCsMrUvLBN4+T2UEHkkjv+KHZesbnUfWscYs Grq8+K3pyHO8F6AA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1738838518; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=9nAH5O3dKn0ela33TBo40pW/2FZBzDM9roViKwIVSsc=; b=EF+WvkQqx0DT1Ro/yrSMO2X5MkW2SmGGMo/f1j4Zx/IF8HCOcQACTIUU4X60PHx34Im/OP VilMyPykC5x0E6sC01/QG1RBsV4BUn4ASQXq0Hv3zmh/zn50kD8FJYloURCessGvGxAa1Y cJYCIDejPsqQIf72jThgu6umMd/aE+E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1738838518; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=9nAH5O3dKn0ela33TBo40pW/2FZBzDM9roViKwIVSsc=; b=ylF0STydj7lcDT4Z8Ch27M0XE2RLGw72IGfCsMrUvLBN4+T2UEHkkjv+KHZesbnUfWscYs Grq8+K3pyHO8F6AA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id E8FBA13694; Thu, 6 Feb 2025 10:41:57 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id exVEN/WRpGfoXAAAD6G6ig (envelope-from ); Thu, 06 Feb 2025 10:41:57 +0000 Message-ID: Date: Thu, 6 Feb 2025 11:41:57 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/1] refcount: provide ops for cases when object's memory can be reused To: Suren Baghdasaryan , peterz@infradead.org Cc: will@kernel.org, paulmck@kernel.org, boqun.feng@gmail.com, mark.rutland@arm.com, mjguzik@gmail.com, akpm@linux-foundation.org, willy@infradead.org, liam.howlett@oracle.com, lorenzo.stoakes@oracle.com, david.laight.linux@gmail.com, mhocko@suse.com, hannes@cmpxchg.org, oliver.sang@intel.com, mgorman@techsingularity.net, david@redhat.com, peterx@redhat.com, oleg@redhat.com, dave@stgolabs.net, brauner@kernel.org, dhowells@redhat.com, hdanton@sina.com, hughd@google.com, lokeshgidra@google.com, minchan@google.com, jannh@google.com, shakeel.butt@linux.dev, souravpanda@google.com, pasha.tatashin@soleen.com, klarasmodin@gmail.com, richard.weiyang@gmail.com, corbet@lwn.net, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com References: <20250206025201.979573-1-surenb@google.com> Content-Language: en-US From: Vlastimil Babka Autocrypt: addr=vbabka@suse.cz; keydata= xsFNBFZdmxYBEADsw/SiUSjB0dM+vSh95UkgcHjzEVBlby/Fg+g42O7LAEkCYXi/vvq31JTB KxRWDHX0R2tgpFDXHnzZcQywawu8eSq0LxzxFNYMvtB7sV1pxYwej2qx9B75qW2plBs+7+YB 87tMFA+u+L4Z5xAzIimfLD5EKC56kJ1CsXlM8S/LHcmdD9Ctkn3trYDNnat0eoAcfPIP2OZ+ 9oe9IF/R28zmh0ifLXyJQQz5ofdj4bPf8ecEW0rhcqHfTD8k4yK0xxt3xW+6Exqp9n9bydiy tcSAw/TahjW6yrA+6JhSBv1v2tIm+itQc073zjSX8OFL51qQVzRFr7H2UQG33lw2QrvHRXqD Ot7ViKam7v0Ho9wEWiQOOZlHItOOXFphWb2yq3nzrKe45oWoSgkxKb97MVsQ+q2SYjJRBBH4 8qKhphADYxkIP6yut/eaj9ImvRUZZRi0DTc8xfnvHGTjKbJzC2xpFcY0DQbZzuwsIZ8OPJCc LM4S7mT25NE5kUTG/TKQCk922vRdGVMoLA7dIQrgXnRXtyT61sg8PG4wcfOnuWf8577aXP1x 6mzw3/jh3F+oSBHb/GcLC7mvWreJifUL2gEdssGfXhGWBo6zLS3qhgtwjay0Jl+kza1lo+Cv BB2T79D4WGdDuVa4eOrQ02TxqGN7G0Biz5ZLRSFzQSQwLn8fbwARAQABzSBWbGFzdGltaWwg QmFia2EgPHZiYWJrYUBzdXNlLmN6PsLBlAQTAQoAPgIbAwULCQgHAwUVCgkICwUWAgMBAAIe AQIXgBYhBKlA1DSZLC6OmRA9UCJPp+fMgqZkBQJkBREIBQkRadznAAoJECJPp+fMgqZkNxIQ ALZRqwdUGzqL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fnzIKmZAUCZAUSmwUJDK5EZgAKCRAiT6fnzIKmZOJGEACOKABgo9wJXsbWhGWYO7mD 8R8mUyJHqbvaz+yTLnvRwfe/VwafFfDMx5GYVYzMY9TWpA8psFTKTUIIQmx2scYsRBUwm5VI EurRWKqENcDRjyo+ol59j0FViYysjQQeobXBDDE31t5SBg++veI6tXfpco/UiKEsDswL1WAr tEAZaruo7254TyH+gydURl2wJuzo/aZ7Y7PpqaODbYv727Dvm5eX64HCyyAH0s6sOCyGF5/p eIhrOn24oBf67KtdAN3H9JoFNUVTYJc1VJU3R1JtVdgwEdr+NEciEfYl0O19VpLE/PZxP4wX PWnhf5WjdoNI1Xec+RcJ5p/pSel0jnvBX8L2cmniYnmI883NhtGZsEWj++wyKiS4NranDFlA HdDM3b4lUth1pTtABKQ1YuTvehj7EfoWD3bv9kuGZGPrAeFNiHPdOT7DaXKeHpW9homgtBxj 8aX/UkSvEGJKUEbFL9cVa5tzyialGkSiZJNkWgeHe+jEcfRT6pJZOJidSCdzvJpbdJmm+eED w9XOLH1IIWh7RURU7G1iOfEfmImFeC3cbbS73LQEFGe1urxvIH5K/7vX+FkNcr9ujwWuPE9b 1C2o4i/yZPLXIVy387EjA6GZMqvQUFuSTs/GeBcv0NjIQi8867H3uLjz+mQy63fAitsDwLmR EP+ylKVEKb0Q2A== In-Reply-To: <20250206025201.979573-1-surenb@google.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Action: no action X-Stat-Signature: 8bbbwsu98o1apasxnxp4j1e391aitx6o X-Rspam-User: X-Rspamd-Queue-Id: E34DB4004E X-Rspamd-Server: rspam03 X-HE-Tag: 1738838519-459045 X-HE-Meta: U2FsdGVkX1/IfdHvPeALjv5gwDvbRfRQW4e5EyLoQFcbBnqF8HvpiWfFfEWBZK1LUM7Yx56mvC+06JOdJ4YKDOVvGe53tEo9v6TPGGdWHq70yh1XrhXpWFKT/dmaON0bpkDZtvIOWIf8js+wQ1nxB3wUjuz6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eFQUMd5DE5W4WxeEEM3lhUsakJczYLpA1VOhYKyopk1cHiJ4gbGNH6PnWDTBlP3CdO+fWB8KuUBNqSmTbk76TiyIQFiY5DxREBs4F5zdBeVcEvPjf6nnDTlt5nPoOSZ2UfksbibNxJDFpE5kMkytJXELdvYKU/Y68bObD93nVHdXFO53suyDDzUwmcfjH1m1IxG+jksBXmOEDH6N2B3OANGzdFDLn6obPHnuvg== 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: List-Subscribe: List-Unsubscribe: On 2/6/25 03:52, Suren Baghdasaryan wrote: > For speculative lookups where a successful inc_not_zero() pins the > object, but where we still need to double check if the object acquired > is indeed the one we set out to acquire (identity check), needs this > validation to happen *after* the increment. > Similarly, when a new object is initialized and its memory might have > been previously occupied by another object, all stores to initialize the > object should happen *before* refcount initialization. > > Notably SLAB_TYPESAFE_BY_RCU is one such an example when this ordering > is required for reference counting. > > Add refcount_{add|inc}_not_zero_acquire() to guarantee the proper ordering > between acquiring a reference count on an object and performing the > identity check for that object. > Add refcount_set_release() to guarantee proper ordering between stores > initializing object attributes and the store initializing the refcount. > refcount_set_release() should be done after all other object attributes > are initialized. Once refcount_set_release() is called, the object should > be considered visible to other tasks even if it was not yet added into an > object collection normally used to discover it. This is because other > tasks might have discovered the object previously occupying the same > memory and after memory reuse they can succeed in taking refcount for the > new object and start using it. > > Object reuse example to consider: > > consumer: > obj = lookup(collection, key); > if (!refcount_inc_not_zero_acquire(&obj->ref)) > return; > if (READ_ONCE(obj->key) != key) { /* identity check */ > put_ref(obj); > return; > } > use(obj->value); > > producer: > remove(collection, obj->key); > if (!refcount_dec_and_test(&obj->ref)) > return; > obj->key = KEY_INVALID; > free(obj); > obj = malloc(); /* obj is reused */ > obj->key = new_key; > obj->value = new_value; > refcount_set_release(obj->ref, 1); > add(collection, new_key, obj); > > refcount_{add|inc}_not_zero_acquire() is required to prevent the following > reordering when refcount_inc_not_zero() is used instead: > > consumer: > obj = lookup(collection, key); > if (READ_ONCE(obj->key) != key) { /* reordered identity check */ > put_ref(obj); > return; > } > producer: > remove(collection, obj->key); > if (!refcount_dec_and_test(&obj->ref)) > return; > obj->key = KEY_INVALID; > free(obj); > obj = malloc(); /* obj is reused */ > obj->key = new_key; > obj->value = new_value; > refcount_set_release(obj->ref, 1); > add(collection, new_key, obj); > > if (!refcount_inc_not_zero(&obj->ref)) > return; > use(obj->value); /* USING WRONG OBJECT */ > > refcount_set_release() is required to prevent the following reordering > when refcount_set() is used instead: > > consumer: > obj = lookup(collection, key); > > producer: > remove(collection, obj->key); > if (!refcount_dec_and_test(&obj->ref)) > return; > obj->key = KEY_INVALID; > free(obj); > obj = malloc(); /* obj is reused */ > obj->key = new_key; /* new_key == old_key */ > refcount_set(obj->ref, 1); > > if (!refcount_inc_not_zero_acquire(&obj->ref)) > return; > if (READ_ONCE(obj->key) != key) { /* pass since new_key == old_key */ > put_ref(obj); > return; > } > use(obj->value); /* USING STALE obj->value */ > > obj->value = new_value; /* reordered store */ > add(collection, key, obj); > > Signed-off-by: Suren Baghdasaryan > Cc: Peter Zijlstra > Cc: Will Deacon > Cc: Paul E. McKenney Acked-by: Vlastimil Babka #slab