Re: preemptible spinlock?

2021-04-22 Thread Max Filippov
On Thu, Apr 22, 2021 at 11:28 PM Wonhyuk Yang  wrote:
> > I think the main idea spinlock disables preemption is,
>
> > the other process that's spinning on the lock can acquire lock.
>
> > but in some implementations of spinlock, like qspinlock in x86 (or mcs 
> > lock),
> > I think there's no need to disable preemption. because processes
> > waiting for lock cannot acquire the lock before the lock holder hand
> > over to other process.
>
> Are you talking about disabling local irq(ex. spin_lock_irqsave)?
>
> If so, think about the situation that a process holding the lock is preempted
> by interrupt. And that interrupt handler tries to grab the spinlock.
> It will lead to deadlock.

Usually the word "preemption" is used when a task switch occurs and one task
preempts another task. Interrupts and tasklets are not usually described with
this word.

Now if an interrupt handler may need to acquire a lock that may also be
acquired by a task then the task must use spin_lock_irq or
spin_lock_irqsave to avoid the possibility of a deadlock. This is documented
in Documentation/kernel-hacking/locking.rst

Going back to the original question:

> > but in some implementations of spinlock, like qspinlock in x86 (or mcs 
> > lock),
> > I think there's no need to disable preemption. because processes
> > waiting for lock cannot acquire the lock before the lock holder hand
> > over to other process.

Imagine what happens if a task acquires a spinlock, gets interrupted,
task switch happens in the interrupt (remember, preemption is enabled),
and the new task tries to acquire the same spinlock on the same CPU?

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: why iowrite32_rep() doesn't work, I have to use the iowrite32() with a loop

2017-11-19 Thread Max Filippov
On Sat, Nov 18, 2017 at 7:36 PM, ayaka  wrote:
> What I want is a relaxed version of the io{read,write}32, as I don't need to
> flush between I am writing the registers table into the registers. I only
> want to flush the cache at the last and isolated register which I will set
> later.

None of these functions do anything with cache. And with
devm_ioremap_resource you will likely get an uncached mapping of your
IO memory, so you don't even need to manage cache.

What you still need to preserve the order of writes is a memory barrier,
and none of io{read,write}32 do it either. You can insert proper barrier
before the write to that last isolated register.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: why iowrite32_rep() doesn't work, I have to use the iowrite32() with a loop

2017-11-18 Thread Max Filippov
On Thu, Nov 16, 2017 at 12:12 PM, ayaka  wrote:
> #if 1
>  for (i = 0; i < count; i++) {
>  u32 *cur = (u32 *)buffer;
>  u32 pos = offset + i * 4;
>
>  cur += i;
>  mpp_debug(DEBUG_SET_REG, "write reg[%03d]: %08x\n",
> pos, *cur);
>  iowrite32(*cur, mpp_dev->reg_base + pos);
> }
> #else
>  iowrite32_rep(mpp_dev->reg_base + offset, buffer, count);
> mb();
>  for (i = 0; i < count; i++) {
>  u32 cur = 0;
>  u32 pos = offset / 4 + i;
>
>  cur = ioread32(mpp_dev->reg_base + pos * 4);
>  pr_info("get reg[%03d]: %08x\n", pos, cur);
> }
> #endif

The loop with iowrite32 writes consecutive u32 words from buffer
to consecutive IO memory locations. But iowrite32_rep writes
consecutive u32 words from buffer to the same IO memory location.
So they do different things when count > 1.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: sleep

2017-11-14 Thread Max Filippov
On Tue, Nov 14, 2017 at 8:52 AM, Bruno E. O. Meneguele
 wrote:
> What
> confused me for sometime was the 'atomic' vs 'interrupt' naming, but
> after reading Robert's book it cames to the fact that they're the same.

Not exactly the same. Atomic means you're protected from some sort of
interruption: e.g. you raise preemption counter and you're protected from
scheduling, but you still may be interrupted, or you disable interrupts and
you're protected from scheduling and interrupts, but there still may be an
NMI.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: is there any more to having a single interrupt drive multiple handlers than IRQF_SHARED?

2017-10-26 Thread Max Filippov
On Thu, Oct 26, 2017 at 2:17 PM,   wrote:
> On Thu, 26 Oct 2017 16:32:42 -0400, "Robert P. J. Day" said:
>
>>   now, i do realize that it can be used along with a unique dev_id
>> values to isolate a *particular* handler amongst a group of handlers,
>> but if one simply wants to trigger *all* handlers registered for that
>> interrupt, is there anything about that that's tricky?
>
> You mean, other than the fact that multiple handlers for the same device
> damned well be coded to know that, and be aware of each other?

They don't have to be aware of each other, only about the fact that their
IRQ may be shared. And they have to be aware of that because they've
registered their ISR with IRQF_SHARED flag.

>  Locking, etc.
> and all the other Bad Juju that can happen when multiple drivers are all
> acting on one device.

Not on one device, each handler will check registers of its own device,
to see if it triggered the IRQ.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: qemu problem

2015-04-19 Thread Max Filippov
On Sun, Apr 19, 2015 at 5:36 PM, Mustafa Hussain
 wrote:
> @max i didn't understand, what i should type exactly to run qemu?

-net user,hostfwd=tcp::1-10.0.2.15:22

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: qemu problem

2015-04-18 Thread Max Filippov
On Sun, Apr 19, 2015 at 6:38 AM, Max Filippov  wrote:
> On Sun, Apr 19, 2015 at 5:14 AM, Mustafa Hussain
>  wrote:
>> Hi,
>> i'm trying to run kernel with qemu but i get some errors while running this
>> command
>> "
>> qemu-system-x86_64  --nographic -M pc -no-reboot  -net nic,model=e1000  -net
>> nic,vlan=0 -net user,hostfwd=tcp:1:10.0.2.15:22-hda hd.img -append
>> "root=/dev/hda1 debug rw console=ttyS0,115200" -s -kernel
>> "
>> error says that
>> qemu-system-x86_64: -net user,hostfwd=tcp:1:10.0.2.15:22: invalid host
>> forwarding rule 'tcp:1:10.0.2.15:22'
>
> From 'man QEMU' hostfwd has the following format:
>
> hostfwd=[tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport
>
> but you have a colon instead of a dash.

...and you're missing an additional colon before the first port number.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: qemu problem

2015-04-18 Thread Max Filippov
On Sun, Apr 19, 2015 at 5:14 AM, Mustafa Hussain
 wrote:
> Hi,
> i'm trying to run kernel with qemu but i get some errors while running this
> command
> "
> qemu-system-x86_64  --nographic -M pc -no-reboot  -net nic,model=e1000  -net
> nic,vlan=0 -net user,hostfwd=tcp:1:10.0.2.15:22-hda hd.img -append
> "root=/dev/hda1 debug rw console=ttyS0,115200" -s -kernel
> "
> error says that
> qemu-system-x86_64: -net user,hostfwd=tcp:1:10.0.2.15:22: invalid host
> forwarding rule 'tcp:1:10.0.2.15:22'

>From 'man QEMU' hostfwd has the following format:

hostfwd=[tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport

but you have a colon instead of a dash.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: ternary vs double exclamation

2014-12-29 Thread Max Filippov
On Tue, Dec 30, 2014 at 3:25 AM, Vinícius Tinti  wrote:
> I was looking the kernel source code and there are a lot of places in
> which either "(expression) ? 1 : 0" or "(expression) ? 0 : 1" appear.
> As fair as I can tell both can be replaced by "!!expression" and
> "!expression".
>
> Moreover there it seems that using "!!" does not add a "nopl"
> instruction at the end of the call. Does anybody knows why?

It seems that the nop instruction is inserted for alignment, and if you
reverse the order of functions in your c source, nop will still be
inserted between them.

>  :
>0:   31 c0   xor%eax,%eax
>2:   85 ff   test   %edi,%edi
>4:   0f 95 c0setne  %al
>7:   c3  retq
>8:   0f 1f 84 00 00 00 00nopl   0x0(%rax,%rax,1)
>f:   00
>
> 0010 :
>   10:   31 c0   xor%eax,%eax
>   12:   85 ff   test   %edi,%edi
>   14:   0f 95 c0setne  %al
>   17:   c3  retq

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: the cost of inlining?

2014-12-05 Thread Max Filippov
On Fri, Dec 5, 2014 at 4:32 AM, Jeff Haran  wrote:
> Disassembly of section .text:
>
>  :
> #include 
> #include 
>
> int samp_atomic_read(atomic_t *v)
> {
>0:   55  push   rbp
>1:   48 89 e5movrbp,rsp
>4:   e8 00 00 00 00  call   9 
>  *
>  * Atomically reads the value of @v.
>  */
> static inline int atomic_read(const atomic_t *v)
> {
> return v->counter;
>9:   8b 07   moveax,DWORD PTR [rdi]
> int val;
>
> val = atomic_read(v);
> return val;
> }
>b:   c9  leave
>c:   c3  ret
>d:   90  nop
>e:   90  nop
>f:   90  nop

[...]

> But what is that call instruction at offset 4 for?

Looks like you have ftrace enabled in your kernel config, and this call is
a call to _mcount.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: [PATCH] staging: Check for Null return of allocated skb in fw_download_code

2014-08-13 Thread Max Filippov
On Wed, Aug 13, 2014 at 10:03 PM, Nick Krause  wrote:
> I did test my patch by doing a kernel build and I get this error,
> drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c:66:4: error:
> implicit declaration of function ‘skb_quene_purge’
> [-Werror=implicit-function-declaration]
> skb_quene_purge(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]);
> I am wondering how do I fix this, I will attach my patch so I can fix
> this out and send a proper patch :).

Typo, skb_queue_purge.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: [PATCH] staging: Check for Null return of allocated skb in fw_download_code

2014-08-13 Thread Max Filippov
On Wed, Aug 13, 2014 at 5:56 PM,   wrote:
> On Tue, 12 Aug 2014 22:53:37 -0700, Manish Katiyar said:
>
>> And it may also be a good idea to post the logs regarding how you tested
>> your patch and verified that the fix works as expected. As you are asking
>
> I want to see how he sets up an environment where he can *trigger* the issue
> reliably. :)

No need to trigger it, faking it would be enough, e.g.:

-   skb  = dev_alloc_skb(frag_length + 4);
+{
+static int i;
+if (++i < 3)
+skb  = dev_alloc_skb(frag_length + 4);
+else
+skb = NULL;
+}
+if (skb == NULL) {
+rt_status =  false;
+break;
+}

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: head.S

2014-06-02 Thread Max Filippov
Hi,

On Mon, Jun 2, 2014 at 7:53 PM, Saurabh Jain  wrote:
> I am trying to trace Linux kernel booting process for ARM architecture.
> Right now i am doing it manually . I am getting problem in reading assembly
> codes (like in head.s and other files) . Can any body tell me the correct
> way of tracing the linux kernel booting process ? Is there any guide which
> perfectly document Linux kernel files function by function ?

Not *the* (supposedly only) correct way, just one possible way is to use
gdb connected to qemu to step through your kernel code. Google suggests
the following, which looks pretty decent:
http://files.meetup.com/1590495/debugging-with-qemu.pdf

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: two virtual address pointing to same physical address

2014-05-20 Thread Max Filippov
Hi,

On Tue, May 20, 2014 at 6:28 AM,   wrote:
> On Tue, 20 May 2014 00:39:26 -, Chan Kim said:
>> But still it's confusing. Can two virtual addresses from the "same process"
>> (in init process, one for nocache pool, the other not) point to the same
>> physical address?
>
> I'm not sure what you're trying to say there.  In general, the hardware
> tags like non-cacheable and write-combining are applied to physical addresses,
> not virtual.

AFAIK most processors with MMU have cache control bits built into page
table entries, effectively controlling caching by virtual address.
E.g. x86 has Write Through and Cache Disabled bits in its PTEs,
ARM PTEs have bits B and C that determine caching and buffering
of the page, etc.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: hook SIGSEGV

2014-05-14 Thread Max Filippov
On Wed, May 14, 2014 at 4:14 PM, Kristof Provost  wrote:
> On 2014-05-10 21:46:01 (+0800), net.study@gmail.com 
>  wrote:
>>  I want to know is it possible to hook SIGSEGV to restart the
>>  thread which the signal is sent to,without restart the whole
>>  process? And record the place where has caused this signal?
>>
> Yes, as others have already pointed out, you can hook SIGSEGV like any
> other signal.
>
> You're not going to be able to save the process any more, but you can
> still collect some useful information.
>
> I've found it very useful to have a SIGSEGV (and SIGPIPE, SIGABRT,
> SIGFPE, SIGILL) handler which logs a backtrace (look at 'man backtrace')
> to syslog. Very useful for debugging on targets where core dumps are
> impractical.
>
> Others have also pointed out that it might no longer be safe to call
> printf() or malloc() there. That's true, but usually it's OK, and if it
> turns out that it wasn't ... Well, you were crashing anyway.

Well, not anyway: you still should be able to take a longjmp out of the
signal handler to a safe place.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: how can I get the ZONE_DMA size ??

2014-05-12 Thread Max Filippov
On Mon, May 12, 2014 at 11:14 AM, RS  wrote:
> So, how can I get the ZONE_DMA real size ? Is there any kernel functions
> or configuration files ? I want to get the accurate number.

Take a look at the /proc/zoneinfo file in your running kernel.
It will tell you where each zone starts ('start_pfn' lines) and how many
pages there are ('pages spanned' lines).

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Update kernel source to latest?

2014-03-29 Thread Max Filippov
On Sun, Mar 30, 2014 at 4:51 AM, habisravi  wrote:
> I cloned the linux kernel source from 
> "git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git". When i 
> type "git tag" in the source folder i see version up to "v3.9-rc8", but 
> kernel.org shows latest version is 3.14-rc8. How can i update my source 
> repository to the latest.

Tags are sorted lexicographically, so v3.14-rc8 is < v3.9-rc8. Scroll
up or pipe to less.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Kernel panic when using initramfs instead of initrd

2013-05-06 Thread Max Filippov
Hi Ivan,

On Sun, May 5, 2013 at 1:44 PM, Ivan Nikolaev  wrote:
...
> EVERYTHING WORKS if I enable in the kernel some filesystem (like ext2 or
> squashfs) and create the initrd image of that type and put inside the
> contents of the directory. Isolinux then boots the kernel this way:
>
> LABEL linux
> SAY Booting linux...
> KERNEL /vmlinuz
> APPEND root=/dev/ram0 initrd=/initrd.gz console=ttyS0,38400 vga=0x305
>
> To make this work, I obviously enabled the "Initial RAM filesystem and RAM
> disk (initramfs/initrd) support" in "General setup" and the "RAM block
> device support" in "Device drivers - block devices" options.
>
> Well, ensuring everything works, I tried to switch to initramfs. Disabled
> the "RAM block device support", the ext2 and squashfs filesystems and
> created the init cpio image with a command like this:
>
> find | cpio -H newc -o | gzip -9 > initrd.gz
>
> The boot arguments are the same. When I boot, what I have is a kernel panic
> with the following details:
>
> List of all partitions:
> No filesystem could mount root, tried:
> Kernel panic - not syncing: VFS: Unable to mount root fs on
> unknown-block(0,0)
>
> Here's my config file: http://pastebin.com/XVd8ZukU

Empty CONFIG_INITRAMFS_SOURCE parameter in your config
should actually look like

CONFIG_INITRAMFS_SOURCE="/path/to/your/initrd.gz"

given the command you use for initramfs creation, because initramfs
image is linked into the vmlinuz at the build time.

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: simple question about the function memcmp in kernel

2013-04-07 Thread Max Filippov
On Mon, Apr 8, 2013 at 5:33 AM,   wrote:
> On Mon, 08 Apr 2013 08:57:01 +0800, Ben Wu said:
>
>> int memcmp(const void *cs, const void *ct, size_t count)
>> {
>
>> I want to know why it use the temp pointer su1, su2? why it doesn't directly
>> use the cs and ct pointer?
>
> This is a C 101 question, not a kernel question.  But anyhow..
>
> They're declared const, so the compiler will whine about ++'ing them.

const is the the object they point to, not the pointers themselves
(that would be
void * const cs).

memcmp compares bytes at which cs and ct point, but these are void pointers,
and the expression res = *cs - *ct is thus meaningless. One must convert them
to (const unsigned char *), which looks ugly, otherwise such implementation
looks like pretty much valid:

int memcmp(const void *cs, const void *ct, size_t count)
{
 int res = 0;

 for (; 0 < count; ++cs, ++ct, count--)
  if ((res =*(const unsigned char *)cs - *(const unsigned char
*)ct) != 0)
   break;
 return res;
}

-- 
Thanks.
-- Max

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies