From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) (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 54D5D18C31 for ; Wed, 29 Jan 2025 15:22:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738164155; cv=none; b=MxT1u/zgs3BRuz2M24rlCCNSPKYS8BfDyVNax2KPBmxTgWjygnrexAPREyFwmBtxQIsh+EfQT/4zVby96WafUHlSMycyOxZaK+LMc8yqEojgtRJr79Kx0MZQiNbgXlvBnJr8OlZ+Wb2ihV4u5BRcOMHzTmiWkY8G2/58bHrYWss= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738164155; c=relaxed/simple; bh=shvje0UITzir1dpP8zPb6hRdIHFxfjfhmmnoX+o2PJ0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Bmj4PUQRF6qf4j14RXs3C8EQ7RIpKvpY4G20bkeypNeqN+aEKRgCAEy86o2vbiBWUR2xR1vZ60WUC8K5KMZwa8MzKiRtli1o4ZcEediRyE98NSOOULtFgwBnSh4bPInPnViWDd1SXwv150ryrzqj11NJeymtCe5BeyvjwGL2jv4= 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=CNYIJ3Ww; arc=none smtp.client-ip=209.85.208.48 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="CNYIJ3Ww" Received: by mail-ed1-f48.google.com with SMTP id 4fb4d7f45d1cf-5d0ac27b412so9276200a12.1 for ; Wed, 29 Jan 2025 07:22:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738164151; x=1738768951; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=s6XkNkMwf0es0PBIA5Ev2pBPlcQq5dWE/A5ck9n73CQ=; b=CNYIJ3WwRx/FNGCdVyswhcFELUYEAR8iNLpkiby+0fw8/L6/Ka6Hgp0r7Abn8ZKo0I E6+4XumFwsdcx+TxbR0hEuNVlUcnPAnHQ2iFac6rWWsmiMbWyz/S44BRoiD8gna1InBh eYHkb1vNuXfkWIDP2yARnY1kEQ/TP2kqjujTJ+XZY/9jnOpHK8TjgrvulWNj+mZ8mps7 2qIme2h2i8kc1WU07ugGvycvRWPxolvmKmO+68vziuRoNRbQdLTq3ATok7/rNB33S4Y7 21L9UhzGWN2bCUBGx6ig5WjTSteieMz1dJJPSvJcMCMW0ICnZi2rjvemtqGe/2ptK2JV Vd3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738164151; x=1738768951; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=s6XkNkMwf0es0PBIA5Ev2pBPlcQq5dWE/A5ck9n73CQ=; b=nzW7ghb8XY/POJom5Nulp2tBoG6LbpAFzKs1aVa+KWs1In5mho+1q5Wh/MuyGFkMYh E+dgQara49ZdGLs7DPgl3B7uWOVYeMRJXTyB/Sgnt88hOcOgbrLjw7z95HGwNooV4DMb njMYNx0M9STNR9HC7shk5FmvVv1rMYvryIU65CniZdM6qn87R+MsdVu6nkj3FQsNyoqC CrLoYEhEeS7rYCuCcr9S8dQdQ7ZQ/Np72rpiVA0fmsM68H+P9RkyGKK70ilq3VzQh6Wh btcBlkSnf3i3BV1pGqAhGDdppaBWxAwNG6EJPP6dmWKnW08xqB7nwj0P4R0NVzP8Kh4E LQsg== X-Forwarded-Encrypted: i=1; AJvYcCVdSt0D6Yah6rCPMyjo4lIv93Qk2woKomOMBUC0Tx7LiD4v5WYdCx5rnFwfN+on+7BHxfX/j/80uH9U10s=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6zFPVl3+n/vu3jzrLIluphmYhzD6u7iVLF2UWne6JjavdG5Zn Va3kjT+xOz49mdczSeEDH+oURiCp4mHXRlatzVHUDTlprQAcz30e X-Gm-Gg: ASbGncuWw805u8tgYYjur23HW5sIelRNhSRNlSktcOqsJUIEXbJQFB2Ptd3AJ3Y38x9 sVmntDacmlL8keTkzh7mBPO30XiX31hvjYFhltgy6oiE1bLPbcN4X3xDwr6w3nwSnQAJRQQVw5m O+W5FrsCDfj6fiHTGYpnXGSVz1jy1V4sF1SAlxPjzBw5ZlqhUjX83N267SiQGXftjcPqZatU32P Of4FJSYqNeJ1d5W7FbsoHbcSo0xuOG7Gmm25PXs0QudDUB1xt+CUbOPORFR2luaUzy3wkxhsWpS tyk49MjWqgrQpNx8LjVpyJicB5s= X-Google-Smtp-Source: AGHT+IEPvsB5wiIxECbqiHsWlevGvuSZ+hxGT8O14vPvVJYyamCB8v5H9omglGOp8XAN26xtzC9nJQ== X-Received: by 2002:a05:6402:3217:b0:5db:da94:6e7e with SMTP id 4fb4d7f45d1cf-5dc5efebfa2mr3683306a12.24.1738164151169; Wed, 29 Jan 2025 07:22:31 -0800 (PST) Received: from [192.168.1.100] ([46.248.82.114]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5dc186183cdsm8967189a12.15.2025.01.29.07.22.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jan 2025 07:22:30 -0800 (PST) Message-ID: <3f787832-fbb7-8590-c090-1f511b16449a@gmail.com> Date: Wed, 29 Jan 2025 16:22:29 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCHv1 3/6] zsmalloc: make zspage lock preemptible To: Sergey Senozhatsky , Andrew Morton , Minchan Kim , Johannes Weiner , Yosry Ahmed , Nhat Pham Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20250129064853.2210753-1-senozhatsky@chromium.org> <20250129064853.2210753-4-senozhatsky@chromium.org> Content-Language: en-US From: Uros Bizjak In-Reply-To: <20250129064853.2210753-4-senozhatsky@chromium.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 29. 01. 25 07:43, Sergey Senozhatsky wrote: > +/* > + * zspage lock permits preemption on the reader-side (there can be multiple > + * readers). Writers (exclusive zspage ownership), on the other hand, are > + * always run in atomic context and cannot spin waiting for a (potentially > + * preempted) reader to unlock zspage. This, basically, means that writers > + * can only call write-try-lock and must bail out if it didn't succeed. > + * > + * At the same time, writers cannot reschedule under zspage write-lock, > + * so readers can spin waiting for the writer to unlock zspage. > + */ > +static void zspage_read_lock(struct zspage *zspage) > +{ > + atomic_t *lock = &zspage->lock; > + int old; > + > + while (1) { > + old = atomic_read(lock); > + if (old == ZS_PAGE_WRLOCKED) { > + cpu_relax(); > + continue; > + } > + > + if (atomic_try_cmpxchg(lock, &old, old + 1)) > + return; > + > + cpu_relax(); > + } > +} Please note that atomic_try_cmpxchg updates old variable on failure, so the whole loop can be rewritten as: { atomic_t *lock = &zspage->lock; int old = atomic_read(lock); while (1) { if (old == ZS_PAGE_WRLOCKED) { cpu_relax(); old = atomic_read(lock); continue; } if (atomic_try_cmpxchg(lock, &old, old + 1)) return; cpu_relax(); } } Please note that cpu_relax() in the cmpxchg() loop is actually harmful [1] because: --q-- On the x86-64 architecture even a failing cmpxchg grants exclusive access to the cacheline, making it preferable to retry the failed op immediately instead of stalling with the pause instruction. --/q-- [1] https://lore.kernel.org/all/20230113184447.1707316-1-mjguzik@gmail.com/ Based on the above, cpu_relax() should be removed from the loop, which becomes: { atomic_t *lock = &zspage->lock; int old = atomic_read(lock); do { if (old == ZS_PAGE_WRLOCKED) { cpu_relax(); old = atomic_read(lock); continue; } } while (!atomic_try_cmpxchg(lock, &old, old + 1)); } > +static int zspage_try_write_lock(struct zspage *zspage) This function can be declared as bool, returning true/false. Uros.