Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4411104pxf; Tue, 30 Mar 2021 07:13:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxpHqh0bqViMIpCAu0EJiGMmBWjFrFIkms3vTOeHRNWUtQpTASHavzN1hAMadEE0KYmwNKd X-Received: by 2002:a05:6402:3550:: with SMTP id f16mr33548587edd.134.1617113620118; Tue, 30 Mar 2021 07:13:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617113620; cv=none; d=google.com; s=arc-20160816; b=xMC6BFsyMT3CkE7t1XG9lVj+kPdkYwKHplCR9EsZEg5vH9u7OZep5oboYK+C3pZtXu 6gm+hHJUCZ+h+ZwkzRM7mftZf5/ZKNc4/jAeFbDER/kRvibkjYPxopARmQsEX70pV6Wp yqMpjSYRp1rei+4wftTcuNwctQ6SL8+6eHbHHfvvKHZSNsr5liG0icHU59wQ+msVcuhm yjUFPaA123MnLgjTIMWowqcUV6RxBMRwozHIbExIKO24PRn1sB8jskVFtzUNq0qDNbQZ 8ITVMa+YObPsSemZcnw5ylkvWqPTMUj7YU7uRJmo2kOEtA27eyLJ3y1aoliU2+ZPiaUc XbaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:organization :from:references:cc:to:subject:dkim-signature; bh=JjceWuLqxTVqN7nJGF6xvHg+l6EBFWCS4ZdW7zopx+w=; b=bBpg2fE+hYwO7LhgDrT+cQ66/H5dJk21OlfhupoFuw52Uu9Aj/M5MpJ2HuXRu6ZKWE CRPh+eBJwzBqGP3fzRl59T0A8G08W1RtxckfmqquanBrsZdG0/oMbvECKV4W5SOErkRJ BfcyqTO2Q3/S2sc3fQ05YnUbt0nXkF1Ta3oqY0a8HhwvdEoXcT235rh4IhbYpCh7bCoD o68oxxnf3013sTdyd+4QeJPK7yh6GCP16JjDLZJpLd+IMD48gFEB6TTKiBkfsHI2cJx5 4wIPR/B2A8Ir7oKrzjkJE/GYmTlZ+DOLRht9R0UdaojABuPsIKNJ+EKxZh1OY9WjhIgV nL0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=glSSC0ck; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id pj10si15113411ejb.348.2021.03.30.07.13.16; Tue, 30 Mar 2021 07:13:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=glSSC0ck; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231764AbhC3OKD (ORCPT + 99 others); Tue, 30 Mar 2021 10:10:03 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:41694 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231859AbhC3OJh (ORCPT ); Tue, 30 Mar 2021 10:09:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617113376; h=from:from: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; bh=JjceWuLqxTVqN7nJGF6xvHg+l6EBFWCS4ZdW7zopx+w=; b=glSSC0ck/f6kt31DqYIZhXjStyXssUUvLNU0MXszy123P+pedqMAPuidFjK5L3aScrjA+d g60QYPkmEMusBdYbloMZXHSYmSLdBERLwth1ZaXAvr6XHm7jVwC9Palz03wgjlMZyZNUhF TFG/7wkcCDd0560HOdtYve5xOkR0rV8= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-168-KiW6mmzYOV6OSjZf91Aqfg-1; Tue, 30 Mar 2021 10:09:32 -0400 X-MC-Unique: KiW6mmzYOV6OSjZf91Aqfg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 903ECDF8A0; Tue, 30 Mar 2021 14:09:30 +0000 (UTC) Received: from llong.remote.csb (ovpn-118-202.rdu2.redhat.com [10.10.118.202]) by smtp.corp.redhat.com (Postfix) with ESMTP id CE2A35D9CC; Tue, 30 Mar 2021 14:09:27 +0000 (UTC) Subject: Re: [PATCH v4 3/4] locking/qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32 To: Guo Ren , Peter Zijlstra Cc: linux-riscv , Linux Kernel Mailing List , linux-csky@vger.kernel.org, linux-arch , Guo Ren , Will Deacon , Ingo Molnar , Arnd Bergmann , Anup Patel References: <1616868399-82848-1-git-send-email-guoren@kernel.org> <1616868399-82848-4-git-send-email-guoren@kernel.org> From: Waiman Long Organization: Red Hat Message-ID: <4d0dbaa0-1f96-470c-0ed0-04f6827ea384@redhat.com> Date: Tue, 30 Mar 2021 10:09:27 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/29/21 11:13 PM, Guo Ren wrote: > On Mon, Mar 29, 2021 at 8:50 PM Peter Zijlstra wrote: >> On Mon, Mar 29, 2021 at 08:01:41PM +0800, Guo Ren wrote: >>> u32 a = 0x55aa66bb; >>> u16 *ptr = &a; >>> >>> CPU0 CPU1 >>> ========= ========= >>> xchg16(ptr, new) while(1) >>> WRITE_ONCE(*(ptr + 1), x); >>> >>> When we use lr.w/sc.w implement xchg16, it'll cause CPU0 deadlock. >> Then I think your LL/SC is broken. >> >> That also means you really don't want to build super complex locking >> primitives on top, because that live-lock will percolate through. > Do you mean the below implementation has live-lock risk? > +static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail) > +{ > + u32 old, new, val = atomic_read(&lock->val); > + > + for (;;) { > + new = (val & _Q_LOCKED_PENDING_MASK) | tail; > + old = atomic_cmpxchg(&lock->val, val, new); > + if (old == val) > + break; > + > + val = old; > + } > + return old; > +} If there is a continuous stream of incoming spinlock takers, it is possible that some cpus may have to wait a long time to set the tail right. However, this should only happen on artificial workload. I doubt it will happen with real workload or with limit number of cpus. > >> Step 1 would be to get your architecute fixed such that it can provide >> fwd progress guarantees for LL/SC. Otherwise there's absolutely no point >> in building complex systems with it. > Quote Waiman's comment [1] on xchg16 optimization: > > "This optimization is needed to make the qspinlock achieve performance > parity with ticket spinlock at light load." > > [1] https://lore.kernel.org/kvm/1429901803-29771-6-git-send-email-Waiman.Long@hp.com/ > > So for a non-xhg16 machine: > - ticket-lock for small numbers of CPUs > - qspinlock for large numbers of CPUs > > Okay, I'll put all of them into the next patch :P > It is true that qspinlock may not offer much advantage when the number of cpus is small. It shines for systems with many cpus. You may use NR_CPUS to determine if the default should be ticket or qspinlock with user override. To determine the right NR_CPUS threshold, you may need to run on real SMP RISCV systems to find out. Cheers, Longman