From patchwork Tue Nov 12 19:25:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 13872877 Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD9B82185A9; Tue, 12 Nov 2024 19:25:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731439549; cv=none; b=jnmLevgznLZpXDAIdiG0uBE9JwIVbhZWDXoimo743V71CDOWL+dQYHScDT1Uyec/588/ziKnJ5HkPNJ89BRckwi9xqNTgGcoaMJRQR4Yk003q0DJWqbH4sXJT9mLcI6rrqn6klvMjsGr3+xc+BT7OfulYmF7H5GEzx91Q5Ta+QU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731439549; c=relaxed/simple; bh=uC3ScEW1U+O2Wc2A9bSGiVmy+OUmm1kGk/1ZnabR03U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Dcc6DTfHkyPUgs17+ojxHz/O2DcIeSt62NdhsG73hhsy6AGHolGfx5badWwCFEKfvPQwO/61H6ldHvZXDdwLVTHGLvYIw1ahJy9Z6pBu0LWTurEe/bb6m7AQsMeWj/mbYDDvyqQ3A4z7tgMExElcuzFBiTNH0s4H5MH6AZzRB7k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IcZjzfz7; arc=none smtp.client-ip=209.85.161.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IcZjzfz7" Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-5ebc1af8f10so2588598eaf.2; Tue, 12 Nov 2024 11:25:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731439547; x=1732044347; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=HpVYJlDWFg+wqwWY/7YvJ4k8chIUuFrMv+OyIyUOhHI=; b=IcZjzfz7QpQyEmERajh7sHdCf+ypO/RuP7kqF80gXqzFY0snThBryubLnvKQ19N1nD UKgIUQr5IcQ42D8/z7Uj41U4zpVa+vGsC0lb/bFxXXEmafnZOx/peJjGot1K0KF2u7VI yRAaPFMgLve4tRjctmGYa7eF+B/PA1YPi58AeRkWeCU/MzG3h00hVmo0AeneQaZgUnOs HYBNaz4xFS1mFnJSZ57eUCl+/GQYHtZ79fmcv8Q9G5AHYintBqTr7VKkF0lTcOxd8c8j GS1Q7+BzTmTf9ovyS0FyOxxeNoKyw+Cg0qqtKLP0k/P088Op0ss+0LRSPVIiDbr5YmTT /FpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731439547; x=1732044347; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HpVYJlDWFg+wqwWY/7YvJ4k8chIUuFrMv+OyIyUOhHI=; b=tR689Zo2l2t20ambwqLt2NyTCvlETnEOcT1tO+xG/o8uXbLmBWj8K2hf65Fu500yfq KEc/noTbjrCFQ1lcjh2Td8kjEwfnqhz4BKP6Oci/N5ZeVxkKp4czF/LEHI3xkLMMAErz Lmo8CZv+Aa/vYdIa6Wt6Zaw8rVUrSYGOaNUrSTnE8DxFtkyE4xZkaHZt4R4DE5pus8+9 Wx68p9H9qi6sXOHhnCzL7OJ3QzTgkqcY96Aj9+Wk8dVxV67hyzrqq6JwYByPtCO5Ns34 +4zxcGQV6X1o+rOUj0BO21Ld1Qk1CBj1JNYHH0+vUtK4AzYRaYXXlUzLuqGxEBjxI4vM 5xNw== X-Forwarded-Encrypted: i=1; AJvYcCVtDSmVEp4zA+qysZvVJpZZMkbxPzFbu0pcOQIeD6EjU2GagL90ofMfHocZmOYjAYxoNNh39Ocs0nRNMEA=@vger.kernel.org X-Gm-Message-State: AOJu0YxSg6VgwxrelRuUJyRWTYpDEHAdd2IxvzCAyzODgqeNvh3mjoFq DCpfeCum4vO+7eqKIMu4ajTnpauKD9agM/jDg+2uJCsoUD8WeMlA X-Google-Smtp-Source: AGHT+IFFRV2Y+4sab1Il1y9pJ2Hlo0TmUnKS43V28HLFId8FwN6LDig0N+eoxL2bSpQakMV/GzGL0A== X-Received: by 2002:a05:6218:280a:b0:1c3:89d4:e888 with SMTP id e5c5f4694b2df-1c641f40665mr871574455d.20.1731439546671; Tue, 12 Nov 2024 11:25:46 -0800 (PST) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:2ba5]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-462ff5e14f9sm78395581cf.86.2024.11.12.11.25.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 11:25:46 -0800 (PST) From: Tamir Duberstein Date: Tue, 12 Nov 2024 14:25:36 -0500 Subject: [PATCH 1/2] xarray: extract xa_zero_to_null Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241112-xarray-insert-cmpxchg-v1-1-dc2bdd8c4136@gmail.com> References: <20241112-xarray-insert-cmpxchg-v1-0-dc2bdd8c4136@gmail.com> In-Reply-To: <20241112-xarray-insert-cmpxchg-v1-0-dc2bdd8c4136@gmail.com> To: Andrew Morton , Matthew Wilcox Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl , Andreas Hindborg , Tamir Duberstein X-Mailer: b4 0.15-dev Reduce code duplication by extracting a static inline function that returns its argument if it is non-zero and NULL otherwise. This changes xas_result to check for errors before checking for zero but this cannot change the behavior of existing callers: - __xa_erase: passes the result of xas_store(_, NULL) which cannot fail. - __xa_store: passes the result of xas_store(_, entry) which may fail. xas_store calls xas_create when entry is not NULL which returns NULL on error, which is immediately checked. This should not change observable behavior. - __xa_cmpxchg: passes the result of xas_load(_) which might be zero. This would previously return NULL regardless of the outcome of xas_store but xas_store cannot fail if xas_load returns zero because there is no need to allocate memory. - xa_store_range: same as __xa_erase. Signed-off-by: Tamir Duberstein --- lib/xarray.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/xarray.c b/lib/xarray.c index 32d4bac8c94ca13e11f350c6bcfcacc2040d0359..1b8305bffbff07adb80334fc83b5dc8e40ba2f50 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -435,6 +435,11 @@ static unsigned long max_index(void *entry) return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1; } +static inline void *xa_zero_to_null(void *entry) +{ + return xa_is_zero(entry) ? NULL : entry; +} + static void xas_shrink(struct xa_state *xas) { struct xarray *xa = xas->xa; @@ -451,8 +456,8 @@ static void xas_shrink(struct xa_state *xas) break; if (!xa_is_node(entry) && node->shift) break; - if (xa_is_zero(entry) && xa_zero_busy(xa)) - entry = NULL; + if (xa_zero_busy(xa)) + entry = xa_zero_to_null(entry); xas->xa_node = XAS_BOUNDS; RCU_INIT_POINTER(xa->xa_head, entry); @@ -1474,9 +1479,7 @@ void *xa_load(struct xarray *xa, unsigned long index) rcu_read_lock(); do { - entry = xas_load(&xas); - if (xa_is_zero(entry)) - entry = NULL; + entry = xa_zero_to_null(xas_load(&xas)); } while (xas_retry(&xas, entry)); rcu_read_unlock(); @@ -1486,11 +1489,9 @@ EXPORT_SYMBOL(xa_load); static void *xas_result(struct xa_state *xas, void *curr) { - if (xa_is_zero(curr)) - return NULL; if (xas_error(xas)) curr = xas->xa_node; - return curr; + return xa_zero_to_null(curr); } /** From patchwork Tue Nov 12 19:25:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 13872878 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB5BD21949E; Tue, 12 Nov 2024 19:25:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731439550; cv=none; b=PFAZN80VIeokFyyDy9jdiZoAknro9rSg7QEh8azcQ4+b/amB28EkiHpamXqClnz6ghCARnPJfq3yjkSUGnvde7BsRXEVGqXTMmtjNrznMGM3JF6kTP2Q3NJMgA71TIDSgR9k2MUQDUxlXjzxV1VoA5ese5hKVqxw+fEhVz/ri+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731439550; c=relaxed/simple; bh=AjDxtUOOjIpTEE1nyWRWUP+h0r4wzvf5tBIrwgKyeUM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hT1i/bZiO1o3+fHhAy/yUKj+ye5xLTVhraKvxAnSZ3cvft0S11aFe8c1zby1VzHJbDSshCfgoEB/z3zh7fj3Vr6I+aUEUhB0SgTQGgbzOShv4FSh42GXM/fhJ8RM9+NlfjSFWQsXVV3hU3it4YUBM0Q6J4XR0vmdjZr6ozGPXU0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=niWPaqEi; arc=none smtp.client-ip=209.85.160.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="niWPaqEi" Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-460ace055d8so44138061cf.1; Tue, 12 Nov 2024 11:25:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731439548; x=1732044348; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Hlkpyjr9+m8ziY3XcgbtHwZj36eM6mV2l4GU+t4LNeI=; b=niWPaqEil2xjOi8gxdD9wc2w2F0QPcRzQ3q3MZELpyhIWZeIqrsVFSBI2iiyJr2pq1 3MYsPK72/dLpm6/1pvYn/gfpqHSUwl9plqeeFKiKJlUlzkIlbTSg+U3Ofmw/D8UIhvQp kCAyBPd/0npLKu6Ah7BnXKF34vo9nFZ6kMWEDsI3x4H3vhXPKrW0HETNY9EGW+ga+z1w ZZa368T9K1tgHJ1N9oYn70Cj3DWDSsFl1+kE1VkVZAWK8Scaku5TR0ail+4A/BxmSv+I v6PcGYG4AFSCZDZb6e1hdnQ76GAAvJNPw2Gr81ajOUJbRoaHsg7cGxCoafYvLeJ/rD5Y cAgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731439548; x=1732044348; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Hlkpyjr9+m8ziY3XcgbtHwZj36eM6mV2l4GU+t4LNeI=; b=hGIn+yK7+5DGVkxWnJP4Oi++vfcmVjg50b1ndK+UVJJwzJ8posF28Jtwju8gKYbfZz SkFuoIIDKLD+7LFTgE1rNgMiU8T9cB1B02lj1Zu4sgvzmP0Sor4kXTJTAQZOZbJfjYv6 nnL5KNd0a/i1lL2AfgoEH8fB1I0/hoR556/nqeA/GIK+wttZrbg+N55yOZ5EtegBX3Fg zW8H2TIfuICxFyV/jJQWvuzqaEBDbMPuqxjxowsomIlPKEdIWxNTIfCvv6XTepXl/keP qJDkfguuJKTpQ/8rKceV+7bZjjfiCQplrFvBH5C86L12RPCsfLv1ZIRuYSvVMcg7kGxy H8kg== X-Forwarded-Encrypted: i=1; AJvYcCUTpbdkSuvWXCBXiyew+eGe+RBLcIZi4tG1IdFrdDrtRDvMoJqHKj30+dTkYD7RhwO8Yoe0KTM0BqiyjiE=@vger.kernel.org X-Gm-Message-State: AOJu0Yz1PsdOJQVj65UrIi6GupO8sviPa+dH8FRQJqzvb9Mltyrpok/L ik5aNCefFmE7xBS5B7OPwLY3y+9Bp9wtabXOxeP9LdVdd1n0o9gW X-Google-Smtp-Source: AGHT+IFPTrZJJODGh0jMxoOVDYXovFI4pMfCUXnEjxCK0Y2/4OhWbOTYwcnXfi+scuf9nv+VV+adFQ== X-Received: by 2002:ac8:5a47:0:b0:462:a686:a42e with SMTP id d75a77b69052e-4634b4aea60mr3792921cf.3.1731439547763; Tue, 12 Nov 2024 11:25:47 -0800 (PST) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:2ba5]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-462ff5e14f9sm78395581cf.86.2024.11.12.11.25.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 11:25:47 -0800 (PST) From: Tamir Duberstein Date: Tue, 12 Nov 2024 14:25:37 -0500 Subject: [PATCH 2/2] xarray: extract helper from __xa_{insert,cmpxchg} Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241112-xarray-insert-cmpxchg-v1-2-dc2bdd8c4136@gmail.com> References: <20241112-xarray-insert-cmpxchg-v1-0-dc2bdd8c4136@gmail.com> In-Reply-To: <20241112-xarray-insert-cmpxchg-v1-0-dc2bdd8c4136@gmail.com> To: Andrew Morton , Matthew Wilcox Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl , Andreas Hindborg , Tamir Duberstein X-Mailer: b4 0.15-dev Reduce code duplication by extracting a static inline function. This function is identical to __xa_cmpxchg with the exception that it does not coerce zero entries to null on the return path. Signed-off-by: Tamir Duberstein --- lib/xarray.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/xarray.c b/lib/xarray.c index 1b8305bffbff07adb80334fc83b5dc8e40ba2f50..2af86bede3c119060650ee8b891751531c6732e7 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -1491,7 +1491,7 @@ static void *xas_result(struct xa_state *xas, void *curr) { if (xas_error(xas)) curr = xas->xa_node; - return xa_zero_to_null(curr); + return curr; } /** @@ -1568,7 +1568,7 @@ void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) xas_clear_mark(&xas, XA_FREE_MARK); } while (__xas_nomem(&xas, gfp)); - return xas_result(&xas, curr); + return xas_result(&xas, xa_zero_to_null(curr)); } EXPORT_SYMBOL(__xa_store); @@ -1601,6 +1601,9 @@ void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) } EXPORT_SYMBOL(xa_store); +static inline void *__xa_cmpxchg_raw(struct xarray *xa, unsigned long index, + void *old, void *entry, gfp_t gfp); + /** * __xa_cmpxchg() - Store this entry in the XArray. * @xa: XArray. @@ -1619,6 +1622,13 @@ EXPORT_SYMBOL(xa_store); */ void *__xa_cmpxchg(struct xarray *xa, unsigned long index, void *old, void *entry, gfp_t gfp) +{ + return xa_zero_to_null(__xa_cmpxchg_raw(xa, index, old, entry, gfp)); +} +EXPORT_SYMBOL(__xa_cmpxchg); + +static inline void *__xa_cmpxchg_raw(struct xarray *xa, unsigned long index, + void *old, void *entry, gfp_t gfp) { XA_STATE(xas, xa, index); void *curr; @@ -1637,7 +1647,6 @@ void *__xa_cmpxchg(struct xarray *xa, unsigned long index, return xas_result(&xas, curr); } -EXPORT_SYMBOL(__xa_cmpxchg); /** * __xa_insert() - Store this entry in the XArray if no entry is present. @@ -1657,26 +1666,16 @@ EXPORT_SYMBOL(__xa_cmpxchg); */ int __xa_insert(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) { - XA_STATE(xas, xa, index); void *curr; + int errno; - if (WARN_ON_ONCE(xa_is_advanced(entry))) - return -EINVAL; if (!entry) entry = XA_ZERO_ENTRY; - - do { - curr = xas_load(&xas); - if (!curr) { - xas_store(&xas, entry); - if (xa_track_free(xa)) - xas_clear_mark(&xas, XA_FREE_MARK); - } else { - xas_set_err(&xas, -EBUSY); - } - } while (__xas_nomem(&xas, gfp)); - - return xas_error(&xas); + curr = __xa_cmpxchg_raw(xa, index, NULL, entry, gfp); + errno = xa_err(curr); + if (errno) + return errno; + return (curr != NULL) ? -EBUSY : 0; } EXPORT_SYMBOL(__xa_insert);