Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp2028091pxb; Sat, 2 Oct 2021 04:52:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJya3mHz4A3hR3QsR2uuD+mGmptXnIiNWUmswKk9zxRfjMI8rtFX0uNGfJRjSGvFHzIu9An+ X-Received: by 2002:a50:bf07:: with SMTP id f7mr3566973edk.288.1633175549180; Sat, 02 Oct 2021 04:52:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633175549; cv=none; d=google.com; s=arc-20160816; b=TsNuPa2naRq7Wo+HcAj0OBhJGoUHEzkpO939+xc2ploUU7ZDOyDyByDeND1ATBmgVH r+NBzg3m6ru4sJG7KTHfRN7lmMWlNS6MWOiXT+0Qr5wthQeXdZBUDHToCmLM5o2dICup NzI91eobJtOOVTqk6Dz4+VtaFtlgYoJTYR0ollC0h2IoS1FvYuJIkZXMp66HFb2iH7ul UVcgkQ5sHjy+aej6N2PFVF5i+WgnQGQoZD3HIEwUuag+ES6Vi/8wq79ZCfLSaxZBHG/5 piANspdfLjIbjVBhjlYtG7iRpvmDQABsnUFlSwwwk96kww0oybvhyJ8phgIYf3/563r2 Y6MQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=bSQRibgDnpTqOXZp7vnqZJ1QVyQSI1d0M4GZTbpWPbU=; b=nXS2Vgzl8v8B8AxX4bAGsJ4wuXi5+2cbj+e8Ez7zN2OS6hHiyaVviYryYjigATZIsp 7Iik4eLbJziRK1Ww5czZs2+JT3h/ElmxfgST1RWcGtk0OJzEEz+sN/TeWB9D90KhD2T1 BuBMFs3bkCHJCNAtjyrMzRMaTXSUDP75d8cGuD8h5BpxSgrqtZgplCo2xTl9lKI1stSJ enPL2E/56nFckP2SwAkz893IFkxCqYbozeqOw7snwi9ELDYKd809CopV5ocRZHtQTBER C7YboD4pRntplpk43ljDUYn/kVU0wp0XvgFnlpds+9LKxKFk5JHkJ2hBdpfLoPZtK9Jg fj2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="W/E1MGjE"; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id eb13si17201096edb.334.2021.10.02.04.52.01; Sat, 02 Oct 2021 04:52:29 -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=@gmail.com header.s=20210112 header.b="W/E1MGjE"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232891AbhJBLt5 (ORCPT + 99 others); Sat, 2 Oct 2021 07:49:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232868AbhJBLt4 (ORCPT ); Sat, 2 Oct 2021 07:49:56 -0400 Received: from mail-vs1-xe34.google.com (mail-vs1-xe34.google.com [IPv6:2607:f8b0:4864:20::e34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC94CC061570; Sat, 2 Oct 2021 04:48:10 -0700 (PDT) Received: by mail-vs1-xe34.google.com with SMTP id 66so14272784vsd.11; Sat, 02 Oct 2021 04:48:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=bSQRibgDnpTqOXZp7vnqZJ1QVyQSI1d0M4GZTbpWPbU=; b=W/E1MGjEbSDDZHveDGcgEYVt58pIGf0/RE9j+KpAk4NDuaoWQ9IS7HgGovoyUX8910 GTdOu0RmK04aLK2JRZpzNgn39vidIk9Kr9Qby6tDKfVo34X52nKRgu3A0JRXhBxYqDis yNKvVJKvhe0o4YOzoZrGR5bDqc6S1kj3Uw5tldfK+0hzMW8eF9xA0Zm5oZmJM1iaxXA3 RULDAeUuP5BDYCTC4XfS9v3R2qw69RMlW9QvaEmKG1oZYB8P0LQu1pPTueghbeNhYU4l tVCw/DJFlGrhbkLJibBLm78UlpaaL/AEmGVkcsIVLjY0EHbfqE+2ViiyNFvLFPKPvCl9 LC4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=bSQRibgDnpTqOXZp7vnqZJ1QVyQSI1d0M4GZTbpWPbU=; b=zrYHuaXoPoVOG77PkzPSU5xIDbGB45lwN003uTa9k+W2mh27D75ndS+0Laqs6Gtba+ j8WHY3eRU6IFXoto2TCS8vqE7txklMboZGdOyJqQgYHw45UpyWwvR3+g8Z28ghPgDs7M kId6XXNYLkr8E7OxK4UanZ3lT0RqpYEtAkmdFDZxEqA0v7KuINR5lbm3C83RDKituwyw PsOQn4RaNuFrSZPkp/PKS+b7BWtICb7v/HrF7rkNs5LC0SdPoFsB+SpoKTBLRGgTMH7A xXxmEAiIOZvHxxBkmQmWSPWJFikf69fof7992T/mDvw6TamJoFCnOFu6/AoDi6Daip62 MctQ== X-Gm-Message-State: AOAM530I/0lSTmp42GgRuKfl5sKPaoueGYia0e7MalF6F5GvW7ysORaq QqLYXnN8VBaoE/z/PC89dipFLnMOPnGyk/2a5Wo= X-Received: by 2002:a67:cb96:: with SMTP id h22mr7891227vsl.3.1633175289819; Sat, 02 Oct 2021 04:48:09 -0700 (PDT) MIME-Version: 1.0 References: <20210927064300.624279-1-chenhuacai@loongson.cn> <20210927064300.624279-8-chenhuacai@loongson.cn> In-Reply-To: From: Huacai Chen Date: Sat, 2 Oct 2021 19:47:58 +0800 Message-ID: Subject: Re: [PATCH V4 07/22] LoongArch: Add atomic/locking headers To: Peter Zijlstra Cc: Huacai Chen , Arnd Bergmann , Andy Lutomirski , Thomas Gleixner , Andrew Morton , David Airlie , Jonathan Corbet , Linus Torvalds , linux-arch , "open list:DOCUMENTATION" , LKML , Xuefeng Li , Yanteng Si , Jiaxun Yang Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, Peter, On Fri, Oct 1, 2021 at 6:54 PM Peter Zijlstra wrote: > > On Mon, Sep 27, 2021 at 02:42:44PM +0800, Huacai Chen wrote: > > diff --git a/arch/loongarch/include/asm/bitops.h b/arch/loongarch/include/asm/bitops.h > > new file mode 100644 > > index 000000000000..8b05d9683571 > > --- /dev/null > > +++ b/arch/loongarch/include/asm/bitops.h > > @@ -0,0 +1,220 @@ > > +/* SPDX-License-Identifier: GPL-2.0 */ > > +/* > > + * Copyright (C) 2020-2021 Loongson Technology Corporation Limited > > + */ > > +#ifndef _ASM_BITOPS_H > > +#define _ASM_BITOPS_H > > + > > +#ifndef _LINUX_BITOPS_H > > +#error only can be included directly > > +#endif > > + > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > + > > +#if _LOONGARCH_SZLONG == 32 > > +#define __LL "ll.w " > > +#define __SC "sc.w " > > +#define __AMADD "amadd.w " > > +#define __AMAND_SYNC "amand_db.w " > > +#define __AMOR_SYNC "amor_db.w " > > +#define __AMXOR_SYNC "amxor_db.w " > > +#elif _LOONGARCH_SZLONG == 64 > > +#define __LL "ll.d " > > +#define __SC "sc.d " > > +#define __AMADD "amadd.d " > > +#define __AMAND_SYNC "amand_db.d " > > +#define __AMOR_SYNC "amor_db.d " > > +#define __AMXOR_SYNC "amxor_db.d " > > +#endif > > + > > +/* > > + * set_bit - Atomically set a bit in memory > > + * @nr: the bit to set > > + * @addr: the address to start counting from > > + */ > > +static inline void set_bit(unsigned long nr, volatile unsigned long *addr) > > +{ > > + int bit = nr % BITS_PER_LONG; > > + volatile unsigned long *m = &addr[BIT_WORD(nr)]; > > + > > + __asm__ __volatile__( > > + " " __AMOR_SYNC "$zero, %1, %0 \n" > > + : "+ZB" (*m) > > + : "r" (1UL << bit) > > + : "memory"); > > +} > > + > > +/* > > + * clear_bit - Clears a bit in memory > > + * @nr: Bit to clear > > + * @addr: Address to start counting from > > + */ > > +static inline void clear_bit(unsigned long nr, volatile unsigned long *addr) > > +{ > > + int bit = nr % BITS_PER_LONG; > > + volatile unsigned long *m = &addr[BIT_WORD(nr)]; > > + > > + __asm__ __volatile__( > > + " " __AMAND_SYNC "$zero, %1, %0 \n" > > + : "+ZB" (*m) > > + : "r" (~(1UL << bit)) > > + : "memory"); > > +} > > + > > +/* > > + * clear_bit_unlock - Clears a bit in memory > > + * @nr: Bit to clear > > + * @addr: Address to start counting from > > + */ > > +static inline void clear_bit_unlock(unsigned long nr, volatile unsigned long *addr) > > +{ > > + clear_bit(nr, addr); > > +} > > + > > +/* > > + * change_bit - Toggle a bit in memory > > + * @nr: Bit to change > > + * @addr: Address to start counting from > > + */ > > +static inline void change_bit(unsigned long nr, volatile unsigned long *addr) > > +{ > > + int bit = nr % BITS_PER_LONG; > > + volatile unsigned long *m = &addr[BIT_WORD(nr)]; > > + > > + __asm__ __volatile__( > > + " " __AMXOR_SYNC "$zero, %1, %0 \n" > > + : "+ZB" (*m) > > + : "r" (1UL << bit) > > + : "memory"); > > +} > > + > > +/* > > + * test_and_set_bit - Set a bit and return its old value > > + * @nr: Bit to set > > + * @addr: Address to count from > > + */ > > +static inline int test_and_set_bit(unsigned long nr, > > + volatile unsigned long *addr) > > +{ > > + int bit = nr % BITS_PER_LONG; > > + unsigned long res; > > + volatile unsigned long *m = &addr[BIT_WORD(nr)]; > > + > > + __asm__ __volatile__( > > + " " __AMOR_SYNC "%1, %2, %0 \n" > > + : "+ZB" (*m), "=&r" (res) > > + : "r" (1UL << bit) > > + : "memory"); > > + > > + res = res & (1UL << bit); > > + > > + return res != 0; > > +} > > + > > +/* > > + * test_and_set_bit_lock - Set a bit and return its old value > > + * @nr: Bit to set > > + * @addr: Address to count from > > + */ > > +static inline int test_and_set_bit_lock(unsigned long nr, > > + volatile unsigned long *addr) > > +{ > > + int bit = nr % BITS_PER_LONG; > > + unsigned long res; > > + volatile unsigned long *m = &addr[BIT_WORD(nr)]; > > + > > + __asm__ __volatile__( > > + " " __AMOR_SYNC "%1, %2, %0 \n" > > + : "+ZB" (*m), "=&r" (res) > > + : "r" (1UL << bit) > > + : "memory"); > > + > > + res = res & (1UL << bit); > > + > > + return res != 0; > > +} > > +/* > > + * test_and_clear_bit - Clear a bit and return its old value > > + * @nr: Bit to clear > > + * @addr: Address to count from > > + */ > > +static inline int test_and_clear_bit(unsigned long nr, > > + volatile unsigned long *addr) > > +{ > > + int bit = nr % BITS_PER_LONG; > > + unsigned long res, temp; > > + volatile unsigned long *m = &addr[BIT_WORD(nr)]; > > + > > + __asm__ __volatile__( > > + " " __AMAND_SYNC "%1, %2, %0 \n" > > + : "+ZB" (*m), "=&r" (temp) > > + : "r" (~(1UL << bit)) > > + : "memory"); > > + > > + res = temp & (1UL << bit); > > + > > + return res != 0; > > +} > > + > > +/* > > + * test_and_change_bit - Change a bit and return its old value > > + * @nr: Bit to change > > + * @addr: Address to count from > > + */ > > +static inline int test_and_change_bit(unsigned long nr, > > + volatile unsigned long *addr) > > +{ > > + int bit = nr % BITS_PER_LONG; > > + unsigned long res; > > + volatile unsigned long *m = &addr[BIT_WORD(nr)]; > > + > > + __asm__ __volatile__( > > + " " __AMXOR_SYNC "%1, %2, %0 \n" > > + : "+ZB" (*m), "=&r" (res) > > + : "r" (1UL << bit) > > + : "memory"); > > + > > + res = res & (1UL << bit); > > + > > + return res != 0; > > +} > > Why is asm-generic/bitops/atomic.h not working for you? It seems that __clear_bit_unlock() needs an additional mb() after the generic version, all other functions are the same as the generic version. Huacai >